Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Invoice.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 App\Observers\InvoiceObserver;
  10. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  11. use Illuminate\Database\Eloquent\Factories\HasFactory;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  14. use Illuminate\Database\Eloquent\Relations\MorphMany;
  15. use Illuminate\Database\Eloquent\Relations\MorphOne;
  16. #[ObservedBy(InvoiceObserver::class)]
  17. class Invoice extends Model
  18. {
  19. use Blamable;
  20. use CompanyOwned;
  21. use HasFactory;
  22. protected $table = 'invoices';
  23. protected $fillable = [
  24. 'company_id',
  25. 'client_id',
  26. 'logo',
  27. 'header',
  28. 'subheader',
  29. 'invoice_number',
  30. 'order_number',
  31. 'date',
  32. 'due_date',
  33. 'status',
  34. 'currency_code',
  35. 'subtotal',
  36. 'tax_total',
  37. 'discount_total',
  38. 'total',
  39. 'amount_paid',
  40. 'terms',
  41. 'footer',
  42. 'created_by',
  43. 'updated_by',
  44. ];
  45. protected $casts = [
  46. 'date' => 'date',
  47. 'due_date' => 'date',
  48. 'status' => InvoiceStatus::class,
  49. 'subtotal' => MoneyCast::class,
  50. 'tax_total' => MoneyCast::class,
  51. 'discount_total' => MoneyCast::class,
  52. 'total' => MoneyCast::class,
  53. 'amount_paid' => MoneyCast::class,
  54. 'amount_due' => MoneyCast::class,
  55. ];
  56. public function client(): BelongsTo
  57. {
  58. return $this->belongsTo(Client::class);
  59. }
  60. public function lineItems(): MorphMany
  61. {
  62. return $this->morphMany(DocumentLineItem::class, 'documentable');
  63. }
  64. public function transactions(): MorphMany
  65. {
  66. return $this->morphMany(Transaction::class, 'transactionable');
  67. }
  68. public function payments(): MorphMany
  69. {
  70. return $this->transactions()->where('is_payment', true);
  71. }
  72. public function deposits(): MorphMany
  73. {
  74. return $this->transactions()->where('type', TransactionType::Deposit)->where('is_payment', true);
  75. }
  76. public function withdrawals(): MorphMany
  77. {
  78. return $this->transactions()->where('type', TransactionType::Withdrawal)->where('is_payment', true);
  79. }
  80. public function approvalTransaction(): MorphOne
  81. {
  82. return $this->morphOne(Transaction::class, 'transactionable')
  83. ->where('type', TransactionType::Journal);
  84. }
  85. public function isDraft(): bool
  86. {
  87. return $this->status === InvoiceStatus::Draft;
  88. }
  89. public function canRecordPayment(): bool
  90. {
  91. return ! in_array($this->status, [
  92. InvoiceStatus::Draft,
  93. InvoiceStatus::Paid,
  94. InvoiceStatus::Void,
  95. ]);
  96. }
  97. public static function getNextDocumentNumber(): string
  98. {
  99. $company = auth()->user()->currentCompany;
  100. if (! $company) {
  101. throw new \RuntimeException('No current company is set for the user.');
  102. }
  103. $defaultInvoiceSettings = $company->defaultInvoice;
  104. $numberPrefix = $defaultInvoiceSettings->number_prefix;
  105. $numberDigits = $defaultInvoiceSettings->number_digits;
  106. $latestDocument = static::query()
  107. ->whereNotNull('invoice_number')
  108. ->latest('invoice_number')
  109. ->first();
  110. $lastNumberNumericPart = $latestDocument
  111. ? (int) substr($latestDocument->invoice_number, strlen($numberPrefix))
  112. : 0;
  113. $numberNext = $lastNumberNumericPart + 1;
  114. return $defaultInvoiceSettings->getNumberNext(
  115. padded: true,
  116. format: true,
  117. prefix: $numberPrefix,
  118. digits: $numberDigits,
  119. next: $numberNext
  120. );
  121. }
  122. public function recordPayment(array $data): void
  123. {
  124. $isRefund = $this->status === InvoiceStatus::Overpaid;
  125. if ($isRefund) {
  126. $transactionType = TransactionType::Withdrawal;
  127. $transactionDescription = 'Refund for Overpayment on Invoice #' . $this->invoice_number;
  128. } else {
  129. $transactionType = TransactionType::Deposit;
  130. $transactionDescription = 'Payment for Invoice #' . $this->invoice_number;
  131. }
  132. // Create transaction
  133. $this->transactions()->create([
  134. 'type' => $transactionType,
  135. 'is_payment' => true,
  136. 'posted_at' => $data['posted_at'],
  137. 'amount' => $data['amount'],
  138. 'payment_method' => $data['payment_method'],
  139. 'bank_account_id' => $data['bank_account_id'],
  140. 'account_id' => Account::getAccountsReceivableAccount()->id,
  141. 'description' => $transactionDescription,
  142. 'notes' => $data['notes'] ?? null,
  143. ]);
  144. }
  145. }