Merged changes form stabel branch up to the current state (2.3.22+).
[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         if (!is_subledger_account($account) && $account==get_company_pref('deffered_income_act') || $account==get_company_pref('grn_clearing_act'))
46                 $person_id = $person_type_id = null;
47
48         $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
49                 account, dimension_id, dimension2_id, memo_, amount";
50
51         if ($person_type_id != null)
52                 $sql .= ", person_type_id, person_id";
53
54         $sql .= ") ";
55
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);
60
61         if ($person_type_id != null)
62                 $sql .= ", ".db_escape($person_type_id).", ". db_escape($person_id);
63
64         $sql .= ") ";
65
66         if ($err_msg == "")
67                 $err_msg = "The GL transaction could not be inserted";
68
69         db_query($sql, $err_msg);
70         return $amount_in_home_currency;
71 }
72
73 //--------------------------------------------------------------------------------
74
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
78
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="")
81 {
82         if ($amount != 0)
83                 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
84                         $amount, null, $person_type_id, $person_id, $err_msg);
85         else
86                 return 0;
87 }
88
89 // Function for even out rounding problems
90 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
91 {
92         $amount = round2($amount, user_price_dec());
93
94         if (floatcmp($amount, 0))
95         {
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");
99         } else
100                 return 0;
101 }
102
103 //--------------------------------------------------------------------------------
104
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)
108 {
109         global $show_voided_gl_trans;
110         
111         $from = date2sql($from_date);
112         $to = date2sql($to_date);
113
114         $sql = "SELECT ".TB_PREF."gl_trans.*, "
115                 .TB_PREF."chart_master.account_name FROM "
116                 .TB_PREF."gl_trans
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
121                 AND ISNULL(v.date_)
122                 AND tran_date >= '$from'
123                 AND tran_date <= '$to'";
124         if (isset($show_voided_gl_trans) && $show_voided_gl_trans == 0)
125                 $sql .= " AND ".TB_PREF."gl_trans.amount <> 0"; 
126         if ($trans_no > 0)
127                 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE ".db_escape('%'.$trans_no);
128
129         if ($account != null)
130                 $sql .= " AND ".TB_PREF."gl_trans.account = ".db_escape($account);
131
132         if ($dimension != 0)
133                 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = ".($dimension<0?0:db_escape($dimension));
134
135         if ($dimension2 != 0)
136                 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
137
138         if ($filter_type != null AND is_numeric($filter_type))
139                 $sql .= " AND ".TB_PREF."gl_trans.type= ".db_escape($filter_type);
140                 
141         if ($amount_min != null)
142                 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) >= ABS(".db_escape($amount_min).")";
143         
144         if ($amount_max != null)
145                 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) <= ABS(".db_escape($amount_max).")";
146
147         $sql .= " ORDER BY tran_date, counter";
148
149         return db_query($sql, "The transactions for could not be retrieved");
150 }
151
152
153 //--------------------------------------------------------------------------------
154
155 function get_gl_trans($type, $trans_id)
156 {
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");
166 }
167
168 //--------------------------------------------------------------------------------
169
170 function get_gl_wo_cost_trans($trans_id, $cost_type=-1)
171 {
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
176                 WHERE 
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";
184
185         return db_query($sql, "The gl transactions could not be retrieved");
186 }
187
188 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
189 {
190         $from = date2sql($from_date);
191         $to = date2sql($to_date);
192
193     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
194                 WHERE account='$account'";
195         if ($from_date != "")
196                 $sql .= "  AND tran_date > '$from'";
197         if ($to_date != "")
198                 $sql .= "  AND tran_date < '$to'";
199         if ($dimension != 0)
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));
203
204         $result = db_query($sql, "The starting balance for account $account could not be calculated");
205
206         $row = db_fetch_row($result);
207         return $row[0];
208 }
209
210 //--------------------------------------------------------------------------------
211
212 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
213 {
214         $from = date2sql($from_date);
215         $to = date2sql($to_date);
216
217     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
218                 WHERE account='$account'";
219         if ($from_date != "")
220                 $sql .= " AND tran_date >= '$from'";
221         if ($to_date != "")
222                 $sql .= " AND tran_date <= '$to'";
223         if ($dimension != 0)
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));
227
228         $result = db_query($sql, "Transactions for account $account could not be calculated");
229
230         $row = db_fetch_row($result);
231         return (float)$row[0];
232 }
233
234 //----------------------------------------------------------------------------------------------------
235 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
236 {
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";
244                 
245         if ($account != null)
246                 $sql .= " account=".db_escape($account)." AND";
247         if ($dimension != 0)
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);
252         if ($from_incl)
253                 $sql .= " tran_date >= '$from_date'  AND";
254         else
255                 $sql .= " tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date') AND";
256         $to_date = date2sql($to);
257         if ($to_incl)
258                 $sql .= " tran_date <= '$to_date' ";
259         else
260                 $sql .= " tran_date < '$to_date' ";
261
262         $result = db_query($sql,"No general ledger accounts were returned");
263
264         return db_fetch($result);
265 }
266
267 //--------------------------------------------------------------------------------
268
269 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
270 {
271
272         $from = date2sql($from_date);
273         $to = date2sql($to_date);
274
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' ";
279         if ($to_date != "")
280                 $sql .= " AND tran_date <= '$to' ";
281         if ($dimension != 0)
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");
286
287         $row = db_fetch_row($result);
288         return $row[0];
289 }
290 //-------------------------------------------------------------------------------------
291
292 function exists_gl_budget($date_, $account, $dimension, $dimension2)
293 {
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");
298
299     return (db_num_rows($result) > 0);
300 }
301
302 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
303 {
304         $date = date2sql($date_);
305
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'";
312         else
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).", '')";
317
318         db_query($sql, "The GL budget transaction could not be saved");
319 }
320
321 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
322 {
323         $date = date2sql($date_);
324
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");
330 }
331
332 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
333 {
334
335         $from = date2sql($from_date);
336         $to = date2sql($to_date);
337
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");
344
345         $row = db_fetch_row($result);
346         return $row[0];
347 }
348
349 //--------------------------------------------------------------------------------
350 //      Stores journal/bank transaction tax details if applicable
351 //
352 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
353 {
354         $tax_type = is_tax_account($gl_code);
355         if(!$tax_type) return;  // $gl_code is not tax account
356         
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)
360                 $amount = -$amount;
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."));
366                         $net_amount = 0;
367                 } else { 
368                         // calculate net amount
369                         $net_amount = $amount/$tax['rate']*100; 
370                 }
371         }
372         add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included, 
373                 $amount, $net_amount, $ex_rate, $date, $memo);
374
375 }
376
377 //--------------------------------------------------------------------------------
378 //
379 //      Store transaction tax details for fiscal purposes with 'freezed' 
380 //      actual tax type rate.
381 //
382 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
383         $amount, $net_amount, $ex_rate, $tran_date, $memo)
384 {
385
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).")";
394
395         db_query($sql, "Cannot save trans tax details");
396
397 }
398 //----------------------------------------------------------------------------------------
399
400 function get_trans_tax_details($trans_type, $trans_no)
401 {
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";
411
412         return db_query($sql, "The transaction tax details could not be retrieved");
413 }
414
415 //----------------------------------------------------------------------------------------
416
417 function void_trans_tax_details($type, $type_no)
418 {
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);
422
423         db_query($sql, "The transaction tax details could not be voided");
424 }
425
426 //----------------------------------------------------------------------------------------
427
428 function clear_trans_tax_details($type, $type_no)
429 {
430         $sql = "DELETE FROM ".TB_PREF."trans_tax_details 
431                 WHERE trans_no=".db_escape($type_no)
432                 ." AND trans_type=".db_escape($type);
433
434         db_query($sql, "The transaction tax details could not be cleared");
435 }
436
437 function get_tax_summary($from, $to)
438 {
439         $fromdate = date2sql($from);
440         $todate = date2sql($to);
441
442         $sql = "SELECT 
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,
448
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,
454
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,
459
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,
464                                 taxrec.rate,
465                                 ttype.id,
466                                 ttype.name
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'
473                 GROUP BY ttype.id";
474 //display_error($sql);
475     return db_query($sql,"Cannot retrieve tax summary");
476 }
477
478 //--------------------------------------------------------------------------------
479 // Write/update journal entries.
480 //
481 function write_journal_entries(&$cart, $reverse, $use_transaction=true)
482 {
483         global $Refs;
484
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;
490         
491         if ($new)
492             $cart->order_id = get_next_trans_no($trans_type);
493
494     $trans_id = $cart->order_id;
495
496         if ($use_transaction)
497                 begin_transaction();
498         
499         if(!$new)
500                 void_journal_trans($trans_type, $trans_id, false);
501
502         foreach ($cart->gl_items as $journal_item)
503         {
504                 // post to first found bank account using given gl acount code.
505                 $is_bank_to = is_bank_account($journal_item->code_id);
506
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);
512
513         if ($is_bank_to)
514         {
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");
518         }
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_);
522         }
523         
524         $Refs->save($trans_type, $trans_id, $ref);
525         if ($new) {
526                 add_comments($trans_type, $trans_id, $date_, $memo_);
527         } else {
528                 update_comments($trans_type, $trans_id, null, $memo_);
529         }
530
531         add_audit_trail($trans_type, $trans_id, $date_);
532
533         if ($reverse)
534         {
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));
538
539         $trans_id_reverse = get_next_trans_no($trans_type);
540
541         foreach ($cart->gl_items as $journal_item)
542         {
543                         $is_bank_to = is_bank_account($journal_item->code_id);
544
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);
549
550                 if ($is_bank_to)
551                 {
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");
556                 }
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_);
560         }
561
562         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
563
564         $Refs->save($trans_type, $trans_id_reverse, $ref);
565                 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
566         }
567
568         if ($use_transaction)
569                 commit_transaction();
570
571         return $trans_id;
572 }
573
574 //--------------------------------------------------------------------------------------------------
575
576 function exists_gl_trans($type, $trans_id)
577 {
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");
581
582     return (db_num_rows($result) > 0);
583 }
584
585 //--------------------------------------------------------------------------------------------------
586
587 function void_gl_trans($type, $trans_id, $nested=false)
588 {
589         if (!$nested)
590                 begin_transaction();
591
592         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
593         ." AND type_no=".db_escape($trans_id);
594
595         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
596
597         if (!$nested)
598                 commit_transaction();
599 }
600
601 //----------------------------------------------------------------------------------------
602
603 function clear_gl_trans($type, $trans_id, $nested=false)
604 {
605         if (!$nested)
606                 begin_transaction();
607
608         $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
609         ." AND type_no=".db_escape($trans_id);
610
611         db_query($sql, "could not clear gl transactions for type=$type and trans_no=$trans_id");
612
613         if (!$nested)
614                 commit_transaction();
615 }
616
617 //----------------------------------------------------------------------------------------
618
619 function void_journal_trans($type, $type_no, $use_transaction=true)
620 {
621         if ($use_transaction)
622                 begin_transaction();
623
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
627
628         if ($use_transaction)
629                 commit_transaction();
630 }
631
632 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false)
633 {
634
635         $sql = "SELECT  IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
636                 gl.tran_date,
637                 gl.type,
638                 gl.type_no,
639                 refs.reference,
640                 SUM(IF(gl.amount>0, gl.amount,0)) as amount,
641                 com.memo_,
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
651                         a.user=u.id
652                 WHERE gl.tran_date >= '" . date2sql($from) . "'
653                 AND gl.tran_date <= '" . date2sql($to) . "'
654                 AND gl.amount!=0";
655         if ($ref) {
656                 $sql .= " AND reference LIKE ". db_escape("%$ref%");
657         }
658         if ($memo) {
659                 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
660         }
661         if ($filter != -1) {
662                 $sql .= " AND gl.type=".db_escape($filter);
663         }
664         if (!$alsoclosed) {
665                 $sql .= " AND gl_seq=0";
666         }
667         else
668                 $sql .= " AND NOT ISNULL(a.gl_seq)";
669
670         $sql .= " GROUP BY gl.tran_date, a.gl_seq, gl.type, gl.type_no";
671         return $sql;
672 }
673 ?>