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 ***********************************************************************/
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");
27 //----------------------------------------------------------------------------------------------------
29 print_inventory_planning();
31 function getTransactions($category, $location)
33 $sql = "SELECT ".TB_PREF."stock_master.category_id,
34 ".TB_PREF."stock_category.description AS cat_description,
35 ".TB_PREF."stock_master.stock_id,
36 ".TB_PREF."stock_master.description,
37 IF(".TB_PREF."stock_moves.stock_id IS NULL, '', ".TB_PREF."stock_moves.loc_code) AS loc_code,
38 SUM(IF(".TB_PREF."stock_moves.stock_id IS NULL,0,".TB_PREF."stock_moves.qty)) AS qty_on_hand
39 FROM (".TB_PREF."stock_master,
40 ".TB_PREF."stock_category)
41 LEFT JOIN ".TB_PREF."stock_moves ON
42 (".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id OR ".TB_PREF."stock_master.stock_id IS NULL)
43 WHERE ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
44 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
46 $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
47 if ($location != 'all')
48 $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
49 $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
50 ".TB_PREF."stock_category.description,
51 ".TB_PREF."stock_master.stock_id,
52 ".TB_PREF."stock_master.description
53 ORDER BY ".TB_PREF."stock_master.category_id,
54 ".TB_PREF."stock_master.stock_id";
56 return db_query($sql,"No transactions were returned");
60 function getCustQty($stockid, $location)
62 $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS qty_demand
63 FROM ".TB_PREF."sales_order_details,
64 ".TB_PREF."sales_orders
65 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND ";
67 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
68 $sql .= TB_PREF."sales_order_details.stk_code = '$stockid'";
70 $TransResult = db_query($sql,"No transactions were returned");
71 $DemandRow = db_fetch($TransResult);
72 return $DemandRow['qty_demand'];
75 function getCustAsmQty($stockid, $location)
77 $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
79 FROM ".TB_PREF."sales_order_details,
80 ".TB_PREF."sales_orders,
82 ".TB_PREF."stock_master
83 WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
84 ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
86 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
87 $sql .= TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
88 ".TB_PREF."bom.component='$stockid' AND
89 ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
90 ".TB_PREF."stock_master.mb_flag='A'";
92 $TransResult = db_query($sql,"No transactions were returned");
93 if (db_num_rows($TransResult) == 1)
95 $DemandRow = db_fetch_row($TransResult);
96 $DemandQty = $DemandRow[0];
104 function getSuppQty($stockid, $location)
106 $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - ".TB_PREF."purch_order_details.quantity_received) AS QtyOnOrder
107 FROM ".TB_PREF."purch_order_details,
108 ".TB_PREF."purch_orders
109 WHERE ".TB_PREF."purch_order_details.order_no = ".TB_PREF."purch_orders.order_no
110 AND ".TB_PREF."purch_order_details.item_code = '$stockid'";
112 $sql .= " AND ".TB_PREF."purch_orders.into_stock_location= '$location'";
114 $TransResult = db_query($sql,"No transactions were returned");
115 $DemandRow = db_fetch($TransResult);
116 return $DemandRow['QtyOnOrder'];
119 function getPeriods($stockid, $location)
121 $date5 = date('Y-m-d');
122 $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
123 $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
124 $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
125 $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
126 $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
128 $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
129 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
130 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
131 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
132 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
133 FROM ".TB_PREF."stock_moves
134 WHERE stock_id='$stockid'
135 AND loc_code ='$location'
136 AND (type=13 OR type=11)
139 $TransResult = db_query($sql,"No transactions were returned");
140 return db_fetch($TransResult);
143 //----------------------------------------------------------------------------------------------------
145 function print_inventory_planning()
147 global $path_to_root;
149 $category = $_POST['PARAM_0'];
150 $location = $_POST['PARAM_1'];
151 $comments = $_POST['PARAM_2'];
152 $destination = $_POST['PARAM_3'];
154 include_once($path_to_root . "/reporting/includes/excel_report.inc");
156 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
158 if ($category == reserved_words::get_all_numeric())
163 $cat = get_category_name($category);
165 if ($location == reserved_words::get_all())
167 if ($location == 'all')
172 $cols = array(0, 50, 150, 180, 210, 240, 270, 300, 330, 390, 435, 480, 525);
174 $per0 = strftime('%b',mktime(0,0,0,date('m'),1,date('Y')));
175 $per1 = strftime('%b',mktime(0,0,0,date('m')-1,1,date('Y')));
176 $per2 = strftime('%b',mktime(0,0,0,date('m')-2,1,date('Y')));
177 $per3 = strftime('%b',mktime(0,0,0,date('m')-3,1,date('Y')));
178 $per4 = strftime('%b',mktime(0,0,0,date('m')-4,1,date('Y')));
180 $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
181 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
183 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
184 'right', 'right', 'right', 'right');
186 $params = array( 0 => $comments,
187 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
188 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
190 $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning", user_pagesize());
193 $rep->Info($params, $cols, $headers, $aligns);
196 $res = getTransactions($category, $location);
198 while ($trans=db_fetch($res))
200 if ($catt != $trans['cat_description'])
204 $rep->Line($rep->row - 2);
207 $rep->TextCol(0, 1, $trans['category_id']);
208 $rep->TextCol(1, 2, $trans['cat_description']);
209 $catt = $trans['cat_description'];
212 if ($location == 'all')
215 $loc_code = $trans['loc_code'];
216 $custqty = getCustQty($trans['stock_id'], $loc_code);
217 $custqty += getCustAsmQty($trans['stock_id'], $loc_code);
218 $suppqty = getSuppQty($trans['stock_id'], $loc_code);
219 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
221 $dec = get_qty_dec($trans['stock_id']);
222 $rep->TextCol(0, 1, $trans['stock_id']);
223 $rep->TextCol(1, 2, $trans['description']);
224 $rep->AmountCol(2, 3, $period['prd0'], $dec);
225 $rep->AmountCol(3, 4, $period['prd1'], $dec);
226 $rep->AmountCol(4, 5, $period['prd2'], $dec);
227 $rep->AmountCol(5, 6, $period['prd3'], $dec);
228 $rep->AmountCol(6, 7, $period['prd4'], $dec);
230 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
231 $IdealStockHolding = $MaxMthSales * 3;
232 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
234 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
235 $rep->AmountCol(9, 10, $custqty, $dec);
236 $rep->AmountCol(10, 11, $suppqty, $dec);
238 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
239 if ($SuggestedTopUpOrder < 0.0)
240 $SuggestedTopUpOrder = 0.0;
241 $rep->AmountCol(11, 12, $SuggestedTopUpOrder, $dec);
243 $rep->Line($rep->row - 4);