paymentFacade = $paymentFacade; $this->invoiceFacade = $invoiceFacade; } private function applyFromRetainer($amount, $newInvoice) { $retainer = RetainerInvoice::paid()->where( 'customer_id', $newInvoice->customer_id )->get()->filter(function ($item) { return (int) $item->available_amount > 0; })->first(); if ($retainer) { $this->invoiceFacade->applyFromRetainers( [ (new InvoiceRetInvoiceAttr) ->setInvoiceId($newInvoice->id) ->setRetainerInvoiceId($retainer->id) ->setAmount($amount) ] ); } } private function createPayments($payments, $newInvoice, $customer, $service) { foreach ($payments as $payment) { if ($coa = ChartOfAccount::whereImportId($payment->coa_id, $service->id)->first()) { $taxDeducated = null; if ($payment->tax_deducated) { if ($coa = ChartOfAccount::whereImportId($payment->tax_deducated->coa_id, $service->id)->first()) { $taxDeducated = (new TaxDeducatedAttr) ->setChartOfAccountId($coa->id) ->setAmount($payment->tax_deducated->amount); } } $this->paymentFacade->handleSavePayment( (new PaymentAttr) ->setPaymentFor('invoice') ->setCustomerId($customer->id) ->setChartOfAccountId($coa->id) ->setPaymentDate($payment->date) ->setNotes($payment->notes) ->setPaymentDetailAttrs( [ (new PaymentDetailAtrr) ->setInvoiceId($newInvoice->id) ->setPaymentAmount($payment->amount) ] ) ->setAmount($payment->amount) ->setTaxDeducatedAttr($taxDeducated) ); } } } /** * Run the database seeds. * * @return void */ public function run( SaleOrderFacade $saleOrderFacade, InvoiceRepositoryInterface $invoiceRepositoryInterface, WarehouseRepositoryInterface $warehouseRepositoryInterface, CustomerRepositoryInterface $customerRepositoryInterface ) { ImportSeeder::import('invoices', function ($invoices, $importName, $service) use ($saleOrderFacade, $warehouseRepositoryInterface, $invoiceRepositoryInterface, $customerRepositoryInterface) { foreach ($invoices as $invoice) { $customerName = explode(" | ", $invoice->customer); if ($customer = $customerRepositoryInterface->findByName($customerName)) { /** @var ItemSaleOrderAttr[] */ $items = []; foreach ($invoice->details as $detail) { $alreadyAdded = false; foreach ($items as $item) { if ($item->getImportId() == $detail->item_id) { $alreadyAdded = true; break; } } if (!$alreadyAdded) { $items[] = (new ItemSaleOrderAttr) ->setImportId($detail->item_id) ->setItemId(Item::where('import_id', $detail->item_id)->first()->id) ->setDescription($detail->description) ->setRate($detail->amount) ->setTaxRate($detail->tax) ->setQty($detail->qty) ->setSubtotal($detail->amount * $detail->qty); } } $warehouse = $warehouseRepositoryInterface->findByName('Jakarta'); if (!$warehouse) { $warehouse = $warehouseRepositoryInterface->create( (new WarehouseAttr) ->setName('Jakarta') ); }; $saleOrder = $saleOrderFacade->handleSaveSaleOrder( (new SaleOrderAttr) ->setTransactionDate($invoice->date) ->setDeliveryDate($invoice->due_date) ->setCustomerId($customer->id) ->setWarehouseId($warehouse->id) ->setItemSaleOrderAttrs($items) ->setStatus('sent') ); $total = 0; foreach (ItemSaleOrder::where('sale_order_id', $saleOrder->id)->get() as $item) { $total += $item->subtotal; } $newInvoice = $invoiceRepositoryInterface->create( (new InvoiceAttr) ->setInvoiceDate($invoice->date) ->setDueDate($invoice->due_date) ->setSaleOrderId($saleOrder->id) ->setAmount($total) ->setStatus($invoice->status === 'send' ? 'sent' : $invoice->status) ->setTermId($invoice->term_id ? (Term::where('import_id', $invoice->term_id)->first()->id ?? null) : null), $this->invoiceFacade->generateNoInvoice() ); // if ($newInvoice->id === 1272) { // dd($total, $items); // } if ($invoice->payment_receiveds) { $this->createPayments( $invoice->payment_receiveds, $newInvoice, $customer, $service ); } if ($invoice->apply_amount) { $this->applyFromRetainer($invoice->apply_amount, $newInvoice); } } } }); } }