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

InvoiceTotalViewModel.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\View\Models;
  3. use App\Models\Accounting\Adjustment;
  4. use App\Models\Accounting\Document;
  5. use App\Utilities\Currency\CurrencyConverter;
  6. class InvoiceTotalViewModel
  7. {
  8. public function __construct(
  9. public ?Document $invoice,
  10. public ?array $data = null
  11. ) {}
  12. public function buildViewData(): array
  13. {
  14. $lineItems = collect($this->data['lineItems'] ?? []);
  15. $subtotal = $lineItems->sum(fn ($item) => ($item['quantity'] ?? 0) * ($item['unit_price'] ?? 0));
  16. $taxTotal = $lineItems->reduce(function ($carry, $item) {
  17. $quantity = $item['quantity'] ?? 0;
  18. $unitPrice = $item['unit_price'] ?? 0;
  19. $salesTaxes = $item['salesTaxes'] ?? [];
  20. $lineTotal = $quantity * $unitPrice;
  21. $taxAmount = Adjustment::whereIn('id', $salesTaxes)
  22. ->pluck('rate')
  23. ->sum(fn ($rate) => $lineTotal * ($rate / 100));
  24. return $carry + $taxAmount;
  25. }, 0);
  26. $grandTotal = $subtotal + $taxTotal;
  27. $subTotalFormatted = CurrencyConverter::formatToMoney($subtotal);
  28. $taxTotalFormatted = CurrencyConverter::formatToMoney($taxTotal);
  29. $grandTotalFormatted = CurrencyConverter::formatToMoney($grandTotal);
  30. return [
  31. 'subtotal' => $subTotalFormatted,
  32. 'taxTotal' => $taxTotalFormatted,
  33. 'grandTotal' => $grandTotalFormatted,
  34. ];
  35. }
  36. }