Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / purchasing / manage / suppliers.php
1 <?php
2
3 $page_security=5;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Suppliers"));
8
9 //include($path_to_root . "/includes/date_functions.inc");
10
11 include($path_to_root . "/includes/ui.inc");
12
13 check_db_has_tax_groups(_("There are no tax groups defined in the system. At least one tax group is required before proceeding."));
14
15 if (isset($_GET['supplier_id'])) 
16 {
17         $_POST['supplier_id'] = $_GET['supplier_id'];
18 }
19 $new_supplier = get_post('supplier_id') == ''; 
20
21 if (isset($_POST['submit'])) 
22 {
23
24         //initialise no input errors assumed initially before we test
25         $input_error = 0;
26
27         /* actions to take once the user has clicked the submit button
28         ie the page has called itself with some user input */
29
30         //first off validate inputs sensible
31
32         if (strlen($_POST['supp_name']) == 0 || $_POST['supp_name'] == "") 
33         {
34                 $input_error = 1;
35                 display_error(_("The supplier name must be entered."));
36                 set_focus('supp_name');
37         }
38
39         if ($input_error !=1 )
40         {
41
42                 if (!$new_supplier) 
43                 {
44
45                         $sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($_POST['supp_name']) . ",
46                 address=".db_escape($_POST['address']) . ",
47                 email=".db_escape($_POST['email']) . ",
48                 bank_account=".db_escape($_POST['bank_account']) . ",
49                 dimension_id=".db_escape($_POST['dimension_id']) . ",
50                 dimension2_id=".db_escape($_POST['dimension2_id']) . ",
51                 curr_code=".db_escape($_POST['curr_code']).",
52                 payment_terms=".db_escape($_POST['payment_terms']) . ",
53                                 payable_account=".db_escape($_POST['payable_account']) . ",
54                                 purchase_account=".db_escape($_POST['purchase_account']) . ",
55                                 payment_discount_account=".db_escape($_POST['payment_discount_account']) . ",
56                                 tax_group_id=".db_escape($_POST['tax_group_id']) . " WHERE supplier_id = '" . $_POST['supplier_id'] . "'";
57
58                         db_query($sql,"The supplier could not be updated");
59                         display_notification(_("Supplier has been updated."));
60                 } 
61                 else 
62                 {
63
64                         $sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, address, email, bank_account, dimension_id, dimension2_id, curr_code,
65                                 payment_terms, payable_account, purchase_account, payment_discount_account, tax_group_id)
66                                 VALUES (".db_escape($_POST['supp_name']). ", "
67                                 .db_escape($_POST['address']) . ", "
68                                 .db_escape($_POST['email']). ", "
69                                 .db_escape($_POST['bank_account']). ", "
70                                 .db_escape($_POST['dimension_id']). ", "
71                                 .db_escape($_POST['dimension2_id']). ", "
72                                 .db_escape($_POST['curr_code']). ", "
73                                 .db_escape($_POST['payment_terms']). ", "
74                                 .db_escape($_POST['payable_account']). ", "
75                                 .db_escape($_POST['purchase_account']). ", "
76                                 .db_escape($_POST['payment_discount_account']). ", "
77                                 .db_escape($_POST['tax_group_id']). ")";
78
79                         db_query($sql,"The supplier could not be added");
80                         $_POST['supplier_id'] = db_insert_id();
81                         $new_supplier = false;
82                         display_notification(_("A new supplier has been added."));
83                         $Ajax->activate('_page_body');
84                 }
85         }
86
87
88 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
89 {
90         //the link to delete a selected record was clicked instead of the submit button
91
92         $cancel_delete = 0;
93
94         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
95
96         $sql= "SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE supplier_id='" . $_POST['supplier_id'] . "'";
97         $result = db_query($sql,"check failed");
98         $myrow = db_fetch_row($result);
99         if ($myrow[0] > 0) 
100         {
101                 $cancel_delete = 1;
102                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
103
104         } 
105         else 
106         {
107                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE supplier_id='" . $_POST['supplier_id'] . "'";
108                 $result = db_query($sql,"check failed");
109                 $myrow = db_fetch_row($result);
110                 if ($myrow[0] > 0) 
111                 {
112                         $cancel_delete = 1;
113                         display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
114                 }
115
116         }
117         if ($cancel_delete == 0) 
118         {
119                 $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id='" . $_POST['supplier_id']. "'";
120                 db_query($sql,"check failed");
121
122                 unset($_SESSION['supplier_id']);
123                 $new_supplier = true;
124                 $Ajax->activate('_page_body');
125         } //end if Delete supplier
126 }
127 elseif (isset($_POST['select']))
128 {
129         context_return(array('supplier_id' => $_POST['supplier_id']));
130 }
131
132 start_form();
133
134 if (db_has_suppliers()) 
135 {
136         start_table("", 3);
137 //      start_table("class = 'tablestyle_noborder'");
138         supplier_list_row(_("Select a supplier: "), 'supplier_id', null,
139                   _('New supplier'), true);
140         end_table();
141
142 else 
143 {
144         hidden('supplier_id', get_post('supplier_id'));
145 }
146
147 //start_table("class='tablestyle2'", 0, 3);
148 start_table("class='tablestyle'", 3);
149
150 table_section_title(_("Supplier"));
151
152 if (!$new_supplier) 
153 {
154         //SupplierID exists - either passed when calling the form or from the form itself
155         $myrow = get_supplier($_POST['supplier_id']);
156
157         $_POST['supp_name'] = $myrow["supp_name"];
158         $_POST['address']  = $myrow["address"];
159         $_POST['email']  = $myrow["email"];
160         $_POST['bank_account']  = $myrow["bank_account"];
161         $_POST['dimension_id']  = $myrow["dimension_id"];
162         $_POST['dimension2_id']  = $myrow["dimension2_id"];
163         $_POST['curr_code']  = $myrow["curr_code"];
164         $_POST['payment_terms']  = $myrow["payment_terms"];
165         $_POST['tax_group_id'] = $myrow["tax_group_id"];
166         $_POST['payable_account']  = $myrow["payable_account"];
167         $_POST['purchase_account']  = $myrow["purchase_account"];
168         $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
169
170
171 else 
172 {
173         $_POST['supp_name'] = $_POST['address'] = $_POST['tax_group_id']  = '';
174         $_POST['dimension_id'] = 0;
175         $_POST['dimension2_id'] = 0;
176         $_POST['sales_type'] = -1;
177         $_POST['email'] = $_POST['bank_account'] = '';
178         $_POST['payment_terms']  = '';
179         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
180
181         $company_record = get_company_prefs();
182         $_POST['curr_code']  = $company_record["curr_default"];
183         $_POST['payable_account'] = $company_record["creditors_act"];
184         $_POST['purchase_account'] = $company_record["default_cogs_act"];
185         $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
186 }
187
188 text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
189 textarea_row(_("Address:"), 'address', null, 35, 5);
190 text_row(_("Email:"), 'email', null, 42, 40);
191 text_row(_("Bank Account:"), 'bank_account', null, 42, 40);
192
193 // Sherifoz 23.09.03 currency can't be changed if editing
194 if (!$new_supplier) 
195 {
196         label_row(_("Supplier's Currency:"), $_POST['curr_code']);
197         hidden('curr_code', $_POST['curr_code']);
198
199 else 
200 {
201         currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
202 }
203
204 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
205
206 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
207
208 table_section_title(_("Accounts"));
209
210 gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
211
212 gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account']);
213
214 gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
215
216 $dim = get_company_pref('use_dimension');
217 if ($dim >= 1)
218 {
219         table_section_title(_("Dimension"));
220
221         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
222         if ($dim > 1)
223                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
224 }
225 if ($dim < 1)
226         hidden('dimension_id', 0);
227 if ($dim < 2)
228         hidden('dimension2_id', 0);
229
230 end_table(1);
231 div_start('controls');
232 if (!$new_supplier) 
233 {
234         submit_center_first('submit', _("Update Supplier"), 
235           _('Update supplier data'), true);
236         submit_return('select', _("Return"), _("Select this supplier and return to document entry."), true);
237         submit_center_last('delete', _("Delete Supplier"), 
238           _('Delete supplier data if have been never used'), true);
239 }
240 else 
241 {
242         submit_center('submit', _("Add New Supplier Details"), true, '', true);
243 }
244 div_end();
245 end_form();
246
247 end_page();
248
249 ?>