Inventory Adjustment: fixed error in adjustment entry when inventory notifications...
[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_, $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_', 'reference', 'memo_'), $args);
20         $args->trans_no = 0;
21         hook_db_prewrite($args, ST_INVADJUST);
22
23         $adj_id = get_next_trans_no(ST_INVADJUST);
24
25         if ($SysPrefs->loc_notification() == 1)
26         {
27                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
28                 $st_ids = array();
29                 $st_names = array();
30                 $st_num = array();
31                 $st_reorder = array();
32         }
33         foreach ($items as $line_item)
34         {
35
36                 if ($SysPrefs->loc_notification() == 1 && $line_item->quantity < 0)
37                 {
38                         $chg = $line_item; $chg->quantity= -$chg->quantity;     // calculate_reorder_level expect positive qty
39                         $loc = calculate_reorder_level($location, $chg, $st_ids, $st_names, $st_num, $st_reorder); 
40                 }
41
42                 add_stock_adjustment_item($adj_id, $line_item->stock_id, $location, $date_, $reference,
43                         $line_item->quantity, $line_item->standard_cost, $memo_);
44         }
45
46         add_comments(ST_INVADJUST, $adj_id, $date_, $memo_);
47
48         $Refs->save(ST_INVADJUST, $adj_id, $reference);
49         add_audit_trail(ST_INVADJUST, $adj_id, $date_);
50
51         $args->trans_no = $adj_id;
52         hook_db_postwrite($args, ST_INVADJUST);
53         commit_transaction();
54         if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
55                 send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
56
57         return $adj_id;
58 }
59
60 //-------------------------------------------------------------------------------------------------------------
61
62 function void_stock_adjustment($type_no)
63 {
64         hook_db_prevoid(ST_INVADJUST, $type_no);
65         void_gl_trans(ST_INVADJUST, $type_no);
66         void_stock_move(ST_INVADJUST, $type_no);
67 }
68
69 //-------------------------------------------------------------------------------------------------------------
70
71 function get_stock_adjustment_items($trans_no)
72 {
73         $result = get_stock_moves(ST_INVADJUST, $trans_no);
74
75         if (db_num_rows($result) == 0)
76         {
77                 return null;
78         }
79
80         return $result;
81 }
82
83 //--------------------------------------------------------------------------------------------------
84
85 function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $reference,
86         $quantity, $standard_cost, $memo_)
87 {
88         $mb_flag = get_mb_flag($stock_id);
89
90     if (is_service($mb_flag))
91     {
92         display_db_error("Cannot do inventory adjustment for Service item : $stock_id", "");
93     }
94
95         update_average_material_cost(null, $stock_id, $standard_cost, $quantity, $date_);
96
97         if (is_fixed_asset($mb_flag)) {
98                 $sql = "UPDATE ".TB_PREF."stock_master SET inactive=1
99                         WHERE stock_id=".db_escape($stock_id);
100                 db_query($sql,"The inactive flag for the fixed asset could not be updated");
101         }
102
103         add_stock_move(ST_INVADJUST, $stock_id, $adj_id, $location,
104         $date_, $reference, $quantity, $standard_cost);
105
106         $inv_value = $standard_cost * $quantity;
107         $adj_value = $standard_cost * -($quantity);
108
109         if (is_fixed_asset($mb_flag)) {
110                 // get the initial value of the fixed assset.
111                 $row = get_fixed_asset_move($stock_id, ST_SUPPRECEIVE);
112                 $inv_value = $row['price'] * $quantity;
113                 $adj_value = (-($row['price']) + $standard_cost) * $quantity;
114         }
115
116         if ($standard_cost > 0 || is_fixed_asset($mb_flag))
117         {
118
119                 $stock_gl_codes = get_stock_gl_code($stock_id);
120
121                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_,
122                         $stock_gl_codes['adjustment_account'], $stock_gl_codes['dimension_id'], $stock_gl_codes['dimension2_id'], $memo_, $adj_value  );
123
124                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_, $stock_gl_codes['inventory_account'], 0, 0, $memo_, $inv_value);
125         }
126
127         if (is_fixed_asset($mb_flag)) {
128                 // Additional gl entry for fixed asset.
129                 $grn_act = get_company_pref('default_loss_on_asset_disposal_act');
130                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_, $grn_act, 0, 0, $memo_, ($standard_cost * -($quantity)));
131         }
132 }