Customer Payments, Payments to Supplier: improved readbility and multiply cleanups...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 20 Sep 2013 22:19:28 +0000 (00:19 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 20 Sep 2013 22:19:28 +0000 (00:19 +0200)
15 files changed:
includes/banking.inc
includes/ui/allocation_cart.inc
purchasing/allocations/supplier_allocate.php
purchasing/includes/db/supp_trans_db.inc
purchasing/includes/db/suppalloc_db.inc
purchasing/includes/ui/invoice_ui.inc
purchasing/inquiry/supplier_allocation_inquiry.php
purchasing/supplier_payment.php
purchasing/view/view_supp_payment.php
sales/allocations/customer_allocate.php
sales/allocations/customer_allocation_main.php
sales/customer_payments.php
sales/includes/db/cust_trans_db.inc
sales/includes/db/custalloc_db.inc
sales/inquiry/customer_allocation_inquiry.php

index 0641dd59d2dfd22624e3d9f96b9ec44d9b3a4f32..53af3598b8580b7c3263d6fb46937a201e24a08b 100644 (file)
@@ -176,7 +176,7 @@ function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $am
                $supp_accs = get_supplier_accounts($trans['supplier_id']);
                $ar_ap_act = $supp_accs['payable_account'];
                $person_id = $trans['supplier_id'];
-               $curr = $trans['SupplierCurrCode'];
+               $curr = $trans['curr_code'];
                $date = sql2date($trans['tran_date']);
        }
        if (is_company_currency($curr))
index d408c80a6d0c52e4d7419974af23a749bf1ad089..1be54f5ca61442256d3eedde74d47efbf7172178 100644 (file)
@@ -22,25 +22,39 @@ class allocation
        var $type;
        var $person_id = '';
        var $person_name = '';
-       var $person_type;       // true - supplier, otherwise customer
+       var $person_type;       // PT_SUPPLIER/PT_CUSTOMER
+       var $person_curr;
        var $date_;
        var $amount = 0; /*Total amount of the transaction in FX */
-       
-       var $allocs; /*array of transactions allocated to */    
+       var $currency;
+
+       var $allocs; /*array of transactions allocated to */
 
-       function allocation($type, $trans_no)
+       function allocation($type, $trans_no, $person_id = null, $person_type_id=null)
        {
                $this->allocs = array();
                
                $this->trans_no = $trans_no;
                $this->type = $type;
-               $this->read(); // read payment or credit
+               if ($person_id)
+                       $this->set_person($person_id, $person_type_id);
+
+               $this->read($type, $trans_no, $person_id, $person_type_id); // read payment or credit
+       }
+
+       function set_person($person_id, $person_type)
+       {
+               $this->person_id = $person_id;
+               $this->person_type = $person_type;
+               $this->person_curr = $person_type == PT_SUPPLIER ?
+                       get_supplier_currency($person_id) : get_customer_currency($person_id);
+               return $this->person_curr;
        }
 
        function add_item($type, $type_no, $date_, $due_date, $amount, $amount_allocated, 
                $current_allocated, $ref)
        {
-               if ($amount > 0)
+               if (floatcmp($amount, 0))
                {
                        $this->allocs[count($this->allocs)] = new allocation_item($type, $type_no, 
                                $date_, $due_date, $amount, $amount_allocated, $current_allocated, $ref);
@@ -55,7 +69,7 @@ class allocation
        function update_item($index, $type, $type_no, $date_, $due_date, 
                $amount, $amount_allocated, $current_allocated, $ref)
        {
-               if ($amount > 0)
+               if (floatcmp($amount, 0))
                {
                        $this->allocs[$index] = new allocation_item($type, $type_no, 
                                $date_, $due_date, $amount, $amount_allocated, $current_allocated, $ref);
@@ -65,7 +79,7 @@ class allocation
                {
                        return false;
                }
-       }       
+       }
        
        function add_or_update_item($type, $type_no, $date_, $due_date, 
                $amount, $amount_allocated, $current_allocated, $ref)
@@ -77,7 +91,7 @@ class allocation
                        {
                                return $this->update_item($i, $type, $type_no, $date_, $due_date, 
                                        $amount, $amount_allocated, $current_allocated, $ref);
-                       }  
+                       }
                }
         return $this->add_item($type, $type_no, $date_, $due_date, 
                $amount, $amount_allocated, $current_allocated, $ref);
@@ -88,64 +102,72 @@ class allocation
        //
        // FIXME - read all transactions below twice seems to be suboptimal
        //
