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