3 //----------------------------------------------------------------------------------------
5 function add_supp_allocation($amount, $trans_type_from, $trans_no_from,
6 $trans_type_to, $trans_no_to, $date_)
8 $date = date2sql($date_);
9 $sql = "INSERT INTO ".TB_PREF."supp_allocations (
11 trans_type_from, trans_no_from, trans_no_to, trans_type_to)
12 VALUES ($amount, '$date', $trans_type_from, $trans_no_from, $trans_no_to, $trans_type_to)";
14 db_query($sql, "A supplier allocation could not be added to the database");
17 //----------------------------------------------------------------------------------------
20 function delete_supp_allocation($trans_id)
22 $sql = "DELETE FROM ".TB_PREF."supp_allocations WHERE id = " . $trans_id;
23 db_query($sql, "The existing allocation $trans_id could not be deleted");
26 //----------------------------------------------------------------------------------------
28 function get_supp_trans_allocation_balance($trans_type, $trans_no)
30 $sql = "SELECT (ov_amount+ov_gst-ov_discount-alloc) AS BalToAllocate
31 FROM ".TB_PREF."supp_trans WHERE trans_no=$trans_no AND type=$trans_type";
32 $result = db_query($sql,"calculate the allocation");
33 $myrow = db_fetch_row($result);
38 //----------------------------------------------------------------------------------------
40 function update_supp_trans_allocation($trans_type, $trans_no, $alloc)
42 $sql = "UPDATE ".TB_PREF."supp_trans SET alloc = alloc + $alloc
43 WHERE type=$trans_type AND trans_no = $trans_no";
44 db_query($sql, "The supp transaction record could not be modified for the allocation against it");
47 //-------------------------------------------------------------------------------------------------------------
49 function void_supp_allocations($type, $type_no)
51 return clear_supp_alloctions($type, $type_no);
54 //-------------------------------------------------------------------------------------------------------------
56 function clear_supp_alloctions($type, $type_no)
58 // clear any allocations for this transaction
59 $sql = "SELECT * FROM ".TB_PREF."supp_allocations
60 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
61 OR (trans_type_to=$type AND trans_no_to=$type_no)";
62 $result = db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
64 while ($row = db_fetch($result))
66 $sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
67 WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
68 OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
69 //$sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
70 // WHERE type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'];
71 db_query($sql, "could not clear allocation");
75 // remove any allocations for this transaction
76 $sql = "DELETE FROM ".TB_PREF."supp_allocations
77 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
78 OR (trans_type_to=$type AND trans_no_to=$type_no)";
80 db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
83 //-------------------------------------------------------------------------------------------------------------
85 function get_allocatable_from_supp_transactions($supplier_id, $settled)
90 $settled_sql = "AND round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0";
94 if ($supplier_id != null)
95 $supp_sql = " AND ".TB_PREF."supp_trans.supplier_id = $supplier_id";
97 return get_supplier_transactions("round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) <= 0 AS settled",
98 "(type=22 OR type=21 OR type=1) AND (ov_amount < 0) " . $settled_sql . $supp_sql);
101 //-------------------------------------------------------------------------------------------------------------
103 function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $type=null)
105 if ($trans_no != null && $type!= null)
107 return get_supplier_transactions("amt", "".TB_PREF."supp_trans.trans_no = ".TB_PREF."supp_allocations.trans_no_to
108 AND ".TB_PREF."supp_trans.type = ".TB_PREF."supp_allocations.trans_type_to
109 AND ".TB_PREF."supp_allocations.trans_no_from=$trans_no
110 AND ".TB_PREF."supp_allocations.trans_type_from=$type
111 AND ".TB_PREF."supp_trans.supplier_id=$supplier_id",
112 "".TB_PREF."supp_allocations");
116 return get_supplier_transactions(null, "round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0
117 AND ".TB_PREF."supp_trans.type != 22
118 AND ".TB_PREF."supp_trans.supplier_id=$supplier_id");