-       function read($type = null, $trans_no = 0
+       function read($type = null, $trans_no = 0, $person_id=null, $person_type_id=null)
        {
-               if ($type == null) {    // re-read
+               if ($type !== null) {   // otherwise re-read
                        $type = $this->type;
                        $trans_no = $this->trans_no;
-               }
-               if ($type == ST_BANKPAYMENT || $type == ST_BANKDEPOSIT) {
-                       $bank_trans = db_fetch(get_bank_trans($type, $trans_no));
-                       $this->person_type = $bank_trans['person_type_id'] == PT_SUPPLIER;
-               } else
-                       $this->person_type = $type == ST_SUPPCREDIT || $type == ST_SUPPAYMENT;
-               $this->allocs = array();
 
-               if ($trans_no) {
-                       $trans = $this->person_type ? get_supp_trans($trans_no, $type) 
-                               : get_customer_trans($trans_no, $type);
+                       if (isset($person_type_id))
+                       {
+                               $this->person_type = $person_type_id;
+                               $this->person_id = $person_id;
+                       } else { // guess person_type_id
+                               if (in_array($type, array(ST_BANKPAYMENT, ST_BANKDEPOSIT)))
+                               {
+                                       $bank_trans = db_fetch(get_bank_trans($type, $trans_no));
+                                       $this->person_type = $bank_trans['person_type_id'];
+                               } else
+                                       $this->person_type = in_array($type, array(ST_SUPPCREDIT, ST_SUPPAYMENT)) ? PT_SUPPLIER : PT_CUSTOMER;
+                       }
 
-                       $this->person_id = $trans[$this->person_type ? 'supplier_id':'debtor_no'];
-                       $this->person_name = $trans[$this->person_type ? "supplier_name":"DebtorName"];
-                       $this->amount = $trans["Total"];
-                       $this->date_ = sql2date($trans["tran_date"]);
-               } 
-               else {
-                       $this->person_id = get_post($this->person_type ? 'supplier_id':'customer_id');
-                       $this->date_ = get_post($this->person_type ? 'DatePaid':'DateBanked', Today());
+                       if ($trans_no) {
+                               $trans = $this->person_type == PT_SUPPLIER ? get_supp_trans($trans_no, $type, $person_id)
+                                       : get_customer_trans($trans_no, $type, $person_id);
+
+                               $this->person_id = $trans[$this->person_type == PT_SUPPLIER ? 'supplier_id':'debtor_no'];
+                               $this->person_name = $trans[$this->person_type == PT_SUPPLIER ? "supplier_name":"DebtorName"];
+                               $this->date_ = sql2date($trans["tran_date"]);
+                               $this->person_curr = $trans['curr_code'];
+                               $this->currency = isset($trans['bank_curr_code']) ? $trans['bank_curr_code'] : $trans['curr_code'];
+                               $this->bank_amount = @$trans["bank_amount"];
+                               $this->amount = $trans["Total"];
+                       }
                }
-
        /* Now populate the array of possible (and previous actual) allocations 
                for this customer/supplier. First get the transactions that have 
                outstanding balances ie Total-alloc >0 */
 
-               if ($this->person_type)
-                       $trans_items = get_allocatable_to_supp_transactions($this->person_id);
-               else
-                       $trans_items = get_allocatable_to_cust_transactions($this->person_id);
-
-               while ($myrow = db_fetch($trans_items))
+               $this->allocs = array();
+               if ($this->person_id)
                {
-                       $this->add_item($myrow["type"], $myrow["trans_no"],
-                               sql2date($myrow["tran_date"]),
-                               sql2date($myrow["due_date"]),
-                               $myrow["Total"], // trans total
-                               $myrow["alloc"], // trans total allocated
-                               0,
-                               $myrow["reference"]); // this allocation
+                       if ($this->person_type==PT_SUPPLIER)
+                               $trans_items = get_allocatable_to_supp_transactions($this->person_id);
+                       else
+                               $trans_items = get_allocatable_to_cust_transactions($this->person_id);
+                       while ($myrow = db_fetch($trans_items))
+                       {
+                               $this->add_item($myrow["type"], $myrow["trans_no"],
+                                       sql2date($myrow["tran_date"]),
+                                       sql2date($myrow["due_date"]),
+                                       $myrow["Total"], // trans total
+                                       $myrow["alloc"], // trans total allocated
+                                       0,
+                                       $myrow["reference"]); // this allocation
+                       }
                }
-               if ($trans_no == 0) return; // this is new payment
+               if ($this->trans_no == 0) return; // this is new payment
 
        /* Now get trans that might have previously been allocated to by this trans
        NB existing entries where still some of the trans outstanding entered from
        above logic will be overwritten with the prev alloc detail below */
 
-               if ($this->person_type)
+               if ($this->person_type==PT_SUPPLIER)
                        $trans_items = get_allocatable_to_supp_transactions($this->person_id, 
-                               $trans_no, $type);
+                               $this->trans_no, $this->type);
                else
                        $trans_items = get_allocatable_to_cust_transactions($this->person_id, 
-                               $trans_no, $type);
+                               $this->trans_no, $this->type);
 
                while ($myrow = db_fetch($trans_items))
                {
@@ -163,50 +185,47 @@ class allocation
        {
                begin_transaction();
 
-               if ($this->person_type)
+               if ($this->person_type == PT_SUPPLIER)
                        clear_supp_alloctions($this->type, $this->trans_no, $this->date_);
                else
                        clear_cust_alloctions($this->type, $this->trans_no, $this->date_);
 
                // now add the new allocations
                $total_allocated = 0;
+               $dec = user_price_dec();
                foreach ($this->allocs as $alloc_item)
                {
                        if ($alloc_item->current_allocated > 0)
                        {
-                               if ($this->person_type) {
-                                       add_supp_allocation($alloc_item->current_allocated,
+                               $amount = round($alloc_item->current_allocated, $dec);
+
+                               if ($this->person_type == PT_SUPPLIER) {
+                                       add_supp_allocation($amount,
                                                $this->type, $this->trans_no,
                                        $alloc_item->type, $alloc_item->type_no, $this->date_);
 
-                                       update_supp_trans_allocation($alloc_item->type, 
-                                               $alloc_item->type_no, $alloc_item->current_allocated);
+                                       update_supp_trans_allocation($alloc_item->type, $alloc_item->type_no);
                                } else {
-                                       add_cust_allocation($alloc_item->current_allocated,
+                                       add_cust_allocation($amount,
                                                $this->type, $this->trans_no,
                                        $alloc_item->type, $alloc_item->type_no, $this->date_);
-                       
-                                       update_debtor_trans_allocation($alloc_item->type, 
-                                               $alloc_item->type_no, $alloc_item->current_allocated);
+
+                                       update_debtor_trans_allocation($alloc_item->type, $alloc_item->type_no);
                                }
                                // Exchange Variations Joe Hunt 2008-09-20 ////////////////////
                                exchange_variation($this->type, $this->trans_no,
                                        $alloc_item->type, $alloc_item->type_no, $this->date_,
-                                       $alloc_item->current_allocated,
-                                       $this->person_type ? PT_SUPPLIER : PT_CUSTOMER);
-                               
+                                       $amount, $this->person_type);
 
                                //////////////////////////////////////////////////////////////
                                $total_allocated += $alloc_item->current_allocated;
                        }
 
                }  /*end of the loop through the array of allocations made */
-               if ($this->person_type)
-                       update_supp_trans_allocation($this->type, $this->trans_no, 
-                               $total_allocated);
+               if ($this->person_type == PT_SUPPLIER)
+                       update_supp_trans_allocation($this->type, $this->trans_no);
                else
-                       update_debtor_trans_allocation($this->type,     $this->trans_no, 
-                               $total_allocated);
+                       update_debtor_trans_allocation($this->type,     $this->trans_no);
 
                commit_transaction();
 
@@ -257,50 +276,61 @@ function show_allocatable($show_totals) {
        
     $k = $counter = $total_allocated = 0;
 
-       if (count($_SESSION['alloc']->allocs)) 
+       $cart = $_SESSION['alloc'];
+       $supp_ref = in_array($cart->type, array(ST_SUPPCREDIT, ST_SUPPAYMENT, ST_BANKPAYMENT));
+
+       if (count($cart->allocs)) 
        {
+               if ($cart->currency != $cart->person_curr)
+                       display_heading(sprintf(_("Allocated amounts in %s:"), $cart->person_curr));
                start_table(TABLESTYLE, "width=60%");
-               $th = array(_("Transaction Type"), _("#"), _("Ref"), _("Date"), _("Due Date"), _("Amount"),
-                       _("Other Allocations"), _("This Allocation"), _("Left to Allocate"),'','');
+               $th = array(_("Transaction Type"), _("#"), $supp_ref ? _("Supplier Ref"): _("Ref"), _("Date"), _("Due Date"), _("Amount"),
+                       _("Other Allocations"), _("Left to Allocate"), _("This Allocation"),'','');
+
                table_header($th);
 
-               foreach ($_SESSION['alloc']->allocs as $alloc_item)
-           {
-                       alt_table_row_color($k);
-               label_cell($systypes_array[$alloc_item->type]);
-                       label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no));
-                       label_cell($alloc_item->ref);
-               label_cell($alloc_item->date_, "align=right");
-               label_cell($alloc_item->due_date, "align=right");
-               amount_cell($alloc_item->amount);
-                       amount_cell($alloc_item->amount_allocated);
-
-               $_POST['amount' . $counter] = price_format($alloc_item->current_allocated);
-               amount_cells(null, "amount" . $counter, price_format('amount' . $counter));
-
-               $un_allocated = round($alloc_item->amount - $alloc_item->amount_allocated, 6);
-               amount_cell($un_allocated, false,'', 'maxval'.$counter);
-                       label_cell("<a href='#' name=Alloc$counter onclick='allocate_all(this.name.substr(5));return true;'>"
-                                . _("All") . "</a>");
-                       label_cell("<a href='#' name=DeAll$counter onclick='allocate_none(this.name.substr(5));return true;'>"
-                                . _("None") . "</a>".hidden("un_allocated" . $counter, 
-                                price_format($un_allocated), false));
-                       end_row();
-
-               $total_allocated += input_num('amount' . $counter);
-                   $counter++;
-               }
+               foreach ($cart->allocs as $id => $alloc_item)
+               {
+                   if (floatcmp(abs($alloc_item->amount), $alloc_item->amount_allocated))
+                   {
+                               alt_table_row_color($k);
+                       label_cell($systypes_array[$alloc_item->type]);
+                               label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no));
+                               label_cell($alloc_item->ref);
+                       label_cell($alloc_item->date_, "align=right");
+                       label_cell($alloc_item->due_date, "align=right");
+                       amount_cell(abs($alloc_item->amount));
+                               amount_cell($alloc_item->amount_allocated);
+
+                       $_POST['amount' . $id] = price_format($alloc_item->current_allocated);
+
+                       $un_allocated = round((abs($alloc_item->amount) - $alloc_item->amount_allocated), 6);
+                       amount_cell($un_allocated, false,'', 'maxval'.$id);
+                       amount_cells(null, "amount" . $id);//, input_num('amount' . $id));
+                               label_cell("<a href='#' name=Alloc$id onclick='allocate_all(this.name.substr(5));return true;'>"
+                                        . _("All") . "</a>");
+                               label_cell("<a href='#' name=DeAll$id onclick='allocate_none(this.name.substr(5));return true;'>"
+                                        . _("None") . "</a>".hidden("un_allocated" . $id, 
+                                        price_format($un_allocated), false));
+                               end_row();
+
+                       $total_allocated += input_num('amount' . $id);
+                       }
+               }
                if ($show_totals) {
                label_row(_("Total Allocated"), price_format($total_allocated),
-                       "colspan=6 align=right", "align=right id='total_allocated'", 3);
+                       "colspan=8 align=right", "align=right id='total_allocated'", 3);
+/*
                        $amount = $_SESSION['alloc']->amount;
 
                        if ($_SESSION['alloc']->type == ST_SUPPCREDIT
                                || $_SESSION['alloc']->type == ST_SUPPAYMENT
                                ||  $_SESSION['alloc']->type == ST_BANKPAYMENT)
                                $amount = -$amount;
-                       
-                       if ($amount - $total_allocated < 0)
+*/
+                       $amount = abs($cart->amount);
+
+                       if (floatcmp($amount, $total_allocated) < 0)
                {
                        $font1 = "<font color=red>";
                        $font2 = "</font>";
@@ -309,12 +339,12 @@ function show_allocatable($show_totals) {
                        $font1 = $font2 = "";
                        $left_to_allocate = price_format($amount - $total_allocated);
                label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2, 
-                               "colspan=6 align=right", "nowrap align=right id='left_to_allocate'",
+                               "colspan=8 align=right", "nowrap align=right id='left_to_allocate'",
                                 3);
                }
                end_table(1);
        }
-       hidden('TotalNumberOfAllocs', $counter);
+       hidden('TotalNumberOfAllocs', count($cart->allocs));
 }
 //--------------------------------------------------------------------------------
 
@@ -324,8 +354,9 @@ function check_allocations()
 
        $total_allocated = 0;
 
-       for ($counter = 0; $counter < $_POST["TotalNumberOfAllocs"]; $counter++)
+       for ($counter = 0; $counter < get_post("TotalNumberOfAllocs"); $counter++)
        {
+               if (!isset($_POST['amount'.$counter])) continue;
                if (!check_num('amount' . $counter, 0))
                {
                        display_error(_("The entry for one or more amounts is invalid or negative."));
@@ -333,9 +364,11 @@ function check_allocations()
                        return false;
                 }
 
-                 /*Now check to see that the AllocAmt is no greater than the
-                amount left to be allocated against the transaction under review */
-                if (input_num('amount' . $counter) > input_num('un_allocated' . $counter))
+                 /* Now check to see that the AllocAmt is no greater than the
+                amount left to be allocated against the transaction under review;
+                skip check if no allocation is set to avoid deadlock on mistakenly overallocated transactions*/
+                $allocated = input_num('amount' . $counter);
+                if ($allocated && ($allocated > input_num('un_allocated' . $counter)))
                 {
                        display_error(_("At least one transaction is overallocated."));
                        set_focus('amount'.$counter);
@@ -346,12 +379,13 @@ function check_allocations()
 
                 $total_allocated += input_num('amount' . $counter);
        }
-
+/*
        $amount = $_SESSION['alloc']->amount;
-       
 
        if (in_array($_SESSION['alloc']->type, array(ST_BANKPAYMENT, ST_SUPPCREDIT, ST_SUPPAYMENT)))
                $amount = -$amount;
+*/
+       $amount = abs($_SESSION['alloc']->amount);
 
        if ($total_allocated - ($amount + input_num('discount'))  > $SysPrefs->allocation_settled_allowance())
        {
@@ -361,5 +395,3 @@ function check_allocations()
 
        return true;
 }
-
-?>
index a501d0b34a23eb8b0ba248a867df4ac13786659c..a12494067783efe391fafe93f3ba8ec5fdfbf02f 100644 (file)
@@ -37,6 +37,7 @@ function clear_allocations()
                unset($_SESSION['alloc']->allocs);
                unset($_SESSION['alloc']);
        }
+       //session_register("alloc");
 }
 //--------------------------------------------------------------------------------
 
