From b4eabd226529186d8857913c6c1744768ce5458e Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sun, 19 Jan 2020 18:24:11 +0000 Subject: [PATCH] Fix missing cash refund in sale edit form --- application/controllers/Sales.php | 27 +++++++++++---- application/language/en-GB/sales_lang.php | 1 + application/language/en-US/sales_lang.php | 1 + application/models/Sale.php | 23 ++++++------- application/views/sales/form.php | 41 ++++++++++++++++++----- 5 files changed, 66 insertions(+), 27 deletions(-) diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index 0cf7a1b9c..8c6de1c3d 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -1215,10 +1215,10 @@ class Sales extends Secure_Controller $data['balance_due'] = $balance_due != 0; // don't allow gift card to be a payment option in a sale transaction edit because it's a complex change - $data['payment_options'] = $this->xss_clean($this->Sale->get_payment_options(FALSE)); + $new_payment_options = $this->xss_clean($this->Sale->get_payment_options(FALSE)); + $data['payment_options'] = $new_payment_options; // Set up a slightly modified list of payment types for new payment entry - $new_payment_options = $this->Sale->get_payment_options(FALSE); $new_payment_options["--"] = $this->lang->line('common_none_selected_text'); $data['new_payment_options'] = $this->xss_clean($new_payment_options); @@ -1301,12 +1301,27 @@ class Sales extends Secure_Controller for($i = 0; $i < $number_of_payments; ++$i) { $payment_id = $this->input->post('payment_id_' . $i); - $payment_amount = $this->input->post('payment_amount_' . $i); $payment_type = $this->input->post('payment_type_' . $i); - $cash_refund = 0.00; + $payment_amount = $this->input->post('payment_amount_' . $i); + $refund_type = $this->input->post('refund_type_' . $i); + $cash_refund = $this->input->post('refund_amount_' . $i); - // To maintain tradition we will also delete any payments with 0 amount assuming these are mistakes - // introduced at sale time. This is now done in Sale.php + // if the refund is not cash ... + if(empty(strstr($refund_type, $this->lang->line('sales_cash')))) + { + // ... and it's positive ... + if($cash_refund > 0) + { + // ... change it to be a new negative payment (a "non-cash refund") + $payment_type = $refund_type; + $payment_amount = $payment_amount - $cash_refund; + $cash_refund = 0.00; + } + } + + // To maintain tradition we will also delete any payments with 0 amount + // assuming these are mistakes introduced at sale time. + // This is now done in models/Sale.php $payments[] = array('payment_id' => $payment_id, 'payment_type' => $payment_type, 'payment_amount' => $payment_amount, 'cash_refund' => $cash_refund, 'employee_id' => $employee_id); } diff --git a/application/language/en-GB/sales_lang.php b/application/language/en-GB/sales_lang.php index 7520d2d58..c48af41fd 100644 --- a/application/language/en-GB/sales_lang.php +++ b/application/language/en-GB/sales_lang.php @@ -121,6 +121,7 @@ $lang["sales_receipt_no_email"] = "This customer does not have a valid email add $lang["sales_receipt_number"] = "Sale #"; $lang["sales_receipt_sent"] = "Receipt sent to"; $lang["sales_receipt_unsent"] = "Receipt failed to be sent to"; +$lang["sales_refund"] = "Refund Type"; $lang["sales_register"] = "Sales Register"; $lang["sales_remove_customer"] = "Remove Customer"; $lang["sales_return"] = "Return"; diff --git a/application/language/en-US/sales_lang.php b/application/language/en-US/sales_lang.php index 7e83e0ab5..350eb9178 100644 --- a/application/language/en-US/sales_lang.php +++ b/application/language/en-US/sales_lang.php @@ -121,6 +121,7 @@ $lang["sales_receipt_no_email"] = "This customer does not have a valid email add $lang["sales_receipt_number"] = "Sale #"; $lang["sales_receipt_sent"] = "Receipt sent to"; $lang["sales_receipt_unsent"] = "Receipt failed to be sent to"; +$lang["sales_refund"] = "Refund Type"; $lang["sales_register"] = "Sales Register"; $lang["sales_remove_customer"] = "Remove Customer"; $lang["sales_return"] = "Return"; diff --git a/application/models/Sale.php b/application/models/Sale.php index e0792214f..fa91d1c07 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -549,7 +549,7 @@ class Sale extends CI_Model $cash_refund = $payment['cash_refund']; $employee_id = $payment['employee_id']; - if($payment_id == - 1 && $payment_amount > 0) + if($payment_id == -1 && $payment_amount != 0) { // Add a new payment transaction $sales_payments_data = array( @@ -561,21 +561,22 @@ class Sale extends CI_Model ); $success = $this->db->insert('sales_payments', $sales_payments_data); } - - if($payment_id != - 1) + elseif($payment_id != -1) { - if($payment_amount > 0) + if($payment_amount != 0) { // Update existing payment transactions (payment_type only) $sales_payments_data = array( - 'payment_type' => $payment_type + 'payment_type' => $payment_type, + 'payment_amount' => $payment_amount, + 'cash_refund' => $cash_refund ); - $this->db->where('payment_id',$payment_id); + $this->db->where('payment_id', $payment_id); $success = $this->db->update('sales_payments', $sales_payments_data); } else { - // Remove existing payment transactions with a payment amount of zero + // Remove existing payment transactions with a payment amount of zero $success = $this->db->delete('sales_payments', array('payment_id' => $payment_id)); } } @@ -589,7 +590,6 @@ class Sale extends CI_Model return $success; } - /** * Save the sale information after the sales is complete but before the final document is printed * The sales_taxes variable needs to be initialized to an empty array before calling @@ -640,17 +640,15 @@ class Sale extends CI_Model $total_amount_used = 0; foreach($payments as $payment_id=>$payment) { - if( substr( $payment['payment_type'], 0, strlen( $this->lang->line('sales_giftcard') ) ) == $this->lang->line('sales_giftcard') ) + if(!empty(strstr($payment['payment_type'], $this->lang->line('sales_giftcard')))) { // We have a gift card and we have to deduct the used value from the total value of the card. $splitpayment = explode( ':', $payment['payment_type'] ); $cur_giftcard_value = $this->Giftcard->get_giftcard_value( $splitpayment[1] ); $this->Giftcard->update_giftcard_value( $splitpayment[1], $cur_giftcard_value - $payment['payment_amount'] ); } - - if( substr( $payment['payment_type'], 0, strlen( $this->lang->line('sales_rewards') ) ) == $this->lang->line('sales_rewards') ) + elseif(!empty(strstr($payment['payment_type'], $this->lang->line('sales_rewards')))) { - $cur_rewards_value = $this->Customer->get_info($customer_id)->points; $this->Customer->update_reward_points_value($customer_id, $cur_rewards_value - $payment['payment_amount'] ); $total_amount_used = floatval($total_amount_used) + floatval($payment['payment_amount']); @@ -1398,6 +1396,7 @@ class Sale extends CI_Model return $this->db->trans_status(); } + /** * Gets suspended sale info */ diff --git a/application/views/sales/form.php b/application/views/sales/form.php index 687eb3340..a6462c6ed 100755 --- a/application/views/sales/form.php +++ b/application/views/sales/form.php @@ -50,7 +50,7 @@ config->item('currency_symbol'); ?> 'payment_amount_new', 'value'=>$payment_amount_new, 'id'=>'payment_amount_new', 'class'=>'form-control input-sm'));?> - + config->item('currency_symbol'); ?> @@ -68,13 +68,13 @@
lang->line('sales_payment'), 'payment_'.$i, array('class'=>'control-label col-xs-3')); ?>
- - payment_id); ?> - payment_type, $this->lang->line('sales_giftcard'))) ): ?> - 'payment_type_'.$i, 'value'=>$row->payment_type, 'id'=>'payment_type_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?> - - payment_type, array('id'=>'payment_types_'.$i, 'class'=>'form-control')); ?> - + + payment_id); ?> + payment_type, $this->lang->line('sales_giftcard'))) ): ?> + 'payment_type_'.$i, 'value'=>$row->payment_type, 'id'=>'payment_type_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?> + + payment_type, array('id'=>'payment_types_'.$i, 'class'=>'form-control')); ?> +
@@ -82,7 +82,30 @@ config->item('currency_symbol'); ?> 'payment_amount_'.$i, 'value'=>$row->payment_amount, 'id'=>'payment_amount_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?> - + + config->item('currency_symbol'); ?> + +
+
+
+ +
+ lang->line('sales_refund'), 'refund_'.$i, array('class'=>'control-label col-xs-3')); ?> +
+ + payment_type, $this->lang->line('sales_giftcard'))) ): ?> + 'refund_type_'.$i, 'value'=>$this->lang->line('sales_cash'), 'id'=>'refund_type_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?> + + lang->line('sales_cash'), array('id'=>'refund_types_'.$i, 'class'=>'form-control')); ?> + +
+
+
+ + config->item('currency_symbol'); ?> + + 'refund_amount_'.$i, 'value'=>$row->cash_refund, 'id'=>'refund_amount_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?> + config->item('currency_symbol'); ?>