Dimension is missing in Supplier Invoice View, GL Rows.
[fa-stable.git] / sales / includes / db / custalloc_db.inc
index 896649090fedf2658b5dd85ac8b4b4d1a4d4d289..a3abb44bdb93119746ec8c87d82cfcf55049b30f 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");
 }
 
@@ -68,6 +75,10 @@ function clear_cust_alloctions($type, $type_no, $date="")
                if ($date != "")
                        exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
                                $row['amt'], PT_CUSTOMER, true);
+               // 2014-01-16 Joe Hunt. Fix for voiding allocated currency sales_invoices.              
+               elseif ($type == ST_SALESINVOICE && $row['trans_type_to'] == $type && $row['trans_no_to'] == $type_no)          
+                       exchange_variation($row['trans_type_from'], $row['trans_no_from'], $row['trans_type_to'], $row['trans_no_to'], 
+                               sql2date($row['date_alloc']), $row['amt'], PT_CUSTOMER, true);
                //////////////////////
        }
 
@@ -94,7 +105,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 +196,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 +225,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 +258,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;
 }
 
@@ -273,29 +286,27 @@ function credit_sales_invoice_allocate($invoice_no, $credit_no, $amount, $date)
 
                while(($free < $amount) && ($alloc = db_fetch($result))) {
                        $unalloc = min($alloc['amt'], $amount-$free);
-                       update_debtor_trans_allocation($alloc['trans_type_to'], $alloc['trans_no_to'], 
-                               -$unalloc);
-                       update_debtor_trans_allocation($alloc['trans_type_from'], $alloc['trans_no_from'], 
-                               -$unalloc);
 
                        delete_cust_allocation($alloc['id']);
                        if ($unalloc < $alloc['amt'])
                                add_cust_allocation($alloc['amt']-$unalloc, $alloc['trans_type_from'],
                                        $alloc['trans_no_from'], ST_SALESINVOICE, $invoice_no);
 
+                       update_debtor_trans_allocation($alloc['trans_type_to'], $alloc['trans_no_to']);
+                       update_debtor_trans_allocation($alloc['trans_type_from'], $alloc['trans_no_from']);
                        $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.
  Check all credit notes allocated to this invoice for summarized freight charges."));
                return false;
        }
-       update_debtor_trans_allocation(ST_SALESINVOICE, $invoice_no, $amount);
-       update_debtor_trans_allocation(ST_CUSTCREDIT, $credit_no, $amount);
        add_cust_allocation($amount, ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no);
+       update_debtor_trans_allocation(ST_SALESINVOICE, $invoice_no);
+       update_debtor_trans_allocation(ST_CUSTCREDIT, $credit_no);
 
        exchange_variation(ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no, $date,
                $amount, PT_CUSTOMER);