Coede reorganization to reuse sql query by db_pager.
[fa-stable.git] / purchasing / includes / db / suppalloc_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4
5 function add_supp_allocation($amount, $trans_type_from, $trans_no_from,
6         $trans_type_to, $trans_no_to, $date_)
7 {
8         $date = date2sql($date_);
9         $sql = "INSERT INTO ".TB_PREF."supp_allocations (
10                 amt, date_alloc,
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)";
13
14         db_query($sql, "A supplier allocation could not be added to the database");
15 }
16
17 //----------------------------------------------------------------------------------------
18
19
20 function delete_supp_allocation($trans_id)
21 {
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");
24 }
25
26 //----------------------------------------------------------------------------------------
27
28 function get_supp_trans_allocation_balance($trans_type, $trans_no)
29 {
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);
34
35         return $myrow[0];
36 }
37
38 //----------------------------------------------------------------------------------------
39
40 function update_supp_trans_allocation($trans_type, $trans_no, $alloc)
41 {
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");
45 }
46
47 //-------------------------------------------------------------------------------------------------------------
48
49 function void_supp_allocations($type, $type_no, $date="")
50 {
51         return clear_supp_alloctions($type, $type_no, $date);
52 }
53
54 //-------------------------------------------------------------------------------------------------------------
55
56 function clear_supp_alloctions($type, $type_no, $date="")
57 {
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");
63
64         while ($row = db_fetch($result))
65         {
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");
72                 // 2008-09-20 Joe Hunt
73                 if ($date != "")
74                         exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
75                                 $row['amt'], payment_person_types::supplier(), true);
76                 //////////////////////
77         }
78
79
80         // remove any allocations for this transaction
81         $sql = "DELETE FROM ".TB_PREF."supp_allocations
82                 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
83                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
84
85         db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
86 }
87
88 //----------------------------------------------------------------------------------------
89 function get_alloc_supp_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
90 {
91         $sql = "SELECT
92                 trans.type,
93                 trans.trans_no,
94                 trans.reference,
95                 trans.tran_date,
96                 supplier.supp_name, 
97                 supplier.curr_code, 
98                 ov_amount+ov_gst+ov_discount AS Total,
99                 trans.alloc,
100                 trans.due_date,
101                 trans.supplier_id,
102                 supplier.address";
103 /*      $sql = "SELECT trans.*,
104                 ov_amount+ov_gst+ov_discount AS Total,
105                 supplier.supp_name, supplier.address,
106                 supplier.curr_code ";
107 */
108         if ($extra_fields)
109                 $sql .= ", $extra_fields ";
110
111         $sql .= " FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier";
112         if ($extra_tables)
113                 $sql .= " ,$extra_tables ";
114
115         $sql .= " WHERE trans.supplier_id=supplier.supplier_id";
116
117         if ($extra_conditions)
118                 $sql .= " AND $extra_conditions ";
119
120         return $sql;
121 }
122
123
124 //-------------------------------------------------------------------------------------------------------------
125
126 function get_allocatable_from_supp_sql($supplier_id, $settled)
127 {
128         $settled_sql = "";
129         if (!$settled)
130         {
131                 $settled_sql = "AND round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0";
132         }
133
134         $supp_sql = "";
135         if ($supplier_id != null)
136                 $supp_sql = " AND trans.supplier_id = $supplier_id";
137
138         $sql = get_alloc_supp_sql("round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) <= 0 AS settled",
139                 "(type=22 OR type=21 OR type=1) AND (ov_amount < 0) " . $settled_sql . $supp_sql);
140
141         return $sql;
142 }
143
144 //-------------------------------------------------------------------------------------------------------------
145
146 function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $type=null)
147 {
148         if ($trans_no != null && $type!= null)
149         {
150                 $sql = get_alloc_supp_sql("amt", "trans.trans_no = alloc.trans_no_to
151                         AND trans.type = alloc.trans_type_to
152                         AND alloc.trans_no_from=$trans_no
153                         AND alloc.trans_type_from=$type
154                         AND trans.supplier_id=$supplier_id",
155                         "".TB_PREF."supp_allocations as alloc");
156         }
157         else
158         {
159                 $sql = get_alloc_supp_sql(null, "round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0
160                         AND trans.type != 22
161                         AND trans.supplier_id=$supplier_id");
162         }
163
164         return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
165 }
166
167
168 ?>