Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Offering.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Models\Common;
  3. use App\Casts\MoneyCast;
  4. use App\Concerns\Blamable;
  5. use App\Concerns\CompanyOwned;
  6. use App\Enums\Accounting\AdjustmentCategory;
  7. use App\Enums\Accounting\AdjustmentType;
  8. use App\Enums\Common\OfferingType;
  9. use App\Models\Accounting\Account;
  10. use App\Models\Accounting\Adjustment;
  11. use App\Observers\OfferingObserver;
  12. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  13. use Illuminate\Database\Eloquent\Factories\HasFactory;
  14. use Illuminate\Database\Eloquent\Model;
  15. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  16. use Illuminate\Database\Eloquent\Relations\MorphToMany;
  17. #[ObservedBy(OfferingObserver::class)]
  18. class Offering extends Model
  19. {
  20. use Blamable;
  21. use CompanyOwned;
  22. use HasFactory;
  23. protected $fillable = [
  24. 'company_id',
  25. 'name',
  26. 'description',
  27. 'type',
  28. 'price',
  29. 'sellable',
  30. 'purchasable',
  31. 'income_account_id',
  32. 'expense_account_id',
  33. 'created_by',
  34. 'updated_by',
  35. ];
  36. protected $casts = [
  37. 'type' => OfferingType::class,
  38. 'price' => MoneyCast::class,
  39. 'sellable' => 'boolean',
  40. 'purchasable' => 'boolean',
  41. ];
  42. public function clearSellableAdjustments(): void
  43. {
  44. if (! $this->sellable) {
  45. $this->income_account_id = null;
  46. $adjustmentIds = $this->salesAdjustments()->pluck('adjustment_id');
  47. $this->adjustments()->detach($adjustmentIds);
  48. }
  49. }
  50. public function clearPurchasableAdjustments(): void
  51. {
  52. if (! $this->purchasable) {
  53. $this->expense_account_id = null;
  54. $adjustmentIds = $this->purchaseAdjustments()->pluck('adjustment_id');
  55. $this->adjustments()->detach($adjustmentIds);
  56. }
  57. }
  58. public function incomeAccount(): BelongsTo
  59. {
  60. return $this->belongsTo(Account::class, 'income_account_id');
  61. }
  62. public function expenseAccount(): BelongsTo
  63. {
  64. return $this->belongsTo(Account::class, 'expense_account_id');
  65. }
  66. public function adjustments(): MorphToMany
  67. {
  68. return $this->morphToMany(Adjustment::class, 'adjustmentable', 'adjustmentables');
  69. }
  70. public function salesAdjustments(): MorphToMany
  71. {
  72. return $this->adjustments()->where('type', AdjustmentType::Sales);
  73. }
  74. public function purchaseAdjustments(): MorphToMany
  75. {
  76. return $this->adjustments()->where('type', AdjustmentType::Purchase);
  77. }
  78. public function salesTaxes(): MorphToMany
  79. {
  80. return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Sales);
  81. }
  82. public function purchaseTaxes(): MorphToMany
  83. {
  84. return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Purchase);
  85. }
  86. public function salesDiscounts(): MorphToMany
  87. {
  88. return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Sales);
  89. }
  90. public function purchaseDiscounts(): MorphToMany
  91. {
  92. return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Purchase);
  93. }
  94. public function hasInactiveAdjustments(): bool
  95. {
  96. return $this->adjustments->contains(function (Adjustment $adjustment) {
  97. return $adjustment->isInactive();
  98. });
  99. }
  100. }