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