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 $page_security = 'SA_ITEMSVALREP';
13 // ----------------------------------------------------------------
15 // Creator: Jujuk, Joe Hunt
17 // Title: Stock Movements
18 // ----------------------------------------------------------------
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/ui/ui_input.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
27 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
29 //----------------------------------------------------------------------------------------------------
31 inventory_movements();
33 function get_domestic_price($myrow, $stock_id, $qty, $old_std_cost, $old_qty)
35 if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
37 $price = $myrow['price'];
38 if ($myrow['type'] == ST_SUPPRECEIVE)
40 // Has the supplier invoice increased the receival price?
41 $sql = "SELECT DISTINCT act_price FROM ".TB_PREF."purch_order_details pod INNER JOIN ".TB_PREF."grn_batch grn ON pod.order_no =
42 grn.purch_order_no WHERE grn.id = ".$myrow['trans_no']." AND pod.item_code = '$stock_id'";
43 $result = db_query($sql, "Could not retrieve act_price from purch_order_details");
44 $row = db_fetch_row($result);
45 if ($row[0] > 0 AND $row[0] <> $myrow['price'])
48 if ($myrow['person_id'] > 0)
50 // Do we have foreign currency?
51 $supp = get_supplier($myrow['person_id']);
52 $currency = $supp['curr_code'];
53 $ex_rate = get_exchange_rate_to_home_currency($currency, sql2date($myrow['tran_date']));
57 elseif ($myrow['type'] != ST_INVADJUST) // calcutale the price from avg. price
58 $price = ($myrow['standard_cost'] * $qty - $old_std_cost * $old_qty) / $myrow['qty'];
60 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
64 function fetch_items($category=0)
66 $sql = "SELECT stock_id, stock.description AS name,
67 stock.category_id,units,
69 FROM ".TB_PREF."stock_master stock LEFT JOIN ".TB_PREF."stock_category cat ON stock.category_id=cat.category_id
70 WHERE mb_flag <> 'D'";
72 $sql .= " AND cat.category_id = ".db_escape($category);
73 $sql .= " ORDER BY stock.category_id, stock_id";
75 return db_query($sql,"No transactions were returned");
78 function trans_qty($stock_id, $location=null, $from_date, $to_date, $inward = true)
80 if ($from_date == null)
83 $from_date = date2sql($from_date);
88 $to_date = date2sql($to_date);
90 $sql = "SELECT ".($inward ? '' : '-')."SUM(qty) FROM ".TB_PREF."stock_moves
91 WHERE stock_id=".db_escape($stock_id)."
92 AND tran_date >= '$from_date'
93 AND tran_date <= '$to_date' AND type <> ".ST_LOCTRANSFER;
96 $sql .= " AND loc_code = ".db_escape($location);
99 $sql .= " AND qty > 0 ";
101 $sql .= " AND qty < 0 ";
103 $result = db_query($sql, "QOH calculation failed");
105 $myrow = db_fetch_row($result);
111 function avg_unit_cost($stock_id, $location=null, $to_date)
113 if ($to_date == null)
116 $to_date = date2sql($to_date);
118 $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
119 WHERE stock_id=".db_escape($stock_id)."
120 AND tran_date < '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
123 $sql .= " AND loc_code = ".db_escape($location);
124 $sql .= " ORDER BY tran_date";
126 $result = db_query($sql, "No standard cost transactions were returned");
127 if ($result == false)
129 $qty = $old_qty = $count = $old_std_cost = $tot_cost = 0;
130 while ($row=db_fetch($result))
134 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
136 $old_std_cost = $row['standard_cost'];
143 return $tot_cost / $count;
146 //----------------------------------------------------------------------------------------------------
148 function trans_qty_unit_cost($stock_id, $location=null, $from_date, $to_date, $inward = true)
150 if ($from_date == null)
151 $from_date = Today();
153 $from_date = date2sql($from_date);
155 if ($to_date == null)
158 $to_date = date2sql($to_date);
160 $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
161 WHERE stock_id=".db_escape($stock_id)."
162 AND tran_date <= '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
165 $sql .= " AND loc_code = ".db_escape($location);
168 $sql .= " AND qty > 0 ";
170 $sql .= " AND qty < 0 ";
171 $sql .= " ORDER BY tran_date";
172 $result = db_query($sql, "No standard cost transactions were returned");
173 if ($result == false)
175 $qty = $count = $old_qty = $old_std_cost = $tot_cost = 0;
176 while ($row=db_fetch($result))
180 $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty);
182 if (strncmp($row['tran_date'], $from_date,10) >= 0)
188 $old_std_cost = $row['standard_cost'];
193 return $tot_cost / $count;
197 //----------------------------------------------------------------------------------------------------
199 function inventory_movements()
201 global $path_to_root;
203 $from_date = $_POST['PARAM_0'];
204 $to_date = $_POST['PARAM_1'];
205 $category = $_POST['PARAM_2'];
206 $location = $_POST['PARAM_3'];
207 $comments = $_POST['PARAM_4'];
208 $orientation = $_POST['PARAM_5'];
209 $destination = $_POST['PARAM_6'];
211 include_once($path_to_root . "/reporting/includes/excel_report.inc");
213 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
215 $orientation = ($orientation ? 'L' : 'P');
216 if ($category == ALL_NUMERIC)
221 $cat = get_category_name($category);
226 $loc = get_location_name($location);
228 $cols = array(0, 60, 130, 160, 185, 215, 250, 275, 305, 340, 365, 395, 430, 455, 485, 520);
230 $headers = array(_('Category'), _('Description'), _('UOM'), '', '', _('OpeningStock'), '', '',_('StockIn'), '', '', _('Delivery'), '', '', _('ClosingStock'));
231 $headers2 = array("", "", "", _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"));
233 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right','right' ,'right', 'right', 'right','right', 'right', 'right', 'right');
235 $params = array( 0 => $comments,
236 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
237 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
238 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
240 $rep = new FrontReport(_('Costed Inventory Movements'), "CostedInventoryMovements", user_pagesize(), 8, $orientation);
241 if ($orientation == 'L')
242 recalculate_cols($cols);
245 $rep->Info($params, $cols, $headers2, $aligns, $cols, $headers, $aligns);
248 $totval_open = $totval_in = $totval_out = $totval_close = 0;
249 $result = fetch_items($category);
251 $dec = user_price_dec();
253 while ($myrow=db_fetch($result))
255 if ($catgor != $myrow['description'])
259 $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
260 $catgor = $myrow['description'];
264 $qoh_start = get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
265 $qoh_end = get_qoh_on_date($myrow['stock_id'], $location, $to_date);
267 $inward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
268 $outward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
269 $openCost = avg_unit_cost($myrow['stock_id'], $location, $from_date);
270 $unitCost = avg_unit_cost($myrow['stock_id'], $location, add_days($to_date, 1));
271 if ($qoh_start == 0 && $inward == 0 && $outward == 0 && $qoh_end == 0)
274 $rep->TextCol(0, 1, $myrow['stock_id']);
275 $rep->TextCol(1, 2, $myrow['name']);
276 $rep->TextCol(2, 3, $myrow['units']);
277 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
278 $rep->AmountCol(4, 5, $openCost, $dec);
279 $openCost *= $qoh_start;
280 $totval_open += $openCost;
281 $rep->AmountCol(5, 6, $openCost);
284 $rep->AmountCol(6, 7, $inward, get_qty_dec($myrow['stock_id']));
285 $unitCost_in = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date);
286 $rep->AmountCol(7, 8, $unitCost_in,$dec);
287 $unitCost_in *= $inward;
288 $totval_in += $unitCost_in;
289 $rep->AmountCol(8, 9, $unitCost_in);
293 $rep->AmountCol(9, 10, $outward, get_qty_dec($myrow['stock_id']));
294 $unitCost_out = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date, false);
295 $rep->AmountCol(10, 11, $unitCost_out,$dec);
296 $unitCost_out *= $outward;
297 $totval_out += $unitCost_out;
298 $rep->AmountCol(11, 12, $unitCost_out);
301 $rep->AmountCol(12, 13, $qoh_end, get_qty_dec($myrow['stock_id']));
302 $rep->AmountCol(13, 14, $unitCost,$dec);
303 $unitCost *= $qoh_end;
304 $totval_close += $unitCost;
305 $rep->AmountCol(14, 15, $unitCost);
309 $rep->Line($rep->row - 4);
311 $rep->TextCol(0, 1, _("Total"));
312 $rep->AmountCol(5, 6, $totval_open);
313 $rep->AmountCol(8, 9, $totval_in);
314 $rep->AmountCol(11, 12, $totval_out);
315 $rep->AmountCol(14, 15, $totval_close);
316 $rep->Line($rep->row - 4);