Additional access control related ui helpers added: check_edit_access, access_post...
[fa-stable.git] / purchasing / includes / db / suppalloc_db.inc
index c16c97557f015b1a3d3cac1e809fe945279ef727..2d1b96bd3369c91c1379e5032d3ca8d4291b7854 100644 (file)
@@ -48,19 +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.($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));
 
-       if ($trans_type == ST_PURCHORDER)
-               $sql = "UPDATE ".TB_PREF."purch_orders SET alloc = alloc + ".db_escape($alloc)."
-                       WHERE order_no = ".db_escape($trans_no);
-       else
-               $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);
        db_query($sql, "The supp transaction record could not be modified for the allocation against it");
 }
 
+
 //-------------------------------------------------------------------------------------------------------------
 
 function void_supp_allocations($type, $type_no, $date="")
@@ -247,10 +252,10 @@ function get_allocatable_from_supp_transactions($supplier_id, $trans_no=null, $t
 }
 
 
-function get_sql_for_supplier_allocation_inquiry()
+function get_sql_for_supplier_allocation_inquiry($from, $to, $filter, $supplier_id, $all=false)
 {
-       $date_after = date2sql($_POST['TransAfterDate']);
-       $date_to = date2sql($_POST['TransToDate']);
+       $date_after = date2sql($from);
+       $date_to = date2sql($to);
 
     $sql = "SELECT 
                trans.type, 
@@ -272,34 +277,34 @@ function get_sql_for_supplier_allocation_inquiry()
        AND trans.tran_date >= '$date_after'
        AND trans.tran_date <= '$date_to'";
 
-       if ($_POST['supplier_id'] != ALL_TEXT)
-               $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
-       if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
+       if ($supplier_id != ALL_TEXT)
+               $sql .= " AND trans.supplier_id = ".db_escape($supplier_id);
+
+       if ($filter != ALL_TEXT)
        {
-               if (($_POST['filterType'] == '1') || ($_POST['filterType'] == '2'))
+               if (($filter == '1') || ($filter == '2'))
                {
                        $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
                }
-               elseif ($_POST['filterType'] == '3')
+               elseif ($filter == '3')
                {
                        $sql .= " AND trans.type = ".ST_SUPPAYMENT." ";
                }
-               elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5'))
+               elseif (($filter == '4') || ($filter == '5'))
                {
                        $sql .= " AND trans.type = ".ST_SUPPCREDIT." ";
                }
 
-               if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5'))
+               if (($filter == '2') || ($filter == '5'))
                {
                        $today =  date2sql(Today());
                        $sql .= " AND trans.due_date < '$today' ";
                }
        }
 
-       if (!check_value('showSettled'))
+       if (!$all)
        {
                $sql .= " AND (round(abs(ov_amount + ov_gst + ov_discount) - alloc,6) != 0) ";
        }
        return $sql;
 }
-?>
\ No newline at end of file