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 function get_qoh_on_date($stock_id, $location=null, $date_=null, $exclude=0)
17 $date = date2sql($date_);
19 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
20 WHERE stock_id=".db_escape($stock_id)."
21 AND tran_date <= '$date'";
23 if ($location != null)
24 $sql .= " AND loc_code = ".db_escape($location);
26 $result = db_query($sql, "QOH calulcation failed");
28 $myrow = db_fetch_row($result);
31 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
32 WHERE stock_id=".db_escape($stock_id)
33 ." AND type=".db_escape($exclude)
34 ." AND tran_date = '$date'";
36 $result = db_query($sql, "QOH calulcation failed");
37 $myrow2 = db_fetch_row($result);
38 if ($myrow2 !== false)
39 $myrow[0] -= $myrow2[0];
45 //--------------------------------------------------------------------------------------
47 function get_item_edit_info($stock_id)
49 $sql = "SELECT material_cost + labour_cost + overhead_cost AS standard_cost, units, decimals
50 FROM ".TB_PREF."stock_master,".TB_PREF."item_units
51 WHERE stock_id=".db_escape($stock_id)
52 ." AND ".TB_PREF."stock_master.units=".TB_PREF."item_units.abbr";
53 $result = db_query($sql, "The standard cost cannot be retrieved");
55 return db_fetch($result);
58 //--------------------------------------------------------------------------------------
60 function get_standard_cost($stock_id)
62 $sql = "SELECT IF(s.mb_flag='D', 0, material_cost + labour_cost + overhead_cost) AS std_cost
63 FROM ".TB_PREF."stock_master s WHERE stock_id=".db_escape($stock_id);
64 $result = db_query($sql, "The standard cost cannot be retrieved");
66 $myrow = db_fetch_row($result);
71 //--------------------------------------------------------------------------------------
73 function is_inventory_item($stock_id)
75 $sql = "SELECT stock_id FROM ".TB_PREF."stock_master
76 WHERE stock_id=".db_escape($stock_id)." AND mb_flag <> 'D'";
77 $result = db_query($sql, "Cannot query is inventory item or not");
79 return db_num_rows($result) > 0;
82 //-------------------------------------------------------------------
84 function last_negative_stock_begin_date($stock_id, $to)
89 $sql = "SET @flag = 0";
91 $sql = "SELECT SUM(qty), @q:= @q + qty, IF(@q < 0 AND @flag=0, @flag:=1,@flag:=0), IF(@q < 0 AND @flag=1, tran_date,'') AS begin_date
92 FROM ".TB_PREF."stock_moves
93 WHERE stock_id=".db_escape($stock_id)." AND tran_date<='$to'
95 GROUP BY stock_id ORDER BY tran_date";
97 $result = db_query($sql, "The dstock moves could not be retrieved");
98 $row = db_fetch_row($result);
102 //-------------------------------------------------------------------
104 function get_deliveries_between($stock_id, $from, $to)
106 $from = date2sql($from);
108 $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
109 WHERE type=".ST_CUSTDELIVERY." AND stock_id=".db_escape($stock_id)." AND
110 tran_date>='$from' AND tran_date<='$to' GROUP BY stock_id";
112 $result = db_query($sql, "The deliveries could not be updated");
113 return db_fetch_row($result);
116 //-------------------------------------------------------------------
118 function adjust_deliveries($stock_id, $material_cost, $to)
120 if (!is_inventory_item($stock_id))
122 $from = last_negative_stock_begin_date($stock_id, $to);
123 if ($from == false || $from == "")
125 $from = sql2date($from);
126 $row = get_deliveries_between($stock_id, $from, $to);
130 $new_cost = $row[0] * $material_cost;
131 $diff = $new_cost - $old_cost;
134 $update_no = get_next_trans_no(ST_COSTUPDATE);
135 if (!is_date_in_fiscalyear($to))
136 $to = end_fiscalyear();
138 $stock_gl_code = get_stock_gl_code($stock_id);
140 $memo_ = _("Cost was ") . $old_cost . _(" changed to ") . $new_cost . _(" for item ")."'$stock_id'";
141 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["cogs_account"],
142 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, $diff);
144 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["inventory_account"], 0, 0, $memo_,
146 add_audit_trail(ST_COSTUPDATE, $update_no, $to);
150 function get_stock_gl_code($stock_id)
152 /*Gets the GL Codes relevant to the item account */
154 $sql = "SELECT inventory_account, cogs_account,
155 adjustment_account, sales_account, assembly_account, dimension_id, dimension2_id FROM
156 ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
158 $get = db_query($sql,"retreive stock gl code");
159 return db_fetch($get);
162 //--------------------------------------------------------------------------------------
164 // $date_ - display / non-sql date
165 // $std_cost - in HOME currency
166 // $show_or_hide - wil this move be visible in reports, etc
167 // $price - in $person_id's currency
169 function add_stock_move($type, $stock_id, $trans_no, $location,
170 $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1,
171 $price=0, $discount_percent=0, $error_msg="")
173 // do not add a stock move if it's a non-inventory item
174 if (!is_inventory_item($stock_id))
177 $date = date2sql($date_);
179 $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, trans_no, type, loc_code,
180 tran_date, person_id, reference, qty, standard_cost, visible, price,
181 discount_percent) VALUES (".db_escape($stock_id)
182 .", ".db_escape($trans_no).", ".db_escape($type)
183 .", ".db_escape($location).", '$date', "
184 .db_escape($person_id).", ".db_escape($reference).", "
185 .db_escape($quantity).", ".db_escape($std_cost).","
186 .db_escape($show_or_hide).", ".db_escape($price).", "
187 .db_escape($discount_percent).")";
189 if ($error_msg == "")
190 $error_msg = "The stock movement record cannot be inserted";
192 db_query($sql, $error_msg);
194 return db_insert_id();
197 function update_stock_move_pid($type, $stock_id, $from, $to, $pid, $cost)
199 $from = date2sql($from);
201 $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost)
202 ." WHERE type=".db_escape($type)
203 ." AND stock_id=".db_escape($stock_id)
204 ." AND tran_date>='$from' AND tran_date<='$to'
205 AND person_id = ".db_escape($pid);
206 db_query($sql, "The stock movement standard_cost cannot be updated");
209 //--------------------------------------------------------------------------------------------------
211 function get_stock_moves($type, $type_no, $visible=false)
213 $sql = "SELECT ".TB_PREF."stock_moves.*, ".TB_PREF."stock_master.description, "
214 .TB_PREF."stock_master.units,".TB_PREF."locations.location_name,"
215 .TB_PREF."stock_master.material_cost + "
216 .TB_PREF."stock_master.labour_cost + "
217 .TB_PREF."stock_master.overhead_cost AS FixedStandardCost
218 FROM ".TB_PREF."stock_moves,".TB_PREF."locations,".TB_PREF."stock_master
219 WHERE ".TB_PREF."stock_moves.stock_id = ".TB_PREF."stock_master.stock_id
220 AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code
221 AND type=".db_escape($type)." AND trans_no=".db_escape($type_no)." ORDER BY trans_id";
223 $sql .= " AND ".TB_PREF."stock_moves.visible=1";
225 return db_query($sql, "Could not get stock moves");
228 //--------------------------------------------------------------------------------------------------
230 function void_stock_move($type, $type_no)
232 $sql = "UPDATE ".TB_PREF."stock_moves SET qty=0, price=0, discount_percent=0,
233 standard_cost=0 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
235 db_query($sql, "Could not void stock moves");
238 //--------------------------------------------------------------------------------------------------
240 function get_location_name($loc_code)
242 $sql = "SELECT location_name FROM ".TB_PREF."locations WHERE loc_code="
243 .db_escape($loc_code);
245 $result = db_query($sql, "could not retreive the location name for $loc_code");
247 if (db_num_rows($result) == 1)
249 $row = db_fetch_row($result);
253 display_db_error("could not retreive the location name for $loc_code", $sql, true);
256 //--------------------------------------------------------------------------------------------------