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