Improved Customer and Supplier Balances Report. Option to choose last column as balance.
[fa-stable.git] / sales / includes / db / custalloc_db.inc
index 16f9227e95f7f961ab5cf163e76c54e3cf1d2a6c..7260f0e10ed0702b19ab0c4e0dd8648cd8eb9693 100644 (file)
@@ -158,6 +158,32 @@ function get_allocatable_to_cust_transactions($customer_id, $trans_no=null, $typ
        return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
 }
 
+//-------------------------------------------------------------------------------------------------------------
+
+function get_allocatable_from_cust_transactions($customer_id, $trans_no=null, $type=null)
+{
+       if ($trans_no != null and $type != null)
+       {
+               $sql = get_alloc_trans_sql("amt", "trans.trans_no = alloc.trans_no_from
+                       AND trans.type = alloc.trans_type_from
+                       AND alloc.trans_no_to=".db_escape($trans_no)."
+                       AND alloc.trans_type_to=".db_escape($type)."
+                       AND trans.debtor_no=".db_escape($customer_id),
+                       "".TB_PREF."cust_allocations as alloc");
+       }
+       else
+       {
+               $sql = get_alloc_trans_sql(null, "round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) > 0
+                       AND trans.type <> " . ST_CUSTPAYMENT . "
+                       AND trans.type <> " . ST_BANKDEPOSIT . "
+                       AND trans.type <> " . ST_CUSTCREDIT . "
+                       AND trans.type <> " . ST_CUSTDELIVERY . "
+                       AND trans.debtor_no=".db_escape($customer_id));
+       }
+
+       return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
+}
+
 function get_sql_for_customer_allocation_inquiry()
 {
        $data_after = date2sql($_POST['TransAfterDate']);
@@ -176,7 +202,8 @@ function get_sql_for_customer_allocation_inquiry()
                        + trans.ov_freight_tax + trans.ov_discount)     AS TotalAmount,
                trans.alloc AS Allocated,
                ((trans.type = ".ST_SALESINVOICE.")
-                       AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
+                       AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
+               trans.debtor_no
        FROM "
                        .TB_PREF."debtor_trans as trans, "
                        .TB_PREF."debtors_master as debtor
@@ -259,7 +286,7 @@ function credit_sales_invoice_allocate($invoice_no, $credit_no, $amount, $date)
                        $free += $unalloc;
                }
        }
-       if ($free < $amount) {
+       if (floatcmp($free, $amount)<0) {
                // this should never happen unless sparse credit notices were allocated to 
                // the invoice, or summarized freight costs on credit notes is more than those on invoice.
                display_error(_("Unsuspected overallocation happened due to sparse credit notes exists for this invoice.