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 ".TB_PREF."stock_master.category_id,
35 ".TB_PREF."stock_category.description AS cat_description,
36 ".TB_PREF."stock_master.stock_id,
37 ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
38 IF(".TB_PREF."stock_moves.stock_id IS NULL, '', ".TB_PREF."stock_moves.loc_code) AS loc_code,
39 SUM(IF(".TB_PREF."stock_moves.stock_id IS NULL,0,".TB_PREF."stock_moves.qty)) AS qty_on_hand
40 FROM (".TB_PREF."stock_master,
41 ".TB_PREF."stock_category)
42 LEFT JOIN ".TB_PREF."stock_moves ON
43 (".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id)
44 WHERE ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
45 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
47 $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
48 if ($location != 'all')
49 $sql .= " AND IF(".TB_PREF."stock_moves.stock_id IS NULL, '1=1',".TB_PREF."stock_moves.loc_code = ".db_escape($location).")";
50 $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
51 ".TB_PREF."stock_category.description,
52 ".TB_PREF."stock_master.stock_id,
53 ".TB_PREF."stock_master.description
54 ORDER BY ".TB_PREF."stock_master.category_id,
55 ".TB_PREF."stock_master.stock_id";
57 return db_query($sql,"No transactions were returned");
61 function getPeriods($stockid, $location)
63 $date5 = date('Y-m-d');
64 $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
65 $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
66 $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
67 $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
68 $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
70 $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
71 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
72 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
73 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
74 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
75 FROM ".TB_PREF."stock_moves
76 WHERE stock_id='$stockid'
77 AND loc_code ='$location'
78 AND (type=13 OR type=11)
81 $TransResult = db_query($sql,"No transactions were returned");
82 return db_fetch($TransResult);
85 //----------------------------------------------------------------------------------------------------
87 function print_inventory_planning()
91 $category = $_POST['PARAM_0'];
92 $location = $_POST['PARAM_1'];
93 $comments = $_POST['PARAM_2'];
94 $destination = $_POST['PARAM_3'];
96 include_once($path_to_root . "/reporting/includes/excel_report.inc");
98 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
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());
135 $rep->Info($params, $cols, $headers, $aligns);
138 $res = getTransactions($category, $location);
140 while ($trans=db_fetch($res))
142 if ($catt != $trans['cat_description'])
146 $rep->Line($rep->row - 2);
149 $rep->TextCol(0, 1, $trans['category_id']);
150 $rep->TextCol(1, 2, $trans['cat_description']);
151 $catt = $trans['cat_description'];
154 if ($location == 'all')
157 $loc_code = $location;
158 $custqty = get_demand_qty($trans['stock_id'], $loc_code);
159 $custqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
160 $suppqty = get_on_porder_qty($trans['stock_id'], $loc_code);
161 $suppqty += get_on_worder_qty($trans['stock_id'], $loc_code);
162 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
164 $dec = get_qty_dec($trans['stock_id']);
165 $rep->TextCol(0, 1, $trans['stock_id']);
166 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
167 $rep->AmountCol(2, 3, $period['prd0'], $dec);
168 $rep->AmountCol(3, 4, $period['prd1'], $dec);
169 $rep->AmountCol(4, 5, $period['prd2'], $dec);
170 $rep->AmountCol(5, 6, $period['prd3'], $dec);
171 $rep->AmountCol(6, 7, $period['prd4'], $dec);
173 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
174 $IdealStockHolding = $MaxMthSales * 3;
175 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
177 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
178 $rep->AmountCol(9, 10, $custqty, $dec);
179 $rep->AmountCol(10, 11, $suppqty, $dec);
181 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
182 if ($SuggestedTopUpOrder < 0.0)
183 $SuggestedTopUpOrder = 0.0;
184 $rep->AmountCol(11, 12, $SuggestedTopUpOrder, $dec);
186 $rep->Line($rep->row - 4);