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