@@ -46,17 +47,26 @@ function edit_allocations_for_transaction($type, $trans_no)
 
        start_form();
 
-    display_heading(_("Allocation of") . " " . $systypes_array[$_SESSION['alloc']->type] . " # " . $_SESSION['alloc']->trans_no);
+       $cart = $_SESSION['alloc'];
 
-       display_heading($_SESSION['alloc']->person_name);
+    display_heading(_("Allocation of") . " " . $systypes_array[$cart->type] . " # " . $cart->trans_no);
 
-    display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
-    display_heading2(_("Total:") . " <b>" . price_format(-$_SESSION['alloc']->amount) . "</b>");
+       display_heading($cart->person_name);
 
+    display_heading2(_("Date:") . " <b>" . $cart->date_ . "</b>");
+
+       display_heading2(_("Total:"). " <b>" . price_format(-$cart->bank_amount).' '.$cart->currency."</b>");
+
+       if ($cart->currency != $cart->person_curr)
+       {
+           $total = _("Total in clearing currency:") . " <b>" . price_format(-$cart->amount)."</b>"
+               . sprintf(" %s (%s %s/%s)", $cart->person_curr, exrate_format($cart->bank_amount/$cart->amount), $cart->currency, $cart->person_curr);
+       display_heading2($total);
+       }
     echo "<br>";
 
        div_start('alloc_tbl');
