Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DocumentLineItem.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Models\Accounting;
  3. use Akaunting\Money\Money;
  4. use App\Casts\DocumentMoneyCast;
  5. use App\Concerns\Blamable;
  6. use App\Concerns\CompanyOwned;
  7. use App\Enums\Accounting\AdjustmentCategory;
  8. use App\Enums\Accounting\AdjustmentType;
  9. use App\Models\Common\Offering;
  10. use App\Observers\DocumentLineItemObserver;
  11. use App\Utilities\Currency\CurrencyAccessor;
  12. use App\Utilities\RateCalculator;
  13. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  14. use Illuminate\Database\Eloquent\Factories\HasFactory;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  17. use Illuminate\Database\Eloquent\Relations\MorphTo;
  18. use Illuminate\Database\Eloquent\Relations\MorphToMany;
  19. #[ObservedBy(DocumentLineItemObserver::class)]
  20. class DocumentLineItem extends Model
  21. {
  22. use Blamable;
  23. use CompanyOwned;
  24. use HasFactory;
  25. protected $table = 'document_line_items';
  26. protected $fillable = [
  27. 'company_id',
  28. 'offering_id',
  29. 'description',
  30. 'quantity',
  31. 'unit_price',
  32. 'tax_total',
  33. 'discount_total',
  34. 'line_number',
  35. 'created_by',
  36. 'updated_by',
  37. ];
  38. protected $casts = [
  39. 'unit_price' => DocumentMoneyCast::class,
  40. 'subtotal' => DocumentMoneyCast::class,
  41. 'tax_total' => DocumentMoneyCast::class,
  42. 'discount_total' => DocumentMoneyCast::class,
  43. 'total' => DocumentMoneyCast::class,
  44. ];
  45. public function documentable(): MorphTo
  46. {
  47. return $this->morphTo();
  48. }
  49. public function offering(): BelongsTo
  50. {
  51. return $this->belongsTo(Offering::class);
  52. }
  53. public function sellableOffering(): BelongsTo
  54. {
  55. return $this->offering()->where('sellable', true);
  56. }
  57. public function purchasableOffering(): BelongsTo
  58. {
  59. return $this->offering()->where('purchasable', true);
  60. }
  61. public function adjustments(): MorphToMany
  62. {
  63. return $this->morphToMany(Adjustment::class, 'adjustmentable', 'adjustmentables');
  64. }
  65. public function salesTaxes(): MorphToMany
  66. {
  67. return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Sales);
  68. }
  69. public function purchaseTaxes(): MorphToMany
  70. {
  71. return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Purchase);
  72. }
  73. public function salesDiscounts(): MorphToMany
  74. {
  75. return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Sales);
  76. }
  77. public function purchaseDiscounts(): MorphToMany
  78. {
  79. return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Purchase);
  80. }
  81. public function taxes(): MorphToMany
  82. {
  83. return $this->adjustments()->where('category', AdjustmentCategory::Tax);
  84. }
  85. public function discounts(): MorphToMany
  86. {
  87. return $this->adjustments()->where('category', AdjustmentCategory::Discount);
  88. }
  89. public function calculateTaxTotal(): Money
  90. {
  91. $subtotal = money($this->subtotal, CurrencyAccessor::getDefaultCurrency());
  92. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  93. return $this->taxes->reduce(function (Money $carry, Adjustment $tax) use ($subtotal, $defaultCurrency) {
  94. if ($tax->computation->isPercentage()) {
  95. return $carry->add($subtotal->multiply($tax->rate / 100));
  96. } else {
  97. return $carry->add(money($tax->rate, $defaultCurrency));
  98. }
  99. }, money(0, $defaultCurrency));
  100. }
  101. public function calculateTaxTotalAmount(): int
  102. {
  103. $subtotalInCents = $this->getRawOriginal('subtotal');
  104. return $this->taxes->reduce(function (int $carry, Adjustment $tax) use ($subtotalInCents) {
  105. if ($tax->computation->isPercentage()) {
  106. $scaledRate = $tax->getRawOriginal('rate');
  107. return $carry + RateCalculator::calculatePercentage($subtotalInCents, $scaledRate);
  108. } else {
  109. return $carry + $tax->getRawOriginal('rate');
  110. }
  111. }, 0);
  112. }
  113. public function calculateDiscountTotal(): Money
  114. {
  115. $subtotal = money($this->subtotal, CurrencyAccessor::getDefaultCurrency());
  116. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  117. return $this->discounts->reduce(function (Money $carry, Adjustment $discount) use ($subtotal, $defaultCurrency) {
  118. if ($discount->computation->isPercentage()) {
  119. return $carry->add($subtotal->multiply($discount->rate / 100));
  120. } else {
  121. return $carry->add(money($discount->rate, $defaultCurrency));
  122. }
  123. }, money(0, $defaultCurrency));
  124. }
  125. public function calculateDiscountTotalAmount(): int
  126. {
  127. $subtotalInCents = $this->getRawOriginal('subtotal');
  128. return $this->discounts->reduce(function (int $carry, Adjustment $discount) use ($subtotalInCents) {
  129. if ($discount->computation->isPercentage()) {
  130. $scaledRate = $discount->getRawOriginal('rate');
  131. return $carry + RateCalculator::calculatePercentage($subtotalInCents, $scaledRate);
  132. } else {
  133. return $carry + $discount->getRawOriginal('rate');
  134. }
  135. }, 0);
  136. }
  137. public function calculateAdjustmentTotal(Adjustment $adjustment): Money
  138. {
  139. $subtotal = money($this->subtotal, CurrencyAccessor::getDefaultCurrency());
  140. return $subtotal->multiply($adjustment->rate / 100);
  141. }
  142. public function calculateAdjustmentTotalAmount(Adjustment $adjustment): int
  143. {
  144. $subtotalInCents = $this->getRawOriginal('subtotal');
  145. if ($adjustment->computation->isPercentage()) {
  146. $scaledRate = $adjustment->getRawOriginal('rate');
  147. return RateCalculator::calculatePercentage($subtotalInCents, $scaledRate);
  148. } else {
  149. return $adjustment->getRawOriginal('rate');
  150. }
  151. }
  152. }