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