-    if (count($_SESSION['alloc']->allocs) > 0)
+    if (count($cart->allocs) > 0)
     {
                show_allocatable(true);
 
@@ -99,7 +109,7 @@ if (isset($_POST['Cancel']))
 
 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
 {
-       $_SESSION['alloc'] = new allocation($_GET['trans_type'], $_GET['trans_no']);
+       $_SESSION['alloc'] = new allocation($_GET['trans_type'], $_GET['trans_no'], @$_GET['supplier_id'], PT_SUPPLIER);
 }
 
 if (get_post('UpdateDisplay'))
index e17897e754741fb1965968eb7ecc275ab023ef04..ddbdc9e7d9808be9db0d55428de84c6c90bdbf22 100644 (file)
@@ -53,13 +53,13 @@ function write_supp_trans($type, $trans_no, $supplier_id, $date_, $due_date, $re
 function get_supp_trans($trans_no, $trans_type=-1)
 {
        $sql = "SELECT ".TB_PREF."supp_trans.*, (".TB_PREF."supp_trans.ov_amount+".TB_PREF."supp_trans.ov_gst+".TB_PREF."supp_trans.ov_discount) AS Total,
-               ".TB_PREF."suppliers.supp_name AS supplier_name, ".TB_PREF."suppliers.curr_code AS SupplierCurrCode ";
+               ".TB_PREF."suppliers.supp_name AS supplier_name, ".TB_PREF."suppliers.curr_code AS curr_code ";
 
        if ($trans_type == ST_SUPPAYMENT)
        {
                // it's a payment so also get the bank account
                $sql .= ", ".TB_PREF."bank_accounts.bank_name, ".TB_PREF."bank_accounts.bank_account_name, ".TB_PREF."bank_accounts.bank_curr_code,
-                       ".TB_PREF."bank_accounts.account_type AS BankTransType, ".TB_PREF."bank_trans.amount AS BankAmount,
+                       ".TB_PREF."bank_accounts.account_type AS BankTransType, ".TB_PREF."bank_trans.amount AS bank_amount,
                        ".TB_PREF."bank_trans.ref ";
        }
 
index 535e66c5a0208a3b79a44cc50e0447da8e51c1c3..f54bd3ca27d73e13eae7c8aeecfb41ca3d78d70a 100644 (file)
@@ -48,14 +48,24 @@ function get_supp_trans_allocation_balance($trans_type, $trans_no)
 }
 
 //----------------------------------------------------------------------------------------
-
-function update_supp_trans_allocation($trans_type, $trans_no, $alloc)
+//     Update supplier trans alloc field according to current status of supp_allocations
+//
+function update_supp_trans_allocation($trans_type, $trans_no)
 {
-       $sql = "UPDATE ".TB_PREF."supp_trans SET alloc = alloc + ".db_escape($alloc)."
-               WHERE type=".db_escape($trans_type)." AND trans_no = ".db_escape($trans_no);
+       $sql = "UPDATE `".TB_PREF.($trans_type==ST_PURCHORDER ? 'purch_orders' : 'supp_trans')."` trans,
+                       (SELECT sum(amt) amt from ".TB_PREF."supp_allocations
+                               WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no).")
+                               OR (trans_type_from=".db_escape($trans_type)." AND trans_no_from=".db_escape($trans_no).")) allocated
+               SET 
+                       trans.alloc=IFNULL(allocated.amt, 0)
+               WHERE " . ($trans_type==ST_PURCHORDER ? 
+                         "trans.order_no=".db_escape($trans_no)
+                       : "trans.type=".db_escape($trans_type)." AND trans.trans_no=".db_escape($trans_no));
+
        db_query($sql, "The supp transaction record could not be modified for the allocation against it");
 }
 
+
 //-------------------------------------------------------------------------------------------------------------
 
 function void_supp_allocations($type, $type_no, $date="")
index aae151d3d6120d0bda6738c838cc34e315653293..ff9c84680d428b9ffdd25f1281022cec57d87265 100644 (file)
@@ -82,7 +82,7 @@ function invoice_header(&$supp_trans)
        {
                $trans = get_supp_trans($_POST['invoice_no'], ST_SUPPINVOICE);
                $_POST['supplier_id'] = $trans['supplier_id'];
-               $supp = $trans['supplier_name'] . " - " . $trans['SupplierCurrCode'];
+               $supp = $trans['supplier_name'] . " - " . $trans['curr_code'];
                label_row(_("Supplier:"), $supp.hidden('supplier_id', $_POST['supplier_id'], false));
        }       
        else
