Implemented journal entries with multiply virtual AR/AP subaccounts.
[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))
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         if ($amount != 0)
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                 ." ORDER BY counter";
161         return db_query($sql, "The gl transactions could not be retrieved");
162 }
163
164 //--------------------------------------------------------------------------------
165
166 function get_gl_wo_cost_trans($trans_id, $cost_type=-1)
167 {
168         $sql = "SELECT costing.*, gl.*, chart.account_name, com.memo_ FROM "
169                 .TB_PREF."wo_costing costing, "
170                 .TB_PREF."gl_trans gl LEFT JOIN ".TB_PREF."comments com ON gl.type=com.type     AND gl.type_no=com.id,"
171                 .TB_PREF."chart_master chart
172                 WHERE 
173                         costing.workorder_id=".db_escape($trans_id)
174                 ."      AND chart.account_code=gl.account
175                         AND gl.type=costing.trans_type
176                         AND gl.type_no=costing.trans_no";
177         if ($cost_type != -1)
178                 $sql .= " AND costing.cost_type=".db_escape($cost_type);
179         $sql .= " AND amount < 0";
180
181         return db_query($sql, "The gl transactions could not be retrieved");
182 }
183
184 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
185 {
186         $from = date2sql($from_date);
187         $to = date2sql($to_date);
188
189     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
190                 WHERE account='$account'";
191         if ($from_date != "")
192                 $sql .= "  AND tran_date > '$from'";
193         if ($to_date != "")
194                 $sql .= "  AND tran_date < '$to'";
195         if ($dimension != 0)
196                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
197         if ($dimension2 != 0)
198                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
199
200         $result = db_query($sql, "The starting balance for account $account could not be calculated");
201
202         $row = db_fetch_row($result);
203         return $row[0];
204 }
205
206 //--------------------------------------------------------------------------------
207
208 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
209 {
210         $from = date2sql($from_date);
211         $to = date2sql($to_date);
212
213     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
214                 WHERE account='$account'";
215         if ($from_date != "")
216                 $sql .= " AND tran_date >= '$from'";
217         if ($to_date != "")
218                 $sql .= " AND tran_date <= '$to'";
219         if ($dimension != 0)
220                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
221         if ($dimension2 != 0)
222                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
223
224         $result = db_query($sql, "Transactions for account $account could not be calculated");
225
226         $row = db_fetch_row($result);
227         return (float)$row[0];
228 }
229
230 //----------------------------------------------------------------------------------------------------
231 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
232 {
233         $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, 
234                 SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance 
235                 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,"
236                         .TB_PREF."chart_types, ".TB_PREF."chart_class 
237                 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND "
238                 .TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id 
239                 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
240                 
241         if ($account != null)
242                 $sql .= " account=".db_escape($account)." AND";
243         if ($dimension != 0)
244                 $sql .= " dimension_id = ".($dimension<0?0:db_escape($dimension))." AND";
245         if ($dimension2 != 0)
246                 $sql .= " dimension2_id = ".($dimension2<0?0:db_escape($dimension2))." AND";
247         $from_date = date2sql($from);
248         if ($from_incl)
249                 $sql .= " tran_date >= '$from_date'  AND";
250         else
251                 $sql .= " tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date') AND";
252         $to_date = date2sql($to);
253         if ($to_incl)
254                 $sql .= " tran_date <= '$to_date' ";
255         else
256                 $sql .= " tran_date < '$to_date' ";
257
258         $result = db_query($sql,"No general ledger accounts were returned");
259
260         return db_fetch($result);
261 }
262
263 //--------------------------------------------------------------------------------
264
265 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
266 {
267
268         $from = date2sql($from_date);
269         $to = date2sql($to_date);
270
271         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
272                 WHERE account=".db_escape($account);
273         if ($from_date != "")
274                 $sql .= " AND tran_date >= '$from' ";
275         if ($to_date != "")
276                 $sql .= " AND tran_date <= '$to' ";
277         if ($dimension != 0)
278                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
279         if ($dimension2 != 0)
280                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
281         $result = db_query($sql,"No budget accounts were returned");
282
283         $row = db_fetch_row($result);
284         return $row[0];
285 }
286 //-------------------------------------------------------------------------------------
287
288 function exists_gl_budget($date_, $account, $dimension, $dimension2)
289 {
290         $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
291         ." AND tran_date='$date_' AND
292                 dimension_id=".db_escape($dimension)." AND dimension2_id=".db_escape($dimension2);
293         $result = db_query($sql, "Cannot retreive a gl transaction");
294
295     return (db_num_rows($result) > 0);
296 }
297
298 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
299 {
300         $date = date2sql($date_);
301
302         if (exists_gl_budget($date, $account, $dimension, $dimension2))
303                 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=".db_escape($amount)
304                 ." WHERE account=".db_escape($account)
305                 ." AND dimension_id=".db_escape($dimension)
306                 ." AND dimension2_id=".db_escape($dimension2)
307                 ." AND tran_date='$date'";
308         else
309                 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
310                         account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
311                         ".db_escape($account).", ".db_escape($dimension).", "
312                         .db_escape($dimension2).", ".db_escape($amount).", '')";
313
314         db_query($sql, "The GL budget transaction could not be saved");
315 }
316
317 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
318 {
319         $date = date2sql($date_);
320
321         $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
322         ." AND dimension_id=".db_escape($dimension)
323         ." AND dimension2_id=".db_escape($dimension2)
324         ." AND tran_date='$date'";
325         db_query($sql, "The GL budget transaction could not be deleted");
326 }
327
328 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
329 {
330
331         $from = date2sql($from_date);
332         $to = date2sql($to_date);
333
334         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
335                 WHERE account=".db_escape($account)
336                 ." AND tran_date >= '$from' AND tran_date <= '$to'
337                  AND dimension_id = ".db_escape($dimension)
338                  ." AND dimension2_id = ".db_escape($dimension2);
339         $result = db_query($sql,"No budget accounts were returned");
340
341         $row = db_fetch_row($result);
342         return $row[0];
343 }
344
345 //--------------------------------------------------------------------------------
346 //      Stores journal/bank transaction tax details if applicable
347 //
348 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
349 {
350         $tax_type = is_tax_account($gl_code);
351         if(!$tax_type) return;  // $gl_code is not tax account
352         
353         $tax = get_tax_type($tax_type);
354         //if ($gl_code == $tax['sales_gl_code'])
355         if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
356                 $amount = -$amount;
357         // we have to restore net amount as we cannot know the base amount
358         if ($net_amount===null) {
359                 if ($tax['rate'] == 0) {
360 //                      display_warning(_("You should not post gl transactions  
361 //                              to tax account with     zero tax rate."));
362                         $net_amount = 0;
363                 } else { 
364                         // calculate net amount
365                         $net_amount = $amount/$tax['rate']*100; 
366                 }
367         }
368         add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included, 
369                 $amount, $net_amount, $ex_rate, $date, $memo);
370
371 }
372
373 //--------------------------------------------------------------------------------
374 //
375 //      Store transaction tax details for fiscal purposes with 'freezed' 
376 //      actual tax type rate.
377 //
378 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
379         $amount, $net_amount, $ex_rate, $tran_date, $memo)
380 {
381
382         $sql = "INSERT INTO ".TB_PREF."trans_tax_details 
383                 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
384                         included_in_price, net_amount, amount, memo)
385                 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
386                                 .date2sql($tran_date)."',".db_escape($tax_id).","
387                                 .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
388                                 .db_escape($net_amount).","
389                                 .db_escape($amount).",".db_escape($memo).")";
390
391         db_query($sql, "Cannot save trans tax details");
392
393 }
394 //----------------------------------------------------------------------------------------
395
396 function get_trans_tax_details($trans_type, $trans_no)
397 {
398         $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
399                 .TB_PREF."tax_types.name AS tax_type_name, "
400                 .TB_PREF."trans_tax_details.rate AS effective_rate, "
401                 .TB_PREF."tax_types.rate AS rate
402                 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
403                 WHERE trans_type = ".db_escape($trans_type)."
404                 AND trans_no = ".db_escape($trans_no)."
405                 AND (net_amount != 0 OR amount != 0)
406                 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
407
408         return db_query($sql, "The transaction tax details could not be retrieved");
409 }
410
411 //----------------------------------------------------------------------------------------
412
413 function void_trans_tax_details($type, $type_no)
414 {
415         $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
416                 WHERE trans_no=".db_escape($type_no)
417                 ." AND trans_type=".db_escape($type);
418
419         db_query($sql, "The transaction tax details could not be voided");
420 }
421
422 //----------------------------------------------------------------------------------------
423
424 function clear_trans_tax_details($type, $type_no)
425 {
426         $sql = "DELETE FROM ".TB_PREF."trans_tax_details 
427                 WHERE trans_no=".db_escape($type_no)
428                 ." AND trans_type=".db_escape($type);
429
430         db_query($sql, "The transaction tax details could not be cleared");
431 }
432
433 function get_tax_summary($from, $to)
434 {
435         $fromdate = date2sql($from);
436         $todate = date2sql($to);
437
438         $sql = "SELECT 
439                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
440                                         ." || trans_type=".ST_JOURNAL.",-1,1)*
441                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE 
442                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
443                                         ." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
444
445                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE
446                                         ." || trans_type=".ST_JOURNAL.",-1,1)*
447                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
448                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
449                                         ." || trans_type=".ST_CUSTCREDIT.", amount*ex_rate,0)) payable,
450
451                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
452                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
453                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
454                                         ." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
455
456                                 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
457                                 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE
458                                         ." || (trans_type=".ST_JOURNAL ." AND amount<0)"
459                                         ." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
460                                 taxrec.rate,
461                                 ttype.id,
462                                 ttype.name
463                 FROM ".TB_PREF."tax_types ttype,
464                          ".TB_PREF."trans_tax_details taxrec
465                 WHERE taxrec.tax_type_id=ttype.id
466                         AND taxrec.trans_type != ".ST_CUSTDELIVERY."
467                         AND taxrec.tran_date >= '$fromdate'
468                         AND taxrec.tran_date <= '$todate'
469                 GROUP BY ttype.id";
470 //display_error($sql);
471     return db_query($sql,"Cannot retrieve tax summary");
472 }
473
474 //--------------------------------------------------------------------------------
475 // Write/update journal entries.
476 //
477 function write_journal_entries(&$cart, $reverse, $use_transaction=true)
478 {
479         global $Refs;
480
481         $date_ = $cart->tran_date;
482         $ref   = $cart->reference;
483         $memo_ = $cart->memo_;
484         $trans_type = $cart->trans_type;
485         $new = $cart->order_id == 0;
486         
487         if ($new)
488             $cart->order_id = get_next_trans_no($trans_type);
489
490     $trans_id = $cart->order_id;
491
492         if ($use_transaction)
493                 begin_transaction();
494         
495         if(!$new)
496                 void_journal_trans($trans_type, $trans_id, false);
497
498         foreach ($cart->gl_items as $journal_item)
499         {
500                 // post to first found bank account using given gl acount code.
501                 $is_bank_to = is_bank_account($journal_item->code_id);
502
503                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
504                         $journal_item->dimension_id, $journal_item->dimension2_id,
505                         $journal_item->reference, $journal_item->amount, null, 
506                         $journal_item->person_type_id, 
507                         $journal_item->person_id);
508
509         if ($is_bank_to)
510         {
511                 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
512                         $date_, $journal_item->amount,  0, "", get_company_currency(),
513                         "Cannot insert a destination bank transaction");
514         }
515                 // store tax details if the gl account is a tax account
516                 add_gl_tax_details($journal_item->code_id, 
517                         ST_JOURNAL, $trans_id, $journal_item->amount, 1, $date_, $memo_);
518         }
519         
520         $Refs->save($trans_type, $trans_id, $ref);
521         if ($new) {
522                 add_comments($trans_type, $trans_id, $date_, $memo_);
523         } else {
524                 update_comments($trans_type, $trans_id, null, $memo_);
525         }
526
527         add_audit_trail($trans_type, $trans_id, $date_);
528
529         if ($reverse)
530         {
531         //$reversingDate = date(user_date_display(),
532         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
533         $reversingDate = begin_month(add_months($date_, 1));
534
535         $trans_id_reverse = get_next_trans_no($trans_type);
536
537         foreach ($cart->gl_items as $journal_item)
538         {
539                         $is_bank_to = is_bank_account($journal_item->code_id);
540
541                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
542                         $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
543                         $journal_item->reference, -$journal_item->amount, 
544                         null, $journal_item->person_type_id, $journal_item->person_id);
545
546                 if ($is_bank_to)
547                 {
548                         add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
549                                 $reversingDate, -$journal_item->amount,
550                                 0, "", get_company_currency(),
551                                 "Cannot insert a destination bank transaction");
552                 }
553                         // store tax details if the gl account is a tax account
554                         add_gl_tax_details($journal_item->code_id, 
555                                 ST_JOURNAL, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
556         }
557
558         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
559
560         $Refs->save($trans_type, $trans_id_reverse, $ref);
561                 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
562         }
563
564         if ($use_transaction)
565                 commit_transaction();
566
567         return $trans_id;
568 }
569
570 //--------------------------------------------------------------------------------------------------
571
572 function exists_gl_trans($type, $trans_id)
573 {
574         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
575                 ." AND type_no=".db_escape($trans_id);
576         $result = db_query($sql, "Cannot retreive a gl transaction");
577
578     return (db_num_rows($result) > 0);
579 }
580
581 //--------------------------------------------------------------------------------------------------
582
583 function void_gl_trans($type, $trans_id, $nested=false)
584 {
585         if (!$nested)
586                 begin_transaction();
587
588         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
589         ." AND type_no=".db_escape($trans_id);
590
591         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
592
593         if (!$nested)
594                 commit_transaction();
595 }
596
597 //--------------------------------------------------------------------------------------------------
598
599 function clear_gl_trans($type, $trans_id, $nested=false)
600 {
601         if (!$nested)
602                 begin_transaction();
603
604         $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
605         ." AND type_no=".db_escape($trans_id);
606
607         db_query($sql, "could not clear gl transactions for type=$type and trans_no=$trans_id");
608
609         if (!$nested)
610                 commit_transaction();
611 }
612
613 //----------------------------------------------------------------------------------------
614
615 function void_journal_trans($type, $type_no, $use_transaction=true)
616 {
617         if ($use_transaction)
618                 begin_transaction();
619
620         void_bank_trans($type, $type_no, true);
621 //      void_gl_trans($type, $type_no, true);    // this is done above
622 //      void_trans_tax_details($type, $type_no); // ditto
623
624         if ($use_transaction)
625                 commit_transaction();
626 }
627
628 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false)
629 {
630
631         $sql = "SELECT  IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
632                 gl.tran_date,
633                 gl.type,
634                 gl.type_no,
635                 refs.reference,
636                 SUM(IF(gl.amount>0, gl.amount,0)) as amount,
637                 com.memo_,
638                 IF(ISNULL(u.user_id),'',u.user_id) as user_id
639                 FROM ".TB_PREF."gl_trans as gl
640                  LEFT JOIN ".TB_PREF."audit_trail as a ON
641                         (gl.type=a.type AND gl.type_no=a.trans_no)
642                  LEFT JOIN ".TB_PREF."comments as com ON
643                         (gl.type=com.type AND gl.type_no=com.id)
644                  LEFT JOIN ".TB_PREF."refs as refs ON
645                         (gl.type=refs.type AND gl.type_no=refs.id)
646                  LEFT JOIN ".TB_PREF."users as u ON
647                         a.user=u.id
648                 WHERE gl.tran_date >= '" . date2sql($from) . "'
649                 AND gl.tran_date <= '" . date2sql($to) . "'
650                 AND gl.amount!=0";
651         if ($ref) {
652                 $sql .= " AND reference LIKE ". db_escape("%$ref%");
653         }
654         if ($memo) {
655                 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
656         }
657         if ($filter != -1) {
658                 $sql .= " AND gl.type=".db_escape($filter);
659         }
660         if (!$alsoclosed) {
661                 $sql .= " AND gl_seq=0";
662         }
663         else
664                 $sql .= " AND NOT ISNULL(a.gl_seq)";
665
666         $sql .= " GROUP BY tran_date, gl_seq, gl.type, gl.type_no";
667
668         return $sql;
669 }
670 ?>