Bug 0001669: get_exchange_rate not raising error
[fa-stable.git] / inventory / prices.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_SALESPRICE';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Inventory Item Sales prices"));
17
18 include_once($path_to_root . "/sales/includes/sales_db.inc");
19 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
20 include_once($path_to_root . "/includes/ui.inc");
21 include_once($path_to_root . "/includes/data_checks.inc");
22
23 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
24
25 //---------------------------------------------------------------------------------------------------
26
27 check_db_has_stock_items(_("There are no items defined in the system."));
28
29 check_db_has_sales_types(_("There are no sales types in the system. Please set up sales types befor entering pricing."));
30
31 simple_page_mode(true);
32 //---------------------------------------------------------------------------------------------------
33 $input_error = 0;
34
35 if (isset($_GET['stock_id']))
36 {
37         $_POST['stock_id'] = $_GET['stock_id'];
38 }
39 if (isset($_GET['Item']))
40 {
41         $_POST['stock_id'] = $_GET['Item'];
42 }
43
44 if (!isset($_POST['curr_abrev']))
45 {
46         $_POST['curr_abrev'] = get_company_currency();
47 }
48
49 //---------------------------------------------------------------------------------------------------
50
51 start_form();
52
53 if (!isset($_POST['stock_id']))
54         $_POST['stock_id'] = get_global_stock_item();
55
56 echo "<center>" . _("Item:"). "&nbsp;";
57 echo sales_items_list('stock_id', $_POST['stock_id'], false, true, '', array('editable' => false));
58 echo "<hr></center>";
59
60 set_global_stock_item($_POST['stock_id']);
61
62 //----------------------------------------------------------------------------------------------------
63
64 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
65 {
66
67         if (!check_num('price', 0))
68         {
69                 $input_error = 1;
70                 display_error( _("The price entered must be numeric."));
71                 set_focus('price');
72         }
73         elseif(!db_has_currency_rates($_POST['curr_abrev'], Today(), true))
74                 $input_error = 1;
75
76         if ($input_error != 1)
77         {
78
79         if ($selected_id != -1) 
80                 {
81                         //editing an existing price
82                         update_item_price($selected_id, $_POST['sales_type_id'],
83                         $_POST['curr_abrev'], input_num('price'));
84
85                         $msg = _("This price has been updated.");
86                 }
87                 else
88                 {
89
90                         add_item_price($_POST['stock_id'], $_POST['sales_type_id'],
91                             $_POST['curr_abrev'], input_num('price'));
92
93                         $msg = _("The new price has been added.");
94                 }
95                 display_notification($msg);
96                 $Mode = 'RESET';
97         }
98
99 }
100
101 //------------------------------------------------------------------------------------------------------
102
103 if ($Mode == 'Delete')
104 {
105         //the link to delete a selected record was clicked
106         delete_item_price($selected_id);
107         display_notification(_("The selected price has been deleted."));
108         $Mode = 'RESET';
109 }
110
111 if ($Mode == 'RESET')
112 {
113         $selected_id = -1;
114 }
115
116 if (list_updated('stock_id')) {
117         $Ajax->activate('price_table');
118         $Ajax->activate('price_details');
119 }
120 if (list_updated('stock_id') || isset($_POST['_curr_abrev_update']) || isset($_POST['_sales_type_id_update'])) {
121         // after change of stock, currency or salestype selector
122         // display default calculated price for new settings. 
123         // If we have this price already in db it is overwritten later.
124         unset($_POST['price']);
125         $Ajax->activate('price_details');
126 }
127
128 //---------------------------------------------------------------------------------------------------
129
130 $prices_list = get_prices($_POST['stock_id']);
131
132 div_start('price_table');
133 start_table(TABLESTYLE, "width=30%");
134
135 $th = array(_("Currency"), _("Sales Type"), _("Price"), "", "");
136 table_header($th);
137 $k = 0; //row colour counter
138 $calculated = false;
139 while ($myrow = db_fetch($prices_list))
140 {
141
142         alt_table_row_color($k);
143
144         label_cell($myrow["curr_abrev"]);
145     label_cell($myrow["sales_type"]);
146     amount_cell($myrow["price"]);
147         edit_button_cell("Edit".$myrow['id'], _("Edit"));
148         delete_button_cell("Delete".$myrow['id'], _("Delete"));
149     end_row();
150
151 }
152 end_table();
153 if (db_num_rows($prices_list) == 0)
154 {
155         if (get_company_pref('add_pct') != -1)
156                 $calculated = true;
157         display_note(_("There are no prices set up for this part."), 1);
158 }
159 div_end();
160 //------------------------------------------------------------------------------------------------
161
162 echo "<br>";
163
164 if ($Mode == 'Edit')
165 {
166         $myrow = get_stock_price($selected_id);
167         $_POST['curr_abrev'] = $myrow["curr_abrev"];
168         $_POST['sales_type_id'] = $myrow["sales_type_id"];
169         $_POST['price'] = price_format($myrow["price"]);
170 }
171
172 hidden('selected_id', $selected_id);
173 div_start('price_details');
174 start_table(TABLESTYLE2);
175
176 currencies_list_row(_("Currency:"), 'curr_abrev', null, true);
177
178 sales_types_list_row(_("Sales Type:"), 'sales_type_id', null, true);
179
180 if (!isset($_POST['price'])) {
181         $_POST['price'] = price_format(get_kit_price(get_post('stock_id'), 
182                 get_post('curr_abrev'), get_post('sales_type_id')));
183 }
184
185 $kit = get_item_code_dflts($_POST['stock_id']);
186 small_amount_row(_("Price:"), 'price', null, '', _('per') .' '.$kit["units"]);
187
188 end_table(1);
189 if ($calculated)
190         display_note(_("The price is calculated."), 0, 1);
191
192 submit_add_or_update_center($selected_id == -1, '', 'both');
193 div_end();
194
195 end_form();
196 end_page();
197 ?>