Service Items - BOM & Cost Update allowed without side-effects
[fa-stable.git] / inventory / includes / db / items_trans_db.inc
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 //-------------------------------------------------------------------------------------------------------------
13
14 function stock_cost_update($stock_id, $material_cost, $labour_cost, $overhead_cost,
15         $last_cost)
16 {
17         $mb_flag = get_mb_flag($stock_id);
18     
19         $update_no = -1;\r
20 \r
21     if (is_service($mb_flag))
22     {
23         //display_db_error("Cannot do cost update for Service item : $stock_id", "");   \r
24                 \r
25                 //Chaitanya\r
26                 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."\r
27                 WHERE stock_id=".db_escape($stock_id);\r
28                 \r
29                 db_query($sql,"The cost details for the inventory item could not be updated");\r
30                 \r
31                 return $update_no;\r
32                 \r
33     }           
34         
35         begin_transaction();
36         
37         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost).", 
38                 labour_cost=".db_escape($labour_cost).", 
39                 overhead_cost=".db_escape($overhead_cost).", 
40                 last_cost=".db_escape($last_cost)." 
41                 WHERE stock_id=".db_escape($stock_id);
42         db_query($sql,"The cost details for the inventory item could not be updated");
43
44         $qoh = get_qoh_on_date($_POST['stock_id']);
45         
46         $date_ = Today();
47         if (!is_date_in_fiscalyear($date_))\r
48                 $date_ = end_fiscalyear();\r
49 \r
50         if ($qoh > 0)
51         {
52                 $new_cost = $material_cost + $labour_cost + $overhead_cost;
53
54                 $value_of_change = round2($qoh * ($new_cost - $last_cost), user_price_dec());
55 \r
56                 if ($value_of_change != 0)
57                 {\r
58                         $stock_gl_code = get_stock_gl_code($stock_id);\r
59                         $update_no = get_next_trans_no(ST_COSTUPDATE);\r
60                         $memo_ = "Cost was " . $last_cost . " changed to " . $new_cost . " x quantity on hand of $qoh";
61                         add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $date_, $stock_gl_code["adjustment_account"], 
62                                 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, (-$value_of_change));     
63
64                         add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $date_, $stock_gl_code["inventory_account"], 0, 0, $memo_, 
65                                 $value_of_change);
66                 }               \r
67         }
68
69         add_audit_trail(ST_COSTUPDATE, $update_no, $date_);
70         commit_transaction();
71
72         return $update_no;
73 }
74
75 //-------------------------------------------------------------------------------------------------------------
76
77 ?>