Add/view on F4 in customer selector.
[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'])); // return to sales document
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                 display_notification(_("Selected customer has been deleted."));
169                 unset($_POST['customer_id']);
170                 $new_customer = true;
171                 $Ajax->activate('_page_body');
172         } //end if Delete Customer
173 }
174
175 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
176  
177 start_form();
178
179 if (db_has_customers()) 
180 {
181         start_table("class = 'tablestyle_noborder'");
182         customer_list_row(_("Select a customer: "), 'customer_id', null,
183           _('New customer'), true);
184         end_table();
185
186 else 
187 {
188         hidden('customer_id');
189 }
190
191 start_table($table_style2, 7, 6);
192 echo "<tr valign=top><td>"; // outer table      
193
194
195 start_table("class='tablestyle_noborder'");     
196
197 if ($new_customer) 
198 {
199         $_POST['CustName'] = $_POST['address'] = $_POST['tax_id']  = '';
200         $_POST['dimension_id'] = 0;
201         $_POST['dimension2_id'] = 0;
202         $_POST['sales_type'] = -1;
203         $_POST['email'] = '';
204         $_POST['curr_code']  = get_company_currency();
205         $_POST['credit_status']  = -1;
206         $_POST['payment_terms']  = '';
207         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
208         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
209
210 else 
211 {
212
213         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no = '" . $_POST['customer_id'] . "'";
214         $result = db_query($sql,"check failed");
215
216         $myrow = db_fetch($result);
217
218         $_POST['CustName'] = $myrow["name"];
219         $_POST['address']  = $myrow["address"];
220         $_POST['tax_id']  = $myrow["tax_id"];
221         $_POST['email']  = $myrow["email"];
222         $_POST['dimension_id']  = $myrow["dimension_id"];
223         $_POST['dimension2_id']  = $myrow["dimension2_id"];
224         $_POST['sales_type'] = $myrow["sales_type"];
225         $_POST['curr_code']  = $myrow["curr_code"];
226         $_POST['credit_status']  = $myrow["credit_status"];
227         $_POST['payment_terms']  = $myrow["payment_terms"];
228         $_POST['discount']  = percent_format($myrow["discount"] * 100);
229         $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
230         $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
231 }
232
233 text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 40);
234 textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
235
236 text_row(_("Email:"), 'email', null, 40, 40);
237 text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
238
239
240 // Sherifoz 23.09.03 currency can't be changed if editing
241 if ($new_customer) 
242 {
243         currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
244
245 else 
246 {
247         label_row(_("Customer's Currency:"), $_POST['curr_code']);
248         hidden('curr_code', $_POST['curr_code']);                               
249 }       
250 end_table();
251
252 echo "</td><td class='tableseparator'>"; // outer table
253
254 start_table("class='tablestyle_noborder'");     
255
256 sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
257 $dim = get_company_pref('use_dimension');
258 if ($dim >= 1)
259         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
260 if ($dim > 1)
261         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
262 if ($dim < 1)
263         hidden('dimension_id', 0);
264 if ($dim < 2)
265         hidden('dimension2_id', 0);
266
267 percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
268 percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
269 amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
270
271 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
272 credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
273 if (!$new_customer)  {
274         start_row();
275         echo '<td>'._('Customer branches').':</td>';
276         hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",'<b>'. (count($_SESSION['Context']) ?  _("Select or Add") : _("Add or Edit")).'</b>', "debtor_no=".$_POST['customer_id']);
277         end_row();
278 }
279 end_table();
280
281 end_table(1); // outer table    
282 div_start('controls');
283 if ($new_customer)
284 {
285         submit_center('submit', _("Add New Customer"), true, '', true);
286
287 else 
288 {
289         submit_center_first('submit', _("Update Customer"), 
290           _('Update customer data'), true);
291         submit_return('select', _("Return"), _("Select this customer and return to document entry."), true);
292         submit_center_last('delete', _("Delete Customer"), 
293           _('Delete customer data if have been never used'), true);
294 }
295 div_end();
296 end_form();
297 end_page();
298
299 ?>