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' AND mb_flag <>'F'";
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 inventory_movements()
87 $from_date = $_POST['PARAM_0'];
88 $to_date = $_POST['PARAM_1'];
89 $category = $_POST['PARAM_2'];
90 $location = $_POST['PARAM_3'];
91 $comments = $_POST['PARAM_4'];
92 $orientation = $_POST['PARAM_5'];
93 $destination = $_POST['PARAM_6'];
95 include_once($path_to_root . "/reporting/includes/excel_report.inc");
97 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
99 $orientation = ($orientation ? 'L' : 'P');
100 if ($category == ALL_NUMERIC)
105 $cat = get_category_name($category);
110 $loc = get_location_name($location);
112 $cols = array(0, 60, 220, 240, 310, 380, 450, 520);
114 $headers = array(_('Category'), _('Description'), _('UOM'), _('Opening'), _('Quantity In'), _('Quantity Out'), _('Balance'));
116 $aligns = array('left', 'left', 'left', 'right', 'right', 'right','right');
118 $params = array( 0 => $comments,
119 1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
120 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
121 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
123 $rep = new FrontReport(_('Inventory Movements'), "InventoryMovements", user_pagesize(), 9, $orientation);
124 if ($orientation == 'L')
125 recalculate_cols($cols);
128 $rep->Info($params, $cols, $headers, $aligns);
131 $result = fetch_items($category);
134 while ($myrow=db_fetch($result))
136 if ($catgor != $myrow['description'])
138 $rep->Line($rep->row - $rep->lineHeight);
141 $rep->TextCol(0, 3, $myrow['category_id'] . " - " . $myrow['description']);
142 $catgor = $myrow['description'];
147 $rep->TextCol(0, 1, $myrow['stock_id']);
148 $rep->TextCol(1, 2, $myrow['name']);
149 $rep->TextCol(2, 3, $myrow['units']);
150 $qoh_start= $inward = $outward = $qoh_end = 0;
152 $qoh_start += get_qoh_on_date($myrow['stock_id'], $location, add_days($from_date, -1));
153 $qoh_end += get_qoh_on_date($myrow['stock_id'], $location, $to_date);
155 $inward += trans_qty($myrow['stock_id'], $location, $from_date, $to_date);
156 $outward += trans_qty($myrow['stock_id'], $location, $from_date, $to_date, false);
158 $rep->AmountCol(3, 4, $qoh_start, get_qty_dec($myrow['stock_id']));
159 $rep->AmountCol(4, 5, $inward, get_qty_dec($myrow['stock_id']));
160 $rep->AmountCol(5, 6, $outward, get_qty_dec($myrow['stock_id']));
161 $rep->AmountCol(6, 7, $qoh_end, get_qty_dec($myrow['stock_id']));
165 $rep->Line($rep->row - 4);