index 17bf983bf87247d39273b2a18d651e14f5bcf202..1d339306e18f3f90cd3ce547f30700422c69f9d3 100644 (file)
@@ -96,9 +96,9 @@ function fmt_balance($row)
 function alloc_link($row)
 {
        if (($row["type"] == ST_BANKPAYMENT || $row["type"] == ST_SUPPCREDIT || $row["type"] == ST_SUPPAYMENT) 
-               && (-$row["TotalAmount"] - $row["Allocated"]) > 0)
+               && (-$row["TotalAmount"] - $row["Allocated"]) >= 0)
                        return  pager_link(_("Allocations"), "/purchasing/allocations/supplier_allocate.php?trans_no=" .
-                               $row["trans_no"]. "&trans_type=" . $row["type"], ICON_MONEY );
+                               $row["trans_no"]. "&trans_type=" . $row["type"]. "&supplier_id=" . $row["supplier_id"], ICON_MONEY );
        elseif ($row["type"] == ST_SUPPINVOICE && ($row["TotalAmount"] - $row["Allocated"]) > 0)
                        return  pager_link(_("Pay"), "/purchasing/supplier_payment.php?supplier_id=".$row["supplier_id"]
                                ."&PInvoice=".$row["trans_no"], ICON_MONEY );
index 393a3d8914458f7e6e45bb34847e3b7b9d693623..8096c2f4e83cb2c3e0618ab912aaa633b9bb42ee 100644 (file)
@@ -57,10 +57,17 @@ if (isset($_POST['_DatePaid_changed'])) {
   $Ajax->activate('_ex_rate');
 }
 
+if (list_updated('supplier_id')) {
+       $_POST['amount'] = price_format(0);
+       $_SESSION['alloc']->person_id = get_post('supplier_id');
+       $Ajax->activate('amount');
+} elseif (list_updated('bank_account'))
+       $Ajax->activate('alloc_tbl');
+
 //----------------------------------------------------------------------------------------
 
 if (!isset($_POST['bank_account'])) { // first page call
-         $_SESSION['alloc'] = new allocation(ST_SUPPAYMENT, 0);
+       $_SESSION['alloc'] = new allocation(ST_SUPPAYMENT, 0, get_post('supplier_id'));
 
        if (isset($_GET['PInvoice'])) {
                //  get date and supplier
@@ -68,15 +75,12 @@ if (!isset($_POST['bank_account'])) { // first page call
                if($inv) {
                        $_POST['supplier_id'] = $inv['supplier_id'];
                        $_POST['DatePaid'] = sql2date($inv['tran_date']);
-//                     $_POST['discount'] = price_format(0);
-//             $_POST['bank_account'], $_POST['ref']
                        $_POST['memo_'] = $inv['supp_reference'];
                        foreach($_SESSION['alloc']->allocs as $line => $trans) {
                                if ($trans->type == ST_SUPPINVOICE && $trans->type_no == $_GET['PInvoice']) {
-                                       $_POST['amount'] =
-                                               $_SESSION['alloc']->amount = price_format($_SESSION['alloc']->allocs[$line]->amount);
-                                       $_SESSION['alloc']->allocs[$line]->current_allocated =
-                                               $_SESSION['alloc']->allocs[$line]->amount;
+                                       $un_allocated = abs($trans->amount) - $trans->amount_allocated;
+                                       $_POST['amount'] = $_SESSION['alloc']->amount =
+                                               $_SESSION['alloc']->allocs[$line]->current_allocated = price_format($un_allocated);
                                        break;
                                }
                        }
@@ -93,12 +97,14 @@ if (isset($_GET['AddedID'])) {
        submenu_print(_("&Print This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, 'prtopt');
        submenu_print(_("&Email This Remittance"), ST_SUPPAYMENT, $payment_id."-".ST_SUPPAYMENT, null, 1);
 
-    display_note(get_gl_view_str(ST_SUPPAYMENT, $payment_id, _("View the GL &Journal Entries for this Payment")));
-
-       hyperlink_no_params($path_to_root . "/purchasing/inquiry/supplier_allocation_inquiry.php?supplier_id=", _("Select Another &Supplier Transaction for Payment"));
-//    hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("&Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
+       submenu_view(_("View this Payment"), ST_SUPPAYMENT, $payment_id);
+    display_note(get_gl_view_str(ST_SUPPAYMENT, $payment_id, _("View the GL &Journal Entries for this Payment")), 0, 1);
 
-       hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Supplier &Payment"), "supplier_id=" . $_POST['supplier_id']);
+       submenu_option(_("Enter another supplier &payment"), "/purchasing/supplier_payment.php?supplier_id=".$_POST['supplier_id']);
+       submenu_option(_("Enter Other &Payment"), "/gl/gl_bank.php?NewPayment=Yes");
+       submenu_option(_("Enter &Customer Payment"), "/sales/customer_payments.php");
+       submenu_option(_("Enter Other &Deposit"), "/gl/gl_bank.php?NewDeposit=Yes");
+       submenu_option(_("Bank Account &Transfer"), "/gl/bank_transfer.php");
 
        display_footer_exit();
 }
@@ -116,7 +122,7 @@ function check_inputs()
                return false;
        } 
        
-       if ($_POST['amount'] == "") 
+       if (@$_POST['amount'] == "") 
        {
                $_POST['amount'] = price_format(0);
        }
@@ -143,7 +149,7 @@ function check_inputs()
                }       
        }
 
-       if ($_POST['discount'] == "") 
+       if (@$_POST['discount'] == "") 
        {
                $_POST['discount'] = 0;
        }
@@ -155,6 +161,7 @@ function check_inputs()
                return false;
        }
 
+       //if (input_num('amount') - input_num('discount') <= 0) 
        if (input_num('amount') <= 0) 
        {
                display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
@@ -178,14 +185,14 @@ function check_inputs()
        } 
        elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
        {
-               display_error(_("The entered date is not in fiscal year."));
+               display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
                set_focus('DatePaid');
                return false;
        }
 
        $limit = get_bank_account_limit($_POST['bank_account'], $_POST['DatePaid']);
 
-       if ($limit !== null && floatcmp($limit, input_num('amount') < 0))
+       if (($limit !== null) && (floatcmp($limit, input_num('amount')) < 0))
        {
                display_error(sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit)));
                set_focus('amount');
@@ -208,6 +215,7 @@ function check_inputs()
 
        if (!db_has_currency_rates(get_supplier_currency($_POST['supplier_id']), $_POST['DatePaid'], true))
                return false;
+
        $_SESSION['alloc']->amount = -input_num('amount');
 
        if (isset($_POST["TotalNumberOfAllocs"]))
@@ -270,20 +278,29 @@ start_form();
 
        set_global_supplier($_POST['supplier_id']);
 
+       if (!list_updated('bank_account') && !get_post('__ex_rate_changed'))
+               $_POST['bank_account'] = get_default_supplier_bank_account($_POST['supplier_id']);
+       else
+               $_POST['amount'] = price_format(0);
+
     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
 
        bank_balance_row($_POST['bank_account']);
 
        table_section(2);
 
-    ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT));
-
     date_row(_("Date Paid") . ":", 'DatePaid', '', true, 0, 0, 0, null, true);
 
