Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Adjustment.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 $table = 'adjustments';
  27. protected $fillable = [
  28. 'company_id',
  29. 'account_id',
  30. 'name',
  31. 'status',
  32. 'status_reason',
  33. 'description',
  34. 'category',
  35. 'type',
  36. 'recoverable',
  37. 'rate',
  38. 'computation',
  39. 'scope',
  40. 'start_date',
  41. 'end_date',
  42. 'paused_at',
  43. 'paused_until',
  44. 'archived_at',
  45. 'created_by',
  46. 'updated_by',
  47. ];
  48. protected $casts = [
  49. 'status' => AdjustmentStatus::class,
  50. 'category' => AdjustmentCategory::class,
  51. 'type' => AdjustmentType::class,
  52. 'recoverable' => 'boolean',
  53. 'rate' => RateCast::class,
  54. 'computation' => AdjustmentComputation::class,
  55. 'scope' => AdjustmentScope::class,
  56. 'start_date' => 'datetime',
  57. 'end_date' => 'datetime',
  58. 'paused_at' => 'datetime',
  59. 'paused_until' => 'datetime',
  60. 'archived_at' => 'datetime',
  61. ];
  62. public function account(): BelongsTo
  63. {
  64. return $this->belongsTo(Account::class, 'account_id');
  65. }
  66. public function offerings(): MorphToMany
  67. {
  68. return $this->morphedByMany(Offering::class, 'adjustmentable', 'adjustmentables');
  69. }
  70. public function isSalesTax(): bool
  71. {
  72. return $this->category->isTax() && $this->type->isSales();
  73. }
  74. public function isNonRecoverablePurchaseTax(): bool
  75. {
  76. return $this->category->isTax() && $this->type->isPurchase() && $this->recoverable === false;
  77. }
  78. public function isRecoverablePurchaseTax(): bool
  79. {
  80. return $this->category->isTax() && $this->type->isPurchase() && $this->recoverable === true;
  81. }
  82. public function isSalesDiscount(): bool
  83. {
  84. return $this->category->isDiscount() && $this->type->isSales();
  85. }
  86. public function isPurchaseDiscount(): bool
  87. {
  88. return $this->category->isDiscount() && $this->type->isPurchase();
  89. }
  90. // Add these methods to your Adjustment model
  91. /**
  92. * Check if adjustment can be paused
  93. */
  94. public function canBePaused(): bool
  95. {
  96. return $this->status === AdjustmentStatus::Active;
  97. }
  98. /**
  99. * Check if adjustment can be resumed
  100. */
  101. public function canBeResumed(): bool
  102. {
  103. return $this->status === AdjustmentStatus::Paused;
  104. }
  105. /**
  106. * Check if adjustment can be archived
  107. */
  108. public function canBeArchived(): bool
  109. {
  110. return $this->status !== AdjustmentStatus::Archived;
  111. }
  112. /**
  113. * Calculate the natural status of the adjustment based on dates
  114. */
  115. public function calculateNaturalStatus(): AdjustmentStatus
  116. {
  117. if ($this->start_date?->isFuture()) {
  118. return AdjustmentStatus::Upcoming;
  119. }
  120. if ($this->end_date?->isPast()) {
  121. return AdjustmentStatus::Expired;
  122. }
  123. return AdjustmentStatus::Active;
  124. }
  125. /**
  126. * Pause the adjustment
  127. */
  128. public function pause(?string $reason = null, ?\DateTime $untilDate = null): bool
  129. {
  130. if (! $this->canBePaused()) {
  131. return false;
  132. }
  133. $this->paused_at = now();
  134. $this->paused_until = $untilDate;
  135. $this->status = AdjustmentStatus::Paused;
  136. $this->status_reason = $reason;
  137. return $this->save();
  138. }
  139. /**
  140. * Resume the adjustment
  141. */
  142. public function resume(): bool
  143. {
  144. if (! $this->canBeResumed()) {
  145. return false;
  146. }
  147. $this->paused_at = null;
  148. $this->paused_until = null;
  149. $this->status_reason = null;
  150. $this->status = $this->calculateNaturalStatus();
  151. return $this->save();
  152. }
  153. /**
  154. * Archive the adjustment
  155. */
  156. public function archive(?string $reason = null): bool
  157. {
  158. if (! $this->canBeArchived()) {
  159. return false;
  160. }
  161. $this->status = AdjustmentStatus::Archived;
  162. $this->status_reason = $reason;
  163. return $this->save();
  164. }
  165. /**
  166. * Check if the adjustment should be automatically resumed
  167. */
  168. public function shouldAutoResume(): bool
  169. {
  170. return $this->status === AdjustmentStatus::Paused &&
  171. $this->paused_until !== null &&
  172. $this->paused_until->isPast();
  173. }
  174. /**
  175. * Refresh the status based on current dates and conditions
  176. */
  177. public function refreshStatus(): bool
  178. {
  179. // Don't automatically change archived or paused status
  180. if ($this->status === AdjustmentStatus::Archived ||
  181. ($this->status === AdjustmentStatus::Paused && ! $this->shouldAutoResume())) {
  182. return false;
  183. }
  184. // Check if a paused adjustment should be auto-resumed
  185. if ($this->shouldAutoResume()) {
  186. return $this->resume();
  187. }
  188. // Calculate natural status based on dates
  189. $naturalStatus = $this->calculateNaturalStatus();
  190. // Only update if the status would change
  191. if ($this->status !== $naturalStatus) {
  192. $this->status = $naturalStatus;
  193. return $this->save();
  194. }
  195. return false;
  196. }
  197. protected static function newFactory(): Factory
  198. {
  199. return AdjustmentFactory::new();
  200. }
  201. }