Moving bugfixes from 2.3 to 2.4 and updated install languages for Denmark and Sweden.
[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 $SysPrefs;
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($SysPrefs->use_audit_trail) && $SysPrefs->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         if ($type != ST_WORKORDER && (!is_subledger_account($account) || $account==get_company_pref('grn_clearing_act')))
46                 $person_id = $person_type_id = null;
47         
48         $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
49                 account, dimension_id, dimension2_id, memo_, amount";
50
51         if ($person_type_id != null)
52                 $sql .= ", person_type_id, person_id";
53
54         $sql .= ") ";
55
56         $sql .= "VALUES (".db_escape($type).", ".db_escape($trans_id).", '$date',
57                 ".db_escape($account).", ".db_escape($dimension).", "
58                 .db_escape($dimension2).", ".db_escape($memo_).", "
59                 .db_escape($amount_in_home_currency);
60
61         if ($person_type_id != null)
62                 $sql .= ", ".db_escape($person_type_id).", ". db_escape($person_id);
63
64         $sql .= ") ";
65
66         if ($err_msg == "")
67                 $err_msg = "The GL transaction could not be inserted";
68
69         db_query($sql, $err_msg);
70         return $amount_in_home_currency;
71 }
72
73 //--------------------------------------------------------------------------------
74
75 // GL Trans for standard costing, always home currency regardless of person
76 // $date_ is display date (non-sql)
77 // $amount is in HOME currency
78
79 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
80         $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
81 {
82         if ($amount != 0)
83                 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
84                         $amount, null, $person_type_id, $person_id, $err_msg);
85         else
86                 return 0;
87 }
88
89 // Function for even out rounding problems
90 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
91 {
92         $amount = round2($amount, user_price_dec());
93
94         if (floatcmp($amount, 0))
95         {
96                 error_log(sprintf( _("Rounding error %s encountered for trans_type:%s,trans_no:%s"), $amount, $type, $trans_id));
97                 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
98                         $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
99         } else
100                 return 0;
101 }
102
103 //--------------------------------------------------------------------------------
104
105 function get_gl_transactions($from_date, $to_date, $trans_no=0,
106         $account=null, $dimension=0, $dimension2=0, $filter_type=null,
107         $amount_min=null, $amount_max=null, $person_id=null)
108 {
109         global $SysPrefs;
110
111         $from = date2sql($from_date);
112         $to = date2sql($to_date);
113
114         $sql = "SELECT gl.*, j.event_date, j.doc_date, a.gl_seq, u.user_id, st.supp_reference, gl.person_id subcode,
115                         IFNULL(IFNULL(sup.supp_name, debt.name), bt.person_id) as person_name, 
116                         IFNULL(gl.person_id, IFNULL(sup.supplier_id, debt.debtor_no)) as person_id,
117                         IFNULL(st.tran_date, IFNULL(dt.tran_date, IFNULL(bt.trans_date, IFNULL(grn.delivery_date, gl.tran_date)))) as doc_date,
118                         coa.account_name, ref.reference
119                          FROM "
120                         .TB_PREF."gl_trans gl
121                         LEFT JOIN ".TB_PREF."voided v ON gl.type_no=v.id AND v.type=gl.type
122
123                         LEFT JOIN ".TB_PREF."supp_trans st ON gl.type_no=st.trans_no AND st.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=st.supplier_id)
124                         LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=gl.type_no AND gl.type=".ST_SUPPRECEIVE." AND gl.person_id=grn.supplier_id
125                         LEFT JOIN ".TB_PREF."debtor_trans dt ON gl.type_no=dt.trans_no AND dt.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=dt.debtor_no)
126
127                         LEFT JOIN ".TB_PREF."suppliers sup ON st.supplier_id=sup.supplier_id OR grn.supplier_id=sup.supplier_id
128                         LEFT JOIN ".TB_PREF."cust_branch branch ON dt.branch_code=branch.branch_code
129                         LEFT JOIN ".TB_PREF."debtors_master debt ON dt.debtor_no=debt.debtor_no
130
131                         LEFT JOIN ".TB_PREF."bank_trans bt ON bt.type=gl.type AND bt.trans_no=gl.type_no AND bt.amount!=0
132                                  AND bt.person_type_id=gl.person_type_id AND bt.person_id=gl.person_id
133
134                         LEFT JOIN ".TB_PREF."journal j ON j.type=gl.type AND j.trans_no=gl.type_no
135                         LEFT JOIN ".TB_PREF."audit_trail a ON a.type=gl.type AND a.trans_no=gl.type_no AND NOT ISNULL(gl_seq)
136                         LEFT JOIN ".TB_PREF."users u ON a.user=u.id
137
138                         LEFT JOIN ".TB_PREF."refs ref ON ref.type=gl.type AND ref.id=gl.type_no,"
139                 .TB_PREF."chart_master coa
140                 WHERE coa.account_code=gl.account
141                 AND ISNULL(v.date_)
142                 AND gl.tran_date >= '$from'
143                 AND gl.tran_date <= '$to'";
144         if (isset($SysPrefs->show_voided_gl_trans) && $SysPrefs->show_voided_gl_trans == 0)
145                 $sql .= " AND gl.amount <> 0"; 
146
147         if ($person_id)
148                 $sql .= " AND gl.person_id=".db_escape($person_id); 
149
150         if ($trans_no > 0)
151                 $sql .= " AND gl.type_no LIKE ".db_escape('%'.$trans_no);;
152
153         if ($account != null)
154                 $sql .= " AND gl.account = ".db_escape($account);
155
156         if ($dimension > 0)
157                 $sql .= " AND gl.dimension_id = ".($dimension<0 ? 0 : db_escape($dimension));
158
159         if ($dimension2 > 0)
160                 $sql .= " AND gl.dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2));
161
162         if ($filter_type != null AND is_numeric($filter_type))
163                 $sql .= " AND gl.type= ".db_escape($filter_type);
164
165         if ($amount_min != null)
166                 $sql .= " AND ABS(gl.amount) >= ABS(".db_escape($amount_min).")";
167         
168         if ($amount_max != null)
169                 $sql .= " AND ABS(gl.amount) <= ABS(".db_escape($amount_max).")";
170
171         $sql .= " ORDER BY tran_date, counter";
172
173         return db_query($sql, "The transactions for could not be retrieved");
174 }
175
176
177 //--------------------------------------------------------------------------------
178
179 function get_gl_trans($type, $trans_id)
180 {
181         $sql = "SELECT gl.*, cm.account_name, IFNULL(refs.reference, '') AS reference, user.real_name, 
182                         COALESCE(st.tran_date, dt.tran_date, bt.trans_date, grn.delivery_date, gl.tran_date) as doc_date,
183                         IF(ISNULL(st.supp_reference), '', st.supp_reference) AS supp_reference
184         FROM ".TB_PREF."gl_trans as gl
185                 LEFT JOIN ".TB_PREF."chart_master as cm ON gl.account = cm.account_code
186                 LEFT JOIN ".TB_PREF."refs as refs ON (gl.type=refs.type AND gl.type_no=refs.id)
187                 LEFT JOIN ".TB_PREF."audit_trail as audit ON (gl.type=audit.type AND gl.type_no=audit.trans_no AND NOT ISNULL(gl_seq))
188                 LEFT JOIN ".TB_PREF."users as user ON (audit.user=user.id)
189         # all this below just to retrieve doc_date :>
190                 LEFT JOIN ".TB_PREF."supp_trans st ON gl.type_no=st.trans_no AND st.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=st.supplier_id)
191                 LEFT JOIN ".TB_PREF."grn_batch grn ON grn.id=gl.type_no AND gl.type=".ST_SUPPRECEIVE." AND gl.person_id=grn.supplier_id
192                 LEFT JOIN ".TB_PREF."debtor_trans dt ON gl.type_no=dt.trans_no AND dt.type=gl.type AND (gl.type!=".ST_JOURNAL." OR gl.person_id=dt.debtor_no)
193                 LEFT JOIN ".TB_PREF."bank_trans bt ON bt.type=gl.type AND bt.trans_no=gl.type_no AND bt.amount!=0
194                          AND bt.person_type_id=gl.person_type_id AND bt.person_id=gl.person_id
195                 LEFT JOIN ".TB_PREF."journal j ON j.type=gl.type AND j.trans_no=gl.type_no"
196
197                 ." WHERE gl.type= ".db_escape($type) 
198                 ." AND gl.type_no = ".db_escape($trans_id)
199                 ." AND gl.amount <> 0"
200                 ." ORDER BY tran_date, counter";
201
202         return db_query($sql, "The gl transactions could not be retrieved");
203 }
204
205 //--------------------------------------------------------------------------------
206
207 function get_gl_wo_cost_trans($trans_id, $cost_type=-1)
208 {
209         $sql = "SELECT gl.*, chart.account_name FROM ".TB_PREF."gl_trans gl, ".TB_PREF."chart_master chart
210                 WHERE chart.account_code=gl.account AND gl.type=".ST_WORKORDER." AND gl.type_no=".db_escape($trans_id)."
211                 AND gl.person_type_id=".PT_WORKORDER;
212         if ($cost_type!= -1)
213                 $sql .= " AND gl.person_id=".db_escape($cost_type);
214         $sql .= " AND amount < 0";
215
216         return db_query($sql, "The gl transactions could not be retrieved");
217 }
218
219 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
220 {
221         $from = date2sql($from_date);
222         $to = date2sql($to_date);
223
224     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
225                 WHERE account='$account'";
226         if ($from_date != "")
227                 $sql .= "  AND tran_date > '$from'";
228         if ($to_date != "")
229                 $sql .= "  AND tran_date < '$to'";
230         if ($dimension != 0)
231                 $sql .= " AND dimension_id = ".($dimension<0 ? 0 : db_escape($dimension));
232         if ($dimension2 != 0)
233                 $sql .= " AND dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2));
234
235         $result = db_query($sql, "The starting balance for account $account could not be calculated");
236
237         $row = db_fetch_row($result);
238         return $row[0];
239 }
240
241 //--------------------------------------------------------------------------------
242
243 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
244 {
245         $from = date2sql($from_date);
246         $to = date2sql($to_date);
247
248     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
249                 WHERE account='$account'";
250         if ($from_date != "")
251                 $sql .= " AND tran_date >= '$from'";
252         if ($to_date != "")
253                 $sql .= " AND tran_date <= '$to'";
254         if ($dimension != 0)
255                 $sql .= " AND dimension_id = ".($dimension<0 ? 0 : db_escape($dimension));
256         if ($dimension2 != 0)
257                 $sql .= " AND dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2));
258
259         $result = db_query($sql, "Transactions for account $account could not be calculated");
260
261         $row = db_fetch_row($result);
262         return (float)$row[0];
263 }
264
265 //----------------------------------------------------------------------------------------------------
266 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
267 {
268         $from_date = date2sql($from);
269         $to_date = date2sql($to);
270
271         $sql = "SELECT  SUM(IF(amount >= 0, amount, 0)) as debit, 
272                                         SUM(IF(amount < 0, -amount, 0)) as credit,
273                                         SUM(amount) as balance 
274                 FROM ".TB_PREF."gl_trans trans,"
275                         .TB_PREF."chart_master coa,"
276                         .TB_PREF."chart_types act_type, "
277                         .TB_PREF."chart_class act_class
278                 WHERE trans.account=coa.account_code
279                         AND coa.account_type=act_type.id 
280                 AND act_type.class_id=act_class.cid"
281                 ." AND ".($from_incl ? "tran_date >= '$from_date'" : "tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date')")
282                 ." AND ".($to_incl ? "tran_date <= '$to_date'" : "tran_date < '$to_date'")
283                 .($account == null ? '' : " AND account=".db_escape($account))
284                 .($dimension == 0 ? ''  : " AND dimension_id = ".($dimension<0 ? 0 : db_escape($dimension)))
285                 .($dimension2 == 0 ? '' : " AND dimension2_id = ".($dimension2<0 ? 0 : db_escape($dimension2)));
286
287         $result = db_query($sql,"No general ledger accounts were returned");
288
289         return db_fetch($result);
290 }
291
292 //--------------------------------------------------------------------------------
293
294 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
295 {
296
297         $from = date2sql($from_date);
298         $to = date2sql($to_date);
299
300         $sql = "SELECT SUM(amount)
301                 FROM ".TB_PREF."budget_trans
302                 WHERE account=".db_escape($account);
303         if ($from_date != "")
304                 $sql .= " AND tran_date >= '$from' ";
305         if ($to_date != "")
306                 $sql .= " AND tran_date <= '$to' ";
307         if ($dimension != 0)
308                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
309         if ($dimension2 != 0)
310                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
311         $result = db_query($sql,"No budget accounts were returned");
312
313         $row = db_fetch_row($result);
314         return $row[0];
315 }
316 //-------------------------------------------------------------------------------------
317
318 function exists_gl_budget($date_, $account, $dimension, $dimension2)
319 {
320         $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
321         ." AND tran_date='$date_' AND
322                 dimension_id=".db_escape($dimension)." AND dimension2_id=".db_escape($dimension2);
323         $result = db_query($sql, "Cannot retreive a gl transaction");
324
325     return (db_num_rows($result) > 0);
326 }
327
328 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
329 {
330         $date = date2sql($date_);
331
332         if (exists_gl_budget($date, $account, $dimension, $dimension2))
333                 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=".db_escape($amount)
334                 ." WHERE account=".db_escape($account)
335                 ." AND dimension_id=".db_escape($dimension)
336                 ." AND dimension2_id=".db_escape($dimension2)
337                 ." AND tran_date='$date'";
338         else
339                 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
340                         account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
341                         ".db_escape($account).", ".db_escape($dimension).", "
342                         .db_escape($dimension2).", ".db_escape($amount).", '')";
343
344         db_query($sql, "The GL budget transaction could not be saved");
345 }
346
347 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
348 {
349         $date = date2sql($date_);
350
351         $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account=".db_escape($account)
352         ." AND dimension_id=".db_escape($dimension)
353         ." AND dimension2_id=".db_escape($dimension2)
354         ." AND tran_date='$date'";
355         db_query($sql, "The GL budget transaction could not be deleted");
356 }
357
358 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
359 {
360
361         $from = date2sql($from_date);
362         $to = date2sql($to_date);
363
364         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
365                 WHERE account=".db_escape($account)
366                 ." AND tran_date >= '$from' AND tran_date <= '$to'
367                  AND dimension_id = ".db_escape($dimension)
368                  ." AND dimension2_id = ".db_escape($dimension2);
369         $result = db_query($sql,"No budget accounts were returned");
370
371         $row = db_fetch_row($result);
372         return $row[0];
373 }
374
375 //--------------------------------------------------------------------------------
376 //      Stores journal/bank transaction tax details if applicable
377 //
378 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo, $included=0, $net_amount = null)
379 {
380         $tax_type = is_tax_account($gl_code);
381         if(!$tax_type) return;  // $gl_code is not tax account
382         
383         $tax = get_tax_type($tax_type);
384         if ($trans_type == ST_SALESINVOICE || $trans_type == ST_CUSTDELIVERY || $trans_type == ST_CUSTCREDIT)
385                 $amount = -$amount;
386         // we have to restore net amount as we cannot know the base amount
387         if ($net_amount===null) {
388                 if ($tax['rate'] == 0) {
389                         $net_amount = 0;
390                 } else { 
391                         // calculate net amount
392                         $net_amount = $amount/$tax['rate']*100; 
393                 }
394         }
395         add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], $included, 
396                 $amount, $net_amount, $ex_rate, $date, $memo, null);
397
398 }
399
400 //--------------------------------------------------------------------------------
401 //
402 //      Store transaction tax details for fiscal purposes with 'freezed' 
403 //      actual tax type rate.
404 //
405 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
406         $amount, $net_amount, $ex_rate, $tran_date, $memo, $reg_type=null)
407 {
408         // guess tax register if not set
409         if (!isset($reg_type))
410                 $reg_type = in_array($trans_type, array(ST_SUPPINVOICE, ST_SUPPCREDIT)) ? TR_OUTPUT
411                                 : in_array($trans_type, array(ST_SALESINVOICE, ST_CUSTCREDIT)) ? TR_INPUT : null;
412
413         $sql = "INSERT INTO ".TB_PREF."trans_tax_details 
414                 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
415                         included_in_price, net_amount, amount, memo, reg_type)
416                 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
417                                 .date2sql($tran_date)."',".db_escape($tax_id).","
418                                 .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
419                                 .db_escape($net_amount).","
420                                 .db_escape($amount).",".db_escape($memo).",".db_escape($reg_type, true).")";
421
422         db_query($sql, "Cannot save trans tax details");
423
424 }
425 //----------------------------------------------------------------------------------------
426
427 function get_trans_tax_details($trans_type, $trans_no)
428 {
429         $sql = "SELECT tax_details.*,
430                                 tax_type.name AS tax_type_name,
431                                 tax_details.rate AS effective_rate,
432                                 tax_type.rate AS rate
433                 FROM ".TB_PREF."trans_tax_details tax_details,
434                         ".TB_PREF."tax_types tax_type
435                 WHERE 
436                         trans_type = ".db_escape($trans_type)."
437                 AND trans_no = ".db_escape($trans_no)."
438                 AND (net_amount != 0 OR amount != 0)
439                 AND tax_type.id = tax_details.tax_type_id";
440
441         return db_query($sql, "The transaction tax details could not be retrieved");
442 }
443
444 //----------------------------------------------------------------------------------------
445
446 function void_trans_tax_details($type, $type_no)
447 {
448         $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
449                 WHERE trans_no=".db_escape($type_no)
450                 ." AND trans_type=".db_escape($type);
451
452         db_query($sql, "The transaction tax details could not be voided");
453 }
454
455 //----------------------------------------------------------------------------------------
456
457 function clear_trans_tax_details($type, $type_no)
458 {
459         $sql = "DELETE FROM ".TB_PREF."trans_tax_details 
460                 WHERE trans_no=".db_escape($type_no)
461                 ." AND trans_type=".db_escape($type);
462
463         db_query($sql, "The transaction tax details could not be cleared");
464 }
465
466 function get_tax_summary($from, $to, $also_zero_purchases=false)
467 {
468         $fromdate = date2sql($from);
469         $todate = date2sql($to);
470
471         $sql = "SELECT 
472                                 SUM(IF(trans_type=".ST_SUPPCREDIT.",-1,1)*
473                                 IF((reg_type=".TR_OUTPUT.")"
474                                         ." || ((trans_type IN(".ST_SUPPINVOICE.",".ST_SUPPCREDIT.") OR (trans_type=".ST_JOURNAL." AND reg_type=".TR_INPUT."))
475                                         ), net_amount*ex_rate,0)
476                                 ) net_output,
477
478                                 SUM(IF(trans_type=".ST_SUPPCREDIT.",-1,1)*
479                                 IF((reg_type=".TR_OUTPUT.")"
480                                         ." || ((trans_type IN(".ST_SUPPINVOICE.",".ST_SUPPCREDIT.") OR (trans_type=".ST_JOURNAL." AND reg_type=".TR_INPUT."))
481                                         ), amount*ex_rate,0)) payable,
482
483                                 SUM(IF(trans_type IN(".ST_SUPPCREDIT."),-1,1)*
484                                 IF(reg_type=".TR_INPUT
485                                         . ($also_zero_purchases ? '': " AND tax_type_id AND taxrec.rate")
486                                         .", net_amount*ex_rate, 0)) net_input,
487
488                                 SUM(IF(trans_type IN(".ST_SUPPCREDIT."),-1,1)*
489                                 IF(reg_type=".TR_INPUT
490                                         . ($also_zero_purchases ? '': " AND tax_type_id AND taxrec.rate ") 
491                                         .", amount*ex_rate, 0)) collectible,
492                                 taxrec.rate,
493                                 ttype.id,
494                                 ttype.name
495                 FROM ".TB_PREF."trans_tax_details taxrec LEFT JOIN ".TB_PREF."tax_types ttype ON taxrec.tax_type_id=ttype.id
496                 WHERE taxrec.trans_type IN (".implode(',',
497                         array(ST_SALESINVOICE, ST_CUSTCREDIT, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_JOURNAL)).")
498                         AND taxrec.tran_date >= '$fromdate'
499                         AND taxrec.tran_date <= '$todate'
500                 GROUP BY ttype.id";
501
502                 // display_error($sql);
503     return db_query($sql,"Cannot retrieve tax summary");
504 }
505
506 //--------------------------------------------------------------------------------------------------
507
508 function exists_gl_trans($type, $trans_id)
509 {
510         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
511                 ." AND type_no=".db_escape($trans_id);
512         $result = db_query($sql, "Cannot retreive a gl transaction");
513
514     return (db_num_rows($result) > 0);
515 }
516
517 //--------------------------------------------------------------------------------------------------
518
519 function void_gl_trans($type, $trans_id, $nested=false)
520 {
521         if (!$nested)
522                 begin_transaction();
523
524         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
525         ." AND type_no=".db_escape($trans_id);
526
527         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
528
529         if (!$nested)
530                 commit_transaction();
531 }
532
533 //----------------------------------------------------------------------------------------
534
535 function clear_gl_trans($type, $trans_id, $nested=false)
536 {
537         if (!$nested)
538                 begin_transaction();
539
540         $sql = "DELETE FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
541         ." AND type_no=".db_escape($trans_id);
542
543         db_query($sql, "could not clear gl transactions for type=$type and trans_no=$trans_id");
544
545         if (!$nested)
546                 commit_transaction();
547 }
548
549 function get_sql_for_journal_inquiry($filter, $from, $to, $ref='', $memo='', $alsoclosed=false,
550                  $user_id=null, $contractor_id=null, $dimension=null)
551 {
552
553         $sql = "SELECT  IFNULL(a.gl_seq,0) as gl_seq,
554                 gl.tran_date,
555                 gl.type as trans_type,
556                 gl.type_no as trans_no,
557                 IFNULL(MAX(supp.supp_name), MAX(cust.name)) as name,
558                 IF(ISNULL(st.supp_reference), '', st.supp_reference) AS supp_reference,
559                 refs.reference,"
560                 .($dimension ? " -SUM(IF(dim.dimension in(gl.dimension_id,gl.dimension2_id), gl.amount, 0)) as amount,":" SUM(IF(gl.amount>0, gl.amount,0)) as amount,")
561                 ."com.memo_,
562                 IF(ISNULL(u.user_id),'',u.user_id) as user_id";
563
564         if ($contractor_id > 0) {
565                 $sql.= ", st.supplier_id, dt.debtor_no ";
566         }
567
568         $sql.= " FROM ".TB_PREF."gl_trans as gl
569                  LEFT JOIN ".TB_PREF."audit_trail as a ON
570                         (gl.type=a.type AND gl.type_no=a.trans_no)
571                  LEFT JOIN ".TB_PREF."comments as com ON
572                         (gl.type=com.type AND gl.type_no=com.id)
573                  LEFT JOIN ".TB_PREF."refs as refs ON
574                         (gl.type=refs.type AND gl.type_no=refs.id)
575                  LEFT JOIN ".TB_PREF."users as u ON
576                         a.user=u.id
577                  LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=gl.type AND gl.type_no=dt.trans_no
578                  LEFT JOIN ".TB_PREF."debtors_master cust ON gl.person_type_id=2 AND gl.person_id=cust.debtor_no
579                  LEFT JOIN ".TB_PREF."supp_trans st ON st.type=gl.type AND gl.type_no=st.trans_no
580                  LEFT JOIN ".TB_PREF."suppliers supp ON gl.person_type_id=3 AND gl.person_id=supp.supplier_id"
581                  .($dimension ? 
582                  " LEFT JOIN (SELECT type, type_no, MAX(IFNULL(dimension_id, dimension2_id)) dimension FROM ".TB_PREF."gl_trans GROUP BY type, type_no) dim 
583                                 ON gl.type=dim.type AND gl.type_no=dim.type_no" : '')
584                 ." WHERE gl.tran_date >= '" . date2sql($from) . "'
585                 AND gl.tran_date <= '" . date2sql($to) . "'
586                 AND gl.amount!=0";
587         if ($ref) {
588                 $sql .= " AND refs.reference LIKE ". db_escape("%$ref%");
589         }
590         if ($memo) {
591                 $sql .= " AND com.memo_ LIKE ". db_escape("%$memo%");
592         }
593         if ($filter != -1) {
594                 $sql .= " AND gl.type=".db_escape($filter);
595         }
596         if (!$alsoclosed) {
597                 $sql .= " AND gl_seq=0";
598         }
599         else
600                 $sql .= " AND NOT ISNULL(a.gl_seq)";
601
602         if ($user_id != null)
603                 $sql .= " AND user_id = ".db_escape($user_id);
604
605         if ($contractor_id > 0) {
606                 $sql.= " AND (dt.debtor_no =".$contractor_id;
607                 $sql.= " OR st.supplier_id =".$contractor_id.") ";
608         }       
609
610         if ($dimension != null)
611                 $sql .= " AND dim.dimension = ".db_escape($dimension);
612
613         $sql .= " GROUP BY gl.tran_date, a.gl_seq, gl.type, gl.type_no";
614
615         return $sql;
616 }