+    ref_row(_("Reference:"), 'ref', '', $Refs->get_next(ST_SUPPAYMENT));
+
        table_section(3);
 
-       $supplier_currency = get_supplier_currency($_POST['supplier_id']);
-       $bank_currency = get_bank_account_currency($_POST['bank_account']);
+       $comp_currency = get_company_currency();
+       $supplier_currency = $_SESSION['alloc']->set_person($_POST['supplier_id'], PT_SUPPLIER);
+       if (!$supplier_currency)
+                       $supplier_currency = $comp_currency;
+       $_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);
+
        if ($bank_currency != $supplier_currency) 
        {
                amount_row("Bank Amount:", 'bank_amount', null, '', $bank_currency, 2);
@@ -295,7 +312,6 @@ start_form();
        end_outer_table(1);
 
        div_start('alloc_tbl');
-       display_heading(sprintf(_("Accounts Payable settled in %s:"), $supplier_currency));
        show_allocatable(false);
        div_end();
 
index d6786f5538e075faafdcf226edf2e79f99094085..a23e8d713574a4a90b726305bf3f0fb1a98ae5b0 100644 (file)
@@ -33,10 +33,10 @@ $company_currency = get_company_currency();
 $show_currencies = false;
 $show_both_amounts = false;
 
-if (($receipt['bank_curr_code'] != $company_currency) || ($receipt['SupplierCurrCode'] != $company_currency))
+if (($receipt['bank_curr_code'] != $company_currency) || ($receipt['curr_code'] != $company_currency))
        $show_currencies = true;
 
-if ($receipt['bank_curr_code'] != $receipt['SupplierCurrCode']) 
+if ($receipt['bank_curr_code'] != $receipt['curr_code']) 
 {
        $show_currencies = true;
        $show_both_amounts = true;
@@ -57,7 +57,7 @@ end_row();
 start_row();
 if ($show_currencies)
        label_cells(_("Payment Currency"), $receipt['bank_curr_code'], "class='tableheader2'");
-label_cells(_("Amount"), number_format2(-$receipt['BankAmount'], user_price_dec()), "class='tableheader2'");
+label_cells(_("Amount"), number_format2(-$receipt['bank_amount'], user_price_dec()), "class='tableheader2'");
 if ($receipt['ov_discount'] != 0)
        label_cells(_("Discount"), number_format2(-$receipt['ov_discount']*$receipt['rate'], user_price_dec()), "class='tableheader2'");
 else
@@ -66,7 +66,7 @@ end_row();
 start_row();
 if ($show_currencies) 
 {
-       label_cells(_("Supplier's Currency"), $receipt['SupplierCurrCode'], "class='tableheader2'");
+       label_cells(_("Supplier's Currency"), $receipt['curr_code'], "class='tableheader2'");
 }
 if ($show_both_amounts)
        label_cells(_("Amount"), number_format2(-$receipt['Total'], user_price_dec()), "class='tableheader2'");
index 7f7fe7b1b31c030173da3e8a1089e7b5a9c53c19..cf0005fe04e7d9e5c0f5ea122c4a94bfa06bc8c9 100644 (file)
@@ -36,6 +36,7 @@ function clear_allocations()
                unset($_SESSION['alloc']->allocs);
                unset($_SESSION['alloc']);
        }
+       //session_register('alloc');
 }
 
 //--------------------------------------------------------------------------------
@@ -44,19 +45,27 @@ function edit_allocations_for_transaction($type, $trans_no)
 {
        global $systypes_array;
 
-    display_heading(sprintf(_("Allocation of %s # %d"), $systypes_array[$_SESSION['alloc']->type],
-       $_SESSION['alloc']->trans_no));
+       $cart = $_SESSION['alloc'];
 
-    display_heading($_SESSION['alloc']->person_name);
+    display_heading(sprintf(_("Allocation of %s # %d"), $systypes_array[$cart->type], $cart->trans_no));
 
-    display_heading2(_("Date:") . " <b>" . $_SESSION['alloc']->date_ . "</b>");
-    display_heading2(_("Total:") . " <b>" . price_format($_SESSION['alloc']->amount) . "</b>");
+    display_heading($cart->person_name);
+
+    display_heading2(_("Date:") . " <b>" . $cart->date_ . "</b>");
+       display_heading2(_("Total:"). " <b>" . price_format($cart->bank_amount).' '.$cart->currency."</b>");
+
+       if ($cart->currency != $cart->person_curr)
+       {
+           $total = _("Total in clearing currency:") . " <b>" . price_format($cart->amount)."</b>"
+               . sprintf(" %s (%s %s/%s)", $cart->person_curr, exrate_format($cart->bank_amount/$cart->amount), $cart->currency, $cart->person_curr);
+       display_heading2($total);
+       }
 
     echo "<br>";
 
        start_form();
        div_start('alloc_tbl');
-    if (count($_SESSION['alloc']->allocs) > 0)
+    if (count($cart->allocs) > 0)
     {
                show_allocatable(true);
                submit_center_first('UpdateDisplay', _("Refresh"), _('Start again allocation of selected amount'), true);
@@ -98,7 +107,7 @@ if (isset($_POST['Cancel']))
 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
 {
        clear_allocations();
-       $_SESSION['alloc'] = new allocation($_GET['trans_type'], $_GET['trans_no']);
+       $_SESSION['alloc'] = new allocation($_GET['trans_type'], $_GET['trans_no'], @$_GET['debtor_no'], PT_CUSTOMER);
 }
 
 if(get_post('UpdateDisplay'))
index ef40147e2a8490753961653b0d1d4e1ab9e15b3b..2bd169ef7dfa3c5f62e27e630a60bd7550411c98 100644 (file)
@@ -73,7 +73,7 @@ function alloc_link($row)
 {
        return pager_link(_("Allocate"),
                "/sales/allocations/customer_allocate.php?trans_no="
-                       .$row["trans_no"] . "&trans_type=" . $row["type"], ICON_MONEY);
+                       .$row["trans_no"] . "&trans_type=" . $row["type"]. "&debtor_no=" . $row["debtor_no"], ICON_MONEY);
 }
 
 function amount_left($row)
index 4e707158c7c580d7181248f0814bfefeaf44b4fc..1f60e058068e745caccce1e6099f48a0690e033f 100644 (file)
@@ -18,7 +18,6 @@ include_once($path_to_root . "/includes/ui.inc");
 include_once($path_to_root . "/includes/banking.inc");
 include_once($path_to_root . "/includes/data_checks.inc");
 include_once($path_to_root . "/sales/includes/sales_db.inc");
-//include_once($path_to_root . "/sales/includes/ui/cust_alloc_ui.inc");
 include_once($path_to_root . "/reporting/includes/reporting.inc");
 
 $js = "";
@@ -44,9 +43,8 @@ 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);
+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
@@ -56,10 +54,13 @@ if (!isset($_POST['bank_account']))
                        $_POST['DateBanked'] = sql2date($inv['tran_date']);
                        foreach($_SESSION['alloc']->allocs as $line => $trans) {
                                if ($trans->type == ST_SALESINVOICE && $trans->type_no == $_GET['SInvoice']) {
-                                       $_POST['amount'] =
-                                               $_SESSION['alloc']->amount = price_format($_SESSION['alloc']->allocs[$line]->amount);
+                                       $un_allocated = $trans->amount - $trans->amount_allocated;
+                                       if($un_allocated){
+                                       $_POST['amount'] = $_SESSION['alloc']->amount = 
+//                                             price_format($trans->amount);
                                        $_SESSION['alloc']->allocs[$line]->current_allocated =
-                                               $_SESSION['alloc']->allocs[$line]->amount;
+//                                             $trans->amount;
+                                               price_format($un_allocated);}
                                        break;
                                }
                        }
