Created
April 1, 2022 04:45
-
-
Save bogordesaincom/6f413210e6d144a7014f229375578828 to your computer and use it in GitHub Desktop.
Purchase Import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Imports\Atom; | |
use Maatwebsite\Excel\Concerns\Importable; | |
use Maatwebsite\Excel\Concerns\RegistersEventListeners; | |
use Maatwebsite\Excel\Concerns\ToModel; | |
use Maatwebsite\Excel\Concerns\WithChunkReading; | |
use Maatwebsite\Excel\Concerns\WithEvents; | |
use Maatwebsite\Excel\Concerns\WithHeadingRow; | |
use Maatwebsite\Excel\Concerns\WithValidation; | |
use Maatwebsite\Excel\Validators\ValidationException; | |
use Modules\Atom\Entities\AtomInvoice; | |
use Modules\Atom\Entities\AtomInvoiceProduct; | |
use Modules\Atom\Entities\AtomProduct; | |
use Modules\Atom\Entities\AtomUnit; | |
use Modules\People\Entities\Customer; | |
use Modules\Warehouse\Entities\Warehouse; | |
use PhpOffice\PhpSpreadsheet\Shared\Date; | |
class AtomInvoiceImport implements ToModel, WithHeadingRow, WithEvents, WithValidation, WithChunkReading | |
{ | |
use Importable, RegistersEventListeners; | |
/** | |
* @param Model $model | |
*/ | |
public function model(array $row) | |
{ | |
try { | |
$customer = Customer::query()->where('name', 'LIKE', "%{$row['customer']}%")->first(); | |
$warehouse = Warehouse::query()->where('name', 'LIKE', "%{$row['warehouse']}%")->first(); | |
$date = Date::excelToDateTimeObject($row['tanggal'])->format('Y-m-d'); | |
$due_date = Date::excelToDateTimeObject($row['jatuh_tempo'])->format('Y-m-d'); | |
$nama_item = AtomProduct::query()->where('name', 'LIKE', "%{$row['nama_item']}%")->first(); | |
$unit = AtomUnit::query()->where('name', 'LIKE', "%{$row['unit']}%")->first(); | |
$invoice_exist = AtomInvoice::where('reference_no', 'LIKE', "{$row['no_referensi']}")->first(); | |
$invoice_last = AtomInvoice::latest()->first(); | |
if (!empty($invoice_last)) { | |
$no_last = $invoice_last->id + 1; | |
} else { | |
$no_last = 1; | |
} | |
$generate_no = 'ATOM-INV/'.str_pad($no_last, 7, 0, STR_PAD_LEFT).''; | |
if (empty($invoice_exist)) { | |
$invoice = AtomInvoice::create([ | |
'no' => $generate_no, | |
'customer_id' => $customer->id, | |
'warehouse_id' => $warehouse->id, | |
'reference_no' => $row['no_referensi'], | |
'date' => $date, | |
'due_date' => $due_date, | |
'tax' => $row['ppn'], | |
'discount' => $row['diskon_pembelian'], | |
'shipping' => $row['shipping'], | |
'total' => $row['total'], | |
'grand_total' => $row['grand_total'], | |
'status' => $row['status'], | |
'note' => $row['catatan'], | |
]); | |
} else { | |
$invoice = AtomInvoice::find($invoice_exist->id); | |
} | |
$invoice_product = AtomInvoiceProduct::create([ | |
'invoice_id' => $invoice->id, | |
'product_id' => $nama_item->id, | |
'qty' => $row['qty'], | |
'unit_id' => $unit->id, | |
'price' => $row['harga'], | |
'discount_item' => $row['diskon_item'], | |
'sub_total' => $row['sub_total'], | |
]); | |
$invoice->products()->attach($invoice_product->id); | |
return $invoice; | |
} catch (ValidationException $e) { | |
$failures = $e->failures(); | |
foreach ($failures as $failure) { | |
$failure->row(); // row that went wrong | |
$failure->attribute(); // either heading key (if using heading row concern) or column index | |
$failure->errors(); // Actual error messages from Laravel validator | |
$failure->values(); // The values of the row that has failed. | |
} | |
} | |
} | |
public function rules(): array | |
{ | |
return [ | |
'no_referensi' => 'required', | |
'nama_item' => 'required', | |
'qty' => 'required', | |
'unit' => 'required', | |
'harga' => 'required', | |
'sub_total' => 'required', | |
// 'total' => 'required', | |
]; | |
} | |
public function customValidationMessages() | |
{ | |
return [ | |
'no_referensi.required' => ':attribute Wajib Diisi .', | |
'nama_item.required' => ':attribute Wajib Diisi.', | |
'qty.required' => ':attribute Wajib Diisi.', | |
'unit.required' => ':attribute Wajib Diisi.', | |
'harga.required' => ':attribute Wajib Diisi.', | |
'sub_total.required' => ':attribute Wajib Diisi.', | |
]; | |
} | |
public function chunkSize(): int | |
{ | |
return 1000; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment