e51de6f149234bd8004084fa8545d0f0919025ae
[fa-stable.git] / sales / includes / db / branches_db.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 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)
16 {
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");
37 }
38
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)
43 {
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");
63 }
64
65 function delete_branch($customer_id, $branch_code)
66 {
67         delete_entity_contacts('cust_branch', $branch_code);
68
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");
71 }
72
73 function branch_in_foreign_table($customer_id, $branch_code, $table)
74 {
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);
80 }
81
82 function get_branch($branch_id)
83 {
84         $sql = "SELECT branch.*, salesman.salesman_name 
85                 FROM "
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);
90
91         $result = db_query($sql, "Cannot retreive a customer branch");
92
93         return db_fetch($result);
94 }
95
96 function get_cust_branch($customer_id, $branch_code)
97 {
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);
103 }
104
105 function get_branch_accounts($branch_id)
106 {
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);
109         
110         $result = db_query($sql, "Cannot retreive a customer branch");
111         
112         return db_fetch($result);
113 }
114
115 function get_branch_name($branch_id)
116 {
117         $sql = "SELECT br_name FROM ".TB_PREF."cust_branch 
118                 WHERE branch_code = ".db_escape($branch_id);
119
120         $result = db_query($sql,"could not retreive name for branch" . $branch_id);
121
122         $myrow = db_fetch_row($result);
123         return $myrow[0];
124 }
125
126 function get_cust_branches_from_group($group_no)
127 {
128         $sql = "SELECT branch_code, debtor_no FROM ".TB_PREF."cust_branch 
129                 WHERE group_no = ".db_escape($group_no);
130
131         return db_query($sql,"could not retreive branches for group " . $group_no);
132 }
133
134 function get_default_info_for_branch($customer_id)
135 {
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);
140 }
141
142 function get_sql_for_customer_branches($customer_id)
143 {
144         $sql = "SELECT "
145                 ."b.branch_code, "
146                 ."b.branch_ref, "
147                 ."b.br_name, "
148                 ."p.name as contact_name, "
149                 ."s.salesman_name, "
150                 ."a.description, "
151                 ."p.phone, "
152                 ."p.fax, "
153                 ."p.email, "
154                 ."t.name AS tax_group_name, "
155                 ."b.inactive
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,"
160                         .TB_PREF."areas a, "
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);
167
168         if (!get_post('show_inactive')) $sql .= " AND !b.inactive";
169         $sql .= " GROUP BY b.branch_code ORDER BY branch_ref";
170
171         return $sql;
172 }
173 /*
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
180 */
181 function get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = true)
182 {
183         $defs = array('cust_branch.'.$action, 
184                                 'customer.'.$action,
185                                 'cust_branch.general',
186                                 'customer.general');
187
188         $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type 
189                 FROM "
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).')';
194         if($customer_id) {
195                 $sql .= " OR (r.type='customer' AND r.entity_id=".db_escape($customer_id).")";
196         }
197         $sql .= ')';
198         
199         if ($action)
200                 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'" : '').')';
201         
202         $res = db_query($sql, "can't retrieve branch contacts");
203
204         $results = array();
205         while($contact = db_fetch($res))
206                 $results[] = $contact;
207
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'))
212                                 return $n;
213                 }
214                 return null;
215         }
216         return $results;
217 }
218
219 function _get_branch_contacts($branch_code, $action=null, $customer_id=null, $default = false)
220 {
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);
226
227         if ($action)
228                 $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
229
230         if($customer_id) {
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);
236                 if ($action) 
237                         $sql .= ' AND (r.action='.db_escape($action).($default ? " OR r.action='general'":'').')';
238                 $sql .= ')';
239         }
240
241         $res = db_query($sql, "can't retrieve branch contacts");
242         $results = array();
243         $type = '';
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'];
248         }
249         if ($results && $default) {
250                 // here we have general and action contacts selected
251                 if ($n = array_search_value($action, $results, 'action')) {
252                         return $n;
253                 }
254                 // only general contact found
255                 return $results[0];
256         }
257         return $results;
258 }
259
260 function get_default_branch($customer_id, $ar_account=null)
261 {
262         $sql = "SELECT * 
263                 FROM ".TB_PREF."cust_branch WHERE debtor_no = ".db_escape($customer_id);
264         if($ar_account)
265                 $sql .= " AND receivables_account=".db_escape($ar_account);
266         $result = db_query($sql,"cannot retrieve default branch");
267         return db_fetch($result);
268 }
269
270 function  get_branches_search($customer, $branch)
271 {
272         global $SysPrefs;
273
274         if (isset($SysPrefs->max_rows_in_search))
275                 $limit = $SysPrefs->max_rows_in_search;
276         else
277                 $limit = 10;
278
279         $sql = "SELECT 
280                     b.branch_code,
281                 b.branch_ref,
282                 b.br_name,
283                 p.name as contact_name,
284                     p.phone
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);
292
293         return db_query($sql, "Failed in retreiving branches list.");
294 }
295