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