Registration of changes on GL tax acounts into tax_details
[fa-stable.git] / gl / includes / db / gl_db_trans.inc
1 <?php
2
3 //--------------------------------------------------------------------------------
4
5 // Base function for adding a GL transaction
6 // $date_ is display date (non-sql)
7 // $amount is in $currency currency
8 // if $currency is not set, then defaults to no conversion
9
10 function add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
11         $amount, $currency=null, $person_type_id=null, $person_id=null, $err_msg="", $rate=0)
12 {
13         global $use_audit_trail;
14
15         $date = date2sql($date_);
16         if ($currency != null)
17         {
18                 if ($rate == 0)
19                         $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
20                 else
21                         $amount_in_home_currency = round2($amount * $rate,  user_price_dec());
22         }               
23         else
24                 $amount_in_home_currency = round2($amount, user_price_dec());
25         if ($dimension == null || $dimension < 0)
26                 $dimension = 0;
27         if ($dimension2 == null || $dimension2 < 0)
28                 $dimension2 = 0;
29         if (isset($use_audit_trail) && $use_audit_trail)
30         {
31                 if ($memo_ == "" || $memo_ == null)
32                         $memo_ = $_SESSION["wa_current_user"]->username;
33                 else
34                         $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
35         }
36         $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
37                 account, dimension_id, dimension2_id, memo_, amount";
38
39         if ($person_type_id != null)
40                 $sql .= ", person_type_id, person_id";
41
42         $sql .= ") ";
43
44         $sql .= "VALUES ($type, $trans_id, '$date',
45                 '$account', $dimension, $dimension2, ".db_escape($memo_).", $amount_in_home_currency";
46
47         if ($person_type_id != null)
48                 $sql .= ", $person_type_id, ". db_escape($person_id);
49
50         $sql .= ") ";
51
52         if ($err_msg == "")
53                 $err_msg = "The GL transaction could not be inserted";
54
55         db_query($sql, $err_msg);
56         return $amount_in_home_currency;
57 }
58
59 //--------------------------------------------------------------------------------
60
61 // GL Trans for standard costing, always home currency regardless of person
62 // $date_ is display date (non-sql)
63 // $amount is in HOME currency
64
65 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
66         $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
67 {
68         if ($amount != 0)
69                 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
70                         $amount, null, $person_type_id, $person_id, $err_msg);
71         else
72                 return 0;
73 }
74
75 // Function for even out rounding problems
76 function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
77 {
78         $amount = round2($amount, user_price_dec());
79         if ($amount != 0)
80                 return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
81                         $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
82         else
83                 return 0;
84 }       
85
86 //--------------------------------------------------------------------------------
87
88 function get_gl_transactions($from_date, $to_date, $trans_no=0,
89         $account=null, $dimension=0, $dimension2=0, $filter_type=null)
90 {
91         $from = date2sql($from_date);
92         $to = date2sql($to_date);
93
94         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
95                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
96                 AND tran_date >= '$from'
97                 AND tran_date <= '$to'";
98         if ($trans_no > 0)
99                 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
100
101         if ($account != null)
102                 $sql .= " AND ".TB_PREF."gl_trans.account = '$account'";
103
104         if ($dimension > 0)
105                 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
106
107         if ($dimension2 > 0)
108                 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
109
110         if ($filter_type != null AND is_numeric($filter_type))
111                 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
112
113         $sql .= " ORDER BY tran_date";
114
115         return db_query($sql, "The transactions for could not be retrieved");
116 }
117
118
119 //--------------------------------------------------------------------------------
120
121 function get_gl_trans($type, $trans_id)
122 {
123         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
124                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
125                 AND ".TB_PREF."gl_trans.type=$type AND ".TB_PREF."gl_trans.type_no=$trans_id";
126
127         return db_query($sql, "The gl transactions could not be retrieved");
128 }
129
130 //--------------------------------------------------------------------------------
131
132 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
133 {
134         $from = date2sql($from_date);
135         $to = date2sql($to_date);
136
137     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
138                 WHERE account='$account'";
139         if ($from_date != "")
140                 $sql .= "  AND tran_date > '$from'";
141         if ($to_date != "")
142                 $sql .= "  AND tran_date < '$to'";
143         if ($dimension > 0)
144                 $sql .= " AND dimension_id = $dimension";
145         if ($dimension2 > 0)
146                 $sql .= " AND dimension2_id = $dimension2";
147
148         $result = db_query($sql, "The starting balance for account $account could not be calculated");
149
150         $row = db_fetch_row($result);
151         return $row[0];
152 }
153
154 //--------------------------------------------------------------------------------
155
156 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
157 {
158         $from = date2sql($from_date);
159         $to = date2sql($to_date);
160
161     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
162                 WHERE account='$account'";
163         if ($from_date != "")
164                 $sql .= " AND tran_date >= '$from'";
165         if ($to_date != "")
166                 $sql .= " AND tran_date <= '$to'";
167         if ($dimension > 0)
168                 $sql .= " AND dimension_id = $dimension";
169         if ($dimension2 > 0)
170                 $sql .= " AND dimension2_id = $dimension2";
171
172         $result = db_query($sql, "Transactions for account $account could not be calculated");
173
174         $row = db_fetch_row($result);
175         return $row[0];
176 }
177
178 //--------------------------------------------------------------------------------
179
180 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
181 {
182
183         $from = date2sql($from_date);
184         $to = date2sql($to_date);
185
186         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
187                 WHERE account='$account' ";
188         if ($from_date != "")
189                 $sql .= " AND tran_date >= '$from' ";
190         if ($to_date != "")
191                 $sql .= " AND tran_date <= '$to' ";
192         if ($dimension > 0)
193                 $sql .= " AND dimension_id = $dimension";
194         if ($dimension2 > 0)
195                 $sql .= " AND dimension2_id = $dimension2";
196         $result = db_query($sql,"No budget accounts were returned");
197
198         $row = db_fetch_row($result);
199         return $row[0];
200 }
201
202 //--------------------------------------------------------------------------------
203 //      Stores GL journal/bank transaction on tax account for tax report
204 //
205 function add_gl_tax_details($gl_code, $trans_type, $trans_id, $amount)
206 {
207         $tax_type = is_tax_account($gl_code);
208         if(!$tax_type) return;  // $gl_code is not tax account
209
210         $tax = get_tax_type($tax_type);
211         if ($gl_code == $tax['sales_gl_code'])
212                 add_customer_trans_tax_detail_item($trans_type, $trans_id, 
213                         $tax['id'], $tax['rate'], 0, -$amount);
214         else
215                 add_supp_invoice_tax_item($trans_type, $trans_id, 
216                         $tax['id'], $tax['rate'], 0, $amount);
217                         
218 }
219
220 //--------------------------------------------------------------------------------
221 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
222 {
223         begin_transaction();
224
225         $trans_type = systypes::journal_entry();
226         $trans_id = get_next_trans_no($trans_type);
227
228         foreach ($items as $journal_item)
229         {
230                 // post to first found bank account using given gl acount code.
231                 $is_bank_to = is_bank_account($journal_item->code_id);
232
233                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
234                         $journal_item->dimension_id, $journal_item->dimension2_id,
235                         $journal_item->reference, $journal_item->amount);
236         if ($is_bank_to)
237         {
238                 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
239                         $date_, $journal_item->amount,
240                         0, "", get_company_currency(),
241                         "Cannot insert a destination bank transaction");
242         }
243                 // store tax details if the gl account is a tax account
244                 add_gl_tax_details($journal_item->code_id, 
245                         $trans_type, $trans_id, $journal_item->amount);
246         }
247         
248         add_comments($trans_type, $trans_id, $date_, $memo_);
249
250         references::save($trans_type, $trans_id, $ref);
251
252         if ($reverse)
253         {
254
255         //$reversingDate = date(user_date_display(),
256         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
257         $reversingDate = begin_month(add_months($date_, 1));
258
259         $trans_id_reverse = get_next_trans_no($trans_type);
260
261         foreach ($items as $journal_item)
262         {
263                         $is_bank_to = is_bank_account($journal_item->code_id);
264
265                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
266                         $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
267                         $journal_item->reference, -$journal_item->amount);
268                 if ($is_bank_to)
269                 {
270                         add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
271                                 $reversingDate, $journal_item->amount,
272                                 0, "", get_company_currency(),
273                                 "Cannot insert a destination bank transaction");
274                 }
275                         // store tax details if the gl account is a tax account
276                         add_gl_tax_details($journal_item->code_id, 
277                                 $trans_type, $trans_id, -$journal_item->amount);
278         }
279
280         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
281
282         references::save($trans_type, $trans_id_reverse, $ref);
283         }
284
285         commit_transaction();
286
287         return $trans_id;
288 }
289
290 //--------------------------------------------------------------------------------------------------
291
292 function exists_gl_trans($type, $trans_id)
293 {
294         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
295         $result = db_query($sql, "Cannot retreive a gl transaction");
296
297     return (db_num_rows($result) > 0);
298 }
299
300 //--------------------------------------------------------------------------------------------------
301
302 function void_gl_trans($type, $trans_id, $nested=false)
303 {
304         if (!$nested)
305                 begin_transaction();
306
307         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
308
309         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
310
311         if (!$nested)
312                 commit_transaction();
313 }
314
315 //--------------------------------------------------------------------------------------------------
316
317 ?>