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 // ----------------------------------------------------------------
17 // Title: Costed Inventory 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)
35 if ($myrow['type'] == ST_SUPPRECEIVE || $myrow['type'] == ST_SUPPCREDIT)
37 $price = $myrow['price'];
38 if ($myrow['person_id'] > 0)
40 // Do we have foreign currency?
41 $supp = get_supplier($myrow['person_id']);
42 $currency = $supp['curr_code'];
43 $ex_rate = get_exchange_rate_to_home_currency($currency, sql2date($myrow['tran_date']));
48 $price = $myrow['standard_cost']; // Item Adjustments just have the real cost
52 function fetch_items($category=0)
54 $sql = "SELECT stock_id, stock.description AS name,
55 stock.category_id,units,
57 FROM ".TB_PREF."stock_master stock LEFT JOIN ".TB_PREF."stock_category cat ON stock.category_id=cat.category_id
58 WHERE mb_flag <> 'D'";
60 $sql .= " AND cat.category_id = ".db_escape($category);
61 $sql .= " ORDER BY stock.category_id, stock_id";
62 return db_query($sql,"No transactions were returned");
65 function trans_qty($stock_id, $location=null, $from_date, $to_date, $inward = true)
67 if ($from_date == null)
70 $from_date = date2sql($from_date);
75 $to_date = date2sql($to_date);
77 $sql = "SELECT ".($inward ? '' : '-')."SUM(qty) FROM ".TB_PREF."stock_moves
78 WHERE stock_id=".db_escape($stock_id)."
79 AND tran_date >= '$from_date'
80 AND tran_date <= '$to_date' AND type <> ".ST_LOCTRANSFER;
83 $sql .= " AND loc_code = ".db_escape($location);
86 $sql .= " AND qty > 0 ";
88 $sql .= " AND qty < 0 ";
90 $result = db_query($sql, "QOH calculation failed");
92 $myrow = db_fetch_row($result);
97 function avg_unit_cost($stock_id, $location=null, $to_date)
102 $to_date = date2sql($to_date);
104 $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
105 WHERE stock_id=".db_escape($stock_id)."
106 AND tran_date < '$to_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
109 $sql .= " AND loc_code = ".db_escape($location);
110 $sql .= " ORDER BY tran_date";
112 $result = db_query($sql, "No standard cost transactions were returned");
114 if ($result == false)
117 $qty = $tot_cost = 0;
118 while ($row=db_fetch($result))
121 $price = get_domestic_price($row, $stock_id);
122 $tran_cost = $price * $row['qty'];
123 $tot_cost += $tran_cost;
126 return $tot_cost/ $qty;
128 return 0; //by Faisal
131 //----------------------------------------------------------------------------------------------------
133 function trans_qty_unit_cost($stock_id, $location=null, $from_date, $to_date, $inward = true)
135 if ($from_date == null)
136 $from_date = Today();
138 $from_date = date2sql($from_date);
140 if ($to_date == null)
143 $to_date = date2sql($to_date);
145 $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM ".TB_PREF."stock_moves
146 WHERE stock_id=".db_escape($stock_id)."
147 AND tran_date <= '$to_date' AND tran_date >= '$from_date' AND standard_cost > 0.001 AND qty <> 0 AND type <> ".ST_LOCTRANSFER;
150 $sql .= " AND loc_code = ".db_escape($location);
153 $sql .= " AND qty > 0 ";
155 $sql .= " AND qty < 0 ";
156 $sql .= " ORDER BY tran_date";
158 $result = db_query($sql, "No standard cost transactions were returned");
160 if ($result == false)
163 $qty = $tot_cost = 0;
164 while ($row=db_fetch($result))
167 $price = get_domestic_price($row, $stock_id);
168 $tran_cost = $row['qty'] * $price;
169 $tot_cost += $tran_cost;
172 return $tot_cost/ $qty;
174 return 0; //by Faisal
177 //----------------------------------------------------------------------------------------------------
179 function inventory_movements()
181 global $path_to_root;
183 $from_date = $_POST['PARAM_0'];
184 $to_date = $_POST['PARAM_1'];
185 $category = $_POST['PARAM_2'];
186 $location = $_POST['PARAM_3'];
187 $comments = $_POST['PARAM_4'];
189 $orientation = $_POST['PARAM_5'];
190 $destination = $_POST['PARAM_6'];
192 include_once($path_to_root . "/reporting/includes/excel_report.inc");
194 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
195 $orientation = ($orientation ? 'L' : 'P');
196 if ($category == ALL_NUMERIC)
201 $cat = get_category_name($category);
206 $loc = get_location_name($location);
208 $cols = array(0, 60, 130, 160, 185, 215, 250, 275, 305, 340, 365, 395, 430, 455, 485, 520);
210 $headers = array(_('Category'), _('Description'), _('UOM'), '', '', _('OpeningStock'), '', '',_('StockIn'), '', '', _('Delivery'), '', '', _('ClosingStock'));
211 $headers2 = array("", "", "", _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"));
213 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right','right' ,'right', 'right', 'right','right', 'right', 'right', 'right');
215 $params = array( 0 => $comments,
216 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
217 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
218 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
220 $rep = new FrontReport(_('Costed Inventory Movements'), "CostedInventoryMovements", user_pagesize(), 8, $orientation);
221 if ($orientation == 'L')
222 recalculate_cols($cols);
225 $rep->Info($params, $cols, $headers2, $aligns, $cols, $headers, $aligns);
228 $totval_open = $totval_in = $totval_out = $totval_close = 0;
229 $result = fetch_items($category);
231 $dec = user_price_dec();
233 while ($myrow=db_fetch($result))
235 if ($catgor != $myrow['description'])
239 $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
240 $catgor = $myrow['description'];
244 $qoh_start = get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
245 $qoh_end = get_qoh_on_date($myrow['stock_id'], $location, $to_date);
247 $inward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
248 $outward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
249 $openCost = avg_unit_cost($myrow['stock_id'], $location, $from_date);
250 $unitCost = avg_unit_cost($myrow['stock_id'], $location, add_days($to_date, 1));
251 if ($qoh_start == 0 && $inward == 0 && $outward == 0 && $qoh_end == 0)
254 $rep->TextCol(0, 1, $myrow['stock_id']);
255 $rep->TextCol(1, 2, $myrow['name']);
256 $rep->TextCol(2, 3, $myrow['units']);
257 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
258 $rep->AmountCol(4, 5, $openCost, $dec);
259 $openCost *= $qoh_start;
260 $totval_open += $openCost;
261 $rep->AmountCol(5, 6, $openCost);
264 $rep->AmountCol(6, 7, $inward, get_qty_dec($myrow['stock_id']));
265 $unitCost_in = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date);
266 $rep->AmountCol(7, 8, $unitCost_in,$dec);
267 $unitCost_in *= $inward;
268 $totval_in += $unitCost_in;
269 $rep->AmountCol(8, 9, $unitCost_in);
273 $rep->AmountCol(9, 10, $outward, get_qty_dec($myrow['stock_id']));
274 $unitCost_out = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date, false);
275 $rep->AmountCol(10, 11, $unitCost_out,$dec);
276 $unitCost_out *= $outward;
277 $totval_out += $unitCost_out;
278 $rep->AmountCol(11, 12, $unitCost_out);
281 $rep->AmountCol(12, 13, $qoh_end, get_qty_dec($myrow['stock_id']));
282 $rep->AmountCol(13, 14, $unitCost,$dec);
283 $unitCost *= $qoh_end;
284 $totval_close += $unitCost;
285 $rep->AmountCol(14, 15, $unitCost);
289 $rep->Line($rep->row - 4);
291 $rep->TextCol(0, 1, _("Total"));
292 $rep->AmountCol(5, 6, $totval_open);
293 $rep->AmountCol(8, 9, $totval_in);
294 $rep->AmountCol(11, 12, $totval_out);
295 $rep->AmountCol(14, 15, $totval_close);
296 $rep->Line($rep->row - 4);