-if ($ret = context_restore()) {
- if(isset($ret['customer_id']))
- $_POST['customer_id'] = $ret['customer_id'];
- if(isset($ret['branch_id']))
- $_POST['BranchID'] = $ret['branch_id'];
+if (isset($_GET['customer_id']))
+{
+ $_POST['customer_id'] = $_GET['customer_id'];
+}
+
+if (!isset($_POST['bank_account'])) { // first page call
+ $_SESSION['alloc'] = new allocation(ST_CUSTPAYMENT, 0, get_post('customer_id'));
+
+ if (isset($_GET['SInvoice'])) {
+ // get date and supplier
+ $inv = get_customer_trans($_GET['SInvoice'], ST_SALESINVOICE);
+ $dflt_act = get_default_bank_account($inv['curr_code']);
+ $_POST['bank_account'] = $dflt_act['id'];
+ if($inv) {
+ $_SESSION['alloc']->person_id = $_POST['customer_id'] = $inv['debtor_no'];
+ $_SESSION['alloc']->read();
+ $_POST['BranchID'] = $inv['branch_code'];
+ $_POST['DateBanked'] = sql2date($inv['tran_date']);
+ foreach($_SESSION['alloc']->allocs as $line => $trans) {
+ if ($trans->type == ST_SALESINVOICE && $trans->type_no == $_GET['SInvoice']) {
+ $un_allocated = $trans->amount - $trans->amount_allocated;
+ if ($un_allocated){
+ $_SESSION['alloc']->allocs[$line]->current_allocated = $un_allocated;
+ $_POST['amount'] = $_POST['amount'.$line] = price_format($un_allocated);
+ }
+ break;
+ }
+ }
+ unset($inv);
+ } else
+ display_error(_("Invalid sales invoice number."));
+ }
+}
+
+if (list_updated('BranchID')) {
+ // when branch is selected via external editor also customer can change
+ $br = get_branch(get_post('BranchID'));
+ $_POST['customer_id'] = $br['debtor_no'];
+ $_SESSION['alloc']->person_id = $br['debtor_no'];
+ $Ajax->activate('customer_id');
+}
+
+if (!isset($_POST['customer_id'])) {
+ $_SESSION['alloc']->person_id = $_POST['customer_id'] = get_global_customer(false);
+ $_SESSION['alloc']->read();