From 3e09c4c2c5cfccc987522c1c8ba28e586a273fbc Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Thu, 5 Oct 2023 18:35:14 +0530 Subject: [PATCH 1/4] Added: Configuration in Preferences->order tab for price display currency in order list page --- classes/order/Order.php | 10 ++++++++- classes/order/OrderPayment.php | 11 ++++++++++ .../admin/AdminOrderPreferencesController.php | 21 +++++++++++++++++++ controllers/admin/AdminOrdersController.php | 13 ++++++++++-- install/data/xml/configuration.xml | 5 ++++- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/classes/order/Order.php b/classes/order/Order.php index 79ba75e60..d5ec08a55 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -34,6 +34,10 @@ class OrderCore extends ObjectModel const ORDER_PAYMENT_TYPE_FULL = 1; const ORDER_PAYMENT_TYPE_ADVANCE = 2; + //Consts for: In the order list in which currency prices should be displayed + const ORDER_LIST_PRICE_DISPLAY_IN_PAYMENT_CURRENCY = 1; + const ORDER_LIST_PRICE_DISPLAY_IN_DEFAULT_CURRENCY = 2; + /** @var int Delivery address id */ public $id_address_delivery; @@ -1835,6 +1839,10 @@ public function addOrderPayment($amount_paid, $payment_method = null, $payment_t } } + // Whenever payment is adding in any order then set a cumulative conversion rate for the payment currency in the order + $this->conversion_rate = $order_payment->getCumulativeConversionRate($this->reference, $this->id_currency); + $this->save(); + return $res; } @@ -2660,4 +2668,4 @@ public function setWsTransactionId($transactionId) { $this->transaction_id = $transactionId; } -} \ No newline at end of file +} diff --git a/classes/order/OrderPayment.php b/classes/order/OrderPayment.php index 247f981c1..bb8b8a5ec 100644 --- a/classes/order/OrderPayment.php +++ b/classes/order/OrderPayment.php @@ -148,4 +148,15 @@ public function getOrderInvoice($id_order) return new OrderInvoice((int)$res); } + + /** + * Provides the cumulative conversion rate for a given order in any currency + * @param [string] $order_reference + * @param [int] $idCurrency + * @return float + */ + public function getCumulativeConversionRate($order_reference, $idCurrency) + { + return Db::getInstance()->getValue('SELECT (SUM(`conversion_rate`) / COUNT(*)) FROM `'._DB_PREFIX_.'order_payment` WHERE `order_reference` = \''.pSQL($order_reference).'\' AND `id_currency` = '.(int)$idCurrency); + } } diff --git a/controllers/admin/AdminOrderPreferencesController.php b/controllers/admin/AdminOrderPreferencesController.php index 21b31f6a7..d0be09ba2 100644 --- a/controllers/admin/AdminOrderPreferencesController.php +++ b/controllers/admin/AdminOrderPreferencesController.php @@ -58,6 +58,18 @@ public function __construct() ) ); + // Options to display currency in order list + $displayCurrencyOptions = array( + array( + 'value' => Order::ORDER_LIST_PRICE_DISPLAY_IN_PAYMENT_CURRENCY, + 'name' => $this->l('Payment currency') + ), + array( + 'value' => Order::ORDER_LIST_PRICE_DISPLAY_IN_DEFAULT_CURRENCY, + 'name' => $this->l('Default currency') + ) + ); + $this->fields_options = array( 'order_restrict' => array( 'title' => $this->l('Order Restrict'), @@ -159,6 +171,15 @@ public function __construct() 'cast' => 'intval', 'type' => 'bool' ), + 'PS_ORDER_LIST_PRICE_DISPLAY_CURRENCY' => array( + 'title' => $this->l('Display order list prices in'), + 'hint' => $this->l('Choose the currency in which you want the prices in the order list to be displayed.'), + 'validation' => 'isInt', + 'type' => 'select', + 'cast' => 'intval', + 'list' => $displayCurrencyOptions, + 'identifier' => 'value', + ), ), 'submit' => array('title' => $this->l('Save')) ), diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index 63a271ecd..d1a9c1719 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -58,11 +58,14 @@ public function __construct() $this->context = Context::getContext(); $this->_select = ' - (a.total_paid - a.total_paid_real) AS `amount_due`, a.source AS order_source, + IF('.((Configuration::get('PS_ORDER_LIST_PRICE_DISPLAY_CURRENCY') == Order::ORDER_LIST_PRICE_DISPLAY_IN_DEFAULT_CURRENCY)? 1 : 0).', (a.total_paid_tax_incl / a.conversion_rate), total_paid_tax_incl) AS total_paid_tax_incl, + IF('.((Configuration::get('PS_ORDER_LIST_PRICE_DISPLAY_CURRENCY') == Order::ORDER_LIST_PRICE_DISPLAY_IN_DEFAULT_CURRENCY)? 1 : 0).', ((a.total_paid - a.total_paid_real) / a.conversion_rate), (a.total_paid - a.total_paid_real)) AS amount_due, + a.source AS order_source, a.id_currency, a.id_order AS id_pdf, CONCAT(c.`firstname`, \' \', c.`lastname`) AS `customer`, osl.`name` AS `osname`, os.`color`, + cu.iso_code AS currency, IF((SELECT so.id_order FROM `'._DB_PREFIX_.'orders` so WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0, 0, 1) as new, IF(a.valid, 1, 0) badge_success, hbil.`hotel_name`, @@ -77,6 +80,7 @@ public function __construct() $this->_join = ' LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = a.`id_customer`) + LEFT JOIN `'._DB_PREFIX_.'currency` cu ON (cu.`id_currency` = a.`id_currency`) LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (os.`id_order_state` = a.`current_state`) LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int) $this->context->language->id.') LEFT JOIN `'._DB_PREFIX_.'htl_booking_detail` hbd ON (hbd.`id_order` = a.`id_order`) @@ -200,6 +204,11 @@ public function __construct() 'payment' => array( 'title' => $this->l('Payment') ), + 'currency' => array( + 'title' => $this->l('Payment Currency'), + 'hint' => $this->l('This is the currency in which customer made the payment'), + 'havingFilter' => true, + ), 'osname' => array( 'title' => $this->l('Status'), 'type' => 'select', @@ -5494,4 +5503,4 @@ public function changeRoomStatus() } } } -} \ No newline at end of file +} diff --git a/install/data/xml/configuration.xml b/install/data/xml/configuration.xml index 935de20ba..f7814e17a 100644 --- a/install/data/xml/configuration.xml +++ b/install/data/xml/configuration.xml @@ -224,6 +224,9 @@ 3 + + 1 + 0 @@ -906,4 +909,4 @@ 1 - \ No newline at end of file + From 01392fd3a52a9923cedc17e51ebf7a0f9df67ac8 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Thu, 12 Oct 2023 18:39:01 +0530 Subject: [PATCH 2/4] Fixed: Average conversion rate insertion and currency sign issue resolved in order list --- classes/order/Order.php | 4 +++- classes/order/OrderPayment.php | 4 ++-- controllers/admin/AdminOrdersController.php | 10 +++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/classes/order/Order.php b/classes/order/Order.php index d5ec08a55..b7de17382 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -1840,7 +1840,9 @@ public function addOrderPayment($amount_paid, $payment_method = null, $payment_t } // Whenever payment is adding in any order then set a cumulative conversion rate for the payment currency in the order - $this->conversion_rate = $order_payment->getCumulativeConversionRate($this->reference, $this->id_currency); + if ($avgConversionRate = $order_payment->getAverageConversionRate($this->reference, $this->id_currency)) { + $this->conversion_rate = $avgConversionRate; + } $this->save(); return $res; diff --git a/classes/order/OrderPayment.php b/classes/order/OrderPayment.php index bb8b8a5ec..5bd65d31e 100644 --- a/classes/order/OrderPayment.php +++ b/classes/order/OrderPayment.php @@ -150,12 +150,12 @@ public function getOrderInvoice($id_order) } /** - * Provides the cumulative conversion rate for a given order in any currency + * Provides the average conversion rate for a given order in any currency * @param [string] $order_reference * @param [int] $idCurrency * @return float */ - public function getCumulativeConversionRate($order_reference, $idCurrency) + public function getAverageConversionRate($order_reference, $idCurrency) { return Db::getInstance()->getValue('SELECT (SUM(`conversion_rate`) / COUNT(*)) FROM `'._DB_PREFIX_.'order_payment` WHERE `order_reference` = \''.pSQL($order_reference).'\' AND `id_currency` = '.(int)$idCurrency); } diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index d1a9c1719..b98d427f7 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -264,10 +264,14 @@ public function __construct() parent::__construct(); } - public static function setOrderCurrency($echo, $tr) + public static function setOrderCurrency($echo, $row) { - $order = new Order($tr['id_order']); - return Tools::displayPrice($echo, (int)$order->id_currency); + if (Configuration::get('PS_ORDER_LIST_PRICE_DISPLAY_CURRENCY') == Order::ORDER_LIST_PRICE_DISPLAY_IN_DEFAULT_CURRENCY) { + $idCurrency = Configuration::get('PS_CURRENCY_DEFAULT'); + } else { + $idCurrency = $row['id_currency']; + } + return Tools::displayPrice($echo, (int)$idCurrency); } public function renderForm() From f1ecb81b4de729476580303de9d6340ee6a20b0a Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Thu, 12 Oct 2023 19:07:44 +0530 Subject: [PATCH 3/4] Text changes for better explanations --- controllers/admin/AdminOrderPreferencesController.php | 3 ++- controllers/admin/AdminOrdersController.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/controllers/admin/AdminOrderPreferencesController.php b/controllers/admin/AdminOrderPreferencesController.php index d0be09ba2..77f3c791f 100644 --- a/controllers/admin/AdminOrderPreferencesController.php +++ b/controllers/admin/AdminOrderPreferencesController.php @@ -62,7 +62,7 @@ public function __construct() $displayCurrencyOptions = array( array( 'value' => Order::ORDER_LIST_PRICE_DISPLAY_IN_PAYMENT_CURRENCY, - 'name' => $this->l('Payment currency') + 'name' => $this->l('Order currency') ), array( 'value' => Order::ORDER_LIST_PRICE_DISPLAY_IN_DEFAULT_CURRENCY, @@ -174,6 +174,7 @@ public function __construct() 'PS_ORDER_LIST_PRICE_DISPLAY_CURRENCY' => array( 'title' => $this->l('Display order list prices in'), 'hint' => $this->l('Choose the currency in which you want the prices in the order list to be displayed.'), + 'desc' => $this->l('\'Order currency\' is the currency in which customer created the order and \'Default currency\' is the currency configured in localization.'), 'validation' => 'isInt', 'type' => 'select', 'cast' => 'intval', diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index b98d427f7..7bbd4d895 100644 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -205,8 +205,8 @@ public function __construct() 'title' => $this->l('Payment') ), 'currency' => array( - 'title' => $this->l('Payment Currency'), - 'hint' => $this->l('This is the currency in which customer made the payment'), + 'title' => $this->l('Order Currency'), + 'hint' => $this->l('This is the currency in which customer created the order.'), 'havingFilter' => true, ), 'osname' => array( From 9efa630464b08ddc117b423632f1e7f650413350 Mon Sep 17 00:00:00 2001 From: Sumit Panwar Date: Sat, 13 Jan 2024 15:59:04 +0530 Subject: [PATCH 4/4] Fixed:: Need to Consider each payment amount to calculate average conversion rate. --- classes/order/OrderPayment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/order/OrderPayment.php b/classes/order/OrderPayment.php index 5bd65d31e..b3f3c981b 100644 --- a/classes/order/OrderPayment.php +++ b/classes/order/OrderPayment.php @@ -157,6 +157,6 @@ public function getOrderInvoice($id_order) */ public function getAverageConversionRate($order_reference, $idCurrency) { - return Db::getInstance()->getValue('SELECT (SUM(`conversion_rate`) / COUNT(*)) FROM `'._DB_PREFIX_.'order_payment` WHERE `order_reference` = \''.pSQL($order_reference).'\' AND `id_currency` = '.(int)$idCurrency); + return Db::getInstance()->getValue('SELECT (SUM(`amount` * `conversion_rate`) / SUM(`amount`)) FROM `'._DB_PREFIX_.'order_payment` WHERE `order_reference` = \''.pSQL($order_reference).'\' AND `id_currency` = '.(int)$idCurrency); } }