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