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 //--------------------------------------------------------------------------------
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
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)
22 global $use_audit_trail;
24 $date = date2sql($date_);
25 if ($currency != null)
28 $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
30 $amount_in_home_currency = round2($amount * $rate, user_price_dec());
33 $amount_in_home_currency = round2($amount, user_price_dec());
34 if ($dimension == null || $dimension < 0)
36 if ($dimension2 == null || $dimension2 < 0)
38 if (isset($use_audit_trail) && $use_audit_trail)
40 if ($memo_ == "" || $memo_ == null)
41 $memo_ = $_SESSION["wa_current_user"]->username;
43 $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
45 $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
46 account, dimension_id, dimension2_id, memo_, amount";
48 if ($person_type_id != null)
49 $sql .= ", person_type_id, person_id";
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);
58 if ($person_type_id != null)
59 $sql .= ", ".db_escape($person_type_id).", ". db_escape($person_id);
64 $err_msg = "The GL transaction could not be inserted";
66 db_query($sql, $err_msg);
67 return $amount_in_home_currency;
70 //--------------------------------------------------------------------------------
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
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="")
80 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
81 $amount, null, $person_type_id, $person_id, $err_msg);
86 // Function for even out rounding problems
87 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
89 $amount = round2($amount, user_price_dec());
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");
97 //--------------------------------------------------------------------------------
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)
103 $from = date2sql($from_date);
104 $to = date2sql($to_date);
106 $sql = "SELECT ".TB_PREF."gl_trans.*, "
107 .TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, "
108 .TB_PREF."chart_master
109 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
110 AND tran_date >= '$from'
111 AND tran_date <= '$to'";
113 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE ".db_escape('%'.$trans_no);
115 if ($account != null)
116 $sql .= " AND ".TB_PREF."gl_trans.account = ".db_escape($account);
119 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = ".($dimension<0?0:db_escape($dimension));
121 if ($dimension2 != 0)
122 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
124 if ($filter_type != null AND is_numeric($filter_type))
125 $sql .= " AND ".TB_PREF."gl_trans.type= ".db_escape($filter_type);
127 if ($amount_min != null)
128 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) >= ABS(".db_escape($amount_min).")";
130 if ($amount_max != null)
131 $sql .= " AND ABS(".TB_PREF."gl_trans.amount) <= ABS(".db_escape($amount_max).")";
133 $sql .= " ORDER BY tran_date";
135 return db_query($sql, "The transactions for could not be retrieved");
139 //--------------------------------------------------------------------------------
141 function get_gl_trans($type, $trans_id)
143 $sql = "SELECT ".TB_PREF."gl_trans.*, "
144 .TB_PREF."chart_master.account_name FROM "
145 .TB_PREF."gl_trans, ".TB_PREF."chart_master
146 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
147 AND ".TB_PREF."gl_trans.type=".db_escape($type)
148 ." AND ".TB_PREF."gl_trans.type_no=".db_escape($trans_id);
150 return db_query($sql, "The gl transactions could not be retrieved");
153 //--------------------------------------------------------------------------------
155 function get_gl_wo_cost_trans($trans_id, $person_id=-1)
157 $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM "
158 .TB_PREF."gl_trans, ".TB_PREF."chart_master
159 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
160 AND ".TB_PREF."gl_trans.type=".ST_WORKORDER
161 ." AND ".TB_PREF."gl_trans.type_no=".db_escape($trans_id)."
162 AND ".TB_PREF."gl_trans.person_type_id=".PT_WORKORDER;
163 if ($person_id != -1)
164 $sql .= " AND ".TB_PREF."gl_trans.person_id=".db_escape($person_id);
165 $sql .= " AND amount < 0";
167 return db_query($sql, "The gl transactions could not be retrieved");
170 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
172 $from = date2sql($from_date);
173 $to = date2sql($to_date);
175 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
176 WHERE account='$account'";
177 if ($from_date != "")
178 $sql .= " AND tran_date > '$from'";
180 $sql .= " AND tran_date < '$to'";
182 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
183 if ($dimension2 != 0)
184 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
186 $result = db_query($sql, "The starting balance for account $account could not be calculated");
188 $row = db_fetch_row($result);
192 //--------------------------------------------------------------------------------
194 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
196 $from = date2sql($from_date);
197 $to = date2sql($to_date);
199 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
200 WHERE account='$account'";
201 if ($from_date != "")
202 $sql .= " AND tran_date >= '$from'";
204 $sql .= " AND tran_date <= '$to'";
206 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
207 if ($dimension2 != 0)
208 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
210 $result = db_query($sql, "Transactions for account $account could not be calculated");
212 $row = db_fetch_row($result);
216 //----------------------------------------------------------------------------------------------------
217 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true)
219 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit,
220 SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance
221 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,"
222 .TB_PREF."chart_types, ".TB_PREF."chart_class
223 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND "
224 .TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
225 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
227 if ($account != null)
228 $sql .= " account=".db_escape($account)." AND";
230 $sql .= " dimension_id = ".($dimension<0?0:db_escape($dimension))." AND";
231 if ($dimension2 != 0)
232 $sql .= " dimension2_id = ".($dimension2<0?0:db_escape($dimension2))." AND";
233 $from_date = date2sql($from);
235 $sql .= " tran_date >= '$from_date' AND";
237 $sql .= " tran_date > IF(ctype>0 AND ctype<".CL_INCOME.", '0000-00-00', '$from_date') AND";
238 $to_date = date2sql($to);
240 $sql .= " tran_date <= '$to_date' ";
242 $sql .= " tran_date < '$to_date' ";
244 $result = db_query($sql,"No general ledger accounts were returned");
246 return db_fetch($result);
249 //--------------------------------------------------------------------------------
251 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
254 $from = date2sql($from_date);
255 $to = date2sql($to_date);
257 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
258 WHERE account=".db_escape($account);
259 if ($from_date != "")
260 $sql .= " AND tran_date >= '$from' ";
262 $sql .= " AND tran_date <= '$to' ";
264 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
265 if ($dimension2 != 0)
266 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
267 $result = db_query($sql,"No budget accounts were returned");
269 $row = db_fetch_row($result);
273 //--------------------------------------------------------------------------------
274 // Stores journal/bank transaction tax details if applicable
276 function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate, $date, $memo)
278 $tax_type = is_tax_account($gl_code);
279 if(!$tax_type) return; // $gl_code is not tax account
281 $tax = get_tax_type($tax_type);
282 //if ($gl_code == $tax['sales_gl_code'])
283 // $amount = -$amount;
284 // we have to restore net amount as we cannot know the base amount
285 if ($tax['rate'] == 0) {
286 // display_warning(_("You should not post gl transactions
287 // to tax account with zero tax rate."));
290 // calculate net amount
291 $net_amount = $amount/$tax['rate']*100;
294 add_trans_tax_details($trans_type, $trans_no, $tax['id'], $tax['rate'], 0,
295 $amount, $net_amount, $ex_rate, $date, $memo);
299 //--------------------------------------------------------------------------------
301 // Store transaction tax details for fiscal purposes with 'freezed'
302 // actual tax type rate.
304 function add_trans_tax_details($trans_type, $trans_no, $tax_id, $rate, $included,
305 $amount, $net_amount, $ex_rate, $tran_date, $memo)
308 $sql = "INSERT INTO ".TB_PREF."trans_tax_details
309 (trans_type, trans_no, tran_date, tax_type_id, rate, ex_rate,
310 included_in_price, net_amount, amount, memo)
311 VALUES (".db_escape($trans_type)."," . db_escape($trans_no).",'"
312 .date2sql($tran_date)."',".db_escape($tax_id).","
313 .db_escape($rate).",".db_escape($ex_rate).",".($included ? 1:0).","
314 .db_escape($net_amount).","
315 .db_escape($amount).",".db_escape($memo).")";
317 db_query($sql, "Cannot save trans tax details");
320 //----------------------------------------------------------------------------------------
322 function get_trans_tax_details($trans_type, $trans_no)
324 $sql = "SELECT ".TB_PREF."trans_tax_details.*, "
325 .TB_PREF."tax_types.name AS tax_type_name
326 FROM ".TB_PREF."trans_tax_details,".TB_PREF."tax_types
327 WHERE trans_type = ".db_escape($trans_type)."
328 AND trans_no = ".db_escape($trans_no)."
329 AND (net_amount != 0 OR amount != 0)
330 AND ".TB_PREF."tax_types.id = ".TB_PREF."trans_tax_details.tax_type_id";
332 return db_query($sql, "The transaction tax details could not be retrieved");
335 //----------------------------------------------------------------------------------------
337 function void_trans_tax_details($type, $type_no)
339 $sql = "UPDATE ".TB_PREF."trans_tax_details SET amount=0, net_amount=0
340 WHERE trans_no=".db_escape($type_no)
341 ." AND trans_type=".db_escape($type);
343 db_query($sql, "The transaction tax details could not be voided");
346 function get_tax_summary($from, $to)
348 $fromdate = date2sql($from);
349 $todate = date2sql($to);
352 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
353 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE." || trans_type=".ST_CUSTCREDIT.", net_amount*ex_rate,0)) net_output,
354 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
355 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE." || trans_type=".ST_CUSTCREDIT.", amount*ex_rate,0)) payable,
356 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
357 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE." || trans_type=".ST_CUSTCREDIT.", 0, net_amount*ex_rate)) net_input,
358 SUM(IF(trans_type=".ST_CUSTCREDIT." || trans_type=".ST_SUPPINVOICE.",-1,1)*
359 IF(trans_type=".ST_BANKDEPOSIT." || trans_type=".ST_SALESINVOICE." || trans_type=".ST_CUSTCREDIT.", 0, amount*ex_rate)) collectible,
363 FROM ".TB_PREF."tax_types ttype,
364 ".TB_PREF."trans_tax_details taxrec
365 WHERE taxrec.tax_type_id=ttype.id
366 AND taxrec.trans_type != ".ST_CUSTDELIVERY."
367 AND taxrec.tran_date >= '$fromdate'
368 AND taxrec.tran_date <= '$todate'
370 //display_error($sql);
371 return db_query($sql,"Cannot retrieve tax summary");
374 //--------------------------------------------------------------------------------
375 // Write/update journal entries.
377 function write_journal_entries(&$cart, $reverse, $use_transaction=true)
381 $date_ = $cart->tran_date;
382 $ref = $cart->reference;
383 $memo_ = $cart->memo_;
384 $trans_type = $cart->trans_type;
385 $new = $cart->order_id == 0;
388 $cart->order_id = get_next_trans_no($trans_type);
390 $trans_id = $cart->order_id;
392 if ($use_transaction)
396 void_journal_trans($trans_type, $trans_id, true);
398 foreach ($cart->gl_items as $journal_item)
400 // post to first found bank account using given gl acount code.
401 $is_bank_to = is_bank_account($journal_item->code_id);
403 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
404 $journal_item->dimension_id, $journal_item->dimension2_id,
405 $journal_item->reference, $journal_item->amount);
408 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
409 $date_, $journal_item->amount, 0, "", get_company_currency(),
410 "Cannot insert a destination bank transaction");
412 // store tax details if the gl account is a tax account
413 add_gl_tax_details($journal_item->code_id,
414 ($journal_item->amount < 0.0 ? ST_BANKDEPOSIT : ST_BANKPAYMENT), $trans_id, -$journal_item->amount, 1, $date_, $memo_);
418 add_comments($trans_type, $trans_id, $date_, $memo_);
419 $Refs->save($trans_type, $trans_id, $ref);
421 update_comments($trans_type, $trans_id, null, $memo_);
422 $Refs->update($trans_type, $trans_id, $ref);
425 add_audit_trail($trans_type, $trans_id, $date_);
429 //$reversingDate = date(user_date_display(),
430 // Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
431 $reversingDate = begin_month(add_months($date_, 1));
433 $trans_id_reverse = get_next_trans_no($trans_type);
435 foreach ($cart->gl_items as $journal_item)
437 $is_bank_to = is_bank_account($journal_item->code_id);
439 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
440 $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
441 $journal_item->reference, -$journal_item->amount);
444 add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
445 $reversingDate, -$journal_item->amount,
446 0, "", get_company_currency(),
447 "Cannot insert a destination bank transaction");
449 // store tax details if the gl account is a tax account
450 add_gl_tax_details($journal_item->code_id,
451 ($journal_item->amount < 0.0 ? ST_BANKDEPOSIT : ST_BANKPAYMENT), $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
454 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
456 $Refs->save($trans_type, $trans_id_reverse, $ref);
457 add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
460 if ($use_transaction)
461 commit_transaction();
466 //--------------------------------------------------------------------------------------------------
468 function exists_gl_trans($type, $trans_id)
470 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=".db_escape($type)
471 ." AND type_no=".db_escape($trans_id);
472 $result = db_query($sql, "Cannot retreive a gl transaction");
474 return (db_num_rows($result) > 0);
477 //--------------------------------------------------------------------------------------------------
479 function void_gl_trans($type, $trans_id, $nested=false)
484 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=".db_escape($type)
485 ." AND type_no=".db_escape($trans_id);
487 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
490 commit_transaction();
493 //----------------------------------------------------------------------------------------
495 function void_journal_trans($type, $type_no)
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
503 commit_transaction();