You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdjustmentObserver.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\Accounting\AdjustmentStatus;
  4. use App\Models\Accounting\Account;
  5. use App\Models\Accounting\Adjustment;
  6. class AdjustmentObserver
  7. {
  8. public function creating(Adjustment $adjustment): void
  9. {
  10. if (! $adjustment->account_id && ! $adjustment->isNonRecoverablePurchaseTax()) {
  11. $account = null;
  12. if ($adjustment->isSalesTax()) {
  13. $account = Account::factory()->forSalesTax($adjustment->name, $adjustment->description)->create();
  14. } elseif ($adjustment->isRecoverablePurchaseTax()) {
  15. $account = Account::factory()->forPurchaseTax($adjustment->name, $adjustment->description)->create();
  16. } elseif ($adjustment->isSalesDiscount()) {
  17. $account = Account::factory()->forSalesDiscount($adjustment->name, $adjustment->description)->create();
  18. } elseif ($adjustment->isPurchaseDiscount()) {
  19. $account = Account::factory()->forPurchaseDiscount($adjustment->name, $adjustment->description)->create();
  20. }
  21. if ($account) {
  22. $adjustment->account()->associate($account);
  23. }
  24. }
  25. }
  26. public function updating(Adjustment $adjustment): void
  27. {
  28. if ($adjustment->account) {
  29. $adjustment->account->update([
  30. 'name' => $adjustment->name,
  31. 'description' => $adjustment->description,
  32. ]);
  33. }
  34. }
  35. public function saved(Adjustment $adjustment): void
  36. {
  37. if ($adjustment->wasChanged('status') || $adjustment->wasRecentlyCreated) {
  38. if ($adjustment->isInactive()) {
  39. $adjustment->account?->update([
  40. 'archived' => true,
  41. ]);
  42. } else {
  43. $adjustment->account?->update([
  44. 'archived' => false,
  45. ]);
  46. }
  47. }
  48. }
  49. /**
  50. * Handle the Adjustment "saving" event.
  51. */
  52. public function saving(Adjustment $adjustment): void
  53. {
  54. // Handle dates changes affecting status
  55. // Only if the status isn't being explicitly changed and not in a manual state
  56. if ($adjustment->isDirty(['start_date', 'end_date']) &&
  57. ! $adjustment->isDirty('status') &&
  58. ! in_array($adjustment->status, [AdjustmentStatus::Archived, AdjustmentStatus::Paused])) {
  59. $adjustment->status = $adjustment->calculateNaturalStatus();
  60. }
  61. // Handle auto-resume for paused adjustments with a paused_until date
  62. if ($adjustment->shouldAutoResume() && ! $adjustment->isDirty('status')) {
  63. $adjustment->status = $adjustment->calculateNaturalStatus();
  64. $adjustment->paused_at = null;
  65. $adjustment->paused_until = null;
  66. $adjustment->status_reason = null;
  67. }
  68. // Ensure consistency between paused status and paused_at field
  69. if ($adjustment->status === AdjustmentStatus::Paused && ! $adjustment->paused_at) {
  70. $adjustment->paused_at = now();
  71. }
  72. }
  73. }