@@ -73,6 +74,7 @@ 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');
 }
 
@@ -95,13 +97,15 @@ if (isset($_GET['AddedID'])) {
 
        submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');
 
-       display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));
-
-//     hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("&Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");
+       submenu_view(_("&View this Customer Payment"), ST_CUSTPAYMENT, $payment_no);
 
-       hyperlink_no_params($path_to_root . "/sales/inquiry/customer_allocation_inquiry.php?customer_id=", _("Select Another &Customer Transaction for Payment"));
+       submenu_option(_("Enter Another &Customer Payment"), "/sales/customer_payments.php");
+       submenu_option(_("Enter Other &Deposit"), "/gl/gl_bank.php?NewDeposit=Yes");
+       submenu_option(_("Enter Payment to &Supplier"), "/purchasing/supplier_payment.php");
+       submenu_option(_("Enter Other &Payment"), "/gl/gl_bank.php?NewPayment=Yes");
+       submenu_option(_("Bank Account &Transfer"), "/gl/bank_transfer.php");
 
-       hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another &Customer Payment"));
+       display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));
 
        display_footer_exit();
 }
@@ -136,7 +140,7 @@ function can_process()
                return false;
        } 
        
-       if (!get_post('BranchID')) 
+       if (!get_post('BranchID'))
        {
                display_error(_("This customer has no branch defined."));
                set_focus('BranchID');
@@ -200,7 +204,7 @@ function can_process()
 //             return false;
 //     }
 
-       if ($_POST['discount'] == "") 
+       if (@$_POST['discount'] == "") 
        {
                $_POST['discount'] = 0;
        }
@@ -343,6 +347,7 @@ start_form();
        }
 
        if (list_updated('customer_id') || ($new && list_updated('bank_account'))) {
+               $_SESSION['alloc']->set_person($_POST['customer_id'], PT_CUSTOMER);
                $_SESSION['alloc']->read();
                $_POST['memo_'] = $_POST['amount'] = $_POST['discount'] = '';
                $Ajax->activate('alloc_tbl');
@@ -359,30 +364,25 @@ start_form();
 
        date_row(_("Date of Deposit:"), 'DateBanked', '', true, 0, 0, 0, null, true);
 
-       text_row(_("Reference:"), 'ref', null, 20, 40);
+       ref_row(_("Reference:"), 'ref','' , null, '', ST_CUSTPAYMENT);
 
        table_section(3);
 
        $comp_currency = get_company_currency();
-       $cust_currency = get_customer_currency($_POST['customer_id']);
-       $bank_currency = get_bank_account_currency($_POST['bank_account']);
-
-//     if ($cust_currency != $bank_currency) {
-//             exchange_rate_display($bank_currency, $cust_currency, $_POST['DateBanked'], ($bank_currency == $comp_currency));
-//     }
+       $cust_currency = $_SESSION['alloc']->set_person($_POST['customer_id'], PT_CUSTOMER);
+       if (!$cust_currency)
+               $cust_currency = $comp_currency;
+       $_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);
 
        if ($cust_currency != $bank_currency)
        {
                amount_row(_("Payment Amount:"), 'bank_amount', null, '', $bank_currency);
-               // aproximate value in customer currency:
-//             label_row(_("Current value:"), price_format(input_num('bank_amount')*$rate).' '.$cust_currency);
        }
 
        amount_row(_("Bank Charge:"), 'charge', null, '', $bank_currency);
 
        end_outer_table(1);
 
-       display_heading(sprintf(_("Accounts Receivable settled in %s:"), $cust_currency));
        div_start('alloc_tbl');
        show_allocatable(false);
        div_end();
