Merged changes from main CVS up to 2.1.5
[fa-stable.git] / gl / includes / db / gl_db_bank_accounts.inc
1 <?php
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
14 function add_bank_account($account_code, $account_type, $bank_account_name, $bank_name, $bank_account_number,
15         $bank_address, $bank_curr_code)
16 {
17         $sql = "INSERT INTO ".TB_PREF."bank_accounts (account_code, account_type, bank_account_name, bank_name, bank_account_number, bank_address, bank_curr_code)
18                 VALUES (".db_escape($account_code).", $account_type, ".db_escape($bank_account_name).", ".db_escape($bank_name).", ".db_escape($bank_account_number).",
19                 ".db_escape($bank_address).", '$bank_curr_code')";
20
21         db_query($sql, "could not add a bank account for $account_code");
22 }
23
24 //---------------------------------------------------------------------------------------------
25
26 function update_bank_account($id, $account_code, $account_type, $bank_account_name, $bank_name, $bank_account_number,
27         $bank_address, $bank_curr_code)
28 {
29         $sql = "UPDATE ".TB_PREF."bank_accounts SET account_type = $account_type,
30                 account_code=".db_escape($account_code).",
31                 bank_account_name=".db_escape($bank_account_name).", bank_name=".db_escape($bank_name).",
32                 bank_account_number=".db_escape($bank_account_number).", bank_curr_code='$bank_curr_code',
33                 bank_address=".db_escape($bank_address)." WHERE id = '$id'";
34
35         db_query($sql, "could not update bank account for $account_code");
36 }
37
38 //---------------------------------------------------------------------------------------------
39
40 function delete_bank_account($id)
41 {
42         $sql = "DELETE FROM ".TB_PREF."bank_accounts WHERE id='$id'";
43
44         db_query($sql,"could not delete bank account for $id");
45 }
46
47
48 //---------------------------------------------------------------------------------------------
49
50 function get_bank_account($id)
51 {
52         $sql = "SELECT * FROM ".TB_PREF."bank_accounts WHERE id='$id'";
53
54         $result = db_query($sql, "could not retreive bank account for $id");
55
56         return db_fetch($result);
57 }
58
59 //---------------------------------------------------------------------------------------------
60 function get_bank_gl_account($id)
61 {
62         $sql = "SELECT account_code FROM ".TB_PREF."bank_accounts WHERE id='$id'";
63
64         $result = db_query($sql, "could not retreive bank account for $id");
65
66         $bank_account = db_fetch($result);
67
68         return $bank_account['account_code'];
69 }
70
71 //---------------------------------------------------------------------------------------------
72
73 function add_quick_entry($description, $type, $base_amount, $base_desc)
74 {
75         $sql = "INSERT INTO ".TB_PREF."quick_entries (description, type, base_amount, base_desc) 
76         VALUES (".db_escape($description).", $type, "
77                 .db_escape($base_amount).", ".db_escape($base_desc).")";
78
79         db_query($sql, "could not insert quick entry for $description");
80 }
81
82 //---------------------------------------------------------------------------------------------
83
84 function update_quick_entry($selected_id, $description, $type, $base_amount, $base_desc)
85 {
86         $sql = "UPDATE ".TB_PREF."quick_entries SET description = ".db_escape($description).",
87                 type=$type, base_amount=".db_escape($base_amount).", base_desc=".db_escape($base_desc)."
88                 WHERE id = $selected_id";
89
90         db_query($sql, "could not update quick entry for $selected_id");
91 }
92
93 //---------------------------------------------------------------------------------------------
94
95 function delete_quick_entry($selected_id)
96 {
97         $sql = "DELETE FROM ".TB_PREF."quick_entries WHERE id=$selected_id";
98
99         db_query($sql,"could not delete quick entry $selected_id");
100 }
101
102 //---------------------------------------------------------------------------------------------
103
104 function add_quick_entry_line($qid, $action, $dest_id, $amount, $dim, $dim2)
105 {
106         $sql = "INSERT INTO ".TB_PREF."quick_entry_lines 
107                 (qid, action, dest_id, amount, dimension_id, dimension2_id) 
108         VALUES 
109                 ($qid, ".db_escape($action).",".db_escape($dest_id).",
110                         $amount, $dim, $dim2)";
111
112         db_query($sql, "could not insert quick entry line for $qid");
113 }
114
115 //---------------------------------------------------------------------------------------------
116
117 function update_quick_entry_line($selected_id, $qid, $action, $dest_id, $amount, $dim, $dim2)
118 {
119         $sql = "UPDATE ".TB_PREF."quick_entry_lines     SET qid = $qid, action=".db_escape($action).",
120                 dest_id=".db_escape($dest_id).", amount=$amount, dimension_id=$dim, dimension2_id=$dim2 
121                 WHERE id = $selected_id";
122
123         db_query($sql, "could not update quick entry line for $selected_id");
124 }
125
126 //---------------------------------------------------------------------------------------------
127
128 function delete_quick_entry_line($selected_id)
129 {
130         $sql = "DELETE FROM ".TB_PREF."quick_entry_lines WHERE id=$selected_id";
131
132         db_query($sql,"could not delete quick entry line $selected_id");
133 }
134 //---------------------------------------------------------------------------------------------
135
136
137 function has_quick_entries($type=null)
138 {
139         $sql = "SELECT id FROM ".TB_PREF."quick_entries";
140         if ($type != null)
141                 $sql .= " WHERE type=$type";
142
143         $result = db_query($sql, "could not retreive quick entries");
144         return db_num_rows($result) > 0;
145 }
146
147 function get_quick_entries($type = null)
148 {
149         $sql = "SELECT * FROM ".TB_PREF."quick_entries";
150         if ($type != null)
151                 $sql .= " WHERE type=$type";
152         $sql .= " ORDER BY description";
153
154         return db_query($sql, "could not retreive quick entries");
155 }
156
157 function get_quick_entry($selected_id)
158 {
159         $sql = "SELECT * FROM ".TB_PREF."quick_entries WHERE id=$selected_id";
160
161         $result = db_query($sql, "could not retreive quick entry $selected_id");
162
163         return db_fetch($result);
164 }       
165
166 function get_quick_entry_lines($qid)
167 {
168         $sql = "SELECT ".TB_PREF."quick_entry_lines.*, ".TB_PREF."chart_master.account_name,
169                         ".TB_PREF."tax_types.name as tax_name
170                 FROM ".TB_PREF."quick_entry_lines
171                 LEFT JOIN ".TB_PREF."chart_master ON
172                         ".TB_PREF."quick_entry_lines.dest_id = ".TB_PREF."chart_master.account_code
173                 LEFT JOIN ".TB_PREF."tax_types ON
174                         ".TB_PREF."quick_entry_lines.dest_id = ".TB_PREF."tax_types.id
175                 WHERE 
176                         qid=$qid
177                 ORDER by id";
178
179         return db_query($sql, "could not retreive quick entries");
180 }
181
182 function has_quick_entry_lines($qid)
183 {
184         $sql = "SELECT id FROM ".TB_PREF."quick_entry_lines WHERE qid=$qid";
185
186         $result = db_query($sql, "could not retreive quick entries");
187         return db_num_rows($result) > 0;
188 }
189
190 //---------------------------------------------------------------------------------------------
191
192 function get_quick_entry_line($selected_id)
193 {
194         $sql = "SELECT * FROM ".TB_PREF."quick_entry_lines WHERE id=$selected_id";
195
196         $result = db_query($sql, "could not retreive quick entry for $selected_id");
197
198         return db_fetch($result);
199 }
200
201 //---------------------------------------------------------------------------------------------
202
203 ?>