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