Merged changes from stable branch up to 2.3.23.
[fa-stable.git] / purchasing / manage / suppliers.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_SUPPLIER';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16 $js = "";
17 if ($SysPrefs->use_popup_windows)
18         $js .= get_js_open_window(900, 500);
19 if (user_use_date_picker())
20         $js .= get_js_date_picker();
21
22 page(_($help_context = "Suppliers"), @$_REQUEST['popup'], false, "", $js);
23
24 include_once($path_to_root . "/includes/ui.inc");
25 include_once($path_to_root . "/includes/ui/contacts_view.inc");
26
27 check_db_has_tax_groups(_("There are no tax groups defined in the system. At least one tax group is required before proceeding."));
28
29 if (isset($_GET['supplier_id'])) 
30 {
31         $_POST['supplier_id'] = $_GET['supplier_id'];
32 }
33
34 $supplier_id = get_post('supplier_id'); 
35 //--------------------------------------------------------------------------------------------
36 function supplier_settings(&$supplier_id)
37 {
38
39         start_outer_table(TABLESTYLE2);
40
41         table_section(1);
42
43         if ($supplier_id) 
44         {
45                 //SupplierID exists - either passed when calling the form or from the form itself
46                 $myrow = get_supplier($_POST['supplier_id']);
47
48                 $_POST['supp_name'] = $myrow["supp_name"];
49                 $_POST['supp_ref'] = $myrow["supp_ref"];
50                 $_POST['address']  = $myrow["address"];
51                 $_POST['supp_address']  = $myrow["supp_address"];
52
53                 $_POST['gst_no']  = $myrow["gst_no"];
54                 $_POST['website']  = $myrow["website"];
55                 $_POST['supp_account_no']  = $myrow["supp_account_no"];
56                 $_POST['bank_account']  = $myrow["bank_account"];
57                 $_POST['dimension_id']  = $myrow["dimension_id"];
58                 $_POST['dimension2_id']  = $myrow["dimension2_id"];
59                 $_POST['curr_code']  = $myrow["curr_code"];
60                 $_POST['payment_terms']  = $myrow["payment_terms"];
61                 $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
62                 $_POST['tax_group_id'] = $myrow["tax_group_id"];
63                 $_POST['tax_included'] = $myrow["tax_included"];
64                 $_POST['tax_algorithm'] = $myrow["tax_algorithm"];
65                 $_POST['payable_account']  = $myrow["payable_account"];
66                 $_POST['purchase_account']  = $myrow["purchase_account"];
67                 $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
68                 $_POST['notes']  = $myrow["notes"];
69                 $_POST['inactive'] = $myrow["inactive"];
70         } 
71         else 
72         {
73                 $_POST['supp_name'] = $_POST['supp_ref'] = $_POST['address'] = $_POST['supp_address'] = 
74                         $_POST['tax_group_id'] = $_POST['website'] = $_POST['supp_account_no'] = $_POST['notes'] = '';
75                 $_POST['dimension_id'] = 0;
76                 $_POST['dimension2_id'] = 0;
77                 $_POST['tax_included'] = 0;
78                 $_POST['tax_algorithm'] = 1;
79                 $_POST['sales_type'] = -1;
80                 $_POST['gst_no'] = $_POST['bank_account'] = '';
81                 $_POST['payment_terms']  = '';
82                 $_POST['credit_limit'] = price_format(0);
83
84                 $company_record = get_company_prefs();
85                 $_POST['curr_code']  = $company_record["curr_default"];
86                 $_POST['payable_account'] = $company_record["creditors_act"];
87                 $_POST['purchase_account'] = ''; // default/item's cogs account
88                 $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
89         }
90
91         table_section_title(_("Basic Data"));
92
93         text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
94         text_row(_("Supplier Short Name:"), 'supp_ref', null, 30, 30);
95
96         text_row(_("GSTNo:"), 'gst_no', null, 42, 40);
97         link_row(_("Website:"), 'website', null, 35, 55);
98         if ($supplier_id && !is_new_supplier($supplier_id) && (key_in_foreign_table($_POST['supplier_id'], 'supp_trans', 'supplier_id') ||
99                 key_in_foreign_table($_POST['supplier_id'], 'purch_orders', 'supplier_id'))) 
100         {
101                 label_row(_("Supplier's Currency:"), $_POST['curr_code']);
102                 hidden('curr_code', $_POST['curr_code']);
103         } 
104         else 
105         {
106                 currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
107         }
108         tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
109         text_row(_("Our Customer No:"), 'supp_account_no', null, 42, 40);
110
111         table_section_title(_("Purchasing"));
112         text_row(_("Bank Name/Account:"), 'bank_account', null, 42, 40);
113         amount_row(_("Credit Limit:"), 'credit_limit', null);
114         payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
115         //
116         // tax_included option from supplier record is used directly in update_average_cost() function,
117         // therefore we can't edit the option after any transaction was done for the supplier.
118         //
119         if (is_new_supplier($supplier_id))
120                 check_row(_("Prices contain tax included:"), 'tax_included');
121         else {
122                 hidden('tax_included');
123                 label_row(_("Prices contain tax included:"), $_POST['tax_included'] ? _('Yes') : _('No'));
124         }
125         tax_algorithm_list_row(_("Tax Calculation Algorithm:"), 'tax_algorithm', null);
126
127         if (!$supplier_id) table_section(2);
128
129         table_section_title(_("Accounts"));
130         gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
131         gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account'],
132                 false, false, _("Use Item Inventory/COGS Account"));
133         gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
134         if (!$supplier_id) {
135                 table_section_title(_("Contact Data"));
136                 text_row(_("Contact Person:"), 'contact', null, 42, 40);
137                 text_row(_("Phone Number:"), 'phone', null, 32, 30);
138                 text_row(_("Secondary Phone Number:"), 'phone2', null, 32, 30);
139                 table_section_title(_("Contact Data"));
140                 text_row(_("Fax Number:"), 'fax', null, 32, 30);
141                 email_row(_("E-mail:"), 'email', null, 35, 55);
142                 languages_list_row(_("Document Language:"), 'rep_lang', null, _('System default'));
143         }
144         else
145                 table_section(2);
146         $dim = get_company_pref('use_dimension');
147         if ($dim >= 1)
148         {
149                 table_section_title(_("Dimension"));
150                 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
151                 if ($dim > 1)
152                         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
153         }
154         if ($dim < 1)
155                 hidden('dimension_id', 0);
156         if ($dim < 2)
157                 hidden('dimension2_id', 0);
158         if (!$supplier_id)      
159                 table_section(2);
160
161         table_section_title(_("Addresses"));
162         textarea_row(_("Mailing Address:"), 'address', null, 35, 5);
163         textarea_row(_("Physical Address:"), 'supp_address', null, 35, 5);
164
165         table_section_title(_("General"));
166         textarea_row(_("General Notes:"), 'notes', null, 35, 5);
167         if ($supplier_id)
168                 record_status_list_row(_("Supplier status:"), 'inactive');
169         end_outer_table(1);
170
171         div_start('controls');
172         if ($supplier_id) 
173         {
174                 submit_center_first('submit', _("Update Supplier"), 
175                   _('Update supplier data'), @$_REQUEST['popup'] ? true : 'default');
176                 submit_return('select', get_post('supplier_id'), _("Select this supplier and return to document entry."));
177                 submit_center_last('delete', _("Delete Supplier"), 
178                   _('Delete supplier data if have been never used'), true);
179         }
180         else 
181         {
182                 submit_center('submit', _("Add New Supplier Details"), true, '', 'default');
183         }
184         div_end();
185 }
186
187 if (isset($_POST['submit'])) 
188 {
189
190         //initialise no input errors assumed initially before we test
191         $input_error = 0;
192
193         /* actions to take once the user has clicked the submit button
194         ie the page has called itself with some user input */
195
196         //first off validate inputs sensible
197
198         if (strlen($_POST['supp_name']) == 0 || $_POST['supp_name'] == "") 
199         {
200                 $input_error = 1;
201                 display_error(_("The supplier name must be entered."));
202                 set_focus('supp_name');
203         }
204
205         if (strlen($_POST['supp_ref']) == 0 || $_POST['supp_ref'] == "") 
206         {
207                 $input_error = 1;
208                 display_error(_("The supplier short name must be entered."));
209                 set_focus('supp_ref');
210         }
211
212         if ($input_error !=1 )
213         {
214
215                 begin_transaction();
216                 if ($supplier_id) 
217                 {
218                         update_supplier($_POST['supplier_id'], $_POST['supp_name'], $_POST['supp_ref'], $_POST['address'],
219                                 $_POST['supp_address'], $_POST['gst_no'],
220                                 $_POST['website'], $_POST['supp_account_no'], $_POST['bank_account'], 
221                                 input_num('credit_limit', 0), $_POST['dimension_id'], $_POST['dimension2_id'], $_POST['curr_code'],
222                                 $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'], $_POST['payment_discount_account'],
223                                 $_POST['notes'], $_POST['tax_group_id'], get_post('tax_included', 0), get_post('tax_algorithm'));
224                         update_record_status($_POST['supplier_id'], $_POST['inactive'],
225                                 'suppliers', 'supplier_id');
226
227                         $Ajax->activate('supplier_id'); // in case of status change
228                         display_notification(_("Supplier has been updated."));
229                 } 
230                 else 
231                 {
232                         add_supplier($_POST['supp_name'], $_POST['supp_ref'], $_POST['address'], $_POST['supp_address'],
233                                 $_POST['gst_no'], $_POST['website'], $_POST['supp_account_no'], $_POST['bank_account'], 
234                                 input_num('credit_limit',0), $_POST['dimension_id'], $_POST['dimension2_id'],
235                                 $_POST['curr_code'], $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'],
236                                 $_POST['payment_discount_account'], $_POST['notes'], $_POST['tax_group_id'], check_value('tax_included'), get_post('tax_algorithm'));
237
238                         $supplier_id = $_POST['supplier_id'] = db_insert_id();
239
240                         add_crm_person($_POST['supp_ref'], $_POST['contact'], '', $_POST['address'], 
241                                 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], 
242                                 $_POST['rep_lang'], '');
243
244                         add_crm_contact('supplier', 'general', $supplier_id, db_insert_id());
245
246                         display_notification(_("A new supplier has been added."));
247                         $Ajax->activate('_page_body');
248                 }
249                 commit_transaction();
250         }
251
252
253 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
254 {
255         //the link to delete a selected record was clicked instead of the submit button
256
257         $cancel_delete = 0;
258
259         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
260
261         if (key_in_foreign_table($_POST['supplier_id'], 'supp_trans', 'supplier_id'))
262         {
263                 $cancel_delete = 1;
264                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
265
266         } 
267         else 
268         {
269                 if (key_in_foreign_table($_POST['supplier_id'], 'purch_orders', 'supplier_id'))
270                 {
271                         $cancel_delete = 1;
272                         display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
273                 }
274
275         }
276         if ($cancel_delete == 0) 
277         {
278                 delete_supplier($_POST['supplier_id']);
279
280                 unset($_SESSION['supplier_id']);
281                 $supplier_id = '';
282                 $Ajax->activate('_page_body');
283         } //end if Delete supplier
284 }
285
286 start_form();
287
288 if (db_has_suppliers()) 
289 {
290         start_table(false, "", 3);
291 //      start_table(TABLESTYLE_NOBORDER);
292         start_row();
293         supplier_list_cells(_("Select a supplier: "), 'supplier_id', null,
294                   _('New supplier'), true, check_value('show_inactive'));
295         check_cells(_("Show inactive:"), 'show_inactive', null, true);
296         end_row();
297         end_table();
298         if (get_post('_show_inactive_update')) {
299                 $Ajax->activate('supplier_id');
300                 set_focus('supplier_id');
301         }
302
303 else 
304 {
305         hidden('supplier_id', get_post('supplier_id'));
306 }
307
308 if (!$supplier_id)
309         unset($_POST['_tabs_sel']); // force settings tab for new customer
310
311 tabbed_content_start('tabs', array(
312                 'settings' => array(_('&General settings'), $supplier_id),
313                 'contacts' => array(_('&Contacts'), $supplier_id),
314                 'transactions' => array(_('&Transactions'), $supplier_id),
315                 'orders' => array(_('Purchase &Orders'), $supplier_id),
316         ));
317         
318         switch (get_post('_tabs_sel')) {
319                 default:
320                 case 'settings':
321                         supplier_settings($supplier_id); 
322                         break;
323                 case 'contacts':
324                         $contacts = new contacts('contacts', $supplier_id, 'supplier');
325                         $contacts->show();
326                         break;
327                 case 'transactions':
328                         $_GET['supplier_id'] = $supplier_id;
329                         $_GET['popup'] = 1;
330                         include_once($path_to_root."/purchasing/inquiry/supplier_inquiry.php");
331                         break;
332                 case 'orders':
333                         $_GET['supplier_id'] = $supplier_id;
334                         $_GET['popup'] = 1;
335                         include_once($path_to_root."/purchasing/inquiry/po_search_completed.php");
336                         break;
337         };
338 br();
339 tabbed_content_end();
340 hidden('popup', @$_REQUEST['popup']);
341 end_form();
342
343 end_page(@$_REQUEST['popup']);
344