private function calcPrice($product, &$quantity) { $quantity = $product['cart_quantity']; if ($quantity > 1) { $price1 = PP::calcPrice($product['price_wt'], $quantity, $product['cart_quantity_fractional'], $product); $price2 = PP::calcPrice($product['price_wt'], $quantity, $product['cart_quantity_fractional'], null); if ($price1 != $price2) { $price = $price1; $quantity = 1; } else { $price = PP::calcPrice($product['price_wt'], 1, $product['cart_quantity_fractional'], null); } } else { $price = PP::calcPrice($product['price_wt'], 1, $product['cart_quantity_fractional'], $product); } return $price; }
public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products = null, $id_carrier = null, $use_cache = true) { static $address = null; if (!$this->id) { return 0; } $type = (int) $type; $array_type = array(Cart::ONLY_PRODUCTS, Cart::ONLY_DISCOUNTS, Cart::BOTH, Cart::BOTH_WITHOUT_SHIPPING, Cart::ONLY_SHIPPING, Cart::ONLY_WRAPPING, Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING, Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING); // Define virtual context to prevent case where the cart is not the in the global context $virtual_context = Context::getContext()->cloneContext(); $virtual_context->cart = $this; if (!in_array($type, $array_type)) { die(Tools::displayError()); } $with_shipping = in_array($type, array(Cart::BOTH, Cart::ONLY_SHIPPING)); // if cart rules are not used if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive()) { return 0; } // no shipping cost if is a cart with only virtuals products $virtual = $this->isVirtualCart(); if ($virtual && $type == Cart::ONLY_SHIPPING) { return 0; } if ($virtual && $type == Cart::BOTH) { $type = Cart::BOTH_WITHOUT_SHIPPING; } if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) { if (is_null($products) && is_null($id_carrier)) { $shipping_fees = $this->getTotalShippingCost(null, (bool) $with_taxes); } else { $shipping_fees = $this->getPackageShippingCost($id_carrier, (bool) $with_taxes, null, $products); } } else { $shipping_fees = 0; } if ($type == Cart::ONLY_SHIPPING) { return $shipping_fees; } if ($type == Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING) { $type = Cart::ONLY_PRODUCTS; } $param_product = true; if (is_null($products)) { $param_product = false; $products = $this->getProducts(); } if ($type == Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING) { foreach ($products as $key => $product) { if ($product['is_virtual']) { unset($products[$key]); } } $type = Cart::ONLY_PRODUCTS; } $order_total = 0; if (Tax::excludeTaxeOption()) { $with_taxes = false; } $products_total = array(); $ecotax_total = 0; foreach ($products as $product) { if ($virtual_context->shop->id != $product['id_shop']) { $virtual_context->shop = new Shop((int) $product['id_shop']); } if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') { $id_address = (int) $this->id_address_invoice; } else { $id_address = (int) $product['id_address_delivery']; } // Get delivery address of the product from the cart if (!Address::addressExists($id_address)) { $id_address = null; } $null = null; $price = Product::getPriceStatic((int) $product['id_product'], false, (int) $product['id_product_attribute'], 6, null, false, true, array($product['cart_quantity'], $product['cart_quantity_fractional']), false, (int) $this->id_customer ? (int) $this->id_customer : null, (int) $this->id, $id_address, $null, false, true, $virtual_context); if (Configuration::get('PS_USE_ECOTAX')) { $ecotax = $product['ecotax']; if (isset($product['attribute_ecotax']) && $product['attribute_ecotax'] > 0) { $ecotax = $product['attribute_ecotax']; } } else { $ecotax = 0; } $address = Address::initialize($id_address, true); if ($with_taxes) { $id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int) $product['id_product'], $virtual_context); $tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator(); if ($ecotax) { $ecotax_tax_calculator = TaxManagerFactory::getManager($address, (int) Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID'))->getTaxCalculator(); } } else { $id_tax_rules_group = 0; } if (in_array(Configuration::get('PS_ROUND_TYPE'), array(Order::ROUND_ITEM, Order::ROUND_LINE))) { if (!isset($products_total[$id_tax_rules_group])) { $products_total[$id_tax_rules_group] = 0; } } else { if (!isset($products_total[$id_tax_rules_group . '_' . $id_address])) { $products_total[$id_tax_rules_group . '_' . $id_address] = 0; } } switch (Configuration::get('PS_ROUND_TYPE')) { case Order::ROUND_TOTAL: $products_total[$id_tax_rules_group . '_' . $id_address] += PP::calcPrice($price, $product['cart_quantity'], $product['cart_quantity_fractional'], (int) $product['id_product'], false); $ppropertiessmartprice_hook3 = null; if ($ecotax) { $ecotax_total += PP::calcPrice($ecotax, $product['cart_quantity'], $product['cart_quantity_fractional'], null, false); } break; case Order::ROUND_LINE: $product_price = PP::calcPrice($price, $product['cart_quantity'], $product['cart_quantity_fractional'], (int) $product['id_product'], false); $ppropertiessmartprice_hook4 = null; if ($with_taxes) { $products_total[$id_tax_rules_group] += Tools::ps_round($product_price + $tax_calculator->getTaxesTotalAmount($product_price), _PS_PRICE_COMPUTE_PRECISION_); } else { $products_total[$id_tax_rules_group] += Tools::ps_round($product_price, _PS_PRICE_COMPUTE_PRECISION_); } if ($ecotax) { $ecotax_price = PP::calcPrice($ecotax, $product['cart_quantity'], $product['cart_quantity_fractional'], null, false); if ($with_taxes) { $ecotax_total += Tools::ps_round($ecotax_price + $ecotax_tax_calculator->getTaxesTotalAmount($ecotax_price), _PS_PRICE_COMPUTE_PRECISION_); } else { $ecotax_total += Tools::ps_round($ecotax_price, _PS_PRICE_COMPUTE_PRECISION_); } } break; case Order::ROUND_ITEM: default: $product_price = $with_taxes ? $tax_calculator->addTaxes($price) : $price; $products_total[$id_tax_rules_group] += PP::calcPrice($product_price, $product['cart_quantity'], $product['cart_quantity_fractional'], (int) $product['id_product'], Order::ROUND_ITEM); $ppropertiessmartprice_hook5 = null; if ($ecotax) { $ecotax_price = $with_taxes ? $ecotax_tax_calculator->addTaxes($ecotax) : $ecotax; $ecotax_total += PP::calcPrice($ecotax_price, $product['cart_quantity'], $product['cart_quantity_fractional'], null, Order::ROUND_ITEM); } break; } } foreach ($products_total as $key => $price) { if ($with_taxes && Configuration::get('PS_ROUND_TYPE') == Order::ROUND_TOTAL) { $tmp = explode('_', $key); $address = Address::initialize((int) $tmp[1], true); $tax_calculator = TaxManagerFactory::getManager($address, $tmp[0])->getTaxCalculator(); $order_total += Tools::ps_round($price + $tax_calculator->getTaxesTotalAmount($price), _PS_PRICE_COMPUTE_PRECISION_); } else { $order_total += $price; } } if ($ecotax_total && $with_taxes && Configuration::get('PS_ROUND_TYPE') == Order::ROUND_TOTAL) { $ecotax_total = Tools::ps_round($ecotax_total, _PS_PRICE_COMPUTE_PRECISION_) + Tools::ps_round($ecotax_tax_calculator->getTaxesTotalAmount($ecotax_total), _PS_PRICE_COMPUTE_PRECISION_); } $order_total += $ecotax_total; $order_total_products = $order_total; if ($type == Cart::ONLY_DISCOUNTS) { $order_total = 0; } // Wrapping Fees $wrapping_fees = 0; if ($this->gift) { $wrapping_fees = Tools::convertPrice(Tools::ps_round($this->getGiftWrappingPrice($with_taxes), _PS_PRICE_COMPUTE_PRECISION_), Currency::getCurrencyInstance((int) $this->id_currency)); } if ($type == Cart::ONLY_WRAPPING) { return $wrapping_fees; } $order_total_discount = 0; $order_shipping_discount = 0; if (!in_array($type, array(Cart::ONLY_SHIPPING, Cart::ONLY_PRODUCTS)) && CartRule::isFeatureActive()) { // First, retrieve the cart rules associated to this "getOrderTotal" if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) { $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_ALL); } else { $cart_rules = $this->getCartRules(CartRule::FILTER_ACTION_REDUCTION); // Cart Rules array are merged manually in order to avoid doubles foreach ($this->getCartRules(CartRule::FILTER_ACTION_GIFT) as $tmp_cart_rule) { $flag = false; foreach ($cart_rules as $cart_rule) { if ($tmp_cart_rule['id_cart_rule'] == $cart_rule['id_cart_rule']) { $flag = true; } } if (!$flag) { $cart_rules[] = $tmp_cart_rule; } } } $id_address_delivery = 0; if (isset($products[0])) { $id_address_delivery = is_null($products) ? $this->id_address_delivery : $products[0]['id_address_delivery']; } $package = array('id_carrier' => $id_carrier, 'id_address' => $id_address_delivery, 'products' => $products); // Then, calculate the contextual value for each one $flag = false; foreach ($cart_rules as $cart_rule) { // If the cart rule offers free shipping, add the shipping cost if (($with_shipping || $type == Cart::ONLY_DISCOUNTS) && $cart_rule['obj']->free_shipping && !$flag) { $order_shipping_discount = (double) Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_SHIPPING, $param_product ? $package : null, $use_cache), _PS_PRICE_COMPUTE_PRECISION_); $flag = true; } // If the cart rule is a free gift, then add the free gift value only if the gift is in this package if ((int) $cart_rule['obj']->gift_product) { $in_order = false; if (is_null($products)) { $in_order = true; } else { foreach ($products as $product) { if ($cart_rule['obj']->gift_product == $product['id_product'] && $cart_rule['obj']->gift_product_attribute == $product['id_product_attribute']) { $in_order = true; } } } if ($in_order) { $order_total_discount += $cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_GIFT, $package, $use_cache); } } // If the cart rule offers a reduction, the amount is prorated (with the products in the package) if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount > 0) { $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), _PS_PRICE_COMPUTE_PRECISION_); } } $order_total_discount = min(Tools::ps_round($order_total_discount, 2), (double) $order_total_products) + (double) $order_shipping_discount; $order_total -= $order_total_discount; } if ($type == Cart::BOTH) { $order_total += $shipping_fees + $wrapping_fees; } if ($order_total < 0 && $type != Cart::ONLY_DISCOUNTS) { return 0; } if ($type == Cart::ONLY_DISCOUNTS) { return $order_total_discount; } return Tools::ps_round((double) $order_total, _PS_PRICE_COMPUTE_PRECISION_); }
public function ajaxProcessEditProductOnOrder() { // Return value $res = true; $order = new Order((int) Tools::getValue('id_order')); $order_detail = new OrderDetail((int) Tools::getValue('product_id_order_detail')); if (Tools::isSubmit('product_invoice')) { $order_invoice = new OrderInvoice((int) Tools::getValue('product_invoice')); } // Check fields validity $this->doEditProductValidation($order_detail, $order, isset($order_invoice) ? $order_invoice : null); // If multiple product_quantity, the order details concern a product customized $qty_behavior = PP::qtyBehavior($order_detail, $order_detail->product_quantity) && !is_array(Tools::getValue('product_quantity')); $product_quantity = 0; if (is_array(Tools::getValue('product_quantity'))) { foreach (Tools::getValue('product_quantity') as $id_customization => $qty) { // Update quantity of each customization Db::getInstance()->update('customization', array('quantity' => (int) $qty), 'id_customization = ' . (int) $id_customization); // Calculate the real quantity of the product $product_quantity += $qty; } } else { $product_quantity = str_replace(',', '.', Tools::getValue('product_quantity')); } $product_price_tax_incl = Tools::ps_round(Tools::getValue('product_price_tax_incl'), 2); $product_price_tax_excl = Tools::ps_round(Tools::getValue('product_price_tax_excl'), 2); $total_products_tax_incl = PP::calcPrice($product_price_tax_incl, $qty_behavior ? $order_detail->product_quantity : $product_quantity, $qty_behavior ? $product_quantity : $order_detail->product_quantity_fractional, $order_detail->product_id); $total_products_tax_excl = PP::calcPrice($product_price_tax_excl, $qty_behavior ? $order_detail->product_quantity : $product_quantity, $qty_behavior ? $product_quantity : $order_detail->product_quantity_fractional, $order_detail->product_id); // Calculate differences of price (Before / After) $diff_price_tax_incl = $total_products_tax_incl - $order_detail->total_price_tax_incl; $diff_price_tax_excl = $total_products_tax_excl - $order_detail->total_price_tax_excl; $ppropertiessmartprice_hook1 = null; // Apply change on OrderInvoice if (isset($order_invoice)) { // If OrderInvoice to use is different, we update the old invoice and new invoice if ($order_detail->id_order_invoice != $order_invoice->id) { $old_order_invoice = new OrderInvoice($order_detail->id_order_invoice); // We remove cost of products $old_order_invoice->total_products -= $order_detail->total_price_tax_excl; $old_order_invoice->total_products_wt -= $order_detail->total_price_tax_incl; $old_order_invoice->total_paid_tax_excl -= $order_detail->total_price_tax_excl; $old_order_invoice->total_paid_tax_incl -= $order_detail->total_price_tax_incl; $res &= $old_order_invoice->update(); $order_invoice->total_products += $order_detail->total_price_tax_excl; $order_invoice->total_products_wt += $order_detail->total_price_tax_incl; $order_invoice->total_paid_tax_excl += $order_detail->total_price_tax_excl; $order_invoice->total_paid_tax_incl += $order_detail->total_price_tax_incl; $order_detail->id_order_invoice = $order_invoice->id; } } if ($diff_price_tax_incl != 0 && $diff_price_tax_excl != 0) { $order_detail->unit_price_tax_excl = $product_price_tax_excl; $order_detail->unit_price_tax_incl = $product_price_tax_incl; $order_detail->total_price_tax_incl += $diff_price_tax_incl; $order_detail->total_price_tax_excl += $diff_price_tax_excl; if (isset($order_invoice)) { // Apply changes on OrderInvoice $order_invoice->total_products += $diff_price_tax_excl; $order_invoice->total_products_wt += $diff_price_tax_incl; $order_invoice->total_paid_tax_excl += $diff_price_tax_excl; $order_invoice->total_paid_tax_incl += $diff_price_tax_incl; } // Apply changes on Order $order = new Order($order_detail->id_order); $order->total_products += $diff_price_tax_excl; $order->total_products_wt += $diff_price_tax_incl; $order->total_paid += $diff_price_tax_incl; $order->total_paid_tax_excl += $diff_price_tax_excl; $order->total_paid_tax_incl += $diff_price_tax_incl; $res &= $order->update(); } $old_quantity = PP::resolveQty($order_detail->product_quantity, $order_detail->product_quantity_fractional); if ($qty_behavior) { $order_detail->product_quantity_fractional = $product_quantity; } else { $order_detail->product_quantity = $product_quantity; } // update taxes $res &= $order_detail->updateTaxAmount($order); // Save order detail $res &= $order_detail->update(); // Update weight SUM $order_carrier = new OrderCarrier((int) $order->getIdOrderCarrier()); if (Validate::isLoadedObject($order_carrier)) { $order_carrier->weight = (double) $order->getTotalWeight(); $res &= $order_carrier->update(); if ($res) { $order->weight = sprintf('%.3f ' . Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight); } } // Save order invoice if (isset($order_invoice)) { $res &= $order_invoice->update(); } // Update product available quantity StockAvailable::updateQuantity($order_detail->product_id, $order_detail->product_attribute_id, $old_quantity - PP::resolveQty($order_detail->product_quantity, $order_detail->product_quantity_fractional), $order->id_shop); $products = $this->getProducts($order); // Get the last product $product = $products[$order_detail->id]; $resume = OrderSlip::getProductSlipResume($order_detail->id); $product['quantity_refundable'] = $product['product_quantity'] - $resume['product_quantity']; $product['amount_refundable'] = $product['total_price_tax_excl'] - $resume['amount_tax_excl']; $product['amount_refund'] = Tools::displayPrice($resume['amount_tax_incl']); $product['refund_history'] = OrderSlip::getProductSlipDetail($order_detail->id); if ($product['id_warehouse'] != 0) { $warehouse = new Warehouse((int) $product['id_warehouse']); $product['warehouse_name'] = $warehouse->name; } else { $product['warehouse_name'] = '--'; } // Get invoices collection $invoice_collection = $order->getInvoicesCollection(); $invoice_array = array(); foreach ($invoice_collection as $invoice) { $invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int) $order->id_shop); $invoice_array[] = $invoice; } // Assign to smarty informations in order to show the new product line $this->context->smarty->assign(array('product' => $product, 'order' => $order, 'currency' => new Currency($order->id_currency), 'can_edit' => $this->tabAccess['edit'], 'invoices_collection' => $invoice_collection, 'current_id_lang' => Context::getContext()->language->id, 'link' => Context::getContext()->link, 'current_index' => self::$currentIndex, 'display_warehouse' => (int) Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))); if (!$res) { die(Tools::jsonEncode(array('result' => $res, 'error' => Tools::displayError('An error occurred while editing the product line.')))); } if (is_array(Tools::getValue('product_quantity'))) { $view = $this->createTemplate('_customized_data.tpl')->fetch(); } else { $view = $this->createTemplate('_product_line.tpl')->fetch(); } $this->sendChangedNotification($order); die(Tools::jsonEncode(array('result' => $res, 'view' => $view, 'can_edit' => $this->tabAccess['add'], 'invoices_collection' => $invoice_collection, 'order' => $order, 'invoices' => $invoice_array, 'documents_html' => $this->createTemplate('_documents.tpl')->fetch(), 'shipping_html' => $this->createTemplate('_shipping.tpl')->fetch(), 'customized_product' => is_array(Tools::getValue('product_quantity'))))); }
private function processPrice() { if ($this->id_product) { $product = new Product($this->id_product, true, $this->context->language->id); if ($product->id && $product->active) { $error = false; $properties = $product->productProperties(); if ($properties['pp_ext'] == 1 && in_array($properties['pp_ext_policy'], array(0, 2))) { $qty = PP::resolveInputQty(Tools::getValue('qty'), 0, $properties['pp_qty_step']); } else { $qty = PP::resolveInputQty(Tools::getValue('qty'), $properties['pp_qty_policy'], $properties['pp_qty_step']); } if ($qty <= 0) { $error = true; } if (!$error) { if ($properties['pp_ext'] == 1 && in_array($properties['pp_ext_policy'], array(0, 2))) { $ext_prop_quantities = array(); $ext_prop_qty_ratio = array(); if ($properties['pp_ext_policy'] == 2) { $prop = $product->productProp(); } $positions = count($properties['pp_ext_prop']); for ($position = 1; $position <= $positions; $position++) { $pp_ext_prop = $properties['pp_ext_prop'][$position]; if ($properties['pp_ext_policy'] == 2) { $q = PP::productProp($prop, $this->id_product_attribute, $position, 'quantity'); if ($q === false) { $q = (double) $pp_ext_prop['default_quantity']; } if ($q <= 0) { $q = 1; } } else { $q = PP::resolveInputQty(Tools::getValue('pp_ext_prop_quantity_' . $position, 'default'), $properties['pp_qty_policy'], $pp_ext_prop['qty_step'], $pp_ext_prop['default_quantity'] > 0 ? $pp_ext_prop['default_quantity'] : 1); } $ext_prop_quantities[$position] = $q; $ext_prop_qty_ratio[$position] = $properties['pp_ext_prop'][$position]['qty_ratio']; if ($q <= 0) { $error = true; } $min_qty = (double) $pp_ext_prop['minimum_quantity']; if ($min_qty > 0 && $q < $min_qty) { $error = true; } $max_qty = (double) $pp_ext_prop['maximum_quantity']; if ($max_qty > 0 && $q > $max_qty) { $error = true; } } if (!$error) { $ext_prop_qty = array(); $quantity = $qty; $quantity_fractional = $properties['pp_ext_method'] == 1 ? 1 : 0; $count = count($ext_prop_quantities); for ($position = 1; $position <= $count; $position++) { $value = $ext_prop_quantities[$position]; $qty_ratio = $ext_prop_qty_ratio[$position]; $ext_prop_qty[$position] = $qty_ratio > 0 ? $value / $qty_ratio : $value; if ($properties['pp_ext_method'] == 1) { $quantity_fractional *= $ext_prop_qty[$position]; } elseif ($properties['pp_ext_method'] == 2) { $quantity_fractional += $ext_prop_qty[$position]; } } } } else { if (PP::qtyPolicyFractional($properties['pp_qty_policy'])) { $quantity = 1; $quantity_fractional = $qty; } else { $quantity = $qty; $quantity_fractional = 0; } } } if (!$error) { $id_customer = isset($this->context->customer) ? $this->context->customer->id : null; $this->id_product_attribute = $this->id_product_attribute > 0 ? $this->id_product_attribute : null; $qty = array($quantity, $quantity_fractional); $address = new Address($this->context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}); $tax = (double) $product->getTaxesRate($address); $no_tax = Tax::excludeTaxeOption() || !$tax || Group::getPriceDisplayMethod($this->context->customer->id_default_group); $specific_price_output = null; $total = Product::getPriceStatic($this->id_product, !$no_tax, $this->id_product_attribute, 6, null, false, true, $qty, false, $id_customer, null, $address->id, $specific_price_output, true, true, $this->context); $total = PP::calcPrice($total, $quantity, $quantity_fractional, $this->id_product, false); $result = array('status' => 'success', 'total' => &$total); /*[HOOK ppropertiessmartprice]*/ $result['total'] = Tools::ps_round($total, _PS_PRICE_COMPUTE_PRECISION_); } } } if (!isset($result)) { $result = array('status' => 'error'); } die(Tools::jsonEncode($result)); }