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