// fixed migrate_orders to keep potential table customization
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@13795 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
@@ -34,28 +34,21 @@ function migrate_orders()
|
|||||||
if (!defined('PS_TAX_INC'))
|
if (!defined('PS_TAX_INC'))
|
||||||
define('PS_TAX_INC', 0);
|
define('PS_TAX_INC', 0);
|
||||||
|
|
||||||
$values_order_detail = array();
|
|
||||||
// init insert order detail query
|
// init insert order detail query
|
||||||
$insert_order_detail = 'INSERT INTO `'._DB_PREFIX_.'order_detail_2`
|
$values_order_detail = array();
|
||||||
(`id_order_detail`, `id_order`, `id_order_invoice`, `id_warehouse`, `product_id`, `product_attribute_id`,
|
$col_order_detail = Db::getInstance()->query('SHOW FIELDS FROM `'._DB_PREFIX_.'order_detail`');
|
||||||
`product_name`, `product_quantity`, `product_quantity_in_stock`, `product_quantity_refunded`, `product_quantity_return`,
|
if (!$col_order_detail)
|
||||||
`product_quantity_reinjected`, `product_price`, `reduction_percent`, `reduction_amount`, `reduction_amount_tax_incl`,
|
return array('error' => true, 'msg' => 'unable to get fields list from order_detail table');
|
||||||
`reduction_amount_tax_excl`, `group_reduction`, `product_quantity_discount`, `product_ean13`, `product_upc`, `product_reference`,
|
$col_order_detail = $col_order_detail->fetchAll(PDO::FETCH_COLUMN);
|
||||||
`product_supplier_reference`, `product_weight`, `tax_computation_method`, `tax_name`, `tax_rate`, `ecotax`,
|
$insert_order_detail = 'INSERT INTO `'._DB_PREFIX_.'order_detail_2` (`'.implode('`, `', $col_order_detail).'`) VALUES ';
|
||||||
`ecotax_tax_rate`, `discount_quantity_applied`, `download_hash`, `download_nb`, `download_deadline`,
|
|
||||||
`total_price_tax_incl`, `total_price_tax_excl`, `unit_price_tax_incl`, `unit_price_tax_excl`,
|
|
||||||
`total_shipping_price_tax_incl`, `total_shipping_price_tax_excl`, `purchase_supplier_price`,
|
|
||||||
`original_product_price`) VALUES ';
|
|
||||||
|
|
||||||
$values_order = array();
|
|
||||||
// init insert order query
|
// init insert order query
|
||||||
$insert_order = 'INSERT INTO `'._DB_PREFIX_.'orders_2` (`id_order`, `reference`, `id_group_shop`, `id_shop`, `id_carrier`,
|
$values_order = array();
|
||||||
`id_lang`, `id_customer`, `id_cart`, `id_currency`, `id_address_delivery`, `id_address_invoice`, `secure_key`, `payment`,
|
$col_orders = Db::getInstance()->query('SHOW FIELDS FROM `'._DB_PREFIX_.'orders`');
|
||||||
`conversion_rate`, `module`, `recyclable`, `gift`, `gift_message`, `shipping_number`, `total_discounts`,
|
if (!$col_orders)
|
||||||
`total_discounts_tax_incl`, `total_discounts_tax_excl`, `total_paid`, `total_paid_tax_incl`, `total_paid_tax_excl`,
|
return array('error' => true, 'msg' => 'unable to get fields list from orders table');
|
||||||
`total_paid_real`, `total_products`, `total_products_wt`, `total_shipping`, `total_shipping_tax_incl`, `total_shipping_tax_excl`,
|
$col_orders = $col_orders->fetchAll(PDO::FETCH_COLUMN);
|
||||||
`carrier_tax_rate`, `total_wrapping`, `total_wrapping_tax_incl`, `total_wrapping_tax_excl`, `invoice_number`, `delivery_number`,
|
$insert_order = 'INSERT INTO `'._DB_PREFIX_.'orders_2` (`'.implode('`, `', $col_orders).'`) VALUES ';
|
||||||
`invoice_date`, `delivery_date`, `valid`, `date_add`, `date_upd`) VALUES ';
|
|
||||||
|
|
||||||
// create temporary tables
|
// create temporary tables
|
||||||
$res = mo_duplicateTables();
|
$res = mo_duplicateTables();
|
||||||
@@ -66,53 +59,56 @@ function migrate_orders()
|
|||||||
'SELECT *
|
'SELECT *
|
||||||
FROM `'._DB_PREFIX_.'orders`');
|
FROM `'._DB_PREFIX_.'orders`');
|
||||||
|
|
||||||
|
// this was done like that previously
|
||||||
|
$wrapping_tax_rate = 1 + ((float)Db::getInstance()->getValue('SELECT value
|
||||||
|
FROM `'._DB_PREFIX_.'configuration`
|
||||||
|
WHERE name = "PS_GIFT_WRAPPING_TAX"') / 100);
|
||||||
|
|
||||||
$cpt = 0;
|
$cpt = 0;
|
||||||
$flush_limit = 1000;
|
$flush_limit = 1000;
|
||||||
while ($order = Db::getInstance()->nextRow($order_res))
|
while ($order = Db::getInstance()->nextRow($order_res))
|
||||||
{
|
{
|
||||||
$sum_total_products = 0;
|
$sum_total_products = 0;
|
||||||
$sum_tax_amount = 0;
|
$sum_tax_amount = 0;
|
||||||
$default_group_id = mo_getCustomerDefaultGroup((int)$order['id_customer']);
|
$default_group_id = mo_getCustomerDefaultGroup((int)$order['id_customer']);
|
||||||
$price_display_method = mo_getPriceDisplayMethod((int)$default_group_id);
|
$price_display_method = mo_getPriceDisplayMethod((int)$default_group_id);
|
||||||
|
$order_details_list = Db::getInstance()->executeS('
|
||||||
|
SELECT *
|
||||||
|
FROM `'._DB_PREFIX_.'order_detail` od
|
||||||
|
LEFT JOIN `'._DB_PREFIX_.'product` p
|
||||||
|
ON p.id_product = od.product_id
|
||||||
|
WHERE od.`id_order` = '.(int)($order['id_order']));
|
||||||
|
|
||||||
$order_details_list = Db::getInstance()->executeS('
|
foreach ($order_details_list as $order_details)
|
||||||
SELECT *
|
{
|
||||||
FROM `'._DB_PREFIX_.'order_detail` od
|
// we don't want to erase order_details data in order to create the insert query
|
||||||
LEFT JOIN `'._DB_PREFIX_.'product` p
|
$products = mo_setProductPrices($order_details, $price_display_method);
|
||||||
ON p.id_product = od.product_id
|
$tax_rate = 1 + ((float)$products['tax_rate'] / 100);
|
||||||
WHERE od.`id_order` = '.(int)($order['id_order']));
|
$reduction_amount_tax_incl = (float)$products['reduction_amount'];
|
||||||
|
|
||||||
foreach ($order_details_list as $order_details)
|
// cart::getTaxesAverageUsed equivalent
|
||||||
{
|
$sum_total_products += $products['total_wt'];
|
||||||
// we don't want to erase order_details data in order to create the insert query
|
$sum_tax_amount += $products['total_wt'] - $products['total_price'];
|
||||||
$products = mo_setProductPrices($order_details, $price_display_method);
|
|
||||||
$tax_rate = 1 + ((float)$products['tax_rate'] / 100);
|
|
||||||
$reduction_amount_tax_incl = (float)$products['reduction_amount'];
|
|
||||||
|
|
||||||
// cart::getTaxesAverageUsed equivalent
|
$order_details['reduction_amount_tax_incl']= $reduction_amount_tax_incl;
|
||||||
$sum_total_products += $products['total_wt'];
|
$order_details['reduction_amount_tax_excl']= (float)mo_ps_round($reduction_amount_tax_incl / $tax_rate);
|
||||||
$sum_tax_amount += $products['total_wt'] - $products['total_price'];
|
$order_details['total_price_tax_incl']= (float)$products['total_wt'];
|
||||||
|
$order_details['total_price_tax_excl']= (float)$products['total_price'];
|
||||||
|
$order_details['unit_price_tax_incl']= (float)$products['product_price_wt'];
|
||||||
|
$order_details['unit_price_tax_excl']= (float)$products['product_price'];
|
||||||
|
foreach (array_keys($order_details) as $k)
|
||||||
|
if (!in_array($k, $col_order_detail))
|
||||||
|
unset($order_details[$k]);
|
||||||
|
$values_order_detail[] = '(\''.implode('\', \'', $order_details).'\')';
|
||||||
|
}
|
||||||
|
|
||||||
$order_details['reduction_amount_tax_incl']= $reduction_amount_tax_incl;
|
$average_tax_used = 1;
|
||||||
$order_details['reduction_amount_tax_excl']= (float)mo_ps_round($reduction_amount_tax_incl / $tax_rate);
|
if ($sum_total_products > 0)
|
||||||
$order_details['total_price_tax_incl']= (float)$products['total_wt'];
|
$average_tax_used += ($sum_tax_amount / $sum_total_products) * 0.01;
|
||||||
$order_details['total_price_tax_excl']= (float)$products['total_price'];
|
|
||||||
$order_details['unit_price_tax_incl']= (float)$products['product_price_wt'];
|
|
||||||
$order_details['unit_price_tax_excl']= (float)$products['product_price'];
|
|
||||||
$values_order_detail[] = '(\''.$order_details['id_order_detail'].'\', \''.$order_details['id_order'].'\', \''.$order_details['id_order_invoice'].'\', \''.$order_details['id_warehouse'].'\', \''.$order_details['product_id'].'\', \''.$order_details['product_attribute_id'].'\', \''.$order_details['product_name'].'\', \''.$order_details['product_quantity'].'\', \''.$order_details['product_quantity_in_stock'].'\', \''.$order_details['product_quantity_refunded'].'\', \''.$order_details['product_quantity_return'].'\', \''.$order_details['product_quantity_reinjected'].'\', \''.$order_details['product_price'].'\', \''.$order_details['reduction_percent'].'\', \''.$order_details['reduction_amount'].'\', \''.$order_details['reduction_amount_tax_incl'].'\', \''.$order_details['reduction_amount_tax_excl'].'\', \''.$order_details['group_reduction'].'\', \''.$order_details['product_quantity_discount'].'\', \''.$order_details['product_ean13'].'\', \''.$order_details['product_upc'].'\', \''.$order_details['product_reference'].'\', \''.$order_details['product_supplier_reference'].'\', \''.$order_details['product_weight'].'\', \''.$order_details['tax_computation_method'].'\', \''.$order_details['tax_name'].'\', \''.$order_details['tax_rate'].'\', \''.$order_details['ecotax'].'\', \''.$order_details['ecotax_tax_rate'].'\', \''.$order_details['discount_quantity_applied'].'\', \''.$order_details['download_hash'].'\', \''.$order_details['download_nb'].'\', \''.$order_details['download_deadline'].'\', \''.$order_details['total_price_tax_incl'].'\', \''.$order_details['total_price_tax_excl'].'\', \''.$order_details['unit_price_tax_incl'].'\', \''.$order_details['unit_price_tax_excl'].'\', \''.$order_details['total_shipping_price_tax_incl'].'\', \''.$order_details['total_shipping_price_tax_excl'].'\', \''.$order_details['purchase_supplier_price'].'\', \''.$order_details['original_product_price'].'\')';
|
|
||||||
}
|
|
||||||
|
|
||||||
$average_tax_used = 1;
|
$carrier_tax_rate = 1 + ((float)$order['carrier_tax_rate'] / 100);
|
||||||
if ($sum_total_products > 0)
|
|
||||||
$average_tax_used += ($sum_tax_amount / $sum_total_products) * 0.01;
|
|
||||||
|
|
||||||
// this was done like that previously
|
$total_discount_tax_excl = $order['total_discounts'] / $average_tax_used;
|
||||||
$wrapping_tax_rate = 1 + ((float)Db::getInstance()->getValue('SELECT value
|
|
||||||
FROM `'._DB_PREFIX_.'configuration`
|
|
||||||
WHERE name = "PS_GIFT_WRAPPING_TAX"') / 100);
|
|
||||||
$carrier_tax_rate = 1 + ((float)$order['carrier_tax_rate'] / 100);
|
|
||||||
|
|
||||||
$total_discount_tax_excl = $order['total_discounts'] / $average_tax_used;
|
|
||||||
|
|
||||||
$order['total_discounts_tax_incl'] = (float)$order['total_discounts'];
|
$order['total_discounts_tax_incl'] = (float)$order['total_discounts'];
|
||||||
$order['total_discounts_tax_excl'] = (float)$total_discount_tax_excl;
|
$order['total_discounts_tax_excl'] = (float)$total_discount_tax_excl;
|
||||||
@@ -122,7 +118,7 @@ function migrate_orders()
|
|||||||
$order['total_shipping_tax_excl'] = (float)($order['total_shipping'] / $carrier_tax_rate);
|
$order['total_shipping_tax_excl'] = (float)($order['total_shipping'] / $carrier_tax_rate);
|
||||||
$order['total_wrapping_tax_incl'] = (float)$order['total_wrapping'];
|
$order['total_wrapping_tax_incl'] = (float)$order['total_wrapping'];
|
||||||
$order['total_wrapping_tax_excl'] = ((float)$order['total_wrapping'] / $wrapping_tax_rate);
|
$order['total_wrapping_tax_excl'] = ((float)$order['total_wrapping'] / $wrapping_tax_rate);
|
||||||
$values_order[] = '(\''.$order['id_order'].'\', \''.$order['reference'].'\', \''.$order['id_group_shop'].'\', \''.$order['id_shop'].'\', \''.$order['id_carrier'].'\', \''.$order['id_lang'].'\', \''.$order['id_customer'].'\', \''.$order['id_cart'].'\', \''.$order['id_currency'].'\', \''.$order['id_address_delivery'].'\', \''.$order['id_address_invoice'].'\', \''.$order['secure_key'].'\', \''.$order['payment'].'\', \''.$order['conversion_rate'].'\', \''.$order['module'].'\', \''.$order['recyclable'].'\', \''.$order['gift'].'\', \''.$order['gift_message'].'\', \''.$order['shipping_number'].'\', \''.$order['total_discounts'].'\', \''.$order['total_discounts_tax_incl'].'\', \''.$order['total_discounts_tax_excl'].'\', \''.$order['total_paid'].'\', \''.$order['total_paid_tax_incl'].'\', \''.$order['total_paid_tax_excl'].'\', \''.$order['total_paid_real'].'\', \''.$order['total_products'].'\', \''.$order['total_products_wt'].'\', \''.$order['total_shipping'].'\', \''.$order['total_shipping_tax_incl'].'\', \''.$order['total_shipping_tax_excl'].'\', \''.$order['carrier_tax_rate'].'\', \''.$order['total_wrapping'].'\', \''.$order['total_wrapping_tax_incl'].'\', \''.$order['total_wrapping_tax_excl'].'\', \''.$order['invoice_number'].'\', \''.$order['delivery_number'].'\', \''.$order['invoice_date'].'\', \''.$order['delivery_date'].'\', \''.$order['valid'].'\', \''.$order['date_add'].'\', \''.$order['date_upd'].'\')';
|
$values_order[] = '(\''.implode('\', \'', $order).'\')';
|
||||||
|
|
||||||
unset($order);
|
unset($order);
|
||||||
$cpt++;
|
$cpt++;
|
||||||
@@ -131,20 +127,25 @@ function migrate_orders()
|
|||||||
{
|
{
|
||||||
$cpt = 0;
|
$cpt = 0;
|
||||||
$res &= Db::getInstance()->execute($insert_order_detail. implode(',', $values_order_detail));
|
$res &= Db::getInstance()->execute($insert_order_detail. implode(',', $values_order_detail));
|
||||||
|
if (!$res)
|
||||||
|
return array('error' => true, 'msg' => Db::getInstance()->getMsgError());
|
||||||
$res &= Db::getInstance()->execute($insert_order. implode(',', $values_order));
|
$res &= Db::getInstance()->execute($insert_order. implode(',', $values_order));
|
||||||
if (!$res)
|
if (!$res)
|
||||||
return array('error' => true, 'msg' => 'error on insertion in temporary table order_detail / orders ');
|
return array('error' => true, 'msg' => Db::getInstance()->getMsgError());
|
||||||
$values_order = array();
|
$values_order = array();
|
||||||
$values_order_detail = array();
|
$values_order_detail = array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($cpt> 0)
|
if ($cpt > 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
$res &= Db::getInstance()->execute($insert_order_detail. implode(',', $values_order_detail));
|
$res &= Db::getInstance()->execute($insert_order_detail. implode(',', $values_order_detail));
|
||||||
|
if (!$res)
|
||||||
|
return array('error' => true, 'msg' => Db::getInstance()->getMsgError());
|
||||||
$res &= Db::getInstance()->execute($insert_order. implode(',', $values_order));
|
$res &= Db::getInstance()->execute($insert_order. implode(',', $values_order));
|
||||||
if (!$res)
|
if (!$res)
|
||||||
return array('error' => true, 'msg' => 'error on last insertion in temporary table order_detail / orders ');
|
return array('error' => true, 'msg' => Db::getInstance()->getMsgError());
|
||||||
}
|
}
|
||||||
|
|
||||||
$res &= mo_renameTables();
|
$res &= mo_renameTables();
|
||||||
|
|||||||
Reference in New Issue
Block a user