Merge pull request #1582 from RuleDomain/sales-update-fix

Restore code that was mistakenly "cleaned up"
This commit is contained in:
FrancescoUK
2017-09-12 07:04:20 +01:00
committed by GitHub

View File

@@ -1128,6 +1128,68 @@ class Sales extends Secure_Controller
}
}
/**
* This saves the sale from the update sale view (sales/form).
* It only updates the sales table and payments.
* @param int $sale_id
*/
public function save($sale_id = -1)
{
$newdate = $this->input->post('date');
$date_formatter = date_create_from_format($this->config->item('dateformat') . ' ' . $this->config->item('timeformat'), $newdate);
$sale_data = array(
'sale_time' => $date_formatter->format('Y-m-d H:i:s'),
'customer_id' => $this->input->post('customer_id') != '' ? $this->input->post('customer_id') : NULL,
'employee_id' => $this->input->post('employee_id'),
'comment' => $this->input->post('comment'),
'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
$payments = array();
$number_of_payments = $this->input->post('number_of_payments');
for($i = 0; $i < $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));
}
else
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('sales_unsuccessfully_updated'), 'id' => $sale_id));
}
}
/**
* This is used to cancel a suspended pos sale, quote.
* Completed sales (POS Sales or Invoiced Sales) can not be removed from the system