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="")
83 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
84 $amount, null, $person_type_id, $person_id, $err_msg);
89 // Function for even out rounding problems
90 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
92 $amount = round2($amount, user_price_dec());
94 if (floatcmp($amount, 0))
96 error_log(sprintf( _("Rounding error %s encountered for trans_type:%s,trans_no:%s"), $amount, $type, $trans_id));
97 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
98 $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
103 //--------------------------------------------------------------------------------
105 function get_gl_transactions($from_date, $to_date, $trans_no=0,
106 $account=null, $dimension=0, $dimension2=0, $filter_type=null,
107 $amount_min=null, $amount_max=null)
111 $from = date2sql($from_date);
112 $to = date2sql($to_date);
114 $sql = "SELECT ".TB_PREF."gl_trans.*, "
115 .TB_PREF."chart_master.account_name FROM "
117 LEFT JOIN ".TB_PREF."voided v ON "
118 .TB_PREF."gl_trans.type_no=v.id AND v.type=".TB_PREF."gl_trans.type,"
119 .TB_PREF."chart_master"
120 ." WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
122 AND tran_date >= '$from'
123 AND tran_date <= '$to'";
124 if (isset($SysPrefs->show_voided_gl_trans) && $SysPrefs->show_voided_gl_trans == 0)
125 $sql .= " AND ".TB_PREF."gl_trans.amount <> 0";
127 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE ".db_escape('%'.$trans_no);
129 if ($account != null)
130 $sql .= " AND ".TB_PREF."gl_trans.account = ".db_escape($account);
133 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = ".($dimension<0?0:db_escape($dimension));
135 if ($dimension2 != 0)
136 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
138 if ($filter_type != null AND is_numeric($filter_type))
139 $sql .= " AND ".TB_PREF."gl_trans.type= ".db_escape($filter_type);
141 if ($amount_min != null)
142 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) >= ABS(".db_escape($amount_min).")";
144 if ($amount_max != null)
145 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) <= ABS(".db_escape($amount_max).")";
147 $sql .= " ORDER BY tran_date, counter";
149 return db_query($sql, "The transactions for could not be retrieved");
153 //--------------------------------------------------------------------------------
155 function get_gl_trans($type, $trans_id)
157 $sql = "SELECT gl.*, cm.account_name, IF(ISNULL(refs.reference), '', refs.reference) AS reference FROM "
158 .TB_PREF."gl_trans as gl
159 LEFT JOIN ".TB_PREF."chart_master as cm ON gl.account = cm.account_code
160 LEFT JOIN ".TB_PREF."refs as refs ON (gl.type=refs.type AND gl.type_no=refs.id)"
161 ." WHERE gl.type= ".db_escape($type)
162 ." AND gl.type_no = ".db_escape($trans_id)
163 ." AND gl.amount <> 0"
164 ." ORDER BY counter";
165 return db_query($sql, "The gl transactions could not be retrieved");
168 //--------------------------------------------------------------------------------
170 function get_gl_wo_cost_trans($trans_id, $cost_type=-1)
172 $sql = "SELECT costing.*, gl.*, chart.account_name, com.memo_ FROM "
173 .TB_PREF."wo_costing costing, "
174 .TB_PREF."gl_trans gl LEFT JOIN ".TB_PREF."comments com ON gl.type=com.type AND gl.type_no=com.id,"
175 .TB_PREF."chart_master chart
177 costing.workorder_id=".db_escape($trans_id)
178 ." AND chart.account_code=gl.account
179 AND gl.type=costing.trans_type
180 AND gl.type_no=costing.trans_no";
181 if ($cost_type != -1)
182 $sql .= " AND costing.cost_type=".db_escape($cost_type);
183 $sql .= " AND amount < 0";
185 return db_query($sql, "The gl transactions could not be retrieved");
188 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
190 $from = date2sql($from_date);
191 $to = date2sql($to_date);
193 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
194 WHERE account='$account'";
195 if ($from_date != "")
196 $sql .= " AND tran_date > '$from'";
198 $sql .= " AND tran_date < '$to'";
200 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
201 if ($dimension2 != 0)
202 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
204 $result = db_query($sql, "The starting balance for account $account could not be calculated");
206 $row = db_fetch_row($result);
210 //--------------------------------------------------------------------------------
212 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
214 $from = date2sql($from_date);
215 $to = date2sql($to_date);
217 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
218 WHERE account='$account'";
219 if ($from_date != "")
220 $sql .= " AND tran_date >= '$from'";
222 $sql .= " AND tran_date <= '$to'";
224 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
225 if ($dimension2 != 0)
226 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
228 $result = db_query($sql, "Transactions for account $account could not be calculated");
230 $row = db_fetch_row($result);
231 return (float)$row[0];
234 //----------------------------------------------------------------------------------------------------
235 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
237 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit,
238 SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance
239 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,"
240 .TB_PREF."chart_types, ".TB_PREF."chart_class
241 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND "
242 .TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
243 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
245 if ($account != null)
246 $sql .= " account=".db_escape($account)." AND";
248 $sql .= " dimension_id = ".($dimension<0?0:db_escape($dimension))." AND";
249 if ($dimension2 != 0)
250 $sql .= " dimension2_id = ".($dimension2<0?0:db_escape($dimension2))." AND";
251 $from_date = date2sql($from);
253 $sql .= " tran_date >= '$from_date' AND";
255 $sql .= " tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date') AND";
256 $to_date = date2sql($to);
258 $sql .= " tran_date <= '$to_date' ";
260 $sql .= " tran_date < '$to_date' ";
262 $result = db_query($sql,"No general ledger accounts were returned");
264 return db_fetch($result);
267 //--------------------------------------------------------------------------------
269 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
272 $from = date2sql($from_date);
273 $to = date2sql($to_date);
275 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
276 WHERE account=".db_escape($account);
277 if ($from_date != "")
278 $sql .= " AND tran_date >= '$from' ";
280 $sql .= " AND tran_date <= '$to' ";
282 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
283 if ($dimension2 != 0)
284 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
285 $result = db_query($sql,"No budget accounts were returned");
287 $row = db_fetch_row($result);
290 //-------------------------------------------------------------------------------------
292 function exists_gl_budget($date_, $account, $dimension, $dimension2)
294 $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
295 ." AND tran_date='$date_' AND
296 dimension_id=".db_escape($dimension)." AND dimension2_id=".db_escape($dimension2);
297 $result = db_query($sql, "Cannot retreive a gl transaction");
299 return (db_num_rows($result) > 0);
302 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
304 $date = date2sql($date_);
306 if (exists_gl_budget($date, $account, $dimension, $dimension2))
307 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=".db_escape($amount)
308 ." WHERE account=".db_escape($account)
309 ." AND dimension_id=".db_escape($dimension)
310 ." AND dimension2_id=".db_escape($dimension2)
311 ." AND tran_date='$date'";
313 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
314 account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
315 ".db_escape($account).", ".db_escape($dimension).", "
316 .db_escape($dimension2).", ".db_escape($amount).", '')";
318 db_query($sql, "The GL budget transaction could not be saved");
321 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
323 $date = date2sql($date_);
325 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
326 ." AND dimension_id=".db_escape($dimension)
327 ." AND dimension2_id=".db_escape($dimension2)
328 ." AND tran_date='$date'";
329 db_query($sql, "The GL budget transaction could not be deleted");
332 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
335 $from = date2sql($from_date);
336 $to = date2sql($to_date);
338 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
339 WHERE account=".db_escape($account)
340 ." AND tran_date >= '$from' AND tran_date <= '$to'
341 AND dimension_id = ".db_escape($dimension)
342 ." AND dimension2_id = ".db_escape($dimension2);
343 $result = db_query($sql,"No budget accounts were returned");
345 $row = db_fetch_row($result);
349 //--------------------------------------------------------------------------------
350 // Stores journal/bank transaction tax details if applicable
352 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
354 $tax_type = is_tax_account($gl_code);
355 if(!$tax_type) return; // $gl_code is not tax account
357 $tax = get_tax_type($tax_type);
358 //if ($gl_code == $tax['sales_gl_code'])
359 if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
361 // we have to restore net amount as we cannot know the base amount
362 if ($net_amount===null) {
363 if ($tax['rate'] == 0) {
364 // display_warning(_("You should not post gl transactions
365 // to tax account with zero tax rate."));
368 // calculate net amount
369 $net_amount = $amount/$tax['rate']*100;
372 add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included,
373 $amount, $net_amount, $ex_rate, $date, $memo);
377 //--------------------------------------------------------------------------------
379 // Store transaction tax details for fiscal purposes with 'freezed'
380 // actual tax type rate.
382 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
383 $amount, $net_amount, $ex_rate, $tran_date, $memo)
386 $sql = "INSERT INTO ".TB_PREF."trans_tax_details
387 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
388 included_in_price, net_amount, amount, memo)
389 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
390 .date2sql($tran_date)."',".db_escape($tax_id).","
391 .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
392 .db_escape($net_amount).","
393 .db_escape($amount).",".db_escape($memo).")";
395 db_query($sql, "Cannot save trans tax details");
398 //----------------------------------------------------------------------------------------
400 function get_trans_tax_details($trans_type, $trans_no)
402 $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
403 .TB_PREF."tax_types.name AS tax_type_name, "
404 .TB_PREF."trans_tax_details.rate AS effective_rate, "
405 .TB_PREF."tax_types.rate AS rate
406 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
407 WHERE trans_type = ".db_escape($trans_type)."
408 AND trans_no = ".db_escape($trans_no)."
409 AND (net_amount != 0 OR amount != 0)
410 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
412 return db_query($sql, "The transaction tax details could not be retrieved");
415 //----------------------------------------------------------------------------------------
417 function void_trans_tax_details($type, $type_no)
419 $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
420 WHERE trans_no=".db_escape($type_no)
421 ." AND trans_type=".db_escape($type);
423 db_query($sql, "The transaction tax details could not be voided");
426 //----------------------------------------------------------------------------------------
428 function clear_trans_tax_details($type, $type_no)
430 $sql = "DELETE FROM ".TB_PREF."trans_tax_details
431 WHERE trans_no=".db_escape($type_no)
432 ." AND trans_type=".db_escape($type);
434 db_query($sql, "The transaction tax details could not be cleared");
437 function get_tax_summary($from, $to)
439 $fromdate = date2sql($from);
440 $todate = date2sql($to);
443 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
444 ." || trans_type=".ST_JOURNAL.",-1,1)*
445 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
446 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
447 ." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
449 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
450 ." || trans_type=".ST_JOURNAL.",-1,1)*
451 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
452 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
453 ." || trans_type=".ST_CUSTCREDIT.", amount*ex_rate,0)) payable,
455 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE." || trans_type=".ST_JOURNAL.",-1,1)*
456 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
457 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
458 ." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
460 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE." || trans_type=".ST_JOURNAL.",-1,1)*
461 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
462 ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
463 ." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
467 FROM ".TB_PREF."tax_types ttype,
468 ".TB_PREF."trans_tax_details taxrec
469 WHERE taxrec.tax_type_id=ttype.id
470 AND taxrec.trans_type != ".ST_CUSTDELIVERY."
471 AND taxrec.tran_date >= '$fromdate'
472 AND taxrec.tran_date <= '$todate'
474 //display_error($sql);
475 return db_query($sql,"Cannot retrieve tax summary");
478 //--------------------------------------------------------------------------------
479 // Write/update journal entries.
481 function write_journal_entries(&$cart, $reverse, $use_transaction=true)
485 $date_ = $cart->tran_date;
486 $ref = $cart->reference;
487 $memo_ = $cart->memo_;
488 $trans_type = $cart->trans_type;
489 $new = $cart->order_id == 0;
492 $cart->order_id = get_next_trans_no($trans_type);
494 $trans_id = $cart->order_id;
496 if ($use_transaction)
500 void_journal_trans($trans_type, $trans_id, false);
502 foreach ($cart->gl_items as $journal_item)
504 // post to first found bank account using given gl acount code.
505 $is_bank_to = is_bank_account($journal_item->code_id);
507 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
508 $journal_item->dimension_id, $journal_item->dimension2_id,
509 $journal_item->reference, $journal_item->amount, null,
510 $journal_item->person_type_id,
511 $journal_item->person_id);
515 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
516 $date_, $journal_item->amount, 0, "", get_company_currency(),
517 "Cannot insert a destination bank transaction");
519 // store tax details if the gl account is a tax account
520 add_gl_tax_details($journal_item->code_id,
521 ST_JOURNAL, $trans_id, $journal_item->amount, 1, $date_, $memo_);
524 $Refs->save($trans_type, $trans_id, $ref);
526 add_comments($trans_type, $trans_id, $date_, $memo_);
528 update_comments($trans_type, $trans_id, null, $memo_);
531 add_audit_trail($trans_type, $trans_id, $date_);
535 //$reversingDate = date(user_date_display(),
536 // Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
537 $reversingDate = begin_month(add_months($date_, 1));
539 $trans_id_reverse = get_next_trans_no($trans_type);
541 foreach ($cart->gl_items as $journal_item)
543 $is_bank_to = is_bank_account($journal_item->code_id);
545 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
546 $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
547 $journal_item->reference, -$journal_item->amount,
548 null, $journal_item->person_type_id, $journal_item->person_id);
552 add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
553 $reversingDate, -$journal_item->amount,
554 0, "", get_company_currency(),
555 "Cannot insert a destination bank transaction");
557 // store tax details if the gl account is a tax account
558 add_gl_tax_details($journal_item->code_id,
559 ST_JOURNAL, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
562 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
564 $Refs->save($trans_type, $trans_id_reverse, $ref);
565 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
568 if ($use_transaction)
569 commit_transaction();
574 //--------------------------------------------------------------------------------------------------
576 function exists_gl_trans($type, $trans_id)
578 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
579 ." AND type_no=".db_escape($trans_id);
580 $result = db_query($sql, "Cannot retreive a gl transaction");
582 return (db_num_rows($result) > 0);
585 //--------------------------------------------------------------------------------------------------
587 function void_gl_trans($type, $trans_id, $nested=false)
592 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
593 ." AND type_no=".db_escape($trans_id);
595 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
598 commit_transaction();
601 //----------------------------------------------------------------------------------------
603 function clear_gl_trans($type, $trans_id, $nested=false)
608 $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
609 ." AND type_no=".db_escape($trans_id);
611 db_query($sql, "could not clear gl transactions for type=$type and trans_no=$trans_id");
614 commit_transaction();
617 //----------------------------------------------------------------------------------------
619 function void_journal_trans($type, $type_no, $use_transaction=true)
621 if ($use_transaction)
624 void_bank_trans($type, $type_no, true);
625 // void_gl_trans($type, $type_no, true); // this is done above
626 // void_trans_tax_details($type, $type_no); // ditto
628 if ($use_transaction)
629 commit_transaction();
632 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false)
635 $sql = "SELECT IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
640 SUM(IF(gl.amount>0, gl.amount,0)) as amount,
642 IF(ISNULL(u.user_id),'',u.user_id) as user_id
643 FROM ".TB_PREF."gl_trans as gl
644 LEFT JOIN ".TB_PREF."audit_trail as a ON
645 (gl.type=a.type AND gl.type_no=a.trans_no)
646 LEFT JOIN ".TB_PREF."comments as com ON
647 (gl.type=com.type AND gl.type_no=com.id)
648 LEFT JOIN ".TB_PREF."refs as refs ON
649 (gl.type=refs.type AND gl.type_no=refs.id)
650 LEFT JOIN ".TB_PREF."users as u ON
652 WHERE gl.tran_date >= '" . date2sql($from) . "'
653 AND gl.tran_date <= '" . date2sql($to) . "'
656 $sql .= " AND reference LIKE ". db_escape("%$ref%");
659 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
662 $sql .= " AND gl.type=".db_escape($filter);
665 $sql .= " AND gl_seq=0";
668 $sql .= " AND NOT ISNULL(a.gl_seq)";
670 $sql .= " GROUP BY gl.tran_date, a.gl_seq, gl.type, gl.type_no";