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