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

DocumentLineItem.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. public function documentable(): MorphTo
  39. {
  40. return $this->morphTo();
  41. }
  42. public function offering(): BelongsTo
  43. {
  44. return $this->belongsTo(Offering::class);
  45. }
  46. public function sellableOffering(): BelongsTo
  47. {
  48. return $this->offering()->where('sellable', true);
  49. }
  50. public function purchasableOffering(): BelongsTo
  51. {
  52. return $this->offering()->where('purchasable', true);
  53. }
  54. public function adjustments(): MorphToMany
  55. {
  56. return $this->morphToMany(Adjustment::class, 'adjustmentable', 'adjustmentables');
  57. }
  58. public function salesTaxes(): MorphToMany
  59. {
  60. return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Sales);
  61. }
  62. public function purchaseTaxes(): MorphToMany
  63. {
  64. return $this->adjustments()->where('category', AdjustmentCategory::Tax)->where('type', AdjustmentType::Purchase);
  65. }
  66. public function salesDiscounts(): MorphToMany
  67. {
  68. return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Sales);
  69. }
  70. public function purchaseDiscounts(): MorphToMany
  71. {
  72. return $this->adjustments()->where('category', AdjustmentCategory::Discount)->where('type', AdjustmentType::Purchase);
  73. }
  74. public function taxes(): MorphToMany
  75. {
  76. return $this->adjustments()->where('category', AdjustmentCategory::Tax);
  77. }
  78. public function discounts(): MorphToMany
  79. {
  80. return $this->adjustments()->where('category', AdjustmentCategory::Discount);
  81. }
  82. public function calculateTaxTotal(): Money
  83. {
  84. $subtotal = money($this->subtotal, CurrencyAccessor::getDefaultCurrency());
  85. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  86. return $this->taxes->reduce(function (Money $carry, Adjustment $tax) use ($subtotal, $defaultCurrency) {
  87. if ($tax->computation->isPercentage()) {
  88. return $carry->add($subtotal->multiply($tax->rate / 100));
  89. } else {
  90. return $carry->add(money($tax->rate, $defaultCurrency));
  91. }
  92. }, money(0, $defaultCurrency));
  93. }
  94. public function calculateTaxTotalAmount(): int
  95. {
  96. $subtotalInCents = $this->getRawOriginal('subtotal');
  97. return $this->taxes->reduce(function (int $carry, Adjustment $tax) use ($subtotalInCents) {
  98. if ($tax->computation->isPercentage()) {
  99. $scaledRate = $tax->getRawOriginal('rate');
  100. return $carry + RateCalculator::calculatePercentage($subtotalInCents, $scaledRate);
  101. } else {
  102. return $carry + $tax->getRawOriginal('rate');
  103. }
  104. }, 0);
  105. }
  106. public function calculateDiscountTotal(): Money
  107. {
  108. $subtotal = money($this->subtotal, CurrencyAccessor::getDefaultCurrency());
  109. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  110. return $this->discounts->reduce(function (Money $carry, Adjustment $discount) use ($subtotal, $defaultCurrency) {
  111. if ($discount->computation->isPercentage()) {
  112. return $carry->add($subtotal->multiply($discount->rate / 100));
  113. } else {
  114. return $carry->add(money($discount->rate, $defaultCurrency));
  115. }
  116. }, money(0, $defaultCurrency));
  117. }
  118. public function calculateDiscountTotalAmount(): int
  119. {
  120. $subtotalInCents = $this->getRawOriginal('subtotal');
  121. return $this->discounts->reduce(function (int $carry, Adjustment $discount) use ($subtotalInCents) {
  122. if ($discount->computation->isPercentage()) {
  123. $scaledRate = $discount->getRawOriginal('rate');
  124. return $carry + RateCalculator::calculatePercentage($subtotalInCents, $scaledRate);
  125. } else {
  126. return $carry + $discount->getRawOriginal('rate');
  127. }
  128. }, 0);
  129. }
  130. public function calculateAdjustmentTotal(Adjustment $adjustment): Money
  131. {
  132. $subtotal = money($this->subtotal, CurrencyAccessor::getDefaultCurrency());
  133. return $subtotal->multiply($adjustment->rate / 100);
  134. }
  135. public function calculateAdjustmentTotalAmount(Adjustment $adjustment): int
  136. {
  137. $subtotalInCents = $this->getRawOriginal('subtotal');
  138. if ($adjustment->computation->isPercentage()) {
  139. $scaledRate = $adjustment->getRawOriginal('rate');
  140. return RateCalculator::calculatePercentage($subtotalInCents, $scaledRate);
  141. } else {
  142. return $adjustment->getRawOriginal('rate');
  143. }
  144. }
  145. }