feat(billing): renewals, coupons, VAT numbering, billing cycles and invoice payment tracking

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-19 16:37:56 +07:00
co-authored by Claude Sonnet 5.5
parent ea9a53f742
commit 25f23d541e
134 changed files with 4926 additions and 185 deletions
@@ -2,6 +2,7 @@
namespace App\Http\Resources\Api\Admin;
use App\Support\Billing\BillingDuplicateDetector;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -12,6 +13,7 @@ class InvoiceResource extends JsonResource
return [
'id' => $this->id,
'invoice_number' => $this->invoice_number,
'duplicate' => BillingDuplicateDetector::forInvoice($this->id),
'order_num' => $this->whenLoaded('order', fn() => $this->order?->order_num),
'order_items' => $this->when(
$this->relationLoaded('order') && $this->order?->relationLoaded('orderItems'),
@@ -25,6 +27,7 @@ class InvoiceResource extends JsonResource
]),
'sub_total' => $this->sub_total,
'discount_amount' => $this->discount_amount,
'coupon_code' => $this->coupon_code,
'tax' => $this->tax,
'total' => $this->total,
// Công nợ: đã thu bao nhiêu / còn thiếu bao nhiêu. Kế toán cần thấy ngay trên
@@ -2,6 +2,7 @@
namespace App\Http\Resources\Api\Admin;
use App\Support\Billing\BillingDuplicateDetector;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -12,6 +13,7 @@ class OrderResource extends JsonResource
return [
'id' => $this->id,
'order_num' => $this->order_num,
'duplicate' => BillingDuplicateDetector::forOrder($this->id),
'customer' => $this->whenLoaded('customer', fn() => [
'id' => $this->customer->id,
'uuid' => $this->customer->uuid,
@@ -95,6 +97,7 @@ class OrderResource extends JsonResource
])),
'sub_total' => $this->sub_total,
'discount_amount' => $this->discount_amount,
'coupon_code' => $this->coupon_code,
'total' => $this->total,
// Order has no payment_id FK — `payment` relation is always null. Fall back to
// the raw payment_method string instead of letting whenLoaded() mask it with null.
@@ -2,6 +2,7 @@
namespace App\Http\Resources\Api\Admin;
use App\Support\BillingCycleCatalog;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -37,6 +38,8 @@ class ProductResource extends JsonResource
'bandwidth' => $this->bandwidth,
'domain_count' => $this->domain_count,
'features' => $this->features,
'short_description' => $this->short_description,
'description' => $this->description,
'setup_fee' => $this->setup_fee,
'min_price' => $minPrice,
'billing_type' => $this->whenLoaded('pricings', function () {
@@ -52,6 +55,9 @@ class ProductResource extends JsonResource
'price_annually' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'annually'))->price),
'price_biennially' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'biennially'))->price),
'price_triennially' => $this->whenLoaded('pricings', fn () => optional($this->pricings->firstWhere('billing_cycle', 'triennially'))->price),
'custom_prices' => $this->whenLoaded('pricings', fn () => $this->pricings
->filter(fn ($pricing) => str_starts_with((string) $pricing->billing_cycle, BillingCycleCatalog::CODE_PREFIX))
->mapWithKeys(fn ($pricing) => [$pricing->billing_cycle => $pricing->price])),
'price_onetime' => $this->whenLoaded('pricings', fn () => optional($this->pricings->first(fn ($pricing) => in_array($pricing->billing_cycle, ['onetime', 'one_time'], true)))->price),
'is_active' => $this->is_active,
'created_at' => $this->created_at,
@@ -112,6 +112,7 @@ class ServiceClientDetailResource extends JsonResource
->where('is_active', 1)
->value('price') ?? 0),
'addon_total' => \App\Support\RenewalPricing::addonTotal($this->resource, 1),
'renewal_options' => \App\Support\RenewalPricing::options($product),
] : null,
'vm' => $vm ? [
'id' => $vm->id,
@@ -3,6 +3,7 @@
namespace App\Http\Resources\Api\Service\Order;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\InvoiceAttachment;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -20,7 +21,10 @@ class InvoiceDetailResource extends JsonResource
'status_text' => InvoiceStatus::tryFrom((int) $this->status)?->label() ?? '-',
'sub_total' => (float) $this->sub_total,
'discount_amount' => (float) ($this->discount_amount ?? 0),
'coupon_code' => $this->coupon_code,
'tax' => (float) $this->tax,
'lookup_code' => $this->lookup_code,
'vat_number' => $this->vat_number,
'total' => (float) $this->total,
// Công nợ: đã thu / còn thiếu. Dịch vụ chỉ kích hoạt khi outstanding = 0.
'amount_paid' => (float) $this->amount_paid,
@@ -37,7 +41,10 @@ class InvoiceDetailResource extends JsonResource
]),
'order' => $this->whenLoaded('order', fn () => [
'order_num' => $this->order->order_num,
'status' => (int) $this->order->status,
'status_text' => OrderStatus::tryFrom((int) $this->order->status)?->label() ?? '-',
'total' => (float) $this->order->total,
'created_at' => $this->order->created_at?->toISOString(),
]),
'items' => $this->whenLoaded('order', fn () => $this->order->orderItems->map(fn ($item) => [
'product_name' => $item->product?->name,
@@ -47,8 +54,9 @@ class InvoiceDetailResource extends JsonResource
'months' => $item->months,
'os_id' => $item->os_id,
])->values()->all()),
'service' => $this->whenLoaded('order', fn () => $this->resolveService()),
'attachments' => $this->whenLoaded('attachments', fn () => $this->attachments
->where('type', '!=', InvoiceAttachment::TYPE_DEPLOYMENT)
->whereNotIn('type', [InvoiceAttachment::TYPE_DEPLOYMENT, InvoiceAttachment::TYPE_CONTRACT])
->values()
->map(fn ($a) => [
'filename' => $a->filename,
@@ -62,6 +70,24 @@ class InvoiceDetailResource extends JsonResource
];
}
private function resolveService(): ?array
{
$items = $this->order->orderItems;
$item = $this->service_id
? $items->first(fn ($i) => (int) $i->service?->id === (int) $this->service_id)
: null;
$item ??= $items->first();
return $item ? [
'product_id' => $item->product_id,
'name' => $item->product?->name,
'billing_cycle' => $item->billing_cycle,
'months' => $item->months,
'server_name' => $item->server_name,
'product_type' => $item->product?->product_type,
] : null;
}
private function resolveVmware(\App\Models\ModuleVmware $vm): array
{
return [
@@ -4,6 +4,8 @@ namespace App\Http\Resources\Api\Service\Order;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\InvoiceAttachment;
use App\Services\InvoicePaymentService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -11,6 +13,8 @@ class OrderDetailResource extends JsonResource
{
use ResolvesPaymentMethod;
private const INVOICE_TYPE_LABELS = [1 => 'Đơn mới', 2 => 'Gia hạn', 3 => 'Nâng cấp'];
public function toArray(Request $request): array
{
return [
@@ -37,6 +41,39 @@ class OrderDetailResource extends JsonResource
'paid_at' => $this->invoice->paid_at?->toISOString(),
'payment_method' => $this->resolvePaymentMethod($this->invoice),
]),
'invoices' => $this->whenLoaded('invoices', fn () => $this->invoices
->sortByDesc('id')
->values()
->map(fn ($invoice) => [
'id' => $invoice->id,
'invoice_number' => $invoice->invoice_number,
'invoice_type' => (int) $invoice->invoice_type,
'type_text' => self::INVOICE_TYPE_LABELS[(int) $invoice->invoice_type] ?? 'Hóa đơn',
'status' => (int) $invoice->status,
'status_text' => InvoiceStatus::tryFrom((int) $invoice->status)?->label() ?? '-',
'total' => (float) $invoice->total,
'amount_paid' => (float) $invoice->amount_paid,
'due_date' => $invoice->due_date?->toISOString(),
'paid_at' => $invoice->paid_at?->toISOString(),
'billing_period_start' => $invoice->billing_period_start,
'billing_period_end' => $invoice->billing_period_end,
'payable' => in_array((int) $invoice->status, InvoicePaymentService::PAYABLE_STATUSES, true),
'payment_url' => $invoice->setRelation('order', $this->resource)->clientPath(),
])),
'contracts' => $this->whenLoaded('invoices', fn () => $this->invoices
->flatMap(fn ($invoice) => $invoice->relationLoaded('attachments')
? $invoice->attachments
->where('type', InvoiceAttachment::TYPE_CONTRACT)
->map(fn ($attachment) => [
'id' => $attachment->id,
'filename' => $attachment->filename,
'mime_type' => $attachment->mime_type,
'size' => (int) $attachment->size,
'invoice_number' => $invoice->invoice_number,
'uploaded_at' => $attachment->created_at?->toISOString(),
])
: collect())
->values()),
'items' => $this->whenLoaded('orderItems') ? OrderItemResource::collection($this->orderItems) : [],
];
}
@@ -2,6 +2,8 @@
namespace App\Http\Resources\Api\Service;
use App\Support\BillingCycleCatalog;
use App\Support\ProductBillingCycles;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -77,15 +79,16 @@ class ServiceResource extends JsonResource
];
})->values(),
'pricing' => $this->pricings
->sortBy(fn($p) => $periodMonths[$p->billing_cycle] ?? 999)
->filter(fn($p) => ProductBillingCycles::allows($this->product_type, $p->billing_cycle))
->sortBy(fn($p) => $periodMonths[$p->billing_cycle] ?? BillingCycleCatalog::monthsFor($p->billing_cycle) ?? 999)
->values()
->map(fn($p) => [
'id' => $p->id,
'name' => $billingCycleLabels[$p->billing_cycle] ?? $p->billing_cycle,
'name' => $billingCycleLabels[$p->billing_cycle] ?? BillingCycleCatalog::labelForCode($p->billing_cycle) ?? $p->billing_cycle,
'price' => (int) $p->price,
'price_sale' => $p->price_sale ? (int) $p->price_sale : null,
'period' => $periodMonths[$p->billing_cycle] ?? 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month',
'period' => $periodMonths[$p->billing_cycle] ?? BillingCycleCatalog::monthsFor($p->billing_cycle) ?? 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? (BillingCycleCatalog::monthsFor($p->billing_cycle) ? $p->billing_cycle : 'month'),
]),
$this->mergeWhen($this->product_type === 'hosting', fn () => [
'service_plan' => $this->servicePlan?->summary(),