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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Enums\Accounting\TransactionType;
  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. use Illuminate\Database\Eloquent\Relations\MorphOne;
  14. class Invoice extends Model
  15. {
  16. use Blamable;
  17. use CompanyOwned;
  18. use HasFactory;
  19. protected $table = 'invoices';
  20. protected $fillable = [
  21. 'company_id',
  22. 'client_id',
  23. 'logo',
  24. 'header',
  25. 'subheader',
  26. 'invoice_number',
  27. 'order_number',
  28. 'date',
  29. 'due_date',
  30. 'status',
  31. 'currency_code',
  32. 'subtotal',
  33. 'tax_total',
  34. 'discount_total',
  35. 'total',
  36. 'amount_paid',
  37. 'terms',
  38. 'footer',
  39. 'created_by',
  40. 'updated_by',
  41. ];
  42. protected $casts = [
  43. 'date' => 'date',
  44. 'due_date' => 'date',
  45. 'status' => InvoiceStatus::class,
  46. 'subtotal' => MoneyCast::class,
  47. 'tax_total' => MoneyCast::class,
  48. 'discount_total' => MoneyCast::class,
  49. 'total' => MoneyCast::class,
  50. 'amount_paid' => MoneyCast::class,
  51. 'amount_due' => MoneyCast::class,
  52. ];
  53. public function client(): BelongsTo
  54. {
  55. return $this->belongsTo(Client::class);
  56. }
  57. public function lineItems(): MorphMany
  58. {
  59. return $this->morphMany(DocumentLineItem::class, 'documentable');
  60. }
  61. public function transactions(): MorphMany
  62. {
  63. return $this->morphMany(Transaction::class, 'transactionable');
  64. }
  65. public function payments(): MorphMany
  66. {
  67. return $this->transactions()->where('is_payment', true);
  68. }
  69. public function deposits(): MorphMany
  70. {
  71. return $this->transactions()->where('type', TransactionType::Deposit)->where('is_payment', true);
  72. }
  73. public function withdrawals(): MorphMany
  74. {
  75. return $this->transactions()->where('type', TransactionType::Withdrawal)->where('is_payment', true);
  76. }
  77. public function approvalTransaction(): MorphOne
  78. {
  79. return $this->morphOne(Transaction::class, 'transactionable')
  80. ->where('type', TransactionType::Approval);
  81. }
  82. public function isDraft(): bool
  83. {
  84. return $this->status === InvoiceStatus::Draft;
  85. }
  86. public function canRecordPayment(): bool
  87. {
  88. return ! in_array($this->status, [
  89. InvoiceStatus::Draft,
  90. InvoiceStatus::Paid,
  91. InvoiceStatus::Overpaid,
  92. InvoiceStatus::Void,
  93. ]);
  94. }
  95. public static function getNextDocumentNumber(): string
  96. {
  97. $company = auth()->user()->currentCompany;
  98. if (! $company) {
  99. throw new \RuntimeException('No current company is set for the user.');
  100. }
  101. $defaultInvoiceSettings = $company->defaultInvoice;
  102. $numberPrefix = $defaultInvoiceSettings->number_prefix;
  103. $numberDigits = $defaultInvoiceSettings->number_digits;
  104. $latestDocument = static::query()
  105. ->whereNotNull('invoice_number')
  106. ->latest('invoice_number')
  107. ->first();
  108. $lastNumberNumericPart = $latestDocument
  109. ? (int) substr($latestDocument->invoice_number, strlen($numberPrefix))
  110. : 0;
  111. $numberNext = $lastNumberNumericPart + 1;
  112. return $defaultInvoiceSettings->getNumberNext(
  113. padded: true,
  114. format: true,
  115. prefix: $numberPrefix,
  116. digits: $numberDigits,
  117. next: $numberNext
  118. );
  119. }
  120. }