3 //--------------------------------------------------------------------------------
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
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="")
13 $date = date2sql($date_);
14 if ($currency != null)
15 $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
17 $amount_in_home_currency = $amount;
18 if ($dimension == null || $dimension < 0)
20 if ($dimension2 == null || $dimension2 < 0)
22 $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
23 account, dimension_id, dimension2_id, memo_, amount";
25 if ($person_type_id != null)
26 $sql .= ", person_type_id, person_id";
30 $sql .= "VALUES ($type, $trans_id, '$date',
31 $account, $dimension, $dimension2, '$memo_', $amount_in_home_currency";
33 if ($person_type_id != null)
34 $sql .= ", $person_type_id, '$person_id'";
39 $err_msg = "The GL transaction could not be inserted";
41 return db_query($sql, $err_msg);
44 //--------------------------------------------------------------------------------
46 // GL Trans for standard costing, always home currency regardless of person
47 // $date_ is display date (non-sql)
48 // $amount is in HOME currency
50 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
51 $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
54 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
55 $amount, null, $person_type_id, $person_id, $err_msg);
60 //--------------------------------------------------------------------------------
62 function get_gl_transactions($from_date, $to_date, $trans_no=0,
63 $account=null, $dimension=0, $dimension2=0, $filter_type=null)
65 $from = date2sql($from_date);
66 $to = date2sql($to_date);
68 $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
69 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
70 AND tran_date >= '$from'
71 AND tran_date <= '$to'";
73 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
76 $sql .= " AND ".TB_PREF."gl_trans.account = $account";
79 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
82 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
84 if ($filter_type != null AND is_numeric($filter_type))
85 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
87 $sql .= " ORDER BY tran_date";
89 return db_query($sql, "The transactions for could not be retrieved");
93 //--------------------------------------------------------------------------------
95 function get_gl_trans($type, $trans_id)
97 $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
98 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
99 AND ".TB_PREF."gl_trans.type=$type AND ".TB_PREF."gl_trans.type_no=$trans_id";
101 return db_query($sql, "The gl transactions could not be retrieved");
104 //--------------------------------------------------------------------------------
106 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
108 $from = date2sql($from_date);
109 $to = date2sql($to_date);
111 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
112 WHERE account='" . $account . "'";
113 if ($from_date != "")
114 $sql .= " AND tran_date > '$from'";
116 $sql .= " AND tran_date < '$to'";
118 $sql .= " AND dimension_id = $dimension";
120 $sql .= " AND dimension2_id = $dimension2";
122 $result = db_query($sql, "The starting balance for account $account could not be calculated");
124 $row = db_fetch_row($result);
128 //--------------------------------------------------------------------------------
130 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
132 $from = date2sql($from_date);
133 $to = date2sql($to_date);
135 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
136 WHERE account='" . $account . "'";
137 if ($from_date != "")
138 $sql .= " AND tran_date >= '$from'";
140 $sql .= " AND tran_date <= '$to'";
142 $sql .= " AND dimension_id = $dimension";
144 $sql .= " AND dimension2_id = $dimension2";
146 $result = db_query($sql, "Transactions for account $account could not be calculated");
148 $row = db_fetch_row($result);
152 //--------------------------------------------------------------------------------
154 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
157 $from = date2sql($from_date);
158 $to = date2sql($to_date);
160 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
161 WHERE account=$account ";
162 if ($from_date != "")
163 $sql .= " AND tran_date >= '$from' ";
165 $sql .= " AND tran_date <= '$to' ";
167 $sql .= " AND dimension_id = $dimension";
169 $sql .= " AND dimension2_id = $dimension2";
170 $result = db_query($sql,"No budget accounts were returned");
172 $row = db_fetch_row($result);
176 //--------------------------------------------------------------------------------
178 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
182 $trans_type = systypes::journal_entry();
183 $trans_id = get_next_trans_no($trans_type);
185 foreach ($items as $journal_item)
187 $is_bank_to = is_bank_account($journal_item->code_id);
189 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
190 $journal_item->dimension_id, $journal_item->dimension2_id,
191 $journal_item->reference, $journal_item->amount);
194 add_bank_trans($trans_type, $trans_id, $journal_item->code_id, $ref,
195 $date_, 3, $journal_item->amount,
196 0, "", get_company_currency(),
197 "Cannot insert a destination bank transaction");
201 add_comments($trans_type, $trans_id, $date_, $memo_);
203 references::save($trans_type, $trans_id, $ref);
208 $reversingDate = date(user_date_display(),
209 Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
211 $trans_id_reverse = get_next_trans_no($trans_type);
213 foreach ($items as $journal_item)
215 $is_bank_to = is_bank_account($journal_item->code_id);
217 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
218 $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
219 $journal_item->reference, -$journal_item->amount);
222 add_bank_trans($trans_type, $trans_id_reverse, $journal_item->code_id, $ref,
223 $reversingDate, 3, $journal_item->amount,
224 0, "", get_company_currency(),
225 "Cannot insert a destination bank transaction");
229 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
231 references::save($trans_type, $trans_id_reverse, $ref);
234 commit_transaction();
239 //--------------------------------------------------------------------------------------------------
241 function exists_gl_trans($type, $trans_id)
243 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
244 $result = db_query($sql, "Cannot retreive a gl transaction");
246 return (db_num_rows($result) > 0);
249 //--------------------------------------------------------------------------------------------------
251 function void_gl_trans($type, $trans_id, $nested=false)
256 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
258 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
261 commit_transaction();
264 //--------------------------------------------------------------------------------------------------