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 update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
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");
44 //-------------------------------------------------------------------------------------------------------------
46 function void_cust_allocations($type, $type_no, $date="")
48 return clear_cust_alloctions($type, $type_no, $date);
51 //-------------------------------------------------------------------------------------------------------------
53 function clear_cust_alloctions($type, $type_no, $date="")
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");
61 while ($row = db_fetch($result))
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
69 exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
70 $row['amt'], PT_CUSTOMER, true);
71 //////////////////////
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).")";
80 db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
82 //----------------------------------------------------------------------------------------
84 function get_alloc_trans_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
91 debtor.name AS DebtorName,
93 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
100 $sql .= ", $extra_fields ";
102 $sql .= " FROM ".TB_PREF."debtor_trans as trans, "
103 .TB_PREF."debtors_master as debtor";
105 $sql .= ",$extra_tables ";
107 $sql .= " WHERE trans.debtor_no=debtor.debtor_no";
109 if ($extra_conditions)
110 $sql .= " AND $extra_conditions ";
116 //-------------------------------------------------------------------------------------------------------------
118 function get_allocatable_from_cust_sql($customer_id, $settled)
123 $settled_sql = " AND (round(ov_amount+ov_gst+ov_freight+ov_freight_tax-ov_discount-alloc,6) > 0)";
126 if ($customer_id != null)
127 $cust_sql = " AND trans.debtor_no = ".db_escape($customer_id);
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);
135 //-------------------------------------------------------------------------------------------------------------
137 function get_allocatable_to_cust_transactions($customer_id, $trans_no=null, $type=null)
139 if ($trans_no != null and $type != null)
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");
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));
158 return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
161 function get_sql_for_customer_allocation_inquiry()
163 $data_after = date2sql($_POST['TransAfterDate']);
164 $date_to = date2sql($_POST['TransToDate']);
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
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'";
189 if ($_POST['customer_id'] != ALL_TEXT)
190 $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
192 if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
194 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
196 $sql .= " AND trans.type = ".ST_SALESINVOICE." ";
198 elseif ($_POST['filterType'] == '3')
200 $sql .= " AND trans.type = " . ST_CUSTPAYMENT;
202 elseif ($_POST['filterType'] == '4')
204 $sql .= " AND trans.type = ".ST_CUSTCREDIT." ";
207 if ($_POST['filterType'] == '2')
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) ";
218 $sql .= " AND trans.type <> ".ST_CUSTDELIVERY." ";
222 if (!check_value('showSettled'))
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) ";
231 function credit_sales_invoice_allocate($invoice_no, $credit_no, $amount, $date)
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'];
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");
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'],
251 update_debtor_trans_allocation($alloc['trans_type_from'], $alloc['trans_no_from'],
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);
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."));
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);
273 exchange_variation(ST_CUSTCREDIT, $credit_no, ST_SALESINVOICE, $invoice_no, $date,
274 $amount, PT_CUSTOMER);