Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocumentTotalViewModel.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\View\Models;
  3. use App\Enums\Accounting\AdjustmentComputation;
  4. use App\Enums\Accounting\DocumentDiscountMethod;
  5. use App\Enums\Accounting\DocumentType;
  6. use App\Models\Accounting\Adjustment;
  7. use App\Utilities\Currency\CurrencyAccessor;
  8. use App\Utilities\Currency\CurrencyConverter;
  9. use App\Utilities\RateCalculator;
  10. use Illuminate\Support\Number;
  11. class DocumentTotalViewModel
  12. {
  13. public function __construct(
  14. public ?array $data,
  15. public DocumentType $documentType = DocumentType::Invoice,
  16. ) {}
  17. public function buildViewData(): array
  18. {
  19. $currencyCode = $this->data['currency_code'] ?? CurrencyAccessor::getDefaultCurrency();
  20. $defaultCurrencyCode = CurrencyAccessor::getDefaultCurrency();
  21. $lineItems = collect($this->data['lineItems'] ?? []);
  22. $subtotalInCents = $lineItems->sum(fn ($item) => $this->calculateLineSubtotalInCents($item, $currencyCode));
  23. $taxTotalInCents = $this->calculateAdjustmentsTotalInCents($lineItems, $this->documentType->getTaxKey(), $currencyCode);
  24. $discountTotalInCents = $this->calculateDiscountTotalInCents($lineItems, $subtotalInCents, $currencyCode);
  25. $grandTotalInCents = $subtotalInCents + ($taxTotalInCents - $discountTotalInCents);
  26. $amountDueInCents = $this->calculateAmountDueInCents($grandTotalInCents, $currencyCode);
  27. $conversionMessage = $this->buildConversionMessage($grandTotalInCents, $currencyCode, $defaultCurrencyCode);
  28. $discountMethod = DocumentDiscountMethod::parse($this->data['discount_method']);
  29. $isPerDocumentDiscount = $discountMethod->isPerDocument();
  30. $taxTotal = $taxTotalInCents > 0
  31. ? CurrencyConverter::formatCentsToMoney($taxTotalInCents, $currencyCode)
  32. : null;
  33. $discountTotal = ($isPerDocumentDiscount || $discountTotalInCents > 0)
  34. ? CurrencyConverter::formatCentsToMoney($discountTotalInCents, $currencyCode)
  35. : null;
  36. $subtotal = ($taxTotal || $discountTotal)
  37. ? CurrencyConverter::formatCentsToMoney($subtotalInCents, $currencyCode)
  38. : null;
  39. $grandTotal = CurrencyConverter::formatCentsToMoney($grandTotalInCents, $currencyCode);
  40. $amountDue = $this->documentType !== DocumentType::Estimate
  41. ? CurrencyConverter::formatCentsToMoney($amountDueInCents, $currencyCode)
  42. : null;
  43. return [
  44. 'subtotal' => $subtotal,
  45. 'taxTotal' => $taxTotal,
  46. 'discountTotal' => $discountTotal,
  47. 'grandTotal' => $grandTotal,
  48. 'amountDue' => $amountDue,
  49. 'currencyCode' => $currencyCode,
  50. 'conversionMessage' => $conversionMessage,
  51. 'isPerDocumentDiscount' => $isPerDocumentDiscount,
  52. ];
  53. }
  54. private function calculateLineSubtotalInCents(array $item, string $currencyCode): int
  55. {
  56. $quantity = max((float) ($item['quantity'] ?? 0), 0);
  57. $unitPrice = max((float) ($item['unit_price'] ?? 0), 0);
  58. $subtotal = $quantity * $unitPrice;
  59. return CurrencyConverter::convertToCents($subtotal, $currencyCode);
  60. }
  61. private function calculateAdjustmentsTotalInCents($lineItems, string $key, string $currencyCode): int
  62. {
  63. return $lineItems->reduce(function ($carry, $item) use ($key, $currencyCode) {
  64. $quantity = max((float) ($item['quantity'] ?? 0), 0);
  65. $unitPrice = max((float) ($item['unit_price'] ?? 0), 0);
  66. $adjustmentIds = $item[$key] ?? [];
  67. $lineTotal = $quantity * $unitPrice;
  68. $lineTotalInCents = CurrencyConverter::convertToCents($lineTotal, $currencyCode);
  69. $adjustmentTotal = Adjustment::whereIn('id', $adjustmentIds)
  70. ->get()
  71. ->sum(function (Adjustment $adjustment) use ($lineTotalInCents) {
  72. if ($adjustment->computation->isPercentage()) {
  73. return RateCalculator::calculatePercentage($lineTotalInCents, $adjustment->getRawOriginal('rate'));
  74. } else {
  75. return $adjustment->getRawOriginal('rate');
  76. }
  77. });
  78. return $carry + $adjustmentTotal;
  79. }, 0);
  80. }
  81. private function calculateDiscountTotalInCents($lineItems, int $subtotalInCents, string $currencyCode): int
  82. {
  83. $discountMethod = DocumentDiscountMethod::parse($this->data['discount_method']) ?? DocumentDiscountMethod::PerLineItem;
  84. if ($discountMethod->isPerLineItem()) {
  85. return $this->calculateAdjustmentsTotalInCents($lineItems, $this->documentType->getDiscountKey(), $currencyCode);
  86. }
  87. $discountComputation = AdjustmentComputation::parse($this->data['discount_computation']) ?? AdjustmentComputation::Percentage;
  88. $discountRate = blank($this->data['discount_rate']) ? '0' : $this->data['discount_rate'];
  89. if ($discountComputation->isPercentage()) {
  90. $scaledDiscountRate = RateCalculator::parseLocalizedRate($discountRate);
  91. return RateCalculator::calculatePercentage($subtotalInCents, $scaledDiscountRate);
  92. }
  93. if (! CurrencyConverter::isValidAmount($discountRate)) {
  94. $discountRate = '0';
  95. }
  96. return CurrencyConverter::convertToCents($discountRate, $currencyCode);
  97. }
  98. private function buildConversionMessage(int $grandTotalInCents, string $currencyCode, string $defaultCurrencyCode): ?string
  99. {
  100. if ($currencyCode === $defaultCurrencyCode) {
  101. return null;
  102. }
  103. $rate = currency($currencyCode)->getRate();
  104. $indirectRate = 1 / $rate;
  105. $convertedTotalInCents = CurrencyConverter::convertBalance($grandTotalInCents, $currencyCode, $defaultCurrencyCode);
  106. $formattedRate = Number::format($indirectRate, maxPrecision: 10);
  107. return sprintf(
  108. 'Currency conversion: %s (%s) at an exchange rate of 1 %s = %s %s',
  109. CurrencyConverter::formatCentsToMoney($convertedTotalInCents, $defaultCurrencyCode),
  110. $defaultCurrencyCode,
  111. $currencyCode,
  112. $formattedRate,
  113. $defaultCurrencyCode
  114. );
  115. }
  116. private function calculateAmountDueInCents(int $grandTotalInCents, string $currencyCode): int
  117. {
  118. $amountPaid = $this->data['amount_paid'] ?? '0.00';
  119. $amountPaidInCents = CurrencyConverter::convertToCents($amountPaid, $currencyCode);
  120. return $grandTotalInCents - $amountPaidInCents;
  121. }
  122. }