From ebecd4d2dd811016529d8a91beea98ef758f3365 Mon Sep 17 00:00:00 2001 From: amit sharma Date: Wed, 7 Oct 2026 16:25:35 +0530 Subject: [PATCH] Fixed: revenue KPI not updating after order refund Co-authored-by: Name --- classes/Cart.php | 1 - controllers/admin/AdminStatsController.php | 8 ++++---- modules/dashgoals/dashgoals.php | 10 ++++++---- .../views/templates/hook/dashboard_zone_two.tpl | 2 +- modules/dashproducts/dashproducts.php | 2 +- modules/dashtrends/dashtrends.php | 8 +++++--- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 43b51984d..928e532af 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -2732,7 +2732,6 @@ class CartCore extends ObjectModel } $final_package_list = $hotelWisePackageList; - /// ddd($final_package_list); // END $package_list hotel wise $cache[$cache_key] = $final_package_list; return $final_package_list; diff --git a/controllers/admin/AdminStatsController.php b/controllers/admin/AdminStatsController.php index 42e26ab47..b55be0afd 100644 --- a/controllers/admin/AdminStatsController.php +++ b/controllers/admin/AdminStatsController.php @@ -644,11 +644,11 @@ class AdminStatsControllerCore extends AdminStatsTabController if ($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT LEFT(`invoice_date`, 10) as date, - SUM(orr.`refunded_amount`) as total_refund_amount + SUM(orr.`refunded_amount` * (o.`total_paid_tax_excl` / o.`total_paid_tax_incl`) / o.`conversion_rate`) as total_refund_amount FROM `'._DB_PREFIX_.'orders` o LEFT JOIN `'._DB_PREFIX_.'order_return` orr ON (o.id_order = orr.id_order) LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (o.current_state = os.id_order_state) - WHERE orr.`payment_mode` != "" AND `invoice_date` BETWEEN "'.pSQL($date_from).' 00:00:00" + WHERE os.`logable` = 1 AND o.`total_paid_tax_incl` > 0 AND orr.`payment_mode` != "" AND `invoice_date` BETWEEN "'.pSQL($date_from).' 00:00:00" AND "'.pSQL($date_to).' 23:59:59" AND ( EXISTS ( @@ -1646,9 +1646,9 @@ class AdminStatsControllerCore extends AdminStatsTabController public static function getRevenue($dateFrom, $dateTo, $idHotel = false, $orderSource = '') { - $sql = 'SELECT SUM(total_paid_tax_excl - refunded_amount) + $sql = 'SELECT SUM(GREATEST(total_paid_tax_excl - refunded_amount, 0)) FROM (SELECT o.`total_paid_tax_excl` / o.`conversion_rate` AS total_paid_tax_excl, - (SELECT IFNULL(SUM(orr.`refunded_amount`), 0) FROM`'._DB_PREFIX_.'order_return` orr WHERE orr.`id_order` = o.`id_order`) AS refunded_amount, + (SELECT IFNULL(SUM(orr.`refunded_amount`), 0) FROM`'._DB_PREFIX_.'order_return` orr WHERE orr.`id_order` = o.`id_order`) * IF(o.`total_paid_tax_incl` > 0, o.`total_paid_tax_excl` / o.`total_paid_tax_incl`, 1) / o.`conversion_rate` AS refunded_amount, (SELECT hbd.`id_hotel` FROM`'._DB_PREFIX_.'htl_booking_detail` hbd WHERE hbd.`id_order` = o.`id_order` LIMIT 1) AS id_hotel FROM `'._DB_PREFIX_.'orders` o diff --git a/modules/dashgoals/dashgoals.php b/modules/dashgoals/dashgoals.php index 16bc46c17..d629d9c5f 100644 --- a/modules/dashgoals/dashgoals.php +++ b/modules/dashgoals/dashgoals.php @@ -178,7 +178,9 @@ class Dashgoals extends Module { $year = ((isset($params['extra']) && $params['extra'] > 1970 && $params['extra'] < 2999) ? $params['extra'] : Configuration::get('PS_DASHGOALS_CURRENT_YEAR')); - return array('data_chart' => array('dash_goals_chart1' => $this->getChartData($year))); + $idHotel = isset($params['id_hotel']) ? (int) $params['id_hotel'] : 0; + + return array('data_chart' => array('dash_goals_chart1' => $this->getChartData($year, $idHotel))); } protected function fakeConfigurationKPI_get($key) @@ -198,7 +200,7 @@ class Dashgoals extends Module } } - public function getChartData($year) + public function getChartData($year, $idHotel = 0) { // There are stream types (different charts) and for each types there are 3 available zones (one color for the goal, one if you over perform and one if you under perfom) $stream_types = array( @@ -370,8 +372,8 @@ class Dashgoals extends Module } else { // Retrieve gross data from AdminStatsController $visits = AdminStatsController::getVisits(false, $year.date('-01-01'), $year.date('-12-31'), 'month'); - $orders = AdminStatsController::getOrders($year.date('-01-01'), $year.date('-12-31'), 'month'); - $sales = AdminStatsController::getTotalSales($year.date('-01-01'), $year.date('-12-31'), 'month'); + $orders = AdminStatsController::getOrders($year.date('-01-01'), $year.date('-12-31'), 'month', $idHotel); + $sales = AdminStatsController::getTotalSales($year.date('-01-01'), $year.date('-12-31'), 'month', $idHotel); // Now we can calculate the value for every months for ($i = '01'; $i <= 12; $i = sprintf('%02d', $i + 1)) { diff --git a/modules/dashgoals/views/templates/hook/dashboard_zone_two.tpl b/modules/dashgoals/views/templates/hook/dashboard_zone_two.tpl index 7217b8458..1bf5db652 100644 --- a/modules/dashgoals/views/templates/hook/dashboard_zone_two.tpl +++ b/modules/dashgoals/views/templates/hook/dashboard_zone_two.tpl @@ -57,7 +57,7 @@

