123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
-
- namespace App\Models\Common;
-
- use App\Casts\MoneyCast;
- use App\Concerns\Blamable;
- use App\Concerns\CompanyOwned;
- use App\Enums\Accounting\AdjustmentCategory;
- use App\Enums\Accounting\AdjustmentType;
- use App\Enums\Common\OfferingType;
- use App\Models\Accounting\Account;
- use App\Models\Accounting\Adjustment;
- use App\Observers\OfferingObserver;
- use Illuminate\Database\Eloquent\Attributes\ObservedBy;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\MorphToMany;
-
- #[ObservedBy(OfferingObserver::class)]
- class Offering extends Model
- {
- use Blamable;
- use CompanyOwned;
- use HasFactory;
-
- protected $fillable = [
- 'company_id',
- 'name',
- 'description',
- 'type',
- 'price',
- 'sellable',
- 'purchasable',
- 'income_account_id',
- 'expense_account_id',
- 'created_by',
- 'updated_by',
- ];
-
- protected $casts = [
- 'type' => OfferingType::class,
- 'price' => MoneyCast::class,
- 'sellable' => 'boolean',
- 'purchasable' => 'boolean',
- ];
-
- public function clearSellableAdjustments(): void
- {
- if (! $this->sellable) {
- $this->income_account_id = null;
-
- $adjustmentIds = $this->salesAdjustments()->pluck('adjustment_id');
-
- $this->adjustments()->detach($adjustmentIds);
- }
- }
-
- public function clearPurchasableAdjustments(): void
- {
- if (! $this->purchasable) {
- $this->expense_account_id = null;
-
- $adjustmentIds = $this->purchaseAdjustments()->pluck('adjustment_id');
-
- $this->adjustments()->detach($adjustmentIds);
- }
- }
-
- public function incomeAccount(): BelongsTo
- {
- return $this->belongsTo(Account::class, 'income_account_id');
- }
-
- public function expenseAccount(): BelongsTo
- {
- return $this->belongsTo(Account::class, 'expense_account_id');
- }
-
- public function adjustments(): MorphToMany
- {
- return $this->morphToMany(Adjustment::class, 'adjustmentable', 'adjustmentables');
- }
-
- public function salesAdjustments(): MorphToMany
- {
- return $this->adjustments()->where('type', AdjustmentType::Sales);
- }
-
- public function purchaseAdjustments(): MorphToMany
- {
- return $this->adjustments()->where('type', AdjustmentType::Purchase);
- }
-
- public function salesTaxes(): MorphToMany
- {
- return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Sales);
- }
-
- public function purchaseTaxes(): MorphToMany
- {
- return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Purchase);
- }
-
- public function salesDiscounts(): MorphToMany
- {
- return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Sales);
- }
-
- public function purchaseDiscounts(): MorphToMany
- {
- return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Purchase);
- }
-
- public function hasInactiveAdjustments(): bool
- {
- return $this->adjustments->contains(function (Adjustment $adjustment) {
- return $adjustment->isInactive();
- });
- }
- }
|