[0000095] Inbalance double entry on Documents
[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 = round($amount * $rate,  user_price_dec());
22         }               
23         else
24                 $amount_in_home_currency = round($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 //--------------------------------------------------------------------------------
76
77 function get_gl_transactions($from_date, $to_date, $trans_no=0,
78         $account=null, $dimension=0, $dimension2=0, $filter_type=null)
79 {
80         $from = date2sql($from_date);
81         $to = date2sql($to_date);
82
83         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
84                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
85                 AND tran_date >= '$from'
86                 AND tran_date <= '$to'";
87         if ($trans_no > 0)
88                 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
89
90         if ($account != null)
91                 $sql .= " AND ".TB_PREF."gl_trans.account = '$account'";
92
93         if ($dimension > 0)
94                 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
95
96         if ($dimension2 > 0)
97                 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
98
99         if ($filter_type != null AND is_numeric($filter_type))
100                 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
101
102         $sql .= " ORDER BY tran_date";
103
104         return db_query($sql, "The transactions for could not be retrieved");
105 }
106
107
108 //--------------------------------------------------------------------------------
109
110 function get_gl_trans($type, $trans_id)
111 {
112         $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
113                 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
114                 AND ".TB_PREF."gl_trans.type=$type AND ".TB_PREF."gl_trans.type_no=$trans_id";
115
116         return db_query($sql, "The gl transactions could not be retrieved");
117 }
118
119 //--------------------------------------------------------------------------------
120
121 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
122 {
123         $from = date2sql($from_date);
124         $to = date2sql($to_date);
125
126     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
127                 WHERE account='$account'";
128         if ($from_date != "")
129                 $sql .= "  AND tran_date > '$from'";
130         if ($to_date != "")
131                 $sql .= "  AND tran_date < '$to'";
132         if ($dimension > 0)
133                 $sql .= " AND dimension_id = $dimension";
134         if ($dimension2 > 0)
135                 $sql .= " AND dimension2_id = $dimension2";
136
137         $result = db_query($sql, "The starting balance for account $account could not be calculated");
138
139         $row = db_fetch_row($result);
140         return $row[0];
141 }
142
143 //--------------------------------------------------------------------------------
144
145 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
146 {
147         $from = date2sql($from_date);
148         $to = date2sql($to_date);
149
150     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
151                 WHERE account='$account'";
152         if ($from_date != "")
153                 $sql .= " AND tran_date >= '$from'";
154         if ($to_date != "")
155                 $sql .= " AND tran_date <= '$to'";
156         if ($dimension > 0)
157                 $sql .= " AND dimension_id = $dimension";
158         if ($dimension2 > 0)
159                 $sql .= " AND dimension2_id = $dimension2";
160
161         $result = db_query($sql, "Transactions for account $account could not be calculated");
162
163         $row = db_fetch_row($result);
164         return $row[0];
165 }
166
167 //--------------------------------------------------------------------------------
168
169 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
170 {
171
172         $from = date2sql($from_date);
173         $to = date2sql($to_date);
174
175         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
176                 WHERE account='$account' ";
177         if ($from_date != "")
178                 $sql .= " AND tran_date >= '$from' ";
179         if ($to_date != "")
180                 $sql .= " AND tran_date <= '$to' ";
181         if ($dimension > 0)
182                 $sql .= " AND dimension_id = $dimension";
183         if ($dimension2 > 0)
184                 $sql .= " AND dimension2_id = $dimension2";
185         $result = db_query($sql,"No budget accounts were returned");
186
187         $row = db_fetch_row($result);
188         return $row[0];
189 }
190
191 //--------------------------------------------------------------------------------
192
193 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
194 {
195         begin_transaction();
196
197         $trans_type = systypes::journal_entry();
198         $trans_id = get_next_trans_no($trans_type);
199
200         foreach ($items as $journal_item)
201         {
202                 $is_bank_to = is_bank_account($journal_item->code_id);
203
204                 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
205                         $journal_item->dimension_id, $journal_item->dimension2_id,
206                         $journal_item->reference, $journal_item->amount);
207         if ($is_bank_to)
208         {
209                 add_bank_trans($trans_type, $trans_id, $journal_item->code_id, $ref,
210                         $date_, 3, $journal_item->amount,
211                         0, "", get_company_currency(),
212                         "Cannot insert a destination bank transaction");
213         }
214         }
215
216         add_comments($trans_type, $trans_id, $date_, $memo_);
217
218         references::save($trans_type, $trans_id, $ref);
219
220         if ($reverse)
221         {
222
223         //$reversingDate = date(user_date_display(),
224         //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
225         $reversingDate = begin_month(add_months($date_, 1));
226
227         $trans_id_reverse = get_next_trans_no($trans_type);
228
229         foreach ($items as $journal_item)
230         {
231                         $is_bank_to = is_bank_account($journal_item->code_id);
232
233                 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
234                         $journal_item->code_id, $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_reverse, $journal_item->code_id, $ref,
239                                 $reversingDate, 3, $journal_item->amount,
240                                 0, "", get_company_currency(),
241                                 "Cannot insert a destination bank transaction");
242                 }
243         }
244
245         add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
246
247         references::save($trans_type, $trans_id_reverse, $ref);
248         }
249
250         commit_transaction();
251
252         return $trans_id;
253 }
254
255 //--------------------------------------------------------------------------------------------------
256
257 function exists_gl_trans($type, $trans_id)
258 {
259         $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
260         $result = db_query($sql, "Cannot retreive a gl transaction");
261
262     return (db_num_rows($result) > 0);
263 }
264
265 //--------------------------------------------------------------------------------------------------
266
267 function void_gl_trans($type, $trans_id, $nested=false)
268 {
269         if (!$nested)
270                 begin_transaction();
271
272         $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
273
274         db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
275
276         if (!$nested)
277                 commit_transaction();
278 }
279
280 //--------------------------------------------------------------------------------------------------
281
282 ?>