findByImportIdAndName($retainerInvoice->client_order_id, $importName)) { /** @var RetainerInvoiceDetail[] */ $details = []; foreach ($retainerInvoice->details as $detail) { $details[] = (new RetainerInvoiceDetailAttr) ->setName($detail->item_name) ->setDescription($detail->description) ->setRate($detail->rate) ->setTaxRate((int)$detail->rate * ($detail->tax / 100)) ->setQty($detail->qty) ->setSubtotal((int)$detail->amount); } $newRetainerInvoice = $retainerInvoiceFacade->handleSaveRetainerInvoice( (new RetainerInvoiceAttr) ->setInvoiceDate($retainerInvoice->date) ->setCustomerId($customer->id) ->setRetainerInvoiceDetailAttrs($details) ->setStatus($retainerInvoice->status == 'send' ? 'sent' : $retainerInvoice->status) ); if ($retainerInvoice->payment_receiveds) { foreach ($retainerInvoice->payment_receiveds as $payment) { if ($coa = ChartOfAccount::whereImportId($payment->coa_id, $service->id)->first()) { $paymentFacade->handleSavePayment( (new PaymentAttr) ->setPaymentFor('retainer_invoice') ->setCustomerId($customer->id) ->setChartOfAccountId($coa->id) ->setPaymentDate($payment->date) ->setNotes($payment->notes) ->setPaymentDetailAttrs( [ (new PaymentDetailAtrr) ->setRetainerInvoiceId($newRetainerInvoice->id) ->setPaymentAmount($payment->amount) ] )->setAmount((int)$payment->amount) ); } } } } } }); } }