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.

Adjustment.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 'description',
  33. 'category',
  34. 'type',
  35. 'recoverable',
  36. 'rate',
  37. 'computation',
  38. 'scope',
  39. 'start_date',
  40. 'end_date',
  41. 'created_by',
  42. 'updated_by',
  43. ];
  44. protected $casts = [
  45. 'status' => AdjustmentStatus::class,
  46. 'category' => AdjustmentCategory::class,
  47. 'type' => AdjustmentType::class,
  48. 'recoverable' => 'boolean',
  49. 'rate' => RateCast::class,
  50. 'computation' => AdjustmentComputation::class,
  51. 'scope' => AdjustmentScope::class,
  52. 'start_date' => 'datetime',
  53. 'end_date' => 'datetime',
  54. ];
  55. public function account(): BelongsTo
  56. {
  57. return $this->belongsTo(Account::class, 'account_id');
  58. }
  59. public function offerings(): MorphToMany
  60. {
  61. return $this->morphedByMany(Offering::class, 'adjustmentable', 'adjustmentables');
  62. }
  63. public function isSalesTax(): bool
  64. {
  65. return $this->category->isTax() && $this->type->isSales();
  66. }
  67. public function isNonRecoverablePurchaseTax(): bool
  68. {
  69. return $this->category->isTax() && $this->type->isPurchase() && $this->recoverable === false;
  70. }
  71. public function isRecoverablePurchaseTax(): bool
  72. {
  73. return $this->category->isTax() && $this->type->isPurchase() && $this->recoverable === true;
  74. }
  75. public function isSalesDiscount(): bool
  76. {
  77. return $this->category->isDiscount() && $this->type->isSales();
  78. }
  79. public function isPurchaseDiscount(): bool
  80. {
  81. return $this->category->isDiscount() && $this->type->isPurchase();
  82. }
  83. public function calculateStatus(): AdjustmentStatus
  84. {
  85. if ($this->status === AdjustmentStatus::Archived) {
  86. return AdjustmentStatus::Archived;
  87. }
  88. if ($this->start_date?->isFuture()) {
  89. return AdjustmentStatus::Upcoming;
  90. }
  91. if ($this->end_date?->isPast()) {
  92. return AdjustmentStatus::Expired;
  93. }
  94. return AdjustmentStatus::Active;
  95. }
  96. protected static function newFactory(): Factory
  97. {
  98. return AdjustmentFactory::new();
  99. }
  100. }