From 83d93464bc363311503167bda56fe8ffdd439a5b Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sun, 17 Apr 2016 21:51:52 +0100 Subject: [PATCH] Added ability to update payment type when editing a sale (#314) --- application/controllers/Sales.php | 48 +++++++++++++++++++++------ application/libraries/Sale_lib.php | 4 +-- application/models/Sale.php | 41 +++++++++++++++++++++-- application/views/customers/form.php | 4 +-- application/views/items/form.php | 8 ++--- application/views/items/form_bulk.php | 8 ++--- application/views/sales/form.php | 39 +++++++++++++++++++--- 7 files changed, 123 insertions(+), 29 deletions(-) diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index e8d36c27d..723f08bc8 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -526,6 +526,7 @@ class Sales extends Secure_area { $invoice_number = $this->config->config['sales_invoice_format']; $invoice_number = $this->_substitute_variables($invoice_number, $cust_info); + return $this->sale_lib->get_invoice_number() != $invoice_number; } @@ -600,7 +601,7 @@ class Sales extends Secure_area function invoice($sale_id, $sale_info='') { - if ($sale_info == '') + if($sale_info == '') { $sale_info = $this->_load_sale_data($sale_id); } @@ -622,9 +623,12 @@ class Sales extends Secure_area $sale_info = $this->Sale->get_info($sale_id)->row_array(); $person_name = $sale_info['first_name'] . " " . $sale_info['last_name']; - $data['selected_customer_name'] = !empty($sale_info['customer_id']) ? $person_name : ''; + $data['selected_customer_name'] = !empty($sale_info['customer_id']) ? $person_name : ''; $data['selected_customer_id'] = $sale_info['customer_id']; $data['sale_info'] = $sale_info; + $data['payments'] = $this->Sale->get_sale_payments($sale_id); + // 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->Sale->get_payment_options(false); $this->load->view('sales/form', $data); } @@ -659,7 +663,37 @@ class Sales extends Secure_area 'invoice_number' => $this->input->post('invoice_number') != '' ? $this->input->post('invoice_number') : null ); - if ($this->Sale->update($sale_data, $sale_id)) + // go through all the payment type input from the form, make sure the form matches the name and iterator number + $payments = array(); + for($i = 0; $i < $this->input->post('number_of_payments'); $i++) + { + $payment_amount = $this->input->post('payment_amount_'.$i); + $payment_type = $this->input->post('payment_type_'.$i); + // remove any 0 payment if by mistake any was introduced at sale time + if($payment_amount != 0) + { + // search for any payment of the same type that was already added, if that's the case add up the new payment amount + $key = FALSE; + if( !empty($payments) ) + { + // search in the multi array the key of the entry containing the current payment_type (NOTE: in PHP5.5 the array_map could be replaced by an array_column) + $key = array_search($payment_type, array_map(function($v){return $v['payment_type'];}, $payments)); + } + + // if no previous payment is found add a new one + if( $key === FALSE ) + { + $payments[] = array('payment_type'=>$payment_type, 'payment_amount'=>$payment_amount); + } + else + { + // add up the new payment amount to an existing payment type + $payments[$key]['payment_amount'] += $payment_amount; + } + } + } + + 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)); } @@ -709,13 +743,7 @@ class Sales extends Secure_area $data['payments_total'] = $this->sale_lib->get_payments_total(); $data['amount_due'] = $this->sale_lib->get_amount_due(); $data['payments'] = $this->sale_lib->get_payments(); - $data['payment_options'] = array( - $this->lang->line('sales_debit') => $this->lang->line('sales_debit'), - $this->lang->line('sales_credit') => $this->lang->line('sales_credit'), - $this->lang->line('sales_cash') => $this->lang->line('sales_cash'), - $this->lang->line('sales_giftcard') => $this->lang->line('sales_giftcard'), - $this->lang->line('sales_check') => $this->lang->line('sales_check') - ); + $data['payment_options'] = $this->Sale->get_payment_options(); $customer_id = $this->sale_lib->get_customer(); $cust_info = $this->_load_customer_data($customer_id, $data, true); diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index e0c6af192..b3dd0aeb4 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -25,8 +25,8 @@ class Sale_lib // Multiple Payments function get_payments() { - if( !$this->CI->session->userdata( 'payments' ) ) - $this->set_payments( array( ) ); + if(!$this->CI->session->userdata('payments')) + $this->set_payments(array()); return $this->CI->session->userdata('payments'); } diff --git a/application/models/Sale.php b/application/models/Sale.php index 5f5d2eb92..70482f3a8 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -10,7 +10,6 @@ class Sale extends CI_Model $this->db->select('SUM(item_unit_price * quantity_purchased * (1 - discount_percent / 100)) AS amount_due'); $this->db->from('sales_items_temp'); $this->db->join('people', 'people.person_id = sales_items_temp.customer_id', 'left'); - $this->db->where('sales_items_temp.sale_id', $sale_id); $this->db->group_by('sale_id'); $this->db->order_by('sale_time', 'asc'); @@ -230,7 +229,7 @@ class Sale extends CI_Model $this->db->where("invoice_number IS NOT ", "NULL", FALSE); $result = $this->db->get()->row_array(); - return ($start_from + $result[ 'invoice_number_year']); + return ($start_from + $result['invoice_number_year']); } public function exists($sale_id) @@ -241,10 +240,29 @@ class Sale extends CI_Model return ($this->db->get()->num_rows()==1); } - public function update($sale_data, $sale_id) + public function update($sale_id, $sale_data, $payments) { $this->db->where('sale_id', $sale_id); $success = $this->db->update('sales', $sale_data); + + // touch payment only if update sale is successful and there is a payments object otherwise the result would be to delete all the payments associated to the sale + if($success && !empty($payments)) + { + // first delete all payments + $this->db->delete('sales_payments', array('sale_id'=>$sale_id)); + + // add new payments + foreach($payments as $payment) + { + $sales_payments_data = array( + 'sale_id'=>$sale_id, + 'payment_type'=>$payment['payment_type'], + 'payment_amount'=>$payment['payment_amount'] + ); + + $success = $this->db->insert('sales_payments', $sales_payments_data); + } + } return $success; } @@ -424,6 +442,23 @@ class Sale extends CI_Model return $this->db->get(); } + + public function get_payment_options($giftcard=true) + { + $payments = array( + $this->lang->line('sales_debit') => $this->lang->line('sales_debit'), + $this->lang->line('sales_credit') => $this->lang->line('sales_credit'), + $this->lang->line('sales_cash') => $this->lang->line('sales_cash'), + $this->lang->line('sales_check') => $this->lang->line('sales_check') + ); + + if($giftcard) + { + $payments[$this->lang->line('sales_giftcard')] = $this->lang->line('sales_giftcard'); + } + + return $payments; + } public function get_customer($sale_id) { diff --git a/application/views/customers/form.php b/application/views/customers/form.php index 20ed32bba..870213bdb 100644 --- a/application/views/customers/form.php +++ b/application/views/customers/form.php @@ -34,7 +34,7 @@
config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?> 'total', @@ -44,7 +44,7 @@ 'disabled'=>'') );?> config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?>
diff --git a/application/views/items/form.php b/application/views/items/form.php index 84254d960..bb46e1889 100644 --- a/application/views/items/form.php +++ b/application/views/items/form.php @@ -58,7 +58,7 @@
config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?> 'cost_price', @@ -67,7 +67,7 @@ 'value'=>to_currency_no_money($item_info->cost_price)) );?> config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?>
@@ -78,7 +78,7 @@
config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?> 'unit_price', @@ -87,7 +87,7 @@ 'value'=>to_currency_no_money($item_info->unit_price)) );?> config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?>
diff --git a/application/views/items/form_bulk.php b/application/views/items/form_bulk.php index d214d23f1..ab6e3c177 100644 --- a/application/views/items/form_bulk.php +++ b/application/views/items/form_bulk.php @@ -41,7 +41,7 @@
config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?> 'cost_price', @@ -49,7 +49,7 @@ 'class'=>'form-control input-sm') );?> config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?>
@@ -60,7 +60,7 @@
config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?> 'unit_price', @@ -68,7 +68,7 @@ 'class'=>'form-control input-sm') );?> config->item('currency_side')): ?> - config->item('currency_symbol'); ?> + config->item('currency_symbol'); ?>
diff --git a/application/views/sales/form.php b/application/views/sales/form.php index 7da5d029a..bbe5fec04 100755 --- a/application/views/sales/form.php +++ b/application/views/sales/form.php @@ -7,9 +7,7 @@ 'sales_edit_form', 'class'=>'form-horizontal')); ?>
lang->line('sales_receipt_number'), 'receipt_number', array('class'=>'control-label col-xs-3')); ?> -
- lang->line('sales_receipt_number') .$sale_info['sale_id'], array('target' => '_blank'));?> -
+ '_blank', 'class'=>'control-label col-xs-6', "style"=>"text-align:left"));?>
@@ -31,11 +29,44 @@
+ + result() as $row) + { + ?> +
+ lang->line('sales_payment'), 'payment_'.$i, array('class'=>'control-label col-xs-3')); ?> +
+ + 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')); ?> + +
+
+
+ config->item('currency_side')): ?> + 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_side')): ?> + config->item('currency_symbol'); ?> + +
+
+
+
lang->line('sales_customer'), 'customer', array('class'=>'control-label col-xs-3')); ?>
- 'customer_name', 'value' => $selected_customer_name, 'id' => 'customer_name', 'class'=>'form-control input-sm'));?> + 'customer_name', 'value =>$selected_customer_name', 'id'=>'customer_name', 'class'=>'form-control input-sm'));?>