From 38d234fa5fb9db5c4ae54cf79e3c5d15b1eb59c7 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sat, 9 Jul 2016 10:04:04 +0100 Subject: [PATCH] Fixed receipt, change due and total cash issues with no payment cases (#707) --- application/controllers/Sales.php | 8 ++--- application/helpers/locale_helper.php | 10 +++++- application/helpers/table_helper.php | 6 ++-- application/libraries/Sale_lib.php | 4 +-- application/models/Sale.php | 37 +++++++++++---------- application/views/sales/receipt_default.php | 6 ++-- application/views/sales/receipt_email.php | 6 ++-- application/views/sales/receipt_short.php | 6 ++-- 8 files changed, 47 insertions(+), 36 deletions(-) diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index daf301988..267b733c3 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -727,7 +727,7 @@ class Sales extends Secure_Controller public function save($sale_id = -1) { $newdate = $this->input->post('date'); - + $date_formatter = date_create_from_format($this->config->item('dateformat') . ' ' . $this->config->item('timeformat'), $newdate); $sale_data = array( @@ -737,7 +737,7 @@ class Sales extends Secure_Controller 'comment' => $this->input->post('comment'), 'invoice_number' => $this->input->post('invoice_number') != '' ? $this->input->post('invoice_number') : NULL ); - + // go through all the payment type input from the form, make sure the form matches the name and iterator number $payments = array(); $number_of_payments = $this->input->post('number_of_payments'); @@ -769,7 +769,7 @@ class Sales extends Secure_Controller } } } - + if($this->Sale->update($sale_id, $sale_data, $payments)) { echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('sales_successfully_updated'), 'id' => $sale_id)); @@ -786,7 +786,7 @@ class Sales extends Secure_Controller $this->_reload(); } - + public function suspend() { $cart = $this->sale_lib->get_cart(); diff --git a/application/helpers/locale_helper.php b/application/helpers/locale_helper.php index 6fa642474..06dadbd8d 100644 --- a/application/helpers/locale_helper.php +++ b/application/helpers/locale_helper.php @@ -36,6 +36,13 @@ function to_currency_no_money($number) function to_tax_decimals($number) { + // taxes that are NULL, '' or 0 don't need to be displayed + // NOTE: do not remove this line otherwise the items edit form will show a tax with 0 and it will save it + if(empty($number)) + { + return $number; + } + return to_decimals($number, 'tax_decimals'); } @@ -47,7 +54,8 @@ function to_quantity_decimals($number) function to_decimals($number, $decimals, $type=\NumberFormatter::DECIMAL) { // ignore empty strings and return - if(empty($number)) + // NOTE: do not change it to empty because otherwise tables will show a 0 with no decimal nor currency symbol + if(!isset($number)) { return $number; } diff --git a/application/helpers/table_helper.php b/application/helpers/table_helper.php index 0216400b6..340533a84 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -89,7 +89,7 @@ Get the sales payments summary function get_sales_manage_payments_summary($payments, $sales, $controller) { $CI =& get_instance(); - $table='
'; + $table = '
'; foreach($payments as $key=>$payment) { @@ -104,9 +104,9 @@ function get_sales_manage_payments_summary($payments, $sales, $controller) $amount -= $sale['change_due']; } } - $table.='
'.$payment['payment_type'].': '.to_currency( $amount ) . '
'; + $table .= '
' . $payment['payment_type'] . ': ' . to_currency( $amount ) . '
'; } - $table.='
'; + $table .= '
'; return $table; } diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index fd9c8e75f..fd7b89cb2 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -188,7 +188,7 @@ class Sale_lib { $payment_total = $this->get_payments_total(); $sales_total = $this->get_total(); - + return bcsub($sales_total, $payment_total, PRECISION); } @@ -553,7 +553,7 @@ class Sale_lib $customer = $this->CI->Customer->get_info($customer_id); //Do not charge sales tax if we have a customer that is not taxable - return $customer->taxable or $customer_id==-1; + return $customer->taxable or $customer_id == -1; } public function get_taxes() diff --git a/application/models/Sale.php b/application/models/Sale.php index aefd6c460..60deaf3cd 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -4,7 +4,7 @@ class Sale extends CI_Model public function get_info($sale_id) { $this->db->select('customer_id, customer_name, customer_first_name AS first_name, customer_last_name AS last_name, customer_email AS email, customer_comments AS comments, - sale_payment_amount AS amount_tendered, (sale_payment_amount - total) AS change_due, total AS amount_due, payment_type, + sale_payment_amount AS amount_tendered, SUM(total) AS amount_due, (sale_payment_amount - SUM(total)) AS change_due, payment_type, sale_id, sale_date, sale_time, comment, invoice_number, employee_id'); $this->db->from('sales_items_temp'); @@ -14,7 +14,7 @@ class Sale extends CI_Model return $this->db->get(); } - + /* Get number of rows for the takings (sales/manage) view */ @@ -76,7 +76,10 @@ class Sale extends CI_Model if($filters['only_cash'] != FALSE) { - $this->db->like('payment_type ', $this->lang->line('sales_cash'), 'after'); + $this->db->group_start(); + $this->db->like('payment_type', $this->lang->line('sales_cash'), 'after'); + $this->db->or_where('payment_type IS NULL'); + $this->db->group_end(); } $this->db->group_by('sale_id'); @@ -136,7 +139,7 @@ class Sale extends CI_Model if($filters['only_cash'] != FALSE) { - $this->db->like('payment_type ', $this->lang->line('sales_cash'), 'after'); + $this->db->like('payment_type', $this->lang->line('sales_cash'), 'after'); } $this->db->group_by('payment_type'); @@ -215,7 +218,7 @@ class Sale extends CI_Model return $this->db->count_all_results(); } - + public function get_sale_by_invoice_number($invoice_number) { $this->db->from('sales'); @@ -223,7 +226,7 @@ class Sale extends CI_Model return $this->db->get(); } - + public function get_invoice_number_for_year($year = '', $start_from = 0) { $year = $year == '' ? date('Y') : $year; @@ -243,7 +246,7 @@ class Sale extends CI_Model return ($this->db->get()->num_rows()==1); } - + public function update($sale_id, $sale_data, $payments) { $this->db->where('sale_id', $sale_id); @@ -277,7 +280,7 @@ class Sale extends CI_Model return $success; } - + public function save($items, $customer_id, $employee_id, $comment, $invoice_number, $payments, $sale_id = FALSE) { if(count($items) == 0) @@ -349,7 +352,7 @@ class Sale extends CI_Model } // Inventory Count Details - $sale_remarks ='POS '.$sale_id; + $sale_remarks = 'POS '.$sale_id; $inv_data = array( 'trans_date' => date('Y-m-d H:i:s'), 'trans_items' => $item['item_id'], @@ -385,7 +388,7 @@ class Sale extends CI_Model return $sale_id; } - + public function delete_list($sale_ids, $employee_id, $update_inventory = TRUE) { $result = TRUE; @@ -397,7 +400,7 @@ class Sale extends CI_Model return $result; } - + public function delete($sale_id, $employee_id, $update_inventory = TRUE) { // start a transaction to assure data integrity @@ -458,7 +461,7 @@ class Sale extends CI_Model return $this->db->get(); } - + public function get_payment_options($giftcard = TRUE) { $payments = array(); @@ -499,7 +502,7 @@ class Sale extends CI_Model return $this->Customer->get_info($this->db->get()->row()->customer_id); } - + public function invoice_number_exists($invoice_number, $sale_id = '') { $this->db->from('sales'); @@ -511,7 +514,7 @@ class Sale extends CI_Model return ($this->db->get()->num_rows() == 1); } - + public function get_giftcard_value($giftcardNumber) { if(!$this->Giftcard->exists($this->Giftcard->get_giftcard_id($giftcardNumber))) @@ -576,7 +579,7 @@ class Sale extends CI_Model sales_items.item_location, sales_items.description, payments.payment_type, - payments.sale_payment_amount, + IFNULL(payments.sale_payment_amount, 0) AS sale_payment_amount, SUM(sales_items_taxes.percent) AS item_tax_percent, ' . " ROUND($sale_total * $total, $decimals) AS total, @@ -592,8 +595,8 @@ class Sale extends CI_Model ON sales_items.item_id = items.item_id LEFT OUTER JOIN ( SELECT sale_id, - SUM(payment_amount) AS sale_payment_amount, - GROUP_CONCAT(CONCAT(payment_type, " ", payment_amount) SEPARATOR ", ") AS payment_type + SUM(payment_amount) AS sale_payment_amount, + GROUP_CONCAT(CONCAT(payment_type, " ", payment_amount) SEPARATOR ", ") AS payment_type FROM ' . $this->db->dbprefix('sales_payments') . ' GROUP BY sale_id ) AS payments diff --git a/application/views/sales/receipt_default.php b/application/views/sales/receipt_default.php index 7b935a5d6..8ed81e9cd 100644 --- a/application/views/sales/receipt_default.php +++ b/application/views/sales/receipt_default.php @@ -143,12 +143,12 @@ $payment) { - $only_sale_check &= $payment['payment_type'] == $this->lang->line('sales_check'); - $splitpayment=explode(':',$payment['payment_type']); + $only_sale_check |= $payment['payment_type'] == $this->lang->line('sales_check'); + $splitpayment = explode(':', $payment['payment_type']); $show_giftcard_remainder |= $splitpayment[0] == $this->lang->line('sales_giftcard'); ?> diff --git a/application/views/sales/receipt_email.php b/application/views/sales/receipt_email.php index 568201109..97cc4bc42 100644 --- a/application/views/sales/receipt_email.php +++ b/application/views/sales/receipt_email.php @@ -138,12 +138,12 @@ $payment) { - $only_sale_check &= $payment['payment_type'] == $this->lang->line('sales_check'); - $splitpayment=explode(':',$payment['payment_type']); + $only_sale_check |= $payment['payment_type'] == $this->lang->line('sales_check'); + $splitpayment = explode(':', $payment['payment_type']); $show_giftcard_remainder |= $splitpayment[0] == $this->lang->line('sales_giftcard'); ?> diff --git a/application/views/sales/receipt_short.php b/application/views/sales/receipt_short.php index ce486c6e7..69640260d 100644 --- a/application/views/sales/receipt_short.php +++ b/application/views/sales/receipt_short.php @@ -141,12 +141,12 @@ $payment) { - $only_sale_check &= $payment['payment_type'] == $this->lang->line('sales_check'); - $splitpayment=explode(':',$payment['payment_type']); + $only_sale_check |= $payment['payment_type'] == $this->lang->line('sales_check'); + $splitpayment = explode(':', $payment['payment_type']); $show_giftcard_remainder |= $splitpayment[0] == $this->lang->line('sales_giftcard'); ?>