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