diff --git a/classes/SpecificPrice.php b/classes/SpecificPrice.php index 6c7696d74..1f6d2d845 100644 --- a/classes/SpecificPrice.php +++ b/classes/SpecificPrice.php @@ -135,7 +135,7 @@ class SpecificPriceCore extends ObjectModel $priority = SpecificPrice::getPriority($id_product); foreach (array_reverse($priority) as $k => $field) - $select .= ' IF (`'.$field.'` = '.(int)(${$field}).', '.pow(2, $k + 1).', 0) + '; + $select .= ' IF (`'.bqSQL($field).'` = '.(int)(${$field}).', '.pow(2, $k + 1).', 0) + '; return rtrim($select, ' +').') AS `score`'; } diff --git a/install-dev/php/migrate_orders.php b/install-dev/php/migrate_orders.php new file mode 100644 index 000000000..060a546ce --- /dev/null +++ b/install-dev/php/migrate_orders.php @@ -0,0 +1,176 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + + +include('config/config.inc.php'); + +if (!defined('PS_TAX_EXC')) + define('PS_TAX_EXC', 1); + +if (!defined('PS_TAX_INC')) + define('PS_TAX_INC', 0); + +$sql = 'SELECT * + FROM `'._DB_PREFIX_.'orders`'; + +$orders = Db::getInstance()->executeS($sql); + +foreach ($orders as $order) +{ + $sum_total_products = 0; + $sum_tax_amount = 0; + $default_group_id = getCustomerDefaultGroup((int)$order['id_customer']); + $price_display_method = 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'])); + + foreach ($order_details_list as $order_details) + { + // we don't want to erase order_details data in order to create the insert query + $products = 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 + $sum_total_products += $products['total_wt']; + $sum_tax_amount += $products['total_wt'] - $products['total_price']; + + $sql = 'UPDATE `'._DB_PREFIX_.'order_detail` + SET `reduction_amount_tax_incl` = '.$reduction_amount_tax_incl.', + `reduction_amount_tax_excl` = '.(float)Tools::ps_round($reduction_amount_tax_incl / $tax_rate, 2).', + `total_price_tax_incl` = '.(float)$products['total_wt'].', + `total_price_tax_excl` = '.(float)$products['total_price'].', + `unit_price_tax_incl` = '.(float)$products['product_price_wt'].', + `unit_price_tax_excl` = '.(float)$products['product_price'].' + WHERE `id_order_detail` = '.(int)$products['id_order_detail']; + + Db::getInstance()->execute($sql); + } + + $average_tax_used = 1; + if ($sum_total_products > 0) + $average_tax_used += ($sum_tax_amount / $sum_total_products) * 0.01; + + // this was done like that previously + $wrapping_tax_rate = 1 + ((float)Configuration::get('PS_GIFT_WRAPPING_TAX') / 100); + $carrier_tax_rate = 1 + ((float)$order['carrier_tax_rate'] / 100); + + $total_discount_tax_excl = $order['total_discount'] / $average_tax_used; + + $sql = 'UPDATE `'._DB_PREFIX_.'orders` + SET `total_discount_tax_incl` = '.(float)$order['total_discount'].', + `total_discount_tax_excl` = '.(float)$total_discount_tax_excl.', + `total_paid_tax_incl` = '.(float)$order['total_paid'].', + `total_paid_tax_excl` = '.(float)$order['total_paid'].', + `total_shipping_tax_incl` = '.(float)$order['total_shipping'].', + `total_shipping_tax_excl` = '.(float)($order['total_shipping'] / $carrier_tax_rate).', + `total_wrapping_tax_incl` = '.(float)$order['total_wrapping'].', + `total_wrapping_tax_excl` = '.((float)$order['total_wrapping'] / $wrapping_tax_rate).' + WHERE `id_order` = '.(int)$order['id_order']; + + Db::getInstance()->execute($sql); + + unset($order); +} + + + +function getCustomerDefaultGroup($id_customer) +{ + Db::getInstance()->getValue('SELECT `id_default_group` FROM `'._DB_PREFIX_.'customer` WHERE `id_customer` = '.(int)$id_customer); +} + +function getPriceDisplayMethod($id_group) +{ + return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT `price_display_method` + FROM `'._DB_PREFIX_.'group` + WHERE `id_group` = '.(int)$id_group); +} + +function setProductPrices($row, $tax_calculation_method) +{ + if ($tax_calculation_method == PS_TAX_EXC) + $row['product_price'] = Tools::ps_round($row['product_price'], 2); + else + $row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + $row['tax_rate'] / 100), 2); + + $group_reduction = 1; + if ($row['group_reduction'] > 0) + $group_reduction = 1 - $row['group_reduction'] / 100; + + if ($row['reduction_percent'] != 0) + { + if ($tax_calculation_method == PS_TAX_EXC) + $row['product_price'] = ($row['product_price'] - $row['product_price'] * ($row['reduction_percent'] * 0.01)); + else + { + $reduction = Tools::ps_round($row['product_price_wt'] * ($row['reduction_percent'] * 0.01), 2); + $row['product_price_wt'] = Tools::ps_round(($row['product_price_wt'] - $reduction), 2); + } + } + + if ($row['reduction_amount'] != 0) + { + if ($tax_calculation_method == PS_TAX_EXC) + $row['product_price'] = ($row['product_price'] - ($row['reduction_amount'] / (1 + $row['tax_rate'] / 100))); + else + $row['product_price_wt'] = Tools::ps_round(($row['product_price_wt'] - $row['reduction_amount']), 2); + } + + if ($row['group_reduction'] > 0) + { + if ($tax_calculation_method == PS_TAX_EXC) + $row['product_price'] = $row['product_price'] * $group_reduction; + else + $row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] * $group_reduction , 2); + } + + if (($row['reduction_percent'] OR $row['reduction_amount'] OR $row['group_reduction']) AND $tax_calculation_method == PS_TAX_EXC) + $row['product_price'] = Tools::ps_round($row['product_price'], 2); + + if ($tax_calculation_method == PS_TAX_EXC) + $row['product_price_wt'] = Tools::ps_round($row['product_price'] * (1 + ($row['tax_rate'] * 0.01)), 2) + Tools::ps_round($row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2); + else + { + $row['product_price_wt_but_ecotax'] = $row['product_price_wt']; + $row['product_price_wt'] = Tools::ps_round($row['product_price_wt'] + $row['ecotax'] * (1 + $row['ecotax_tax_rate'] / 100), 2); + } + + $row['total_wt'] = $row['product_quantity'] * $row['product_price_wt']; + $row['total_price'] = $row['product_quantity'] * $row['product_price']; + + return $row; +} + + diff --git a/install-dev/sql/upgrade/1.5.0.2.sql b/install-dev/sql/upgrade/1.5.0.2.sql index 39fbad5b3..e91f39599 100644 --- a/install-dev/sql/upgrade/1.5.0.2.sql +++ b/install-dev/sql/upgrade/1.5.0.2.sql @@ -278,7 +278,10 @@ CREATE TABLE IF NOT EXISTS `PREFIX_stock_mvt` ( ALTER TABLE `PREFIX_orders` DROP COLUMN `id_warehouse`; ALTER TABLE `PREFIX_supplier` ADD COLUMN `id_address` int(10) unsigned NOT NULL default '0' AFTER `id_supplier`; ALTER TABLE `PREFIX_address` ADD COLUMN `id_warehouse` int(10) unsigned NOT NULL DEFAULT 0 AFTER `id_supplier`; -ALTER TABLE `PREFIX_order_detail` ADD COLUMN `id_warehouse` int(10) unsigned NOT NULL default '0' AFTER `id_order_invoice`; +ALTER TABLE `PREFIX_order_detail` +ADD COLUMN `id_warehouse` int(10) unsigned NOT NULL default '0' AFTER `id_order_invoice`; + + ALTER TABLE `PREFIX_stock_mvt_reason` ADD COLUMN `deleted` tinyint(1) unsigned NOT NULL default '0' AFTER `date_upd`; ALTER TABLE `PREFIX_product` ADD COLUMN `advanced_stock_management` tinyint(1) default '0' NOT NULL; @@ -290,8 +293,27 @@ DELETE FROM `PREFIX_configuration` WHERE `name` = 'PS_PDF_FONT'; ALTER TABLE `PREFIX_order_detail` -ADD `reduction_amount_tax_incl` FLOAT( 20.6 ) NOT NULL AFTER `reduction_amount` , -ADD `reduction_amount_tax_excl` FLOAT( 20.6 ) NOT NULL AFTER `reduction_amount_tax_incl`; +ADD `reduction_amount_tax_incl` FLOAT( 20.6 ) NOT NULL AFTER `reduction_amount` , +ADD `reduction_amount_tax_excl` FLOAT( 20.6 ) NOT NULL AFTER `reduction_amount_tax_incl`, +ADD `total_price_tax_incl` DECIMAL(20, 6) NOT NULL AFTER `download_deadline`,, +ADD `total_price_tax_excl` DECIMAL(20, 6) NOT NULL AFTER `total_price_tax_incl`, +ADD `unit_price_tax_incl` DECIMAL(20, 6) NOT NULL AFTER `total_price_tax_excl`, +ADD `unit_price_tax_excl` DECIMAL(20, 6) NOT NULL AFTER `unit_price_tax_incl`, +ADD `total_shipping_price_tax_incl` DECIMAL(20, 6) NOT NULL AFTER `unit_price_tax_excl`, +ADD `total_shipping_price_tax_excl` DECIMAL(20, 6) NOT NULL AFTER `total_shipping_price_tax_incl`, +ADD `purchase_supplier_price` DECIMAL(20, 6) NOT NULL AFTER `total_shipping_price_tax_excl`, +ADD `original_product_price` DECIMAL(20, 6) NOT NULL AFTER `purchase_supplier_price`; + + +ALTER TABLE `PREFIX_orders` +ADD `total_discount_tax_excl` decimal(17,2) NOT NULL AFTER `total_discounts`, +ADD `total_discount_tax_incl` decimal(17,2) NOT NULL AFTER `total_discount_tax_excl`, +ADD `total_paid_tax_excl` decimal(17,2) NOT NULL AFTER `total_paid`, +ADD `total_paid_tax_incl` decimal(17,2) NOT NULL AFTER `total_paid_tax_excl`, +ADD `total_shipping_tax_excl` decimal(17,2) NOT NULL AFTER `total_shipping`, +ADD `total_shipping_tax_incl` decimal(17,2) NOT NULL AFTER `total_shipping_tax_excl`, +ADD `total_wrapping_tax_excl` decimal(17,2) NOT NULL AFTER `total_wrapping`, +ADD `total_wrapping_tax_incl` decimal(17,2) NOT NULL AFTER `total_wrapping_tax_excl`; ALTER TABLE `PREFIX_order_cart_rule` ADD `value_tax_excl` DECIMAL(17, 2) NOT NULL DEFAULT '0.00'; ALTER TABLE `PREFIX_order_cart_rule` ADD `id_order_invoice` INT NOT NULL DEFAULT '0' AFTER `id_cart_rule`; @@ -311,6 +333,8 @@ CREATE TABLE `PREFIX_category_shop` ( INSERT INTO `PREFIX_category_shop` (`id_category`, `id_shop`) VALUES (1, 1); +/* PHP:migrate_orders(); */; + CREATE TABLE IF NOT EXISTS `PREFIX_linksmenutop` ( `id_link` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `id_shop` INT UNSIGNED NOT NULL, @@ -328,4 +352,4 @@ CREATE TABLE IF NOT EXISTS `PREFIX_linksmenutop_lang` ( ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; INSERT INTO `PREFIX_configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES -('PS_SMARTY_CONSOLE', '0', NOW(), NOW()); \ No newline at end of file +('PS_SMARTY_CONSOLE', '0', NOW(), NOW()); diff --git a/install-dev/xml/doUpgrade.php b/install-dev/xml/doUpgrade.php index 5ba9e6cc8..a9df4feaa 100644 --- a/install-dev/xml/doUpgrade.php +++ b/install-dev/xml/doUpgrade.php @@ -147,6 +147,8 @@ require_once(_PS_INSTALLER_PHP_UPGRADE_DIR_.'update_order_canada.php'); require_once(_PS_INSTALLER_PHP_UPGRADE_DIR_.'generate_order_reference.php'); +require_once(_PS_INSTALLER_PHP_UPGRADE_DIR_.'migrate_orders.php'); + //old version detection global $oldversion, $logger; $oldversion = false;