2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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 ($type, $trans_id, '$date',
54 '$account', $dimension, $dimension2, ".db_escape($memo_).", $amount_in_home_currency";
56 if ($person_type_id != null)
57 $sql .= ", $person_type_id, ". db_escape($person_id);
62 $err_msg = "The GL transaction could not be inserted";
64 db_query($sql, $err_msg);
65 return $amount_in_home_currency;
68 //--------------------------------------------------------------------------------
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
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="")
78 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
79 $amount, null, $person_type_id, $person_id, $err_msg);
84 // Function for even out rounding problems
85 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
87 $amount = round2($amount, user_price_dec());
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");
95 //--------------------------------------------------------------------------------
97 function get_gl_transactions($from_date, $to_date, $trans_no=0,
98 $account=null, $dimension=0, $dimension2=0, $filter_type=null)
100 $from = date2sql($from_date);
101 $to = date2sql($to_date);
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'";
108 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
110 if ($account != null)
111 $sql .= " AND ".TB_PREF."gl_trans.account = '$account'";
114 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
117 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
119 if ($filter_type != null AND is_numeric($filter_type))
120 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
122 $sql .= " ORDER BY tran_date";
124 return db_query($sql, "The transactions for could not be retrieved");
128 //--------------------------------------------------------------------------------
130 function get_gl_trans($type, $trans_id)
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";
136 return db_query($sql, "The gl transactions could not be retrieved");
139 //--------------------------------------------------------------------------------
141 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
143 $from = date2sql($from_date);
144 $to = date2sql($to_date);
146 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
147 WHERE account='$account'";
148 if ($from_date != "")
149 $sql .= " AND tran_date > '$from'";
151 $sql .= " AND tran_date < '$to'";
153 $sql .= " AND dimension_id = $dimension";
155 $sql .= " AND dimension2_id = $dimension2";
157 $result = db_query($sql, "The starting balance for account $account could not be calculated");
159 $row = db_fetch_row($result);
163 //--------------------------------------------------------------------------------
165 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
167 $from = date2sql($from_date);
168 $to = date2sql($to_date);
170 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
171 WHERE account='$account'";
172 if ($from_date != "")
173 $sql .= " AND tran_date >= '$from'";
175 $sql .= " AND tran_date <= '$to'";
177 $sql .= " AND dimension_id = $dimension";
179 $sql .= " AND dimension2_id = $dimension2";
181 $result = db_query($sql, "Transactions for account $account could not be calculated");
183 $row = db_fetch_row($result);
187 //--------------------------------------------------------------------------------
189 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
192 $from = date2sql($from_date);
193 $to = date2sql($to_date);
195 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
196 WHERE account='$account' ";
197 if ($from_date != "")
198 $sql .= " AND tran_date >= '$from' ";
200 $sql .= " AND tran_date <= '$to' ";
202 $sql .= " AND dimension_id = $dimension";
204 $sql .= " AND dimension2_id = $dimension2";
205 $result = db_query($sql,"No budget accounts were returned");
207 $row = db_fetch_row($result);
211 //--------------------------------------------------------------------------------
212 // Stores GL journal/bank transaction on tax account for tax report
214 function add_gl_tax_details($gl_code, $trans_type, $trans_id, $amount)
216 $tax_type = is_tax_account($gl_code);
217 if(!$tax_type) return; // $gl_code is not tax account
219 $tax = get_tax_type($tax_type);
220 if ($gl_code == $tax['sales_gl_code'])
221 add_customer_trans_tax_detail_item($trans_type, $trans_id,
222 $tax['id'], $tax['rate'], 0, -$amount);
224 add_supp_invoice_tax_item($trans_type, $trans_id,
225 $tax['id'], $tax['rate'], 0, $amount);
229 //--------------------------------------------------------------------------------
230 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
234 $trans_type = systypes::journal_entry();
235 $trans_id = get_next_trans_no($trans_type);
237 foreach ($items as $journal_item)
239 // post to first found bank account using given gl acount code.
240 $is_bank_to = is_bank_account($journal_item->code_id);
242 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
243 $journal_item->dimension_id, $journal_item->dimension2_id,
244 $journal_item->reference, $journal_item->amount);
247 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
248 $date_, $journal_item->amount,
249 0, "", get_company_currency(),
250 "Cannot insert a destination bank transaction");
252 // store tax details if the gl account is a tax account
253 add_gl_tax_details($journal_item->code_id,
254 $trans_type, $trans_id, $journal_item->amount);
257 add_comments($trans_type, $trans_id, $date_, $memo_);
259 references::save($trans_type, $trans_id, $ref);
264 //$reversingDate = date(user_date_display(),
265 // Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
266 $reversingDate = begin_month(add_months($date_, 1));
268 $trans_id_reverse = get_next_trans_no($trans_type);
270 foreach ($items as $journal_item)
272 $is_bank_to = is_bank_account($journal_item->code_id);
274 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
275 $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
276 $journal_item->reference, -$journal_item->amount);
279 add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
280 $reversingDate, $journal_item->amount,
281 0, "", get_company_currency(),
282 "Cannot insert a destination bank transaction");
284 // store tax details if the gl account is a tax account
285 add_gl_tax_details($journal_item->code_id,
286 $trans_type, $trans_id, -$journal_item->amount);
289 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
291 references::save($trans_type, $trans_id_reverse, $ref);
294 commit_transaction();
299 //--------------------------------------------------------------------------------------------------
301 function exists_gl_trans($type, $trans_id)
303 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
304 $result = db_query($sql, "Cannot retreive a gl transaction");
306 return (db_num_rows($result) > 0);
309 //--------------------------------------------------------------------------------------------------
311 function void_gl_trans($type, $trans_id, $nested=false)
316 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
318 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
321 commit_transaction();
324 //----------------------------------------------------------------------------------------
326 function void_journal_trans($type, $type_no)
330 void_gl_trans($type, $type_no, true);
331 void_bank_trans($type, $type_no,true);
332 void_customer_trans_tax_details($type, $type_no);
333 void_supp_invoice_tax_items($type, $type_no);
335 commit_transaction();