Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/app/functions/fn.sales_reports.php b/app/functions/fn.sales_reports.php
- index 8ee3b762af..ae65909a44 100644
- --- a/app/functions/fn.sales_reports.php
- +++ b/app/functions/fn.sales_reports.php
- @@ -802,13 +802,15 @@ function fn_get_report_statistics(&$table)
- $time_start = $first_elm['time_from'];
- $time_end = $last_elm['time_to'];
- $new_data = array();
- + $data = array();
- foreach ($table['elements'] as $element) {
- + $hash = $element['element_hash'];
- - $a = $element['element_hash'];
- if (empty($element['auto_generated'])) {
- $element['request'] = fn_get_parameter_request($table['table_id'], $element['element_hash']);
- }
- +
- $time_condition = db_quote(" timestamp BETWEEN ?i AND ?i", $time_start, $time_end);
- $group_condition = ' GROUP BY `interval`';
- @@ -821,7 +823,7 @@ function fn_get_report_statistics(&$table)
- } elseif ($interval_code == 'day') {
- $add_field = db_quote(", DATE_FORMAT(FROM_UNIXTIME(timestamp), '%Y-%m-%d') as `interval`, timestamp");
- } else {
- - $add_field = db_quote(", 1 as `interval`, timestamp");
- + $add_field = db_quote(", 1 as `interval`, `timestamp`");
- $group_condition = '';
- }
- @@ -829,63 +831,62 @@ function fn_get_report_statistics(&$table)
- $fields = !empty($element['fields']) ? $element['fields'] : 'SUM(total)';
- $tables = !empty($element['tables']) ? $element['tables'] : '?:orders';
- - $data[$a] = db_get_hash_array("SELECT $fields as total $add_field FROM $tables WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- + $data[$hash] = db_get_hash_array("SELECT $fields as total $add_field FROM $tables WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- } elseif ($table['display'] == 'order_number') {
- - $data[$a] = db_get_hash_array("SELECT COUNT(total) as total $add_field FROM ?:orders WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- + $data[$hash] = db_get_hash_array("SELECT COUNT(total) as total $add_field FROM ?:orders WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- } elseif ($table['display'] == 'shipping') {
- - $data[$a] = db_get_hash_array("SELECT SUM(shipping_cost) as total $add_field FROM ?:orders WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- + $data[$hash] = db_get_hash_array("SELECT SUM(shipping_cost) as total $add_field FROM ?:orders WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- } elseif ($table['display'] == 'discount') {
- - $data[$a] = db_get_hash_array("SELECT SUM(subtotal_discount) as total, ?:order_details.extra $add_field FROM ?:order_details LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id WHERE $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- + $where = db_quote(
- + '?p AND ?p ?p ?p',
- + $element['request'],
- + $time_condition,
- + $order_ids,
- + $group_condition
- + );
- - foreach ($data[$a] as $int => $interval_data) {
- - $extra = @unserialize($interval_data['extra']);
- - if (!empty($extra['discount'])) {
- - $data[$a][$int]['total'] += $extra['discount'];
- - }
- - unset($interval_data['extra']);
- - $data[$a][$int]['total'] = fn_format_price($data[$a][$int]['total']);
- - }
- + $data = fn_sales_reports_get_orders_subtotal_discount($data, $add_field, $where, $hash);
- } elseif ($table['display'] == 'tax') {
- $all_taxes = db_get_hash_array("SELECT ?:order_data.data $add_field FROM ?:order_data LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_data.order_id WHERE ?:order_data.type = 'T' AND $element[request] AND $time_condition $order_ids $group_condition", 'interval');
- foreach ($all_taxes as $int => $interval_data) {
- - $data[$a][$int] = $interval_data;
- - $data[$a][$int]['total'] = 0;
- + $data[$hash][$int] = $interval_data;
- + $data[$hash][$int]['total'] = 0;
- $taxes = @unserialize($interval_data['data']);
- if (is_array($taxes)) {
- foreach ($taxes as $tax_data) {
- if (!empty($tax_data['tax_subtotal'])) {
- - $data[$a][$int]['total'] += $tax_data['tax_subtotal'];
- + $data[$hash][$int]['total'] += $tax_data['tax_subtotal'];
- }
- }
- }
- - unset($data[$a][$int]['data']);
- - $data[$a][$int]['total'] = fn_format_price($data[$a][$int]['total']);
- + unset($data[$hash][$int]['data']);
- + $data[$hash][$int]['total'] = fn_format_price($data[$hash][$int]['total']);
- }
- } elseif ($table['display'] == 'product_cost') {
- $product_cost = (empty($element['product_ids'])) ? '' : db_quote(" AND ?:order_details.product_id IN (?p)", $element['product_ids']);
- - $data[$a] = db_get_hash_array("SELECT SUM(amount * price) as total $add_field FROM ?:order_details LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id WHERE $element[request] AND $time_condition $order_ids ?p $group_condition", 'interval', $product_cost);
- + $data[$hash] = db_get_hash_array("SELECT SUM(amount * price) as total $add_field FROM ?:order_details LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id WHERE $element[request] AND $time_condition $order_ids ?p $group_condition", 'interval', $product_cost);
- } elseif ($table['display'] == 'product_number') {
- $product_count = (empty($element['product_ids'])) ? '' : db_quote(" AND ?:order_details.product_id IN (?p)", $element['product_ids']);
- - $data[$a] = db_get_hash_array("SELECT SUM(amount) as total $add_field FROM ?:order_details LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id WHERE $element[request] AND $time_condition $order_ids ?p $group_condition", 'interval', $product_count);
- + $data[$hash] = db_get_hash_array("SELECT SUM(amount) as total $add_field FROM ?:order_details LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id WHERE $element[request] AND $time_condition $order_ids ?p $group_condition", 'interval', $product_count);
- }
- foreach ($table['intervals'] as $interval) {
- $b = $interval['interval_id'];
- - if (isset($data[$a])) {
- - foreach ($data[$a] as $interval_data) {
- + if (isset($data[$hash])) {
- + foreach ($data[$hash] as $interval_data) {
- if ($interval_data['timestamp'] >= $interval['time_from'] && $interval_data['timestamp'] <= $interval['time_to']) {
- - $new_data[$a][$b] = $interval_data['total'];
- + $new_data[$hash][$b] = $interval_data['total'];
- break;
- }
- }
- }
- - if (!isset($new_data[$a][$b])) {
- - $new_data[$a][$b] = 0;
- + if (!isset($new_data[$hash][$b])) {
- + $new_data[$hash][$b] = 0;
- }
- }
- }
- @@ -1320,8 +1321,8 @@ function fn_get_order_totals($table)
- $table_totals = array();
- foreach ($table['elements'] as $element) {
- -
- $data = array();
- +
- if (empty($element['auto_generated'])) {
- $element['request'] = fn_get_parameter_request($table['table_id'], $element['element_hash']);
- }
- @@ -1351,16 +1352,15 @@ function fn_get_order_totals($table)
- } elseif ($table['display'] == 'shipping') {
- $data = db_get_hash_array("SELECT SUM(shipping_cost) as total $add_field FROM ?:orders WHERE {$element['request']} AND $time_condition $order_ids $group_condition", 'interval');
- } elseif ($table['display'] == 'discount') {
- - $data = db_get_hash_array("SELECT SUM(subtotal_discount) as total, ?:order_details.extra $add_field FROM ?:order_details LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id WHERE {$element['request']} AND $time_condition $order_ids $group_condition", 'interval');
- + $where = db_quote(
- + '?p AND ?p ?p ?p',
- + $element['request'],
- + $time_condition,
- + $order_ids,
- + $group_condition
- + );
- - foreach ($data as $int => $interval_data) {
- - $extra = @unserialize($interval_data['extra']);
- - if (!empty($extra['discount'])) {
- - $data[$int]['total'] += $extra['discount'];
- - }
- - unset($data[$int]['extra']);
- - $data[$int]['total'] = fn_format_price($data[$int]['total']);
- - }
- + $data = fn_sales_reports_get_orders_subtotal_discount($data, $add_field, $where);
- } elseif ($table['display'] == 'tax') {
- $data = db_get_hash_array("SELECT ?:order_data.data $add_field FROM ?:order_data LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_data.order_id WHERE ?:order_data.type = 'T' AND {$element['request']} AND $time_condition $order_ids $group_condition", 'interval');
- @@ -1573,3 +1573,55 @@ function fn_get_max_value_report_interval($report, $table_id)
- return $max_value;
- }
- +
- +/**
- + * Calculates total discount for product in orders or an order itself
- + *
- + * @param string $additional_fields Additional fields fot fetch from the database
- + * @param string $where Where clause for query
- + * @param null|string $hash Item hash that the totals is calculated for
- + *
- + * @return array|mixed
- + */
- +function fn_sales_reports_get_orders_subtotal_discount($data, $additional_fields, $where, $hash = null)
- +{
- + $original_hash = $hash;
- + $hash = !is_null($hash) ? $hash : 0;
- +
- + $orders_data = db_get_array(
- + "SELECT ?:orders.order_id, subtotal_discount as total, ?:order_details.extra {$additional_fields}"
- + . ' FROM ?:order_details'
- + . ' LEFT JOIN ?:orders ON ?:orders.order_id = ?:order_details.order_id'
- + . ' WHERE ?p',
- + $where
- + );
- +
- + $applied_discounts_order_ids = array();
- +
- + foreach ($orders_data as $order_item_data) {
- + $interval = $order_item_data['interval'];
- +
- + if (!isset($data[$hash][$interval]['total'])) {
- + $data[$hash][$interval]['total'] = 0;
- + $data[$hash][$interval]['interval'] = $interval;
- + $data[$hash][$interval]['timestamp'] = $order_item_data['timestamp'];
- + }
- +
- + if (!empty($order_item_data['total'])
- + && !in_array($order_item_data['order_id'], $applied_discounts_order_ids)
- + ) {
- + $data[$hash][$interval]['total'] += $order_item_data['total'];
- + $applied_discounts_order_ids[] = $order_item_data['order_id'];
- + }
- +
- + $extra = @unserialize($order_item_data['extra']);
- +
- + if (!empty($extra['discount'])) {
- + $data[$hash][$interval]['total'] += $extra['discount'];
- + }
- +
- + $data[$hash][$interval]['total'] = fn_format_price($data[$hash][$interval]['total']);
- + }
- +
- + return is_null($original_hash) ? reset($data) : $data;
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement