From d2565009d8ce8ef570774bb354a6265479c79ddc Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sat, 12 Mar 2016 09:23:54 +0000 Subject: [PATCH] fix wrong behaviour with due amount and Complete button on "sale" <=0 and "return" >=0 (#116) --- application/controllers/Sales.php | 22 ++++++++++++++++++- application/libraries/Sale_lib.php | 33 ++++++++++++++-------------- application/views/sales/register.php | 2 +- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index c30775bcd..35a82c588 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -230,7 +230,7 @@ class Sales extends Secure_area $new_giftcard_value = ( $new_giftcard_value >= 0 ) ? $new_giftcard_value : 0; $this->sale_lib->set_giftcard_remainder($new_giftcard_value); $data['warning'] = $this->lang->line('giftcards_remaining_balance', $this->input->post('amount_tendered'), to_currency( $new_giftcard_value, TRUE )); - $payment_amount = min( $this->sale_lib->get_amount_due( ), $this->Giftcard->get_giftcard_value( $this->input->post('amount_tendered') ) ); + $payment_amount = min( $this->sale_lib->get_amount_due(), $this->Giftcard->get_giftcard_value( $this->input->post('amount_tendered') ) ); } else { @@ -669,6 +669,25 @@ class Sales extends Secure_area } } + private function _payments_cover_total() + { + // Changed the conditional to account for floating point rounding + + // "sale" amount due needs to be <=0 to state it's fine + if( ($this->sale_lib->get_mode() == 'sale') && $this->sale_lib->get_amount_due() > 1e-6 ) + { + return false; + } + + // "return" amount due needs to be >=0 to state it's fine + if( ($this->sale_lib->get_mode() == 'return') && $this->sale_lib->get_amount_due() < -(1e-6) ) + { + return false; + } + + return true; + } + private function _reload($data=array()) { $person_info = $this->Employee->get_logged_in_employee_info(); @@ -735,6 +754,7 @@ class Sales extends Secure_area $data['invoice_number'] = $this->_substitute_invoice_number($cust_info); $data['invoice_number_enabled'] = $this->sale_lib->is_invoice_number_enabled(); $data['print_after_sale'] = $this->sale_lib->is_print_after_sale(); + $data['payments_cover_total'] = $this->_payments_cover_total(); $this->load->view("sales/register", $data); } diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index 828e9536a..a1ba8c524 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -110,7 +110,7 @@ class Sale_lib $this->CI->session->unset_userdata('email_receipt'); } - function add_payment( $payment_id, $payment_amount ) + function add_payment($payment_id, $payment_amount) { $payments = $this->get_payments(); if( isset( $payments[$payment_id] ) ) @@ -122,22 +122,22 @@ class Sale_lib { //add to existing array $payment = array( $payment_id=> - array( - 'payment_type' => $payment_id, - 'payment_amount' => $payment_amount - ) + array( + 'payment_type' => $payment_id, + 'payment_amount' => $payment_amount + ) ); $payments += $payment; } - $this->set_payments( $payments ); + $this->set_payments($payments); return true; } // Multiple Payments - function edit_payment($payment_id,$payment_amount) + function edit_payment($payment_id, $payment_amount) { $payments = $this->get_payments(); if(isset($payments[$payment_id])) @@ -151,7 +151,7 @@ class Sale_lib } // Multiple Payments - function delete_payment( $payment_id ) + function delete_payment($payment_id) { $payments = $this->get_payments(); unset( $payments[urldecode( $payment_id )] ); @@ -172,17 +172,17 @@ class Sale_lib { $subtotal = bcadd($payments['payment_amount'], $subtotal, PRECISION); } + return to_currency_no_money($subtotal); } // Multiple Payments function get_amount_due() { - $amount_due=0; $payment_total = $this->get_payments_total(); - $sales_total=$this->get_total(); - $amount_due=to_currency_no_money(bcsub($sales_total, $payment_total, PRECISION)); - return $amount_due; + $sales_total = $this->get_total(); + + return to_currency_no_money(bcsub($sales_total, $payment_total, PRECISION)); } function get_customer() @@ -218,6 +218,7 @@ class Sale_lib $location_id = $this->CI->Stock_location->get_default_location_id(); $this->set_sale_location($location_id); } + return $this->CI->session->userdata('sale_location'); } @@ -246,7 +247,7 @@ class Sale_lib $this->CI->session->unset_userdata('giftcard_remainder'); } - function add_item($item_id,$quantity=1,$item_location,$discount=0,$price=null,$description=null,$serialnumber=null) + function add_item($item_id, $quantity=1, $item_location, $discount=0, $price=null, $description=null, $serialnumber=null) { //make sure item exists if($this->validate_item($item_id) == false) @@ -334,7 +335,7 @@ class Sale_lib return true; } - function out_of_stock($item_id,$item_location) + function out_of_stock($item_id, $item_location) { //make sure item exists if($this->validate_item($item_id) == false) @@ -359,7 +360,7 @@ class Sale_lib return false; } - function get_quantity_already_added($item_id,$item_location) + function get_quantity_already_added($item_id, $item_location) { $items = $this->get_cart(); $quanity_already_added = 0; @@ -389,7 +390,7 @@ class Sale_lib return -1; } - function edit_item($line,$description,$serialnumber,$quantity,$discount,$price) + function edit_item($line, $description, $serialnumber, $quantity, $discount, $price) { $items = $this->get_cart(); if(isset($items[$line])) diff --git a/application/views/sales/register.php b/application/views/sales/register.php index 00e4996b1..e69b1ddcf 100644 --- a/application/views/sales/register.php +++ b/application/views/sales/register.php @@ -341,7 +341,7 @@ if (isset($success)) 'add_payment_form', 'class'=>'form-horizontal')); ?>