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: 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,
39 FROM ".TB_PREF."stock_master stock LEFT JOIN ".TB_PREF."stock_category cat ON stock.category_id=cat.category_id
40 WHERE mb_flag <> 'D'";
42 $sql .= " AND cat.category_id = ".db_escape($category);
43 $sql .= " ORDER BY stock.category_id, stock_id";
45 return db_query($sql,"No transactions were returned");
48 function trans_qty($stock_id, $location=null, $from_date, $to_date, $inward = true)
50 if ($from_date == null)
53 $from_date = date2sql($from_date);
58 $to_date = date2sql($to_date);
60 $sql = "SELECT ".($inward ? '' : '-')."SUM(qty) FROM ".TB_PREF."stock_moves
61 WHERE stock_id=".db_escape($stock_id)."
62 AND tran_date >= '$from_date'
63 AND tran_date <= '$to_date'";
66 $sql .= " AND loc_code = ".db_escape($location);
69 $sql .= " AND qty > 0 ";
71 $sql .= " AND qty < 0 ";
73 $result = db_query($sql, "QOH calculation failed");
75 $myrow = db_fetch_row($result);
81 //----------------------------------------------------------------------------------------------------
83 function trans_qty_unit_cost($stock_id, $location=null, $from_date, $to_date, $inward = true)
85 if ($from_date == null)
88 $from_date = date2sql($from_date);
93 $to_date = date2sql($to_date);
95 $sql = "SELECT AVG (price) FROM ".TB_PREF."stock_moves
96 WHERE stock_id=".db_escape($stock_id)."
97 AND tran_date >= '$from_date'
98 AND tran_date <= '$to_date'";
101 $sql .= " AND loc_code = ".db_escape($location);
104 $sql .= " AND qty > 0 ";
106 $sql .= " AND qty < 0 ";
108 $result = db_query($sql, "QOH calculation failed");
110 $myrow = db_fetch_row($result);
116 //----------------------------------------------------------------------------------------------------
118 function inventory_movements()
120 global $path_to_root;
122 $from_date = $_POST['PARAM_0'];
123 $to_date = $_POST['PARAM_1'];
124 $category = $_POST['PARAM_2'];
125 $location = $_POST['PARAM_3'];
126 $comments = $_POST['PARAM_4'];
127 $orientation = $_POST['PARAM_5'];
128 $destination = $_POST['PARAM_6'];
130 include_once($path_to_root . "/reporting/includes/excel_report.inc");
132 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
134 $orientation = ($orientation ? 'L' : 'P');
135 if ($category == ALL_NUMERIC)
140 $cat = get_category_name($category);
145 $loc = get_location_name($location);
147 $cols = array(0, 60, 130, 160, 185, 210, 250, 275, 300, 340, 365, 390, 430, 455, 480, 520);
149 $headers = array(_('Category'), _('Description'), _('UOM'), '', '', _('OpeningStock'), '', '',_('StockIn'), '', '', _('Delivery'), '', '', _('ClosingStock'));
150 $headers2 = array("", "", "", _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"), _("QTY"), _("Rate"), _("Value"));
152 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right','right' ,'right', 'right', 'right','right', 'right', 'right', 'right');
154 $params = array( 0 => $comments,
155 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
156 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
157 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
159 $rep = new FrontReport(_('Costed Inventory Movements'), "CostedInventoryMovements", user_pagesize(), 8, $orientation);
160 if ($orientation == 'L')
161 recalculate_cols($cols);
164 $rep->Info($params, $cols, $headers2, $aligns, $cols, $headers, $aligns);
167 $result = fetch_items($category);
170 while ($myrow=db_fetch($result))
172 if ($catgor != $myrow['description'])
176 $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
177 $catgor = $myrow['description'];
182 $rep->TextCol(0, 1, $myrow['stock_id']);
183 $rep->TextCol(1, 2, $myrow['name']);
184 $rep->TextCol(2, 3, $myrow['units']);
185 $qoh_start= $inward = $outward = $qoh_end = 0;
187 $qoh_start += get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
188 $qoh_end += get_qoh_on_date($myrow['stock_id'], $location, $to_date);
190 $inward += trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
191 $outward += trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
192 $unitCost=$myrow['material_cost'];
193 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
194 // $rep->AmountCol(4, 5, $unitCost, get_qty_dec($myrow['stock_id']));
195 $rep->AmountCol(4, 5, $myrow['material_cost']);
196 $rep->AmountCol(5, 6, $qoh_start*$unitCost, get_qty_dec($myrow['stock_id']));
199 $rep->AmountCol(6, 7, $inward, get_qty_dec($myrow['stock_id']));
200 $unitCost_IN= trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date);
201 $rep->AmountCol(7, 8, $unitCost_IN,get_qty_dec($myrow['stock_id']));
202 $rep->AmountCol(8, 9, $inward*$unitCost_IN, get_qty_dec($myrow['stock_id']));
206 $rep->AmountCol(9, 10, $outward, get_qty_dec($myrow['stock_id']));
208 $unitCost_out= trans_qty_unit_cost($myrow['stock_id'], $location, $from_date, $to_date, false);
209 $rep->AmountCol(10, 11, $unitCost_out,get_qty_dec($myrow['stock_id']));
210 $rep->AmountCol(11, 12, $outward*$unitCost_out, get_qty_dec($myrow['stock_id']));
213 $rep->AmountCol(12, 13, $qoh_end, get_qty_dec($myrow['stock_id']));
214 $rep->AmountCol(13, 14, $myrow['material_cost'],get_qty_dec($myrow['stock_id']));
215 $rep->AmountCol(14, 15, $qoh_end*$unitCost, get_qty_dec($myrow['stock_id']));
219 $rep->Line($rep->row - 4);