Jump to content

Need to update order_invoice when cancelling product from BO


nnab

Recommended Posts

I was checking on cancelling some qty or products for order is created along with invoice number, the qty or product when cancelled updated the order correctly but the invoice is not updated as below code only updates the order_detail. Need help in updating the order_invoice also so the pdf invoice gets updated value. What code need to be added to get the order_invoice updated in adminordercontroller. Below is code for cancelling the products in a order in BO.

 

 

 

/* Cancel product from order */

elseif (Tools::isSubmit('cancelProduct') && isset($order))

{

if ($this->tabAccess['delete'] === '1')

{

if (!Tools::isSubmit('id_order_detail') && !Tools::isSubmit('id_customization'))

$this->errors[] = Tools::displayError('You must select a product.');

elseif (!Tools::isSubmit('cancelQuantity') && !Tools::isSubmit('cancelCustomizationQuantity'))

$this->errors[] = Tools::displayError('You must enter a quantity.');

else

{

$productList = Tools::getValue('id_order_detail');

if ($productList)

$productList = array_map('intval', $productList);

 

$customizationList = Tools::getValue('id_customization');

if ($customizationList)

$customizationList = array_map('intval', $customizationList);

 

$qtyList = Tools::getValue('cancelQuantity');

if ($qtyList)

$qtyList = array_map('intval', $qtyList);

 

$customizationQtyList = Tools::getValue('cancelCustomizationQuantity');

if ($customizationQtyList)

$customizationQtyList = array_map('intval', $customizationQtyList);

 

$full_product_list = $productList;

$full_quantity_list = $qtyList;

 

if ($customizationList)

foreach ($customizationList as $key => $id_order_detail)

{

$full_product_list[(int)$id_order_detail] = $id_order_detail;

if (isset($customizationQtyList[$key]))

$full_quantity_list[(int)$id_order_detail] += $customizationQtyList[$key];

}

 

if ($productList || $customizationList)

{

if ($productList)

{

$id_cart = Cart::getCartIdByOrderId($order->id);

$customization_quantities = Customization::countQuantityByCart($id_cart);

 

foreach ($productList as $key => $id_order_detail)

{

$qtyCancelProduct = abs($qtyList[$key]);

if (!$qtyCancelProduct)

$this->errors[] = Tools::displayError('No quantity has been selected for this product.');

 

$order_detail = new OrderDetail($id_order_detail);

$customization_quantity = 0;

if (array_key_exists($order_detail->product_id, $customization_quantities) && array_key_exists($order_detail->product_attribute_id, $customization_quantities[$order_detail->product_id]))

$customization_quantity = (int)$customization_quantities[$order_detail->product_id][$order_detail->product_attribute_id];

 

if (($order_detail->product_quantity - $customization_quantity - $order_detail->product_quantity_refunded - $order_detail->product_quantity_return) < $qtyCancelProduct)

$this->errors[] = Tools::displayError('An invalid quantity was selected for this product.');

 

}

}

if ($customizationList)

{

$customization_quantities = Customization::retrieveQuantitiesFromIds(array_keys($customizationList));

 

foreach ($customizationList as $id_customization => $id_order_detail)

{

$qtyCancelProduct = abs($customizationQtyList[$id_customization]);

$customization_quantity = $customization_quantities[$id_customization];

 

if (!$qtyCancelProduct)

$this->errors[] = Tools::displayError('No quantity has been selected for this product.');

 

if ($qtyCancelProduct > ($customization_quantity['quantity'] - ($customization_quantity['quantity_refunded'] + $customization_quantity['quantity_returned'])))

$this->errors[] = Tools::displayError('An invalid quantity was selected for this product.');

}

}

 

if (!count($this->errors) && $productList)

foreach ($productList as $key => $id_order_detail)

{

$qty_cancel_product = abs($qtyList[$key]);

$order_detail = new OrderDetail((int)($id_order_detail));

 

if (!$order->hasBeenDelivered() || ($order->hasBeenDelivered() && Tools::isSubmit('reinjectQuantities')) && $qty_cancel_product > 0)

$this->reinjectQuantity($order_detail, $qty_cancel_product);

 

// Delete product

$order_detail = new OrderDetail((int)$id_order_detail);

if (!$order->deleteProduct($order, $order_detail, $qty_cancel_product))

$this->errors[] = Tools::displayError('An error occurred while attempting to delete the product.').' <span class="bold">'.$order_detail->product_name.'</span>';

// Update weight SUM

$order_carrier = new OrderCarrier((int)$order->getIdOrderCarrier());

if (Validate::isLoadedObject($order_carrier))

{

$order_carrier->weight = (float)$order->getTotalWeight();

if ($order_carrier->update())

$order->weight = sprintf("%.3f ".Configuration::get('PS_WEIGHT_UNIT'), $order_carrier->weight);

}

Hook::exec('actionProductCancel', array('order' => $order, 'id_order_detail' => (int)$id_order_detail), null, false, true, false, $order->id_shop);

}

if (!count($this->errors) && $customizationList)

foreach ($customizationList as $id_customization => $id_order_detail)

{

$order_detail = new OrderDetail((int)($id_order_detail));

$qtyCancelProduct = abs($customizationQtyList[$id_customization]);

if (!$order->deleteCustomization($id_customization, $qtyCancelProduct, $order_detail))

$this->errors[] = Tools::displayError('An error occurred while attempting to delete product customization.').' '.$id_customization;

Link to comment
Share on other sites

×
×
  • Create New...