purchaseOrderFacade = $purchaseOrderFacade; $this->warehouseRepositoryInterface = $warehouseRepositoryInterface; $this->billRepositoryInterface = $BillRepositoryInterface; $this->billFacade = $billFacade; $this->paymentFacade = $paymentFacade; } private function createPayments($payments, $newPurchaseOrder, $supplier, $service) { foreach ($payments as $payment) { if ($coa = ChartOfAccount::whereImportId($payment->coa_id, $service->id)->first()) { $this->paymentFacade->handleSavePayment( (new PaymentAttr) ->setPaymentFor('po') ->setSupplierId($supplier->id) ->setChartOfAccountId($coa->id) ->setPaymentDate($payment->date) ->setNotes($payment->notes) ->setPaymentDetailAttrs( [ (new PaymentDetailAtrr) ->setPurchaseOrderId($newPurchaseOrder->id) ->setPaymentAmount($payment->amount) ] ) ->setAmount($payment->amount) ); } } } public function run() { ImportSeeder::import('purchase_orders', function ($purchaseOrders, $a, $service) { foreach ($purchaseOrders as $purchaseOrder) { if ($supplier = Supplier::where('import_id', $purchaseOrder->supplier_id)->first()) { $warehouse = $this->warehouseRepositoryInterface->findByName('Jakarta'); if (!$warehouse) { $warehouse = $this->warehouseRepositoryInterface->create( (new WarehouseAttr) ->setName('Jakarta') ); }; $details = []; foreach ($purchaseOrder->details as $detail) { $details[] = (new ItemPurchaseOrderAttr) ->setItemId(Item::where('import_id', $detail->item_id)->first()->id) ->setDescription($detail->description) ->setRate($detail->rate) ->setTaxRate($detail->tax) ->setQty($detail->qty) ->setSubtotal($detail->amount ?? 0); } $newPurchaseOrder = $this->purchaseOrderFacade->handleSavePurchaseOrder( (new PurchaseOrderAttr) ->setTransactionDate($purchaseOrder->date) ->setDeliveryDate($purchaseOrder->delivery_date) ->setSupplierId($supplier->id) ->setWarehouseId($warehouse->id) ->setItemPurchaseOrderAttrs($details) ->setStatus($purchaseOrder->status === 'send' ? 'received' : $purchaseOrder->status) ); $this->createPayments($purchaseOrder->payment_mades, $newPurchaseOrder, $supplier, $service); if ($bill = $purchaseOrder->bill) { $newBill = $this->billRepositoryInterface->create( (new BillAttr) ->setBillDate($bill->date) ->setDueDate($bill->due_date) ->setPurchaseOrderId($newPurchaseOrder->id) ->setAmount($bill->amount) ->setStatus($bill->status === 'send' ? 'open' : 'draft'), $this->billFacade->generateNoBill() ); if ($payment = $bill->payment) { if ($coa = ChartOfAccount::whereImportId($bill->payment->coa_id, $service->id)->first()) { $this->paymentFacade->handleSavePayment( (new PaymentAttr) ->setPaymentFor('bill') ->setSupplierId($supplier->id) ->setChartOfAccountId($coa->id) ->setPaymentDate($payment->date) ->setNotes($payment->notes) ->setPaymentDetailAttrs( [ (new PaymentDetailAtrr) ->setBillId($newBill->id) ->setPaymentAmount($payment->amount) ] ) ->setAmount($payment->amount) ); } } } } } }); } }