forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRPShortages.php
343 lines (294 loc) · 12.8 KB
/
MRPShortages.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
<?php
// MRPShortages.php - Report of parts with demand greater than supply as determined by MRP
include ('includes/session.php');
if (!DB_table_exists('mrprequirements')) {
$Title = _('MRP error');
include ('includes/header.php');
echo '<br />';
prnMsg(_('The MRP calculation must be run before you can run this report') . '<br />' . _('To run the MRP calculation click') . ' ' . '<a href="' . $RootPath . '/MRP.php">' . _('here') . '</a>', 'error');
include ('includes/footer.php');
exit;
}
if (isset($_POST['PrintPDF'])) {
include ('includes/PDFStarter.php');
if ($_POST['ReportType'] == 'Shortage') {
$pdf->addInfo('Title', _('MRP Shortages Report'));
$pdf->addInfo('Subject', _('MRP Shortages'));
} else {
$pdf->addInfo('Title', _('MRP Excess Report'));
$pdf->addInfo('Subject', _('MRP Excess'));
}
$FontSize = 9;
$PageNumber = 1;
$LineHeight = 12;
// Create temporary tables for supply and demand, with one record per part with the
// total for either supply or demand. Did this to simplify main sql where used
// several subqueries.
$SQL = "CREATE TEMPORARY TABLE demandtotal (
part char(20),
demand double,
KEY `PART` (`part`)) DEFAULT CHARSET=utf8";
$Result = DB_query($SQL, _('Create of demandtotal failed because'));
$SQL = "INSERT INTO demandtotal
(part,
demand)
SELECT part,
SUM(quantity) as demand
FROM mrprequirements
GROUP BY part";
$Result = DB_query($SQL);
$SQL = "CREATE TEMPORARY TABLE supplytotal (
part char(20),
supply double,
KEY `PART` (`part`)) DEFAULT CHARSET=utf8";
$Result = DB_query($SQL, _('Create of supplytotal failed because'));
/* 21/03/2010: Ricard modification to allow items with total supply = 0 be included in the report */
$SQL = "INSERT INTO supplytotal
(part,
supply)
SELECT stockid,
0
FROM stockmaster";
$Result = DB_query($SQL);
$SQL = "UPDATE supplytotal
SET supply = (SELECT SUM(mrpsupplies.supplyquantity)
FROM mrpsupplies
WHERE supplytotal.part = mrpsupplies.part
AND mrpsupplies.supplyquantity > 0)";
$Result = DB_query($SQL);
$SQL = "UPDATE supplytotal SET supply = 0 WHERE supply IS NULL";
$Result = DB_query($SQL);
// Only include directdemand mrprequirements so don't have demand for top level parts and also
// show demand for the lower level parts that the upper level part generates. See MRP.php for
// more notes - Decided not to exclude derived demand so using $SQL, not $SQLexclude
$SQLexclude = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.mbflag,
stockmaster.actualcost,
stockmaster.decimalplaces,
(stockmaster.actualcost) as computedcost,
demandtotal.demand,
supplytotal.supply,
(demandtotal.demand - supplytotal.supply) *
(stockmaster.actualcost) as extcost
FROM stockmaster
LEFT JOIN demandtotal ON stockmaster.stockid = demandtotal.part
LEFT JOIN supplytotal ON stockmaster.stockid = supplytotal.part
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.mbflag,
stockmaster.actualcost,
stockmaster.decimalplaces,
supplytotal.supply,
demandtotal.demand,
extcost
HAVING demand > supply
ORDER BY '" . $_POST['Sort'] . "'";
if ($_POST['CategoryID'] == 'All') {
$SQLCategory = ' ';
} else {
$SQLCategory = "WHERE stockmaster.categoryid = '" . $_POST['CategoryID'] . "'";
}
if ($_POST['ReportType'] == 'Shortage') {
$SQLHaving = " HAVING demandtotal.demand > supplytotal.supply ";
} else {
$SQLHaving = " HAVING demandtotal.demand <= supplytotal.supply ";
}
$SQL = "SELECT stockmaster.stockid,
stockmaster.description,
stockmaster.mbflag,
stockmaster.actualcost,
stockmaster.decimalplaces,
(stockmaster.actualcost) as computedcost,
demandtotal.demand,
supplytotal.supply,
(demandtotal.demand - supplytotal.supply) *
(stockmaster.actualcost) as extcost
FROM stockmaster
LEFT JOIN demandtotal ON stockmaster.stockid = demandtotal.part
LEFT JOIN supplytotal ON stockmaster.stockid = supplytotal.part
LEFT JOIN stockcategory ON stockmaster.categoryid = stockcategory.categoryid " . $SQLCategory . "WHERE stockcategory.stocktype<>'L'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockmaster.mbflag,
stockmaster.actualcost,
stockmaster.decimalplaces,
stockmaster.actualcost,
supplytotal.supply,
demandtotal.demand " . $SQLHaving . " ORDER BY '" . $_POST['Sort'] . "'";
$Result = DB_query($SQL, '', '', false, true);
if (DB_error_no() != 0) {
$Title = _('MRP Shortages and Excesses') . ' - ' . _('Problem Report');
include ('includes/header.php');
prnMsg(_('The MRP shortages and excesses could not be retrieved by the SQL because') . ' ' . DB_error_msg(), 'error');
echo '<br/><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
if ($Debug == 1) {
echo '<br/>' . $SQL;
}
include ('includes/footer.php');
exit;
}
if (DB_num_rows($Result) == 0) {
$Title = _('MRP Shortages and Excesses') . ' - ' . _('Problem Report');
include ('includes/header.php');
prnMsg(_('No MRP shortages - Excess retrieved'), 'warn');
echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
if ($Debug == 1) {
echo '<br />' . $SQL;
}
include ('includes/footer.php');
exit;
}
PrintHeader($pdf, $YPos, $PageNumber, $Page_Height, $Top_Margin, $Left_Margin, $Page_Width, $Right_Margin);
$Total_Shortage = 0;
$Partctr = 0;
$Fill = false;
$pdf->SetFillColor(224, 235, 255); // Defines color to make alternating lines highlighted
while ($MyRow = DB_fetch_array($Result)) {
if ($_POST['ReportType'] == 'Shortage') {
$LineToPrint = ($MyRow['demand'] > $MyRow['supply']);
} else {
$LineToPrint = ($MyRow['demand'] <= $MyRow['supply']);
}
if ($LineToPrint) {
$YPos-= $LineHeight;
$FontSize = 8;
// Use to alternate between lines with transparent and painted background
if ($_POST['Fill'] == 'yes') {
$Fill = !$Fill;
}
// Parameters for addTextWrap are defined in /includes/class.pdf.php
// 1) X position 2) Y position 3) Width
// 4) Height 5) Text 6) Alignment 7) Border 8) Fill - True to use SetFillColor
// and False to set to transparent
$Shortage = ($MyRow['demand'] - $MyRow['supply']) * -1;
$Extcost = $Shortage * $MyRow['computedcost'];
$pdf->addTextWrap($Left_Margin, $YPos, 90, $FontSize, $MyRow['stockid'], '', 0, $Fill);
$pdf->addTextWrap(130, $YPos, 150, $FontSize, $MyRow['description'], '', 0, $Fill);
$pdf->addTextWrap(280, $YPos, 25, $FontSize, $MyRow['mbflag'], 'right', 0, $Fill);
$pdf->addTextWrap(305, $YPos, 55, $FontSize, locale_number_format($MyRow['computedcost'], 2), 'right', 0, $Fill);
$pdf->addTextWrap(360, $YPos, 50, $FontSize, locale_number_format($MyRow['supply'], $MyRow['decimalplaces']), 'right', 0, $Fill);
$pdf->addTextWrap(410, $YPos, 50, $FontSize, locale_number_format($MyRow['demand'], $MyRow['decimalplaces']), 'right', 0, $Fill);
$pdf->addTextWrap(460, $YPos, 50, $FontSize, locale_number_format($Shortage, $MyRow['decimalplaces']), 'right', 0, $Fill);
$pdf->addTextWrap(510, $YPos, 60, $FontSize, locale_number_format($MyRow['extcost'], 2), 'right', 0, $Fill);
$Total_Shortage+= $MyRow['extcost'];
$Partctr++;
if ($YPos < $Bottom_Margin + $LineHeight) {
PrintHeader($pdf, $YPos, $PageNumber, $Page_Height, $Top_Margin, $Left_Margin, $Page_Width, $Right_Margin);
}
}
} /*end while loop */
$FontSize = 8;
$YPos-= (2 * $LineHeight);
if ($YPos < $Bottom_Margin + $LineHeight) {
PrintHeader($pdf, $YPos, $PageNumber, $Page_Height, $Top_Margin, $Left_Margin, $Page_Width, $Right_Margin);
}
/*Print out the grand totals */
$pdf->addTextWrap($Left_Margin, $YPos, 120, $FontSize, _('Number of Parts: '), 'left');
$pdf->addTextWrap(150, $YPos, 30, $FontSize, $Partctr, 'left');
if ($_POST['ReportType'] == 'Shortage') {
$pdf->addTextWrap(300, $YPos, 180, $FontSize, _('Total Extended Shortage:'), 'right');
} else {
$pdf->addTextWrap(300, $YPos, 180, $FontSize, _('Total Extended Excess:'), 'right');
}
$DisplayTotalVal = locale_number_format($Total_Shortage, 2);
$pdf->addTextWrap(510, $YPos, 60, $FontSize, $DisplayTotalVal, 'right');
if ($_POST['ReportType'] == 'Shortage') {
$pdf->OutputD($_SESSION['DatabaseName'] . '_MRPShortages_' . date('Y-m-d') . '.pdf');
} else {
$pdf->OutputD($_SESSION['DatabaseName'] . '_MRPExcess_' . date('Y-m-d') . '.pdf');
}
$pdf->__destruct();
} else { /*The option to print PDF was not hit so display form */
$Title = _('MRP Shortages - Excess Reporting');
$ViewTopic = 'MRP';
$BookMark = '';
include ('includes/header.php');
echo '<p class="page_title_text"><img src="' . $RootPath . '/css/' . $Theme . '/images/inventory.png" title="' . _('Stock') . '" alt="" />' . ' ' . $Title . '</p>';
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<fieldset>
<legend>', _('Report Criteria'), '</legend>';
echo '<field>
<label for="CategoryID">' . _('Inventory Category') . ':</label>
<select name="CategoryID">';
echo '<option selected="selected" value="All">' . _('All Stock Categories') . '</option>';
$SQL = "SELECT categoryid,
categorydescription
FROM stockcategory";
$Result = DB_query($SQL);
while ($MyRow = DB_fetch_array($Result)) {
echo '<option value="' . $MyRow['categoryid'] . '">' . $MyRow['categoryid'] . ' - ' . $MyRow['categorydescription'] . '</option>';
} //end while loop
echo '</select>
</field>';
echo '<field>
<label for="Sort">' . _('Sort') . ':</label>
<select name="Sort">
<option selected="selected" value="extcost">' . _('Extended Shortage Dollars') . '</option>
<option value="stockid">' . _('Part Number') . '</option>
</select>
</field>';
echo '<field>
<label for="ReportType">' . _('Shortage-Excess Option') . ':</label>
<select name="ReportType">
<option selected="selected" value="Shortage">' . _('Report MRP Shortages') . '</option>
<option value="Excess">' . _('Report MRP Excesses') . '</option>
</select>
</field>';
echo '<field>
<label for="Fill">' . _('Print Option') . ':</label>
<select name="Fill">
<option selected="selected" value="yes">' . _('Print With Alternating Highlighted Lines') . '</option>
<option value="no">' . _('Plain Print') . '</option>
</select>
</field>';
echo '</fieldset>
<div class="centre">
<input type="submit" name="PrintPDF" value="' . _('Print PDF') . '" />
</div>
</form>';
include ('includes/footer.php');
} /*end of else not PrintPDF */
function PrintHeader(&$pdf, &$YPos, &$PageNumber, $Page_Height, $Top_Margin, $Left_Margin, $Page_Width, $Right_Margin) {
$LineHeight = 12;
/*PDF page header for MRP Shortages report */
if ($PageNumber > 1) {
$pdf->newPage();
}
$FontSize = 9;
$YPos = $Page_Height - $Top_Margin;
$pdf->addTextWrap($Left_Margin, $YPos, 300, $FontSize, $_SESSION['CompanyRecord']['coyname']);
$YPos-= $LineHeight;
if ($_POST['ReportType'] == 'Shortage') {
$pdf->addTextWrap($Left_Margin, $YPos, 300, $FontSize, _('MRP Shortages Report'));
} else {
$pdf->addTextWrap($Left_Margin, $YPos, 300, $FontSize, _('MRP Excess Report'));
}
$pdf->addTextWrap($Page_Width - $Right_Margin - 110, $YPos, 160, $FontSize, _('Printed') . ': ' . Date($_SESSION['DefaultDateFormat']) . ' ' . _('Page') . ' ' . $PageNumber, 'left');
$YPos-= (2 * $LineHeight);
/*Draw a rectangle to put the headings in */
//$pdf->line($Left_Margin, $YPos+$LineHeight,$Page_Width-$Right_Margin, $YPos+$LineHeight);
//$pdf->line($Left_Margin, $YPos+$LineHeight,$Left_Margin, $YPos- $LineHeight);
//$pdf->line($Left_Margin, $YPos- $LineHeight,$Page_Width-$Right_Margin, $YPos- $LineHeight);
//$pdf->line($Page_Width-$Right_Margin, $YPos+$LineHeight,$Page_Width-$Right_Margin, $YPos- $LineHeight);
/*set up the headings */
$Xpos = $Left_Margin + 1;
$pdf->addTextWrap($Xpos, $YPos, 130, $FontSize, _('Part Number'), 'left');
$pdf->addTextWrap(130, $YPos, 150, $FontSize, _('Description'), 'left');
$pdf->addTextWrap(285, $YPos, 20, $FontSize, _('M/B'), 'right');
$pdf->addTextWrap(305, $YPos, 55, $FontSize, _('Unit Cost'), 'right');
$pdf->addTextWrap(360, $YPos, 50, $FontSize, _('Supply'), 'right');
$pdf->addTextWrap(410, $YPos, 50, $FontSize, _('Demand'), 'right');
if ($_POST['ReportType'] == 'Shortage') {
$pdf->addTextWrap(460, $YPos, 50, $FontSize, _('Shortage'), 'right');
$pdf->addTextWrap(510, $YPos, 60, $FontSize, _('Ext. Shortage'), 'right');
} else {
$pdf->addTextWrap(460, $YPos, 50, $FontSize, _('Excess'), 'right');
$pdf->addTextWrap(510, $YPos, 60, $FontSize, _('Ext. Excess'), 'right');
}
$FontSize = 8;
$YPos = $YPos - (2 * $LineHeight);
$PageNumber++;
} // End of PrintHeader function
?>