mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-02-18 14:48:42 -05:00
Added ability to update payment type when editing a sale (#314)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<div class="col-xs-6">
|
||||
<div class="input-group input-group-sm">
|
||||
<?php if (!$this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
<?php echo form_input(array(
|
||||
'name'=>'total',
|
||||
@@ -44,7 +44,7 @@
|
||||
'disabled'=>'')
|
||||
);?>
|
||||
<?php if ($this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
<div class="col-xs-4">
|
||||
<div class="input-group input-group-sm">
|
||||
<?php if (!$this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
<?php echo form_input(array(
|
||||
'name'=>'cost_price',
|
||||
@@ -67,7 +67,7 @@
|
||||
'value'=>to_currency_no_money($item_info->cost_price))
|
||||
);?>
|
||||
<?php if ($this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
@@ -78,7 +78,7 @@
|
||||
<div class='col-xs-4'>
|
||||
<div class="input-group input-group-sm">
|
||||
<?php if (!$this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
<?php echo form_input(array(
|
||||
'name'=>'unit_price',
|
||||
@@ -87,7 +87,7 @@
|
||||
'value'=>to_currency_no_money($item_info->unit_price))
|
||||
);?>
|
||||
<?php if ($this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
<div class='col-xs-4'>
|
||||
<div class="input-group input-group-sm">
|
||||
<?php if (!$this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
<?php echo form_input(array(
|
||||
'name'=>'cost_price',
|
||||
@@ -49,7 +49,7 @@
|
||||
'class'=>'form-control input-sm')
|
||||
);?>
|
||||
<?php if ($this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
@@ -60,7 +60,7 @@
|
||||
<div class='col-xs-4'>
|
||||
<div class="input-group input-group-sm">
|
||||
<?php if (!$this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
<?php echo form_input(array(
|
||||
'name'=>'unit_price',
|
||||
@@ -68,7 +68,7 @@
|
||||
'class'=>'form-control input-sm')
|
||||
);?>
|
||||
<?php if ($this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
<?php echo form_open("sales/save/".$sale_info['sale_id'], array('id'=>'sales_edit_form', 'class'=>'form-horizontal')); ?>
|
||||
<div class="form-group form-group-sm">
|
||||
<?php echo form_label($this->lang->line('sales_receipt_number'), 'receipt_number', array('class'=>'control-label col-xs-3')); ?>
|
||||
<div class='col-xs-6'>
|
||||
<?php echo anchor('sales/receipt/'.$sale_info['sale_id'], $this->lang->line('sales_receipt_number') .$sale_info['sale_id'], array('target' => '_blank'));?>
|
||||
</div>
|
||||
<?php echo anchor('sales/receipt/'.$sale_info['sale_id'], 'POS ' . $sale_info['sale_id'], array('target'=>'_blank', 'class'=>'control-label col-xs-6', "style"=>"text-align:left"));?>
|
||||
</div>
|
||||
|
||||
<div class="form-group form-group-sm">
|
||||
@@ -31,11 +29,44 @@
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
$i = 0;
|
||||
foreach($payments->result() as $row)
|
||||
{
|
||||
?>
|
||||
<div class="form-group form-group-sm">
|
||||
<?php echo form_label($this->lang->line('sales_payment'), 'payment_'.$i, array('class'=>'control-label col-xs-3')); ?>
|
||||
<div class='col-xs-3'>
|
||||
<?php // no editing of Gift Card payments as it's a complex change ?>
|
||||
<?php if( !empty(strstr($row->payment_type, $this->lang->line('sales_giftcard'))) ): ?>
|
||||
<?php echo form_input(array('name'=>'payment_type_'.$i, 'value'=>$row->payment_type, 'id'=>'payment_type_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?>
|
||||
<?php else: ?>
|
||||
<?php echo form_dropdown('payment_type_'.$i, $payment_options, $row->payment_type, array('id'=>'payment_types_'.$i, 'class'=>'form-control')); ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class='col-xs-3'>
|
||||
<div class="input-group input-group-sm">
|
||||
<?php if(!$this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
<?php echo form_input(array('name'=>'payment_amount_'.$i, 'value'=>$row->payment_amount, 'id'=>'payment_amount_'.$i, 'class'=>'form-control input-sm', 'readonly'=>'true'));?>
|
||||
<?php if ($this->config->item('currency_side')): ?>
|
||||
<span class="input-group-addon input-sm"><b><?php echo $this->config->item('currency_symbol'); ?></b></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
$i++;
|
||||
}
|
||||
echo form_hidden('number_of_payments', $i);
|
||||
?>
|
||||
|
||||
<div class="form-group form-group-sm">
|
||||
<?php echo form_label($this->lang->line('sales_customer'), 'customer', array('class'=>'control-label col-xs-3')); ?>
|
||||
<div class='col-xs-6'>
|
||||
<?php echo form_input(array('name' => 'customer_name', 'value' => $selected_customer_name, 'id' => 'customer_name', 'class'=>'form-control input-sm'));?>
|
||||
<?php echo form_input(array('name'=>'customer_name', 'value =>$selected_customer_name', 'id'=>'customer_name', 'class'=>'form-control input-sm'));?>
|
||||
<?php echo form_hidden('customer_id', $selected_customer_id);?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user