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