Fixed page update after item search.
[fa-stable.git] / inventory / purchasing_data.php
1 <?php
2 $page_security = 4;
3 $path_to_root="..";
4 include_once($path_to_root . "/includes/session.inc");
5
6 page(_("Supplier Purchasing Data"));
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/ui.inc");
10 include_once($path_to_root . "/includes/manufacturing.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
12
13 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
14 check_db_has_suppliers(_("There are no suppliers defined in the system."));
15
16 simple_page_mode(true);
17 //--------------------------------------------------------------------------------------------------
18
19 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
20 {
21
22         $input_error = 0;
23         if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
24         {
25         $input_error = 1;
26         display_error( _("There is no item selected."));
27         set_focus('stock_id');
28         }
29         elseif (!check_num('price', 0))
30         {
31         $input_error = 1;
32         display_error( _("The price entered was not numeric."));
33         set_focus('price');
34         }
35         elseif (!check_num('conversion_factor'))
36         {
37         $input_error = 1;
38         display_error( _("The conversion factor entered was not numeric. The conversion factor is the number by which the price must be divided by to get the unit price in our unit of measure."));
39                 set_focus('conversion_factor');
40         }
41
42         if ($input_error == 0)
43         {
44         if ($Mode == 'ADD_ITEM') 
45         {
46
47                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
48                         conversion_factor, supplier_description) VALUES (";
49                 $sql .= "'".$_POST['supplier_id']."', '" . $_POST['stock_id'] . "', " .
50                     input_num('price') . ", '" . $_POST['suppliers_uom'] . "', " .
51                         input_num('conversion_factor') . ", '" . $_POST['supplier_description'] . "')";
52
53                 db_query($sql,"The supplier purchasing details could not be added");
54                 display_notification(_("This supplier purchasing data has been added."));
55         } else
56         {
57                 $sql = "UPDATE ".TB_PREF."purch_data SET price=" . input_num('price') . ",
58                                 suppliers_uom='" . $_POST['suppliers_uom'] . "',
59                                 conversion_factor=" . input_num('conversion_factor') . ",
60                                 supplier_description='" . $_POST['supplier_description'] . "'
61                                 WHERE stock_id='" . $_POST['stock_id'] . "' AND
62                                 supplier_id='$selected_id'";
63                 db_query($sql,"The supplier purchasing details could not be updated");
64
65                 display_notification(_("Supplier purchasing data has been updated."));
66         }
67                 $Mode = 'RESET';
68         }
69 }
70
71 //--------------------------------------------------------------------------------------------------
72
73 if ($Mode == 'Delete')
74 {
75
76         $sql = "DELETE FROM ".TB_PREF."purch_data WHERE supplier_id='$selected_id'
77                 AND stock_id='" . $_POST['stock_id'] . "'";
78         db_query($sql,"could not delete purchasing data");
79
80         display_notification(_("The purchasing data item has been sucessfully deleted."));
81         $Mode = 'RESET';
82 }
83
84 if ($Mode == 'RESET')
85 {
86         $selected_id = -1;
87 }
88
89 if (isset($_POST['_selected_id_update']) )
90 {
91         $selected_id = $_POST['selected_id'];
92         $Ajax->activate('_page_body');
93 }
94
95 if (list_updated('stock_id')) 
96         $Ajax->activate('price_table');
97 //--------------------------------------------------------------------------------------------------
98
99 start_form(false, true);
100
101 if (!isset($_POST['stock_id']))
102         $_POST['stock_id'] = get_global_stock_item();
103
104 echo "<center>" . _("Item:"). "&nbsp;";
105 stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
106
107 echo "<hr></center>";
108
109 set_global_stock_item($_POST['stock_id']);
110
111 $mb_flag = get_mb_flag($_POST['stock_id']);
112
113 if ($mb_flag == -1)
114 {
115         display_error(_("Entered item is not defined. Please re-enter."));
116         set_focus('stock_id');
117 }
118 else
119 {
120
121     $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name,".TB_PREF."suppliers.curr_code
122                 FROM ".TB_PREF."purch_data INNER JOIN ".TB_PREF."suppliers
123                 ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
124                 WHERE stock_id = '" . $_POST['stock_id'] . "'";
125
126     $result = db_query($sql, "The supplier purchasing details for the selected part could not be retrieved");
127   div_start('price_table');
128     if (db_num_rows($result) == 0)
129     {
130         display_note(_("There is no purchasing data set up for the part selected"));
131     }
132     else
133     {
134         start_table("$table_style width=60%");
135
136                 $th = array(_("Supplier"), _("Price"), _("Currency"),
137                         _("Supplier's Unit"), _("Supplier's Description"), "", "");
138
139         table_header($th);
140
141         $k = $j = 0; //row colour counter
142
143         while ($myrow = db_fetch($result))
144         {
145                         alt_table_row_color($k);
146
147             label_cell($myrow["supp_name"]);
148             amount_cell($myrow["price"]);
149             label_cell($myrow["curr_code"]);
150             label_cell($myrow["suppliers_uom"]);
151             label_cell($myrow["supplier_description"]);
152                         edit_button_cell("Edit".$myrow['supplier_id'], _("Edit"));
153                         edit_button_cell("Delete".$myrow['supplier_id'], _("Delete"));
154             end_row();
155
156             $j++;
157             If ($j == 12)
158             {
159                 $j = 1;
160                         table_header($th);
161             } //end of page full new headings
162         } //end of while loop
163
164         end_table();
165     }
166  div_end();
167 }
168
169 //-----------------------------------------------------------------------------------------------
170
171 if ($Mode =='Edit')
172 {
173
174         $sql = "SELECT ".TB_PREF."purch_data.*,".TB_PREF."suppliers.supp_name FROM ".TB_PREF."purch_data
175                 INNER JOIN ".TB_PREF."suppliers ON ".TB_PREF."purch_data.supplier_id=".TB_PREF."suppliers.supplier_id
176                 WHERE ".TB_PREF."purch_data.supplier_id='$selected_id'
177                 AND ".TB_PREF."purch_data.stock_id='" . $_POST['stock_id'] . "'";
178
179         $result = db_query($sql, "The supplier purchasing details for the selected supplier and item could not be retrieved");
180
181         $myrow = db_fetch($result);
182
183     $supp_name = $myrow["supp_name"];
184     $_POST['price'] = price_format($myrow["price"]);
185     $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
186     $_POST['supplier_description'] = $myrow["supplier_description"];
187     $_POST['conversion_factor'] = exrate_format($myrow["conversion_factor"]);
188 }
189
190 echo "<br>";
191 hidden('selected_id', $selected_id);
192 start_table($table_style2);
193
194 if ($Mode == 'Edit')
195 {
196         hidden('supplier_id');
197         label_row(_("Supplier:"), $supp_name);
198 }
199 else
200 {
201         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true);
202 }
203 amount_row(_("Price:"), 'price', null,'', get_supplier_currency($selected_id));
204 text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
205
206 if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
207 {
208         $_POST['conversion_factor'] = exrate_format(1);
209 }
210 amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor',
211   exrate_format($_POST['conversion_factor']), null, null, user_exrate_dec() );
212 text_row(_("Supplier's Code or Description:"), 'supplier_description', null, 50, 51);
213
214 end_table(1);
215
216 submit_add_or_update_center($selected_id == -1, '', true);
217
218 end_form();
219 end_page();
220
221 ?>