Moved all SQL statements from PHP files into relevant *_db.inc files.
[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(_($help_context = "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                 update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
81                         $_POST['tax_id'], $_POST['curr_code'], $_POST['email'], $_POST['dimension_id'], $_POST['dimension2_id'],
82                         $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
83                         input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
84
85                 update_record_status($_POST['customer_id'], $_POST['inactive'],
86                         'debtors_master', 'debtor_no');
87
88                 $Ajax->activate('customer_id'); // in case of status change
89                 display_notification(_("Customer has been updated."));
90         } 
91         else 
92         {       //it is a new customer
93
94                 begin_transaction();
95                 add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
96                         $_POST['tax_id'], $_POST['curr_code'], $_POST['email'], $_POST['dimension_id'], $_POST['dimension2_id'],
97                         $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
98                         input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
99
100                 $_POST['customer_id'] = db_insert_id();
101                 $new_customer = false;
102                 commit_transaction();                   
103
104                 display_notification(_("A new customer has been added."));
105
106                 $Ajax->activate('_page_body');
107         }
108 }
109 //--------------------------------------------------------------------------------------------
110
111 if (isset($_POST['submit'])) 
112 {
113         handle_submit();
114 }
115 //-------------------------------------------------------------------------------------------- 
116
117 if (isset($_POST['delete'])) 
118 {
119
120         //the link to delete a selected record was clicked instead of the submit button
121
122         $cancel_delete = 0;
123
124         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
125         $sel_id = db_escape($_POST['customer_id']);
126
127         if (key_in_foreign_table($sel_id, 'debtor_trans', 'debtor_no', true))
128         {
129                 $cancel_delete = 1;
130                 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
131         } 
132         else 
133         {
134                 if (key_in_foreign_table($sel_id, 'sales_orders', 'debtor_no', true))
135                 {
136                         $cancel_delete = 1;
137                         display_error(_("Cannot delete the customer record because orders have been created against it."));
138                 } 
139                 else 
140                 {
141                         if (key_in_foreign_table($sel_id, 'cust_branch', 'debtor_no', true))
142                         {
143                                 $cancel_delete = 1;
144                                 display_error(_("Cannot delete this customer because there are branch records set up against it."));
145                                 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
146                         }
147                 }
148         }
149         
150         if ($cancel_delete == 0) 
151         {       //ie not cancelled the delete as a result of above tests
152         
153                 delete_customer($sel_id, true);
154
155                 display_notification(_("Selected customer has been deleted."));
156                 unset($_POST['customer_id']);
157                 $new_customer = true;
158                 $Ajax->activate('_page_body');
159         } //end if Delete Customer
160 }
161
162 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
163  
164 start_form();
165
166 if (db_has_customers()) 
167 {
168         start_table("class = 'tablestyle_noborder'");
169         start_row();
170         customer_list_cells(_("Select a customer: "), 'customer_id', null,
171                 _('New customer'), true, check_value('show_inactive'));
172         check_cells(_("Show inactive:"), 'show_inactive', null, true);
173         end_row();
174         end_table();
175         if (get_post('_show_inactive_update')) {
176                 $Ajax->activate('customer_id');
177                 set_focus('customer_id');
178         }
179
180 else 
181 {
182         hidden('customer_id');
183 }
184
185 if ($new_customer) 
186 {
187         $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id']  = '';
188         $_POST['dimension_id'] = 0;
189         $_POST['dimension2_id'] = 0;
190         $_POST['sales_type'] = -1;
191         $_POST['email'] = '';
192         $_POST['curr_code']  = get_company_currency();
193         $_POST['credit_status']  = -1;
194         $_POST['payment_terms']  = $_POST['notes']  = '';
195
196         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
197         $_POST['credit_limit']  = price_format($SysPrefs->default_credit_limit());
198         $_POST['inactive'] = 0;
199
200 else 
201 {
202         $myrow = get_customer($_POST['customer_id']);
203
204         $_POST['CustName'] = $myrow["name"];
205         $_POST['cust_ref'] = $myrow["debtor_ref"];
206         $_POST['address']  = $myrow["address"];
207         $_POST['tax_id']  = $myrow["tax_id"];
208         $_POST['email']  = $myrow["email"];
209         $_POST['dimension_id']  = $myrow["dimension_id"];
210         $_POST['dimension2_id']  = $myrow["dimension2_id"];
211         $_POST['sales_type'] = $myrow["sales_type"];
212         $_POST['curr_code']  = $myrow["curr_code"];
213         $_POST['credit_status']  = $myrow["credit_status"];
214         $_POST['payment_terms']  = $myrow["payment_terms"];
215         $_POST['discount']  = percent_format($myrow["discount"] * 100);
216         $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
217         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
218         $_POST['notes']  = $myrow["notes"];
219         $_POST['inactive'] = $myrow["inactive"];
220 }
221
222 start_outer_table($table_style2, 5);
223 table_section(1);
224 table_section_title(_("Name and Address"));
225
226 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
227 text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
228 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
229
230 email_row(_("E-mail:"), 'email', null, 40, 40);
231 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
232
233
234 if ($new_customer) 
235 {
236         currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
237
238 else 
239 {
240         label_row(_("Customer's Currency:"), $_POST['curr_code']);
241         hidden('curr_code', $_POST['curr_code']);                               
242 }       
243 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
244
245 table_section(2);
246
247 table_section_title(_("Sales"));
248
249 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
250 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
251 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
252
253 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
254 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
255 $dim = get_company_pref('use_dimension');
256 if ($dim >= 1)
257         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
258 if ($dim > 1)
259         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
260 if ($dim < 1)
261         hidden('dimension_id', 0);
262 if ($dim < 2)
263         hidden('dimension2_id', 0);
264
265 if (!$new_customer)  {
266         start_row();
267         echo '<td>'._('Customer branches').':</td>';
268         hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
269                 '<b>'. (@$_REQUEST['popup'] ?  _("Select or &Add") : _("&Add or Edit ")).'</b>', 
270                 "debtor_no=".$_POST['customer_id'].(@$_REQUEST['popup'] ? '&popup=1':''));
271         end_row();
272
273 }
274
275 textarea_row(_("General Notes:"), 'notes', null, 35, 5);
276 record_status_list_row(_("Customer status:"), 'inactive');
277 end_outer_table(1);
278
279 div_start('controls');
280 if ($new_customer)
281 {
282         submit_center('submit', _("Add New Customer"), true, '', 'default');
283
284 else 
285 {
286         submit_center_first('submit', _("Update Customer"), 
287           _('Update customer data'), @$_REQUEST['popup'] ? true : 'default');
288         submit_return('select', get_post('customer_id'), _("Select this customer and return to document entry."));
289         submit_center_last('delete', _("Delete Customer"), 
290           _('Delete customer data if have been never used'), true);
291 }
292 div_end();
293 hidden('popup', @$_REQUEST['popup']);
294 end_form();
295 end_page();
296
297 ?>