Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/demo-catatan-keuangan.aplikasipos.info/app/Imports/
Upload File :
Current File : /home/aplikasiposinfo/www/demo-catatan-keuangan.aplikasipos.info/app/Imports/BudgetImport.php

<?php

namespace App\Imports;

use App\Models\Budget;
use App\Models\Divisi;
use App\Models\FormatKodeBudget;
use App\Models\Perusahaan;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Validators\ValidationException;
use Illuminate\Support\Facades\Validator;
use Session, Str, Auth, DB;
use \Carbon\Carbon;
use Exception;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class BudgetImport implements ToCollection,WithEvents,WithCalculatedFormulas
{

    public $divisi;
    public $perusahaan;
    public function __construct()
    {
        // $this->divisi = $divisi;
        // $this->perusahaan = $perusahaan;
    }
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    // public function model(array $row)
    // {
    //      Validator::make($row,[
    //          '*.0' => 'required',
    //      ],[
    //         '*.0.required'=>'baris pertama pada file tidak boleh kosong',
    //      ])->validate();

    //     return new Budget([
    //         'kode_budget'=>$row[0],
    //     ]);
    // }
    public function collection(Collection $rows)
    {
        //  Validator::make($rows->toArray(), [
        //      '*.0' => 'required',
        //  ],[
        //     '*.0.required'=>'baris pertama pada file tidak boleh kosong',
        //  ])->validate();
        
        try {
            foreach ($rows as $key => $value) {
                
                if (count($value) > 3 && trim($value[0]) != "" && $value[0] != null && isset($value[1]) && $value[1] != "" && $value[1] != null && isset($value[2]) && $value[2] != "" && $value[2] != null && trim($value[0]) != "Kode Departemen" && trim($value[0]) != "Format Kode") {
                    // $divisi = Auth::user()->divisi->divisi_id;
                    // $divisi = Divisi::where('kode_divisi', $value[0])->first();
                    // if ($divisi == null) {
                    //     DB::rollback();
                    //     throw new Exception("Kode Divisi : ".$value[0]." tidak ditemukan");

                    //     // return redirect()->back()->withErrors('Kode Divisi tidak ditemukan');
                    // }
                    // $perusahaan = Perusahaan::where('kode_perusahaan', $value[1])->first();
                    // if ($perusahaan == null) {
                    //     DB::rollback();
                    //     throw new Exception("Kode Perusahaan tidak ditemukan");
                    //     // return redirect()->back()->withErrors('Kode Perusahaan tidak ditemukan');
                    // }
                    // $formatKode = FormatKodeBudget::where('departemen_id', $divisi->id)
                    //     ->whereHas('perusahaans', function ($wp) use ($perusahaan) {
                    //         $wp->where('perusahaan_id', $perusahaan->id);
                    //     })->first();
                    // if ($formatKode == null) {
                    //     DB::rollback();
                    //     throw new Exception("Format Kode Budget tidak ditemukan");
                    // }
                    DB::beginTransaction();
                    $formatKode = FormatKodeBudget::whereHas('perusahaans')->where('kode', $value[0])->first();
                    if(!$formatKode){
                        DB::rollback();
                        throw new Exception("Format Kode Budget tidak ditemukan : ".$value[0]." - ".$value[1]);
                    }
                    //list bulan indo
                    $bulans = [
                        'januari' => 1,
                        'februari' => 2,
                        'maret' => 3,
                        'april' => 4,
                        'mei' => 5,
                        'juni' => 6,
                        'juli' => 7,
                        'agustus' => 8,
                        'september' => 9,
                        'oktober' => 10,
                        'november' => 11,
                        'desember' => 12,
                    ];
                    
                    $bulan = (int) $bulans[strtolower($value[3])];

                    $tahun = (int) $value[4];
                    $tanggal = null;
                    if($value[6] != null && $value[6] != ""){
                        if(is_int($value[6])){
                            $tanggal = Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value[6]));
                        } else {
                            $tanggal = Carbon::createFromFormat('d/m/Y', $value[6]);
                        }
                    }
                    try {
                        $last_budget = Budget::where('format_kode_budget_id', $formatKode->id)->orderBy('id', 'desc')->first();
          
                        if ($last_budget) {
                            $total_budget = (int) str_replace($formatKode->format_kode, '', $last_budget->kode_budget);
                        } else {
                            $total_budget = 0;
                        }
                        $total_budget = str_pad($total_budget + 1, 4, '0', STR_PAD_LEFT);
                        $kode_budget = $formatKode->format_kode . $total_budget;
                        Budget::create([
                            'kode_budget' => $kode_budget,
                            'nama_anggaran' => $value[1],
                            'balance' => Str::remove(',', number_format(Str::remove('Rp', Str::replace(',', ".",$value[2])),3)),
                            'jumlah_anggaran' => Str::remove(',', number_format(Str::remove('Rp', Str::replace(',', ".",$value[2])),3)),
                            'budget_bulan' => $bulan,
                            'budget_tahun' => $tahun,
                            'divisi_id' => $formatKode->departemen_id,
                            'perusahaan_id' => $formatKode->perusahaans[0]->id,
                            'keterangan_realisasi' => $value[5] ?? null,
                            'tanggal_realisasi' =>  $tanggal,
                            'format_kode_budget_id'=>$formatKode->id
                        ]);
                        DB::commit();
                    } catch (\Throwable $th) {
                        DB::rollback();
                        throw new Exception("Ada masalah saat mengimport data : ".$th->getMessage());
                    }
                }
                
            }
           

        } catch (Exception $e) {
            // DB::rollback();
            throw new Exception($e->getMessage());
            // return redirect()->back()->withErrors('Error :'.$e->getMessage());
        }
    }

    public function registerEvents(): array
    {
        return [	
            AfterSheet::class => [self::class, 'afterSheet'],
                        
        ];
    }
    
    public static function afterSheet(AfterSheet $event) 
    {   
        //dd(NumberFormat::FORMAT_CURRENCY_USD);
        // dd($event->getSheet()->getStyle('D16:D17')->getNumberFormat()->getFormatCode());
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]