/** * @param $quantity * @param $taxRate * @param $basePrice * @param $shippingPrice * @return array */ public function getValues($quantity, $taxRate, $basePrice, $shippingPrice) { $subTotal = $quantity * $basePrice; $tax = TaxCalculator::taxFromTotal($basePrice, $taxRate, $quantity); $shippingTotal = $shippingPrice === null ? null : $shippingPrice * $quantity; return ['total' => $subTotal, 'shipping' => $shippingTotal, 'tax' => $tax, 'shippingBeforeTax' => $shippingTotal - TaxCalculator::taxFromTotal($shippingTotal, $taxRate), 'totalBeforeTax' => $subTotal - TaxCalculator::taxFromTotal($subTotal, $taxRate)]; }
/** * @param $orderShippingPrice * @param $chargeTaxOnShipping * @param $freeShipping * @param $defaultTaxRate * @param $items * @return mixed */ public function getValues($orderShippingPrice, $chargeTaxOnShipping, $freeShipping, $defaultTaxRate, $items) { $subTotal = 0; $shipping = $orderShippingPrice; $tax = 0; // add up values for each item foreach ($items as $cartItem) { /* @var $cartItem CartItem */ $cartItemTotals = $cartItem->getValues(new CartItemValuesVATExcludedTransformer()); $subTotal += $cartItemTotals['total']; $shipping += $cartItemTotals['shipping']; $tax += $cartItemTotals['tax']; } // calculate tax on shipping $shippingTax = TaxCalculator::totalPlusTax($shipping, $defaultTaxRate, 1); return ['subTotal' => $subTotal + $tax, 'displayTotal' => $subTotal, 'shipping' => $freeShipping ? 0 : $shipping + $shippingTax, 'shippingTax' => $shippingTax, 'displayShipping' => $shipping, 'tax' => $tax + $shippingTax, 'total' => $subTotal + $shipping + $tax + $shippingTax]; }
/** * @param $orderShippingPrice * @param $chargeTaxOnShipping * @param $freeShipping * @param $defaultTaxRate * @param $items * @return mixed */ public function getValues($orderShippingPrice, $chargeTaxOnShipping, $freeShipping, $defaultTaxRate, $items) { $subTotal = 0; $shipping = $orderShippingPrice; $tax = 0; foreach ($items as $cartItem) { /* @var $cartItem CartItem */ $cartItemTotals = $cartItem->getValues(new CartItemValuesVATIncludedTransformer()); $subTotal += $cartItemTotals['total']; if ($cartItemTotals['shipping'] !== null) { $shipping += $cartItemTotals['shipping']; } $tax += $cartItemTotals['tax']; } // calculate tax on shipping $shippingTax = $shipping === null ? null : TaxCalculator::taxFromTotal($shipping, $defaultTaxRate, 1); return ['subTotal' => $subTotal, 'displayTotal' => $subTotal, 'shipping' => $freeShipping ? 0 : $shipping == null ? null : $shipping, 'displayShipping' => $shipping, 'shippingTax' => $shippingTax, 'tax' => $tax + $shippingTax, 'total' => $subTotal + $shipping]; }
/** * @param $items * @param null $countryId * @param string $shippingType * @return Cart */ public function makeFromIds($items, $countryId = null, $shippingType = '') { $clientInfo = Config::get('clientInfo'); $priceIncludesTax = $clientInfo->business_settings->tax->price_includes_tax; $taxes = ProductTaxOptionFactory::makeFromApi($clientInfo->business_settings->tax->country_tax_rates); $defaultTaxRate = TaxCalculator::calculateProductTaxRate($taxes, $countryId, $clientInfo->business_settings->tax->default_tax_rate); $params = ['ids' => $items, 'priceIncludesTax' => $priceIncludesTax, 'chargeTaxOnShipping' => $clientInfo->business_settings->tax->charge_tax_on_shipping, 'defaultTaxRate' => $defaultTaxRate, 'countryId' => $countryId, 'shippingType' => $shippingType ? $shippingType : null]; $cart = new Cart($params); $cartItemFactory = new CartItemFactory($this->cartRepository, $cart); foreach ($cartItemFactory->makeFromIds($items, $priceIncludesTax, $cart->getDefaultTaxRate()) as $item) { $cart->add($item); } $cart->checkIdList(); // check for free shipping if (isset($clientInfo->business_settings->free_shipping) && $clientInfo->business_settings->free_shipping->free_shipping_enabled) { $cart->updateForFreeShipping($this->cartValuesTransformer, $clientInfo->business_settings->free_shipping->free_shipping_minimum_order, $clientInfo->business_settings->free_shipping->free_shipping_countries); } return $cart; }
/** * @param $countryId * @param int $default */ public function setTaxPrice($countryId, $default = 0) { $this->taxRate = TaxCalculator::calculateProductTaxRate($this->product->getTax(), $countryId, $default); }
/** * @return int */ public function getTotalPrice() { if (!$this->items) { return 0; } $total = 0; foreach ($this->getItems() as $item) { $total += $item->getPrice() + TaxCalculator::totalPlusTax($item->getPrice(), $item->getTaxRate()); } return $total; }