Rerun Items Summary Report fix
[fa-stable.git] / purchasing / includes / db / suppalloc_db.inc
index 535e66c5a0208a3b79a44cc50e0447da8e51c1c3..59ce2ba14785a4114c6209ace59aeddd2e18d59a 100644 (file)
 //----------------------------------------------------------------------------------------
 
 function add_supp_allocation($amount, $trans_type_from, $trans_no_from,
-       $trans_type_to, $trans_no_to, $date_)
+       $trans_type_to, $trans_no_to, $date_=null)
 {
+       if (!$date_)
+               $date = Today();
+
        $date = date2sql($date_);
        $sql = "INSERT INTO ".TB_PREF."supp_allocations (
                amt, date_alloc,
@@ -48,14 +51,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="")
@@ -78,13 +91,16 @@ function clear_supp_alloctions($type, $type_no, $date="")
                $sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
                        WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
                        OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
-               //$sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
-               //      WHERE type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'];
                db_query($sql, "could not clear allocation");
+
                // 2008-09-20 Joe Hunt
                if ($date != "")
                        exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
                                $row['amt'], PT_SUPPLIER, true);
+               // 2014-01-16 Joe Hunt. Fix for voiding allocated currency supp_invoices.               
+               elseif ($type == ST_SUPPINVOICE && $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_SUPPLIER, true);
                //////////////////////
        }
 
@@ -149,6 +165,7 @@ function get_allocatable_from_supp_sql($supplier_id, $settled)
        return $sql;
 }
 
+
 //-------------------------------------------------------------------------------------------------------------
 
 function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $type=null)
@@ -165,7 +182,7 @@ function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $typ
        else
        {
                $sql = get_alloc_supp_sql(null, "round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0
-                       AND trans.type != ".ST_SUPPAYMENT."
+                       AND trans.type NOT IN (".implode(',',array(ST_SUPPAYMENT, ST_BANKPAYMENT, ST_SUPPCREDIT)).")
                        AND trans.supplier_id=".db_escape($supplier_id));
        }