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'))))); }
protected function processChangeProductInCart() { $mode = Tools::getIsset('update') && $this->id_product ? 'update' : 'add'; if (!$this->id_product) { $this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax')); } $product = new Product($this->id_product, true, $this->context->language->id); if (!$product->id || !$product->active || !$product->checkAccess($this->context->cart->id_customer)) { $this->errors[] = Tools::displayError('This product is no longer available.', !Tools::getValue('ajax')); return; } $qty_factor = 1; $ext_qty_factor = 1; $ext_prop_quantities = null; $id_cart_product = 0; $qty_behavior = 0; $icp = (int) Tools::getValue('icp'); $properties = $product->productProperties(); if ($icp && $this->context->cart->id) { $cart_products = $this->context->cart->getProducts(); if (count($cart_products)) { foreach ($cart_products as $cart_product) { if ($icp == (int) $cart_product['id_cart_product']) { $id_cart_product = $icp; if ($mode == 'add') { if (Tools::getValue('qty') != 'default') { $qty_factor = (int) Tools::getValue('qty'); } $_POST['qty'] = (double) $cart_product['cart_quantity_fractional'] > 0 ? (double) $cart_product['cart_quantity_fractional'] : ($product->qtyStep() > 0 ? $product->qtyStep() : 1); } elseif ($mode == 'update') { $qty_behavior = PP::qtyBehavior($product, $cart_product['cart_quantity']); } break; } } } } else { 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(); if ($this->id_product_attribute) { $id_product_attribute = $this->id_product_attribute; } else { if ($product->hasAttributes()) { $id_product_attribute = Product::getDefaultAttribute($product->id); } else { $id_product_attribute = 0; } } } $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, $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) { $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax')); } $min_qty = (double) $pp_ext_prop['minimum_quantity']; if ($min_qty > 0 && $q < $min_qty) { $this->errors[] = Tools::displayError(sprintf('Please specify at least %s for %s', (string) PP::formatQty($min_qty), (string) $pp_ext_prop['property']), !Tools::getValue('ajax')); } $max_qty = (double) $pp_ext_prop['maximum_quantity']; if ($max_qty > 0 && $q > $max_qty) { $this->errors[] = Tools::displayError(sprintf('Please specify no more than %s for %s', (string) PP::formatQty($max_qty), (string) $pp_ext_prop['property']), !Tools::getValue('ajax')); } } if (!$this->errors) { $ext_qty_factor = $properties['pp_ext_method'] == 1 ? 1 : 0; $positions = count($ext_prop_quantities); for ($position = 1; $position <= $positions; $position++) { $value = $ext_prop_quantities[$position]; $qty_ratio = $ext_prop_qty_ratio[$position]; if ($properties['pp_ext_method'] == 1) { $ext_qty_factor *= $qty_ratio > 0 ? $value / $qty_ratio : $value; } elseif ($properties['pp_ext_method'] == 2) { $ext_qty_factor += $qty_ratio > 0 ? $value / $qty_ratio : $value; } } } } } if (!$this->errors) { if ($this->id_product_attribute) { $default_quantity = $product->attributeDefaultQty($this->id_product_attribute); $this->qty = $qty_factor * $this->resolveInputQty($properties, $default_quantity); if ($this->qty == 0) { $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax')); } else { if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $ext_qty_factor * $this->qty)) { $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax')); } } } else { if ($product->hasAttributes()) { $min_quantity = $product->out_of_stock == 2 ? !Configuration::get('PS_ORDER_OUT_OF_STOCK') : !$product->out_of_stock; $this->id_product_attribute = Product::getDefaultAttribute($product->id, $min_quantity); if (!$this->id_product_attribute) { Tools::redirectAdmin($this->context->link->getProductLink($product)); } else { $default_quantity = $product->attributeDefaultQty($this->id_product_attribute); $this->qty = $qty_factor * $this->resolveInputQty($properties, $default_quantity); if ($this->qty == 0) { $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax')); } else { if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && !Attribute::checkAttributeQty($this->id_product_attribute, $ext_qty_factor * $this->qty)) { $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax')); } } } } else { $default_quantity = $product->defaultQty(); $this->qty = $qty_factor * $this->resolveInputQty($properties, $default_quantity); if ($this->qty == 0) { $this->errors[] = Tools::displayError('Quantity not specified.', !Tools::getValue('ajax')); } else { if (!$product->checkQty($ext_qty_factor * $this->qty)) { $this->errors[] = Tools::displayError('There isn\'t enough product in stock.', !Tools::getValue('ajax')); } } } } } if (!$this->errors && ($mode == 'add' || $mode == 'update' && $qty_behavior)) { if ($mode == 'add' && !$this->context->cart->id) { if (Context::getContext()->cookie->id_guest) { $guest = new Guest(Context::getContext()->cookie->id_guest); $this->context->cart->mobile_theme = $guest->mobile_theme; } $this->context->cart->add(); if ($this->context->cart->id) { $this->context->cookie->id_cart = (int) $this->context->cart->id; } } if (!$product->hasAllRequiredCustomizableFields() && !$this->customization_id) { $this->errors[] = Tools::displayError('Please fill in all of the required fields, and then save your customizations.', !Tools::getValue('ajax')); } if (!$this->errors) { $cart_rules = $this->context->cart->getCartRules(); $update_quantity = $this->context->cart->updateQty($id_cart_product ? $mode == 'add' ? $qty_factor : $this->qty : ($ext_prop_quantities !== null ? $ext_qty_factor : $this->qty), $this->id_product, $this->id_product_attribute, $this->customization_id, $mode == 'update' ? 'update' : Tools::getValue('op', 'up'), $this->id_address_delivery, null, true, $id_cart_product, $ext_prop_quantities, $this->qty); if ($update_quantity < 0) { $minimal_quantity = $this->id_product_attribute ? $product->attributeMinQty($this->id_product_attribute) : $product->minQty(); $this->errors[] = Tools::displayError(sprintf('You must add %s minimum quantity', $minimal_quantity), !Tools::getValue('ajax')); } elseif (!$update_quantity) { $this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.', !Tools::getValue('ajax')); } elseif ((int) Tools::getValue('allow_refresh')) { $cart_rules2 = $this->context->cart->getCartRules(); if (count($cart_rules2) != count($cart_rules)) { $this->ajax_refresh = true; } else { $rule_list = array(); foreach ($cart_rules2 as $rule) { $rule_list[] = $rule['id_cart_rule']; } foreach ($cart_rules as $rule) { if (!in_array($rule['id_cart_rule'], $rule_list)) { $this->ajax_refresh = true; break; } } } } } } $removed = CartRule::autoRemoveFromCart(); CartRule::autoAddToCart(); if (count($removed) && (int) Tools::getValue('allow_refresh')) { $this->ajax_refresh = true; } }
protected function doEditProductValidation(OrderDetail $order_detail, Order $order, OrderInvoice $order_invoice = null) { if (!Validate::isLoadedObject($order_detail)) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('The Order Detail object could not be loaded.')))); } if (!empty($order_invoice) && !Validate::isLoadedObject($order_invoice)) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('The invoice object cannot be loaded.')))); } if (!Validate::isLoadedObject($order)) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('The order object cannot be loaded.')))); } if ($order_detail->id_order != $order->id) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('You cannot edit the order detail for this order.')))); } // We can't edit a delivered order if ($order->hasBeenDelivered()) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('You cannot edit a delivered order.')))); } if (!empty($order_invoice) && $order_invoice->id_order != Tools::getValue('id_order')) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('You cannot use this invoice for the order')))); } // Clean price $product_price_tax_incl = str_replace(',', '.', Tools::getValue('product_price_tax_incl')); $product_price_tax_excl = str_replace(',', '.', Tools::getValue('product_price_tax_excl')); if (!Validate::isPrice($product_price_tax_incl) || !Validate::isPrice($product_price_tax_excl)) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('Invalid price')))); } if (!is_array(Tools::getValue('product_quantity')) && !(PP::qtyBehavior($order_detail, $order_detail->product_quantity) ? Validate::isUnsignedFloat(str_replace(",", ".", Tools::getValue("product_quantity"))) : Validate::isUnsignedInt(Tools::getValue("product_quantity")))) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('Invalid quantity')))); } elseif (is_array(Tools::getValue('product_quantity'))) { foreach (Tools::getValue('product_quantity') as $qty) { if (!Validate::isUnsignedInt($qty)) { die(Tools::jsonEncode(array('result' => false, 'error' => Tools::displayError('Invalid quantity')))); } } } }