This commit is contained in:
baodev0
2026-09-23 15:17:18 +07:00
parent cd38c5d6d0
commit 82a71244ca
107 changed files with 3332 additions and 3306 deletions
@@ -33,21 +33,27 @@ class InvoiceRepository extends BaseApiRepository implements InvoiceRepositoryIn
// createFromOrder
// -------------------------------------------------------------------------
public function createFromOrder(Order $order): Invoice
public function createFromOrder(Order $order, ?int $dueDays = null): Invoice
{
$maxMonths = $order->orderItems->max(fn($item) => OrderHelper::cycleToMonths($item->billing_cycle)) ?? 1;
$discount = (int) ($order->discount_amount ?? 0);
// due_date là HẠN THANH TOÁN, không phải ngày hết hạn dịch vụ — mốc dịch vụ đã có
// billing_period_end và services.expires_at lo. Cho phép truyền dueDays để admin gia hạn
// hạn thanh toán riêng cho một số khách.
$dueDays = $dueDays ?? (int) config('billing.invoice_due_days', 30);
$invoice = $this->model->create([
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
'customer_id' => $order->customer_id,
'order_id' => $order->id,
'invoice_type' => 1, // new service
'sub_total' => $order->total,
'discount_amount' => $order->discount_amount ?? 0,
// sub_total là tổng TRƯỚC giảm giá, để sub_total - discount_amount = total.
'sub_total' => (int) $order->total + $discount,
'discount_amount' => $discount,
'tax' => 0,
'total' => $order->total,
'status' => InvoiceStatus::UNPAID->value,
'due_date' => now()->addMonths($maxMonths)->toDateString(),
'due_date' => now()->addDays($dueDays)->toDateString(),
]);
// Create one invoice_item per order_item line + addon sub-lines