62 lines
2.2 KiB
PHP
62 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Writers;
|
|
|
|
use App\Integrations\Whmcs\DTOs\InvoiceData;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class InvoiceWriter
|
|
{
|
|
/**
|
|
* Skips invoices whose WHMCS client couldn't be resolved to a local customer —
|
|
* invoices.customer_id is NOT NULL, so there is nothing valid to insert.
|
|
*/
|
|
public function write(InvoiceData $data): void
|
|
{
|
|
if ($data->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,
|
|
]
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|