2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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 // trial_inquiry_controls();
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,
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 OR ".TB_PREF."stock_master.stock_id IS NULL)
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 = '$category'";
48 if ($location != 'all')
49 $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$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 getCustQty($stockid, $location)
63 $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS qty_demand
64 FROM ".TB_PREF."sales_order_details,
65 ".TB_PREF."sales_orders
66 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND ";
68 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
69 $sql .= TB_PREF."sales_order_details.stk_code = '$stockid'";
71 $TransResult = db_query($sql,"No transactions were returned");
72 $DemandRow = db_fetch($TransResult);
73 return $DemandRow['qty_demand'];
76 function getCustAsmQty($stockid, $location)
78 $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
80 FROM ".TB_PREF."sales_order_details,
81 ".TB_PREF."sales_orders,
83 ".TB_PREF."stock_master
84 WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
85 ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
87 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
88 $sql .= TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
89 ".TB_PREF."bom.component='$stockid' AND
90 ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
91 ".TB_PREF."stock_master.mb_flag='A'";
93 $TransResult = db_query($sql,"No transactions were returned");
94 if (db_num_rows($TransResult) == 1)
96 $DemandRow = db_fetch_row($TransResult);
97 $DemandQty = $DemandRow[0];
105 function getSuppQty($stockid, $location)
107 $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - ".TB_PREF."purch_order_details.quantity_received) AS QtyOnOrder
108 FROM ".TB_PREF."purch_order_details,
109 ".TB_PREF."purch_orders
110 WHERE ".TB_PREF."purch_order_details.order_no = ".TB_PREF."purch_orders.order_no
111 AND ".TB_PREF."purch_order_details.item_code = '$stockid'";
113 $sql .= " AND ".TB_PREF."purch_orders.into_stock_location= '$location'";
115 $TransResult = db_query($sql,"No transactions were returned");
116 $DemandRow = db_fetch($TransResult);
117 return $DemandRow['QtyOnOrder'];
120 function getPeriods($stockid, $location)
122 $date5 = date('Y-m-d');
123 $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
124 $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
125 $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
126 $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
127 $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
129 $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
130 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
131 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
132 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
133 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
134 FROM ".TB_PREF."stock_moves
135 WHERE stock_id='$stockid'
136 AND loc_code ='$location'
137 AND (type=13 OR type=11)
140 $TransResult = db_query($sql,"No transactions were returned");
141 return db_fetch($TransResult);
144 //----------------------------------------------------------------------------------------------------
146 function print_inventory_planning()
148 global $path_to_root;
150 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
152 $category = $_POST['PARAM_0'];
153 $location = $_POST['PARAM_1'];
154 $comments = $_POST['PARAM_2'];
156 if ($category == reserved_words::get_all_numeric())
161 $cat = get_category_name($category);
163 if ($location == reserved_words::get_all())
165 if ($location == 'all')
170 $cols = array(0, 50, 150, 180, 210, 240, 270, 300, 330, 390, 435, 480, 525);
172 $per0 = strftime('%b',mktime(0,0,0,date('m'),1,date('Y')));
173 $per1 = strftime('%b',mktime(0,0,0,date('m')-1,1,date('Y')));
174 $per2 = strftime('%b',mktime(0,0,0,date('m')-2,1,date('Y')));
175 $per3 = strftime('%b',mktime(0,0,0,date('m')-3,1,date('Y')));
176 $per4 = strftime('%b',mktime(0,0,0,date('m')-4,1,date('Y')));
178 $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
179 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
181 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
182 'right', 'right', 'right', 'right');
184 $params = array( 0 => $comments,
185 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
186 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
188 $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning.pdf", user_pagesize());
191 $rep->Info($params, $cols, $headers, $aligns);
194 $res = getTransactions($category, $location);
196 while ($trans=db_fetch($res))
198 if ($catt != $trans['cat_description'])
202 $rep->Line($rep->row - 2);
205 $rep->TextCol(0, 1, $trans['category_id']);
206 $rep->TextCol(1, 2, $trans['cat_description']);
207 $catt = $trans['cat_description'];
210 if ($location == 'all')
213 $loc_code = $trans['loc_code'];
214 $custqty = getCustQty($trans['stock_id'], $loc_code);
215 $custqty += getCustAsmQty($trans['stock_id'], $loc_code);
216 $suppqty = getSuppQty($trans['stock_id'], $loc_code);
217 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
219 $dec = get_qty_dec($trans['stock_id']);
220 $rep->TextCol(0, 1, $trans['stock_id']);
221 $rep->TextCol(1, 2, $trans['description']);
222 $rep->TextCol(2, 3, number_format2($period['prd0'], $dec));
223 $rep->TextCol(3, 4, number_format2($period['prd1'], $dec));
224 $rep->TextCol(4, 5, number_format2($period['prd2'], $dec));
225 $rep->TextCol(5, 6, number_format2($period['prd3'], $dec));
226 $rep->TextCol(6, 7, number_format2($period['prd4'], $dec));
228 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
229 $IdealStockHolding = $MaxMthSales * 3;
230 $rep->TextCol(7, 8, number_format2($IdealStockHolding, $dec));
232 $rep->TextCol(8, 9, number_format2($trans['qty_on_hand'], $dec));
233 $rep->TextCol(9, 10, number_format2($custqty, $dec));
234 $rep->TextCol(10, 11, number_format2($suppqty, $dec));
236 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
237 if ($SuggestedTopUpOrder < 0.0)
238 $SuggestedTopUpOrder = 0.0;
239 $rep->TextCol(11, 12, number_format2($SuggestedTopUpOrder, $dec));
241 $rep->Line($rep->row - 4);