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