2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //---------------------------------------------------------------------------------------------
13 function clear_dflt_curr_account($curr_code) {
14 $sql = "UPDATE ".TB_PREF."bank_accounts SET dflt_curr_act=0 WHERE bank_curr_code="
15 .db_escape($curr_code);
16 db_query($sql, "could not update default currency account");
19 function add_bank_account($account_code, $account_type, $bank_account_name,
20 $bank_name, $bank_account_number, $bank_address, $bank_curr_code,
23 if($dflt_curr_act) // only one default account for any currency
24 clear_dflt_curr_account($bank_curr_code);
26 $sql = "INSERT INTO ".TB_PREF."bank_accounts (account_code, account_type,
27 bank_account_name, bank_name, bank_account_number, bank_address,
28 bank_curr_code, dflt_curr_act)
29 VALUES (".db_escape($account_code).", $account_type, ".db_escape($bank_account_name).", ".db_escape($bank_name).", ".db_escape($bank_account_number).",
30 ".db_escape($bank_address).", '$bank_curr_code', '$dflt_curr_act')";
32 db_query($sql, "could not add a bank account for $account_code");
35 //---------------------------------------------------------------------------------------------
37 function update_bank_account($id, $account_code, $account_type, $bank_account_name,
38 $bank_name, $bank_account_number, $bank_address, $bank_curr_code, $dflt_curr_act)
40 if($dflt_curr_act) // only one default account for any currency
41 clear_dflt_curr_account($bank_curr_code);
43 $sql = "UPDATE ".TB_PREF."bank_accounts SET account_type = $account_type,
44 account_code=".db_escape($account_code).",
45 bank_account_name=".db_escape($bank_account_name).", bank_name=".db_escape($bank_name).",
46 bank_account_number=".db_escape($bank_account_number).", bank_curr_code='$bank_curr_code',
47 bank_address=".db_escape($bank_address).","
48 ."dflt_curr_act='$dflt_curr_act' WHERE id = '$id'";
50 db_query($sql, "could not update bank account for $account_code");
53 //---------------------------------------------------------------------------------------------
55 function delete_bank_account($id)
57 $sql = "DELETE FROM ".TB_PREF."bank_accounts WHERE id='$id'";
59 db_query($sql,"could not delete bank account for $id");
63 //---------------------------------------------------------------------------------------------
65 function get_bank_account($id)
67 $sql = "SELECT * FROM ".TB_PREF."bank_accounts WHERE id='$id'";
69 $result = db_query($sql, "could not retreive bank account for $id");
71 return db_fetch($result);
74 //---------------------------------------------------------------------------------------------
75 function get_bank_gl_account($id)
77 $sql = "SELECT account_code FROM ".TB_PREF."bank_accounts WHERE id='$id'";
79 $result = db_query($sql, "could not retreive bank account for $id");
81 $bank_account = db_fetch($result);
83 return $bank_account['account_code'];
86 //---------------------------------------------------------------------------------------------
88 function add_quick_entry($description, $type, $base_amount, $base_desc)
90 $sql = "INSERT INTO ".TB_PREF."quick_entries (description, type, base_amount, base_desc)
91 VALUES (".db_escape($description).", $type, "
92 .db_escape($base_amount).", ".db_escape($base_desc).")";
94 db_query($sql, "could not insert quick entry for $description");
97 //---------------------------------------------------------------------------------------------
99 function update_quick_entry($selected_id, $description, $type, $base_amount, $base_desc)
101 $sql = "UPDATE ".TB_PREF."quick_entries SET description = ".db_escape($description).",
102 type=$type, base_amount=".db_escape($base_amount).", base_desc=".db_escape($base_desc)."
103 WHERE id = $selected_id";
105 db_query($sql, "could not update quick entry for $selected_id");
108 //---------------------------------------------------------------------------------------------
110 function delete_quick_entry($selected_id)
112 $sql = "DELETE FROM ".TB_PREF."quick_entries WHERE id=$selected_id";
114 db_query($sql,"could not delete quick entry $selected_id");
117 //---------------------------------------------------------------------------------------------
119 function add_quick_entry_line($qid, $action, $dest_id, $amount, $dim, $dim2)
121 $sql = "INSERT INTO ".TB_PREF."quick_entry_lines
122 (qid, action, dest_id, amount, dimension_id, dimension2_id)
124 ($qid, ".db_escape($action).",".db_escape($dest_id).",
125 $amount, $dim, $dim2)";
127 db_query($sql, "could not insert quick entry line for $qid");
130 //---------------------------------------------------------------------------------------------
132 function update_quick_entry_line($selected_id, $qid, $action, $dest_id, $amount, $dim, $dim2)
134 $sql = "UPDATE ".TB_PREF."quick_entry_lines SET qid = $qid, action=".db_escape($action).",
135 dest_id=".db_escape($dest_id).", amount=$amount, dimension_id=$dim, dimension2_id=$dim2
136 WHERE id = $selected_id";
138 db_query($sql, "could not update quick entry line for $selected_id");
141 //---------------------------------------------------------------------------------------------
143 function delete_quick_entry_line($selected_id)
145 $sql = "DELETE FROM ".TB_PREF."quick_entry_lines WHERE id=$selected_id";
147 db_query($sql,"could not delete quick entry line $selected_id");
149 //---------------------------------------------------------------------------------------------
152 function has_quick_entries($type=null)
154 $sql = "SELECT id FROM ".TB_PREF."quick_entries";
156 $sql .= " WHERE type=$type";
158 $result = db_query($sql, "could not retreive quick entries");
159 return db_num_rows($result) > 0;
162 function get_quick_entries($type = null)
164 $sql = "SELECT * FROM ".TB_PREF."quick_entries";
166 $sql .= " WHERE type=$type";
167 $sql .= " ORDER BY description";
169 return db_query($sql, "could not retreive quick entries");
172 function get_quick_entry($selected_id)
174 $sql = "SELECT * FROM ".TB_PREF."quick_entries WHERE id=$selected_id";
176 $result = db_query($sql, "could not retreive quick entry $selected_id");
178 return db_fetch($result);
181 function get_quick_entry_lines($qid)
183 $sql = "SELECT ".TB_PREF."quick_entry_lines.*, ".TB_PREF."chart_master.account_name,
184 ".TB_PREF."tax_types.name as tax_name
185 FROM ".TB_PREF."quick_entry_lines
186 LEFT JOIN ".TB_PREF."chart_master ON
187 ".TB_PREF."quick_entry_lines.dest_id = ".TB_PREF."chart_master.account_code
188 LEFT JOIN ".TB_PREF."tax_types ON
189 ".TB_PREF."quick_entry_lines.dest_id = ".TB_PREF."tax_types.id
194 return db_query($sql, "could not retreive quick entries");
197 function has_quick_entry_lines($qid)
199 $sql = "SELECT id FROM ".TB_PREF."quick_entry_lines WHERE qid=$qid";
201 $result = db_query($sql, "could not retreive quick entries");
202 return db_num_rows($result) > 0;
205 //---------------------------------------------------------------------------------------------
207 function get_quick_entry_line($selected_id)
209 $sql = "SELECT * FROM ".TB_PREF."quick_entry_lines WHERE id=$selected_id";
211 $result = db_query($sql, "could not retreive quick entry for $selected_id");
213 return db_fetch($result);
216 //---------------------------------------------------------------------------------------------
218 function get_default_bank_account($curr)
220 /* default bank account is selected as first found account from:
221 . default account in $curr if any
222 . first defined account in $curr if any
223 . default account in home currency
224 . first defined account in home currency
226 $home_curr = get_company_pref('curr_default');
228 $sql = "SELECT b.*, b.bank_curr_code='$home_curr' as fall_back FROM "
229 .TB_PREF."bank_accounts b"
230 ." WHERE b.bank_curr_code=".db_escape($curr)
231 ." OR b.bank_curr_code='$home_curr'
232 ORDER BY fall_back, dflt_curr_act desc";
234 $result = db_query($sql, "could not retreive default bank account");
236 return db_fetch($result);