Fixed warnings related to empty item/customer/supplier selector in search modes.
[fa-stable.git] / inventory / cost_update.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_STANDARDCOST';
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/banking.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
25 include_once($path_to_root . "/includes/ui/items_cart.inc");
26
27 $js = "";
28 if ($SysPrefs->use_popup_windows)
29         $js .= get_js_open_window(900, 500);
30
31 if (isset($_GET['FixedAsset'])) {
32         $_SESSION['page_title'] = _($help_context = "FA Revaluation");
33         $_POST['fixed_asset'] = 1;
34 } else {
35         $_SESSION['page_title'] = _($help_context = "Inventory Item Cost Update");
36 }
37 page($_SESSION['page_title'], false, false, "", $js);
38
39 //--------------------------------------------------------------------------------------
40
41 if (get_post('fixed_asset') == 1)
42         check_db_has_disposable_fixed_assets(_("There are no fixed assets defined in the system."));
43 else
44         check_db_has_costable_items(_("There are no costable inventory items defined in the system (Purchased or manufactured items)."));
45
46 if (isset($_GET['stock_id']))
47 {
48         $_POST['stock_id'] = $_GET['stock_id'];
49 }
50
51 //--------------------------------------------------------------------------------------
52 $should_update = false;
53 if (isset($_POST['UpdateData']))
54 {
55         $old_cost = get_unit_cost($_POST['stock_id']);
56
57         $new_cost = input_num('material_cost') + input_num('labour_cost')
58              + input_num('overhead_cost');
59
60         $should_update = true;
61
62         if (!check_num('material_cost') || !check_num('labour_cost') ||
63                 !check_num('overhead_cost'))
64         {
65                 display_error( _("The entered cost is not numeric."));
66                 set_focus('material_cost');
67                 $should_update = false;
68         }
69         elseif ($old_cost == $new_cost)
70         {
71                 display_error( _("The new cost is the same as the old cost. Cost was not updated."));
72                 $should_update = false;
73         }
74
75         if ($should_update)
76         {
77                 $update_no = stock_cost_update($_POST['stock_id'],
78                     input_num('material_cost'), input_num('labour_cost'),
79                     input_num('overhead_cost'), $old_cost, 
80         $_POST['refline'], $_POST['memo_']);
81
82         display_notification(_("Cost has been updated."));
83
84         if ($update_no > 0)
85         {
86                 display_notification(get_gl_view_str(ST_COSTUPDATE, $update_no, _("View the GL Journal Entries for this Cost Update")));
87         }
88
89         }
90 }
91
92 if (list_updated('stock_id') || $should_update) {
93         unset($_POST['memo_']);
94         $Ajax->activate('cost_table');
95 }
96 //-----------------------------------------------------------------------------------------
97
98 $action = $_SERVER['PHP_SELF'];
99 if ($page_nested)
100         $action .= "?stock_id=".get_post('stock_id');
101 start_form(false, false, $action);
102
103 hidden('fixed_asset');
104
105 if (!isset($_POST['stock_id']))
106         $_POST['stock_id'] = get_global_stock_item();
107
108 if (!$page_nested)
109 {
110         echo "<center>" . _("Item:"). "&nbsp;";
111         if (get_post('fixed_asset') == 1)
112                 echo stock_disposable_fa_list('stock_id', $_POST['stock_id'], false, true);
113         else
114                 echo stock_items_list('stock_id', $_POST['stock_id'], false, true);
115
116         echo "</center><hr>";
117 }
118 else
119         br(2);
120
121 set_global_stock_item($_POST['stock_id']);
122
123 $myrow = get_item($_POST['stock_id']);
124
125 div_start('cost_table');
126
127 start_table(TABLESTYLE2);
128 $dec1 = $dec2 = $dec3 = 0;
129 if ($myrow) {
130         $_POST['material_cost'] = price_decimal_format($myrow["material_cost"], $dec1);
131         $_POST['labour_cost'] = price_decimal_format($myrow["labour_cost"], $dec2);
132         $_POST['overhead_cost'] = price_decimal_format($myrow["overhead_cost"], $dec3);
133 }
134
135 amount_row(_("Unit cost"), "material_cost", null, "class='tableheader2'", null, $dec1);
136
137 if ($myrow && $myrow["mb_flag"]=='M')
138 {
139         amount_row(_("Standard Labour Cost Per Unit"), "labour_cost", null, "class='tableheader2'", null, $dec2);
140         amount_row(_("Standard Overhead Cost Per Unit"), "overhead_cost", null, "class='tableheader2'", null, $dec3);
141 }
142 else
143 {
144         hidden("labour_cost", 0);
145         hidden("overhead_cost", 0);
146 }
147 refline_list_row(_("Reference line:"), 'refline', ST_COSTUPDATE, null, false, get_post('fixed_asset'));
148 textarea_row(_("Memo"), 'memo_', null, 40, 4);
149
150 end_table(1);
151 div_end();
152 submit_center('UpdateData', _("Update"), true, false, 'default');
153
154 end_form();
155 end_page();