Files
portal-api/app/Models/Invoice.php
T
baodev0andClaude Opus 5.5 40bf6b54f9 feat(admin): star/tags marks, service product-type filter, campaign bulk actions, SMTP test
- Star (is_starred) and colored tags for services, customers, orders, invoices:
  Markable trait, tags/taggables tables, PATCH administrator/marks/{type}/{key},
  tag CRUD, list filters starred / tag_ids[]
- GET administrator/services accepts product_type (falls back to product type
  for legacy rows without services.type)
- Mail campaigns: bulk cancel / retry failed / delete; cancelled campaigns are
  skipped by SendMailCampaignRecipient
- POST administrator/settings/test-smtp sends a test email through the saved
  mail transport

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 21:53:41 +07:00

105 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Invoice\InvoiceStatus;
use App\Models\Concerns\Markable;
use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Invoice extends Model
{
use HasFactory, SoftDeletes, LogsActivity, Markable;
protected $table = 'invoices';
protected $guarded = [];
protected $casts = [
'date' => 'date',
'due_date' => 'date',
'paid_at' => 'datetime',
'auto_renew' => 'boolean',
];
protected static function booted(): void
{
static::creating(function (Invoice $invoice) {
$invoice->date ??= now()->toDateString();
});
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
/** The order that originated this invoice. */
public function clientPath(): string
{
return '/hoa-don/' . ($this->order?->order_num ?? $this->invoice_number);
}
/** @param iterable<Service> $services */
public function scopeOfServices(Builder $query, iterable $services): Builder
{
$services = collect($services);
$orderIds = $services->map(fn (Service $service) => $service->orderItem?->order_id)->filter()->unique()->values();
return $query->where(fn (Builder $q) => $q
->whereIn('service_id', $services->pluck('id'))
->orWhere(fn (Builder $q) => $q->whereNull('service_id')->whereIn('order_id', $orderIds)));
}
public function belongsToService(Service $service): bool
{
if ($this->service_id !== null) {
return (int) $this->service_id === (int) $service->id;
}
return $this->order_id !== null && (int) $this->order_id === (int) $service->orderItem?->order_id;
}
public function order()
{
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(InvoiceAttachment::class);
}
/** Services whose active invoice is this one (updated on each renewal). */
public function services()
{
return $this->hasMany(Service::class, 'invoice_id');
}
/** The service this invoice upgrades, for invoice_type = 3 (upgrade). */
public function service()
{
return $this->belongsTo(Service::class, 'service_id');
}
public function payment()
{
return $this->belongsTo(Payment::class, 'payment_method', 'id');
}
public function getStatusLabelAttribute(): string
{
return InvoiceStatus::from($this->status)->label();
}
}