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

Adjustment.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Models\Accounting;
  3. use App\Casts\RateCast;
  4. use App\Concerns\Blamable;
  5. use App\Concerns\CompanyOwned;
  6. use App\Enums\Accounting\AdjustmentCategory;
  7. use App\Enums\Accounting\AdjustmentComputation;
  8. use App\Enums\Accounting\AdjustmentScope;
  9. use App\Enums\Accounting\AdjustmentStatus;
  10. use App\Enums\Accounting\AdjustmentType;
  11. use App\Models\Common\Offering;
  12. use App\Observers\AdjustmentObserver;
  13. use Database\Factories\Accounting\AdjustmentFactory;
  14. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  15. use Illuminate\Database\Eloquent\Factories\Factory;
  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\MorphToMany;
  20. #[ObservedBy(AdjustmentObserver::class)]
  21. class Adjustment extends Model
  22. {
  23. use Blamable;
  24. use CompanyOwned;
  25. use HasFactory;
  26. protected $fillable = [
  27. 'company_id',
  28. 'account_id',
  29. 'name',
  30. 'status',
  31. 'status_reason',
  32. 'description',
  33. 'category',
  34. 'type',
  35. 'recoverable',
  36. 'rate',
  37. 'computation',
  38. 'scope',
  39. 'start_date',
  40. 'end_date',
  41. 'paused_at',
  42. 'paused_until',
  43. 'archived_at',
  44. 'created_by',
  45. 'updated_by',
  46. ];
  47. protected $casts = [
  48. 'status' => AdjustmentStatus::class,
  49. 'category' => AdjustmentCategory::class,
  50. 'type' => AdjustmentType::class,
  51. 'recoverable' => 'boolean',
  52. 'rate' => RateCast::class,
  53. 'computation' => AdjustmentComputation::class,
  54. 'scope' => AdjustmentScope::class,
  55. 'start_date' => 'datetime',
  56. 'end_date' => 'datetime',
  57. 'paused_at' => 'datetime',
  58. 'paused_until' => 'datetime',
  59. 'archived_at' => 'datetime',
  60. ];
  61. public function account(): BelongsTo
  62. {
  63. return $this->belongsTo(Account::class, 'account_id');
  64. }
  65. public function offerings(): MorphToMany
  66. {
  67. return $this->morphedByMany(Offering::class, 'adjustmentable', 'adjustmentables');
  68. }
  69. public function isSalesTax(): bool
  70. {
  71. return $this->category->isTax() && $this->type->isSales();
  72. }
  73. public function isNonRecoverablePurchaseTax(): bool
  74. {
  75. return $this->category->isTax() && $this->type->isPurchase() && $this->recoverable === false;
  76. }
  77. public function isRecoverablePurchaseTax(): bool
  78. {
  79. return $this->category->isTax() && $this->type->isPurchase() && $this->recoverable === true;
  80. }
  81. public function isSalesDiscount(): bool
  82. {
  83. return $this->category->isDiscount() && $this->type->isSales();
  84. }
  85. public function isPurchaseDiscount(): bool
  86. {
  87. return $this->category->isDiscount() && $this->type->isPurchase();
  88. }
  89. public function canBePaused(): bool
  90. {
  91. return $this->status === AdjustmentStatus::Active;
  92. }
  93. public function canBeResumed(): bool
  94. {
  95. return $this->status === AdjustmentStatus::Paused;
  96. }
  97. public function canBeArchived(): bool
  98. {
  99. return $this->status !== AdjustmentStatus::Archived;
  100. }
  101. public function calculateNaturalStatus(): AdjustmentStatus
  102. {
  103. if ($this->start_date?->isFuture()) {
  104. return AdjustmentStatus::Upcoming;
  105. }
  106. if ($this->end_date?->isPast()) {
  107. return AdjustmentStatus::Expired;
  108. }
  109. return AdjustmentStatus::Active;
  110. }
  111. public function pause(?string $reason = null, ?\DateTime $untilDate = null): bool
  112. {
  113. if (! $this->canBePaused()) {
  114. return false;
  115. }
  116. return $this->update([
  117. 'paused_at' => now(),
  118. 'paused_until' => $untilDate,
  119. 'status' => AdjustmentStatus::Paused,
  120. 'status_reason' => $reason,
  121. ]);
  122. }
  123. public function resume(): bool
  124. {
  125. if (! $this->canBeResumed()) {
  126. return false;
  127. }
  128. return $this->update([
  129. 'paused_at' => null,
  130. 'paused_until' => null,
  131. 'status_reason' => null,
  132. 'status' => $this->calculateNaturalStatus(),
  133. ]);
  134. }
  135. public function archive(?string $reason = null): bool
  136. {
  137. if (! $this->canBeArchived()) {
  138. return false;
  139. }
  140. return $this->update([
  141. 'archived_at' => now(),
  142. 'status' => AdjustmentStatus::Archived,
  143. 'status_reason' => $reason,
  144. ]);
  145. }
  146. public function shouldAutoResume(): bool
  147. {
  148. return $this->status === AdjustmentStatus::Paused &&
  149. $this->paused_until !== null &&
  150. $this->paused_until->isPast();
  151. }
  152. public function refreshStatus(): bool
  153. {
  154. // Don't automatically change archived or paused status
  155. if ($this->status === AdjustmentStatus::Archived ||
  156. ($this->status === AdjustmentStatus::Paused && ! $this->shouldAutoResume())) {
  157. return false;
  158. }
  159. // Check if a paused adjustment should be auto-resumed
  160. if ($this->shouldAutoResume()) {
  161. return $this->resume();
  162. }
  163. // Calculate natural status based on dates
  164. $naturalStatus = $this->calculateNaturalStatus();
  165. // Only update if the status would change
  166. if ($this->status !== $naturalStatus) {
  167. return $this->update([
  168. 'status' => $naturalStatus,
  169. ]);
  170. }
  171. return false;
  172. }
  173. protected static function newFactory(): Factory
  174. {
  175. return AdjustmentFactory::new();
  176. }
  177. }