![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/inventory.corals.io/Corals/modules/Inventory/Traits/ |
<?php namespace Corals\Modules\Inventory\Traits; use Corals\Modules\Payment\Common\Models\Tax; use Corals\Modules\Payment\Facades\Payments; trait OrderCalculations { public function calculateOrder($data, $formatCalculationsWithCurrency = true) { $items = data_get($data, 'items', []); $discountType = data_get($data, 'discount_properties.type'); $discountAmount = data_get($data, 'discount_properties.amount'); $currency = data_get($items, 'currency', Payments::session_currency()); $subtotal = $taxTotal = $total = 0; foreach ($items as $type => $typeItems) { foreach ($typeItems as $item) { $itemTotal = $item['unit_price'] * $item['quantity']; $subtotal += $itemTotal; $taxes = $item['taxes'] ?? []; $itemTaxRateTotal = Tax::query()->whereIn('id', $taxes)->sum('rate'); $taxTotal += ($itemTaxRateTotal / 100) * $itemTotal; } } $subtotalWithTax = $subtotal + $taxTotal; $discountAmount = floatval($discountAmount); if ($discountType == 'Percentage') { $discountAmount = ($discountAmount / 100) * $subtotalWithTax; } $total = $subtotalWithTax - $discountAmount; $calculations = [ 'subtotal' => $subtotal, 'tax_total' => $taxTotal, 'discount' => $discountAmount, 'invoiceTotal' => $total, ]; if ($formatCalculationsWithCurrency) { $calculations = array_map(function ($total) use ($currency) { return Payments::currency_convert($total, null, $currency, true); }, $calculations); } return $calculations; } }