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