feat: Reimplement order and invoice management with new service and repository layers, introduce compression middleware, and update database schema.

This commit is contained in:
pbao9
2026-03-12 09:30:29 +07:00
parent 7f6264c87f
commit 1e09ccf17b
87 changed files with 2348 additions and 2612 deletions
+35 -18
View File
@@ -10,41 +10,58 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Invoice extends Model
{
use HasFactory, SoftDeletes;
protected $table = 'invoice';
public $timestamps = true;
protected $table = 'invoices';
protected $guarded = [];
protected $casts = [
'due_date' => 'date',
'paid_at' => 'datetime',
'auto_renew' => 'boolean',
];
public function customer()
{
return $this->belongsTo(\App\Models\Customer::class);
}
public function payment()
{
return $this->belongsTo(\App\Models\Payment::class, 'payment_id', 'id');
}
public function invoiceItem()
{
return $this->hasMany(\App\Models\InvoiceItem::class)->with(['addons.addonItems', 'hosting', 'vps', 'os']);
return $this->belongsTo(Customer::class);
}
/** The order that originated this invoice. */
public function order()
{
return $this->belongsTo(\App\Models\Order::class, 'id', 'invoice_id');
return $this->belongsTo(Order::class, 'order_id');
}
/** Line items on this invoice. */
public function invoiceItems()
{
return $this->hasMany(InvoiceItem::class, 'invoice_id');
}
/** Uploaded payment-proof or supporting documents. */
public function attachments()
{
return $this->hasMany(\App\Models\InvoiceAttachment::class);
return $this->hasMany(InvoiceAttachment::class);
}
public function getStatus()
/** Services whose active invoice is this one (updated on each renewal). */
public function services()
{
return match ($this->attributes['status']) {
return $this->hasMany(Service::class, 'invoice_id');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function getStatus(): string
{
return match ($this->status) {
1 => '<span class="bg-red-100 text-red-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-red-900 dark:text-red-300">Chưa thanh toán</span>',
2 => '<span class="bg-lime-100 text-lime-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-lime-900 dark:text-lime-300">Đã thanh toán</span>',
default => '<span class="bg-red-100 text-red-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-red-900 dark:text-red-300">Đã hủy</span>'
3 => '<span class="bg-gray-100 text-gray-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-gray-900 dark:text-gray-300">Đã hủy</span>',
4 => '<span class="bg-orange-100 text-orange-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-orange-900 dark:text-orange-300">Quá hạn</span>',
default => '<span class="bg-red-100 text-red-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-red-900 dark:text-red-300">Không xác định</span>',
};
}
+7 -5
View File
@@ -5,19 +5,21 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @deprecated Replaced by ServiceAddon.
* The invoice_addons table has been removed in the billing refactor.
* Use ServiceAddon for configurable resources attached to a Service.
*/
class InvoiceAddons extends Model
{
use HasFactory;
protected $table = 'invoice_addons';
protected $guarded = [];
public function invoiceItem()
{
return $this->belongsTo(InvoiceItem::class, 'invoice_item_id');
}
public function addonItems()
{
return $this->belongsTo(Addon::class, 'addon_id');
}
}
+27 -79
View File
@@ -9,101 +9,49 @@ class InvoiceItem extends Model
{
use HasFactory;
protected $table = 'invoice_item';
// protected $fillable = [
// 'invoice_id',
// 'customer_id',
// 'billing',
// 'type_id',
// 'product_id',
// 'invoice_type',
// 'parent_id',
// 'relid',
// 'description',
// 'amount',
// 'tax',
// 'due_date'
// ];
protected $table = 'invoice_items';
protected $guarded = [];
public $timestamps = true;
// -------------------------------------------------------------------------
// Relationships
// -------------------------------------------------------------------------
public function invoice()
{
return $this->belongsTo(\App\Models\Invoice::class, 'invoice_id');
return $this->belongsTo(Invoice::class, 'invoice_id');
}
public function customer()
public function product()
{
return $this->belongsTo(\App\Models\Customer::class);
}
public function type()
{
return $this->belongsTo(\App\Models\Type::class, 'invoice_type');
}
public function os()
{
return $this->belongsTo(Os::class, 'os_id');
return $this->belongsTo(Product::class, 'product_id');
}
public function addons()
{
return $this->hasMany(InvoiceAddons::class, 'invoice_item_id')->with('addonItems');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function hosting()
public function getBillingLabelAttribute(): string
{
return $this->belongsTo(Hosting::class, 'product_id');
}
public function vps()
{
return $this->belongsTo(VPS::class, 'product_id');
}
// Remove the dynamic product relationship as it causes issues with eager loading
public function getProductAttribute()
{
return match ($this->product_type) {
3 => $this->hosting,
4 => $this->vps,
default => null,
return match ((int) $this->months) {
0 => 'One Time',
1 => 'Monthly',
3 => 'Quarterly',
6 => 'Semi-Annually',
12 => 'Annually',
24 => '2 Years',
36 => '3 Years',
default => $this->months . ' Months',
};
}
public function getBilling()
public function getTypeLabelAttribute(): string
{
$months = $this->attributes['months'] ?? 0;
return match ($months) {
0 => "One Time",
1 => "Monthly",
3 => "Quarterly",
6 => "Semi-Annually",
12 => "Annually",
24 => "2 Years",
36 => "3 Years",
default => $months . " Months"
return match ($this->type) {
'service' => 'Dịch vụ',
'addon' => 'Addon',
'upgrade' => 'Nâng cấp',
default => ucfirst($this->type),
};
}
public function GetRelidProduct()
{
$model = null;
switch ($this->type_id) {
case 3:
$model = ModuleHosting::with('server')->find($this->relid);
break;
case 4:
$model = ModuleVmware::with('ipAddress')->find($this->relid);
break;
default:
break;
}
return $model;
}
}
+29 -38
View File
@@ -10,62 +10,53 @@ class Order extends Model
{
use HasFactory, SoftDeletes;
protected $table = 'order';
public $timestamps = true;
// protected $fillable = [
// 'order_num',
// 'customer_id',
// 'date',
// 'payment_id',
// 'discount_id',
// 'discount_value',
// 'invoice_id',
// 'user_id',
// 'amount',
// 'note',
// 'client_ip',
// 'status'
// ];
protected $table = 'orders';
protected $guarded = [];
public function customer()
{
return $this->belongsTo(\App\Models\Customer::class);
}
public function discount()
{
return $this->belongsTo(\App\Models\Discount::class);
}
public function invoice()
{
return $this->belongsTo(\App\Models\Invoice::class, 'invoice_id', 'id');
}
public function payment()
{
return $this->belongsTo(\App\Models\Payment::class, 'payment_method', 'id');
return $this->belongsTo(Customer::class);
}
public function user()
{
return $this->belongsTo(\App\Models\User::class);
return $this->belongsTo(User::class);
}
public function orderItem()
public function discount()
{
return $this->belongsToMany(\App\Models\InvoiceItem::class, 'order_items', 'order_id', 'invoice_item_id');
return $this->belongsTo(Discount::class);
}
/** Line items that make up this order. */
public function orderItems()
{
return $this->hasMany(OrderItem::class, 'order_id');
}
public function vmWare()
/** Invoice generated from this order. */
public function invoice()
{
return $this->hasMany(\App\Models\ModuleVmware::class, 'order_id', 'id');
return $this->hasOne(Invoice::class, 'order_id');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function getStatusLabelAttribute(): string
{
return match ($this->status) {
'pending' => 'Chờ xử lý',
'processing' => 'Đang xử lý',
'completed' => 'Hoàn tất',
'cancelled' => 'Đã hủy',
default => ucfirst($this->status),
};
}
public function payment()
{
return $this->belongsTo(Payment::class);
}
}
+7 -7
View File
@@ -5,21 +5,21 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @deprecated Replaced by ServiceAddon.
* The order__order_addons table has been removed in the billing refactor.
* Use ServiceAddon for configurable resources attached to a Service.
*/
class OrderAddon extends Model
{
use HasFactory;
protected $guarded = [];
protected $table = 'order__order_addons';
protected $guarded = [];
public function orderItem()
{
return $this->belongsTo(OrderItem::class, 'order_item_id');
}
public function addonItems()
{
return $this->belongsTo(Addon::class, 'addon_id');
}
}
+33 -25
View File
@@ -10,50 +10,58 @@ class OrderItem extends Model
use HasFactory;
protected $table = 'order_items';
protected $guarded = [];
public $timestamps = false;
protected $casts = [
'addons_meta' => 'array',
];
// -------------------------------------------------------------------------
// Relationships
// -------------------------------------------------------------------------
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
public function addons()
{
return $this->hasMany(OrderAddon::class, 'order_item_id');
}
public function getProductAttribute()
{
if ($this->product_id) {
return Product::find($this->product_id);
}
return null;
}
/**
* Relationship: OrderItem belongs to Product (new system)
*/
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
/**
* Relationship: OrderItem has pricing info
*/
public function pricing()
{
return $this->belongsTo(ProductPricings::class, 'product_pricing_id');
}
public function os()
{
return $this->belongsTo(Os::class, 'os_id');
}
/** The provisioned service spawned from this order line. */
public function service()
{
return $this->hasOne(Service::class, 'order_item_id');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function getBillingLabelAttribute(): string
{
return match ((int) $this->months) {
0 => 'One Time',
1 => 'Monthly',
3 => 'Quarterly',
6 => 'Semi-Annually',
12 => 'Annually',
24 => '2 Years',
36 => '3 Years',
default => $this->months . ' Months',
};
}
}
+89
View File
@@ -0,0 +1,89 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Service extends Model
{
use HasFactory, SoftDeletes;
protected $table = 'services';
protected $guarded = [];
protected $casts = [
'auto_renew' => 'boolean',
'started_at' => 'datetime',
'expires_at' => 'datetime',
'suspended_at' => 'datetime',
'cancelled_at' => 'datetime',
];
// -------------------------------------------------------------------------
// Relationships
// -------------------------------------------------------------------------
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
/** The order item that triggered provisioning. */
public function orderItem()
{
return $this->belongsTo(OrderItem::class, 'order_item_id');
}
/** The most recent invoice covering this service (updated on renewal). */
public function invoice()
{
return $this->belongsTo(Invoice::class, 'invoice_id');
}
/** Configurable resources / features attached to this service. */
public function addons()
{
return $this->hasMany(ServiceAddon::class, 'service_id');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function isActive(): bool
{
return $this->status === 'active';
}
public function isExpired(): bool
{
return $this->expires_at !== null && $this->expires_at->isPast();
}
public function getStatusLabelAttribute(): string
{
return match ($this->status) {
'pending' => 'Chờ kích hoạt',
'active' => 'Đang hoạt động',
'suspended' => 'Tạm ngưng',
'expired' => 'Hết hạn',
'cancelled' => 'Đã hủy',
'terminated' => 'Đã xóa',
default => ucfirst($this->status),
};
}
/** Convenience: get an addon value by type. */
public function getAddon(string $type): ?string
{
return $this->addons->firstWhere('addon_type', $type)?->addon_value;
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ServiceAddon extends Model
{
use HasFactory;
protected $table = 'service_addons';
protected $guarded = [];
// -------------------------------------------------------------------------
// Relationships
// -------------------------------------------------------------------------
public function service()
{
return $this->belongsTo(Service::class, 'service_id');
}
}