// Fix duplicate invoice delivery number
This commit is contained in:
+51
-14
@@ -1109,6 +1109,18 @@ class OrderCore extends ObjectModel
|
||||
return Db::getInstance()->execute($sql);
|
||||
}
|
||||
|
||||
public function getInvoiceNumber($order_invoice_id)
|
||||
{
|
||||
if (!$order_invoice_id)
|
||||
return false;
|
||||
|
||||
return Db::getInstance()->getValue('
|
||||
SELECT `number`
|
||||
FROM `'._DB_PREFIX_.'order_invoice`
|
||||
WHERE `id_order_invoice` = '.(int)$order_invoice_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method allows to generate first invoice of the current order
|
||||
*/
|
||||
@@ -1193,11 +1205,46 @@ class OrderCore extends ObjectModel
|
||||
|
||||
// Keep it for backward compatibility, to remove on 1.6 version
|
||||
$this->invoice_date = $order_invoice->date_add;
|
||||
$this->invoice_number = $order_invoice->number;
|
||||
$this->invoice_number = $this->getInvoiceNumber($order_invoice->id);
|
||||
$this->update();
|
||||
}
|
||||
}
|
||||
|
||||
public function setDeliveryNumber($order_invoice_id, $id_shop)
|
||||
{
|
||||
if (!$order_invoice_id)
|
||||
return false;
|
||||
|
||||
$number = Configuration::get('PS_DELIVERY_NUMBER', null, null, $id_shop);
|
||||
// If invoice start number has been set, you clean the value of this configuration
|
||||
if ($number)
|
||||
Configuration::updateValue('PS_DELIVERY_NUMBER', false, false, null, $id_shop);
|
||||
|
||||
$sql = 'UPDATE `'._DB_PREFIX_.'order_invoice` SET delivery_number =';
|
||||
|
||||
if ($number)
|
||||
$sql .= (int)$number;
|
||||
else
|
||||
$sql .= '(SELECT new_number FROM (SELECT (MAX(`delivery_number`) + 1) AS new_number
|
||||
FROM `'._DB_PREFIX_.'order_invoice`) AS result)';
|
||||
|
||||
$sql .=' WHERE `id_order_invoice` = '.(int)$order_invoice_id;
|
||||
|
||||
return Db::getInstance()->execute($sql);
|
||||
}
|
||||
|
||||
public function getDeliveryNumber($order_invoice_id)
|
||||
{
|
||||
if (!$order_invoice_id)
|
||||
return false;
|
||||
|
||||
return Db::getInstance()->getValue('
|
||||
SELECT `delivery_number`
|
||||
FROM `'._DB_PREFIX_.'order_invoice`
|
||||
WHERE `id_order_invoice` = '.(int)$order_invoice_id
|
||||
);
|
||||
}
|
||||
|
||||
public function setDelivery()
|
||||
{
|
||||
// Get all invoice
|
||||
@@ -1206,24 +1253,14 @@ class OrderCore extends ObjectModel
|
||||
{
|
||||
if ($order_invoice->delivery_number)
|
||||
continue;
|
||||
|
||||
$number = (int)Configuration::get('PS_DELIVERY_NUMBER', null, null, $this->id_shop);
|
||||
if (!$number)
|
||||
{
|
||||
//if delivery number is not set or wrong, we set a default one.
|
||||
Configuration::updateValue('PS_DELIVERY_NUMBER', 1, false, null, $this->id_shop);
|
||||
$number = 1;
|
||||
}
|
||||
|
||||
// Set delivery number on invoice
|
||||
$order_invoice->delivery_number = $number;
|
||||
$order_invoice->delivery_number = 0;
|
||||
$order_invoice->delivery_date = date('Y-m-d H:i:s');
|
||||
// Update Order Invoice
|
||||
$order_invoice->update();
|
||||
|
||||
// Keep for backward compatibility
|
||||
$this->delivery_number = $number;
|
||||
Configuration::updateValue('PS_DELIVERY_NUMBER', $number + 1, false, null, $this->id_shop);
|
||||
$this->setDeliveryNumber($order_invoice->id, $this->id_shop);
|
||||
$this->delivery_number = $this->getDeliveryNumber($order_invoice->id);
|
||||
}
|
||||
|
||||
// Keep it for backward compatibility, to remove on 1.6 version
|
||||
|
||||
Reference in New Issue
Block a user