7be0f9bbb05a1b759b9cb816f8a14af19592008d
[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         global $Refs;
17
18         begin_transaction();
19         $args = func_get_args();
20         $args = (object)array_combine(array('items', 'location', 'date_', 'type', 'increase',
21                 'reference', 'memo_'), $args);
22         $args->trans_no = 0;
23         hook_db_prewrite($args, ST_INVADJUST);
24
25         $adj_id = get_next_trans_no(ST_INVADJUST);
26
27         foreach ($items as $line_item)
28         {
29
30                 if (!$increase)
31                         $line_item->quantity = -$line_item->quantity;
32
33                 add_stock_adjustment_item($adj_id, $line_item->stock_id, $location, $date_, $type, $reference,
34                         $line_item->quantity, $line_item->standard_cost, $memo_);
35         }
36
37         add_comments(ST_INVADJUST, $adj_id, $date_, $memo_);
38
39         $Refs->save(ST_INVADJUST, $adj_id, $reference);
40         add_audit_trail(ST_INVADJUST, $adj_id, $date_);
41
42         $args->trans_no = $adj_id;
43         hook_db_postwrite($args, ST_INVADJUST);
44         commit_transaction();
45
46         return $adj_id;
47 }
48
49 //-------------------------------------------------------------------------------------------------------------
50
51 function void_stock_adjustment($type_no)
52 {
53         hook_db_prevoid(ST_INVADJUST, $type_no);
54         void_gl_trans(ST_INVADJUST, $type_no);
55         void_stock_move(ST_INVADJUST, $type_no);
56 }
57
58 //-------------------------------------------------------------------------------------------------------------
59
60 function get_stock_adjustment_items($trans_no)
61 {
62         $result = get_stock_moves(ST_INVADJUST, $trans_no);
63
64         if (db_num_rows($result) == 0)
65         {
66                 return null;
67         }
68
69         return $result;
70 }
71
72 //--------------------------------------------------------------------------------------------------
73
74 function add_stock_adjustment_item($adj_id, $stock_id, $location, $date_, $type, $reference,
75         $quantity, $standard_cost, $memo_)
76 {
77         $mb_flag = get_mb_flag($stock_id);
78
79     if (is_service($mb_flag))
80     {
81         display_db_error("Cannot do inventory adjustment for Service item : $stock_id", "");
82     }
83
84         /* Logic shifted to function update_average_material_cost
85         //Chaitanya : If negative adjustment result in negative or zero inventory 
86         //then difference should be adjusted
87         $qoh = get_qoh_on_date($stock_id);
88         if ($qoh + $quantity <= 0 && $qoh > 0) //Positive inventory turning zero/negative
89         {
90                 global $Refs;
91
92                 $id = get_next_trans_no(ST_JOURNAL);
93                 $ref = $Refs->get_next(ST_JOURNAL);
94                 $diff = get_standard_cost($stock_id) - $standard_cost;
95                 
96                 if ($diff !=0)
97                 {
98                         $stock_gl_code = get_stock_gl_code($stock_id);
99                         $memo = _("For zero inventory of ").$stock_id." INVADJ REF: ".$reference;
100                         //Reverse the inventory effect if $qoh <=0
101                         add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
102                                 $stock_gl_code["inventory_account"],
103                                 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo, 
104                                 -$qoh * $diff);
105                         //GL Posting to inventory adjustment account
106                         add_gl_trans_std_cost(ST_JOURNAL, $id, $date_, 
107                                 $stock_gl_code["adjustment_account"],
108                                 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
109                                 $qoh * $diff);
110                                 
111                         add_audit_trail(ST_JOURNAL, $id, $date_);
112                         add_comments(ST_JOURNAL, $id, $date_, $memo);
113                         $Refs->save(ST_JOURNAL, $id, $ref);     
114                 }               
115         }*/
116
117         update_average_material_cost(null, $stock_id, $standard_cost, $quantity, $date_);
118
119         add_stock_move(ST_INVADJUST, $stock_id, $adj_id, $location,
120         $date_, $reference, $quantity, $standard_cost, $type);
121
122         if ($standard_cost > 0)
123         {
124
125                 $stock_gl_codes = get_stock_gl_code($stock_id);
126
127                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_,
128                         $stock_gl_codes['adjustment_account'], $stock_gl_codes['dimension_id'], $stock_gl_codes['dimension2_id'], $memo_, ($standard_cost * -($quantity)));
129
130                 add_gl_trans_std_cost(ST_INVADJUST, $adj_id, $date_, $stock_gl_codes['inventory_account'], 0, 0, $memo_, ($standard_cost * $quantity));
131         }
132 }
133
134 //-------------------------------------------------------------------------------------------------------------
135
136 ?>