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 // Base function for adding a GL transaction
15 // $date_ is display date (non-sql)
16 // $amount is in $currency currency
17 // if $currency is not set, then defaults to no conversion
19 function add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
20 $amount, $currency=null, $person_type_id=null, $person_id=null, $err_msg="", $rate=0)
24 $date = date2sql($date_);
25 if ($currency != null)
28 $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
30 $amount_in_home_currency = round2($amount * $rate, user_price_dec());
33 $amount_in_home_currency = round2($amount, user_price_dec());
34 if ($dimension == null || $dimension < 0)
36 if ($dimension2 == null || $dimension2 < 0)
38 if (isset($SysPrefs->use_audit_trail) && $SysPrefs->use_audit_trail)
40 if ($memo_ == "" || $memo_ == null)
41 $memo_ = $_SESSION["wa_current_user"]->username;
43 $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
45 if (!is_subledger_account($account) || $account==get_company_pref('grn_clearing_act'))
46 $person_id = $person_type_id = null;
48 $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
49 account, dimension_id, dimension2_id, memo_, amount";
51 if ($person_type_id != null)
52 $sql .= ", person_type_id, person_id";
56 $sql .= "VALUES (".db_escape($type).", ".db_escape($trans_id).", '$date',
57 ".db_escape($account).", ".db_escape($dimension).", "
58 .db_escape($dimension2).", ".db_escape($memo_).", "
59 .db_escape($amount_in_home_currency);
61 if ($person_type_id != null)
62 $sql .= ", ".db_escape($person_type_id).", ". db_escape($person_id);
67 $err_msg = "The GL transaction could not be inserted";
69 db_query($sql, $err_msg);
70 return $amount_in_home_currency;
73 //--------------------------------------------------------------------------------
75 // GL Trans for standard costing, always home currency regardless of person
76 // $date_ is display date (non-sql)
77 // $amount is in HOME currency
79 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
80 $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
84 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
85 $amount, null, $person_type_id, $person_id, $err_msg);
90 // Function for even out rounding problems
91 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
93 $amount = round2($amount, user_price_dec());
95 if (floatcmp($amount, 0))
97 error_log(sprintf( _("Rounding error %s encountered for trans_type:%s,trans_no:%s"), $amount, $type, $trans_id));
98 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
99 $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
104 //--------------------------------------------------------------------------------
106 function get_gl_transactions($from_date, $to_date, $trans_no=0,
107 $account=null, $dimension=0, $dimension2=0, $filter_type=null,
108 $amount_min=null, $amount_max=null, $person_type=null, $person_id=null, $memo='')
112 $from = date2sql($from_date);
113 $to = date2sql($to_date);
115 $sql = "SELECT gl.*, j.event_date, j.doc_date, a.gl_seq, u.user_id, st.supp_reference, gl.person_id subcode,
116 IFNULL(IFNULL(sup.supp_name, debt.name), bt.person_id) as person_name,
117 IFNULL(gl.person_id, IFNULL(sup.supplier_id, IFNULL(debt.debtor_no, bt.person_id))) as person_id,
118 IF(gl.person_id, gl.person_type_id, IF(sup.supplier_id,". PT_SUPPLIER . "," . "IF(debt.debtor_no," . PT_CUSTOMER . "," .
119 "IF(bt.person_id != '' AND !ISNULL(bt.person_id), bt.person_type_id, -1)))) as person_type_id,
120 IFNULL(st.tran_date, IFNULL(dt.tran_date, IFNULL(bt.trans_date, IFNULL(grn.delivery_date, gl.tran_date)))) as doc_date,
121 coa.account_name, ref.reference, IF(ISNULL(c.memo_), gl.memo_, CONCAT(gl.memo_,' ',c.memo_)) AS memo
123 .TB_PREF."gl_trans gl
124 LEFT JOIN ".TB_PREF."voided v ON gl.type_no=v.id AND v.type=gl.type
126 LEFT JOIN ".TB_PREF."supp_trans st ON gl.type_no=st.trans_no AND st.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=st.supplier_id)
127 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=gl.type_no AND gl.type=".ST_SUPPRECEIVE."
128 LEFT JOIN ".TB_PREF."debtor_trans dt ON gl.type_no=dt.trans_no AND dt.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=dt.debtor_no)
130 LEFT JOIN ".TB_PREF."suppliers sup ON st.supplier_id=sup.supplier_id
131 LEFT JOIN ".TB_PREF."cust_branch branch ON dt.branch_code=branch.branch_code
132 LEFT JOIN ".TB_PREF."debtors_master debt ON dt.debtor_no=debt.debtor_no
134 LEFT JOIN ".TB_PREF."bank_trans bt ON bt.type=gl.type AND bt.trans_no=gl.type_no AND bt.amount!=0
135 AND (bt.person_id != '' AND !ISNULL(bt.person_id))
137 LEFT JOIN ".TB_PREF."journal j ON j.type=gl.type AND j.trans_no=gl.type_no
138 LEFT JOIN ".TB_PREF."audit_trail a ON a.type=gl.type AND a.trans_no=gl.type_no AND NOT ISNULL(gl_seq)
139 LEFT JOIN ".TB_PREF."users u ON a.user=u.id
140 LEFT JOIN ".TB_PREF."comments c ON c.id=gl.type_no AND c.type=gl.type
142 LEFT JOIN ".TB_PREF."refs ref ON ref.type=gl.type AND ref.id=gl.type_no,"
143 .TB_PREF."chart_master coa
144 WHERE coa.account_code=gl.account
146 AND gl.tran_date >= '$from'
147 AND gl.tran_date <= '$to'
151 $sql .= " AND gl.type_no LIKE ".db_escape('%'.$trans_no);
153 if ($account != null)
154 $sql .= " AND gl.account = ".db_escape($account);
157 $sql .= " AND gl.dimension_id = ".($dimension<0 ? 0 : db_escape($dimension));
159 if ($dimension2 != 0)
160 $sql .= " AND gl.dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2));
162 if ($filter_type != null)
163 $sql .= " AND gl.type IN (" . $filter_type .")";
165 if ($amount_min != null)
166 $sql .= " AND ABS(gl.amount) >= ABS(".db_escape($amount_min).")";
168 if ($amount_max != null)
169 $sql .= " AND ABS(gl.amount) <= ABS(".db_escape($amount_max).")";
172 $sql .= " AND (gl.memo_ LIKE ". db_escape("%$memo%") . " OR c.memo_ LIKE " . db_escape("%$memo%") . ")";
174 $sql .= " GROUP BY counter";
176 $sql .= " HAVING TRUE";
177 if ($person_type != 0)
178 $sql .= " AND person_type_id=".db_escape($person_type);
180 $sql .= " AND person_id=".db_escape($person_id);
182 $sql .= " ORDER BY tran_date, counter";
184 return db_query($sql, "The transactions for could not be retrieved");
187 //--------------------------------------------------------------------------------
189 function get_gl_trans($type, $trans_id)
191 $sql = "SELECT gl.*, cm.account_name, IFNULL(refs.reference, '') AS reference, user.real_name,
192 COALESCE(st.tran_date, dt.tran_date, bt.trans_date, grn.delivery_date, gl.tran_date) as doc_date,
193 IF(ISNULL(st.supp_reference), '', st.supp_reference) AS supp_reference
194 FROM ".TB_PREF."gl_trans as gl
195 LEFT JOIN ".TB_PREF."chart_master as cm ON gl.account = cm.account_code
196 LEFT JOIN ".TB_PREF."refs as refs ON (gl.type=refs.type AND gl.type_no=refs.id)
197 LEFT JOIN ".TB_PREF."audit_trail as audit ON (gl.type=audit.type AND gl.type_no=audit.trans_no AND NOT ISNULL(gl_seq))
198 LEFT JOIN ".TB_PREF."users as user ON (audit.user=user.id)
199 # all this below just to retrieve doc_date :>
200 LEFT JOIN ".TB_PREF."supp_trans st ON gl.type_no=st.trans_no AND st.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=st.supplier_id)
201 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=gl.type_no AND gl.type=".ST_SUPPRECEIVE." AND gl.person_id=grn.supplier_id
202 LEFT JOIN ".TB_PREF."debtor_trans dt ON gl.type_no=dt.trans_no AND dt.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=dt.debtor_no)
203 LEFT JOIN ".TB_PREF."bank_trans bt ON bt.type=gl.type AND bt.trans_no=gl.type_no AND bt.amount!=0
204 AND bt.person_type_id=gl.person_type_id AND bt.person_id=gl.person_id
205 LEFT JOIN ".TB_PREF."journal j ON j.type=gl.type AND j.trans_no=gl.type_no"
207 ." WHERE gl.type= ".db_escape($type)
208 ." AND gl.type_no = ".db_escape($trans_id)
209 ." AND gl.amount <> 0"
210 ." ORDER BY tran_date, counter";
212 return db_query($sql, "The gl transactions could not be retrieved");
215 //--------------------------------------------------------------------------------
217 function get_gl_wo_cost_trans($trans_id, $cost_type=-1, $all_gl=false)
219 $sql = "SELECT costing.*, gl.*, chart.account_name, com.memo_
221 .TB_PREF."wo_costing costing, "
222 .TB_PREF."gl_trans gl LEFT JOIN ".TB_PREF."comments com ON gl.type=com.type AND gl.type_no=com.id,"
223 .TB_PREF."chart_master chart
225 costing.workorder_id=".db_escape($trans_id)
226 ." AND chart.account_code=gl.account
227 AND gl.type=costing.trans_type
228 AND gl.type_no=costing.trans_no";
229 if ($cost_type != -1)
230 $sql .= " AND costing.cost_type=".db_escape($cost_type);
231 $sql .= $all_gl ? " AND amount != 0" : " AND amount < 0";
233 return db_query($sql, "The gl transactions could not be retrieved");
236 function get_gl_wo_issue_trans($trans_id, $person_id=-1, $all_gl=false)
238 $sql = "SELECT issue.*, gl.*, chart.account_name, com.memo_
240 .TB_PREF."wo_issues issue,"
241 .TB_PREF."gl_trans gl LEFT JOIN ".TB_PREF."comments com ON gl.type=com.type AND gl.type_no=com.id,"
242 .TB_PREF."chart_master chart
243 WHERE issue.workorder_id=".db_escape($trans_id)
244 ." AND chart.account_code=gl.account
245 AND gl.type=".ST_MANUISSUE." AND gl.type_no=issue.issue_no";
246 // ." AND gl.person_type_id=".PT_WORKORDER;
247 if ($person_id != -1)
248 $sql .= " AND gl.person_id=".db_escape($person_id);
249 $sql .= $all_gl ? " AND amount != 0" : " AND amount < 0";
250 $sql .= " ORDER BY type, type_no";
251 return db_query($sql, "The gl transactions could not be retrieved");
254 function get_gl_wo_productions($trans_id, $all_gl=false)
256 $sql = "SELECT rcv.*, gl.*, chart.account_name, com.memo_
258 .TB_PREF."wo_manufacture rcv, "
259 .TB_PREF."gl_trans gl LEFT JOIN ".TB_PREF."comments com ON gl.type=com.type AND gl.type_no=com.id,"
260 .TB_PREF."chart_master chart
262 rcv.workorder_id=".db_escape($trans_id)
263 ." AND chart.account_code=gl.account
264 AND gl.type=".ST_MANURECEIVE."
265 AND gl.type_no=rcv.id
267 .($all_gl ? " AND amount != 0" : " AND amount < 0")
268 ." ORDER BY type, type_no";
270 return db_query($sql, "The gl transactions could not be retrieved");
273 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
275 $from = date2sql($from_date);
276 $to = date2sql($to_date);
278 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
279 WHERE account='$account'";
280 if ($from_date != "")
281 $sql .= " AND tran_date > '$from'";
283 $sql .= " AND tran_date < '$to'";
285 $sql .= " AND dimension_id = ".($dimension<0 ? 0 : db_escape($dimension));
286 if ($dimension2 != 0)
287 $sql .= " AND dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2));
289 $result = db_query($sql, "The starting balance for account $account could not be calculated");
291 $row = db_fetch_row($result);
292 return round2($row[0], user_price_dec());
295 //--------------------------------------------------------------------------------
297 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
299 $from = date2sql($from_date);
300 $to = date2sql($to_date);
302 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
303 WHERE account='$account'";
304 if ($from_date != "")
305 $sql .= " AND tran_date >= '$from'";
307 $sql .= " AND tran_date <= '$to'";
309 $sql .= " AND dimension_id = ".($dimension<0 ? 0 : db_escape($dimension));
310 if ($dimension2 != 0)
311 $sql .= " AND dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2));
313 $result = db_query($sql, "Transactions for account $account could not be calculated");
315 $row = db_fetch_row($result);
316 return round2($row[0], user_price_dec());
319 //----------------------------------------------------------------------------------------------------
320 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
322 $from_date = date2sql($from);
323 $to_date = date2sql($to);
325 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit,
326 SUM(IF(amount < 0, -amount, 0)) as credit,
327 SUM(amount) as balance
328 FROM ".TB_PREF."gl_trans trans,"
329 .TB_PREF."chart_master coa,"
330 .TB_PREF."chart_types act_type, "
331 .TB_PREF."chart_class act_class
332 WHERE trans.account=coa.account_code
333 AND coa.account_type=act_type.id
334 AND act_type.class_id=act_class.cid"
335 ." AND ".($from_incl ? "tran_date >= '$from_date'" : "tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date')")
336 ." AND ".($to_incl ? "tran_date <= '$to_date'" : "tran_date < '$to_date'")
337 .($account == null ? '' : " AND account=".db_escape($account))
338 .($dimension == 0 ? '' : " AND dimension_id = ".($dimension<0 ? 0 : db_escape($dimension)))
339 .($dimension2 == 0 ? '' : " AND dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2)));
341 $result = db_query($sql,"No general ledger accounts were returned");
343 return db_fetch($result);
346 //--------------------------------------------------------------------------------
348 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
351 $from = date2sql($from_date);
352 $to = date2sql($to_date);
354 $sql = "SELECT SUM(amount)
355 FROM ".TB_PREF."budget_trans
356 WHERE account=".db_escape($account);
357 if ($from_date != "")
358 $sql .= " AND tran_date >= '$from' ";
360 $sql .= " AND tran_date <= '$to' ";
362 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
363 if ($dimension2 != 0)
364 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
365 $result = db_query($sql,"No budget accounts were returned");
367 $row = db_fetch_row($result);
370 //-------------------------------------------------------------------------------------
372 function exists_gl_budget($date_, $account, $dimension, $dimension2)
374 $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
375 ." AND tran_date='$date_' AND
376 dimension_id=".db_escape($dimension)." AND dimension2_id=".db_escape($dimension2);
377 $result = db_query($sql, "Cannot retreive a gl transaction");
379 return (db_num_rows($result) > 0);
382 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
384 $date = date2sql($date_);
386 if (exists_gl_budget($date, $account, $dimension, $dimension2))
387 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=".db_escape($amount)
388 ." WHERE account=".db_escape($account)
389 ." AND dimension_id=".db_escape($dimension)
390 ." AND dimension2_id=".db_escape($dimension2)
391 ." AND tran_date='$date'";
393 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
394 account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
395 ".db_escape($account).", ".db_escape($dimension).", "
396 .db_escape($dimension2).", ".db_escape($amount).", '')";
398 db_query($sql, "The GL budget transaction could not be saved");
401 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
403 $date = date2sql($date_);
405 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
406 ." AND dimension_id=".db_escape($dimension)
407 ." AND dimension2_id=".db_escape($dimension2)
408 ." AND tran_date='$date'";
409 db_query($sql, "The GL budget transaction could not be deleted");
412 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
415 $from = date2sql($from_date);
416 $to = date2sql($to_date);
418 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
419 WHERE account=".db_escape($account)
420 ." AND tran_date >= '$from' AND tran_date <= '$to'
421 AND dimension_id = ".db_escape($dimension)
422 ." AND dimension2_id = ".db_escape($dimension2);
423 $result = db_query($sql,"No budget accounts were returned");
425 $row = db_fetch_row($result);
429 //--------------------------------------------------------------------------------
430 // Stores journal/bank transaction tax details if applicable
432 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
434 $tax_type = is_tax_account($gl_code);
435 if(!$tax_type) return; // $gl_code is not tax account
437 $tax = get_tax_type($tax_type);
438 if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
440 // we have to restore net amount as we cannot know the base amount
441 if ($net_amount===null) {
442 if ($tax['rate'] == 0) {
445 // calculate net amount
446 $net_amount = $amount/$tax['rate']*100;
449 add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included,
450 $amount, $net_amount, $ex_rate, $date, $memo, null);
454 //--------------------------------------------------------------------------------
456 // Store transaction tax details for fiscal purposes with 'freezed'
457 // actual tax type rate.
459 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
460 $amount, $net_amount, $ex_rate, $tran_date, $memo, $reg_type=null)
462 // guess tax register if not set
463 if (!isset($reg_type))
464 $reg_type = in_array($trans_type, array(ST_SUPPINVOICE, ST_SUPPCREDIT)) ? TR_OUTPUT
465 : (in_array($trans_type, array(ST_SALESINVOICE, ST_CUSTCREDIT)) ? TR_INPUT : null);
467 $sql = "INSERT INTO ".TB_PREF."trans_tax_details
468 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
469 included_in_price, net_amount, amount, memo, reg_type)
470 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
471 .date2sql($tran_date)."',"
474 .(float)($ex_rate).",".($included ? 1:0).","
475 .db_escape($net_amount).","
476 .db_escape($amount).",".db_escape($memo).",".db_escape($reg_type, true).")";
478 db_query($sql, "Cannot save trans tax details");
481 //----------------------------------------------------------------------------------------
483 function get_trans_tax_details($trans_type, $trans_no)
485 $sql = "SELECT tax_details.*, tax_type.name AS tax_type_name, tax_type.rate AS rate
486 FROM ".TB_PREF."trans_tax_details tax_details INNER JOIN
487 ".TB_PREF."tax_types tax_type ON tax_type.id = tax_details.tax_type_id
489 trans_type = ".db_escape($trans_type)."
490 AND trans_no = ".db_escape($trans_no)."
491 AND (net_amount != 0 OR amount != 0)";
493 return db_query($sql, "The transaction tax details could not be retrieved");
496 //----------------------------------------------------------------------------------------
498 function void_trans_tax_details($type, $type_no)
500 $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
501 WHERE trans_no=".db_escape($type_no)
502 ." AND trans_type=".db_escape($type);
504 db_query($sql, "The transaction tax details could not be voided");
507 //----------------------------------------------------------------------------------------
509 function clear_trans_tax_details($type, $type_no)
511 $sql = "DELETE FROM ".TB_PREF."trans_tax_details
512 WHERE trans_no=".db_escape($type_no)
513 ." AND trans_type=".db_escape($type);
515 db_query($sql, "The transaction tax details could not be cleared");
518 function get_tax_summary($from, $to, $also_zero_purchases=false)
520 $fromdate = date2sql($from);
521 $todate = date2sql($to);
524 SUM(IF(trans_type=".ST_CUSTCREDIT.",-1,1)*
525 IF((reg_type=".TR_OUTPUT.")"
526 ." || ((trans_type IN(".ST_SALESINVOICE.",".ST_CUSTCREDIT.") OR (trans_type=".ST_JOURNAL." AND reg_type=".TR_INPUT."))
527 ), net_amount*ex_rate,0)
530 SUM(IF(trans_type=".ST_CUSTCREDIT.",-1,1)*
531 IF((reg_type=".TR_OUTPUT.")"
532 ." || ((trans_type IN(".ST_SALESINVOICE.",".ST_CUSTCREDIT.") OR (trans_type=".ST_JOURNAL." AND reg_type=".TR_INPUT."))
533 ), amount*ex_rate,0)) payable,
535 SUM(IF(trans_type IN(".ST_SUPPCREDIT."),-1,1)*
536 IF(reg_type=".TR_INPUT
537 . ($also_zero_purchases ? '': " AND tax_type_id AND taxrec.rate")
538 .", net_amount*ex_rate, 0)) net_input,
540 SUM(IF(trans_type IN(".ST_SUPPCREDIT."),-1,1)*
541 IF(reg_type=".TR_INPUT
542 . ($also_zero_purchases ? '': " AND tax_type_id AND taxrec.rate ")
543 .", amount*ex_rate, 0)) collectible,
547 FROM ".TB_PREF."trans_tax_details taxrec LEFT JOIN ".TB_PREF."tax_types ttype ON taxrec.tax_type_id=ttype.id
548 WHERE taxrec.trans_type IN (".implode(',',
549 array(ST_SALESINVOICE, ST_CUSTCREDIT, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_JOURNAL)).")
550 AND taxrec.tran_date >= '$fromdate'
551 AND taxrec.tran_date <= '$todate'
554 // display_error($sql);
555 return db_query($sql,"Cannot retrieve tax summary");
558 //--------------------------------------------------------------------------------------------------
560 function exists_gl_trans($type, $trans_id)
562 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
563 ." AND type_no=".db_escape($trans_id);
564 $result = db_query($sql, "Cannot retreive a gl transaction");
566 return (db_num_rows($result) > 0);
569 //--------------------------------------------------------------------------------------------------
571 function void_gl_trans($type, $trans_id, $nested=false)
576 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
577 ." AND type_no=".db_escape($trans_id);
579 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
582 commit_transaction();
585 //----------------------------------------------------------------------------------------
587 function clear_gl_trans($type, $trans_id, $nested=false)
592 $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
593 ." AND type_no=".db_escape($trans_id);
595 db_query($sql, "could not clear gl transactions for type=$type and trans_no=$trans_id");
598 commit_transaction();
601 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false,
604 $sql = "SELECT IFNULL(a.gl_seq,0) as gl_seq,
606 gl.type as trans_type,
607 gl.type_no as trans_no,
608 IF(MAX(gl.person_id), MAX(gl.person_id), IFNULL(st.supplier_id, IFNULL(grn.supplier_id, IFNULL(dt.debtor_no, bt.person_id)))) as person_id,
609 IF(ISNULL(st.supp_reference), '', st.supp_reference) AS supp_reference,
611 IF(gl.type=".ST_BANKTRANSFER.",MAX(gl.amount),SUM(IF(gl.amount>0, gl.amount,0))) as amount,
613 IF(ISNULL(u.user_id),'',u.user_id) as user_id,
614 IF(MAX(gl.person_id), MAX(gl.person_type_id), IF(!ISNULL(st.supplier_id) OR !ISNULL(grn.supplier_id),". PT_SUPPLIER . "," . "IF(dt.debtor_no," . PT_CUSTOMER . "," .
615 "IF(bt.person_id != '' AND !ISNULL(bt.person_id), bt.person_type_id, -1)))) as person_type_id
616 FROM ".TB_PREF."gl_trans as gl
617 LEFT JOIN ".TB_PREF."audit_trail as a ON
618 (gl.type=a.type AND gl.type_no=a.trans_no)
619 LEFT JOIN ".TB_PREF."comments as com ON
620 (gl.type=com.type AND gl.type_no=com.id)
621 LEFT JOIN ".TB_PREF."refs as refs ON
622 (gl.type=refs.type AND gl.type_no=refs.id)
623 LEFT JOIN ".TB_PREF."users as u ON
625 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=gl.type_no AND gl.type=".ST_SUPPRECEIVE."
626 LEFT JOIN ".TB_PREF."bank_trans bt ON bt.type=gl.type AND bt.trans_no=gl.type_no AND bt.amount!=0
627 AND (bt.person_id != '' AND !ISNULL(bt.person_id))
628 LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=gl.type AND gl.type_no=dt.trans_no AND (gl.type_no != 0 or gl.person_id=dt.debtor_no) AND gl.person_type_id=2
629 LEFT JOIN ".TB_PREF."supp_trans st ON st.type=gl.type AND gl.type_no=st.trans_no AND (gl.type !=0 or gl.person_id=st.supplier_id) AND gl.person_type_id=3
630 WHERE gl.tran_date >= '" . date2sql($from) . "'
631 AND gl.tran_date <= '" . date2sql($to) . "'
634 $sql .= " AND refs.reference LIKE ". db_escape("%$ref%");
637 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
640 $sql .= " AND gl.type=".db_escape($filter);
643 $sql .= " AND gl_seq=0";
646 $sql .= " AND NOT ISNULL(a.gl_seq)";
648 if ($user_id != null)
649 $sql .= " AND user_id = ".db_escape($user_id);
651 $sql .= " GROUP BY gl.tran_date, a.gl_seq, gl.type, gl.type_no";