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