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_ITEMSANALYTIC';
13 // ----------------------------------------------------------------
17 // Title: Inventory Planning
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/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
26 include_once($path_to_root . "/includes/db/manufacturing_db.inc");
28 //----------------------------------------------------------------------------------------------------
30 print_inventory_planning();
32 function getTransactions($category, $location)
34 $sql = "SELECT item.category_id,
35 category.description AS cat_description,
37 item.description, item.inactive,
38 IF(move.stock_id IS NULL, '', move.loc_code) AS loc_code,
39 SUM(IF(move.stock_id IS NULL, 0, move.qty)) AS qty_on_hand
40 FROM (".TB_PREF."stock_master item,"
41 .TB_PREF."stock_category category)
42 LEFT JOIN ".TB_PREF."stock_moves move ON item.stock_id=move.stock_id
43 WHERE item.category_id=category.category_id
44 AND (item.mb_flag='B' OR item.mb_flag='M')";
46 $sql .= " AND item.category_id = ".db_escape($category);
47 if ($location != 'all')
48 $sql .= " AND IF(move.stock_id IS NULL, '1=1',move.loc_code = ".db_escape($location).")";
49 $sql .= " GROUP BY item.category_id,
53 ORDER BY item.category_id,
56 return db_query($sql,"No transactions were returned");
60 function getPeriods($stockid, $location)
62 $date5 = date('Y-m-d');
63 $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
64 $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
65 $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
66 $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
67 $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
69 $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
70 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
71 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
72 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
73 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
74 FROM ".TB_PREF."stock_moves
75 WHERE stock_id='$stockid'
76 AND loc_code ='$location'
77 AND (type=13 OR type=11)";
79 $TransResult = db_query($sql,"No transactions were returned");
80 return db_fetch($TransResult);
83 //----------------------------------------------------------------------------------------------------
85 function print_inventory_planning()
89 $category = $_POST['PARAM_0'];
90 $location = $_POST['PARAM_1'];
91 $comments = $_POST['PARAM_2'];
92 $orientation = $_POST['PARAM_3'];
93 $destination = $_POST['PARAM_4'];
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);
107 if ($location == ALL_TEXT)
109 if ($location == 'all')
112 $loc = get_location_name($location);
114 $cols = array(0, 50, 150, 180, 210, 240, 270, 300, 330, 390, 435, 480, 525);
116 $per0 = strftime('%b',mktime(0,0,0,date('m'),1,date('Y')));
117 $per1 = strftime('%b',mktime(0,0,0,date('m')-1,1,date('Y')));
118 $per2 = strftime('%b',mktime(0,0,0,date('m')-2,1,date('Y')));
119 $per3 = strftime('%b',mktime(0,0,0,date('m')-3,1,date('Y')));
120 $per4 = strftime('%b',mktime(0,0,0,date('m')-4,1,date('Y')));
122 $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
123 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
125 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
126 'right', 'right', 'right', 'right');
128 $params = array( 0 => $comments,
129 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
130 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
132 $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning", user_pagesize(), 9, $orientation);
133 if ($orientation == 'L')
134 recalculate_cols($cols);
137 $rep->Info($params, $cols, $headers, $aligns);
140 $res = getTransactions($category, $location);
142 while ($trans=db_fetch($res))
144 if ($catt != $trans['cat_description'])
148 $rep->Line($rep->row - 2);
151 $rep->TextCol(0, 1, $trans['category_id']);
152 $rep->TextCol(1, 2, $trans['cat_description']);
153 $catt = $trans['cat_description'];
156 if ($location == 'all')
159 $loc_code = $location;
160 $custqty = get_demand_qty($trans['stock_id'], $loc_code);
161 $custqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
162 $suppqty = get_on_porder_qty($trans['stock_id'], $loc_code);
163 $suppqty += get_on_worder_qty($trans['stock_id'], $loc_code);
164 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
166 $dec = get_qty_dec($trans['stock_id']);
167 $rep->TextCol(0, 1, $trans['stock_id']);
168 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
169 $rep->AmountCol(2, 3, $period['prd0'], $dec);
170 $rep->AmountCol(3, 4, $period['prd1'], $dec);
171 $rep->AmountCol(4, 5, $period['prd2'], $dec);
172 $rep->AmountCol(5, 6, $period['prd3'], $dec);
173 $rep->AmountCol(6, 7, $period['prd4'], $dec);
175 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
176 $IdealStockHolding = $MaxMthSales * 3;
177 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
179 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
180 $rep->AmountCol(9, 10, $custqty, $dec);
181 $rep->AmountCol(10, 11, $suppqty, $dec);
183 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
184 if ($SuggestedTopUpOrder < 0.0)
185 $SuggestedTopUpOrder = 0.0;
186 $rep->AmountCol(11, 12, $SuggestedTopUpOrder, $dec);
188 $rep->Line($rep->row - 4);