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