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)
22 global $use_audit_trail;
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($use_audit_trail) && $use_audit_trail)
40 if ($memo_ == "" || $memo_ == null)
41 $memo_ = $_SESSION["wa_current_user"]->username;
43 $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
45 $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
46 account, dimension_id, dimension2_id, memo_, amount";
48 if ($person_type_id != null)
49 $sql .= ", person_type_id, person_id";
53 $sql .= "VALUES (".db_escape($type).", ".db_escape($trans_id).", '$date',
54 ".db_escape($account).", ".db_escape($dimension).", "
55 .db_escape($dimension2).", ".db_escape($memo_).", "
56 .db_escape($amount_in_home_currency);
58 if ($person_type_id != null)
59 $sql .= ", ".db_escape($person_type_id).", ". db_escape($person_id);
64 $err_msg = "The GL transaction could not be inserted";
66 db_query($sql, $err_msg);
67 return $amount_in_home_currency;
70 //--------------------------------------------------------------------------------
72 // GL Trans for standard costing, always home currency regardless of person
73 // $date_ is display date (non-sql)
74 // $amount is in HOME currency
76 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
77 $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
80 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
81 $amount, null, $person_type_id, $person_id, $err_msg);
86 // Function for even out rounding problems
87 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
89 $amount = round2($amount, user_price_dec());
91 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
92 $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
97 //--------------------------------------------------------------------------------
99 function get_gl_transactions($from_date, $to_date, $trans_no=0,
100 $account=null, $dimension=0, $dimension2=0, $filter_type=null,
101 $amount_min=null, $amount_max=null)
103 global $show_voided_gl_trans;
105 $from = date2sql($from_date);
106 $to = date2sql($to_date);
108 $sql = "SELECT ".TB_PREF."gl_trans.*, "
109 .TB_PREF."chart_master.account_name FROM "
111 LEFT JOIN ".TB_PREF."voided v ON "
112 .TB_PREF."gl_trans.type_no=v.id AND v.type=".TB_PREF."gl_trans.type,"
113 .TB_PREF."chart_master"
114 ." WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
116 AND tran_date >= '$from'
117 AND tran_date <= '$to'";
118 if (isset($show_voided_gl_trans) && $show_voided_gl_trans == 0)
119 $sql .= " AND ".TB_PREF."gl_trans.amount <> 0";
121 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE ".db_escape('%'.$trans_no);
123 if ($account != null)
124 $sql .= " AND ".TB_PREF."gl_trans.account = ".db_escape($account);
127 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = ".($dimension<0?0:db_escape($dimension));
129 if ($dimension2 != 0)
130 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
132 if ($filter_type != null AND is_numeric($filter_type))
133 $sql .= " AND ".TB_PREF."gl_trans.type= ".db_escape($filter_type);
135 if ($amount_min != null)
136 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) >= ABS(".db_escape($amount_min).")";
138 if ($amount_max != null)
139 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) <= ABS(".db_escape($amount_max).")";
141 $sql .= " ORDER BY tran_date, counter";
143 return db_query($sql, "The transactions for could not be retrieved");
147 //--------------------------------------------------------------------------------
149 function get_gl_trans($type, $trans_id)
151 $sql = "SELECT gl.*, cm.account_name, IF(ISNULL(refs.reference), '', refs.reference) AS reference FROM "
152 .TB_PREF."gl_trans as gl
153 LEFT JOIN ".TB_PREF."chart_master as cm ON gl.account = cm.account_code
154 LEFT JOIN ".TB_PREF."refs as refs ON (gl.type=refs.type AND gl.type_no=refs.id)"
155 ." WHERE gl.type= ".db_escape($type)
156 ." AND gl.type_no = ".db_escape($trans_id)
157 ." ORDER BY counter";
158 return db_query($sql, "The gl transactions could not be retrieved");
161 //--------------------------------------------------------------------------------
163 function get_gl_wo_cost_trans($trans_id, $cost_type=-1)
165 $sql = "SELECT costing.*, gl.*, chart.account_name, com.memo_ FROM "
166 .TB_PREF."wo_costing costing, "
167 .TB_PREF."gl_trans gl LEFT JOIN ".TB_PREF."comments com ON gl.type=com.type AND gl.type_no=com.id,"
168 .TB_PREF."chart_master chart
170 costing.workorder_id=".db_escape($trans_id)
171 ." AND chart.account_code=gl.account
172 AND gl.type=costing.trans_type
173 AND gl.type_no=costing.trans_no";
174 if ($cost_type != -1)
175 $sql .= " AND costing.cost_type=".db_escape($cost_type);
176 $sql .= " AND amount < 0";
178 return db_query($sql, "The gl transactions could not be retrieved");
181 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
183 $from = date2sql($from_date);
184 $to = date2sql($to_date);
186 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
187 WHERE account='$account'";
188 if ($from_date != "")
189 $sql .= " AND tran_date > '$from'";
191 $sql .= " AND tran_date < '$to'";
193 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
194 if ($dimension2 != 0)
195 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
197 $result = db_query($sql, "The starting balance for account $account could not be calculated");
199 $row = db_fetch_row($result);
203 //--------------------------------------------------------------------------------
205 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
207 $from = date2sql($from_date);
208 $to = date2sql($to_date);
210 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
211 WHERE account='$account'";
212 if ($from_date != "")
213 $sql .= " AND tran_date >= '$from'";
215 $sql .= " AND tran_date <= '$to'";
217 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
218 if ($dimension2 != 0)
219 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
221 $result = db_query($sql, "Transactions for account $account could not be calculated");
223 $row = db_fetch_row($result);
224 return (float)$row[0];
227 //----------------------------------------------------------------------------------------------------
228 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
230 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit,
231 SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance
232 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,"
233 .TB_PREF."chart_types, ".TB_PREF."chart_class
234 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND "
235 .TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
236 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
238 if ($account != null)
239 $sql .= " account=".db_escape($account)." AND";
241 $sql .= " dimension_id = ".($dimension<0?0:db_escape($dimension))." AND";
242 if ($dimension2 != 0)
243 $sql .= " dimension2_id = ".($dimension2<0?0:db_escape($dimension2))." AND";
244 $from_date = date2sql($from);
246 $sql .= " tran_date >= '$from_date' AND";
248 $sql .= " tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date') AND";
249 $to_date = date2sql($to);
251 $sql .= " tran_date <= '$to_date' ";
253 $sql .= " tran_date < '$to_date' ";
255 $result = db_query($sql,"No general ledger accounts were returned");
257 return db_fetch($result);
260 //--------------------------------------------------------------------------------
262 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
265 $from = date2sql($from_date);
266 $to = date2sql($to_date);
268 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
269 WHERE account=".db_escape($account);
270 if ($from_date != "")
271 $sql .= " AND tran_date >= '$from' ";
273 $sql .= " AND tran_date <= '$to' ";
275 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
276 if ($dimension2 != 0)
277 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
278 $result = db_query($sql,"No budget accounts were returned");
280 $row = db_fetch_row($result);
283 //-------------------------------------------------------------------------------------
285 function exists_gl_budget($date_, $account, $dimension, $dimension2)
287 $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
288 ." AND tran_date='$date_' AND
289 dimension_id=".db_escape($dimension)." AND dimension2_id=".db_escape($dimension2);
290 $result = db_query($sql, "Cannot retreive a gl transaction");
292 return (db_num_rows($result) > 0);
295 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
297 $date = date2sql($date_);
299 if (exists_gl_budget($date, $account, $dimension, $dimension2))
300 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=".db_escape($amount)
301 ." WHERE account=".db_escape($account)
302 ." AND dimension_id=".db_escape($dimension)
303 ." AND dimension2_id=".db_escape($dimension2)
304 ." AND tran_date='$date'";
306 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
307 account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
308 ".db_escape($account).", ".db_escape($dimension).", "
309 .db_escape($dimension2).", ".db_escape($amount).", '')";
311 db_query($sql, "The GL budget transaction could not be saved");
314 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
316 $date = date2sql($date_);
318 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
319 ." AND dimension_id=".db_escape($dimension)
320 ." AND dimension2_id=".db_escape($dimension2)
321 ." AND tran_date='$date'";
322 db_query($sql, "The GL budget transaction could not be deleted");
325 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
328 $from = date2sql($from_date);
329 $to = date2sql($to_date);
331 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
332 WHERE account=".db_escape($account)
333 ." AND tran_date >= '$from' AND tran_date <= '$to'
334 AND dimension_id = ".db_escape($dimension)
335 ." AND dimension2_id = ".db_escape($dimension2);
336 $result = db_query($sql,"No budget accounts were returned");
338 $row = db_fetch_row($result);
342 //--------------------------------------------------------------------------------
343 // Stores journal/bank transaction tax details if applicable
345 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
347 $tax_type = is_tax_account($gl_code);
348 if(!$tax_type) return; // $gl_code is not tax account
350 $tax = get_tax_type($tax_type);
351 //if ($gl_code == $tax['sales_gl_code'])
352 if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
354 // we have to restore net amount as we cannot know the base amount
355 if ($net_amount===null) {
356 if ($tax['rate'] == 0) {
357 // display_warning(_("You should not post gl transactions
358 // to tax account with zero tax rate."));
361 // calculate net amount
362 $net_amount = $amount/$tax['rate']*100;
365 add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included,
366 $amount, $net_amount, $ex_rate, $date, $memo);
370 //--------------------------------------------------------------------------------
372 // Store transaction tax details for fiscal purposes with 'freezed'
373 // actual tax type rate.
375 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
376 $amount, $net_amount, $ex_rate, $tran_date, $memo)
379 $sql = "INSERT INTO ".TB_PREF."trans_tax_details
380 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
381 included_in_price, net_amount, amount, memo)
382 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
383 .date2sql($tran_date)."',".db_escape($tax_id).","
384 .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
385 .db_escape($net_amount).","
386 .db_escape($amount).",".db_escape($memo).")";
388 db_query($sql, "Cannot save trans tax details");
391 //----------------------------------------------------------------------------------------
393 function get_trans_tax_details($trans_type, $trans_no)
395 $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
396 .TB_PREF."tax_types.name AS tax_type_name, "
397 .TB_PREF."trans_tax_details.rate AS effective_rate, "
398 .TB_PREF."tax_types.rate AS rate
399 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
400 WHERE trans_type = ".db_escape($trans_type)."
401 AND trans_no = ".db_escape($trans_no)."
402 AND (net_amount != 0 OR amount != 0)
403 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
405 return db_query($sql, "The transaction tax details could not be retrieved");
408 //----------------------------------------------------------------------------------------
410 function void_trans_tax_details($type, $type_no)
412 $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
413 WHERE trans_no=".db_escape($type_no)
414 ." AND trans_type=".db_escape($type);
416 db_query($sql, "The transaction tax details could not be voided");
419 //----------------------------------------------------------------------------------------
421 function clear_trans_tax_details($type, $type_no)
423 $sql = "DELETE FROM ".TB_PREF."trans_tax_details
424 WHERE trans_no=".db_escape($type_no)
425 ." AND trans_type=".db_escape($type);
427 db_query($sql, "The transaction tax details could not be cleared");
430 function get_tax_summary($from, $to)
432 $fromdate = date2sql($from);
433 $todate = date2sql($to);
436 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
437 ." || trans_type=".ST_JOURNAL.",-1,1)*
438 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
439 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
440 ." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
442 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
443 ." || trans_type=".ST_JOURNAL.",-1,1)*
444 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
445 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
446 ." || trans_type=".ST_CUSTCREDIT.", amount*ex_rate,0)) payable,
448 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
449 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
450 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
451 ." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
453 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
454 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
455 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
456 ." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
460 FROM ".TB_PREF."tax_types ttype,
461 ".TB_PREF."trans_tax_details taxrec
462 WHERE taxrec.tax_type_id=ttype.id
463 AND taxrec.trans_type != ".ST_CUSTDELIVERY."
464 AND taxrec.tran_date >= '$fromdate'
465 AND taxrec.tran_date <= '$todate'
467 //display_error($sql);
468 return db_query($sql,"Cannot retrieve tax summary");
471 //--------------------------------------------------------------------------------
472 // Write/update journal entries.
474 function write_journal_entries(&$cart, $reverse, $use_transaction=true)
478 $date_ = $cart->tran_date;
479 $ref = $cart->reference;
480 $memo_ = $cart->memo_;
481 $trans_type = $cart->trans_type;
482 $new = $cart->order_id == 0;
485 $cart->order_id = get_next_trans_no($trans_type);
487 $trans_id = $cart->order_id;
489 if ($use_transaction)
493 void_journal_trans($trans_type, $trans_id, false);
495 foreach ($cart->gl_items as $journal_item)
497 // post to first found bank account using given gl acount code.
498 $is_bank_to = is_bank_account($journal_item->code_id);
500 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
501 $journal_item->dimension_id, $journal_item->dimension2_id,
502 $journal_item->reference, $journal_item->amount);
505 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
506 $date_, $journal_item->amount, 0, "", get_company_currency(),
507 "Cannot insert a destination bank transaction");
509 // store tax details if the gl account is a tax account
510 add_gl_tax_details($journal_item->code_id,
511 ST_JOURNAL, $trans_id, $journal_item->amount, 1, $date_, $memo_);
514 $Refs->save($trans_type, $trans_id, $ref);
516 add_comments($trans_type, $trans_id, $date_, $memo_);
518 update_comments($trans_type, $trans_id, null, $memo_);
521 add_audit_trail($trans_type, $trans_id, $date_);
525 //$reversingDate = date(user_date_display(),
526 // Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
527 $reversingDate = begin_month(add_months($date_, 1));
529 $trans_id_reverse = get_next_trans_no($trans_type);
531 foreach ($cart->gl_items as $journal_item)
533 $is_bank_to = is_bank_account($journal_item->code_id);
535 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
536 $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
537 $journal_item->reference, -$journal_item->amount);
540 add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
541 $reversingDate, -$journal_item->amount,
542 0, "", get_company_currency(),
543 "Cannot insert a destination bank transaction");
545 // store tax details if the gl account is a tax account
546 add_gl_tax_details($journal_item->code_id,
547 ST_JOURNAL, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
550 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
552 $Refs->save($trans_type, $trans_id_reverse, $ref);
553 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
556 if ($use_transaction)
557 commit_transaction();
562 //--------------------------------------------------------------------------------------------------
564 function exists_gl_trans($type, $trans_id)
566 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
567 ." AND type_no=".db_escape($trans_id);
568 $result = db_query($sql, "Cannot retreive a gl transaction");
570 return (db_num_rows($result) > 0);
573 //--------------------------------------------------------------------------------------------------
575 function void_gl_trans($type, $trans_id, $nested=false)
580 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
581 ." AND type_no=".db_escape($trans_id);
583 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
586 commit_transaction();
589 //--------------------------------------------------------------------------------------------------
591 function clear_gl_trans($type, $trans_id, $nested=false)
596 $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
597 ." AND type_no=".db_escape($trans_id);
599 db_query($sql, "could not clear gl transactions for type=$type and trans_no=$trans_id");
602 commit_transaction();
605 //----------------------------------------------------------------------------------------
607 function void_journal_trans($type, $type_no, $use_transaction=true)
609 if ($use_transaction)
612 void_bank_trans($type, $type_no, true);
613 // void_gl_trans($type, $type_no, true); // this is done above
614 // void_trans_tax_details($type, $type_no); // ditto
616 if ($use_transaction)
617 commit_transaction();
620 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false)
623 $sql = "SELECT IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
628 SUM(IF(gl.amount>0, gl.amount,0)) as amount,
630 IF(ISNULL(u.user_id),'',u.user_id) as user_id
631 FROM ".TB_PREF."gl_trans as gl
632 LEFT JOIN ".TB_PREF."audit_trail as a ON
633 (gl.type=a.type AND gl.type_no=a.trans_no)
634 LEFT JOIN ".TB_PREF."comments as com ON
635 (gl.type=com.type AND gl.type_no=com.id)
636 LEFT JOIN ".TB_PREF."refs as refs ON
637 (gl.type=refs.type AND gl.type_no=refs.id)
638 LEFT JOIN ".TB_PREF."users as u ON
640 WHERE gl.tran_date >= '" . date2sql($from) . "'
641 AND gl.tran_date <= '" . date2sql($to) . "'
644 $sql .= " AND reference LIKE ". db_escape("%$ref%");
647 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
650 $sql .= " AND gl.type=".db_escape($filter);
653 $sql .= " AND gl_seq=0";
656 $sql .= " AND NOT ISNULL(a.gl_seq)";
658 $sql .= " GROUP BY tran_date, gl_seq, gl.type, gl.type_no";