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)
16 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
17 WHERE stock_id=".db_escape($stock_id);
19 $date = date2sql($date_);
23 $date = date2sql($date_);
24 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
25 WHERE stock_id=".db_escape($stock_id)."
26 AND tran_date <= '$date'";
29 if ($location != null)
30 $sql .= " AND loc_code = ".db_escape($location);
32 $result = db_query($sql, "QOH calulcation failed");
34 $myrow = db_fetch_row($result);
37 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
38 WHERE stock_id=".db_escape($stock_id)
39 ." AND type=".db_escape($exclude)
40 ." AND tran_date = '$date'";
42 $result = db_query($sql, "QOH calulcation failed");
43 $myrow2 = db_fetch_row($result);
44 if ($myrow2 !== false)
45 $myrow[0] -= $myrow2[0];
51 //--------------------------------------------------------------------------------------
53 function get_item_edit_info($stock_id)
55 $sql = "SELECT material_cost + labour_cost + overhead_cost AS standard_cost, units, decimals
56 FROM ".TB_PREF."stock_master,".TB_PREF."item_units
57 WHERE stock_id=".db_escape($stock_id)
58 ." AND ".TB_PREF."stock_master.units=".TB_PREF."item_units.abbr";
59 $result = db_query($sql, "The standard cost cannot be retrieved");
61 return db_fetch($result);
64 //--------------------------------------------------------------------------------------
66 function get_standard_cost($stock_id)
68 //Chaitanya : Compatibility with service items
69 $sql = "SELECT (material_cost + labour_cost + overhead_cost) AS std_cost
70 FROM ".TB_PREF."stock_master s WHERE stock_id=".db_escape($stock_id);
71 $result = db_query($sql, "The standard cost cannot be retrieved");
73 $myrow = db_fetch_row($result);
78 //--------------------------------------------------------------------------------------
80 function is_inventory_item($stock_id)
82 $sql = "SELECT stock_id FROM ".TB_PREF."stock_master
83 WHERE stock_id=".db_escape($stock_id)." AND mb_flag <> 'D'";
84 $result = db_query($sql, "Cannot query is inventory item or not");
86 return db_num_rows($result) > 0;
89 //-------------------------------------------------------------------
91 function last_negative_stock_begin_date($stock_id, $to)
96 $sql = "SET @flag = 0";
98 $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
99 FROM ".TB_PREF."stock_moves
100 WHERE stock_id=".db_escape($stock_id)." AND tran_date<='$to'
102 GROUP BY stock_id ORDER BY tran_date";
104 $result = db_query($sql, "The dstock moves could not be retrieved");
105 $row = db_fetch_row($result);
109 //-------------------------------------------------------------------
111 function get_already_delivered($stock_id, $location, $trans_no)
113 $sql = "SELECT ".TB_PREF."stock_moves.qty
114 FROM ".TB_PREF."stock_moves
115 WHERE ".TB_PREF."stock_moves.stock_id = ".db_escape($stock_id)."
116 AND ".TB_PREF."stock_moves.loc_code = ".db_escape($location)."
117 AND type=".ST_CUSTDELIVERY." AND trans_no=".db_escape($trans_no);
118 $result = db_query($sql, "Could not get stock moves");
119 $row = db_fetch_row($result);
123 function last_negative_stock_trans_id($stock_id, $to)
125 $sql = "SELECT * from ".TB_PREF."stock_moves
126 WHERE stock_id=".db_escape($stock_id)."
127 AND qty <> 0 order by trans_id asc";
129 $result = db_query($sql, "The query on stock moves failed.");
133 $negative_trans_id = 1;
135 while ($myrow = db_fetch($result))
137 $qty += $myrow['qty'];
138 if ($qty < 0 && $flag == 0)
141 $negative_trans_id = $myrow['trans_id'];
147 return $negative_trans_id;
150 //-------------------------------------------------------------------
152 function get_deliveries_between($stock_id, $from, $to)
154 $from = date2sql($from);
156 $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
157 WHERE type=".ST_CUSTDELIVERY." AND stock_id=".db_escape($stock_id)." AND
158 tran_date>='$from' AND tran_date<='$to' GROUP BY stock_id";
160 $result = db_query($sql, "The deliveries could not be updated");
161 return db_fetch_row($result);
164 function get_deliveries_from_trans($stock_id, $from)
166 // -ve qty is delivery either by ST_CUSTDELIVERY or inventory adjustment
167 $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
168 WHERE stock_id=".db_escape($stock_id)." AND qty < 0 AND
169 trans_id>='$from' GROUP BY stock_id";
170 $result = db_query($sql, "The deliveries could not be updated");
171 $row = db_fetch_row($result);
173 // display_notification('Row0 - '.$row[0].' Row1- '.$row[1]);
177 // Get Std cost of previsous transaction before the cut-over delivery
178 // This is useful to get inventory valuation
179 //Chaitanya : Corrected
180 /*$sql = "SELECT max( `trans_id` )
181 FROM ".TB_PREF."stock_moves
182 WHERE stock_id = ".db_escape($stock_id)."
183 AND trans_id<'$from'";
184 $result = db_query($sql, "The deliveries could not be updated");
185 $trans = db_fetch_row($result);
186 $prev_trans = $trans[0];*/
188 // display_notification('From - '.$from);
189 $sql = "SELECT standard_cost FROM ".TB_PREF."stock_moves
190 WHERE stock_id=".db_escape($stock_id)
191 ." AND trans_id ='$from'";
192 $result = db_query($sql, "The deliveries could not be updated");
193 $cost = db_fetch_row($result);
195 // display_notification('Last Delivery Cost - '.$cost[0]);
197 // Adjusting QOH valuation
198 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
199 WHERE stock_id=".db_escape($stock_id)." AND
200 trans_id<'$from' GROUP BY stock_id";
201 $result = db_query($sql, "The deliveries could not be updated");
202 $qoh = db_fetch_row($result);
204 // display_notification('QOH before last delivery - '.$qoh[0]);
206 $qty = $row[0] - $qoh[0]; //Qoh is minus from delivered in -ve
207 $final_cost = $row[1] - $qoh[0]*$cost[0];
209 // display_notification('Qty - '.$qty.' cost- '.$final_cost);
211 return array($qty,$final_cost);
215 function get_purchases_from_trans($stock_id, $from)
217 // Calculate All inward stock moves i.e. qty > 0
218 $sql = "SELECT SUM(qty), SUM(qty*standard_cost) FROM ".TB_PREF."stock_moves
219 WHERE stock_id=".db_escape($stock_id)." AND qty > 0 AND
220 trans_id>'$from' GROUP BY stock_id";
221 $result = db_query($sql, "Could not get get_purchases_from_trans");
222 $row = db_fetch_row($result);
224 // display_notification('Purchase Qty - '.$row[0].' Cost- '.$row[1]);
229 //-------------------------------------------------------------------
230 /* Original Code V0 Leave as is a while
231 function adjust_deliveries_v0($stock_id, $material_cost, $to)
233 if (!is_inventory_item($stock_id))
235 $from = last_negative_stock_begin_date($stock_id, $to);
236 if ($from == false || $from == "")
238 $from = sql2date($from);
239 $row = get_deliveries_between($stock_id, $from, $to);
243 $new_cost = $row[0] * $material_cost;
244 $diff = $new_cost - $old_cost;
247 $update_no = get_next_trans_no(ST_COSTUPDATE);
248 if (!is_date_in_fiscalyear($to))
249 $to = end_fiscalyear();
251 $stock_gl_code = get_stock_gl_code($stock_id);
253 $memo_ = sprintf(_("Cost was %s changed to %s for item '%s'"),
254 $old_cost, $new_cost, $stock_id);
255 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["cogs_account"],
256 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, $diff);
258 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["inventory_account"],
259 0, 0, $memo_, -$diff);
260 add_audit_trail(ST_COSTUPDATE, $update_no, $to);
264 //New written function
265 function adjust_deliveries($stock_id, $material_cost, $to)
267 if (!is_inventory_item($stock_id))
270 $from = last_negative_stock_trans_id($stock_id, $to);
271 if ($from == false || $from == "")
274 $row = get_deliveries_from_trans($stock_id, $from);
278 $old_sales_cost = $row[1];
279 $new_sales_cost = $row[0] * $material_cost;
280 $sales_diff = $new_sales_cost - $old_sales_cost;
282 $row = get_purchases_from_trans($stock_id, $from);
284 $old_purchase_cost = 0;
287 $old_purchase_cost = $row[1];
288 $new_purchase_cost = $row[0] * $material_cost;
289 $purchase_diff = $new_purchase_cost - $old_purchase_cost;
292 $diff = $sales_diff - $purchase_diff;
296 $update_no = get_next_trans_no(ST_COSTUPDATE);
297 if (!is_date_in_fiscalyear($to))
298 $to = end_fiscalyear();
300 $stock_gl_code = get_stock_gl_code($stock_id);
302 $dec = user_price_dec();
303 $old_cost = -round2($old_sales_cost-$old_purchase_cost,$dec);
304 $new_cost = -round2($new_sales_cost-$new_purchase_cost,$dec);
306 $memo_ = _("Cost was ") . $old_cost. _(" changed to ") . $new_cost . _(" for item ")."'$stock_id'";
307 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["cogs_account"],
308 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, $diff);
310 add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $to, $stock_gl_code["inventory_account"], 0, 0, $memo_,
312 add_audit_trail(ST_COSTUPDATE, $update_no, $to);
316 function get_stock_gl_code($stock_id)
318 /*Gets the GL Codes relevant to the item account */
319 $sql = "SELECT mb_flag, inventory_account, cogs_account,
320 adjustment_account, sales_account, assembly_account, dimension_id, dimension2_id FROM
321 ".TB_PREF."stock_master WHERE stock_id = ".db_escape($stock_id);
323 $get = db_query($sql,"retreive stock gl code");
324 return db_fetch($get);
327 //-----------------------------------------------------------------------------------------
328 //Chaitanya : New Function handle negative stock effect
329 //Called in add_stock_move
330 function handle_negative_inventory($stock_id, $quantity, $standard_cost, $date_)
332 //Chaitanya : If negative adjustment result in negative or zero inventory
333 //then difference should be adjusted
334 $qoh = get_qoh_on_date($stock_id);
336 if ($qoh + $quantity <= 0 && $qoh > 0) //Positive inventory turning zero/negative
340 $id = get_next_trans_no(ST_JOURNAL);
341 $ref = $Refs->get_next(ST_JOURNAL);
342 $diff = get_standard_cost($stock_id) - $standard_cost;
346 $stock_gl_code = get_stock_gl_code($stock_id);
347 $memo = _("Zero/negative inventory handling");
348 //Reverse the inventory effect if $qoh <=0
349 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
350 $stock_gl_code["inventory_account"],
351 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
353 //GL Posting to inventory adjustment account
354 add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
355 $stock_gl_code["adjustment_account"],
356 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], $memo,
359 add_audit_trail(ST_JOURNAL, $id, $date_);
360 add_comments(ST_JOURNAL, $id, $date_, $memo);
361 $Refs->save(ST_JOURNAL, $id, $ref);
366 //--------------------------------------------------------------------------------------
368 // $date_ - display / non-sql date
369 // $std_cost - in HOME currency
370 // $show_or_hide - wil this move be visible in reports, etc
371 // $price - in $person_id's currency
373 function add_stock_move($type, $stock_id, $trans_no, $location,
374 $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1,
375 $price=0, $discount_percent=0, $error_msg="")
377 // Chaitanya : Removed following restriction considering WO issues
378 // Voiding issues and productions with Service items can not get the
379 // Service items compatibility
380 // do not add a stock move if it's a non-inventory item
382 $date = date2sql($date_);
384 $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, trans_no, type, loc_code,
385 tran_date, person_id, reference, qty, standard_cost, visible, price,
386 discount_percent) VALUES (".db_escape($stock_id)
387 .", ".db_escape($trans_no).", ".db_escape($type)
388 .", ".db_escape($location).", '$date', "
389 .db_escape($person_id).", ".db_escape($reference).", "
390 .db_escape($quantity).", ".db_escape($std_cost).","
391 .db_escape($show_or_hide).", ".db_escape($price).", "
392 .db_escape($discount_percent).")";
394 if ($error_msg == "")
395 $error_msg = "The stock movement record cannot be inserted";
397 db_query($sql, $error_msg);
399 return db_insert_id();
402 //Chaitanya : Added function
403 function update_stock_move($type, $trans_no, $stock_id, $cost)
405 $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost)
406 ." WHERE type=".db_escape($type)
407 ." AND trans_no=".db_escape($trans_no)
408 ." AND stock_id=".db_escape($stock_id);
409 db_query($sql, "The stock movement standard_cost cannot be updated");
412 function update_stock_move_pid($type, $type_no, $stock_id, $pid, $cost)
414 $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=".db_escape($cost)
415 ." WHERE type=".db_escape($type)
416 ." AND trans_no=".db_escape($type_no)
417 ." AND stock_id=".db_escape($stock_id)
418 ." AND person_id = ".db_escape($pid);
419 db_query($sql, "The stock movement standard_cost cannot be updated");
422 //--------------------------------------------------------------------------------------------------
424 function get_stock_moves($type, $type_no, $visible=false)
426 $sql = "SELECT ".TB_PREF."stock_moves.*, ".TB_PREF."stock_master.description, "
427 .TB_PREF."stock_master.units,".TB_PREF."locations.location_name,"
428 .TB_PREF."stock_master.material_cost + "
429 .TB_PREF."stock_master.labour_cost + "
430 .TB_PREF."stock_master.overhead_cost AS FixedStandardCost
431 FROM ".TB_PREF."stock_moves,".TB_PREF."locations,".TB_PREF."stock_master
432 WHERE ".TB_PREF."stock_moves.stock_id = ".TB_PREF."stock_master.stock_id
433 AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code
434 AND type=".db_escape($type)." AND trans_no=".db_escape($type_no)." ORDER BY trans_id";
436 $sql .= " AND ".TB_PREF."stock_moves.visible=1";
438 return db_query($sql, "Could not get stock moves");
441 //--------------------------------------------------------------------------------------------------
443 function void_stock_move($type, $type_no)
445 //Chaitanya : Reversing stock move rather than voiding as it is hazardous to lose stock movement trail with respect to costing
446 /*$sql = "UPDATE ".TB_PREF."stock_moves SET qty=0, price=0, discount_percent=0,
447 standard_cost=0 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
449 db_query($sql, "Could not void stock moves"); */
451 $sql = "SELECT * from ".TB_PREF."stock_moves WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
452 $result = db_query($sql, "Could not void stock moves");
453 while ($row = db_fetch($result))
455 //Skip cost averaging of service items
456 if (is_inventory_item($row["stock_id"]))
458 // The cost has to be adjusted.
459 //Chaitanya : Transaction rates are stored either as price or standard_cost depending
461 $types = array(ST_SUPPCREDIT);
462 if (in_array($type,$types))
463 $trans_rate = $row["price"];
465 $trans_rate = $row["standard_cost"];
467 update_average_material_cost(0, $row["stock_id"],
468 $trans_rate, -$row["qty"], sql2date($row["tran_date"]));
471 //Post stock move for service items also
472 add_stock_move($type, $row["stock_id"], $type_no, $row["loc_code"],
473 sql2date($row["tran_date"]), $row["reference"], -$row["qty"]
474 , $row["standard_cost"], $row["person_id"], $row["visible"],
475 $row["price"], $row["discount_percent"]);
479 //--------------------------------------------------------------------------------------------------
481 function get_location_name($loc_code)
483 $sql = "SELECT location_name FROM ".TB_PREF."locations WHERE loc_code="
484 .db_escape($loc_code);
486 $result = db_query($sql, "could not retreive the location name for $loc_code");
488 if (db_num_rows($result) == 1)
490 $row = db_fetch_row($result);
494 display_db_error("could not retreive the location name for $loc_code", $sql, true);
497 //--------------------------------------------------------------------------------------------------