Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/hayyumart.com/app/Imports/
Upload File :
Current File : /home/aplikasiposinfo/www/hayyumart.com/app/Imports/RotiImport.php

<?php

namespace App\Imports;

use App\Roti;
use App\HargaRoti;
use App\Pelanggan;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class RotiImport implements ToCollection
{
    
    public $error = [];

    public function collection(Collection $rows)
    {
        $baris = 1;
        foreach ($rows as $row) {

            try {
                if($row[0] && $row[0] != "Barcode"){
                    $roti = Roti::where('nm_roti',$row[1])->first();
                    $pelanggan = Pelanggan::where('nm_pelanggan',$row[4])->first();
                    if($pelanggan){
                        $kd_pelanggan = $pelanggan->kd_pelanggan;
                    } else {
                        $pelanggan = Pelanggan::create([
                            'kd_pelanggan'=>"P-".\Helper::strtotime().$baris,
                            'sinkron' => '0',
                            'nm_pelanggan' => $row[4],
                        ]);
        
                        $kd_pelanggan = $pelanggan->kd_pelanggan;
                    }
                    
                    if ($roti) {
                        $roti->update([
                            'nm_roti'=>$row[1],
                            'barcode'=>$row[0],
                            'stok'=>$row[2],
                            'harga_beli'=>$row[3],
                        ]);
                        if($row[5]){
                            
                            if (count($roti->harga_roti) > 0) {
                                foreach ($roti->harga_roti as $hr) {
                                    $row_5 = \Helper::money($row[5]);

                                    // Jika Harga Member Sudah Tersedia, maka update
                                    if ($hr->kd_pelanggan == $kd_pelanggan) {
                                        $hr->update([
                                            'harga_jual' => $row_5,
                                        ]);
                                    }else{
                                        $harga = HargaRoti::create([
                                            'kd_roti' => $roti->kd_roti,
                                            'kd_pelanggan' => $kd_pelanggan,
                                            'harga_jual' => $row_5,
                                        ]);
                                    }
                                }
                            }else{
                                $row_5 = \Helper::money($row[5]);

                                HargaRoti::create([
                                    'kd_roti' => $roti->kd_roti,
                                    'kd_pelanggan' => $kd_pelanggan,
                                    'harga_jual' => $row_5,
                                ]);
                            }
                        }
                    }else{
                        $row_5 = \Helper::money($row[5]);
                        $roti = Roti::create([
                            'kd_roti'=> "RTI-".\Helper::strtotime().$baris,
                            'nm_roti'=>$row[1],
                            'barcode'=>$row[0],
                            'stok'=>$row[2],
                            'harga_beli'=>$row[3],
                        ]);
                        
                        HargaRoti::create([
                            'kd_roti' => $roti->kd_roti,
                            'kd_pelanggan' => $kd_pelanggan,
                            'harga_jual' => $row_5,
                        ]);
                    }
                }
            } catch (\Throwable $th) {
                array_push($this->error, 'Terjadi Error Gambar Pada  Excel Baris <b> ' . $baris . '  </b> ' . $th->getMessage());
            }

            $baris++;
        }
        
    }
}

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