您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Estimate.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\MoneyCast;
  4. use App\Casts\RateCast;
  5. use App\Collections\Accounting\DocumentCollection;
  6. use App\Concerns\Blamable;
  7. use App\Concerns\CompanyOwned;
  8. use App\Enums\Accounting\AdjustmentComputation;
  9. use App\Enums\Accounting\DocumentDiscountMethod;
  10. use App\Enums\Accounting\EstimateStatus;
  11. use App\Models\Common\Client;
  12. use App\Models\Setting\Currency;
  13. use Illuminate\Database\Eloquent\Attributes\CollectedBy;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Database\Eloquent\Casts\Attribute;
  16. use Illuminate\Database\Eloquent\Factories\HasFactory;
  17. use Illuminate\Database\Eloquent\Model;
  18. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  19. use Illuminate\Database\Eloquent\Relations\MorphMany;
  20. use Illuminate\Support\Carbon;
  21. #[CollectedBy(DocumentCollection::class)]
  22. class Estimate extends Model
  23. {
  24. use Blamable;
  25. use CompanyOwned;
  26. use HasFactory;
  27. protected $fillable = [
  28. 'company_id',
  29. 'client_id',
  30. 'logo',
  31. 'header',
  32. 'subheader',
  33. 'estimate_number',
  34. 'reference_number',
  35. 'date',
  36. 'expiration_date',
  37. 'approved_at',
  38. 'accepted_at',
  39. 'declined_at',
  40. 'last_sent_at',
  41. 'last_viewed_at',
  42. 'status',
  43. 'currency_code',
  44. 'discount_method',
  45. 'discount_computation',
  46. 'discount_rate',
  47. 'subtotal',
  48. 'tax_total',
  49. 'discount_total',
  50. 'total',
  51. 'terms',
  52. 'footer',
  53. 'created_by',
  54. 'updated_by',
  55. ];
  56. protected $casts = [
  57. 'date' => 'date',
  58. 'expiration_date' => 'date',
  59. 'approved_at' => 'datetime',
  60. 'accepted_at' => 'datetime',
  61. 'declined_at' => 'datetime',
  62. 'last_sent_at' => 'datetime',
  63. 'last_viewed_at' => 'datetime',
  64. 'status' => EstimateStatus::class,
  65. 'discount_method' => DocumentDiscountMethod::class,
  66. 'discount_computation' => AdjustmentComputation::class,
  67. 'discount_rate' => RateCast::class,
  68. 'subtotal' => MoneyCast::class,
  69. 'tax_total' => MoneyCast::class,
  70. 'discount_total' => MoneyCast::class,
  71. 'total' => MoneyCast::class,
  72. ];
  73. public function client(): BelongsTo
  74. {
  75. return $this->belongsTo(Client::class);
  76. }
  77. public function currency(): BelongsTo
  78. {
  79. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  80. }
  81. public function lineItems(): MorphMany
  82. {
  83. return $this->morphMany(DocumentLineItem::class, 'documentable');
  84. }
  85. protected function isCurrentlyExpired(): Attribute
  86. {
  87. return Attribute::get(function () {
  88. return $this->expiration_date?->isBefore(today()) && $this->canBeExpired();
  89. });
  90. }
  91. public function isDraft(): bool
  92. {
  93. return $this->status === EstimateStatus::Draft;
  94. }
  95. public function isApproved(): bool
  96. {
  97. return $this->approved_at !== null;
  98. }
  99. public function isAccepted(): bool
  100. {
  101. return $this->accepted_at !== null;
  102. }
  103. public function isDeclined(): bool
  104. {
  105. return $this->declined_at !== null;
  106. }
  107. public function isSent(): bool
  108. {
  109. return $this->last_sent_at !== null;
  110. }
  111. public function canBeExpired(): bool
  112. {
  113. return ! in_array($this->status, [
  114. EstimateStatus::Draft,
  115. EstimateStatus::Accepted,
  116. EstimateStatus::Declined,
  117. EstimateStatus::Converted,
  118. ]);
  119. }
  120. public function scopeActive(Builder $query): Builder
  121. {
  122. return $query->whereIn('status', [
  123. EstimateStatus::Unsent,
  124. EstimateStatus::Sent,
  125. EstimateStatus::Viewed,
  126. EstimateStatus::Accepted,
  127. ]);
  128. }
  129. public static function getNextDocumentNumber(): string
  130. {
  131. $company = auth()->user()->currentCompany;
  132. if (! $company) {
  133. throw new \RuntimeException('No current company is set for the user.');
  134. }
  135. $defaultEstimateSettings = $company->defaultInvoice;
  136. $numberPrefix = 'EST-';
  137. $numberDigits = $defaultEstimateSettings->number_digits;
  138. $latestDocument = static::query()
  139. ->whereNotNull('estimate_number')
  140. ->latest('estimate_number')
  141. ->first();
  142. $lastNumberNumericPart = $latestDocument
  143. ? (int) substr($latestDocument->estimate_number, strlen($numberPrefix))
  144. : 0;
  145. $numberNext = $lastNumberNumericPart + 1;
  146. return $defaultEstimateSettings->getNumberNext(
  147. padded: true,
  148. format: true,
  149. prefix: $numberPrefix,
  150. digits: $numberDigits,
  151. next: $numberNext
  152. );
  153. }
  154. public function approveDraft(?Carbon $approvedAt = null): void
  155. {
  156. if (! $this->isDraft()) {
  157. throw new \RuntimeException('Invoice is not in draft status.');
  158. }
  159. $approvedAt ??= now();
  160. $this->update([
  161. 'approved_at' => $approvedAt,
  162. 'status' => EstimateStatus::Unsent,
  163. ]);
  164. }
  165. }