Support for periodic journal trans closing/indexing.
[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_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
142 {
143         $from = date2sql($from_date);
144         $to = date2sql($to_date);
145
146     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
147                 WHERE account='$account'";
148         if ($from_date != "")
149                 $sql .= "  AND tran_date > '$from'";
150         if ($to_date != "")
151                 $sql .= "  AND tran_date < '$to'";
152         if ($dimension > 0)
153                 $sql .= " AND dimension_id = $dimension";
154         if ($dimension2 > 0)
155                 $sql .= " AND dimension2_id = $dimension2";
156
157         $result = db_query($sql, "The starting balance for account $account could not be calculated");
158
159         $row = db_fetch_row($result);
160         return $row[0];
161 }
162
163 //--------------------------------------------------------------------------------
164
165 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
166 {
167         $from = date2sql($from_date);
168         $to = date2sql($to_date);
169
170     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
171                 WHERE account='$account'";
172         if ($from_date != "")
173                 $sql .= " AND tran_date >= '$from'";
174         if ($to_date != "")
175                 $sql .= " AND tran_date <= '$to'";
176         if ($dimension > 0)
177                 $sql .= " AND dimension_id = $dimension";
178         if ($dimension2 > 0)
179                 $sql .= " AND dimension2_id = $dimension2";
180
181         $result = db_query($sql, "Transactions for account $account could not be calculated");
182
183         $row = db_fetch_row($result);
184         return $row[0];
185 }
186
187 //--------------------------------------------------------------------------------
188
189 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
190 {
191
192         $from = date2sql($from_date);
193         $to = date2sql($to_date);
194
195         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
196                 WHERE account='$account' ";
197         if ($from_date != "")
198                 $sql .= " AND tran_date >= '$from' ";
199         if ($to_date != "")
200                 $sql .= " AND tran_date <= '$to' ";
201         if ($dimension > 0)
202                 $sql .= " AND dimension_id = $dimension";
203         if ($dimension2 > 0)
204                 $sql .= " AND dimension2_id = $dimension2";
205         $result = db_query($sql,"No budget accounts were returned");
206
207         $row = db_fetch_row($result);
208         return $row[0];
209 }
210
211 //--------------------------------------------------------------------------------
212 //      Stores journal/bank transaction tax details if applicable
213 //
214 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo)
215 {
216         $tax_type = is_tax_account($gl_code);
217         if(!$tax_type) return;  // $gl_code is not tax account
218         
219         $tax = get_tax_type($tax_type);
220         if ($gl_code == $tax['sales_gl_code']) 
221                 $amount = -$amount;
222         // we have to restore net amount as we cannot know the base amount
223         if ($tax['rate'] == 0) {
224 //              display_warning(_("You should not post gl transactions  
225 //                      to tax account with     zero tax rate."));
226                 $net_amount = 0;
227         } else { 
228                 // calculate net amount
229                 $net_amount = $amount/$tax['rate']*100; 
230         }
231                 
232         add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], 0, 
233                 $amount, $net_amount, $ex_rate, $date, $memo);
234                         
235 }
236
237 //--------------------------------------------------------------------------------
238 //
239 //      Store transaction tax details for fiscal purposes with 'freezed' 
240 //      actual tax type rate.
241 //
242 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
243         $amount, $net_amount, $ex_rate, $tran_date, $memo)
244 {
245
246         $sql = "INSERT INTO ".TB_PREF."trans_tax_details 
247                 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
248                         included_in_price, net_amount, amount, memo)
249                 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
250                                 .date2sql($tran_date)."',".db_escape($tax_id).","
251                                 .$rate.",".$ex_rate.",".($included ? 1:0).","
252                                 .db_escape($net_amount).","
253                                 .db_escape($amount).",".db_escape($memo).")";
254
255         db_query($sql, "Cannot save trans tax details");
256
257 }
258 //----------------------------------------------------------------------------------------
259
260 function get_trans_tax_details($trans_type, $trans_no)
261 {
262         $sql = "SELECT ".TB_PREF."trans_tax_details.*, ".TB_PREF."tax_types.name AS tax_type_name
263                 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
264                 WHERE trans_type = $trans_type
265                 AND trans_no = $trans_no
266                 AND (net_amount != 0 OR amount != 0)
267                 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
268
269         return db_query($sql, "The transaction tax details could not be retrieved");
270 }
271
272 //----------------------------------------------------------------------------------------
273
274 function void_trans_tax_details($type, $type_no)
275 {
276         $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
277                 WHERE trans_no=$type_no
278                 AND trans_type=$type";
279
280         db_query($sql, "The transaction tax details could not be voided");
281 }
282
283 function get_tax_summary($from, $to)
284 {
285         $fromdate = date2sql($from);
286         $todate = date2sql($to);
287
288         $sql = "SELECT 
289                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
290                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, net_amount*ex_rate,0)) net_output,
291                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
292                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, amount*ex_rate,0)) payable,
293                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
294                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, net_amount*ex_rate)) net_input,
295                                 SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
296                                 IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, amount*ex_rate)) collectible,
297                                 taxrec.rate,
298                                 ttype.id,
299                                 ttype.name
300                 FROM ".TB_PREF."tax_types ttype,
301                          ".TB_PREF."trans_tax_details taxrec
302                 WHERE taxrec.tax_type_id=ttype.id
303                         AND taxrec.trans_type != 13
304                         AND taxrec.tran_date >= '$fromdate'
305                         AND taxrec.tran_date <= '$todate'
306                 GROUP BY ttype.id";
307 //display_error($sql);
308     return db_query($sql,"Cannot retrieve tax summary");
309 }
310
311 //--------------------------------------------------------------------------------
312 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
313 {
314         begin_transaction();
315
316         $trans_type = systypes::journal_entry();
317         $trans_id = get_next_trans_no($trans_type);
318
319         foreach ($items as $journal_item)
320         {
321                 // post to first found bank account using given gl acount code.
322                 $is_bank_to = is_bank_account($journal_item->code_id);
323
324                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
325                         $journal_item->dimension_id, $journal_item->dimension2_id,
326                         $journal_item->reference, $journal_item->amount);
327         if ($is_bank_to)
328         {
329                 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
330                         $date_, $journal_item->amount,
331                         0, "", get_company_currency(),
332                         "Cannot insert a destination bank transaction");
333         }
334                 // store tax details if the gl account is a tax account
335                 add_gl_tax_details($journal_item->code_id, 
336                         $trans_type, $trans_id, -$journal_item->amount, 1, $date_, $memo_);
337         }
338         
339         add_comments($trans_type, $trans_id, $date_, $memo_);
340
341         references::save($trans_type, $trans_id, $ref);
342         add_audit_trail($trans_type, $trans_id, $date_);
343
344         if ($reverse)
345         {
346
347         //$reversingDate = date(user_date_display(),
348         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
349         $reversingDate = begin_month(add_months($date_, 1));
350
351         $trans_id_reverse = get_next_trans_no($trans_type);
352
353         foreach ($items as $journal_item)
354         {
355                         $is_bank_to = is_bank_account($journal_item->code_id);
356
357                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
358                         $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
359                         $journal_item->reference, -$journal_item->amount);
360                 if ($is_bank_to)
361                 {
362                         add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
363                                 $reversingDate, $journal_item->amount,
364                                 0, "", get_company_currency(),
365                                 "Cannot insert a destination bank transaction");
366                 }
367                         // store tax details if the gl account is a tax account
368                         add_gl_tax_details($journal_item->code_id, 
369                                 $trans_type, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
370         }
371
372         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
373
374         references::save($trans_type, $trans_id_reverse, $ref);
375                 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
376         }
377
378         commit_transaction();
379
380         return $trans_id;
381 }
382 //--------------------------------------------------------------------------------
383 // Write/update journal entries.
384 //
385 function write_journal_entries(&$cart, $reverse)
386 {
387         $date_ = $cart->tran_date;
388         $ref   = $cart->reference;
389         $memo_ = $cart->memo_;
390         $new = $cart->order_id == 0;
391         $trans_type = $cart->trans_type;
392         $trans_id = $new ? get_next_trans_no($trans_type) : $cart->order_id;
393
394         begin_transaction();
395         
396         if($new) {
397                 $cart->order_id = $trans_id;
398         } else {
399                 void_journal_trans($trans_type, $trans_id, true);
400                 delete_comments($trans_type, $trans_id);
401         }
402
403         foreach ($cart->gl_items as $journal_item)
404         {
405                 // post to first found bank account using given gl acount code.
406                 $is_bank_to = is_bank_account($journal_item->code_id);
407
408                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
409                         $journal_item->dimension_id, $journal_item->dimension2_id,
410                         $journal_item->reference, $journal_item->amount);
411         if ($is_bank_to)
412         {
413                 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
414                         $date_, $journal_item->amount,  0, "", get_company_currency(),
415                         "Cannot insert a destination bank transaction");
416         }
417                 // store tax details if the gl account is a tax account
418                 add_gl_tax_details($journal_item->code_id, 
419                         $trans_type, $trans_id, -$journal_item->amount, 1, $date_, $memo_);
420         }
421         
422         if ($new) {
423                 add_comments($trans_type, $trans_id, $date_, $memo_);
424                 references::save($trans_type, $trans_id, $ref);
425         } else
426                 update_comments($trans_type, $trans_id, null, $memo_);
427
428         add_audit_trail($trans_type, $trans_id, $date_);
429
430         commit_transaction();
431
432         if ($reverse)
433         {
434         //$reversingDate = date(user_date_display(),
435         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
436         $reversingDate = begin_month(add_months($date_, 1));
437
438         $trans_id_reverse = get_next_trans_no($trans_type);
439
440         foreach ($cart->gl_items as $journal_item)
441         {
442                         $is_bank_to = is_bank_account($journal_item->code_id);
443
444                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
445                         $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
446                         $journal_item->reference, -$journal_item->amount);
447                 if ($is_bank_to)
448                 {
449                         add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
450                                 $reversingDate, $journal_item->amount,
451                                 0, "", get_company_currency(),
452                                 "Cannot insert a destination bank transaction");
453                 }
454                         // store tax details if the gl account is a tax account
455                         add_gl_tax_details($journal_item->code_id, 
456                                 $trans_type, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
457         }
458
459         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
460
461         references::save($trans_type, $trans_id_reverse, $ref);
462                 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
463         }
464
465         return $trans_id;
466 }
467
468 //--------------------------------------------------------------------------------------------------
469
470 function exists_gl_trans($type, $trans_id)
471 {
472         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
473         $result = db_query($sql, "Cannot retreive a gl transaction");
474
475     return (db_num_rows($result) > 0);
476 }
477
478 //--------------------------------------------------------------------------------------------------
479
480 function void_gl_trans($type, $trans_id, $nested=false)
481 {
482         if (!$nested)
483                 begin_transaction();
484
485         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
486
487         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
488
489         if (!$nested)
490                 commit_transaction();
491 }
492
493 //----------------------------------------------------------------------------------------
494
495 function void_journal_trans($type, $type_no)
496 {
497         begin_transaction();
498
499         void_bank_trans($type, $type_no, true);
500 //      void_gl_trans($type, $type_no, true);    // this is done above
501 //      void_trans_tax_details($type, $type_no); // ditto
502
503         commit_transaction();
504 }
505
506 ?>