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