Eliminated non-static method calls and other bulk fixes to fix php5 warnings
[fa-stable.git] / sales / manage / customers.php
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 $page_security = 'SA_CUSTOMER';
13 $path_to_root = "../..";
14
15 include_once($path_to_root . "/includes/session.inc");
16 page(_("Customers"), @$_REQUEST['popup']); 
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/banking.inc");
20 include_once($path_to_root . "/includes/ui.inc");
21
22 if (isset($_GET['debtor_no'])) 
23 {
24         $_POST['customer_id'] = $_GET['debtor_no'];
25 }
26 $new_customer = (!isset($_POST['customer_id']) || $_POST['customer_id'] == ""); 
27 //--------------------------------------------------------------------------------------------
28
29 function can_process()
30 {
31         if (strlen($_POST['CustName']) == 0) 
32         {
33                 display_error(_("The customer name cannot be empty."));
34                 set_focus('CustName');
35                 return false;
36         } 
37
38         if (strlen($_POST['cust_ref']) == 0) 
39         {
40                 display_error(_("The customer short name cannot be empty."));
41                 set_focus('cust_ref');
42                 return false;
43         } 
44         
45         if (!check_num('credit_limit', 0))
46         {
47                 display_error(_("The credit limit must be numeric and not less than zero."));
48                 set_focus('credit_limit');
49                 return false;           
50         } 
51         
52         if (!check_num('pymt_discount', 0, 100)) 
53         {
54                 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
55                 set_focus('pymt_discount');
56                 return false;           
57         } 
58         
59         if (!check_num('discount', 0, 100)) 
60         {
61                 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
62                 set_focus('discount');
63                 return false;           
64         } 
65
66         return true;
67 }
68
69 //--------------------------------------------------------------------------------------------
70
71 function handle_submit()
72 {
73         global $path_to_root, $new_customer, $Ajax;
74
75         if (!can_process())
76                 return;
77                 
78         if ($new_customer == false) 
79         {
80
81                 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($_POST['CustName']) . ", 
82                         debtor_ref=" . db_escape($_POST['cust_ref']) . ",
83                         address=".db_escape($_POST['address']) . ", 
84                         tax_id=".db_escape($_POST['tax_id']) . ", 
85                         curr_code=".db_escape($_POST['curr_code']) . ", 
86                         email=".db_escape($_POST['email']) . ", 
87                         dimension_id=".db_escape($_POST['dimension_id']) . ", 
88                         dimension2_id=".db_escape($_POST['dimension2_id']) . ", 
89             credit_status=".db_escape($_POST['credit_status']) . ", 
90             payment_terms=".db_escape($_POST['payment_terms']) . ", 
91             discount=" . input_num('discount') / 100 . ", 
92             pymt_discount=" . input_num('pymt_discount') / 100 . ", 
93             credit_limit=" . input_num('credit_limit') . ", 
94             sales_type = ".db_escape($_POST['sales_type']) . " 
95             WHERE debtor_no = '". $_POST['customer_id'] . "'";
96
97                 db_query($sql,"The customer could not be updated");
98
99                 update_record_status($_POST['customer_id'], $_POST['inactive'],
100                         'debtors_master', 'debtor_no');
101
102                 $Ajax->activate('customer_id'); // in case of status change
103                 display_notification(_("Customer has been updated."));
104         } 
105         else 
106         {       //it is a new customer
107
108                 begin_transaction();
109
110                 $sql = "INSERT INTO ".TB_PREF."debtors_master (name, debtor_ref, address, tax_id, email, dimension_id, dimension2_id,  
111                         curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,  
112                         sales_type) VALUES (".db_escape($_POST['CustName']) .", " .db_escape($_POST['cust_ref']) .", "
113                         .db_escape($_POST['address']) . ", " . db_escape($_POST['tax_id']) . ","
114                         .db_escape($_POST['email']) . ", ".db_escape($_POST['dimension_id']) . ", " 
115                         .db_escape($_POST['dimension2_id']) . ", ".db_escape($_POST['curr_code']) . ", 
116                         " . db_escape($_POST['credit_status']) . ", ".db_escape($_POST['payment_terms']) . ", " . input_num('discount')/100 . ", 
117                         " . input_num('pymt_discount')/100 . ", " . input_num('credit_limit') . ", ".db_escape($_POST['sales_type']) . ")";
118
119                 db_query($sql,"The customer could not be added");
120
121                 $_POST['customer_id'] = db_insert_id();
122                 $new_customer = false;
123                 commit_transaction();                   
124
125                 display_notification(_("A new customer has been added."));
126
127                 $Ajax->activate('_page_body');
128         }
129 }
130 //--------------------------------------------------------------------------------------------
131
132 if (isset($_POST['submit'])) 
133 {
134         handle_submit();
135 }
136 //-------------------------------------------------------------------------------------------- 
137
138 if (isset($_POST['delete'])) 
139 {
140
141         //the link to delete a selected record was clicked instead of the submit button
142
143         $cancel_delete = 0;
144
145         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
146         $sel_id = db_escape($_POST['customer_id']);
147         $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE debtor_no=$sel_id";
148         $result = db_query($sql,"check failed");
149         $myrow = db_fetch_row($result);
150         if ($myrow[0] > 0) 
151         {
152                 $cancel_delete = 1;
153                 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
154         } 
155         else 
156         {
157                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE debtor_no=$sel_id";
158                 $result = db_query($sql,"check failed");
159                 $myrow = db_fetch_row($result);
160                 if ($myrow[0] > 0) 
161                 {
162                         $cancel_delete = 1;
163                         display_error(_("Cannot delete the customer record because orders have been created against it."));
164                 } 
165                 else 
166                 {
167                         $sql = "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE debtor_no=$sel_id";
168                         $result = db_query($sql,"check failed");
169                         $myrow = db_fetch_row($result);
170                         if ($myrow[0] > 0) 
171                         {
172                                 $cancel_delete = 1;
173                                 display_error(_("Cannot delete this customer because there are branch records set up against it."));
174                                 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
175                         }
176                 }
177         }
178         
179         if ($cancel_delete == 0) 
180         {       //ie not cancelled the delete as a result of above tests
181                 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=$sel_id";
182                 db_query($sql,"cannot delete customer");
183
184                 display_notification(_("Selected customer has been deleted."));
185                 unset($_POST['customer_id']);
186                 $new_customer = true;
187                 $Ajax->activate('_page_body');
188         } //end if Delete Customer
189 }
190
191 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
192  
193 start_form();
194
195 if (db_has_customers()) 
196 {
197         start_table("class = 'tablestyle_noborder'");
198         start_row();
199         customer_list_cells(_("Select a customer: "), 'customer_id', null,
200                 _('New customer'), true, check_value('show_inactive'));
201         check_cells(_("Show inactive:"), 'show_inactive', null, true);
202         end_row();
203         end_table();
204         if (get_post('_show_inactive_update')) {
205                 $Ajax->activate('customer_id');
206                 set_focus('customer_id');
207         }
208
209 else 
210 {
211         hidden('customer_id');
212 }
213
214 if ($new_customer) 
215 {
216         $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id']  = '';
217         $_POST['dimension_id'] = 0;
218         $_POST['dimension2_id'] = 0;
219         $_POST['sales_type'] = -1;
220         $_POST['email'] = '';
221         $_POST['curr_code']  = get_company_currency();
222         $_POST['credit_status']  = -1;
223         $_POST['payment_terms']  = '';
224         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
225         $_POST['credit_limit']  = price_format($SysPrefs->default_credit_limit());
226         $_POST['inactive'] = 0;
227
228 else 
229 {
230
231         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($_POST['customer_id']);
232         $result = db_query($sql,"check failed");
233
234         $myrow = db_fetch($result);
235
236         $_POST['CustName'] = $myrow["name"];
237         $_POST['cust_ref'] = $myrow["debtor_ref"];
238         $_POST['address']  = $myrow["address"];
239         $_POST['tax_id']  = $myrow["tax_id"];
240         $_POST['email']  = $myrow["email"];
241         $_POST['dimension_id']  = $myrow["dimension_id"];
242         $_POST['dimension2_id']  = $myrow["dimension2_id"];
243         $_POST['sales_type'] = $myrow["sales_type"];
244         $_POST['curr_code']  = $myrow["curr_code"];
245         $_POST['credit_status']  = $myrow["credit_status"];
246         $_POST['payment_terms']  = $myrow["payment_terms"];
247         $_POST['discount']  = percent_format($myrow["discount"] * 100);
248         $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
249         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
250         $_POST['inactive'] = $myrow["inactive"];
251 }
252
253 start_outer_table($table_style2, 5);
254 table_section(1);
255 table_section_title(_("Name and Address"));
256
257 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
258 text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
259 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
260
261 email_row(_("E-mail:"), 'email', null, 40, 40);
262 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
263
264
265 if ($new_customer) 
266 {
267         currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
268
269 else 
270 {
271         label_row(_("Customer's Currency:"), $_POST['curr_code']);
272         hidden('curr_code', $_POST['curr_code']);                               
273 }       
274 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
275
276 table_section(2);
277
278 table_section_title(_("Sales"));
279
280 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
281 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
282 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
283
284 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
285 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
286 $dim = get_company_pref('use_dimension');
287 if ($dim >= 1)
288         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
289 if ($dim > 1)
290         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
291 if ($dim < 1)
292         hidden('dimension_id', 0);
293 if ($dim < 2)
294         hidden('dimension2_id', 0);
295
296 if (!$new_customer)  {
297         start_row();
298         echo '<td>'._('Customer branches').':</td>';
299         hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
300                 '<b>'. (@$_REQUEST['popup'] ?  _("Select or &Add") : _("&Add or Edit ")).'</b>', 
301                 "debtor_no=".$_POST['customer_id'].(@$_REQUEST['popup'] ? '&popup=1':''));
302         end_row();
303
304 }
305
306 record_status_list_row(_("Customer status:"), 'inactive');
307 end_outer_table(1);
308
309 div_start('controls');
310 if ($new_customer)
311 {
312         submit_center('submit', _("Add New Customer"), true, '', 'default');
313
314 else 
315 {
316         submit_center_first('submit', _("Update Customer"), 
317           _('Update customer data'), @$_REQUEST['popup'] ? true : 'default');
318         submit_return('select', get_post('customer_id'), _("Select this customer and return to document entry."));
319         submit_center_last('delete', _("Delete Customer"), 
320           _('Delete customer data if have been never used'), true);
321 }
322 div_end();
323 hidden('popup', @$_REQUEST['popup']);
324 end_form();
325 end_page();
326
327 ?>