Merged changes up to 2.3.16 into unstable
[fa-stable.git] / sales / includes / db / custalloc_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_cust_allocation($amount, $trans_type_from, $trans_no_from,
15         $trans_type_to, $trans_no_to)
16 {
17         $sql = "INSERT INTO ".TB_PREF."cust_allocations (
18                 amt, date_alloc,
19                 trans_type_from, trans_no_from, trans_no_to, trans_type_to)
20                 VALUES ($amount, Now(), ".db_escape($trans_type_from).", ".db_escape($trans_no_from).", ".db_escape($trans_no_to)
21                 .", ".db_escape($trans_type_to).")";
22
23         db_query($sql, "A customer allocation could not be added to the database");
24 }
25
26 //----------------------------------------------------------------------------------------
27
28
29 function delete_cust_allocation($trans_id)
30 {
31         $sql = "DELETE FROM ".TB_PREF."cust_allocations WHERE id = ".db_escape($trans_id);
32         return db_query($sql, "The existing allocation $trans_id could not be deleted");
33 }
34
35 //----------------------------------------------------------------------------------------
36
37 function get_cust_allocation($trans_id)
38 {
39         $sql = "SELECT * FROM ".TB_PREF."cust_allocations WHERE id = ".db_escape($trans_id);
40         return db_fetch(db_query($sql), "Cannot retrieve customer allocation $trans_id");
41 }
42
43 //----------------------------------------------------------------------------------------
44
45 function update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
46 {
47         if ($trans_type == ST_SALESORDER)
48                 $sql = "UPDATE ".TB_PREF."sales_orders SET alloc = alloc + $alloc
49                         WHERE trans_type=".db_escape($trans_type)." AND order_no = ".db_escape($trans_no);
50         else
51                 $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc = alloc + $alloc
52                         WHERE type=".db_escape($trans_type)." AND trans_no = ".db_escape($trans_no);
53         db_query($sql, "The debtor transaction record could not be modified for the allocation against it");
54 }
55
56 //-------------------------------------------------------------------------------------------------------------
57
58 function void_cust_allocations($type, $type_no, $date="")
59 {
60         return clear_cust_alloctions($type, $type_no, $date);
61 }
62
63 //-------------------------------------------------------------------------------------------------------------
64
65 function clear_cust_alloctions($type, $type_no, $date="")
66 {
67         $sql = "UPDATE  ".TB_PREF."cust_allocations ca
68                                 LEFT JOIN ".TB_PREF."debtor_trans paym ON ca.trans_type_from=paym.type AND ca.trans_no_from=paym.trans_no
69                                 LEFT JOIN ".TB_PREF."debtor_trans dt ON ca.trans_type_to=dt.type AND ca.trans_no_to=dt.trans_no
70                                 LEFT JOIN ".TB_PREF."sales_orders so ON ca.trans_type_to=so.trans_type AND ca.trans_no_to=so.order_no
71                         SET paym.alloc=paym.alloc - ca.amt,
72                                 dt.alloc=dt.alloc -  ca.amt,
73                                 so.alloc=so.alloc -  ca.amt
74                         WHERE  (ca.trans_type_from=".db_escape($type)." AND ca.trans_no_from=".db_escape($type_no).")
75                                 OR (ca.trans_type_to=".db_escape($type)." AND ca.trans_no_to=".db_escape($type_no).")";
76                 db_query($sql, "could not clear allocation");
77
78         // remove any allocations for this transaction
79         $sql = "DELETE FROM ".TB_PREF."cust_allocations
80                         WHERE  (trans_type_from=".db_escape($type)." AND trans_no_from=".db_escape($type_no).")
81                                 OR (trans_type_to=".db_escape($type)." AND trans_no_to=".db_escape($type_no).")";
82
83         db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
84 // is this necessary?
85 //      if ($date != "")
86 //              exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
87 //                      $row['amt'], PT_CUSTOMER, true);
88 }
89 //-------------------------------------------------------------------------------------------------------------
90
91 function get_allocatable_from_cust_sql($customer_id=null, $settled)
92 {
93         $sql = "SELECT
94                 trans.type,
95                 trans.trans_no,
96                 trans.reference,
97                 trans.tran_date,
98                 debtor.name AS DebtorName, 
99                 debtor.curr_code,
100                 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
101                 trans.alloc,
102                 trans.due_date,
103                 debtor.address,
104                 trans.version,
105                 round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc),6) <= 0 AS settled
106
107          FROM "
108                 .TB_PREF."debtor_trans as trans, "
109                 .TB_PREF."debtors_master as debtor"
110         ." WHERE trans.debtor_no=debtor.debtor_no
111                 AND (((type=".ST_CUSTPAYMENT." OR type=".ST_BANKDEPOSIT.") AND (trans.ov_amount > 0))"
112                 ." OR (type=".ST_CUSTCREDIT." AND (ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount)>0))";
113
114         if (!$settled)
115                 $sql .= " AND (round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax-ov_discount-alloc),6) > 0)";
116
117         if ($customer_id != null)
118                 $sql .= " AND trans.debtor_no = ".db_escape($customer_id);
119
120         return $sql;
121 }
122
123 function get_allocatable_sales_orders($customer_id = null, $trans_no=null, $type=null)
124 {
125         $sql = "SELECT
126                 sorder.trans_type as type,
127                 sorder.order_no as trans_no,
128                 sorder.reference,
129                 sorder.ord_date,
130                 debtor.name AS DebtorName, 
131                 debtor.curr_code,
132                 total-IFNULL(invoiced.amount,0) as Total,
133                 sorder.alloc,
134                 sorder.delivery_date as due_date,
135                 debtor.address,
136                 sorder.version,
137                 amt
138                 FROM ".TB_PREF."sales_orders as sorder
139                         LEFT JOIN ".TB_PREF."debtors_master as debtor ON sorder.debtor_no = debtor.debtor_no
140                         LEFT JOIN ".TB_PREF."cust_allocations as alloc ON sorder.order_no = alloc.trans_no_to AND sorder.trans_type = alloc.trans_type_to
141                         LEFT JOIN (SELECT order_, sum(prep_amount) amount FROM ".TB_PREF."debtor_trans dt
142                         WHERE prep_amount>0 AND dt.type=".ST_SALESINVOICE." GROUP BY order_) as invoiced ON sorder.order_no = invoiced.order_
143                 WHERE sorder.trans_type=".ST_SALESORDER;
144
145         if ($trans_no != null and $type != null)
146         {
147                 $sql .= " AND alloc.trans_no_from=".db_escape($trans_no)."
148                                   AND alloc.trans_type_from=".db_escape($type);
149         }
150         else
151         {
152                 $sql .= " AND round(sorder.prep_amount)>0 and Total>0"; // only sales orders with prepayment level set and no yet fully invoiced
153         }
154         if ($customer_id)
155                 $sql .= " AND sorder.debtor_no=".db_escape($customer_id);
156
157         $sql .= " GROUP BY sorder.order_no";
158
159         return $sql;
160 }
161 //-------------------------------------------------------------------------------------------------------------
162
163 function get_allocatable_to_cust_transactions($customer_id = null, $trans_no=null, $type=null)
164 {
165         $sql = "SELECT
166                 trans.type,
167                 trans.trans_no,
168                 trans.reference,
169                 trans.tran_date,
170                 debtor.name AS DebtorName, 
171                 debtor.curr_code,
172                 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
173                 trans.alloc,
174                 trans.due_date,
175                 debtor.address,
176                 trans.version,
177                 amt
178          FROM ".TB_PREF."debtor_trans as trans
179                         LEFT JOIN ".TB_PREF."cust_allocations as alloc ON trans.trans_no = alloc.trans_no_to AND trans.type = alloc.trans_type_to,"
180                         .TB_PREF."debtors_master as debtor
181          WHERE
182                  trans.debtor_no=debtor.debtor_no";
183         if ($customer_id)
184                 $sql .= " AND trans.debtor_no=".db_escape($customer_id);
185
186         if ($trans_no != null and $type != null)
187         {
188                 $sql .= " AND alloc.trans_no_from=".db_escape($trans_no)."
189                                   AND alloc.trans_type_from=".db_escape($type);
190         }
191         else
192         {
193                 $sql .= " AND round(IF(prep_amount, prep_amount, ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount)-alloc,6) > 0
194                         AND trans.type NOT IN (".implode(',',array(ST_CUSTPAYMENT,ST_BANKDEPOSIT,ST_CUSTCREDIT,ST_CUSTDELIVERY)).")";
195                 $sql .= " GROUP BY type, trans_no";
196         }
197         $orders = get_allocatable_sales_orders($customer_id, $trans_no, $type);
198         $sql = "($sql ORDER BY trans_no) \nUNION \n($orders)";
199
200         return db_query($sql, "Cannot retreive alloc to transactions");
201 }
202
203 //-------------------------------------------------------------------------------------------------------------
204
205 function get_allocatable_from_cust_transactions($customer_id, $trans_no=null, $type=null)
206 {
207
208         $sql = "SELECT
209                 trans.type,
210                 trans.trans_no,
211                 trans.reference,
212                 trans.tran_date,
213                 debtor.name AS DebtorName, 
214                 debtor.curr_code,
215                 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
216                 trans.alloc,
217                 trans.due_date,
218                 debtor.address,
219                 trans.version,
220                 amt
221          FROM  ".TB_PREF."debtor_trans as trans,"
222                         .TB_PREF."debtors_master as debtor,"
223                         .TB_PREF."cust_allocations as alloc
224          WHERE trans.debtor_no=debtor.debtor_no
225                         AND trans.trans_no = alloc.trans_no_from
226                         AND trans.type = alloc.trans_type_from";
227
228         if ($trans_no != null and $type != null)
229         {
230                 $sql .= " AND alloc.trans_no_to=".db_escape($trans_no)."
231                                   AND alloc.trans_type_to=".db_escape($type);
232         }
233         else
234         {
235                 $sql .= " AND round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) > 0
236                         AND trans.type NOT IN (".implode(',',array(ST_CUSTPAYMENT,ST_BANKDEPOSIT,ST_CUSTCREDIT,ST_CUSTDELIVERY)).")";
237                 $sql .= " GROUP BY type, trans_no";
238         }
239
240         if($customer_id)
241                 $sql .= " AND trans.debtor_no=".db_escape($customer_id);
242
243         return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
244 }
245
246 function get_sql_for_customer_allocation_inquiry()
247 {
248         $data_after = date2sql($_POST['TransAfterDate']);
249         $date_to = date2sql($_POST['TransToDate']);
250
251         $sql = "SELECT 
252                 trans.type,
253                 trans.trans_no,
254                 trans.reference,
255                 trans.order_,
256                 trans.tran_date,
257                 trans.due_date,
258                 debtor.name,
259                 debtor.curr_code,
260                 debtor.debtor_no,
261         (trans.ov_amount + trans.ov_gst + trans.ov_freight 
262                         + trans.ov_freight_tax + trans.ov_discount)     AS TotalAmount,
263                 trans.alloc AS Allocated,
264                 ((trans.type = ".ST_SALESINVOICE.")
265                         AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
266         FROM "
267                         .TB_PREF."debtor_trans as trans, "
268                         .TB_PREF."debtors_master as debtor
269         WHERE debtor.debtor_no = trans.debtor_no
270                         AND (trans.ov_amount + trans.ov_gst + trans.ov_freight 
271                                 + trans.ov_freight_tax + trans.ov_discount != 0)
272                 AND trans.tran_date >= '$data_after'
273                 AND trans.tran_date <= '$date_to'";
274
275         if ($_POST['customer_id'] != ALL_TEXT)
276                 $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
277
278         if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
279         {
280                 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
281                 {
282                         $sql .= " AND trans.type = ".ST_SALESINVOICE." ";
283                 }
284                 elseif ($_POST['filterType'] == '3')
285                 {
286                         $sql .= " AND trans.type = " . ST_CUSTPAYMENT;
287                 }
288                 elseif ($_POST['filterType'] == '4')
289                 {
290                         $sql .= " AND trans.type = ".ST_CUSTCREDIT." ";
291                 }
292
293         if ($_POST['filterType'] == '2')
294         {
295                 $today =  date2sql(Today());
296                 $sql .= " AND trans.due_date < '$today'
297                                 AND (round(abs(trans.ov_amount + "
298                                 ."trans.ov_gst + trans.ov_freight + "
299                                 ."trans.ov_freight_tax + trans.ov_discount) - trans.alloc,6) > 0) ";
300         }
301         }
302         else
303         {
304             $sql .= " AND trans.type <> ".ST_CUSTDELIVERY." ";
305         }
306
307
308         if (!check_value('showSettled'))
309         {
310                 $sql .= " AND (round(IF(trans.prep_amount,trans.prep_amount, abs(trans.ov_amount + trans.ov_gst + "
311                 ."trans.ov_freight + trans.ov_freight_tax + "
312                 ."trans.ov_discount)) - trans.alloc,6) != 0) ";
313         }
314         return $sql;
315 }
316
317 function credit_sales_invoice_allocate($invoice_no, $credit_no, $amount, $date)
318 {
319
320         $sql = "SELECT ov_freight+ov_gst+ov_amount+ov_freight_tax as total, alloc FROM ".TB_PREF."debtor_trans
321                 WHERE (`type`=".ST_SALESINVOICE." AND trans_no=".db_escape($invoice_no).")";
322         $result = db_query($sql, "can't retrieve invoice totals");
323         $invoice = db_fetch($result);
324         $free = $invoice['total'] - $invoice['alloc'];
325         
326         if ($free < $amount) {
327          // if there is not enough unallocated amount - remove some payment allocations
328                 $sql = "SELECT * FROM ".TB_PREF."cust_allocations
329                         WHERE (trans_type_to=".ST_SALESINVOICE." AND trans_no_to=".db_escape($invoice_no).")
330                         AND trans_type_from <> ".ST_CUSTCREDIT;
331                 $result = db_query($sql, "can't retrieve invoice allocations");
332
333                 while(($free < $amount) && ($alloc = db_fetch($result))) {
334                         $unalloc = min($alloc['amt'], $amount-$free);
335                         update_debtor_trans_allocation($alloc['trans_type_to'], $alloc['trans_no_to'], 
336                                 -$unalloc);
337                         update_debtor_trans_allocation($alloc['trans_type_from'], $alloc['trans_no_from'], 
338                                 -$unalloc);
339
340                         delete_cust_allocation($alloc['id']);
341                         if ($unalloc < $alloc['amt'])
342                                 add_cust_allocation($alloc['amt']-$unalloc, $alloc['trans_type_from'],
343                                         $alloc['trans_no_from'], ST_SALESINVOICE, $invoice_no);
344
345                         $free += $unalloc;
346                 }
347         }
348         if (floatcmp($free, $amount)<0) {
349                 // this should never happen unless sparse credit notices were allocated to 
350                 // the invoice, or summarized freight costs on credit notes is more than those on invoice.
351                 display_error(_("Unsuspected overallocation happened due to sparse credit notes exists for this invoice.
352  Check all credit notes allocated to this invoice for summarized freight charges."));
353                 return false;
354         }
355         update_debtor_trans_allocation(ST_SALESINVOICE, $invoice_no, $amount);
356         update_debtor_trans_allocation(ST_CUSTCREDIT, $credit_no, $amount);
357         add_cust_allocation($amount, ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no);
358
359         exchange_variation(ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no, $date,
360                 $amount, PT_CUSTOMER);
361         return true;
362 }
363
364 ?>