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 function add_branch($customer_id, $br_name, $br_ref, $br_address, $salesman, $area,
13 $tax_group_id, $sales_account, $sales_discount_account, $receivables_account,
14 $payment_discount_account, $default_location, $br_post_address, $group_no,
15 $default_ship_via, $notes, $bank_account)
17 begin_transaction(__FUNCTION__, func_get_args());
18 $sql = "INSERT INTO ".TB_PREF."cust_branch (debtor_no, br_name, branch_ref, br_address,
19 salesman, area, tax_group_id, sales_account, receivables_account, payment_discount_account,
20 sales_discount_account, default_location,
21 br_post_address, group_no, default_ship_via, notes, bank_account)
22 VALUES (".db_escape($customer_id). ",".db_escape($br_name) . ", "
23 .db_escape($br_ref) . ", "
24 .db_escape($br_address) . ", ".db_escape($salesman) . ", "
25 .db_escape($area) . ","
26 .db_escape($tax_group_id) . ", "
27 .db_escape($sales_account) . ", "
28 .db_escape($receivables_account) . ", "
29 .db_escape($payment_discount_account) . ", "
30 .db_escape($sales_discount_account) . ", "
31 .db_escape($default_location) . ", "
32 .db_escape($br_post_address) . ","
33 .db_escape($group_no) . ", "
34 .db_escape($default_ship_via). ", "
35 .db_escape($notes). ", "
36 .db_escape($bank_account, true).")";
37 db_query($sql,"The branch record could not be added");
41 function update_branch($customer_id, $branch_code, $br_name, $br_ref, $br_address,
42 $salesman, $area, $tax_group_id, $sales_account, $sales_discount_account, $receivables_account,
43 $payment_discount_account, $default_location, $br_post_address, $group_no,
44 $default_ship_via, $notes, $bank_account)
46 begin_transaction(__FUNCTION__, func_get_args());
47 $sql = "UPDATE ".TB_PREF."cust_branch SET br_name = " . db_escape($br_name) . ",
48 branch_ref = " . db_escape($br_ref) . ",
49 br_address = ".db_escape($br_address). ",
50 salesman= ".db_escape($salesman) . ",
51 area=".db_escape($area) . ",
52 tax_group_id=".db_escape($tax_group_id). ",
53 sales_account=".db_escape($sales_account) . ",
54 sales_discount_account=".db_escape($sales_discount_account) . ",
55 receivables_account=".db_escape($receivables_account) . ",
56 payment_discount_account=".db_escape($payment_discount_account) . ",
57 default_location=".db_escape($default_location) . ",
58 br_post_address =".db_escape($br_post_address) . ",
59 group_no=".db_escape($group_no) . ",
60 default_ship_via=".db_escape($default_ship_via) . ",
61 notes=".db_escape($notes) . ",
62 bank_account=".db_escape($bank_account, true)."
63 WHERE branch_code =".db_escape($branch_code) . "
64 AND debtor_no=".db_escape($customer_id);
65 db_query($sql,"The branch record could not be updated");
69 function delete_branch($customer_id, $branch_code)
71 begin_transaction(__FUNCTION__, func_get_args());
72 delete_entity_contacts('cust_branch', $branch_code);
74 $sql="DELETE FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_code)." AND debtor_no=".db_escape($customer_id);
75 db_query($sql,"could not delete branch");
79 function branch_in_foreign_table($customer_id, $branch_code, $table)
81 $sql= "SELECT COUNT(*) FROM ".TB_PREF."$table WHERE branch_code=".db_escape($branch_code)
82 ." AND debtor_no = ".db_escape($customer_id);
83 $result = db_query($sql,"could not query $table");
84 $myrow = db_fetch_row($result);
85 return ($myrow[0] > 0);
88 function get_branch($branch_id)
90 $sql = "SELECT branch.*, salesman.salesman_name
92 .TB_PREF."cust_branch branch,"
93 .TB_PREF."salesman salesman
94 WHERE branch.salesman=salesman.salesman_code
95 AND branch_code=".db_escape($branch_id);
97 $result = db_query($sql, "Cannot retreive a customer branch");
99 return db_fetch($result);
102 function get_cust_branch($customer_id, $branch_code)
104 $sql = "SELECT * FROM ".TB_PREF."cust_branch
105 WHERE branch_code=".db_escape($branch_code)."
106 AND debtor_no=".db_escape($customer_id);
107 $result = db_query($sql,"check failed");
108 return db_fetch($result);
111 function get_branch_accounts($branch_id)
113 $sql = "SELECT receivables_account,sales_account, sales_discount_account, payment_discount_account
114 FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_id);
116 $result = db_query($sql, "Cannot retreive a customer branch");
118 return db_fetch($result);
121 function get_branch_name($branch_id)
123 $sql = "SELECT br_name FROM ".TB_PREF."cust_branch
124 WHERE branch_code = ".db_escape($branch_id);
126 $result = db_query($sql,"could not retreive name for branch" . $branch_id);
128 $myrow = db_fetch_row($result);
132 function get_cust_branches_from_group($group_no)
134 $sql = "SELECT branch_code, debtor_no FROM ".TB_PREF."cust_branch
135 WHERE group_no = ".db_escape($group_no);
137 return db_query($sql,"could not retreive branches for group " . $group_no);
140 function get_default_info_for_branch($customer_id)
142 $sql = "SELECT name, address, debtor_ref
143 FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($customer_id);
144 $result = db_query($sql,"check failed");
145 return db_fetch($result);
148 function get_sql_for_customer_branches($customer_id)
154 ."p.name as contact_name, "
160 ."t.name AS tax_group_name, "
162 FROM ".TB_PREF."cust_branch b "
163 . "LEFT JOIN ".TB_PREF."crm_contacts c
164 ON c.entity_id=b.branch_code AND c.type='cust_branch' AND c.action='general'
165 LEFT JOIN ".TB_PREF."crm_persons p on c.person_id=p.id,"
167 .TB_PREF."salesman s, "
168 .TB_PREF."tax_groups t
169 WHERE b.tax_group_id=t.id
170 AND b.area=a.area_code
171 AND b.salesman=s.salesman_code
172 AND b.debtor_no = ".db_escape($customer_id);
174 if (!get_post('show_inactive')) $sql .= " AND !b.inactive";
175 $sql .= " GROUP BY b.branch_code ORDER BY branch_ref";
180 Get contacts of given type for customer branch.
181 $branch_code - branch id
182 $action - type of contact
183 $customer_id - if passed: get also customer contacts for given action
184 $default - return only single record selected with defaults order defined in $defs array,
185 otherways get all $action contacts
187 function get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = true)
189 $defs = array('cust_branch.'.$action,
191 'cust_branch.general',
194 $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
196 .TB_PREF."crm_persons p,"
197 .TB_PREF."crm_contacts r
198 WHERE r.person_id=p.id AND ((r.type='cust_branch'
199 AND r.entity_id=".db_escape($branch_code).')';
201 $sql .= " OR (r.type='customer' AND r.entity_id=".db_escape($customer_id).")";
206 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'" : '').')';
208 $res = db_query($sql, "can't retrieve branch contacts");
211 while($contact = db_fetch($res))
212 $results[] = $contact;
214 if ($results && $default) {
215 // select first available contact in $defs order
216 foreach($defs as $type) {
217 if ($n = array_search_value($type, $results, 'ext_type'))
225 function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = false)
227 $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
228 FROM ".TB_PREF."crm_persons p,"
229 .TB_PREF."crm_contacts r
230 WHERE r.person_id=p.id AND r.type='cust_branch'
231 AND r.entity_id=".db_escape($branch_code);
234 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
237 $sql = "($sql) UNION (SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
238 FROM ".TB_PREF."crm_persons p,"
239 .TB_PREF."crm_contacts r
240 WHERE r.person_id=p.id AND r.type='customer'
241 AND r.entity_id=".db_escape($customer_id);
243 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
247 $res = db_query($sql, "can't retrieve branch contacts");
250 while($contact = db_fetch($res)) {
251 if ($type && $type != $contact['type']) break; // skip cust when branch contacts found
252 $results[] = $contact;
253 $type = $contact['type'];
255 if ($results && $default) {
256 // here we have general and action contacts selected
257 if ($n = array_search_value($action, $results, 'action')) {
260 // only general contact found
266 function get_default_branch($customer_id, $ar_account=null)
269 FROM ".TB_PREF."cust_branch WHERE debtor_no = ".db_escape($customer_id);
271 $sql .= " AND receivables_account=".db_escape($ar_account);
272 $result = db_query($sql,"cannot retrieve default branch");
273 return db_fetch($result);
276 function get_branches_search($customer, $branch)
280 if (isset($SysPrefs->max_rows_in_search))
281 $limit = $SysPrefs->max_rows_in_search;
289 p.name as contact_name,
291 FROM ".TB_PREF."cust_branch b
292 LEFT JOIN ".TB_PREF."crm_contacts c
293 ON c.entity_id=b.branch_code AND c.type='cust_branch' AND c.action='general'
294 LEFT JOIN ".TB_PREF."crm_persons p ON c.person_id=p.id
295 WHERE b.debtor_no = ".db_escape($customer)."
296 AND b.br_name LIKE " . db_escape("%" . $branch. "%") . "
297 ORDER BY b.br_name LIMIT 0,".(int)($limit);
299 return db_query($sql, "Failed in retreiving branches list.");