Support updates to payments

This commit is contained in:
Steve Ireland
2019-04-26 22:04:55 -04:00
parent 3b49f7829e
commit a89674ccfe
7 changed files with 149 additions and 40 deletions

View File

@@ -4,6 +4,7 @@ require_once("Secure_Controller.php");
define('PRICE_MODE_STANDARD', 0);
define('PRICE_MODE_KIT', 1);
define('PAYMENT_TYPE_UNASSIGNED', '--');
class Sales extends Secure_Controller
{
@@ -1166,6 +1167,11 @@ class Sales extends Secure_Controller
$data['selected_customer_name'] = $sale_info['customer_name'];
$data['selected_customer_id'] = $sale_info['customer_id'];
$data['sale_info'] = $sale_info;
$balance_due = $sale_info['amount_due'] - $sale_info['amount_tendered'];
if($balance_due < 0)
{
$balance_due = 0;
}
$data['payments'] = array();
foreach($this->Sale->get_sale_payments($sale_id)->result() as $payment)
@@ -1177,8 +1183,19 @@ class Sales extends Secure_Controller
$data['payments'][] = $payment;
}
$data['payment_type_new'] = PAYMENT_TYPE_UNASSIGNED;
$data['payment_amount_new'] = $balance_due;
$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));
// 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);
$this->load->view('sales/form', $data);
}
@@ -1240,6 +1257,7 @@ class Sales extends Secure_Controller
public function save($sale_id = -1)
{
$newdate = $this->input->post('date');
$employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
$date_formatter = date_create_from_format($this->config->item('dateformat') . ' ' . $this->config->item('timeformat'), $newdate);
@@ -1251,39 +1269,28 @@ class Sales extends Secure_Controller
'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
// In order to maintain tradition the only element that can change on prior payments is the payment type
$payments = array();
$number_of_payments = $this->input->post('number_of_payments');
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);
// 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;
}
}
// 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
$payments[] = array('payment_id' => $payment_id, 'payment_type' => $payment_type, 'payment_amount' => $payment_amount, 'payment_user' => $employee_id);
}
$payment_id = -1;
$payment_amount = $this->input->post('payment_amount_new');
$payment_type = $this->input->post('payment_type_new');
if($payment_type != PAYMENT_TYPE_UNASSIGNED && $payment_amount <> 0)
{
$payments[] = array('payment_id' => $payment_id, 'payment_type' => $payment_type, 'payment_amount' => $payment_amount, 'payment_user' => $employee_id);
}
if($this->Sale->update($sale_id, $sale_data, $payments))
@@ -1480,7 +1487,7 @@ class Sales extends Secure_Controller
{
foreach($array as $key => $val)
{
if ($val['item_id'] === $id)
if($val['item_id'] === $id)
{
return $key;
}