index 79aa50ab3555eb47ed2d19ec7001912074b02196..0b9663507e0d5c9d559ae56ddcb79d9f73b1f11d 100644 (file)
@@ -116,6 +116,8 @@ function write_customer_trans($trans_type, $trans_no, $debtor_no, $BranchNo,
 
 function get_customer_trans($trans_id, $trans_type)
 {
+       global $go_debug;
+
        $sql = "SELECT trans.*,"
                ."ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,"
                ."cust.name AS DebtorName, cust.address, "
@@ -143,7 +145,8 @@ function get_customer_trans($trans_id, $trans_type)
                .TB_PREF."tax_groups.id AS tax_group_id ";
        }
 
-       $sql .= " FROM ".TB_PREF."debtor_trans trans LEFT JOIN ".TB_PREF."comments com ON trans.type=com.type AND trans.trans_no=com.id
+       $sql .= " FROM ".TB_PREF."debtor_trans trans
+                                       LEFT JOIN ".TB_PREF."comments com ON trans.type=com.type AND trans.trans_no=com.id
                                        LEFT JOIN ".TB_PREF."shippers ON ".TB_PREF."shippers.shipper_id=trans.ship_via, 
                                        ".TB_PREF."debtors_master cust";
 
@@ -181,12 +184,15 @@ function get_customer_trans($trans_id, $trans_type)
 
        if (db_num_rows($result) == 0) {
                // can't return nothing
+               if($go_debug)
+                       display_backtrace();
                display_db_error("no debtor trans found for given params", $sql, true);
                exit;
        }
-
        if (db_num_rows($result) > 1) {
                // can't return multiple
+               if($go_debug)
+                       display_backtrace();
                display_db_error("duplicate debtor transactions found for given params", $sql, true);
                exit;
        }
index 7260f0e10ed0702b19ab0c4e0dd8648cd8eb9693..b7c4607383ce421335ef3d33b32631b1dc7c9c46 100644 (file)
@@ -33,11 +33,18 @@ function delete_cust_allocation($trans_id)
 }
 
 //----------------------------------------------------------------------------------------
-
-function update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
+//     Update debtor trans alloc field according to current status of cust_allocations
+//
+function update_debtor_trans_allocation($trans_type, $trans_no)
 {
-       $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc = alloc + $alloc
-               WHERE type=".db_escape($trans_type)." AND trans_no = ".db_escape($trans_no);
+       $sql = "UPDATE `".TB_PREF."debtor_trans` trans,
+                       (SELECT sum(amt) amt from ".TB_PREF."cust_allocations
+                               WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no).")
+                               OR (trans_type_from=".db_escape($trans_type)." AND trans_no_from=".db_escape($trans_no).")) allocated
+               SET 
+                       trans.alloc=IFNULL(allocated.amt,0)
+               WHERE trans.type=".db_escape($trans_type)." AND trans_no=".db_escape($trans_no);
+
        db_query($sql, "The debtor transaction record could not be modified for the allocation against it");
 }
 
@@ -94,7 +101,8 @@ function get_alloc_trans_sql($extra_fields=null, $extra_conditions=null, $extra_
                trans.alloc,
                trans.due_date,
                debtor.address,
-               trans.version ";
+               trans.version,
+               trans.debtor_no ";
 
        if ($extra_fields)
                $sql .= ", $extra_fields ";
@@ -184,10 +192,10 @@ function get_allocatable_from_cust_transactions($customer_id, $trans_no=null, $t
        return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
 }
 
-function get_sql_for_customer_allocation_inquiry()
+function get_sql_for_customer_allocation_inquiry($from, $to, $customer, $filterType, $settled)
 {
-       $data_after = date2sql($_POST['TransAfterDate']);
-       $date_to = date2sql($_POST['TransToDate']);
+       $data_after = date2sql($from);
+       $date_to = date2sql($to);
 
        $sql = "SELECT 
                trans.type,
@@ -213,25 +221,25 @@ function get_sql_for_customer_allocation_inquiry()
                AND trans.tran_date >= '$data_after'
                AND trans.tran_date <= '$date_to'";
 
-       if ($_POST['customer_id'] != ALL_TEXT)
-               $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
+       if ($customer != ALL_TEXT)
+               $sql .= " AND trans.debtor_no = ".db_escape($customer);
 
-       if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
+       if (isset($filterType) && $filterType != ALL_TEXT)
        {
-               if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
+               if ($filterType == '1' || $filterType == '2')
                {
                        $sql .= " AND trans.type = ".ST_SALESINVOICE." ";
                }
-               elseif ($_POST['filterType'] == '3')
+               elseif ($filterType == '3')
                {
                        $sql .= " AND trans.type = " . ST_CUSTPAYMENT;
                }
-               elseif ($_POST['filterType'] == '4')
+               elseif ($filterType == '4')
                {
                        $sql .= " AND trans.type = ".ST_CUSTCREDIT." ";
                }
 
-       if ($_POST['filterType'] == '2')
+       if ($filterType == '2')
        {
                $today =  date2sql(Today());
                $sql .= " AND trans.due_date < '$today'
@@ -246,12 +254,13 @@ function get_sql_for_customer_allocation_inquiry()
        }
 
 
-       if (!check_value('showSettled'))
+       if (!$settled)
        {
                $sql .= " AND (round(abs(trans.ov_amount + trans.ov_gst + "
                ."trans.ov_freight + trans.ov_freight_tax + "
                ."trans.ov_discount) - trans.alloc,6) != 0) ";
        }
+
        return $sql;
 }
 
index 66bbb58c5ec69dcffbf8046f2486468a10e868f5..b49bb1231a7ba078ca54b2919610fe5906ff06ee 100644 (file)
@@ -95,7 +95,7 @@ function alloc_link($row)
        $link = 
        pager_link(_("Allocation"),
                "/sales/allocations/customer_allocate.php?trans_no=" . $row["trans_no"] 
-               ."&trans_type=" . $row["type"], ICON_MONEY);
+               ."&trans_type=" . $row["type"]."&debtor_no=" . $row["debtor_no"], ICON_MONEY);
 
        if ($row["type"] == ST_CUSTCREDIT && $row['TotalAmount'] > 0)
        {
@@ -103,12 +103,12 @@ function alloc_link($row)
                return $link;
        }
        elseif (($row["type"] == ST_CUSTPAYMENT || $row["type"] == ST_BANKDEPOSIT) &&
-               ($row['TotalAmount'] - $row['Allocated']) > 0)
+               (floatcmp($row['TotalAmount'], $row['Allocated']) >= 0))
        {
                /*its a receipt  which could have an allocation*/
                return $link;
        }
-       elseif ($row["type"] == ST_CUSTPAYMENT && $row['TotalAmount'] < 0)
+       elseif ($row["type"] == ST_CUSTPAYMENT && $row['TotalAmount'] <= 0)
        {
                /*its a negative receipt */
                return '';
@@ -136,7 +136,8 @@ function fmt_credit($row)
 }
 //------------------------------------------------------------------------------------------------
 
-$sql = get_sql_for_customer_allocation_inquiry();
+$sql = get_sql_for_customer_allocation_inquiry(get_post('TransAfterDate'), get_post('TransToDate'),
+               get_post('customer_id'), get_post('filterType'), check_value('showSettled'));
 
 //------------------------------------------------------------------------------------------------
 $cols = array(