選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Adjustment.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 isActive(): bool
  90. {
  91. return $this->status === AdjustmentStatus::Active;
  92. }
  93. public function isInactive(): bool
  94. {
  95. return ! $this->isActive();
  96. }
  97. public function canBePaused(): bool
  98. {
  99. return $this->status === AdjustmentStatus::Active;
  100. }
  101. public function canBeResumed(): bool
  102. {
  103. return $this->status === AdjustmentStatus::Paused;
  104. }
  105. public function canBeArchived(): bool
  106. {
  107. return $this->status !== AdjustmentStatus::Archived;
  108. }
  109. public function calculateNaturalStatus(): AdjustmentStatus
  110. {
  111. if ($this->start_date?->isAfter(company_now())) {
  112. return AdjustmentStatus::Upcoming;
  113. }
  114. if ($this->end_date?->isBefore(company_now())) {
  115. return AdjustmentStatus::Expired;
  116. }
  117. return AdjustmentStatus::Active;
  118. }
  119. public function pause(?string $reason = null, ?\DateTime $untilDate = null): bool
  120. {
  121. if (! $this->canBePaused()) {
  122. return false;
  123. }
  124. return $this->update([
  125. 'paused_at' => company_now(),
  126. 'paused_until' => $untilDate,
  127. 'status' => AdjustmentStatus::Paused,
  128. 'status_reason' => $reason,
  129. ]);
  130. }
  131. public function resume(): bool
  132. {
  133. if (! $this->canBeResumed()) {
  134. return false;
  135. }
  136. return $this->update([
  137. 'paused_at' => null,
  138. 'paused_until' => null,
  139. 'status_reason' => null,
  140. 'status' => $this->calculateNaturalStatus(),
  141. ]);
  142. }
  143. public function archive(?string $reason = null): bool
  144. {
  145. if (! $this->canBeArchived()) {
  146. return false;
  147. }
  148. return $this->update([
  149. 'archived_at' => company_now(),
  150. 'status' => AdjustmentStatus::Archived,
  151. 'status_reason' => $reason,
  152. ]);
  153. }
  154. public function shouldAutoResume(): bool
  155. {
  156. return $this->status === AdjustmentStatus::Paused &&
  157. $this->paused_until !== null &&
  158. $this->paused_until->isBefore(company_now());
  159. }
  160. public function refreshStatus(): bool
  161. {
  162. // Don't automatically change archived or paused status
  163. if ($this->status === AdjustmentStatus::Archived ||
  164. ($this->status === AdjustmentStatus::Paused && ! $this->shouldAutoResume())) {
  165. return false;
  166. }
  167. // Check if a paused adjustment should be auto-resumed
  168. if ($this->shouldAutoResume()) {
  169. return $this->resume();
  170. }
  171. // Calculate natural status based on dates
  172. $naturalStatus = $this->calculateNaturalStatus();
  173. // Only update if the status would change
  174. if ($this->status !== $naturalStatus) {
  175. return $this->update([
  176. 'status' => $naturalStatus,
  177. ]);
  178. }
  179. return false;
  180. }
  181. protected static function newFactory(): Factory
  182. {
  183. return AdjustmentFactory::new();
  184. }
  185. }