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