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