Merging version 2.1 RC to main trunk.
[fa-stable.git] / inventory / includes / db / items_adjust_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 add_stock_adjustment($items, $location, $date_, $type, $increase, $reference, $memo_)
15 {
16         begin_transaction();
17
18         $adj_id = get_next_trans_no(systypes::inventory_adjustment());
19
20         foreach ($items as $line_item)
21         {
22
23                 if (!$increase)
24                         $line_item->quantity = -$line_item->quantity;
25
26                 add_stock_adjustment_item($adj_id, $line_item->stock_id, $location, $date_, $type, $reference,
27                         $line_item->quantity, $line_item->standard_cost, $memo_);
28         }
29
30         add_comments(systypes::inventory_adjustment(), $adj_id, $date_, $memo_);
31
32         references::save_last($reference, systypes::inventory_adjustment());
33
34         commit_transaction();
35
36         return $adj_id;
37 }
38
39 //-------------------------------------------------------------------------------------------------------------
40
41 function void_stock_adjustment($type_no)
42 {
43         void_gl_trans(systypes::inventory_adjustment(), $type_no);
44         void_stock_move(systypes::inventory_adjustment(), $type_no);
45 }
46
47 //-------------------------------------------------------------------------------------------------------------
48
49 function get_stock_adjustment_items($trans_no)
50 {
51         $result = get_stock_moves(systypes::inventory_adjustment(), $trans_no);
52
53         if (db_num_rows($result) == 0)
54         {
55                 return null;
56         }
57
58         return $result;
59 }
60
61 //--------------------------------------------------------------------------------------------------
62
63 function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $type, $reference,
64         $quantity, $standard_cost, $memo_)
65 {
66         $mb_flag = get_mb_flag($stock_id);
67
68     if (is_service($mb_flag))
69     {
70         display_db_error("Cannot do inventory adjustment for Service item : $stock_id", "");
71     }
72
73         update_average_material_cost(null, $stock_id, $standard_cost, $quantity, $date_);
74
75         add_stock_move(systypes::inventory_adjustment(), $stock_id, $adj_id, $location,
76         $date_, $reference, $quantity, $standard_cost, $type);
77
78         if ($standard_cost > 0)
79         {
80
81                 $stock_gl_codes = get_stock_gl_code($stock_id);
82
83                 add_gl_trans_std_cost(systypes::inventory_adjustment(), $adj_id, $date_,
84                         $stock_gl_codes['adjustment_account'], $stock_gl_codes['dimension_id'], $stock_gl_codes['dimension2_id'], $memo_, ($standard_cost * -($quantity)));
85
86                 add_gl_trans_std_cost(systypes::inventory_adjustment(), $adj_id, $date_, $stock_gl_codes['inventory_account'], 0, 0, $memo_, ($standard_cost * $quantity));
87         }
88 }
89
90 //-------------------------------------------------------------------------------------------------------------
91
92 ?>