Fixed problem with counting number of rows for some SQL queries displayed in db pager.
[fa-stable.git] / gl / includes / db / gl_db_trans.inc
1 <?php
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 //--------------------------------------------------------------------------------
13
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
18
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)
21 {
22         global $use_audit_trail;
23
24         $date = date2sql($date_);
25         if ($currency != null)
26         {
27                 if ($rate == 0)
28                         $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
29                 else
30                         $amount_in_home_currency = round2($amount * $rate,  user_price_dec());
31         }               
32         else
33                 $amount_in_home_currency = round2($amount, user_price_dec());
34         if ($dimension == null || $dimension < 0)
35                 $dimension = 0;
36         if ($dimension2 == null || $dimension2 < 0)
37                 $dimension2 = 0;
38         if (isset($use_audit_trail) && $use_audit_trail)
39         {
40                 if ($memo_ == "" || $memo_ == null)
41                         $memo_ = $_SESSION["wa_current_user"]->username;
42                 else
43                         $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
44         }
45         $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
46                 account, dimension_id, dimension2_id, memo_, amount";
47
48         if ($person_type_id != null)
49                 $sql .= ", person_type_id, person_id";
50
51         $sql .= ") ";
52
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);
57
58         if ($person_type_id != null)
59                 $sql .= ", ".db_escape($person_type_id).", ". db_escape($person_id);
60
61         $sql .= ") ";
62
63         if ($err_msg == "")
64                 $err_msg = "The GL transaction could not be inserted";
65
66         db_query($sql, $err_msg);
67         return $amount_in_home_currency;
68 }
69
70 //--------------------------------------------------------------------------------
71
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
75
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="")
78 {
79         if ($amount != 0)
80                 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
81                         $amount, null, $person_type_id, $person_id, $err_msg);
82         else
83                 return 0;
84 }
85
86 // Function for even out rounding problems
87 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
88 {
89         $amount = round2($amount, user_price_dec());
90
91         if (floatcmp($amount, 0))
92         {
93                 error_log(sprintf( _("Rounding error %s encountered for trans_type:%s,trans_no:%s"), $amount, $type, $trans_id));
94                 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
95                         $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
96         } else
97                 return 0;
98 }
99
100 //--------------------------------------------------------------------------------
101
102 function get_gl_transactions($from_date, $to_date, $trans_no=0,
103         $account=null, $dimension=0, $dimension2=0, $filter_type=null,
104         $amount_min=null, $amount_max=null)
105 {
106         global $show_voided_gl_trans;
107         
108         $from = date2sql($from_date);
109         $to = date2sql($to_date);
110
111         $sql = "SELECT ".TB_PREF."gl_trans.*, "
112                 .TB_PREF."chart_master.account_name FROM "
113                 .TB_PREF."gl_trans
114                         LEFT JOIN ".TB_PREF."voided v ON "
115                         .TB_PREF."gl_trans.type_no=v.id AND v.type=".TB_PREF."gl_trans.type,"
116                         .TB_PREF."chart_master"
117                 ." WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
118                 AND ISNULL(v.date_)
119                 AND tran_date >= '$from'
120                 AND tran_date <= '$to'";
121         if (isset($show_voided_gl_trans) && $show_voided_gl_trans == 0)
122                 $sql .= " AND ".TB_PREF."gl_trans.amount <> 0"; 
123         if ($trans_no > 0)
124                 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE ".db_escape('%'.$trans_no);
125
126         if ($account != null)
127                 $sql .= " AND ".TB_PREF."gl_trans.account = ".db_escape($account);
128
129         if ($dimension != 0)
130                 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = ".($dimension<0?0:db_escape($dimension));
131
132         if ($dimension2 != 0)
133                 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
134
135         if ($filter_type != null AND is_numeric($filter_type))
136                 $sql .= " AND ".TB_PREF."gl_trans.type= ".db_escape($filter_type);
137                 
138         if ($amount_min != null)
139                 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) >= ABS(".db_escape($amount_min).")";
140         
141         if ($amount_max != null)
142                 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) <= ABS(".db_escape($amount_max).")";
143
144         $sql .= " ORDER BY tran_date, counter";
145
146         return db_query($sql, "The transactions for could not be retrieved");
147 }
148
149
150 //--------------------------------------------------------------------------------
151
152 function get_gl_trans($type, $trans_id)
153 {
154         $sql = "SELECT gl.*, cm.account_name, IF(ISNULL(refs.reference), '', refs.reference) AS reference FROM "
155                 .TB_PREF."gl_trans as gl
156                 LEFT JOIN ".TB_PREF."chart_master as cm ON gl.account = cm.account_code
157                 LEFT JOIN ".TB_PREF."refs as refs ON (gl.type=refs.type AND gl.type_no=refs.id)"
158                 ." WHERE gl.type= ".db_escape($type) 
159                 ." AND gl.type_no = ".db_escape($trans_id)
160                 ." AND gl.amount <> 0"
161                 ." ORDER BY counter";
162         return db_query($sql, "The gl transactions could not be retrieved");
163 }
164
165 //--------------------------------------------------------------------------------
166
167 function get_gl_wo_cost_trans($trans_id, $person_id=-1)
168 {
169         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM "
170                 .TB_PREF."gl_trans, ".TB_PREF."chart_master
171                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
172                 AND ".TB_PREF."gl_trans.type=".ST_WORKORDER
173                 ." AND ".TB_PREF."gl_trans.type_no=".db_escape($trans_id)."
174                 AND ".TB_PREF."gl_trans.person_type_id=".PT_WORKORDER;
175         if ($person_id != -1)
176                 $sql .= " AND ".TB_PREF."gl_trans.person_id=".db_escape($person_id);
177         $sql .= " AND amount < 0";      
178
179         return db_query($sql, "The gl transactions could not be retrieved");
180 }
181
182 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
183 {
184         $from = date2sql($from_date);
185         $to = date2sql($to_date);
186
187     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
188                 WHERE account='$account'";
189         if ($from_date != "")
190                 $sql .= "  AND tran_date > '$from'";
191         if ($to_date != "")
192                 $sql .= "  AND tran_date < '$to'";
193         if ($dimension != 0)
194                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
195         if ($dimension2 != 0)
196                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
197
198         $result = db_query($sql, "The starting balance for account $account could not be calculated");
199
200         $row = db_fetch_row($result);
201         return $row[0];
202 }
203
204 //--------------------------------------------------------------------------------
205
206 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
207 {
208         $from = date2sql($from_date);
209         $to = date2sql($to_date);
210
211     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
212                 WHERE account='$account'";
213         if ($from_date != "")
214                 $sql .= " AND tran_date >= '$from'";
215         if ($to_date != "")
216                 $sql .= " AND tran_date <= '$to'";
217         if ($dimension != 0)
218                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
219         if ($dimension2 != 0)
220                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
221
222         $result = db_query($sql, "Transactions for account $account could not be calculated");
223
224         $row = db_fetch_row($result);
225         return (float)$row[0];
226 }
227
228 //----------------------------------------------------------------------------------------------------
229 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
230 {
231         $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, 
232                 SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance 
233                 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,"
234                         .TB_PREF."chart_types, ".TB_PREF."chart_class 
235                 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND "
236                 .TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id 
237                 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
238                 
239         if ($account != null)
240                 $sql .= " account=".db_escape($account)." AND";
241         if ($dimension != 0)
242                 $sql .= " dimension_id = ".($dimension<0?0:db_escape($dimension))." AND";
243         if ($dimension2 != 0)
244                 $sql .= " dimension2_id = ".($dimension2<0?0:db_escape($dimension2))." AND";
245         $from_date = date2sql($from);
246         if ($from_incl)
247                 $sql .= " tran_date >= '$from_date'  AND";
248         else
249                 $sql .= " tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date') AND";
250         $to_date = date2sql($to);
251         if ($to_incl)
252                 $sql .= " tran_date <= '$to_date' ";
253         else
254                 $sql .= " tran_date < '$to_date' ";
255
256         $result = db_query($sql,"No general ledger accounts were returned");
257
258         return db_fetch($result);
259 }
260
261 //--------------------------------------------------------------------------------
262
263 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
264 {
265
266         $from = date2sql($from_date);
267         $to = date2sql($to_date);
268
269         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
270                 WHERE account=".db_escape($account);
271         if ($from_date != "")
272                 $sql .= " AND tran_date >= '$from' ";
273         if ($to_date != "")
274                 $sql .= " AND tran_date <= '$to' ";
275         if ($dimension != 0)
276                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
277         if ($dimension2 != 0)
278                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
279         $result = db_query($sql,"No budget accounts were returned");
280
281         $row = db_fetch_row($result);
282         return $row[0];
283 }
284 //-------------------------------------------------------------------------------------
285
286 function exists_gl_budget($date_, $account, $dimension, $dimension2)
287 {
288         $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
289         ." AND tran_date='$date_' AND
290                 dimension_id=".db_escape($dimension)." AND dimension2_id=".db_escape($dimension2);
291         $result = db_query($sql, "Cannot retreive a gl transaction");
292
293     return (db_num_rows($result) > 0);
294 }
295
296 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
297 {
298         $date = date2sql($date_);
299
300         if (exists_gl_budget($date, $account, $dimension, $dimension2))
301                 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=".db_escape($amount)
302                 ." WHERE account=".db_escape($account)
303                 ." AND dimension_id=".db_escape($dimension)
304                 ." AND dimension2_id=".db_escape($dimension2)
305                 ." AND tran_date='$date'";
306         else
307                 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
308                         account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
309                         ".db_escape($account).", ".db_escape($dimension).", "
310                         .db_escape($dimension2).", ".db_escape($amount).", '')";
311
312         db_query($sql, "The GL budget transaction could not be saved");
313 }
314
315 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
316 {
317         $date = date2sql($date_);
318
319         $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
320         ." AND dimension_id=".db_escape($dimension)
321         ." AND dimension2_id=".db_escape($dimension2)
322         ." AND tran_date='$date'";
323         db_query($sql, "The GL budget transaction could not be deleted");
324 }
325
326 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
327 {
328
329         $from = date2sql($from_date);
330         $to = date2sql($to_date);
331
332         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
333                 WHERE account=".db_escape($account)
334                 ." AND tran_date >= '$from' AND tran_date <= '$to'
335                  AND dimension_id = ".db_escape($dimension)
336                  ." AND dimension2_id = ".db_escape($dimension2);
337         $result = db_query($sql,"No budget accounts were returned");
338
339         $row = db_fetch_row($result);
340         return $row[0];
341 }
342
343 //--------------------------------------------------------------------------------
344 //      Stores journal/bank transaction tax details if applicable
345 //
346 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
347 {
348         $tax_type = is_tax_account($gl_code);
349         if(!$tax_type) return;  // $gl_code is not tax account
350         
351         $tax = get_tax_type($tax_type);
352         //if ($gl_code == $tax['sales_gl_code'])
353         if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
354                 $amount = -$amount;
355         // we have to restore net amount as we cannot know the base amount
356         if ($net_amount===null) {
357                 if ($tax['rate'] == 0) {
358 //                      display_warning(_("You should not post gl transactions  
359 //                              to tax account with     zero tax rate."));
360                         $net_amount = 0;
361                 } else { 
362                         // calculate net amount
363                         $net_amount = $amount/$tax['rate']*100; 
364                 }
365         }
366         add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included, 
367                 $amount, $net_amount, $ex_rate, $date, $memo);
368                         
369 }
370
371 //--------------------------------------------------------------------------------
372 //
373 //      Store transaction tax details for fiscal purposes with 'freezed' 
374 //      actual tax type rate.
375 //
376 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
377         $amount, $net_amount, $ex_rate, $tran_date, $memo)
378 {
379
380         $sql = "INSERT INTO ".TB_PREF."trans_tax_details 
381                 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
382                         included_in_price, net_amount, amount, memo)
383                 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
384                                 .date2sql($tran_date)."',".db_escape($tax_id).","
385                                 .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
386                                 .db_escape($net_amount).","
387                                 .db_escape($amount).",".db_escape($memo).")";
388
389         db_query($sql, "Cannot save trans tax details");
390
391 }
392 //----------------------------------------------------------------------------------------
393
394 function get_trans_tax_details($trans_type, $trans_no)
395 {
396         $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
397                 .TB_PREF."tax_types.name AS tax_type_name, "
398                 .TB_PREF."trans_tax_details.rate AS effective_rate, "
399                 .TB_PREF."tax_types.rate AS rate
400                 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
401                 WHERE trans_type = ".db_escape($trans_type)."
402                 AND trans_no = ".db_escape($trans_no)."
403                 AND (net_amount != 0 OR amount != 0)
404                 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
405
406         return db_query($sql, "The transaction tax details could not be retrieved");
407 }
408
409 //----------------------------------------------------------------------------------------
410
411 function void_trans_tax_details($type, $type_no)
412 {
413         $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
414                 WHERE trans_no=".db_escape($type_no)
415                 ." AND trans_type=".db_escape($type);
416
417         db_query($sql, "The transaction tax details could not be voided");
418 }
419
420 //----------------------------------------------------------------------------------------
421
422 function clear_trans_tax_details($type, $type_no)
423 {
424         $sql = "DELETE FROM ".TB_PREF."trans_tax_details 
425                 WHERE trans_no=".db_escape($type_no)
426                 ." AND trans_type=".db_escape($type);
427
428         db_query($sql, "The transaction tax details could not be cleared");
429 }
430
431 function get_tax_summary($from, $to)
432 {
433         $fromdate = date2sql($from);
434         $todate = date2sql($to);
435
436         $sql = "SELECT 
437                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
438                                         ." || trans_type=".ST_JOURNAL.",-1,1)*
439                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE 
440                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
441                                         ." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
442
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.", amount*ex_rate,0)) payable,
448
449                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE." || trans_type=".ST_JOURNAL.",-1,1)*
450                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
451                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
452                                         ." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
453
454                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE." || trans_type=".ST_JOURNAL.",-1,1)*
455                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
456                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
457                                         ." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
458                                 taxrec.rate,
459                                 ttype.id,
460                                 ttype.name
461                 FROM ".TB_PREF."tax_types ttype,
462                          ".TB_PREF."trans_tax_details taxrec
463                 WHERE taxrec.tax_type_id=ttype.id
464                         AND taxrec.trans_type != ".ST_CUSTDELIVERY."
465                         AND taxrec.tran_date >= '$fromdate'
466                         AND taxrec.tran_date <= '$todate'
467                 GROUP BY ttype.id";
468 //display_error($sql);
469     return db_query($sql,"Cannot retrieve tax summary");
470 }
471
472 //--------------------------------------------------------------------------------
473 // Write/update journal entries.
474 //
475 function write_journal_entries(&$cart, $reverse, $use_transaction=true)
476 {
477         global $Refs;
478
479         $date_ = $cart->tran_date;
480         $ref   = $cart->reference;
481         $memo_ = $cart->memo_;
482         $trans_type = $cart->trans_type;
483         $new = $cart->order_id == 0;
484         
485         if ($new)
486             $cart->order_id = get_next_trans_no($trans_type);
487
488     $trans_id = $cart->order_id;
489
490         if ($use_transaction)
491                 begin_transaction();
492         
493         if(!$new)
494                 void_journal_trans($trans_type, $trans_id, false);
495
496         foreach ($cart->gl_items as $journal_item)
497         {
498                 // post to first found bank account using given gl acount code.
499                 $is_bank_to = is_bank_account($journal_item->code_id);
500
501                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
502                         $journal_item->dimension_id, $journal_item->dimension2_id,
503                         $journal_item->reference, $journal_item->amount);
504         if ($is_bank_to)
505         {
506                 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
507                         $date_, $journal_item->amount,  0, "", get_company_currency(),
508                         "Cannot insert a destination bank transaction");
509         }
510                 // store tax details if the gl account is a tax account
511                 add_gl_tax_details($journal_item->code_id, 
512                         ST_JOURNAL, $trans_id, $journal_item->amount, 1, $date_, $memo_);
513         }
514         
515         $Refs->save($trans_type, $trans_id, $ref);
516         if ($new) {
517                 add_comments($trans_type, $trans_id, $date_, $memo_);
518         } else {
519                 update_comments($trans_type, $trans_id, null, $memo_);
520         }
521
522         add_audit_trail($trans_type, $trans_id, $date_);
523
524         if ($reverse)
525         {
526         //$reversingDate = date(user_date_display(),
527         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
528         $reversingDate = begin_month(add_months($date_, 1));
529
530         $trans_id_reverse = get_next_trans_no($trans_type);
531
532         foreach ($cart->gl_items as $journal_item)
533         {
534                         $is_bank_to = is_bank_account($journal_item->code_id);
535
536                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
537                         $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
538                         $journal_item->reference, -$journal_item->amount);
539                 if ($is_bank_to)
540                 {
541                         add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
542                                 $reversingDate, -$journal_item->amount,
543                                 0, "", get_company_currency(),
544                                 "Cannot insert a destination bank transaction");
545                 }
546                         // store tax details if the gl account is a tax account
547                         add_gl_tax_details($journal_item->code_id, 
548                                 ST_JOURNAL, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
549         }
550
551         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
552
553         $Refs->save($trans_type, $trans_id_reverse, $ref);
554                 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
555         }
556
557         if ($use_transaction)
558                 commit_transaction();
559
560         return $trans_id;
561 }
562
563 //--------------------------------------------------------------------------------------------------
564
565 function exists_gl_trans($type, $trans_id)
566 {
567         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
568                 ." AND type_no=".db_escape($trans_id);
569         $result = db_query($sql, "Cannot retreive a gl transaction");
570
571     return (db_num_rows($result) > 0);
572 }
573
574 //--------------------------------------------------------------------------------------------------
575
576 function void_gl_trans($type, $trans_id, $nested=false)
577 {
578         if (!$nested)
579                 begin_transaction();
580
581         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
582         ." AND type_no=".db_escape($trans_id);
583
584         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
585
586         if (!$nested)
587                 commit_transaction();
588 }
589
590 //----------------------------------------------------------------------------------------
591
592 function void_journal_trans($type, $type_no, $use_transaction=true)
593 {
594         if ($use_transaction)
595                 begin_transaction();
596
597         void_bank_trans($type, $type_no, true);
598 //      void_gl_trans($type, $type_no, true);    // this is done above
599 //      void_trans_tax_details($type, $type_no); // ditto
600
601         if ($use_transaction)
602                 commit_transaction();
603 }
604
605 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false)
606 {
607
608         $sql = "SELECT  IFNULL(a.gl_seq,0) as gl_seq,
609                 gl.tran_date,
610                 gl.type,
611                 gl.type_no,
612                 refs.reference,
613                 SUM(IF(gl.amount>0, gl.amount,0)) as amount,
614                 com.memo_,
615                 IF(ISNULL(u.user_id),'',u.user_id) as user_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
624                         a.user=u.id
625                 WHERE gl.tran_date >= '" . date2sql($from) . "'
626                 AND gl.tran_date <= '" . date2sql($to) . "'
627                 AND gl.amount!=0";
628         if ($ref) {
629                 $sql .= " AND reference LIKE ". db_escape("%$ref%");
630         }
631         if ($memo) {
632                 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
633         }
634         if ($filter != -1) {
635                 $sql .= " AND gl.type=".db_escape($filter);
636         }
637         if (!$alsoclosed) {
638                 $sql .= " AND gl_seq=0";
639         }
640         $sql .= " GROUP BY gl.tran_date, a.gl_seq, gl.type, gl.type_no";
641         return $sql;
642 }
643 ?>