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 $sql = "INSERT INTO ".TB_PREF."cust_branch (debtor_no, br_name, branch_ref, br_address,
18 salesman, area, tax_group_id, sales_account, receivables_account, payment_discount_account,
19 sales_discount_account, default_location,
20 br_post_address, group_no, default_ship_via, notes, bank_account)
21 VALUES (".db_escape($customer_id). ",".db_escape($br_name) . ", "
22 .db_escape($br_ref) . ", "
23 .db_escape($br_address) . ", ".db_escape($salesman) . ", "
24 .db_escape($area) . ","
25 .db_escape($tax_group_id) . ", "
26 .db_escape($sales_account) . ", "
27 .db_escape($receivables_account) . ", "
28 .db_escape($payment_discount_account) . ", "
29 .db_escape($sales_discount_account) . ", "
30 .db_escape($default_location) . ", "
31 .db_escape($br_post_address) . ","
32 .db_escape($group_no) . ", "
33 .db_escape($default_ship_via). ", "
34 .db_escape($notes). ", "
35 .db_escape($bank_account, true).")";
36 db_query($sql,"The branch record could not be added");
39 function update_branch($customer_id, $branch_code, $br_name, $br_ref, $br_address,
40 $salesman, $area, $tax_group_id, $sales_account, $sales_discount_account, $receivables_account,
41 $payment_discount_account, $default_location, $br_post_address, $group_no,
42 $default_ship_via, $notes, $bank_account)
44 $sql = "UPDATE ".TB_PREF."cust_branch SET br_name = " . db_escape($br_name) . ",
45 branch_ref = " . db_escape($br_ref) . ",
46 br_address = ".db_escape($br_address). ",
47 salesman= ".db_escape($salesman) . ",
48 area=".db_escape($area) . ",
49 tax_group_id=".db_escape($tax_group_id). ",
50 sales_account=".db_escape($sales_account) . ",
51 sales_discount_account=".db_escape($sales_discount_account) . ",
52 receivables_account=".db_escape($receivables_account) . ",
53 payment_discount_account=".db_escape($payment_discount_account) . ",
54 default_location=".db_escape($default_location) . ",
55 br_post_address =".db_escape($br_post_address) . ",
56 group_no=".db_escape($group_no) . ",
57 default_ship_via=".db_escape($default_ship_via) . ",
58 notes=".db_escape($notes) . ",
59 bank_account=".db_escape($bank_account, true)."
60 WHERE branch_code =".db_escape($branch_code) . "
61 AND debtor_no=".db_escape($customer_id);
62 db_query($sql,"The branch record could not be updated");
65 function delete_branch($customer_id, $branch_code)
67 delete_entity_contacts('cust_branch', $branch_code);
69 $sql="DELETE FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_code)." AND debtor_no=".db_escape($customer_id);
70 db_query($sql,"could not delete branch");
73 function branch_in_foreign_table($customer_id, $branch_code, $table)
75 $sql= "SELECT COUNT(*) FROM ".TB_PREF."$table WHERE branch_code=".db_escape($branch_code)
76 ." AND debtor_no = ".db_escape($customer_id);
77 $result = db_query($sql,"could not query $table");
78 $myrow = db_fetch_row($result);
79 return ($myrow[0] > 0);
82 function get_branch($branch_id)
84 $sql = "SELECT branch.*, salesman.salesman_name
86 .TB_PREF."cust_branch branch,"
87 .TB_PREF."salesman salesman
88 WHERE branch.salesman=salesman.salesman_code
89 AND branch_code=".db_escape($branch_id);
91 $result = db_query($sql, "Cannot retreive a customer branch");
93 return db_fetch($result);
96 function get_cust_branch($customer_id, $branch_code)
98 $sql = "SELECT * FROM ".TB_PREF."cust_branch
99 WHERE branch_code=".db_escape($branch_code)."
100 AND debtor_no=".db_escape($customer_id);
101 $result = db_query($sql,"check failed");
102 return db_fetch($result);
105 function get_branch_accounts($branch_id)
107 $sql = "SELECT receivables_account,sales_account, sales_discount_account, payment_discount_account
108 FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_id);
110 $result = db_query($sql, "Cannot retreive a customer branch");
112 return db_fetch($result);
115 function get_branch_name($branch_id)
117 $sql = "SELECT br_name FROM ".TB_PREF."cust_branch
118 WHERE branch_code = ".db_escape($branch_id);
120 $result = db_query($sql,"could not retreive name for branch" . $branch_id);
122 $myrow = db_fetch_row($result);
126 function get_cust_branches_from_group($group_no)
128 $sql = "SELECT branch_code, debtor_no FROM ".TB_PREF."cust_branch
129 WHERE group_no = ".db_escape($group_no);
131 return db_query($sql,"could not retreive branches for group " . $group_no);
134 function get_default_info_for_branch($customer_id)
136 $sql = "SELECT name, address, debtor_ref
137 FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($customer_id);
138 $result = db_query($sql,"check failed");
139 return db_fetch($result);
142 function get_sql_for_customer_branches($customer_id)
148 ."p.name as contact_name, "
154 ."t.name AS tax_group_name, "
156 FROM ".TB_PREF."cust_branch b "
157 . "LEFT JOIN ".TB_PREF."crm_contacts c
158 ON c.entity_id=b.branch_code AND c.type='cust_branch' AND c.action='general'
159 LEFT JOIN ".TB_PREF."crm_persons p on c.person_id=p.id,"
161 .TB_PREF."salesman s, "
162 .TB_PREF."tax_groups t
163 WHERE b.tax_group_id=t.id
164 AND b.area=a.area_code
165 AND b.salesman=s.salesman_code
166 AND b.debtor_no = ".db_escape($customer_id);
168 if (!get_post('show_inactive')) $sql .= " AND !b.inactive";
169 $sql .= " GROUP BY b.branch_code ORDER BY branch_ref";
174 Get contacts of given type for customer branch.
175 $branch_code - branch id
176 $action - type of contact
177 $customer_id - if passed: get also customer contacts for given action
178 $default - return only single record selected with defaults order defined in $defs array,
179 otherways get all $action contacts
181 function get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = true)
183 $defs = array('cust_branch.'.$action,
185 'cust_branch.general',
188 $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
190 .TB_PREF."crm_persons p,"
191 .TB_PREF."crm_contacts r
192 WHERE r.person_id=p.id AND ((r.type='cust_branch'
193 AND r.entity_id=".db_escape($branch_code).')';
195 $sql .= " OR (r.type='customer' AND r.entity_id=".db_escape($customer_id).")";
200 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'" : '').')';
202 $res = db_query($sql, "can't retrieve branch contacts");
205 while($contact = db_fetch($res))
206 $results[] = $contact;
208 if ($results && $default) {
209 // select first available contact in $defs order
210 foreach($defs as $type) {
211 if ($n = array_search_value($type, $results, 'ext_type'))
219 function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = false)
221 $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
222 FROM ".TB_PREF."crm_persons p,"
223 .TB_PREF."crm_contacts r
224 WHERE r.person_id=p.id AND r.type='cust_branch'
225 AND r.entity_id=".db_escape($branch_code);
228 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
231 $sql = "($sql) UNION (SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type
232 FROM ".TB_PREF."crm_persons p,"
233 .TB_PREF."crm_contacts r
234 WHERE r.person_id=p.id AND r.type='customer'
235 AND r.entity_id=".db_escape($customer_id);
237 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
241 $res = db_query($sql, "can't retrieve branch contacts");
244 while($contact = db_fetch($res)) {
245 if ($type && $type != $contact['type']) break; // skip cust when branch contacts found
246 $results[] = $contact;
247 $type = $contact['type'];
249 if ($results && $default) {
250 // here we have general and action contacts selected
251 if ($n = array_search_value($action, $results, 'action')) {
254 // only general contact found
260 function get_default_branch($customer_id, $ar_account=null)
263 FROM ".TB_PREF."cust_branch WHERE debtor_no = ".db_escape($customer_id);
265 $sql .= " AND receivables_account=".db_escape($ar_account);
266 $result = db_query($sql,"cannot retrieve default branch");
267 return db_fetch($result);
270 function get_branches_search($customer, $branch)
274 if (isset($SysPrefs->max_rows_in_search))
275 $limit = $SysPrefs->max_rows_in_search;
283 p.name as contact_name,
285 FROM ".TB_PREF."cust_branch b
286 LEFT JOIN ".TB_PREF."crm_contacts c
287 ON c.entity_id=b.branch_code AND c.type='cust_branch' AND c.action='general'
288 LEFT JOIN ".TB_PREF."crm_persons p ON c.person_id=p.id
289 WHERE b.debtor_no = ".db_escape($customer)."
290 AND b.br_name LIKE " . db_escape("%" . $branch. "%") . "
291 ORDER BY b.br_name LIMIT 0,".(int)($limit);
293 return db_query($sql, "Failed in retreiving branches list.");