92d4e750db3add241e1d8a59b087bfb933b9a808
[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 (".db_escape($amount).", '$date', "
22                 .db_escape($trans_type_from).", ".db_escape($trans_no_from).", "
23                 .db_escape($trans_no_to).", ".db_escape($trans_type_to).")";
24
25         db_query($sql, "A supplier allocation could not be added to the database");
26 }
27
28 //----------------------------------------------------------------------------------------
29
30
31 function delete_supp_allocation($trans_id)
32 {
33         $sql = "DELETE FROM ".TB_PREF."supp_allocations WHERE id = ".db_escape($trans_id);
34         db_query($sql, "The existing allocation $trans_id could not be deleted");
35 }
36
37 //----------------------------------------------------------------------------------------
38
39 function get_supp_trans_allocation_balance($trans_type, $trans_no)
40 {
41         $sql = "SELECT (ov_amount+ov_gst-ov_discount-alloc) AS BalToAllocate
42                 FROM ".TB_PREF."supp_trans WHERE trans_no="
43                 .db_escape($trans_no)." AND type=".db_escape($trans_type);
44         $result = db_query($sql,"calculate the allocation");
45         $myrow = db_fetch_row($result);
46
47         return $myrow[0];
48 }
49
50 //----------------------------------------------------------------------------------------
51 //      Update supplier trans alloc field according to current status of supp_allocations
52 //
53 function update_supp_trans_allocation($trans_type, $trans_no)
54 {
55         $sql = "UPDATE `".TB_PREF.($trans_type==ST_PURCHORDER ? 'purch_orders' : 'supp_trans')."` trans,
56                         (SELECT sum(amt) amt from ".TB_PREF."supp_allocations
57                                 WHERE (trans_type_to=".db_escape($trans_type)." AND trans_no_to=".db_escape($trans_no).")
58                                 OR (trans_type_from=".db_escape($trans_type)." AND trans_no_from=".db_escape($trans_no).")) allocated
59                 SET 
60                         trans.alloc=IFNULL(allocated.amt, 0)
61                 WHERE " . ($trans_type==ST_PURCHORDER ? 
62                           "trans.order_no=".db_escape($trans_no)
63                         : "trans.type=".db_escape($trans_type)." AND trans.trans_no=".db_escape($trans_no));
64
65         db_query($sql, "The supp transaction record could not be modified for the allocation against it");
66 }
67
68
69 //-------------------------------------------------------------------------------------------------------------
70
71 function void_supp_allocations($type, $type_no, $date="")
72 {
73         return clear_supp_alloctions($type, $type_no, $date);
74 }
75
76 //-------------------------------------------------------------------------------------------------------------
77
78 function clear_supp_alloctions($type, $type_no, $date="")
79 {
80         // clear any allocations for this transaction
81         $sql = "SELECT * FROM ".TB_PREF."supp_allocations
82                 WHERE (trans_type_from=".db_escape($type)." AND trans_no_from=".db_escape($type_no).")
83                 OR (trans_type_to=".db_escape($type)." AND trans_no_to=".db_escape($type_no).")";
84         $result = db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
85
86         while ($row = db_fetch($result))
87         {
88                 $sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
89                         WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
90                         OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
91                 //$sql = "UPDATE ".TB_PREF."supp_trans SET alloc=alloc - " . $row['amt'] . "
92                 //      WHERE type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'];
93                 db_query($sql, "could not clear allocation");
94                 // 2008-09-20 Joe Hunt
95                 if ($date != "")
96                         exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
97                                 $row['amt'], PT_SUPPLIER, true);
98                 //////////////////////
99         }
100
101
102         // remove any allocations for this transaction
103         $sql = "DELETE FROM ".TB_PREF."supp_allocations
104                 WHERE (trans_type_from=".db_escape($type)." AND trans_no_from=".db_escape($type_no).")
105                 OR (trans_type_to=".db_escape($type)." AND trans_no_to=".db_escape($type_no).")";
106
107         db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
108 }
109
110 //----------------------------------------------------------------------------------------
111 function get_alloc_supp_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
112 {
113         $sql = "SELECT
114                 trans.type,
115                 trans.trans_no,
116                 IF(trans.supp_reference='',trans.reference,trans.supp_reference) as reference,
117                 trans.tran_date,
118                 supplier.supp_name, 
119                 supplier.curr_code, 
120                 ov_amount+ov_gst+ov_discount AS Total,
121                 trans.alloc,
122                 trans.due_date,
123                 trans.supplier_id,
124                 supplier.address";
125
126         if ($extra_fields)
127                 $sql .= ", $extra_fields ";
128
129         $sql .= " FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier";
130         if ($extra_tables)
131                 $sql .= " ,$extra_tables ";
132
133         $sql .= " WHERE trans.supplier_id=supplier.supplier_id";
134
135         if ($extra_conditions)
136                 $sql .= " AND $extra_conditions";
137         
138         return $sql;
139 }
140
141
142 //-------------------------------------------------------------------------------------------------------------
143
144 function get_allocatable_from_supp_sql($supplier_id, $settled)
145 {
146         $settled_sql = "";
147         if (!$settled)
148         {
149                 $settled_sql = "AND round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0";
150         }
151
152         $supp_sql = "";
153         if ($supplier_id != null)
154                 $supp_sql = " AND trans.supplier_id = ".db_escape($supplier_id);
155
156         $sql = get_alloc_supp_sql("round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) <= 0 AS settled",
157                 "(type=".ST_SUPPAYMENT." OR type=".ST_SUPPCREDIT." OR type=".ST_BANKPAYMENT.") AND (ov_amount < 0) " . $settled_sql . $supp_sql);
158
159         return $sql;
160 }
161
162
163 //-------------------------------------------------------------------------------------------------------------
164
165 function get_allocatable_to_supp_transactions($supplier_id, $trans_no=null, $type=null)
166 {
167         if ($trans_no != null && $type!= null)
168         {
169                 $sql = get_alloc_supp_sql("amt, supp_reference", "trans.trans_no = alloc.trans_no_to
170                         AND trans.type = alloc.trans_type_to
171                         AND alloc.trans_no_from=".db_escape($trans_no)."
172                         AND alloc.trans_type_from=".db_escape($type)."
173                         AND trans.supplier_id=".db_escape($supplier_id),
174                         TB_PREF."supp_allocations as alloc");
175         }
176         else
177         {
178                 $sql = get_alloc_supp_sql(null, "round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0
179                         AND trans.type NOT IN (".implode(',',array(ST_SUPPAYMENT, ST_BANKPAYMENT)).")
180                         AND trans.supplier_id=".db_escape($supplier_id));
181         }
182
183         return db_query($sql." ORDER BY due_date", "Cannot retreive alloc to transactions");
184 }
185
186 //-------------------------------------------------------------------------------------------------------------
187
188 function get_allocatable_from_supp_transactions($supplier_id, $trans_no=null, $type=null)
189 {
190         if ($trans_no != null && $type!= null)
191         {
192                 $sql = get_alloc_supp_sql("amt, supp_reference", "trans.trans_no = alloc.trans_no_from
193                         AND trans.type = alloc.trans_type_from
194                         AND alloc.trans_no_to=".db_escape($trans_no)."
195                         AND alloc.trans_type_to=".db_escape($type)."
196                         AND trans.supplier_id=".db_escape($supplier_id),
197                         TB_PREF."supp_allocations as alloc");
198         }
199         else
200         {
201                 $sql = get_alloc_supp_sql(null, "round(ABS(ov_amount+ov_gst+ov_discount)-alloc,6) > 0
202                         AND trans.type != ".ST_SUPPAYMENT."
203                         AND trans.supplier_id=".db_escape($supplier_id));
204         }
205
206         return db_query($sql." ORDER BY due_date", "Cannot retreive alloc to transactions");
207 }
208
209 function get_sql_for_supplier_allocation_inquiry()
210 {
211         $date_after = date2sql($_POST['TransAfterDate']);
212         $date_to = date2sql($_POST['TransToDate']);
213
214     $sql = "SELECT 
215                 trans.type, 
216                 trans.trans_no,
217                 trans.reference, 
218                 supplier.supp_name, 
219                 trans.supp_reference,
220         trans.tran_date, 
221                 trans.due_date,
222                 supplier.curr_code, 
223         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
224                 trans.alloc AS Allocated,
225                 ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
226                 trans.supplier_id
227         FROM "
228                         .TB_PREF."supp_trans as trans, "
229                         .TB_PREF."suppliers as supplier
230         WHERE supplier.supplier_id = trans.supplier_id
231         AND trans.tran_date >= '$date_after'
232         AND trans.tran_date <= '$date_to'";
233
234         if ($_POST['supplier_id'] != ALL_TEXT)
235                 $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
236         if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
237         {
238                 if (($_POST['filterType'] == '1') || ($_POST['filterType'] == '2'))
239                 {
240                         $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
241                 }
242                 elseif ($_POST['filterType'] == '3')
243                 {
244                         $sql .= " AND trans.type = ".ST_SUPPAYMENT." ";
245                 }
246                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5'))
247                 {
248                         $sql .= " AND trans.type = ".ST_SUPPCREDIT." ";
249                 }
250
251                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5'))
252                 {
253                         $today =  date2sql(Today());
254                         $sql .= " AND trans.due_date < '$today' ";
255                 }
256         }
257
258         if (!check_value('showSettled'))
259         {
260                 $sql .= " AND (round(abs(ov_amount + ov_gst + ov_discount) - alloc,6) != 0) ";
261         }
262         return $sql;
263 }
264 ?>