Added links to payments for invoices in customer/supplier allocation inquiry
[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 = 'SA_CUSTOMER';
13 $path_to_root = "../..";
14
15 include_once($path_to_root . "/includes/session.inc");
16 page(_($help_context = "Customers"), @$_REQUEST['popup']); 
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 include_once($path_to_root . "/includes/ui/contacts_view.inc");
22
23 if (isset($_GET['debtor_no'])) 
24 {
25         $_POST['customer_id'] = $_GET['debtor_no'];
26 }
27
28 $selected_id = get_post('customer_id','');
29 //--------------------------------------------------------------------------------------------
30
31 function can_process()
32 {
33         if (strlen($_POST['CustName']) == 0) 
34         {
35                 display_error(_("The customer name cannot be empty."));
36                 set_focus('CustName');
37                 return false;
38         } 
39
40         if (strlen($_POST['cust_ref']) == 0) 
41         {
42                 display_error(_("The customer short name cannot be empty."));
43                 set_focus('cust_ref');
44                 return false;
45         } 
46         
47         if (!check_num('credit_limit', 0))
48         {
49                 display_error(_("The credit limit must be numeric and not less than zero."));
50                 set_focus('credit_limit');
51                 return false;           
52         } 
53         
54         if (!check_num('pymt_discount', 0, 100)) 
55         {
56                 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
57                 set_focus('pymt_discount');
58                 return false;           
59         } 
60         
61         if (!check_num('discount', 0, 100)) 
62         {
63                 display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
64                 set_focus('discount');
65                 return false;           
66         } 
67
68         return true;
69 }
70
71 //--------------------------------------------------------------------------------------------
72
73 function handle_submit(&$selected_id)
74 {
75         global $path_to_root, $Ajax;
76
77         if (!can_process())
78                 return;
79                 
80         if ($selected_id) 
81         {
82                 update_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
83                         $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
84                         $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
85                         input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
86
87                 update_record_status($_POST['customer_id'], $_POST['inactive'],
88                         'debtors_master', 'debtor_no');
89
90                 $Ajax->activate('customer_id'); // in case of status change
91                 display_notification(_("Customer has been updated."));
92         } 
93         else 
94         {       //it is a new customer
95
96                 begin_transaction();
97                 add_customer($_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
98                         $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
99                         $_POST['credit_status'], $_POST['payment_terms'], input_num('discount') / 100, input_num('pymt_discount') / 100,
100                         input_num('credit_limit'), $_POST['sales_type'], $_POST['notes']);
101
102                 $selected_id = $_POST['customer_id'] = db_insert_id();
103                 commit_transaction();
104
105                 display_notification(_("A new customer has been added."));
106                 $Ajax->activate('_page_body');
107         }
108 }
109 //--------------------------------------------------------------------------------------------
110
111 if (isset($_POST['submit'])) 
112 {
113         handle_submit($selected_id);
114 }
115 //-------------------------------------------------------------------------------------------- 
116
117 if (isset($_POST['delete'])) 
118 {
119
120         $cancel_delete = 0;
121
122         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
123
124         if (key_in_foreign_table($selected_id, 'debtor_trans', 'debtor_no'))
125         {
126                 $cancel_delete = 1;
127                 display_error(_("This customer cannot be deleted because there are transactions that refer to it."));
128         } 
129         else 
130         {
131                 if (key_in_foreign_table($selected_id, 'sales_orders', 'debtor_no'))
132                 {
133                         $cancel_delete = 1;
134                         display_error(_("Cannot delete the customer record because orders have been created against it."));
135                 } 
136                 else 
137                 {
138                         if (key_in_foreign_table($selected_id, 'cust_branch', 'debtor_no'))
139                         {
140                                 $cancel_delete = 1;
141                                 display_error(_("Cannot delete this customer because there are branch records set up against it."));
142                                 //echo "<br> There are " . $myrow[0] . " branch records relating to this customer";
143                         }
144                 }
145         }
146         
147         if ($cancel_delete == 0) 
148         {       //ie not cancelled the delete as a result of above tests
149         
150                 delete_customer($selected_id);
151
152                 display_notification(_("Selected customer has been deleted."));
153                 unset($_POST['customer_id']);
154                 $selected_id = '';
155                 $Ajax->activate('_page_body');
156         } //end if Delete Customer
157 }
158
159 function customer_settings($selected_id) 
160 {
161         global $SysPrefs, $path_to_root;
162         
163         if (!$selected_id) 
164         {
165                 if (list_updated('customer_id') || !isset($_POST['CustName'])) {
166                         $_POST['CustName'] = $_POST['cust_ref'] = $_POST['address'] = $_POST['tax_id']  = '';
167                         $_POST['dimension_id'] = 0;
168                         $_POST['dimension2_id'] = 0;
169                         $_POST['sales_type'] = -1;
170                         $_POST['curr_code']  = get_company_currency();
171                         $_POST['credit_status']  = -1;
172                         $_POST['payment_terms']  = $_POST['notes']  = '';
173
174                         $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
175                         $_POST['credit_limit']  = price_format($SysPrefs->default_credit_limit());
176                 }
177         }
178         else 
179         {
180                 $myrow = get_customer($selected_id);
181
182                 $_POST['CustName'] = $myrow["name"];
183                 $_POST['cust_ref'] = $myrow["debtor_ref"];
184                 $_POST['address']  = $myrow["address"];
185                 $_POST['tax_id']  = $myrow["tax_id"];
186                 $_POST['dimension_id']  = $myrow["dimension_id"];
187                 $_POST['dimension2_id']  = $myrow["dimension2_id"];
188                 $_POST['sales_type'] = $myrow["sales_type"];
189                 $_POST['curr_code']  = $myrow["curr_code"];
190                 $_POST['credit_status']  = $myrow["credit_status"];
191                 $_POST['payment_terms']  = $myrow["payment_terms"];
192                 $_POST['discount']  = percent_format($myrow["discount"] * 100);
193                 $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
194                 $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
195                 $_POST['notes']  = $myrow["notes"];
196                 $_POST['inactive'] = $myrow["inactive"];
197         }
198
199         start_outer_table(TABLESTYLE2);
200         table_section(1);
201         table_section_title(_("Name and Address"));
202
203         text_row(_("Customer Name:"), 'CustName', $_POST['CustName'], 40, 80);
204         text_row(_("Customer Short Name:"), 'cust_ref', null, 30, 30);
205         textarea_row(_("Address:"), 'address', $_POST['address'], 35, 5);
206
207         text_row(_("GSTNo:"), 'tax_id', null, 40, 40);
208
209
210         if (!$selected_id || is_new_customer($selected_id)) 
211         {
212                 currencies_list_row(_("Customer's Currency:"), 'curr_code', $_POST['curr_code']);
213         } 
214         else 
215         {
216                 label_row(_("Customer's Currency:"), $_POST['curr_code']);
217                 hidden('curr_code', $_POST['curr_code']);                               
218         }
219         sales_types_list_row(_("Sales Type/Price List:"), 'sales_type', $_POST['sales_type']);
220
221         if($selected_id)
222                 record_status_list_row(_("Customer status:"), 'inactive');
223
224         table_section(2);
225
226         table_section_title(_("Sales"));
227
228         percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
229         percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
230         amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
231
232         payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
233         credit_status_list_row(_("Credit Status:"), 'credit_status', $_POST['credit_status']); 
234         $dim = get_company_pref('use_dimension');
235         if ($dim >= 1)
236                 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', $_POST['dimension_id'], true, " ", false, 1);
237         if ($dim > 1)
238                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', $_POST['dimension2_id'], true, " ", false, 2);
239         if ($dim < 1)
240                 hidden('dimension_id', 0);
241         if ($dim < 2)
242                 hidden('dimension2_id', 0);
243
244         if ($selected_id)  {
245                 start_row();
246                 echo '<td class="label">'._('Customer branches').':</td>';
247                 hyperlink_params_td($path_to_root . "/sales/manage/customer_branches.php",
248                         '<b>'. (@$_REQUEST['popup'] ?  _("Select or &Add") : _("&Add or Edit ")).'</b>', 
249                         "debtor_no=".$selected_id.(@$_REQUEST['popup'] ? '&popup=1':''));
250                 end_row();
251         }
252
253         textarea_row(_("General Notes:"), 'notes', null, 35, 5);
254         end_outer_table(1);
255
256         div_start('controls');
257         if (!$selected_id)
258         {
259                 submit_center('submit', _("Add New Customer"), true, '', 'default');
260         } 
261         else 
262         {
263                 submit_center_first('submit', _("Update Customer"), 
264                   _('Update customer data'), @$_REQUEST['popup'] ? true : 'default');
265                 submit_return('select', $selected_id, _("Select this customer and return to document entry."));
266                 submit_center_last('delete', _("Delete Customer"), 
267                   _('Delete customer data if have been never used'), true);
268         }
269         div_end();
270 }
271
272 //--------------------------------------------------------------------------------------------
273
274 check_db_has_sales_types(_("There are no sales types defined. Please define at least one sales type before adding a customer."));
275  
276 start_form();
277
278 if (db_has_customers()) 
279 {
280         start_table(TABLESTYLE_NOBORDER);
281         start_row();
282         customer_list_cells(_("Select a customer: "), 'customer_id', null,
283                 _('New customer'), true, check_value('show_inactive'));
284         check_cells(_("Show inactive:"), 'show_inactive', null, true);
285         end_row();
286         end_table();
287
288         if (get_post('_show_inactive_update')) {
289                 $Ajax->activate('customer_id');
290                 set_focus('customer_id');
291         }
292
293 else 
294 {
295         hidden('customer_id');
296 }
297
298 if (!$selected_id)
299         unset($_POST['_tabs_sel']); // force settings tab for new customer
300
301 tabbed_content_start('tabs', array(
302                 'settings' => array(_('&General settings'), $selected_id),
303                 'contacts' => array(_('&Contacts'), $selected_id),
304         ));
305         
306         switch (get_post('_tabs_sel')) {
307                 default:
308                 case 'settings':
309                         customer_settings($selected_id); 
310                         break;
311                 case 'contacts':
312                         $contacts = new contacts('contacts', $selected_id, 'customer');
313                         $contacts->show();
314                         break;
315                 case 'orders':
316         };
317 br();
318 tabbed_content_end();
319
320 hidden('popup', @$_REQUEST['popup']);
321 end_form();
322 end_page();
323
324 ?>