Cleanups in price/cost/reorder levels pages.
[fa-stable.git] / inventory / purchasing_data.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_PURCHASEPRICING';
13 if (!@$_GET['popup'])
14         $path_to_root = "..";
15 else    
16         $path_to_root = "../..";
17
18 include_once($path_to_root . "/includes/session.inc");
19 include_once($path_to_root . "/includes/date_functions.inc");
20 include_once($path_to_root . "/includes/ui.inc");
21 include_once($path_to_root . "/includes/manufacturing.inc");
22 include_once($path_to_root . "/includes/data_checks.inc");
23
24 page(_($help_context = "Supplier Purchasing Data"));
25
26 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
27 check_db_has_suppliers(_("There are no suppliers defined in the system."));
28
29 //----------------------------------------------------------------------------------------
30 simple_page_mode(true);
31 if (isset($_GET['stock_id']))
32 {
33         $_POST['stock_id'] = $_GET['stock_id'];
34 }
35
36 //--------------------------------------------------------------------------------------------------
37
38 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
39 {
40
41         $input_error = 0;
42         if ($_POST['stock_id'] == "" || !isset($_POST['stock_id']))
43         {
44         $input_error = 1;
45         display_error( _("There is no item selected."));
46                 set_focus('stock_id');
47         }
48         elseif (!check_num('price', 0))
49         {
50         $input_error = 1;
51         display_error( _("The price entered was not numeric."));
52         set_focus('price');
53         }
54         elseif (!check_num('conversion_factor'))
55         {
56         $input_error = 1;
57         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."));
58                 set_focus('conversion_factor');
59         }
60         elseif ($Mode == 'ADD_ITEM' && get_item_purchasing_data($_POST['supplier_id'], $_POST['stock_id']))
61         {
62         $input_error = 1;
63         display_error( _("The purchasing data for this supplier has already been added."));
64                 set_focus('supplier_id');
65         }
66         if ($input_error == 0)
67         {
68         if ($Mode == 'ADD_ITEM') 
69         {
70                         add_item_purchasing_data($_POST['supplier_id'], $_POST['stock_id'], input_num('price',0),
71                                 $_POST['suppliers_uom'], input_num('conversion_factor'), $_POST['supplier_description']);
72                 display_notification(_("This supplier purchasing data has been added."));
73         } 
74         else
75         {
76                 update_item_purchasing_data($selected_id, $_POST['stock_id'], input_num('price',0),
77                         $_POST['suppliers_uom'], input_num('conversion_factor'), $_POST['supplier_description']);
78                 display_notification(_("Supplier purchasing data has been updated."));
79         }
80                 $Mode = 'RESET';
81         }
82 }
83
84 //--------------------------------------------------------------------------------------------------
85
86 if ($Mode == 'Delete')
87 {
88         delete_item_purchasing_data($selected_id, $_POST['stock_id']);
89         display_notification(_("The purchasing data item has been sucessfully deleted."));
90         $Mode = 'RESET';
91 }
92
93 if ($Mode == 'RESET')
94 {
95         $selected_id = -1;
96 }
97
98 if (isset($_POST['_selected_id_update']) )
99 {
100         $selected_id = $_POST['selected_id'];
101         $Ajax->activate('_page_body');
102 }
103
104 if (list_updated('stock_id')) 
105         $Ajax->activate('price_table');
106 //--------------------------------------------------------------------------------------------------
107
108 $action = $_SERVER['PHP_SELF'];
109 if ($page_nested)
110         $action .= "?stock_id=".get_post('stock_id');
111 start_form(false, false, $action);
112
113 if (!isset($_POST['stock_id']))
114         $_POST['stock_id'] = get_global_stock_item();
115
116 if (!$page_nested)
117 {
118         echo "<center>" . _("Item:"). "&nbsp;";
119         //Chaitanya : All items can be purchased
120         echo stock_items_list('stock_id', $_POST['stock_id'], false, true);
121         //echo stock_purchasable_items_list('stock_id', $_POST['stock_id'], false, true);
122         echo "<hr></center>";
123 }
124 else
125         br(2);
126
127 set_global_stock_item($_POST['stock_id']);
128
129 $mb_flag = get_mb_flag($_POST['stock_id']);
130
131 if ($mb_flag == -1)
132 {
133         display_error(_("Entered item is not defined. Please re-enter."));
134         set_focus('stock_id');
135 }
136 else
137 {
138         $result = get_items_purchasing_data($_POST['stock_id']);
139         div_start('price_table');
140     if (db_num_rows($result) == 0)
141     {
142         display_note(_("There is no purchasing data set up for the part selected"));
143     }
144     else
145     {
146         start_table(TABLESTYLE, "width=65%");
147
148                 $th = array(_("Supplier"), _("Price"), _("Currency"),
149                         _("Supplier's Unit"), _("Conversion Factor"), _("Supplier's Description"), "", "");
150
151         table_header($th);
152
153         $k = $j = 0; //row colour counter
154
155         while ($myrow = db_fetch($result))
156         {
157                         alt_table_row_color($k);
158
159             label_cell($myrow["supp_name"]);
160             amount_decimal_cell($myrow["price"]);
161             label_cell($myrow["curr_code"]);
162             label_cell($myrow["suppliers_uom"]);
163             qty_cell($myrow['conversion_factor'], false, 'max');
164             label_cell($myrow["supplier_description"]);
165                         edit_button_cell("Edit".$myrow['supplier_id'], _("Edit"));
166                         delete_button_cell("Delete".$myrow['supplier_id'], _("Delete"));
167             end_row();
168
169             $j++;
170             If ($j == 12)
171             {
172                 $j = 1;
173                         table_header($th);
174             } //end of page full new headings
175         } //end of while loop
176
177         end_table();
178     }
179  div_end();
180 }
181
182 //-----------------------------------------------------------------------------------------------
183
184 $dec2 = 6;
185 if ($Mode =='Edit')
186 {
187         $myrow = get_item_purchasing_data($selected_id, $_POST['stock_id']);
188
189     $supp_name = $myrow["supp_name"];
190     $_POST['price'] = price_decimal_format($myrow["price"], $dec2);
191     $_POST['suppliers_uom'] = $myrow["suppliers_uom"];
192     $_POST['supplier_description'] = $myrow["supplier_description"];
193     $_POST['conversion_factor'] = maxprec_format($myrow["conversion_factor"]);
194 }
195
196 br();
197 hidden('selected_id', $selected_id);
198 if ($page_nested)
199 {
200         hidden('popup', @$_GET['popup']);
201 }
202
203 start_table(TABLESTYLE2);
204
205 if ($Mode == 'Edit')
206 {
207         hidden('supplier_id');
208         label_row(_("Supplier:"), $supp_name);
209 }
210 else
211 {
212         supplier_list_row(_("Supplier:"), 'supplier_id', null, false, true);
213         $_POST['price'] = $_POST['suppliers_uom'] = $_POST['conversion_factor'] = $_POST['supplier_description'] = "";
214 }
215 amount_row(_("Price:"), 'price', null,'', get_supplier_currency($selected_id), $dec2);
216 text_row(_("Suppliers Unit of Measure:"), 'suppliers_uom', null, 50, 51);
217
218 if (!isset($_POST['conversion_factor']) || $_POST['conversion_factor'] == "")
219 {
220         $_POST['conversion_factor'] = maxprec_format(1);
221 }
222 amount_row(_("Conversion Factor (to our UOM):"), 'conversion_factor',
223   maxprec_format($_POST['conversion_factor']), null, null, 'max');
224 text_row(_("Supplier's Code or Description:"), 'supplier_description', null, 50, 51);
225
226 end_table(1);
227
228 submit_add_or_update_center($selected_id == -1, '', 'both');
229
230 end_form();
231 end_page();
232 ?>