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="", $rate=0)
13 global $use_audit_trail;
15 $date = date2sql($date_);
16 if ($currency != null)
19 $amount_in_home_currency = to_home_currency($amount, $currency, $date_);
21 $amount_in_home_currency = round($amount * $rate, user_price_dec());
24 $amount_in_home_currency = $amount;
25 if ($dimension == null || $dimension < 0)
27 if ($dimension2 == null || $dimension2 < 0)
29 if (isset($use_audit_trail) && $use_audit_trail)
31 if ($memo_ == "" || $memo_ == null)
32 $memo_ = $_SESSION["wa_current_user"]->username;
34 $memo_ = $_SESSION["wa_current_user"]->username . " - " . $memo_;
36 $sql = "INSERT INTO ".TB_PREF."gl_trans ( type, type_no, tran_date,
37 account, dimension_id, dimension2_id, memo_, amount";
39 if ($person_type_id != null)
40 $sql .= ", person_type_id, person_id";
44 $sql .= "VALUES ($type, $trans_id, '$date',
45 '$account', $dimension, $dimension2, ".db_escape($memo_).", $amount_in_home_currency";
47 if ($person_type_id != null)
48 $sql .= ", $person_type_id, ". db_escape($person_id);
53 $err_msg = "The GL transaction could not be inserted";
55 return db_query($sql, $err_msg);
58 //--------------------------------------------------------------------------------
60 // GL Trans for standard costing, always home currency regardless of person
61 // $date_ is display date (non-sql)
62 // $amount is in HOME currency
64 function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $dimension2,
65 $memo_, $amount, $person_type_id=null, $person_id=null, $err_msg="")
68 return add_gl_trans($type, $trans_id, $date_, $account, $dimension, $dimension2, $memo_,
69 $amount, null, $person_type_id, $person_id, $err_msg);
74 //--------------------------------------------------------------------------------
76 function get_gl_transactions($from_date, $to_date, $trans_no=0,
77 $account=null, $dimension=0, $dimension2=0, $filter_type=null)
79 $from = date2sql($from_date);
80 $to = date2sql($to_date);
82 $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
83 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
84 AND tran_date >= '$from'
85 AND tran_date <= '$to'";
87 $sql .= " AND ".TB_PREF."gl_trans.type_no LIKE '%$trans_no'";
90 $sql .= " AND ".TB_PREF."gl_trans.account = '$account'";
93 $sql .= " AND ".TB_PREF."gl_trans.dimension_id = $dimension";
96 $sql .= " AND ".TB_PREF."gl_trans.dimension2_id = $dimension2";
98 if ($filter_type != null AND is_numeric($filter_type))
99 $sql .= " AND ".TB_PREF."gl_trans.type= $filter_type";
101 $sql .= " ORDER BY tran_date";
103 return db_query($sql, "The transactions for could not be retrieved");
107 //--------------------------------------------------------------------------------
109 function get_gl_trans($type, $trans_id)
111 $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
112 WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
113 AND ".TB_PREF."gl_trans.type=$type AND ".TB_PREF."gl_trans.type_no=$trans_id";
115 return db_query($sql, "The gl transactions could not be retrieved");
118 //--------------------------------------------------------------------------------
120 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
122 $from = date2sql($from_date);
123 $to = date2sql($to_date);
125 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
126 WHERE account='$account'";
127 if ($from_date != "")
128 $sql .= " AND tran_date > '$from'";
130 $sql .= " AND tran_date < '$to'";
132 $sql .= " AND dimension_id = $dimension";
134 $sql .= " AND dimension2_id = $dimension2";
136 $result = db_query($sql, "The starting balance for account $account could not be calculated");
138 $row = db_fetch_row($result);
142 //--------------------------------------------------------------------------------
144 function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
146 $from = date2sql($from_date);
147 $to = date2sql($to_date);
149 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans
150 WHERE account='$account'";
151 if ($from_date != "")
152 $sql .= " AND tran_date >= '$from'";
154 $sql .= " AND tran_date <= '$to'";
156 $sql .= " AND dimension_id = $dimension";
158 $sql .= " AND dimension2_id = $dimension2";
160 $result = db_query($sql, "Transactions for account $account could not be calculated");
162 $row = db_fetch_row($result);
166 //--------------------------------------------------------------------------------
168 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
171 $from = date2sql($from_date);
172 $to = date2sql($to_date);
174 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
175 WHERE account='$account' ";
176 if ($from_date != "")
177 $sql .= " AND tran_date >= '$from' ";
179 $sql .= " AND tran_date <= '$to' ";
181 $sql .= " AND dimension_id = $dimension";
183 $sql .= " AND dimension2_id = $dimension2";
184 $result = db_query($sql,"No budget accounts were returned");
186 $row = db_fetch_row($result);
190 //--------------------------------------------------------------------------------
192 function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
196 $trans_type = systypes::journal_entry();
197 $trans_id = get_next_trans_no($trans_type);
199 foreach ($items as $journal_item)
201 // post to first found bank account using given gl acount code.
202 $is_bank_to = is_bank_account($journal_item->code_id);
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);
209 add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
210 $date_, $journal_item->amount,
211 0, "", get_company_currency(),
212 "Cannot insert a destination bank transaction");
216 add_comments($trans_type, $trans_id, $date_, $memo_);
218 references::save($trans_type, $trans_id, $ref);
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));
227 $trans_id_reverse = get_next_trans_no($trans_type);
229 foreach ($items as $journal_item)
231 $is_bank_to = is_bank_account($journal_item->code_id);
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);
238 add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
239 $reversingDate, $journal_item->amount,
240 0, "", get_company_currency(),
241 "Cannot insert a destination bank transaction");
245 add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
247 references::save($trans_type, $trans_id_reverse, $ref);
250 commit_transaction();
255 //--------------------------------------------------------------------------------------------------
257 function exists_gl_trans($type, $trans_id)
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");
262 return (db_num_rows($result) > 0);
265 //--------------------------------------------------------------------------------------------------
267 function void_gl_trans($type, $trans_id, $nested=false)
272 $sql = "UPDATE ".TB_PREF."gl_trans SET amount=0 WHERE type=$type AND type_no=$trans_id";
274 db_query($sql, "could not void gl transactions for type=$type and trans_no=$trans_id");
277 commit_transaction();
280 //--------------------------------------------------------------------------------------------------