customerId === null) { return; } DB::transaction(function () use ($data) { DB::table('invoices')->updateOrInsert( ['legacy_id' => $data->legacyId], [ 'invoice_number' => $data->invoiceNumber, 'date' => $data->date, 'customer_id' => $data->customerId, 'order_id' => $data->orderId, 'sub_total' => $data->subTotal, 'discount_amount' => $data->discountAmount, 'tax' => $data->tax, 'total' => $data->total, 'amount_paid' => $data->amountPaid, 'status' => $data->status, 'payment_method' => $data->paymentMethod, 'due_date' => $data->dueDate, 'paid_at' => $data->paidAt, 'note' => $data->note, 'created_at' => $data->createdAt, 'updated_at' => $data->createdAt, ] ); $invoiceId = DB::table('invoices')->where('legacy_id', $data->legacyId)->value('id'); foreach ($data->items as $item) { DB::table('invoice_items')->updateOrInsert( ['legacy_id' => $item->legacyId], [ 'invoice_id' => $invoiceId, 'product_id' => $item->productId, 'type' => $item->type, 'description' => $item->description, 'quantity' => 1, 'unit_price' => $item->unitPrice, 'total_price' => $item->totalPrice, ] ); } }); } }