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 //----------------------------------------------------------------------------------------
14 function add_cust_allocation($amount, $trans_type_from, $trans_no_from,
15 $trans_type_to, $trans_no_to)
17 $sql = "INSERT INTO ".TB_PREF."cust_allocations (
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).")";
23 db_query($sql, "A customer allocation could not be added to the database");
26 //----------------------------------------------------------------------------------------
29 function delete_cust_allocation($trans_id)
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");
35 //----------------------------------------------------------------------------------------
37 function get_cust_allocation($trans_id)
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");
43 //----------------------------------------------------------------------------------------
45 function update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
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);
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");
56 //-------------------------------------------------------------------------------------------------------------
58 function void_cust_allocations($type, $type_no, $date="")
60 return clear_cust_alloctions($type, $type_no, $date);
63 //-------------------------------------------------------------------------------------------------------------
65 function clear_cust_alloctions($type, $type_no, $date="")
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");
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).")";
83 db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
86 // exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
87 // $row['amt'], PT_CUSTOMER, true);
89 //-------------------------------------------------------------------------------------------------------------
91 function get_allocatable_from_cust_sql($customer_id=null, $settled)
98 debtor.name AS DebtorName,
100 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
105 round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc),6) <= 0 AS settled
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))";
115 $sql .= " AND (round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax-ov_discount-alloc),6) > 0)";
117 if ($customer_id != null)
118 $sql .= " AND trans.debtor_no = ".db_escape($customer_id);
123 function get_allocatable_sales_orders($customer_id = null, $trans_no=null, $type=null)
126 sorder.trans_type as type,
127 sorder.order_no as trans_no,
130 debtor.name AS DebtorName,
132 total-IFNULL(invoiced.amount,0) as Total,
134 sorder.delivery_date as due_date,
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;
145 if ($trans_no != null and $type != null)
147 $sql .= " AND alloc.trans_no_from=".db_escape($trans_no)."
148 AND alloc.trans_type_from=".db_escape($type);
152 $sql .= " AND round(sorder.prep_amount)>0 and Total>0"; // only sales orders with prepayment level set and no yet fully invoiced
155 $sql .= " AND sorder.debtor_no=".db_escape($customer_id);
157 $sql .= " GROUP BY sorder.order_no";
161 //-------------------------------------------------------------------------------------------------------------
163 function get_allocatable_to_cust_transactions($customer_id = null, $trans_no=null, $type=null)
170 debtor.name AS DebtorName,
172 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
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
182 trans.debtor_no=debtor.debtor_no";
184 $sql .= " AND trans.debtor_no=".db_escape($customer_id);
186 if ($trans_no != null and $type != null)
188 $sql .= " AND alloc.trans_no_from=".db_escape($trans_no)."
189 AND alloc.trans_type_from=".db_escape($type);
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";
197 $orders = get_allocatable_sales_orders($customer_id, $trans_no, $type);
198 $sql = "($sql ORDER BY trans_no) \nUNION \n($orders)";
200 return db_query($sql, "Cannot retreive alloc to transactions");
203 //-------------------------------------------------------------------------------------------------------------
205 function get_allocatable_from_cust_transactions($customer_id, $trans_no=null, $type=null)
213 debtor.name AS DebtorName,
215 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
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";
228 if ($trans_no != null and $type != null)
230 $sql .= " AND alloc.trans_no_to=".db_escape($trans_no)."
231 AND alloc.trans_type_to=".db_escape($type);
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";
241 $sql .= " AND trans.debtor_no=".db_escape($customer_id);
243 return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
246 function get_sql_for_customer_allocation_inquiry()
248 $data_after = date2sql($_POST['TransAfterDate']);
249 $date_to = date2sql($_POST['TransToDate']);
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
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'";
275 if ($_POST['customer_id'] != ALL_TEXT)
276 $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
278 if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
280 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
282 $sql .= " AND trans.type = ".ST_SALESINVOICE." ";
284 elseif ($_POST['filterType'] == '3')
286 $sql .= " AND trans.type = " . ST_CUSTPAYMENT;
288 elseif ($_POST['filterType'] == '4')
290 $sql .= " AND trans.type = ".ST_CUSTCREDIT." ";
293 if ($_POST['filterType'] == '2')
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) ";
304 $sql .= " AND trans.type <> ".ST_CUSTDELIVERY." ";
308 if (!check_value('showSettled'))
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) ";
317 function credit_sales_invoice_allocate($invoice_no, $credit_no, $amount, $date)
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'];
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");
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'],
337 update_debtor_trans_allocation($alloc['trans_type_from'], $alloc['trans_no_from'],
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);
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."));
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);
359 exchange_variation(ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no, $date,
360 $amount, PT_CUSTOMER);