Ajax additions to sales and purchasing modules.
[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('supplier_id');
80                         $Ajax->activate('controls');
81                 }
82         }
83
84
85 elseif (isset($_POST['delete']) && $_POST['delete'] != "") 
86 {
87         //the link to delete a selected record was clicked instead of the submit button
88
89         $cancel_delete = 0;
90
91         // PREVENT DELETES IF DEPENDENT RECORDS IN 'supp_trans' , purch_orders
92
93         $sql= "SELECT COUNT(*) FROM ".TB_PREF."supp_trans WHERE supplier_id='" . $_POST['supplier_id'] . "'";
94         $result = db_query($sql,"check failed");
95         $myrow = db_fetch_row($result);
96         if ($myrow[0] > 0) 
97         {
98                 $cancel_delete = 1;
99                 display_error(_("Cannot delete this supplier because there are transactions that refer to this supplier."));
100
101         } 
102         else 
103         {
104                 $sql= "SELECT COUNT(*) FROM ".TB_PREF."purch_orders WHERE supplier_id='" . $_POST['supplier_id'] . "'";
105                 $result = db_query($sql,"check failed");
106                 $myrow = db_fetch_row($result);
107                 if ($myrow[0] > 0) 
108                 {
109                         $cancel_delete = 1;
110                         display_error(_("Cannot delete the supplier record because purchase orders have been created against this supplier."));
111                 }
112
113         }
114         if ($cancel_delete == 0) 
115         {
116                 $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id='" . $_POST['supplier_id']. "'";
117                 db_query($sql,"check failed");
118
119                 unset($_SESSION['supplier_id']);
120                 $new_supplier = true;
121                 $Ajax->activate('_page_body');
122         } //end if Delete supplier
123 }
124
125 start_form();
126
127 if (db_has_suppliers()) 
128 {
129         start_table("", 3);
130 //      start_table("class = 'tablestyle_noborder'");
131         supplier_list_row(_("Select a supplier: "), 'supplier_id', null,
132                   _('New supplier'), true);
133         end_table();
134
135 else 
136 {
137         hidden('supplier_id', $_POST['supplier_id']);
138 }
139
140 //start_table("class='tablestyle2'", 0, 3);
141 start_table("class='tablestyle'", 3);
142
143 table_section_title(_("Supplier"));
144
145 if (!$new_supplier) 
146 {
147         //SupplierID exists - either passed when calling the form or from the form itself
148         $myrow = get_supplier($_POST['supplier_id']);
149
150         $_POST['supp_name'] = $myrow["supp_name"];
151         $_POST['address']  = $myrow["address"];
152         $_POST['email']  = $myrow["email"];
153         $_POST['bank_account']  = $myrow["bank_account"];
154         $_POST['dimension_id']  = $myrow["dimension_id"];
155         $_POST['dimension2_id']  = $myrow["dimension2_id"];
156         $_POST['curr_code']  = $myrow["curr_code"];
157         $_POST['payment_terms']  = $myrow["payment_terms"];
158         $_POST['tax_group_id'] = $myrow["tax_group_id"];
159         $_POST['payable_account']  = $myrow["payable_account"];
160         $_POST['purchase_account']  = $myrow["purchase_account"];
161         $_POST['payment_discount_account'] = $myrow["payment_discount_account"];
162
163
164 else 
165 {
166         $_POST['supp_name'] = $_POST['address'] = $_POST['tax_group_id']  = '';
167         $_POST['dimension_id'] = 0;
168         $_POST['dimension2_id'] = 0;
169         $_POST['sales_type'] = -1;
170         $_POST['email'] = $_POST['bank_account'] = '';
171         $_POST['payment_terms']  = '';
172         $_POST['credit_limit']  = price_format(sys_prefs::default_credit_limit());
173
174         $company_record = get_company_prefs();
175         $_POST['curr_code']  = $company_record["curr_default"];
176         $_POST['payable_account'] = $company_record["creditors_act"];
177         $_POST['purchase_account'] = $company_record["default_cogs_act"];
178         $_POST['payment_discount_account'] = $company_record['pyt_discount_act'];
179 }
180
181 text_row(_("Supplier Name:"), 'supp_name', null, 42, 40);
182 textarea_row(_("Address:"), 'address', null, 35, 5);
183 text_row(_("Email:"), 'email', null, 42, 40);
184 text_row(_("Bank Account:"), 'bank_account', null, 42, 40);
185
186 // Sherifoz 23.09.03 currency can't be changed if editing
187 if (!$new_supplier) 
188 {
189         label_row(_("Supplier's Currency:"), $_POST['curr_code']);
190         hidden('curr_code', $_POST['curr_code']);
191
192 else 
193 {
194         currencies_list_row(_("Supplier's Currency:"), 'curr_code', null);
195 }
196
197 tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null);
198
199 payment_terms_list_row(_("Payment Terms:"), 'payment_terms', null);
200
201 table_section_title(_("Accounts"));
202
203 gl_all_accounts_list_row(_("Accounts Payable Account:"), 'payable_account', $_POST['payable_account']);
204
205 gl_all_accounts_list_row(_("Purchase Account:"), 'purchase_account', $_POST['purchase_account']);
206
207 gl_all_accounts_list_row(_("Purchase Discount Account:"), 'payment_discount_account', $_POST['payment_discount_account']);
208
209 $dim = get_company_pref('use_dimension');
210 if ($dim >= 1)
211 {
212         table_section_title(_("Dimension"));
213
214         dimensions_list_row(_("Dimension")." 1:", 'dimension_id', null, true, " ", false, 1);
215         if ($dim > 1)
216                 dimensions_list_row(_("Dimension")." 2:", 'dimension2_id', null, true, " ", false, 2);
217 }
218 if ($dim < 1)
219         hidden('dimension_id', 0);
220 if ($dim < 2)
221         hidden('dimension2_id', 0);
222
223 end_table(1);
224 div_start('controls');
225 if (!$new_supplier) 
226 {
227         submit_center_first('submit', _("Update Supplier"), 
228           _('Update supplier data'), true);
229         submit_center_last('delete', _("Delete Supplier"), 
230           _('Delete supplier data if have been never used'), true);
231 }
232 else 
233 {
234         submit_center('submit', _("Add New Supplier Details"), true, '', true);
235 }
236 div_end();
237 end_form();
238
239 end_page();
240
241 ?>