update
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -17,5 +17,5 @@ interface InvoiceRepositoryInterface extends BaseRepositoryInterface
|
||||
/**
|
||||
* Create an invoice (+ invoice_items) from a completed Order.
|
||||
*/
|
||||
public function createFromOrder(Order $order): Invoice;
|
||||
public function createFromOrder(Order $order, ?int $dueDays = null): Invoice;
|
||||
}
|
||||
|
||||
@@ -56,9 +56,20 @@ class OrderRepository extends BaseApiRepository implements OrderRepositoryInterf
|
||||
// hasPendingOrder
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Chặn khách tạo đơn mới khi còn đơn chưa thanh toán.
|
||||
*
|
||||
* Bỏ qua đơn import từ WHMCS (legacy_id khác null): dữ liệu cũ có rất nhiều đơn ở trạng
|
||||
* thái pending, tính cả chúng thì sau khi đồng bộ gần như mọi khách hàng cũ đều bị khoá
|
||||
* không đặt được đơn mới.
|
||||
*/
|
||||
public function hasPendingOrder(int $customerId): bool
|
||||
{
|
||||
return $this->model->where('customer_id', $customerId)->where('status', OrderStatus::PENDING->value)->exists();
|
||||
return $this->model
|
||||
->where('customer_id', $customerId)
|
||||
->where('status', OrderStatus::PENDING->value)
|
||||
->whereNull('legacy_id')
|
||||
->exists();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user