Changed license type to GPLv3 in top of files
[fa-stable.git] / purchasing / includes / db / suppalloc_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //----------------------------------------------------------------------------------------
13
14 function add_supp_allocation($amount, $trans_type_from, $trans_no_from,
15         $trans_type_to, $trans_no_to, $date_)
16 {
17         $date = date2sql($date_);
18         $sql = "INSERT INTO ".TB_PREF."supp_allocations (
19                 amt, date_alloc,
20                 trans_type_from, trans_no_from, trans_no_to, trans_type_to)
21                 VALUES ($amount, '$date', $trans_type_from, $trans_no_from, $trans_no_to, $trans_type_to)";
22
23         db_query($sql, "A supplier allocation could not be added to the database");
24 }
25
26 //----------------------------------------------------------------------------------------
27
28
29 function delete_supp_allocation($trans_id)
30 {
31         $sql = "DELETE FROM ".TB_PREF."supp_allocations WHERE id = " . $trans_id;
32         db_query($sql, "The existing allocation $trans_id could not be deleted");
33 }
34
35 //----------------------------------------------------------------------------------------
36
37 function get_supp_trans_allocation_balance($trans_type, $trans_no)
38 {
39         $sql = "SELECT (ov_amount+ov_gst-ov_discount-alloc) AS BalToAllocate
40                 FROM ".TB_PREF."supp_trans WHERE trans_no=$trans_no AND type=$trans_type";
41         $result = db_query($sql,"calculate the allocation");
42         $myrow = db_fetch_row($result);
43
44         return $myrow[0];
45 }
46
47 //----------------------------------------------------------------------------------------
48
49 function update_supp_trans_allocation($trans_type, $trans_no, $alloc)
50 {
51         $sql = "UPDATE ".TB_PREF."supp_trans SET alloc = alloc + $alloc
52                 WHERE type=$trans_type AND trans_no = $trans_no";
53         db_query($sql, "The supp transaction record could not be modified for the allocation against it");
54 }
55
56 //-------------------------------------------------------------------------------------------------------------
57
58 function void_supp_allocations($type, $type_no, $date="")
59 {
60         return clear_supp_alloctions($type, $type_no, $date);
61 }
62
63 //-------------------------------------------------------------------------------------------------------------
64
65 function clear_supp_alloctions($type, $type_no, $date="")
66 {
67         // clear any allocations for this transaction
68         $sql = "SELECT * FROM ".TB_PREF."supp_allocations
69                 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
70                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
71         $result = db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
72
73         while ($row = db_fetch($result))
74         {
75                 $sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
76                         WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
77                         OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
78                 //$sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
79                 //      WHERE type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'];
80                 db_query($sql, "could not clear allocation");
81                 // 2008-09-20 Joe Hunt
82                 if ($date != "")
83                         exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
84                                 $row['amt'], payment_person_types::supplier(), true);
85                 //////////////////////
86         }
87
88
89         // remove any allocations for this transaction
90         $sql = "DELETE FROM ".TB_PREF."supp_allocations
91                 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
92                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
93
94         db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
95 }
96
97 //----------------------------------------------------------------------------------------
98 function get_alloc_supp_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
99 {
100         $sql = "SELECT
101                 trans.type,
102                 trans.trans_no,
103                 trans.reference,
104                 trans.tran_date,
105                 supplier.supp_name, 
106                 supplier.curr_code, 
107                 ov_amount+ov_gst+ov_discount AS Total,
108                 trans.alloc,
109                 trans.due_date,
110                 trans.supplier_id,
111                 supplier.address";
112 /*      $sql = "SELECT trans.*,
113                 ov_amount+ov_gst+ov_discount AS Total,
114                 supplier.supp_name, supplier.address,
115                 supplier.curr_code ";
116 */
117         if ($extra_fields)
118                 $sql .= ", $extra_fields ";
119
120         $sql .= " FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier";
121         if ($extra_tables)
122                 $sql .= " ,$extra_tables ";
123
124         $sql .= " WHERE trans.supplier_id=supplier.supplier_id";
125
126         if ($extra_conditions)
127                 $sql .= " AND $extra_conditions ";
128
129         return $sql;
130 }
131
132
133 //-------------------------------------------------------------------------------------------------------------
134
135 function get_allocatable_from_supp_sql($supplier_id, $settled)
136 {
137         $settled_sql = "";
138         if (!$settled)
139         {
140                 $settled_sql = "AND round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0";
141         }
142
143         $supp_sql = "";
144         if ($supplier_id != null)
145                 $supp_sql = " AND trans.supplier_id = $supplier_id";
146
147         $sql = get_alloc_supp_sql("round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) <= 0 AS settled",
148                 "(type=22 OR type=21 OR type=1) AND (ov_amount < 0) " . $settled_sql . $supp_sql);
149
150         return $sql;
151 }
152
153 //-------------------------------------------------------------------------------------------------------------
154
155 function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $type=null)
156 {
157         if ($trans_no != null && $type!= null)
158         {
159                 $sql = get_alloc_supp_sql("amt, supp_reference", "trans.trans_no = alloc.trans_no_to
160                         AND trans.type = alloc.trans_type_to
161                         AND alloc.trans_no_from=$trans_no
162                         AND alloc.trans_type_from=$type
163                         AND trans.supplier_id=$supplier_id",
164                         "".TB_PREF."supp_allocations as alloc");
165         }
166         else
167         {
168                 $sql = get_alloc_supp_sql(null, "round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0
169                         AND trans.type != 22
170                         AND trans.supplier_id=$supplier_id");
171         }
172
173         return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
174 }
175
176
177 ?>