References saved in refs table for all documents for easy access.
[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(systypes::inventory_adjustment(), $adj_id, $reference);
33         add_audit_trail(systypes::inventory_adjustment(), $adj_id, $date_);
34
35         commit_transaction();
36
37         return $adj_id;
38 }
39
40 //-------------------------------------------------------------------------------------------------------------
41
42 function void_stock_adjustment($type_no)
43 {
44         void_gl_trans(systypes::inventory_adjustment(), $type_no);
45         void_stock_move(systypes::inventory_adjustment(), $type_no);
46 }
47
48 //-------------------------------------------------------------------------------------------------------------
49
50 function get_stock_adjustment_items($trans_no)
51 {
52         $result = get_stock_moves(systypes::inventory_adjustment(), $trans_no);
53
54         if (db_num_rows($result) == 0)
55         {
56                 return null;
57         }
58
59         return $result;
60 }
61
62 //--------------------------------------------------------------------------------------------------
63
64 function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $type, $reference,
65         $quantity, $standard_cost, $memo_)
66 {
67         $mb_flag = get_mb_flag($stock_id);
68
69     if (is_service($mb_flag))
70     {
71         display_db_error("Cannot do inventory adjustment for Service item : $stock_id", "");
72     }
73
74         update_average_material_cost(null, $stock_id, $standard_cost, $quantity, $date_);
75
76         add_stock_move(systypes::inventory_adjustment(), $stock_id, $adj_id, $location,
77         $date_, $reference, $quantity, $standard_cost, $type);
78
79         if ($standard_cost > 0)
80         {
81
82                 $stock_gl_codes = get_stock_gl_code($stock_id);
83
84                 add_gl_trans_std_cost(systypes::inventory_adjustment(), $adj_id, $date_,
85                         $stock_gl_codes['adjustment_account'], $stock_gl_codes['dimension_id'], $stock_gl_codes['dimension2_id'], $memo_, ($standard_cost * -($quantity)));
86
87                 add_gl_trans_std_cost(systypes::inventory_adjustment(), $adj_id, $date_, $stock_gl_codes['inventory_account'], 0, 0, $memo_, ($standard_cost * $quantity));
88         }
89 }
90
91 //-------------------------------------------------------------------------------------------------------------
92
93 ?>