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 fetch_items($category=0)
35 $sql = "SELECT stock_id, stock.description AS name,
36 stock.category_id,units,
38 FROM ".TB_PREF."stock_master stock LEFT JOIN ".TB_PREF."stock_category cat ON stock.category_id=cat.category_id
39 WHERE mb_flag <> 'D'";
41 $sql .= " AND cat.category_id = ".db_escape($category);
42 $sql .= " ORDER BY stock.category_id, stock_id";
44 return db_query($sql,"No transactions were returned");
47 function trans_qty($stock_id, $location=null, $from_date, $to_date, $inward = true)
49 if ($from_date == null)
52 $from_date = date2sql($from_date);
57 $to_date = date2sql($to_date);
59 $sql = "SELECT ".($inward ? '' : '-')."SUM(qty) FROM ".TB_PREF."stock_moves
60 WHERE stock_id=".db_escape($stock_id)."
61 AND tran_date >= '$from_date'
62 AND tran_date <= '$to_date'";
65 $sql .= " AND loc_code = ".db_escape($location);
68 $sql .= " AND qty > 0 ";
70 $sql .= " AND qty < 0 ";
72 $result = db_query($sql, "QOH calculation failed");
74 $myrow = db_fetch_row($result);
80 function avg_unit_cost($stock_id, $location=null, $to_date)
85 $to_date = date2sql($to_date);
87 $sql = "SELECT AVG (standard_cost) FROM ".TB_PREF."stock_moves
88 WHERE stock_id=".db_escape($stock_id)."
89 AND tran_date < '$to_date'";
92 $sql .= " AND loc_code = ".db_escape($location);
94 $result = db_query($sql, "QOH calculation failed");
96 $myrow = db_fetch_row($result);
102 //----------------------------------------------------------------------------------------------------
104 function trans_qty_unit_cost($stock_id, $location=null, $from_date, $to_date, $inward = true)
106 if ($from_date == null)
107 $from_date = Today();
109 $from_date = date2sql($from_date);
111 if ($to_date == null)
114 $to_date = date2sql($to_date);
116 $sql = "SELECT AVG (standard_cost) FROM ".TB_PREF."stock_moves
117 WHERE stock_id=".db_escape($stock_id)."
118 AND tran_date >= '$from_date'
119 AND tran_date <= '$to_date'";
122 $sql .= " AND loc_code = ".db_escape($location);
125 $sql .= " AND qty > 0 ";
127 $sql .= " AND qty < 0 ";
129 $result = db_query($sql, "QOH calculation failed");
131 $myrow = db_fetch_row($result);
137 //----------------------------------------------------------------------------------------------------
139 function inventory_movements()
141 global $path_to_root;
143 $from_date = $_POST['PARAM_0'];
144 $to_date = $_POST['PARAM_1'];
145 $category = $_POST['PARAM_2'];
146 $location = $_POST['PARAM_3'];
147 $comments = $_POST['PARAM_4'];
148 $orientation = $_POST['PARAM_5'];
149 $destination = $_POST['PARAM_6'];
151 include_once($path_to_root . "/reporting/includes/excel_report.inc");
153 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
155 $orientation = ($orientation ? 'L' : 'P');
156 if ($category == ALL_NUMERIC)
161 $cat = get_category_name($category);
166 $loc = get_location_name($location);
168 $cols = array(0, 60, 130, 160, 185, 215, 250, 275, 305, 340, 365, 395, 430, 455, 485, 520);
170 $headers = array(_('Category'), _('Description'), _('UOM'), '', '', _('OpeningStock'), '', '',_('StockIn'), '', '', _('Delivery'), '', '', _('ClosingStock'));
171 $headers2 = array("", "", "", _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"));
173 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right','right' ,'right', 'right', 'right','right', 'right', 'right', 'right');
175 $params = array( 0 => $comments,
176 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
177 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
178 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
180 $rep = new FrontReport(_('Costed Inventory Movements'), "CostedInventoryMovements", user_pagesize(), 8, $orientation);
181 if ($orientation == 'L')
182 recalculate_cols($cols);
185 $rep->Info($params, $cols, $headers2, $aligns, $cols, $headers, $aligns);
188 $totval_open = $totval_in = $totval_out = $totval_close = 0;
189 $result = fetch_items($category);
191 $dec = user_price_dec();
193 while ($myrow=db_fetch($result))
195 if ($catgor != $myrow['description'])
199 $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
200 $catgor = $myrow['description'];
205 $rep->TextCol(0, 1, $myrow['stock_id']);
206 $rep->TextCol(1, 2, $myrow['name']);
207 $rep->TextCol(2, 3, $myrow['units']);
209 $qoh_start = get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
210 $qoh_end = get_qoh_on_date($myrow['stock_id'], $location, $to_date);
212 $inward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
213 $outward = trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
214 $openCost = avg_unit_cost($myrow['stock_id'], $location, $from_date);
215 $unitCost = avg_unit_cost($myrow['stock_id'], $location, add_days($to_date, 1));
216 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
217 $rep->AmountCol(4, 5, $openCost, $dec);
218 $openCost *= $qoh_start;
219 $totval_open += $openCost;
220 $rep->AmountCol(5, 6, $openCost);
223 $rep->AmountCol(6, 7, $inward, get_qty_dec($myrow['stock_id']));
224 $unitCost_in = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date);
225 $rep->AmountCol(7, 8, $unitCost_in,$dec);
226 $unitCost_in *= $inward;
227 $totval_in += $unitCost_in;
228 $rep->AmountCol(8, 9, $unitCost_in);
232 $rep->AmountCol(9, 10, $outward, get_qty_dec($myrow['stock_id']));
233 $unitCost_out = trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date, false);
234 $rep->AmountCol(10, 11, $unitCost_out,$dec);
235 $unitCost_out *= $outward;
236 $totval_out += $unitCost_out;
237 $rep->AmountCol(11, 12, $unitCost_out);
240 $rep->AmountCol(12, 13, $qoh_end, get_qty_dec($myrow['stock_id']));
241 $rep->AmountCol(13, 14, $unitCost,$dec);
242 $unitCost *= $qoh_end;
243 $totval_close += $unitCost;
244 $rep->AmountCol(14, 15, $unitCost);
248 $rep->Line($rep->row - 4);
250 $rep->TextCol(0, 1, _("Total"));
251 $rep->AmountCol(5, 6, $totval_open);
252 $rep->AmountCol(8, 9, $totval_in);
253 $rep->AmountCol(11, 12, $totval_out);
254 $rep->AmountCol(14, 15, $totval_close);
255 $rep->Line($rep->row - 4);