Replaced the Excel Writer engine with XLS driver (the XML driver can later be downloaded)
[fa-stable.git] / reporting / rep302.php
1 <?php
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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Planning
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
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
27 //----------------------------------------------------------------------------------------------------
28
29 print_inventory_planning();
30
31 function getTransactions($category, $location)
32 {
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')";
45         if ($category != 0)
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";
55
56     return db_query($sql,"No transactions were returned");
57
58 }
59
60 function getCustQty($stockid, $location)
61 {
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 ";
66         if ($location != "")
67                 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
68         $sql .= TB_PREF."sales_order_details.stk_code = '$stockid'";
69
70     $TransResult = db_query($sql,"No transactions were returned");
71         $DemandRow = db_fetch($TransResult);
72         return $DemandRow['qty_demand'];
73 }
74
75 function getCustAsmQty($stockid, $location)
76 {
77         $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
78                                    AS Dem
79                                    FROM ".TB_PREF."sales_order_details,
80                                                 ".TB_PREF."sales_orders,
81                                                 ".TB_PREF."bom,
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 ";
85         if ($location != "")
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'";
91
92     $TransResult = db_query($sql,"No transactions were returned");
93         if (db_num_rows($TransResult) == 1)
94         {
95                 $DemandRow = db_fetch_row($TransResult);
96                 $DemandQty = $DemandRow[0];
97         }
98         else
99                 $DemandQty = 0.0;
100
101     return $DemandQty;
102 }
103
104 function getSuppQty($stockid, $location)
105 {
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'";
111         if ($location != "")                    
112                 $sql .= " AND ".TB_PREF."purch_orders.into_stock_location= '$location'";
113
114     $TransResult = db_query($sql,"No transactions were returned");
115         $DemandRow = db_fetch($TransResult);
116         return $DemandRow['QtyOnOrder'];
117 }
118
119 function getPeriods($stockid, $location)
120 {
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')));
127
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)
137                         AND visible=1";
138
139     $TransResult = db_query($sql,"No transactions were returned");
140         return db_fetch($TransResult);
141 }
142
143 //----------------------------------------------------------------------------------------------------
144
145 function print_inventory_planning()
146 {
147     global $path_to_root;
148
149     $category = $_POST['PARAM_0'];
150     $location = $_POST['PARAM_1'];
151     $comments = $_POST['PARAM_2'];
152         $destination = $_POST['PARAM_3'];
153         if ($destination)
154                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
155         else
156                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
157
158         if ($category == reserved_words::get_all_numeric())
159                 $category = 0;
160         if ($category == 0)
161                 $cat = _('All');
162         else
163                 $cat = get_category_name($category);
164
165         if ($location == reserved_words::get_all())
166                 $location = 'all';
167         if ($location == 'all')
168                 $loc = _('All');
169         else
170                 $loc = $location;
171
172         $cols = array(0, 50, 150, 180, 210, 240, 270, 300, 330, 390, 435, 480, 525);
173
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')));
179
180         $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
181                 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
182
183         $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
184                 'right', 'right', 'right', 'right');
185
186     $params =   array(  0 => $comments,
187                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
188                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
189
190     $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning", user_pagesize());
191
192     $rep->Font();
193     $rep->Info($params, $cols, $headers, $aligns);
194     $rep->Header();
195
196         $res = getTransactions($category, $location);
197         $catt = '';
198         while ($trans=db_fetch($res))
199         {
200                 if ($catt != $trans['cat_description'])
201                 {
202                         if ($catt != '')
203                         {
204                                 $rep->Line($rep->row - 2);
205                                 $rep->NewLine(2, 3);
206                         }
207                         $rep->TextCol(0, 1, $trans['category_id']);
208                         $rep->TextCol(1, 2, $trans['cat_description']);
209                         $catt = $trans['cat_description'];
210                         $rep->NewLine();
211                 }
212                 if ($location == 'all')
213                         $loc_code = "";
214                 else
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']);
220                 $rep->NewLine();
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);
229
230                 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
231                 $IdealStockHolding = $MaxMthSales * 3;
232                 $rep->AmountCol(7, 8, $IdealStockHolding, $dec);
233
234                 $rep->AmountCol(8, 9, $trans['qty_on_hand'], $dec);
235                 $rep->AmountCol(9, 10, $custqty, $dec);
236                 $rep->AmountCol(10, 11, $suppqty, $dec);
237
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);
242         }
243         $rep->Line($rep->row - 4);
244         $rep->NewLine();
245     $rep->End();
246 }
247
248 ?>