Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed calculation in SQL query if there are no refunds #25

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions statssales.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,11 @@ private function getTotals()
$idCountry = (int) Tools::getValue('id_country');

$sql = 'SELECT COUNT(o.`id_order`) as orderCount,';
$sql .= ' IFNULL(';
// Sum Orders
$sql .= 'SUM(o.`total_paid_tax_excl` / o.conversion_rate)';
$sql .= 'IFNULL(SUM(o.`total_paid_tax_excl` / o.conversion_rate), 0)';
// Sum Refunds
$sql .= '- SUM((ps.total_products_tax_excl - ps.total_shipping_tax_excl) / ps.conversion_rate)';
$sql .= ', 0) as orderSum';
$sql .= '- IFNULL(SUM((ps.total_products_tax_excl - ps.total_shipping_tax_excl) / ps.conversion_rate), 0)';
$sql .= ' as orderSum';
$sql .= ' FROM `' . _DB_PREFIX_ . 'orders` o';
$sql .= ' LEFT JOIN `' . _DB_PREFIX_ . 'order_slip` ps ON o.id_order = ps.id_order';
$sql .= ' INNER JOIN `' . _DB_PREFIX_ . 'order_state` os ON o.current_state = os.id_order_state';
Expand Down Expand Up @@ -262,15 +261,21 @@ protected function getData($layers)
return $this->getStatesData();
}

$this->query = '
SELECT o.`invoice_date`, o.`total_paid_real` / o.conversion_rate as total_paid_real, SUM(od.product_quantity) as product_quantity
FROM `' . _DB_PREFIX_ . 'orders` o
LEFT JOIN `' . _DB_PREFIX_ . 'order_detail` od ON od.`id_order` = o.`id_order`
' . ((int) $this->id_country ? 'LEFT JOIN `' . _DB_PREFIX_ . 'address` a ON o.id_address_delivery = a.id_address' : '') . '
WHERE o.valid = 1
' . Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o') . '
' . ((int) $this->id_country ? 'AND a.id_country = ' . (int) $this->id_country : '') . '
AND o.`invoice_date` BETWEEN ';
$this->query = 'SELECT o.`invoice_date`, '
// Sum Orders
. 'IFNULL(SUM(o.`total_paid_tax_excl` / o.conversion_rate), 0)'
// Sum Refunds
. ' - IFNULL(SUM((ps.total_products_tax_excl - ps.total_shipping_tax_excl) / ps.conversion_rate), 0)'
. ' as total_paid_real, '
. ' (SELECT SUM(product_quantity) FROM `' . _DB_PREFIX_ . 'order_detail` WHERE `id_order` = o.`id_order`) as product_quantity'
. ' FROM `' . _DB_PREFIX_ . 'orders` o'
. ' LEFT JOIN `' . _DB_PREFIX_ . 'order_slip` ps ON o.id_order = ps.id_order'
. ' INNER JOIN `' . _DB_PREFIX_ . 'order_state` os ON o.current_state = os.id_order_state'
. ((int) $this->id_country ? ' INNER JOIN `' . _DB_PREFIX_ . 'address` a ON o.id_address_delivery = a.id_address' : '')
. ' WHERE o.valid = 1 AND os.logable = 1'
. Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o')
. ((int) $this->id_country ? ' AND a.id_country = ' . (int) $this->id_country : '')
. ' AND o.`invoice_date` BETWEEN ';
$this->query_group_by = ' GROUP BY o.id_order';
$this->setDateGraph($layers, true);
}
Expand All @@ -286,6 +291,8 @@ protected function setAllTimeValues($layers)
$this->_values[(int) substr($row['invoice_date'], 0, 4)] += $row['total_paid_real'];
}
}

$this->formatValues();
}

protected function setYearValues($layers)
Expand All @@ -309,6 +316,8 @@ protected function setYearValues($layers)
$this->_values[$mounth] += $row['total_paid_real'];
}
}

$this->formatValues();
}

protected function setMonthValues($layers)
Expand All @@ -322,6 +331,8 @@ protected function setMonthValues($layers)
$this->_values[(int) substr($row['invoice_date'], 8, 2)] += $row['total_paid_real'];
}
}

$this->formatValues();
}

protected function setDayValues($layers)
Expand All @@ -335,6 +346,18 @@ protected function setDayValues($layers)
$this->_values[(int) substr($row['invoice_date'], 11, 2)] += $row['total_paid_real'];
}
}

$this->formatValues();
}

private function formatValues()
{
if ($this->option == 1) {
return;
}
foreach ($this->_values as $key => $value) {
$this->_values[$key] = number_format($value, 2);
}
}

private function getStatesData()
Expand Down