Multiply contacts spport added.
[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['payable_account']  = $myrow["payable_account"];
58                 $_POST['purchase_account']  = $myrow["purchase_account"];
59                 $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
60                 $_POST['notes']  = $myrow["notes"];
61                 $_POST['inactive'] = $myrow["inactive"];
62         } 
63         else 
64         {
65                 $_POST['supp_name'] = $_POST['supp_ref'] = $_POST['address'] = $_POST['supp_address'] = 
66                         $_POST['tax_group_id'] = $_POST['website'] = $_POST['supp_account_no'] = $_POST['notes'] = '';
67                 $_POST['dimension_id'] = 0;
68                 $_POST['dimension2_id'] = 0;
69                 $_POST['sales_type'] = -1;
70                 $_POST['gst_no'] = $_POST['bank_account'] = '';
71                 $_POST['payment_terms']  = '';
72                 $_POST['credit_limit'] = price_format(0);
73
74                 $company_record = get_company_prefs();
75                 $_POST['curr_code']  = $company_record["curr_default"];
76                 $_POST['payable_account'] = $company_record["creditors_act"];
77                 $_POST['purchase_account'] = $company_record["default_cogs_act"];
78                 $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
79         }
80
81         table_section_title(_("Basic Data"));
82
83         text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
84         text_row(_("Supplier Short Name:"), 'supp_ref', null, 30, 30);
85
86         text_row(_("GSTNo:"), 'gst_no', null, 42, 40);
87         link_row(_("Website:"), 'website', null, 35, 55);
88         if ($supplier_id) 
89         {
90                 label_row(_("Supplier's Currency:"), $_POST['curr_code']);
91                 hidden('curr_code', $_POST['curr_code']);
92         } 
93         else 
94         {
95                 currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
96         }
97         tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
98         text_row(_("Our Customer No:"), 'supp_account_no', null, 42, 40);
99
100         if (!$supplier_id) {
101                 table_section_title(_("Contact Data"));
102                 text_row(_("Contact Person:"), 'contact', null, 42, 40);
103                 text_row(_("Phone Number:"), 'phone', null, 32, 30);
104                 text_row(_("Secondary Phone Number:"), 'phone2', null, 32, 30);
105                 text_row(_("Fax Number:"), 'fax', null, 32, 30);
106                 email_row(_("E-mail:"), 'email', null, 35, 55);
107                 languages_list_row(_("Document Language:"), 'rep_lang', null, _('System default'));
108         }
109         table_section_title(_("Purchasing"));
110         text_row(_("Bank Name/Account:"), 'bank_account', null, 42, 40);
111         amount_row(_("Credit Limit:"), 'credit_limit', null);
112         payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
113
114         table_section_title(_("Accounts"));
115         gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
116         gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account']);
117         gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
118
119         table_section(2);
120         $dim = get_company_pref('use_dimension');
121         if ($dim >= 1)
122         {
123                 table_section_title(_("Dimension"));
124                 dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
125                 if ($dim > 1)
126                         dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
127         }
128         if ($dim < 1)
129                 hidden('dimension_id', 0);
130         if ($dim < 2)
131                 hidden('dimension2_id', 0);
132
133
134         table_section_title(_("Addresses"));
135         textarea_row(_("Mailing Address:"), 'address', null, 35, 5);
136         textarea_row(_("Physical Address:"), 'supp_address', null, 35, 5);
137
138         table_section_title(_("General"));
139         textarea_row(_("General Notes:"), 'notes', null, 35, 5);
140         if ($supplier_id)
141                 record_status_list_row(_("Supplier status:"), 'inactive');
142
143         end_outer_table(1);
144
145         div_start('controls');
146         if ($supplier_id) 
147         {
148                 submit_center_first('submit', _("Update Supplier"), 
149                   _('Update supplier data'), @$_REQUEST['popup'] ? true : 'default');
150                 submit_return('select', get_post('supplier_id'), _("Select this supplier and return to document entry."));
151                 submit_center_last('delete', _("Delete Supplier"), 
152                   _('Delete supplier data if have been never used'), true);
153         }
154         else 
155         {
156                 submit_center('submit', _("Add New Supplier Details"), true, '', 'default');
157         }
158         div_end();
159 }
160
161 if (isset($_POST['submit'])) 
162 {
163
164         //initialise no input errors assumed initially before we test
165         $input_error = 0;
166
167         /* actions to take once the user has clicked the submit button
168         ie the page has called itself with some user input */
169
170         //first off validate inputs sensible
171
172         if (strlen($_POST['supp_name']) == 0 || $_POST['supp_name'] == "") 
173         {
174                 $input_error = 1;
175                 display_error(_("The supplier name must be entered."));
176                 set_focus('supp_name');
177         }
178
179         if (strlen($_POST['supp_ref']) == 0 || $_POST['supp_ref'] == "") 
180         {
181                 $input_error = 1;
182                 display_error(_("The supplier short name must be entered."));
183                 set_focus('supp_ref');
184         }
185
186         if ($input_error !=1 )
187         {
188
189                 begin_transaction();
190                 if ($supplier_id) 
191                 {
192                         update_supplier($_POST['supplier_id'], $_POST['supp_name'], $_POST['supp_ref'], $_POST['address'],
193                                 $_POST['supp_address'], $_POST['gst_no'],
194                                 $_POST['website'], $_POST['supp_account_no'], $_POST['bank_account'], 
195                                 input_num('credit_limit', 0), $_POST['dimension_id'], $_POST['dimension2_id'], $_POST['curr_code'],
196                                 $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'], $_POST['payment_discount_account'],
197                                 $_POST['notes'], $_POST['tax_group_id']);
198                         update_record_status($_POST['supplier_id'], $_POST['inactive'],
199                                 'suppliers', 'supplier_id');
200
201                         $Ajax->activate('supplier_id'); // in case of status change
202                         display_notification(_("Supplier has been updated."));
203                 } 
204                 else 
205                 {
206                         add_supplier($_POST['supp_name'], $_POST['supp_ref'], $_POST['address'], $_POST['supp_address'],
207                                 $_POST['gst_no'], $_POST['website'], $_POST['supp_account_no'], $_POST['bank_account'], 
208                                 input_num('credit_limit',0), $_POST['dimension_id'], $_POST['dimension2_id'],
209                                 $_POST['curr_code'], $_POST['payment_terms'], $_POST['payable_account'], $_POST['purchase_account'],
210                                 $_POST['payment_discount_account'], $_POST['notes'], $_POST['tax_group_id']);
211
212                         $supplier_id = $_POST['supplier_id'] = db_insert_id();
213
214                         add_crm_person($_POST['supp_ref'], $_POST['contact'], '', $_POST['address'], 
215                                 $_POST['phone'], $_POST['phone2'], $_POST['fax'], $_POST['email'], 
216                                 $_POST['rep_lang'], '');
217
218                         add_crm_contact('supplier', 'general', $supplier_id, db_insert_id());
219
220                         display_notification(_("A new supplier has been added."));
221                         $Ajax->activate('_page_body');
222                 }
223                 commit_transaction();
224         }
225
226
227 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
228 {
229         //the link to delete a selected record was clicked instead of the submit button
230
231         $cancel_delete = 0;
232
233         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
234
235         if (key_in_foreign_table($_POST['supplier_id'], 'supp_trans', 'supplier_id'))
236         {
237                 $cancel_delete = 1;
238                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
239
240         } 
241         else 
242         {
243                 if (key_in_foreign_table($_POST['supplier_id'], 'purch_orders', 'supplier_id'))
244                 {
245                         $cancel_delete = 1;
246                         display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
247                 }
248
249         }
250         if ($cancel_delete == 0) 
251         {
252                 delete_supplier($_POST['supplier_id']);
253
254                 unset($_SESSION['supplier_id']);
255                 $supplier_id = '';
256                 $Ajax->activate('_page_body');
257         } //end if Delete supplier
258 }
259
260 start_form();
261
262 if (db_has_suppliers()) 
263 {
264         start_table(false, "", 3);
265 //      start_table(TABLESTYLE_NOBORDER);
266         start_row();
267         supplier_list_cells(_("Select a supplier: "), 'supplier_id', null,
268                   _('New supplier'), true, check_value('show_inactive'));
269         check_cells(_("Show inactive:"), 'show_inactive', null, true);
270         end_row();
271         end_table();
272         if (get_post('_show_inactive_update')) {
273                 $Ajax->activate('supplier_id');
274                 set_focus('supplier_id');
275         }
276
277 else 
278 {
279         hidden('supplier_id', get_post('supplier_id'));
280 }
281
282 if (!$supplier_id)
283         unset($_POST['_tabs_sel']); // force settings tab for new customer
284
285 tabbed_content_start('tabs', array(
286                 'settings' => array('&General settings', $supplier_id),
287                 'contacts' => array('&Contacts', $supplier_id),
288         ));
289         
290         switch (get_post('_tabs_sel')) {
291                 default:
292                 case 'settings':
293                         supplier_settings($supplier_id); 
294                         break;
295                 case 'contacts':
296                         $contacts = new contacts('contacts', $supplier_id, 'supplier');
297                         $contacts->show();
298                         break;
299                 case 'orders':
300         };
301 br();
302 tabbed_content_end();
303 hidden('popup', @$_REQUEST['popup']);
304 end_form();
305
306 end_page();
307
308 ?>