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