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 global $use_audit_trail;
15 $date = date2sql($date_);
16 if ($currency != null)
17 $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
19 $amount_in_home_currency = $amount;
20 if ($dimension == null || $dimension < 0)
22 if ($dimension2 == null || $dimension2 < 0)
24 if (isset($use_audit_trail) && $use_audit_trail)
26 if ($memo_ == "" || $memo_ == null)
27 $memo_ = $_SESSION["wa_current_user"]->username;
29 $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
31 $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
32 account, dimension_id, dimension2_id, memo_, amount";
34 if ($person_type_id != null)
35 $sql .= ", person_type_id, person_id";
39 $sql .= "VALUES ($type, $trans_id, '$date',
40 '$account', $dimension, $dimension2, ".db_escape($memo_).", $amount_in_home_currency";
42 if ($person_type_id != null)
43 $sql .= ", $person_type_id, ". db_escape($person_id);
48 $err_msg = "The GL transaction could not be inserted";
50 return db_query($sql, $err_msg);
53 //--------------------------------------------------------------------------------
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
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="")
63 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
64 $amount, null, $person_type_id, $person_id, $err_msg);
69 //--------------------------------------------------------------------------------
71 function get_gl_transactions($from_date, $to_date, $trans_no=0,
72 $account=null, $dimension=0, $dimension2=0, $filter_type=null)
74 $from = date2sql($from_date);
75 $to = date2sql($to_date);
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'";
82 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
85 $sql .= " AND ".TB_PREF."gl_trans.account = '$account'";
88 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
91 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
93 if ($filter_type != null AND is_numeric($filter_type))
94 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
96 $sql .= " ORDER BY tran_date";
98 return db_query($sql, "The transactions for could not be retrieved");
102 //--------------------------------------------------------------------------------
104 function get_gl_trans($type, $trans_id)
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";
110 return db_query($sql, "The gl transactions could not be retrieved");
113 //--------------------------------------------------------------------------------
115 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
117 $from = date2sql($from_date);
118 $to = date2sql($to_date);
120 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
121 WHERE account='$account'";
122 if ($from_date != "")
123 $sql .= " AND tran_date > '$from'";
125 $sql .= " AND tran_date < '$to'";
127 $sql .= " AND dimension_id = $dimension";
129 $sql .= " AND dimension2_id = $dimension2";
131 $result = db_query($sql, "The starting balance for account $account could not be calculated");
133 $row = db_fetch_row($result);
137 //--------------------------------------------------------------------------------
139 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
141 $from = date2sql($from_date);
142 $to = date2sql($to_date);
144 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
145 WHERE account='$account'";
146 if ($from_date != "")
147 $sql .= " AND tran_date >= '$from'";
149 $sql .= " AND tran_date <= '$to'";
151 $sql .= " AND dimension_id = $dimension";
153 $sql .= " AND dimension2_id = $dimension2";
155 $result = db_query($sql, "Transactions for account $account could not be calculated");
157 $row = db_fetch_row($result);
161 //--------------------------------------------------------------------------------
163 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
166 $from = date2sql($from_date);
167 $to = date2sql($to_date);
169 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
170 WHERE account='$account' ";
171 if ($from_date != "")
172 $sql .= " AND tran_date >= '$from' ";
174 $sql .= " AND tran_date <= '$to' ";
176 $sql .= " AND dimension_id = $dimension";
178 $sql .= " AND dimension2_id = $dimension2";
179 $result = db_query($sql,"No budget accounts were returned");
181 $row = db_fetch_row($result);
185 //--------------------------------------------------------------------------------
187 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
191 $trans_type = systypes::journal_entry();
192 $trans_id = get_next_trans_no($trans_type);
194 foreach ($items as $journal_item)
196 $is_bank_to = is_bank_account($journal_item->code_id);
198 add_gl_trans($trans_type, $trans_id, $date_, $journal_item->code_id,
199 $journal_item->dimension_id, $journal_item->dimension2_id,
200 $journal_item->reference, $journal_item->amount);
203 add_bank_trans($trans_type, $trans_id, $journal_item->code_id, $ref,
204 $date_, 3, $journal_item->amount,
205 0, "", get_company_currency(),
206 "Cannot insert a destination bank transaction");
210 add_comments($trans_type, $trans_id, $date_, $memo_);
212 references::save($trans_type, $trans_id, $ref);
217 //$reversingDate = date(user_date_display(),
218 // Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
219 $reversingDate = begin_month(add_months($date_, 1));
221 $trans_id_reverse = get_next_trans_no($trans_type);
223 foreach ($items as $journal_item)
225 $is_bank_to = is_bank_account($journal_item->code_id);
227 add_gl_trans($trans_type, $trans_id_reverse, $reversingDate,
228 $journal_item->code_id, $journal_item->dimension_id, $journal_item->dimension2_id,
229 $journal_item->reference, -$journal_item->amount);
232 add_bank_trans($trans_type, $trans_id_reverse, $journal_item->code_id, $ref,
233 $reversingDate, 3, $journal_item->amount,
234 0, "", get_company_currency(),
235 "Cannot insert a destination bank transaction");
239 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
241 references::save($trans_type, $trans_id_reverse, $ref);
244 commit_transaction();
249 //--------------------------------------------------------------------------------------------------
251 function exists_gl_trans($type, $trans_id)
253 $sql = "SELECT type_no FROM ".TB_PREF."gl_trans WHERE type=$type AND type_no=$trans_id";
254 $result = db_query($sql, "Cannot retreive a gl transaction");
256 return (db_num_rows($result) > 0);
259 //--------------------------------------------------------------------------------------------------
261 function void_gl_trans($type, $trans_id, $nested=false)
266 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
268 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
271 commit_transaction();
274 //--------------------------------------------------------------------------------------------------