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