SID & start_form() cleanup.
[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 = 2;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/includes/ui.inc");
18 include_once($path_to_root . "/includes/banking.inc");
19 include_once($path_to_root . "/includes/manufacturing.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21
22 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
23 $js = "";
24 if ($use_popup_windows)
25         $js .= get_js_open_window(900, 500);
26 page(_("Inventory Item Cost Update"), false, false, "", $js);
27
28 //--------------------------------------------------------------------------------------
29
30 check_db_has_costable_items(_("There are no costable inventory items defined in the system (Purchased or manufactured items)."));
31
32 if (isset($_GET['stock_id']))
33 {
34         $_POST['stock_id'] = $_GET['stock_id'];
35 }
36
37 //--------------------------------------------------------------------------------------
38 if (isset($_POST['UpdateData']))
39 {
40
41         $old_cost = $_POST['OldMaterialCost'] + $_POST['OldLabourCost']
42             + $_POST['OldOverheadCost'];
43         $new_cost = input_num('material_cost') + input_num('labour_cost')
44              + input_num('overhead_cost');
45
46         $should_update = true;
47
48         if (!check_num('material_cost') || !check_num('labour_cost') ||
49                 !check_num('overhead_cost'))
50         {
51                 display_error( _("The entered cost is not numeric."));
52                 set_focus('material_cost');
53                 $should_update = false;
54         }
55         elseif ($old_cost == $new_cost)
56         {
57                 display_error( _("The new cost is the same as the old cost. Cost was not updated."));
58                 $should_update = false;
59         }
60
61         if ($should_update)
62         {
63                 $update_no = stock_cost_update($_POST['stock_id'],
64                     input_num('material_cost'), input_num('labour_cost'),
65                     input_num('overhead_cost'), $old_cost);
66
67         display_notification(_("Cost has been updated."));
68
69         if ($update_no > 0)
70         {
71                 display_note(get_gl_view_str(systypes::cost_update(), $update_no, _("View the GL Journal Entries for this Cost Update")), 0, 1);
72         }
73         }
74 }
75
76 if (list_updated('stock_id'))
77         $Ajax->activate('cost_table');
78 //-----------------------------------------------------------------------------------------
79
80 start_form();
81
82 if (!isset($_POST['stock_id']))
83         $_POST['stock_id'] = get_global_stock_item();
84
85 echo "<center>" . _("Item:"). "&nbsp;";
86 stock_costable_items_list('stock_id', $_POST['stock_id'], false, true);
87
88 echo "</center><hr>";
89 set_global_stock_item($_POST['stock_id']);
90
91 $sql = "SELECT description, units, material_cost, labour_cost,
92         overhead_cost, mb_flag
93         FROM ".TB_PREF."stock_master
94         WHERE stock_id='" . $_POST['stock_id'] . "'
95         GROUP BY description, units, material_cost, labour_cost, overhead_cost, mb_flag";
96 $result = db_query($sql);
97 check_db_error("The cost details for the item could not be retrieved", $sql);
98
99 $myrow = db_fetch($result);
100 div_start('cost_table');
101 hidden("OldMaterialCost", $myrow["material_cost"]);
102 hidden("OldLabourCost", $myrow["labour_cost"]);
103 hidden("OldOverheadCost", $myrow["overhead_cost"]);
104
105 start_table($table_style2);
106
107 $_POST['material_cost'] = price_format($myrow["material_cost"]);
108 $_POST['labour_cost'] = price_format($myrow["labour_cost"]);
109 $_POST['overhead_cost'] = price_format($myrow["overhead_cost"]);
110
111 amount_row(_("Standard Material Cost Per Unit"), "material_cost",
112         null, "class='tableheader2'");
113
114 if ($myrow["mb_flag"]=='M')
115 {
116         amount_row(_("Standard Labour Cost Per Unit"), "labour_cost",
117                 null, "class='tableheader2'");
118         amount_row(_("Standard Overhead Cost Per Unit"), "overhead_cost",
119                 null, "class='tableheader2'");
120 }
121 else
122 {
123         hidden("labour_cost", 0);
124         hidden("overhead_cost", 0);
125 }
126
127 end_table(1);
128 div_end();
129 submit_center('UpdateData', _("Update"));
130
131 end_form();
132 end_page();
133
134 ?>