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