c97be95c1861c115c18c512c4d34acfc89cc7c65
[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, $refline, $memo_)
16 {
17         $mb_flag = get_mb_flag($stock_id);
18
19         $update_no = -1;
20
21     if (is_service($mb_flag))
22     {
23                 $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
24                 WHERE stock_id=".db_escape($stock_id);
25
26                 db_query($sql,"The cost details for the inventory item could not be updated");
27
28                 return $update_no;
29     }
30
31         begin_transaction();
32
33         $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost).", 
34                 labour_cost=".db_escape($labour_cost).", 
35                 overhead_cost=".db_escape($overhead_cost).", 
36                 last_cost=".db_escape($last_cost)."
37                 WHERE stock_id=".db_escape($stock_id);
38         db_query($sql,"The cost details for the inventory item could not be updated");
39
40         $qoh = get_qoh_on_date($stock_id);
41
42         $date_ = Today();
43         if (!is_date_in_fiscalyear($date_))
44                 $date_ = end_fiscalyear();
45
46         if ($qoh > 0)
47         {
48                 $new_cost = $material_cost + $labour_cost + $overhead_cost;
49
50                 $value_of_change = round2($qoh * ($new_cost - $last_cost), user_price_dec());
51
52                 if ($value_of_change != 0)
53                 {
54                         global $Refs;
55                         $stock_gl_code = get_stock_gl_code($stock_id);
56
57                         $cart = new items_cart(ST_COSTUPDATE);
58                         $cart->tran_date = $cart->doc_date = $cart->event_date = $date_;
59                         if (!is_date_in_fiscalyear($cart->tran_date))
60                                 $cart->tran_date = end_fiscalyear();
61                         $cart->reference = $Refs->get_next(ST_COSTUPDATE, $refline, $cart->tran_date, $date_);
62  
63                         if (empty($memo_))
64                                 $cart->memo_ = sprintf(_("Cost was %s changed to %s x quantity on hand of %s"),
65                                         number_format2($last_cost, 2), number_format2($new_cost), $qoh);
66                         else
67                                 $cart->memo_ = $memo_;
68
69                         $cart->add_gl_item($stock_gl_code["adjustment_account"],
70                                 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], -$value_of_change);
71                         $cart->add_gl_item($stock_gl_code["inventory_account"], 0, 0, $value_of_change);
72
73                         write_journal_entries($cart);
74                         change_stock_moves_std_cost($stock_id, $date_, $new_cost - $last_cost); 
75                 }
76         }
77
78         if ($update_no != -1)
79                 add_audit_trail(ST_COSTUPDATE, $update_no, $date_);
80         commit_transaction();
81
82         return $update_no;
83 }
84
85 //-------------------------------------------------------------------------------------------------------------
86
87 function change_stock_moves_std_cost($stock_id, $date, $diff_cost)
88 {
89         $date = date2sql($date);
90         $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost = standard_cost + ".db_escape($diff_cost). " WHERE stock_id = "
91                         .db_escape($stock_id)." AND tran_date <= '$date' AND qty <> 0 AND standard_cost > 0.001 AND type <> ".ST_LOCTRANSFER;
92         db_query($sql,"The stock moves cost details for the inventory item could not be updated");
93 }