New files from unstable branch
[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 update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
38 {
39         $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc = alloc + $alloc
40                 WHERE type=".db_escape($trans_type)." AND trans_no = ".db_escape($trans_no);
41         db_query($sql, "The debtor transaction record could not be modified for the allocation against it");
42 }
43
44 //-------------------------------------------------------------------------------------------------------------
45
46 function void_cust_allocations($type, $type_no, $date="")
47 {
48         return clear_cust_alloctions($type, $type_no, $date);
49 }
50
51 //-------------------------------------------------------------------------------------------------------------
52
53 function clear_cust_alloctions($type, $type_no, $date="")
54 {
55         // clear any allocations for this transaction
56         $sql = "SELECT * FROM ".TB_PREF."cust_allocations
57                 WHERE (trans_type_from=".db_escape($type)." AND trans_no_from=".db_escape($type_no).")
58                 OR (trans_type_to=".db_escape($type)." AND trans_no_to=".db_escape($type_no).")";
59         $result = db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
60
61         while ($row = db_fetch($result))
62         {
63                 $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc=alloc - " . $row['amt'] . "
64                         WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
65                         OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
66                 db_query($sql, "could not clear allocation");
67                 // 2008-09-20 Joe Hunt
68                 if ($date != "")
69                         exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
70                                 $row['amt'], PT_CUSTOMER, true);
71                 //////////////////////
72         }
73
74
75         // remove any allocations for this transaction
76         $sql = "DELETE FROM ".TB_PREF."cust_allocations
77                 WHERE (trans_type_from=".db_escape($type)." AND trans_no_from=".db_escape($type_no).")
78                 OR (trans_type_to=".db_escape($type)." AND trans_no_to=".db_escape($type_no).")";
79
80         db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
81 }
82 //----------------------------------------------------------------------------------------
83
84 function get_alloc_trans_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
85 {
86         $sql = "SELECT
87                 trans.type,
88                 trans.trans_no,
89                 trans.reference,
90                 trans.tran_date,
91                 debtor.name AS DebtorName, 
92                 debtor.curr_code, 
93                 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
94                 trans.alloc,
95                 trans.due_date,
96                 debtor.address,
97                 trans.version ";
98
99         if ($extra_fields)
100                 $sql .= ", $extra_fields ";
101
102         $sql .= " FROM ".TB_PREF."debtor_trans as trans, "
103                                 .TB_PREF."debtors_master as debtor";
104         if ($extra_tables)
105                 $sql .= ",$extra_tables ";
106
107         $sql .= " WHERE trans.debtor_no=debtor.debtor_no";
108
109         if ($extra_conditions)
110                 $sql .= " AND $extra_conditions ";
111
112         return $sql;
113 }
114
115
116 //-------------------------------------------------------------------------------------------------------------
117
118 function get_allocatable_from_cust_sql($customer_id, $settled)
119 {
120         $settled_sql = "";
121         if (!$settled)
122         {
123                 $settled_sql = " AND (round(ov_amount+ov_gst+ov_freight+ov_freight_tax-ov_discount-alloc,6) > 0)";
124         }
125         $cust_sql = "";
126         if ($customer_id != null)
127                 $cust_sql = " AND trans.debtor_no = ".db_escape($customer_id);
128
129         $sql = get_alloc_trans_sql("round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) <= 0 AS settled",
130                 "(type=".ST_CUSTPAYMENT." OR type=".ST_CUSTCREDIT." OR type=".ST_BANKDEPOSIT.") AND (trans.ov_amount > 0) " . $settled_sql . $cust_sql);
131
132         return $sql;
133 }
134
135 //-------------------------------------------------------------------------------------------------------------
136
137 function get_allocatable_to_cust_transactions($customer_id, $trans_no=null, $type=null)
138 {
139         if ($trans_no != null and $type != null)
140         {
141                 $sql = get_alloc_trans_sql("amt", "trans.trans_no = alloc.trans_no_to
142                         AND trans.type = alloc.trans_type_to
143                         AND alloc.trans_no_from=".db_escape($trans_no)."
144                         AND alloc.trans_type_from=".db_escape($type)."
145                         AND trans.debtor_no=".db_escape($customer_id),
146                         "".TB_PREF."cust_allocations as alloc");
147         }
148         else
149         {
150                 $sql = get_alloc_trans_sql(null, "round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) > 0
151                         AND trans.type <> " . ST_CUSTPAYMENT . "
152                         AND trans.type <> " . ST_BANKDEPOSIT . "
153                         AND trans.type <> " . ST_CUSTCREDIT . "
154                         AND trans.type <> " . ST_CUSTDELIVERY . "
155                         AND trans.debtor_no=".db_escape($customer_id));
156         }
157
158         return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
159 }
160
161 function get_sql_for_customer_allocation_inquiry()
162 {
163         $data_after = date2sql($_POST['TransAfterDate']);
164         $date_to = date2sql($_POST['TransToDate']);
165
166         $sql = "SELECT 
167                 trans.type,
168                 trans.trans_no,
169                 trans.reference,
170                 trans.order_,
171                 trans.tran_date,
172                 trans.due_date,
173                 debtor.name,
174                 debtor.curr_code,
175         (trans.ov_amount + trans.ov_gst + trans.ov_freight 
176                         + trans.ov_freight_tax + trans.ov_discount)     AS TotalAmount,
177                 trans.alloc AS Allocated,
178                 ((trans.type = ".ST_SALESINVOICE.")
179                         AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
180         FROM "
181                         .TB_PREF."debtor_trans as trans, "
182                         .TB_PREF."debtors_master as debtor
183         WHERE debtor.debtor_no = trans.debtor_no
184                         AND (trans.ov_amount + trans.ov_gst + trans.ov_freight 
185                                 + trans.ov_freight_tax + trans.ov_discount != 0)
186                 AND trans.tran_date >= '$data_after'
187                 AND trans.tran_date <= '$date_to'";
188
189         if ($_POST['customer_id'] != ALL_TEXT)
190                 $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
191
192         if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
193         {
194                 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
195                 {
196                         $sql .= " AND trans.type = ".ST_SALESINVOICE." ";
197                 }
198                 elseif ($_POST['filterType'] == '3')
199                 {
200                         $sql .= " AND trans.type = " . ST_CUSTPAYMENT;
201                 }
202                 elseif ($_POST['filterType'] == '4')
203                 {
204                         $sql .= " AND trans.type = ".ST_CUSTCREDIT." ";
205                 }
206
207         if ($_POST['filterType'] == '2')
208         {
209                 $today =  date2sql(Today());
210                 $sql .= " AND trans.due_date < '$today'
211                                 AND (round(abs(trans.ov_amount + "
212                                 ."trans.ov_gst + trans.ov_freight + "
213                                 ."trans.ov_freight_tax + trans.ov_discount) - trans.alloc,6) > 0) ";
214         }
215         }
216         else
217         {
218             $sql .= " AND trans.type <> ".ST_CUSTDELIVERY." ";
219         }
220
221
222         if (!check_value('showSettled'))
223         {
224                 $sql .= " AND (round(abs(trans.ov_amount + trans.ov_gst + "
225                 ."trans.ov_freight + trans.ov_freight_tax + "
226                 ."trans.ov_discount) - trans.alloc,6) != 0) ";
227         }
228         return $sql;
229 }
230
231 function credit_sales_invoice_allocate($invoice_no, $credit_no, $amount, $date)
232 {
233
234         $sql = "SELECT ov_freight+ov_gst+ov_amount+ov_freight_tax as total, alloc FROM ".TB_PREF."debtor_trans
235                 WHERE (`type`=".ST_SALESINVOICE." AND trans_no=".db_escape($invoice_no).")";
236         $result = db_query($sql, "can't retrieve invoice totals");
237         $invoice = db_fetch($result);
238         $free = $invoice['total'] - $invoice['alloc'];
239         
240         if ($free < $amount) {
241          // if there is not enough unallocated amount - remove some payment allocations
242                 $sql = "SELECT * FROM ".TB_PREF."cust_allocations
243                         WHERE (trans_type_to=".ST_SALESINVOICE." AND trans_no_to=".db_escape($invoice_no).")
244                         AND trans_type_from <> ".ST_CUSTCREDIT;
245                 $result = db_query($sql, "can't retrieve invoice allocations");
246
247                 while(($free < $amount) && ($alloc = db_fetch($result))) {
248                         $unalloc = min($alloc['amt'], $amount-$free);
249                         update_debtor_trans_allocation($alloc['trans_type_to'], $alloc['trans_no_to'], 
250                                 -$unalloc);
251                         update_debtor_trans_allocation($alloc['trans_type_from'], $alloc['trans_no_from'], 
252                                 -$unalloc);
253
254                         delete_cust_allocation($alloc['id']);
255                         if ($unalloc < $alloc['amt'])
256                                 add_cust_allocation($alloc['amt']-$unalloc, $alloc['trans_type_from'],
257                                         $alloc['trans_no_from'], ST_SALESINVOICE, $invoice_no);
258
259                         $free += $unalloc;
260                 }
261         }
262         if ($free < $amount) {
263                 // this should never happen unless sparse credit notices were allocated to 
264                 // the invoice, or summarized freight costs on credit notes is more than those on invoice.
265                 display_error(_("Unsuspected overallocation happened due to sparse credit notes exists for this invoice.
266  Check all credit notes allocated to this invoice for summarized freight charges."));
267                 return false;
268         }
269         update_debtor_trans_allocation(ST_SALESINVOICE, $invoice_no, $amount);
270         update_debtor_trans_allocation(ST_CUSTCREDIT, $credit_no, $amount);
271         add_cust_allocation($amount, ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no);
272
273         exchange_variation(ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no, $date,
274                 $amount, PT_CUSTOMER);
275         return true;
276 }
277
278 ?>