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 $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' => $qty), 'id_customization = ' . (int) $id_customization); // Calculate the real quantity of the product $product_quantity += $qty; } } else { $product_quantity = 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 = $product_price_tax_incl * $product_quantity; $total_products_tax_excl = $product_price_tax_excl * $product_quantity; // 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; // 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 = $order_detail->product_quantity; $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 - $order_detail->product_quantity, $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_incl'] - $resume['amount_tax_incl']; $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'))))); }
public function ajaxProcessEditProductOnOrder() { $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')); } $this->doEditProductValidation($order_detail, $order, isset($order_invoice) ? $order_invoice : null); $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) { Db::getInstance()->update('customization', array('quantity' => (int) $qty), 'id_customization = ' . (int) $id_customization); $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); $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; if (isset($order_invoice)) { if ($order_detail->id_order_invoice != $order_invoice->id) { $old_order_invoice = new OrderInvoice($order_detail->id_order_invoice); $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)) { $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; } $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; } $res &= $order_detail->updateTaxAmount($order); $res &= $order_detail->update(); $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); } } if (isset($order_invoice)) { $res &= $order_invoice->update(); } 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); $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'] = '--'; } $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; } $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'))))); }
public function addPartialSlipDetail($order_detail_list) { foreach ($order_detail_list as $id_order_detail => $tab) { $order_detail = new OrderDetail($id_order_detail); $order_slip_resume = OrderSlip::getProductSlipResume($id_order_detail); if ($tab['amount'] + $order_slip_resume['amount_tax_incl'] > $order_detail->total_price_tax_incl) { $tab['amount'] = $order_detail->total_price_tax_incl - $order_slip_resume['amount_tax_incl']; } if ($tab['amount'] == 0) { continue; } if ($tab['quantity'] + $order_slip_resume['product_quantity'] > $order_detail->product_quantity) { $tab['quantity'] = $order_detail->product_quantity - $order_slip_resume['product_quantity']; } $tab['amount_tax_excl'] = $tab['amount_tax_incl'] = $tab['amount']; $id_tax = (int) Db::getInstance()->getValue(' SELECT `id_tax` FROM `' . _DB_PREFIX_ . 'order_detail_tax` WHERE `id_order_detail` = ' . (int) $id_order_detail); if ($id_tax > 0) { $rate = (double) Db::getInstance()->getValue(' SELECT `rate` FROM `' . _DB_PREFIX_ . 'tax` WHERE `id_tax` = ' . (int) $id_tax); if ($rate > 0) { $rate = 1 + $rate / 100; $tab['amount_tax_excl'] = $tab['amount_tax_excl'] / $rate; } } if ($tab['quantity'] > 0 && $tab['quantity'] > $order_detail->product_quantity_refunded) { $order_detail->product_quantity_refunded = $tab['quantity']; $order_detail->save(); } $insert_order_slip = array('id_order_slip' => (int) $this->id, 'id_order_detail' => (int) $id_order_detail, 'product_quantity' => (int) $tab['quantity'], 'amount_tax_excl' => (double) $tab['amount_tax_excl'], 'amount_tax_incl' => (double) $tab['amount_tax_incl']); Db::getInstance()->insert('order_slip_detail', $insert_order_slip); } }
public static function create(Order $order, $product_list, $shipping_cost = false, $amount = 0, $amount_choosen = false, $add_tax = true) { $currency = new Currency((int) $order->id_currency); $order_slip = new OrderSlip(); $order_slip->id_customer = (int) $order->id_customer; $order_slip->id_order = (int) $order->id; $order_slip->conversion_rate = $currency->conversion_rate; if ($add_tax) { $add_or_remove = 'add'; $inc_or_ex_1 = 'excl'; $inc_or_ex_2 = 'incl'; } else { $add_or_remove = 'remove'; $inc_or_ex_1 = 'incl'; $inc_or_ex_2 = 'excl'; } $order_slip->{'total_shipping_tax_' . $inc_or_ex_1} = 0; $order_slip->{'total_shipping_tax_' . $inc_or_ex_2} = 0; $order_slip->partial = 0; if ($shipping_cost !== false) { $order_slip->shipping_cost = true; $carrier = new Carrier((int) $order->id_carrier); $address = Address::initialize($order->id_address_delivery, false); $tax_calculator = $carrier->getTaxCalculator($address); $order_slip->{'total_shipping_tax_' . $inc_or_ex_1} = $shipping_cost === null ? $order->{'total_shipping_tax_' . $inc_or_ex_1} : (double) $shipping_cost; if ($tax_calculator instanceof TaxCalculator) { $order_slip->{'total_shipping_tax_' . $inc_or_ex_2} = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($order_slip->{'total_shipping_tax_' . $inc_or_ex_1}), _PS_PRICE_COMPUTE_PRECISION_); } else { $order_slip->{'total_shipping_tax_' . $inc_or_ex_2} = $order_slip->{'total_shipping_tax_' . $inc_or_ex_1}; } } else { $order_slip->shipping_cost = false; } $order_slip->amount = 0; $order_slip->{'total_products_tax_' . $inc_or_ex_1} = 0; $order_slip->{'total_products_tax_' . $inc_or_ex_2} = 0; foreach ($product_list as &$product) { $order_detail = new OrderDetail((int) $product['id_order_detail']); $price = (double) $product['unit_price']; $quantity = (int) $product['quantity']; $order_slip_resume = OrderSlip::getProductSlipResume((int) $order_detail->id); if ($quantity + $order_slip_resume['product_quantity'] > $order_detail->product_quantity) { $quantity = $order_detail->product_quantity - $order_slip_resume['product_quantity']; } if ($quantity == 0) { continue; } $order_detail->product_quantity_refunded += $quantity; $order_detail->save(); $address = Address::initialize($order->id_address_invoice, false); $id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int) $order_detail->product_id); $tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator(); $order_slip->{'total_products_tax_' . $inc_or_ex_1} += $price * $quantity; if (in_array(Configuration::get('PS_ROUND_TYPE'), array(Order::ROUND_ITEM, Order::ROUND_LINE))) { if (!isset($total_products[$id_tax_rules_group])) { $total_products[$id_tax_rules_group] = 0; } else { if (!isset($total_products[$id_tax_rules_group . '_' . $id_address])) { $total_products[$id_tax_rules_group . '_' . $id_address] = 0; } } } $product_tax_incl_line = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price) * $quantity, _PS_PRICE_COMPUTE_PRECISION_); switch (Configuration::get('PS_ROUND_TYPE')) { case Order::ROUND_ITEM: $product_tax_incl = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price), _PS_PRICE_COMPUTE_PRECISION_) * $quantity; $total_products[$id_tax_rules_group] += $product_tax_incl; break; case Order::ROUND_LINE: $product_tax_incl = $product_tax_incl_line; $total_products[$id_tax_rules_group] += $product_tax_incl; break; case Order::ROUND_TOTAL: $product_tax_incl = $product_tax_incl_line; $total_products[$id_tax_rules_group . '_' . $id_address] += $price * $quantity; break; } $product['unit_price_tax_' . $inc_or_ex_1] = $price; $product['unit_price_tax_' . $inc_or_ex_2] = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price), _PS_PRICE_COMPUTE_PRECISION_); $product['total_price_tax_' . $inc_or_ex_1] = Tools::ps_round($price * $quantity, _PS_PRICE_COMPUTE_PRECISION_); $product['total_price_tax_' . $inc_or_ex_2] = Tools::ps_round($product_tax_incl, _PS_PRICE_COMPUTE_PRECISION_); $product['product_id'] = $order_detail->product_id; } unset($product); foreach ($total_products as $key => $price) { if (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_slip->{'total_products_tax_' . $inc_or_ex_2} += Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price), _PS_PRICE_COMPUTE_PRECISION_); } else { $order_slip->{'total_products_tax_' . $inc_or_ex_2} += $price; } } $order_slip->{'total_products_tax_' . $inc_or_ex_2} -= (double) $amount && !$amount_choosen ? (double) $amount : 0; $order_slip->amount = $amount_choosen ? (double) $amount : $order_slip->{'total_products_tax_' . $inc_or_ex_1}; $order_slip->shipping_cost_amount = $order_slip->{'total_shipping_tax_' . $inc_or_ex_1}; if ((double) $amount && !$amount_choosen) { $order_slip->order_slip_type = 1; } if ((double) $amount && $amount_choosen || $order_slip->shipping_cost_amount > 0) { $order_slip->order_slip_type = 2; } if (!$order_slip->add()) { return false; } $res = true; $param = array(); $param['MerchantOrderID'] = $order->id; $prefix = _DB_PREFIX_; $i = 0; foreach ($product_list as $product) { $res &= $order_slip->addProductOrderSlip($product); $order_item_detail = new OrderDetail((int) $product['id_order_detail']); $sql = 'UPDATE `' . $prefix . 'stock_available` set `quantity` = `quantity` + ' . $product['quantity'] . ' where `id_product` = ' . $product['product_id'] . ' and `id_product_attribute` = 0 '; //Db::getInstance()->Execute($sql); if ($order_item_detail->product_attribute_id > 0) { $sql = 'UPDATE `' . $prefix . 'stock_available` set `quantity` = `quantity` + ' . $product['quantity'] . ' where `id_product` = ' . $product['product_id'] . ' and `id_product_attribute` = ' . $order_item_detail->product_attribute_id . ' '; //Db::getInstance()->Execute($sql); } $date = date('Y-m-d'); $sql = 'UPDATE `' . $prefix . 'product_sale` set `quantity` = `quantity` - ' . $product['quantity'] . ', `sale_nbr` = `sale_nbr` - ' . $product['quantity'] . ', `date_upd` = ' . $date . ' where `id_product` = ' . $product['product_id'] . ' '; //Db::getInstance()->Execute($sql); $param['items'][$i]['MerchantOrderItemID'] = $product['product_id']; $param['items'][$i]['Principal'] = $product['total_price_tax_incl']; $param['items'][$i]['Shipping'] = 0; $param['items'][$i]['Tax'] = 0; $param['items'][$i]['ShippingTax'] = 0; $param['items'][$i]['quantity'] = $product['quantity']; $i++; } $refund = new Pwapresta(); $refund->pwa_refund_feed($param); return $res; }