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

BillTotalViewModel.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\View\Models;
  3. use App\Models\Accounting\Adjustment;
  4. use App\Models\Accounting\Bill;
  5. use App\Utilities\Currency\CurrencyConverter;
  6. class BillTotalViewModel
  7. {
  8. public function __construct(
  9. public ?Bill $bill,
  10. public ?array $data = null
  11. ) {}
  12. public function buildViewData(): array
  13. {
  14. $lineItems = collect($this->data['lineItems'] ?? []);
  15. $subtotal = $lineItems->sum(function ($item) {
  16. $quantity = max((float) ($item['quantity'] ?? 0), 0);
  17. $unitPrice = max((float) ($item['unit_price'] ?? 0), 0);
  18. return $quantity * $unitPrice;
  19. });
  20. $taxTotal = $lineItems->reduce(function ($carry, $item) {
  21. $quantity = max((float) ($item['quantity'] ?? 0), 0);
  22. $unitPrice = max((float) ($item['unit_price'] ?? 0), 0);
  23. $purchaseTaxes = $item['purchaseTaxes'] ?? [];
  24. $lineTotal = $quantity * $unitPrice;
  25. $taxAmount = Adjustment::whereIn('id', $purchaseTaxes)
  26. ->pluck('rate')
  27. ->sum(fn ($rate) => $lineTotal * ($rate / 100));
  28. return $carry + $taxAmount;
  29. }, 0);
  30. $discountTotal = $lineItems->reduce(function ($carry, $item) {
  31. $quantity = max((float) ($item['quantity'] ?? 0), 0);
  32. $unitPrice = max((float) ($item['unit_price'] ?? 0), 0);
  33. $purchaseDiscounts = $item['purchaseDiscounts'] ?? [];
  34. $lineTotal = $quantity * $unitPrice;
  35. $discountAmount = Adjustment::whereIn('id', $purchaseDiscounts)
  36. ->pluck('rate')
  37. ->sum(fn ($rate) => $lineTotal * ($rate / 100));
  38. return $carry + $discountAmount;
  39. }, 0);
  40. $grandTotal = $subtotal + ($taxTotal - $discountTotal);
  41. return [
  42. 'subtotal' => CurrencyConverter::formatToMoney($subtotal),
  43. 'taxTotal' => CurrencyConverter::formatToMoney($taxTotal),
  44. 'discountTotal' => CurrencyConverter::formatToMoney($discountTotal),
  45. 'grandTotal' => CurrencyConverter::formatToMoney($grandTotal),
  46. ];
  47. }
  48. }