123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
-
- namespace App\Models\Accounting;
-
- use App\Casts\MoneyCast;
- use App\Concerns\Blamable;
- use App\Concerns\CompanyOwned;
- use App\Enums\Accounting\InvoiceStatus;
- use App\Models\Banking\Payment;
- use App\Models\Common\Client;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\MorphMany;
-
- class Invoice extends Model
- {
- use Blamable;
- use CompanyOwned;
- use HasFactory;
-
- protected $table = 'invoices';
-
- protected $fillable = [
- 'company_id',
- 'client_id',
- 'logo',
- 'header',
- 'subheader',
- 'invoice_number',
- 'order_number',
- 'date',
- 'due_date',
- 'status',
- 'currency_code',
- 'subtotal',
- 'tax_total',
- 'discount_total',
- 'total',
- 'amount_paid',
- 'terms',
- 'footer',
- 'created_by',
- 'updated_by',
- ];
-
- protected $casts = [
- 'date' => 'date',
- 'due_date' => 'date',
- 'status' => InvoiceStatus::class,
- 'subtotal' => MoneyCast::class,
- 'tax_total' => MoneyCast::class,
- 'discount_total' => MoneyCast::class,
- 'total' => MoneyCast::class,
- 'amount_paid' => MoneyCast::class,
- 'amount_due' => MoneyCast::class,
- ];
-
- public function client(): BelongsTo
- {
- return $this->belongsTo(Client::class);
- }
-
- public function lineItems(): MorphMany
- {
- return $this->morphMany(DocumentLineItem::class, 'documentable');
- }
-
- public function payments(): MorphMany
- {
- return $this->morphMany(Payment::class, 'payable');
- }
-
- public static function getNextDocumentNumber(): string
- {
- $company = auth()->user()->currentCompany;
-
- if (! $company) {
- throw new \RuntimeException('No current company is set for the user.');
- }
-
- $defaultInvoiceSettings = $company->defaultInvoice;
-
- $numberPrefix = $defaultInvoiceSettings->number_prefix;
- $numberDigits = $defaultInvoiceSettings->number_digits;
-
- $latestDocument = static::query()
- ->whereNotNull('invoice_number')
- ->latest('invoice_number')
- ->first();
-
- $lastNumberNumericPart = $latestDocument
- ? (int) substr($latestDocument->document_number, strlen($numberPrefix))
- : 0;
-
- $numberNext = $lastNumberNumericPart + 1;
-
- return $defaultInvoiceSettings->getNumberNext(
- padded: true,
- format: true,
- prefix: $numberPrefix,
- digits: $numberDigits,
- next: $numberNext
- );
- }
- }
|