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