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