{l s='Set your targets by clicking the configuration button at right position of the header in this section.' mod='dashgoals'}

-

{l s='Note' mod='dashgoals'}: {l s='Targets will be set and data will be displayed for all the hotels.' mod='dashgoals'}

+

{l s='Note' mod='dashgoals'}: {l s='Targets are common for all the hotels, while the data is displayed for the selected hotel.' mod='dashgoals'}

diff --git a/modules/dashproducts/dashproducts.php b/modules/dashproducts/dashproducts.php index 9adb7f624..a0ec4bf8d 100644 --- a/modules/dashproducts/dashproducts.php +++ b/modules/dashproducts/dashproducts.php @@ -260,7 +260,7 @@ class DashProducts extends Module FROM `'._DB_PREFIX_.'htl_booking_detail` hbd LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_order` = hbd.`id_order`) WHERE o.`invoice_date` BETWEEN "'.pSQL($date_from).' 00:00:00" AND "'.pSQL($date_to).' 23:59:59" - AND o.`valid` = 1 AND hbd.`id` NOT IN ('.OrderReturn::getRefundedBookingIdsSubquery().').'. + AND o.`valid` = 1 AND hbd.`id` NOT IN ('.OrderReturn::getRefundedBookingIdsSubquery().')'. (!is_null($id_hotel) ? HotelBranchInformation::addHotelRestriction($id_hotel, 'hbd') : '').' GROUP BY hbd.`id_product` ORDER BY `sales` DESC diff --git a/modules/dashtrends/dashtrends.php b/modules/dashtrends/dashtrends.php index 084e3f119..72b069966 100644 --- a/modules/dashtrends/dashtrends.php +++ b/modules/dashtrends/dashtrends.php @@ -146,15 +146,17 @@ class Dashtrends extends Module $refined_data['net_profits'][$date] = 0; // calculate actual values now for current date from available $gross_data to be used to display in line chart - $refined_data['sales'][$date] = isset($gross_data['total_paid_tax_excl'][$date]) ? $gross_data['total_paid_tax_excl'][$date] : 0; + // sales are net of refunded amount + $refunds = isset($gross_data['total_refunds'][$date]) ? $gross_data['total_refunds'][$date] : 0; + $refined_data['sales'][$date] = (isset($gross_data['total_paid_tax_excl'][$date]) ? $gross_data['total_paid_tax_excl'][$date] : 0) - $refunds; $refined_data['orders'][$date] = isset($gross_data['orders'][$date]) ? $gross_data['orders'][$date] : 0; $refined_data['average_cart_value'][$date] = $refined_data['orders'][$date] ? $refined_data['sales'][$date] / $refined_data['orders'][$date] : 0; $refined_data['visits'][$date] = isset($gross_data['visits'][$date]) ? $gross_data['visits'][$date] : 0; $refined_data['conversion_rate'][$date] = $refined_data['visits'][$date] ? ($refined_data['orders'][$date] / $refined_data['visits'][$date] * 100) : 0; - $refined_data['net_profits'][$date] += (isset($gross_data['total_paid_tax_excl'][$date]) ? $gross_data['total_paid_tax_excl'][$date] : 0); + // sales already have refunds deducted + $refined_data['net_profits'][$date] += $refined_data['sales'][$date]; $refined_data['net_profits'][$date] -= (isset($gross_data['total_purchases'][$date]) ? $gross_data['total_purchases'][$date] : 0); $refined_data['net_profits'][$date] -= (isset($gross_data['total_expenses'][$date]) ? $gross_data['total_expenses'][$date] : 0); - $refined_data['net_profits'][$date] -= (isset($gross_data['total_refunds'][$date]) ? $gross_data['total_refunds'][$date] : 0); } return $refined_data;