You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Invoice.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\MoneyCast;
  4. use App\Concerns\Blamable;
  5. use App\Concerns\CompanyOwned;
  6. use App\Enums\Accounting\InvoiceStatus;
  7. use App\Models\Banking\Payment;
  8. use App\Models\Common\Client;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Database\Eloquent\Relations\MorphMany;
  13. class Invoice extends Model
  14. {
  15. use Blamable;
  16. use CompanyOwned;
  17. use HasFactory;
  18. protected $table = 'invoices';
  19. protected $fillable = [
  20. 'company_id',
  21. 'client_id',
  22. 'logo',
  23. 'header',
  24. 'subheader',
  25. 'invoice_number',
  26. 'order_number',
  27. 'date',
  28. 'due_date',
  29. 'status',
  30. 'currency_code',
  31. 'subtotal',
  32. 'tax_total',
  33. 'discount_total',
  34. 'total',
  35. 'amount_paid',
  36. 'terms',
  37. 'footer',
  38. 'created_by',
  39. 'updated_by',
  40. ];
  41. protected $casts = [
  42. 'date' => 'date',
  43. 'due_date' => 'date',
  44. 'status' => InvoiceStatus::class,
  45. 'subtotal' => MoneyCast::class,
  46. 'tax_total' => MoneyCast::class,
  47. 'discount_total' => MoneyCast::class,
  48. 'total' => MoneyCast::class,
  49. 'amount_paid' => MoneyCast::class,
  50. 'amount_due' => MoneyCast::class,
  51. ];
  52. public function client(): BelongsTo
  53. {
  54. return $this->belongsTo(Client::class);
  55. }
  56. public function lineItems(): MorphMany
  57. {
  58. return $this->morphMany(DocumentLineItem::class, 'documentable');
  59. }
  60. public function payments(): MorphMany
  61. {
  62. return $this->morphMany(Payment::class, 'payable');
  63. }
  64. public static function getNextDocumentNumber(): string
  65. {
  66. $company = auth()->user()->currentCompany;
  67. if (! $company) {
  68. throw new \RuntimeException('No current company is set for the user.');
  69. }
  70. $defaultInvoiceSettings = $company->defaultInvoice;
  71. $numberPrefix = $defaultInvoiceSettings->number_prefix;
  72. $numberDigits = $defaultInvoiceSettings->number_digits;
  73. $latestDocument = static::query()
  74. ->whereNotNull('invoice_number')
  75. ->latest('invoice_number')
  76. ->first();
  77. $lastNumberNumericPart = $latestDocument
  78. ? (int) substr($latestDocument->document_number, strlen($numberPrefix))
  79. : 0;
  80. $numberNext = $lastNumberNumericPart + 1;
  81. return $defaultInvoiceSettings->getNumberNext(
  82. padded: true,
  83. format: true,
  84. prefix: $numberPrefix,
  85. digits: $numberDigits,
  86. next: $numberNext
  87. );
  88. }
  89. }