您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Offering.php 3.3KB

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