Inventory Adjustment: removed movement type, cleanups.
[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 function add_stock_adjustment($items, $location, $date_, $increase, $reference, $memo_)
14 {
15         global $SysPrefs, $path_to_root, $Refs;
16
17         begin_transaction();
18         $args = func_get_args();
19         $args = (object)array_combine(array('items', 'location', 'date_', 'increase',
20                 'reference', 'memo_'), $args);
21         $args->trans_no = 0;
22         hook_db_prewrite($args, ST_INVADJUST);
23
24         $adj_id = get_next_trans_no(ST_INVADJUST);
25
26         if ($SysPrefs->loc_notification() == 1 && !$increase)
27         {
28                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
29                 $st_ids = array();
30                 $st_names = array();
31                 $st_num = array();
32                 $st_reorder = array();
33         }
34         foreach ($items as $line_item)
35         {
36
37                 if ($SysPrefs->loc_notification() == 1 && !$increase)
38                         $loc = calculate_reorder_level($location, $line_item, $st_ids, $st_names, $st_num, $st_reorder); 
39
40                 if (!$increase)
41                         $line_item->quantity = -$line_item->quantity;
42
43                 add_stock_adjustment_item($adj_id, $line_item->stock_id, $location, $date_, $reference,
44                         $line_item->quantity, $line_item->standard_cost, $memo_);
45         }
46
47         add_comments(ST_INVADJUST, $adj_id, $date_, $memo_);
48
49         $Refs->save(ST_INVADJUST, $adj_id, $reference);
50         add_audit_trail(ST_INVADJUST, $adj_id, $date_);
51
52         $args->trans_no = $adj_id;
53         hook_db_postwrite($args, ST_INVADJUST);
54         commit_transaction();
55         if ($SysPrefs->loc_notification() == 1 && !$increase && count($st_ids) > 0)
56                 send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
57
58         return $adj_id;
59 }
60
61 //-------------------------------------------------------------------------------------------------------------
62
63 function void_stock_adjustment($type_no)
64 {
65         hook_db_prevoid(ST_INVADJUST, $type_no);
66         void_gl_trans(ST_INVADJUST, $type_no);
67         void_stock_move(ST_INVADJUST, $type_no);
68 }
69
70 //-------------------------------------------------------------------------------------------------------------
71
72 function get_stock_adjustment_items($trans_no)
73 {
74         $result = get_stock_moves(ST_INVADJUST, $trans_no);
75
76         if (db_num_rows($result) == 0)
77         {
78                 return null;
79         }
80
81         return $result;
82 }
83
84 //--------------------------------------------------------------------------------------------------
85
86 function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $reference,
87         $quantity, $standard_cost, $memo_)
88 {
89         $mb_flag = get_mb_flag($stock_id);
90
91     if (is_service($mb_flag))
92     {
93         display_db_error("Cannot do inventory adjustment for Service item : $stock_id", "");
94     }
95
96         update_average_material_cost(null, $stock_id, $standard_cost, $quantity, $date_);
97
98         add_stock_move(ST_INVADJUST, $stock_id, $adj_id, $location,
99         $date_, $reference, $quantity, $standard_cost);
100
101         if ($standard_cost > 0)
102         {
103
104                 $stock_gl_codes = get_stock_gl_code($stock_id);
105
106                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_,
107                         $stock_gl_codes['adjustment_account'], $stock_gl_codes['dimension_id'], $stock_gl_codes['dimension2_id'], $memo_, ($standard_cost * -($quantity)));
108
109                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_, $stock_gl_codes['inventory_account'], 0, 0, $memo_, ($standard_cost * $quantity));
110         }
111 }