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