Code clean-up
[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 ($type, $trans_id, '$date',
54                 '$account', $dimension, $dimension2, ".db_escape($memo_).", $amount_in_home_currency";
55
56         if ($person_type_id != null)
57                 $sql .= ", $person_type_id, ". db_escape($person_id);
58
59         $sql .= ") ";
60
61         if ($err_msg == "")
62                 $err_msg = "The GL transaction could not be inserted";
63
64         db_query($sql, $err_msg);
65         return $amount_in_home_currency;
66 }
67
68 //--------------------------------------------------------------------------------
69
70 // GL Trans for standard costing, always home currency regardless of person
71 // $date_ is display date (non-sql)
72 // $amount is in HOME currency
73
74 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
75         $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
76 {
77         if ($amount != 0)
78                 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
79                         $amount, null, $person_type_id, $person_id, $err_msg);
80         else
81                 return 0;
82 }
83
84 // Function for even out rounding problems
85 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
86 {
87         $amount = round2($amount, user_price_dec());
88         if ($amount != 0)
89                 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
90                         $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
91         else
92                 return 0;
93 }       
94
95 //--------------------------------------------------------------------------------
96
97 function get_gl_transactions($from_date, $to_date, $trans_no=0,
98         $account=null, $dimension=0, $dimension2=0, $filter_type=null)
99 {
100         $from = date2sql($from_date);
101         $to = date2sql($to_date);
102
103         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
104                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
105                 AND tran_date >= '$from'
106                 AND tran_date <= '$to'";
107         if ($trans_no > 0)
108                 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
109
110         if ($account != null)
111                 $sql .= " AND ".TB_PREF."gl_trans.account = '$account'";
112
113         if ($dimension > 0)
114                 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
115
116         if ($dimension2 > 0)
117                 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
118
119         if ($filter_type != null AND is_numeric($filter_type))
120                 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
121
122         $sql .= " ORDER BY tran_date";
123
124         return db_query($sql, "The transactions for could not be retrieved");
125 }
126
127
128 //--------------------------------------------------------------------------------
129
130 function get_gl_trans($type, $trans_id)
131 {
132         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
133                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
134                 AND ".TB_PREF."gl_trans.type=$type AND ".TB_PREF."gl_trans.type_no=$trans_id";
135
136         return db_query($sql, "The gl transactions could not be retrieved");
137 }
138
139 //--------------------------------------------------------------------------------
140
141 function get_gl_wo_cost_trans($trans_id, $person_id=-1)
142 {
143         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
144                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
145                 AND ".TB_PREF."gl_trans.type=".systypes::work_order()." AND ".TB_PREF."gl_trans.type_no=$trans_id
146                 AND ".TB_PREF."gl_trans.person_type_id=".payment_person_types::WorkOrder();
147         if ($person_id != -1)
148                 $sql .= " AND ".TB_PREF."gl_trans.person_id=$person_id";
149         $sql .= " AND amount < 0";      
150
151         return db_query($sql, "The gl transactions could not be retrieved");
152 }
153
154 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
155 {
156         $from = date2sql($from_date);
157         $to = date2sql($to_date);
158
159     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
160                 WHERE account='$account'";
161         if ($from_date != "")
162                 $sql .= "  AND tran_date > '$from'";
163         if ($to_date != "")
164                 $sql .= "  AND tran_date < '$to'";
165         if ($dimension > 0)
166                 $sql .= " AND dimension_id = $dimension";
167         if ($dimension2 > 0)
168                 $sql .= " AND dimension2_id = $dimension2";
169
170         $result = db_query($sql, "The starting balance for account $account could not be calculated");
171
172         $row = db_fetch_row($result);
173         return $row[0];
174 }
175
176 //--------------------------------------------------------------------------------
177
178 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
179 {
180         $from = date2sql($from_date);
181         $to = date2sql($to_date);
182
183     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
184                 WHERE account='$account'";
185         if ($from_date != "")
186                 $sql .= " AND tran_date >= '$from'";
187         if ($to_date != "")
188                 $sql .= " AND tran_date <= '$to'";
189         if ($dimension > 0)
190                 $sql .= " AND dimension_id = $dimension";
191         if ($dimension2 > 0)
192                 $sql .= " AND dimension2_id = $dimension2";
193
194         $result = db_query($sql, "Transactions for account $account could not be calculated");
195
196         $row = db_fetch_row($result);
197         return $row[0];
198 }
199
200 //----------------------------------------------------------------------------------------------------
201 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
202 {
203         $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance 
204                 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class 
205                 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id 
206                 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
207                 
208         if ($account != null)
209                 $sql .= " account='$account' AND";
210         if ($dimension > 0)
211                 $sql .= " dimension_id=$dimension AND";
212         if ($dimension2 > 0)
213                 $sql .= " dimension2_id=$dimension2 AND";
214         $from_date = date2sql($from);
215         if ($from_incl)
216                 $sql .= " tran_date >= '$from_date'  AND";
217         else
218                 $sql .= " tran_date > IF(balance_sheet>0 AND balance_sheet<".CL_INCOME.", '0000-00-00', '$from_date') AND";
219         $to_date = date2sql($to);
220         if ($to_incl)
221                 $sql .= " tran_date <= '$to_date' ";
222         else
223                 $sql .= " tran_date < '$to_date' ";
224
225         $result = db_query($sql,"No general ledger accounts were returned");
226
227         return db_fetch($result);
228 }
229
230 //--------------------------------------------------------------------------------
231
232 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
233 {
234
235         $from = date2sql($from_date);
236         $to = date2sql($to_date);
237
238         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
239                 WHERE account='$account' ";
240         if ($from_date != "")
241                 $sql .= " AND tran_date >= '$from' ";
242         if ($to_date != "")
243                 $sql .= " AND tran_date <= '$to' ";
244         if ($dimension > 0)
245                 $sql .= " AND dimension_id = $dimension";
246         if ($dimension2 > 0)
247                 $sql .= " AND dimension2_id = $dimension2";
248         $result = db_query($sql,"No budget accounts were returned");
249
250         $row = db_fetch_row($result);
251         return $row[0];
252 }
253
254 //--------------------------------------------------------------------------------
255 //      Stores journal/bank transaction tax details if applicable
256 //
257 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo)
258 {
259         $tax_type = is_tax_account($gl_code);
260         if(!$tax_type) return;  // $gl_code is not tax account
261         
262         $tax = get_tax_type($tax_type);
263         if ($gl_code == $tax['sales_gl_code']) 
264                 $amount = -$amount;
265         // we have to restore net amount as we cannot know the base amount
266         if ($tax['rate'] == 0) {
267 //              display_warning(_("You should not post gl transactions  
268 //                      to tax account with     zero tax rate."));
269                 $net_amount = 0;
270         } else { 
271                 // calculate net amount
272                 $net_amount = $amount/$tax['rate']*100; 
273         }
274                 
275         add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], 0, 
276                 $amount, $net_amount, $ex_rate, $date, $memo);
277                         
278 }
279
280 //--------------------------------------------------------------------------------
281 //
282 //      Store transaction tax details for fiscal purposes with 'freezed' 
283 //      actual tax type rate.
284 //
285 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
286         $amount, $net_amount, $ex_rate, $tran_date, $memo)
287 {
288
289         $sql = "INSERT INTO ".TB_PREF."trans_tax_details 
290                 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
291                         included_in_price, net_amount, amount, memo)
292                 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
293                                 .date2sql($tran_date)."',".db_escape($tax_id).","
294                                 .$rate.",".$ex_rate.",".($included ? 1:0).","
295                                 .db_escape($net_amount).","
296                                 .db_escape($amount).",".db_escape($memo).")";
297
298         db_query($sql, "Cannot save trans tax details");
299
300 }
301 //----------------------------------------------------------------------------------------
302
303 function get_trans_tax_details($trans_type, $trans_no)
304 {
305         $sql = "SELECT ".TB_PREF."trans_tax_details.*, ".TB_PREF."tax_types.name AS tax_type_name
306                 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
307                 WHERE trans_type = $trans_type
308                 AND trans_no = $trans_no
309                 AND (net_amount != 0 OR amount != 0)
310                 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
311
312         return db_query($sql, "The transaction tax details could not be retrieved");
313 }
314
315 //----------------------------------------------------------------------------------------
316
317 function void_trans_tax_details($type, $type_no)
318 {
319         $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
320                 WHERE trans_no=$type_no
321                 AND trans_type=$type";
322
323         db_query($sql, "The transaction tax details could not be voided");
324 }
325
326 function get_tax_summary($from, $to)
327 {
328         $fromdate = date2sql($from);
329         $todate = date2sql($to);
330
331         $sql = "SELECT 
332                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
333                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, net_amount*ex_rate,0)) net_output,
334                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
335                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, amount*ex_rate,0)) payable,
336                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
337                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, net_amount*ex_rate)) net_input,
338                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
339                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, amount*ex_rate)) collectible,
340                                 taxrec.rate,
341                                 ttype.id,
342                                 ttype.name
343                 FROM ".TB_PREF."tax_types ttype,
344                          ".TB_PREF."trans_tax_details taxrec
345                 WHERE taxrec.tax_type_id=ttype.id
346                         AND taxrec.trans_type != 13
347                         AND taxrec.tran_date >= '$fromdate'
348                         AND taxrec.tran_date <= '$todate'
349                 GROUP BY ttype.id";
350 //display_error($sql);
351     return db_query($sql,"Cannot retrieve tax summary");
352 }
353 //--------------------------------------------------------------------------------
354 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
355 {
356         begin_transaction();
357
358         $trans_type = systypes::journal_entry();
359         $trans_id = get_next_trans_no($trans_type);
360
361         foreach ($items as $journal_item)
362         {
363                 // post to first found bank account using given gl acount code.
364                 $is_bank_to = is_bank_account($journal_item->code_id);
365
366                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
367                         $journal_item->dimension_id, $journal_item->dimension2_id,
368                         $journal_item->reference, $journal_item->amount);
369         if ($is_bank_to)
370         {
371                 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
372                         $date_, $journal_item->amount,
373                         0, "", get_company_currency(),
374                         "Cannot insert a destination bank transaction");
375         }
376                 // store tax details if the gl account is a tax account
377                 add_gl_tax_details($journal_item->code_id, 
378                         $trans_type, $trans_id, -$journal_item->amount, 1, $date_, $memo_);
379         }
380         
381         add_comments($trans_type, $trans_id, $date_, $memo_);
382
383         references::save($trans_type, $trans_id, $ref);
384
385         if ($reverse)
386         {
387
388         //$reversingDate = date(user_date_display(),
389         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
390         $reversingDate = begin_month(add_months($date_, 1));
391
392         $trans_id_reverse = get_next_trans_no($trans_type);
393
394         foreach ($items as $journal_item)
395         {
396                         $is_bank_to = is_bank_account($journal_item->code_id);
397
398                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
399                         $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
400                         $journal_item->reference, -$journal_item->amount);
401                 if ($is_bank_to)
402                 {
403                         add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
404                                 $reversingDate, $journal_item->amount,
405                                 0, "", get_company_currency(),
406                                 "Cannot insert a destination bank transaction");
407                 }
408                         // store tax details if the gl account is a tax account
409                         add_gl_tax_details($journal_item->code_id, 
410                                 $trans_type, $trans_id, $journal_item->amount, 1, $date, $memo_);
411         }
412
413         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
414
415         references::save($trans_type, $trans_id_reverse, $ref);
416         }
417
418         commit_transaction();
419
420         return $trans_id;
421 }
422
423 //--------------------------------------------------------------------------------------------------
424
425 function exists_gl_trans($type, $trans_id)
426 {
427         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
428         $result = db_query($sql, "Cannot retreive a gl transaction");
429
430     return (db_num_rows($result) > 0);
431 }
432
433 //--------------------------------------------------------------------------------------------------
434
435 function void_gl_trans($type, $trans_id, $nested=false)
436 {
437         if (!$nested)
438                 begin_transaction();
439
440         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
441
442         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
443
444         if (!$nested)
445                 commit_transaction();
446 }
447
448 //----------------------------------------------------------------------------------------
449
450 function void_journal_trans($type, $type_no)
451 {
452         begin_transaction();
453
454         void_gl_trans($type, $type_no, true);
455         void_bank_trans($type, $type_no,true);
456         void_trans_tax_details($type, $type_no);
457
458         commit_transaction();
459 }
460
461 ?>