-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInventoryPlanning.php
438 lines (359 loc) · 16.2 KB
/
InventoryPlanning.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/* $Revision: 1.19 $ */
$PageSecurity = 2;
include('includes/session.inc');
If (isset($_POST['PrintPDF'])
AND isset($_POST['FromCriteria'])
AND strlen($_POST['FromCriteria'])>=1
AND isset($_POST['ToCriteria'])
AND strlen($_POST['ToCriteria'])>=1){
include ('includes/class.pdf.php');
/* A4_Landscape */
$Page_Width=842;
$Page_Height=595;
$Top_Margin=20;
$Bottom_Margin=20;
$Left_Margin=25;
$Right_Margin=22;
$PageSize = array(0,0,$Page_Width,$Page_Height);
$pdf = & new Cpdf($PageSize);
$PageNumber = 0;
$pdf->selectFont('./fonts/Helvetica.afm');
/* Standard PDF file creation header stuff */
$pdf->addinfo('Author','webERP ' . $Version);
$pdf->addinfo('Creator','webERP http://www.weberp.org');
$pdf->addinfo('Title',_('Inventory Planning Report') . ' ' . Date($_SESSION['DefaultDateFormat']));
$line_height=12;
$pdf->addinfo('Subject',_('Inventory Planning'));
$PageNumber=1;
$line_height=12;
/*Now figure out the inventory data to report for the category range under review
need QOH, QOO, QDem, Sales Mth -1, Sales Mth -2, Sales Mth -3, Sales Mth -4*/
if ($_POST['Location']=='All'){
$SQL = "SELECT stockmaster.categoryid,
stockmaster.description,
stockcategory.categorydescription,
locstock.stockid,
SUM(locstock.quantity) AS qoh
FROM locstock,
stockmaster,
stockcategory
WHERE locstock.stockid=stockmaster.stockid
AND stockmaster.categoryid=stockcategory.categoryid
AND (stockmaster.mbflag='B' OR stockmaster.mbflag='M')
AND stockmaster.categoryid >= '" . $_POST['FromCriteria'] . "'
AND stockmaster.categoryid <= '" . $_POST['ToCriteria'] . "'
GROUP BY stockmaster.categoryid,
stockmaster.description,
stockcategory.categorydescription,
locstock.stockid,
stockmaster.stockid
ORDER BY stockmaster.categoryid,
stockmaster.stockid";
} else {
$SQL = "SELECT stockmaster.categoryid,
locstock.stockid,
stockmaster.description,
stockcategory.categorydescription,
locstock.quantity AS qoh
FROM locstock,
stockmaster,
stockcategory
WHERE locstock.stockid=stockmaster.stockid
AND stockmaster.categoryid >= '" . $_POST['FromCriteria'] . "'
AND stockmaster.categoryid=stockcategory.categoryid
AND stockmaster.categoryid <= '" . $_POST['ToCriteria'] . "'
AND (stockmaster.mbflag='B' OR stockmaster.mbflag='M')
AND locstock.loccode = '" . $_POST['Location'] . "'
ORDER BY stockmaster.categoryid,
stockmaster.stockid";
}
$InventoryResult = DB_query($SQL,$db,'','',false,false);
if (DB_error_no($db) !=0) {
$title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg(_('The inventory quantities could not be retrieved by the SQL because') . ' - ' . DB_error_msg($db),'error');
echo "<br><a href='" .$rootpath .'/index.php?' . SID . "'>" . _('Back to the menu') . '</a>';
if ($debug==1){
echo "<br>$SQL";
}
include('includes/footer.inc');
exit;
}
$Period_0_Name = strftime('%b',mktime(0,0,0,Date('m'),Date('d'),Date('Y')));
$Period_1_Name = strftime('%b',mktime(0,0,0,Date('m')-1,Date('d'),Date('Y')));
$Period_2_Name = strftime('%b',mktime(0,0,0,Date('m')-2,Date('d'),Date('Y')));
$Period_3_Name = strftime('%b',mktime(0,0,0,Date('m')-3,Date('d'),Date('Y')));
$Period_4_Name = strftime('%b',mktime(0,0,0,Date('m')-4,Date('d'),Date('Y')));
$Period_5_Name = strftime('%b',mktime(0,0,0,Date('m')-5,Date('d'),Date('Y')));
include ('includes/PDFInventoryPlanPageHeader.inc');
$Category = '';
$CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']),$db);
$Period_1 = $CurrentPeriod -1;
$Period_2 = $CurrentPeriod -2;
$Period_3 = $CurrentPeriod -3;
$Period_4 = $CurrentPeriod -4;
$Period_5 = $CurrentPeriod -5;
While ($InventoryPlan = DB_fetch_array($InventoryResult,$db)){
if ($Category!=$InventoryPlan['categoryid']){
$FontSize=10;
if ($Category!=''){ /*Then it's NOT the first time round */
/*draw a line under the CATEGORY TOTAL*/
$YPos -=$line_height;
$pdf->line($Left_Margin, $YPos,$Page_Width-$Right_Margin, $YPos);
$YPos -=(2*$line_height);
}
$LeftOvers = $pdf->addTextWrap($Left_Margin, $YPos, 260-$Left_Margin,$FontSize,$InventoryPlan['categoryid'] . ' - ' . $InventoryPlan['categorydescription'],'left');
$Category = $InventoryPlan['categoryid'];
$FontSize=8;
}
$YPos -=$line_height;
if ($_POST['Location']=='All'){
$SQL = "SELECT SUM(CASE WHEN prd=" . $CurrentPeriod . " THEN -qty ELSE 0 END) AS prd0,
SUM(CASE WHEN prd=" . $Period_1 . " THEN -qty ELSE 0 END) AS prd1,
SUM(CASE WHEN prd=" . $Period_2 . " THEN -qty ELSE 0 END) AS prd2,
SUM(CASE WHEN prd=" . $Period_3 . " THEN -qty ELSE 0 END) AS prd3,
SUM(CASE WHEN prd=" . $Period_4 . " THEN -qty ELSE 0 END) AS prd4,
SUM(CASE WHEN prd=" . $Period_5 . " THEN -qty ELSE 0 END) AS prd5
FROM stockmoves
WHERE stockid='" . $InventoryPlan['stockid'] . "'
AND (type=10 OR type=11)
AND stockmoves.hidemovt=0";
} else {
$SQL = "SELECT SUM(CASE WHEN prd=" . $CurrentPeriod . " THEN -qty ELSE 0 END) AS prd0,
SUM(CASE WHEN prd=" . $Period_1 . " THEN -qty ELSE 0 END) AS prd1,
SUM(CASE WHEN prd=" . $Period_2 . " THEN -qty ELSE 0 END) AS prd2,
SUM(CASE WHEN prd=" . $Period_3 . " THEN -qty ELSE 0 END) AS prd3,
SUM(CASE WHEN prd=" . $Period_4 . " THEN -qty ELSE 0 END) AS prd4,
SUM(CASE WHEN prd=" . $Period_5 . " THEN -qty ELSE 0 END) AS prd5
FROM stockmoves
WHERE stockid='" . $InventoryPlan['stockid'] . "'
AND stockmoves.loccode ='" . $_POST['Location'] . "'
AND (stockmoves.type=10 OR stockmoves.type=11)
AND stockmoves.hidemovt=0";
}
$SalesResult=DB_query($SQL,$db,'','',FALSE,FALSE);
if (DB_error_no($db) !=0) {
$title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg( _('The sales quantities could not be retrieved by the SQL because') . ' - ' . DB_error_msg($db),'error');
echo "<br><a href='" .$rootpath .'/index.php?' . SID . "'>" . _('Back to the menu') . '</a>';
if ($debug==1){
echo "<br>$SQL";
}
include('includes/footer.inc');
exit;
}
$SalesRow = DB_fetch_array($SalesResult);
if ($_POST['Location']=='All'){
$SQL = "SELECT SUM(salesorderdetails.quantity - salesorderdetails.qtyinvoiced) AS qtydemand
FROM salesorderdetails,
salesorders
WHERE salesorderdetails.orderno=salesorders.orderno
AND salesorderdetails.stkcode = '" . $InventoryPlan['stockid'] . "'
AND salesorderdetails.completed = 0";
} else {
$SQL = "SELECT SUM(salesorderdetails.quantity - salesorderdetails.qtyinvoiced) AS qtydemand
FROM salesorderdetails,
salesorders
WHERE salesorderdetails.orderno=salesorders.orderno
AND salesorders.fromstkloc ='" . $_POST['Location'] . "'
AND salesorderdetails.stkcode = '" . $InventoryPlan['stockid'] . "'
AND salesorderdetails.completed = 0";
}
$DemandResult = DB_query($SQL,$db,'','',FALSE,FALSE);
if (DB_error_no($db) !=0) {
$title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg( _('The sales order demand quantities could not be retrieved by the SQL because') . ' - ' . DB_error_msg($db),'error');
echo "<br><a href='" .$rootpath ."/index.php?" . SID . "'>" . _('Back to the menu') . '</a>';
if ($debug==1){
echo "<br>$SQL";
}
include('includes/footer.inc');
exit;
}
//Also need to add in the demand as a component of an assembly items if this items has any assembly parents.
if ($_POST['Location']=='All'){
$SQL = "SELECT SUM((salesorderdetails.quantity-salesorderdetails.qtyinvoiced)*bom.quantity) AS dem
FROM salesorderdetails,
bom,
stockmaster
WHERE salesorderdetails.stkcode=bom.parent
AND salesorderdetails.quantity-salesorderdetails.qtyinvoiced > 0
AND bom.component='" . $InventoryPlan['stockid'] . "'
AND stockmaster.stockid=bom.parent
AND stockmaster.mbflag='A'
AND salesorderdetails.completed=0";
} else {
$SQL = "SELECT SUM((salesorderdetails.quantity-salesorderdetails.qtyinvoiced)*bom.quantity) AS dem
FROM salesorderdetails,
salesorders,
bom,
stockmaster
WHERE salesorderdetails.orderno=salesorders.orderno
AND salesorderdetails.stkcode=bom.parent
AND salesorderdetails.quantity-salesorderdetails.qtyinvoiced > 0
AND bom.component='" . $InventoryPlan['stockid'] . "'
AND stockmaster.stockid=bom.parent
AND salesorders.fromstkloc ='" . $_POST['Location'] . "'
AND stockmaster.mbflag='A'
AND salesorderdetails.completed=0";
}
$BOMDemandResult = DB_query($SQL,$db,'','',false,false);
if (DB_error_no($db) !=0) {
$title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg( _('The sales order demand quantities from parent assemblies could not be retrieved by the SQL because') . ' - ' . DB_error_msg($db),'error');
echo "<br><a href='" .$rootpath ."/index.php?" . SID . "'>" . _('Back to the menu') . '</a>';
if ($debug==1){
echo "<br>$SQL";
}
include('includes/footer.inc');
exit;
}
if ($_POST['Location']=='All'){
$SQL = "SELECT SUM(purchorderdetails.quantityord - purchorderdetails.quantityrecd) as qtyonorder
FROM purchorderdetails,
purchorders
WHERE purchorderdetails.orderno = purchorders.orderno
AND purchorderdetails.itemcode = '" . $InventoryPlan['stockid'] . "'
AND purchorderdetails.completed = 0";
} else {
$SQL = "SELECT SUM(purchorderdetails.quantityord - purchorderdetails.quantityrecd) AS qtyonorder
FROM purchorderdetails,
purchorders
WHERE purchorderdetails.orderno = purchorders.orderno
AND purchorderdetails.itemcode = '" . $InventoryPlan['stockid'] . "'
AND purchorderdetails.completed = 0
AND purchorders.intostocklocation= '" . $_POST['Location'] . "'";
}
$DemandRow = DB_fetch_array($DemandResult);
$BOMDemandRow = DB_fetch_array($BOMDemandResult);
$TotalDemand = $DemandRow['qtydemand'] + $BOMDemandRow['dem'];
$OnOrdResult = DB_query($SQL,$db,'','',false,false);
if (DB_error_no($db) !=0) {
$title = _('Inventory Planning') . ' - ' . _('Problem Report') . '....';
include('includes/header.inc');
prnMsg( _('The purchase order quantities could not be retrieved by the SQL because') . ' - ' . DB_error_msg($db),'error');
echo "<br><a href='" .$rootpath ."/index.php?" . SID . "'>" . _('Back to the menu') . '</a>';
if ($debug==1){
echo "<br>$SQL";
}
include('includes/footer.inc');
exit;
}
$OnOrdRow = DB_fetch_array($OnOrdResult);
$LeftOvers = $pdf->addTextWrap($Left_Margin, $YPos, 110, $FontSize, $InventoryPlan['stockid'], 'left');
$LeftOvers = $pdf->addTextWrap(130, $YPos, 120,6,$InventoryPlan['description'],'left');
$LeftOvers = $pdf->addTextWrap(251, $YPos, 40,$FontSize,number_format($SalesRow['prd5'],0),'right');
$LeftOvers = $pdf->addTextWrap(292, $YPos, 40,$FontSize,number_format($SalesRow['prd4'],0),'right');
$LeftOvers = $pdf->addTextWrap(333, $YPos, 40,$FontSize,number_format($SalesRow['prd3'],0),'right');
$LeftOvers = $pdf->addTextWrap(374, $YPos, 40,$FontSize,number_format($SalesRow['prd2'],0),'right');
$LeftOvers = $pdf->addTextWrap(415, $YPos, 40,$FontSize,number_format($SalesRow['prd1'],0),'right');
$LeftOvers = $pdf->addTextWrap(456, $YPos, 40,$FontSize,number_format($SalesRow['prd0'],0),'right');
if ($_POST['NumberMonthsHolding']>10){
$NumberMonths=$_POST['NumberMonthsHolding']-10;
$MaxMthSales = ($SalesRow['prd1']+$SalesRow['prd2']+$SalesRow['prd3']+$SalesRow['prd4']+$SalesRow['prd5'])/5;
}
else{
$NumberMonths=$_POST['NumberMonthsHolding'];
$MaxMthSales = max($SalesRow['prd1'], $SalesRow['prd2'], $SalesRow['prd3'], $SalesRow['prd4'], $SalesRow['prd5']);
}
$IdealStockHolding = ceil($MaxMthSales * $NumberMonths);
$LeftOvers = $pdf->addTextWrap(497, $YPos, 40,$FontSize,number_format($IdealStockHolding,0),'right');
$LeftOvers = $pdf->addTextWrap(597, $YPos, 40,$FontSize,number_format($InventoryPlan['qoh'],0),'right');
$LeftOvers = $pdf->addTextWrap(638, $YPos, 40,$FontSize,number_format($TotalDemand,0),'right');
$LeftOvers = $pdf->addTextWrap(679, $YPos, 40,$FontSize,number_format($OnOrdRow['qtyonorder'],0),'right');
$SuggestedTopUpOrder = $IdealStockHolding - $InventoryPlan['qoh'] + $TotalDemand - $OnOrdRow['qtyonorder'];
if ($SuggestedTopUpOrder <=0){
$LeftOvers = $pdf->addTextWrap(720, $YPos, 40,$FontSize,_(' '),'right');
} else {
$LeftOvers = $pdf->addTextWrap(720, $YPos, 40,$FontSize,number_format($SuggestedTopUpOrder,0),'right');
}
if ($YPos < $Bottom_Margin + $line_height){
$PageNumber++;
include('includes/PDFInventoryPlanPageHeader.inc');
}
} /*end inventory valn while loop */
$YPos -= (2*$line_height);
$pdf->line($Left_Margin, $YPos+$line_height,$Page_Width-$Right_Margin, $YPos+$line_height);
$pdfcode = $pdf->output();
$len = strlen($pdfcode);
if ($len<=20){
$title = _('Print Inventory Planning Report Empty');
include('includes/header.inc');
prnMsg( _('There were no items in the range and location specified'),'error');
echo "<br><a href='$rootpath/index.php?" . SID . "'>" . _('Back to the menu') . '</a>';
include('includes/footer.inc');
exit;
} else {
header('Content-type: application/pdf');
header('Content-Length: ' . $len);
header('Content-Disposition: inline; filename=InventoryPlanning.pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
$pdf->Output('InventoryPlanning', 'I');
}
} else { /*The option to print PDF was not hit */
$title=_('Inventory Planning Reporting');
include('includes/header.inc');
if (strlen($_POST['FromCriteria'])<1 || strlen($_POST['ToCriteria'])<1) {
/*if $FromCriteria is not set then show a form to allow input */
echo "<form action='" . $_SERVER['PHP_SELF'] . '?' . SID . "' method='POST'><table>";
echo '<tr>
<td>' . _('From Inventory Category Code') . ':</font></td>
<td><select name=FromCriteria>';
$sql='SELECT categoryid, categorydescription FROM stockcategory ORDER BY categoryid';
$CatResult= DB_query($sql,$db);
While ($myrow = DB_fetch_array($CatResult)){
echo "<option VALUE='" . $myrow['categoryid'] . "'>" . $myrow['categoryid'] . " - " . $myrow['categorydescription'];
}
echo "</select>
</td>
</tr>";
echo '<tr>
<td>' . _('To Inventory Category Code') . ':</td>
<td><select name=ToCriteria>';
/*Set the index for the categories result set back to 0 */
DB_data_seek($CatResult,0);
While ($myrow = DB_fetch_array($CatResult)){
echo "<option VALUE='" . $myrow['categoryid'] . "'>" . $myrow['categoryid'] . " - " . $myrow['categorydescription'];
}
echo '</select></td>
</tr>';
echo '<tr>
<td>' . _('For Inventory in Location') . ":</td><td><select name='Location'>";
$sql = 'SELECT loccode, locationname FROM locations';
$LocnResult=DB_query($sql,$db);
echo "<option Value='All'>" . _('All Locations');
while ($myrow=DB_fetch_array($LocnResult)){
echo "<option Value='" . $myrow['loccode'] . "'>" . $myrow['locationname'];
}
echo '</select>
</td>
</tr>';
echo '<tr>
<td>' . _('Stock Planning') . ":</td>
<td><select name='NumberMonthsHolding'>";
echo '<option selected Value=1>' . _('One Month MAX');
echo '<option Value=1.5>' . _('One Month and a half MAX');
echo '<option Value=2>' . _('Two Months MAX');
echo '<option Value=2.5>' . _('Two Month and a half MAX');
echo '<option Value=3>' . _('Three Months MAX');
echo '<option Value=4>' . _('Four Months MAX');
echo '<option Value=11>' . _('One Month AVG');
echo '<option Value=11.5>' . _('One Month and a half AVG');
echo '<option Value=12>' . _('Two Months AVG');
echo '<option Value=12.5>' . _('Two Month and a half AVG');
echo '<option Value=13>' . _('Three Months AVG');
echo '<option Value=14>' . _('Four Months AVG');
echo '</select>
</td>
</tr>';
echo "</table><div class='centre'><input type=Submit Name='PrintPDF' Value='" . _('Print PDF') . "'></div>";
}
include('includes/footer.inc');
} /*end of else not PrintPDF */
?>