mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-03-06 08:11:12 -05:00
Fix cash totals in payment summary (#3138)
This commit is contained in:
@@ -77,7 +77,7 @@ class Sales extends Secure_Controller
|
||||
$sales = $this->Sale->search($search, $filters, $limit, $offset, $sort, $order);
|
||||
$total_rows = $this->Sale->get_found_rows($search, $filters);
|
||||
$payments = $this->Sale->get_payments_summary($search, $filters);
|
||||
$payment_summary = $this->xss_clean(get_sales_manage_payments_summary($payments, $sales));
|
||||
$payment_summary = $this->xss_clean(get_sales_manage_payments_summary($payments));
|
||||
|
||||
$data_rows = array();
|
||||
foreach($sales->result() as $sale)
|
||||
|
||||
@@ -147,25 +147,13 @@ function get_sale_data_last_row($sales)
|
||||
/*
|
||||
Get the sales payments summary
|
||||
*/
|
||||
function get_sales_manage_payments_summary($payments, $sales)
|
||||
function get_sales_manage_payments_summary($payments)
|
||||
{
|
||||
$CI =& get_instance();
|
||||
|
||||
$table = '<div id="report_summary">';
|
||||
|
||||
foreach($payments as $key=>$payment)
|
||||
{
|
||||
$amount = $payment['payment_amount'];
|
||||
|
||||
// WARNING: the strong assumption here is that if a change is due it was a cash transaction always
|
||||
// therefore we remove from the total cash amount any change due
|
||||
if($payment['payment_type'] == $CI->lang->line('sales_cash'))
|
||||
{
|
||||
foreach($sales->result_array() as $key=>$sale)
|
||||
{
|
||||
$amount -= $sale['change_due'];
|
||||
}
|
||||
}
|
||||
$table .= '<div class="summary_row">' . $payment['payment_type'] . ': ' . to_currency($amount) . '</div>';
|
||||
}
|
||||
$table .= '</div>';
|
||||
|
||||
@@ -9,33 +9,15 @@ class Sale extends CI_Model
|
||||
*/
|
||||
public function get_info($sale_id)
|
||||
{
|
||||
// NOTE: temporary tables are created to speed up searches due to the fact that they are ortogonal to the main query
|
||||
// create a temporary table to contain all the payments per sale
|
||||
$this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_payments_temp') .
|
||||
' (PRIMARY KEY(sale_id), INDEX(sale_id))
|
||||
(
|
||||
SELECT payments.sale_id AS sale_id,
|
||||
SUM(CASE WHEN payments.cash_adjustment = 0 THEN payments.payment_amount ELSE 0 END) AS sale_payment_amount,
|
||||
SUM(CASE WHEN payments.cash_adjustment = 1 THEN payments.payment_amount ELSE 0 END) AS sale_cash_adjustment,
|
||||
SUM(payments.cash_refund) AS sale_cash_refund,
|
||||
GROUP_CONCAT(CONCAT(payments.payment_type, " ", (payments.payment_amount - payments.cash_refund)) SEPARATOR ", ") AS payment_type
|
||||
FROM ' . $this->db->dbprefix('sales_payments') . ' AS payments
|
||||
INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales
|
||||
ON sales.sale_id = payments.sale_id
|
||||
WHERE sales.sale_id = ' . $this->db->escape($sale_id) . '
|
||||
GROUP BY sale_id
|
||||
)'
|
||||
);
|
||||
$this->create_temp_table(array('sale_id' => $sale_id));
|
||||
|
||||
$decimals = totals_decimals();
|
||||
|
||||
$sales_tax = 'IFNULL(SUM(sales_items_taxes.sales_tax), 0)';
|
||||
$cash_adjustment = 'IFNULL(SUM(payments.sale_cash_adjustment), 0)';
|
||||
$sale_price = 'CASE WHEN sales_items.discount_type = ' . PERCENT
|
||||
. " THEN sales_items.quantity_purchased * sales_items.item_unit_price - ROUND(sales_items.quantity_purchased * sales_items.item_unit_price * sales_items.discount / 100, $decimals) "
|
||||
. 'ELSE sales_items.quantity_purchased * (sales_items.item_unit_price - sales_items.discount) END';
|
||||
|
||||
$sales_tax = 'IFNULL(SUM(sales_items_taxes.sales_tax), 0)';
|
||||
$cash_adjustment = 'IFNULL(SUM(payments.sale_cash_adjustment), 0)';
|
||||
|
||||
if($this->config->item('tax_included'))
|
||||
{
|
||||
$sale_total = "ROUND(SUM($sale_price), $decimals) + $cash_adjustment";
|
||||
@@ -45,25 +27,6 @@ class Sale extends CI_Model
|
||||
$sale_total = "ROUND(SUM($sale_price), $decimals) + $sales_tax + $cash_adjustment";
|
||||
}
|
||||
|
||||
// create a temporary table to contain all the sum of taxes per sale item
|
||||
$this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') .
|
||||
' (INDEX(sale_id, item_id, line)) ENGINE=MEMORY
|
||||
(
|
||||
SELECT sales_items_taxes.sale_id AS sale_id,
|
||||
sales_items_taxes.item_id AS item_id,
|
||||
sales_items_taxes.line AS line,
|
||||
SUM(ROUND(CASE WHEN sales_items_taxes.tax_type = 0 THEN sales_items_taxes.item_tax_amount ELSE 0 END, ' . $decimals . ')) AS internal_tax,
|
||||
SUM(ROUND(CASE WHEN sales_items_taxes.tax_type = 1 THEN sales_items_taxes.item_tax_amount ELSE 0 END, ' . $decimals . ')) AS sales_tax
|
||||
FROM ' . $this->db->dbprefix('sales_items_taxes') . ' AS sales_items_taxes
|
||||
INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales
|
||||
ON sales.sale_id = sales_items_taxes.sale_id
|
||||
INNER JOIN ' . $this->db->dbprefix('sales_items') . ' AS sales_items
|
||||
ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.line = sales_items_taxes.line
|
||||
WHERE sales.sale_id = ' . $this->db->escape($sale_id) . '
|
||||
GROUP BY sale_id, item_id, line
|
||||
)'
|
||||
);
|
||||
|
||||
$this->db->select('
|
||||
sales.sale_id AS sale_id,
|
||||
MAX(DATE(sales.sale_time)) AS sale_date,
|
||||
@@ -131,12 +94,12 @@ class Sale extends CI_Model
|
||||
$where .= 'sales.sale_time BETWEEN ' . $this->db->escape(rawurldecode($filters['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($filters['end_date']));
|
||||
}
|
||||
|
||||
// NOTE: temporary tables are created to speed up searches due to the fact that they are ortogonal to the main query
|
||||
// NOTE: temporary tables are created to speed up searches due to the fact that they are orthogonal to the main query
|
||||
// create a temporary table to contain all the payments per sale item
|
||||
$this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_payments_temp') .
|
||||
' (PRIMARY KEY(sale_id), INDEX(sale_id))
|
||||
(
|
||||
SELECT payments.sale_id AS sale_id,
|
||||
SELECT payments.sale_id,
|
||||
SUM(CASE WHEN payments.cash_adjustment = 0 THEN payments.payment_amount ELSE 0 END) AS sale_payment_amount,
|
||||
SUM(CASE WHEN payments.cash_adjustment = 1 THEN payments.payment_amount ELSE 0 END) AS sale_cash_adjustment,
|
||||
GROUP_CONCAT(CONCAT(payments.payment_type, " ", (payments.payment_amount - payments.cash_refund)) SEPARATOR ", ") AS payment_type
|
||||
@@ -144,7 +107,7 @@ class Sale extends CI_Model
|
||||
INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales
|
||||
ON sales.sale_id = payments.sale_id
|
||||
WHERE ' . $where . '
|
||||
GROUP BY sale_id
|
||||
GROUP BY payments.sale_id
|
||||
)'
|
||||
);
|
||||
|
||||
@@ -305,7 +268,7 @@ class Sale extends CI_Model
|
||||
public function get_payments_summary($search, $filters)
|
||||
{
|
||||
// get payment summary
|
||||
$this->db->select('payment_type, COUNT(payment_amount) AS count, SUM(payment_amount) AS payment_amount');
|
||||
$this->db->select('payment_type, COUNT(payment_amount) AS count, SUM(payment_amount - cash_refund) AS payment_amount');
|
||||
$this->db->from('sales AS sales');
|
||||
$this->db->join('sales_payments', 'sales_payments.sale_id = sales.sale_id');
|
||||
$this->db->join('people AS customer_p', 'sales.customer_id = customer_p.person_id', 'LEFT');
|
||||
@@ -1109,7 +1072,7 @@ class Sale extends CI_Model
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates sales temporary dimentional table
|
||||
* Creates sales temporary dimensional table
|
||||
* We create a temp table that allows us to do easy report/sales queries
|
||||
*/
|
||||
public function create_temp_table(array $inputs)
|
||||
@@ -1182,6 +1145,7 @@ class Sale extends CI_Model
|
||||
SELECT payments.sale_id AS sale_id,
|
||||
SUM(CASE WHEN payments.cash_adjustment = 0 THEN payments.payment_amount ELSE 0 END) AS sale_payment_amount,
|
||||
SUM(CASE WHEN payments.cash_adjustment = 1 THEN payments.payment_amount ELSE 0 END) AS sale_cash_adjustment,
|
||||
SUM(payments.cash_refund) AS sale_cash_refund,
|
||||
GROUP_CONCAT(CONCAT(payments.payment_type, " ", (payments.payment_amount - payments.cash_refund)) SEPARATOR ", ") AS payment_type
|
||||
FROM ' . $this->db->dbprefix('sales_payments') . ' AS payments
|
||||
INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales
|
||||
@@ -1256,10 +1220,6 @@ class Sale extends CI_Model
|
||||
GROUP BY sale_id, item_id, line
|
||||
)'
|
||||
);
|
||||
|
||||
// drop the temporary table to contain memory consumption as it's no longer required
|
||||
$this->db->query('DROP TEMPORARY TABLE IF EXISTS ' . $this->db->dbprefix('sales_payments_temp'));
|
||||
$this->db->query('DROP TEMPORARY TABLE IF EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user