forked from oliviermichaud1/webERP
/
StockStatus.php
407 lines (351 loc) · 14.5 KB
/
StockStatus.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
<?php
/* $Id: StockStatus.php 5820 2013-02-24 04:28:04Z daintree $*/
include('includes/session.inc');
$Title = _('Stock Status');
include('includes/header.inc');
if (isset($_GET['StockID'])){
$StockID = trim(mb_strtoupper($_GET['StockID']));
} elseif (isset($_POST['StockID'])){
$StockID = trim(mb_strtoupper($_POST['StockID']));
} else {
$StockID = '';
}
if (isset($_POST['UpdateBinLocations'])){
foreach ($_POST as $PostVariableName => $Bin) {
if (mb_substr($PostVariableName,0,11) == 'BinLocation') {
$sql = "UPDATE locstock SET bin='" . strtoupper($Bin) . "' WHERE loccode='" . mb_substr($PostVariableName,11) . "' AND stockid='" . $StockID . "'";
$result = DB_query($sql, $db);
}
}
}
$result = DB_query("SELECT description,
units,
mbflag,
decimalplaces,
serialised,
controlled
FROM stockmaster
WHERE stockid='".$StockID."'",
$db,
_('Could not retrieve the requested item'),
_('The SQL used to retrieve the items was'));
$myrow = DB_fetch_array($result);
$DecimalPlaces = $myrow['decimalplaces'];
$Serialised = $myrow['serialised'];
$Controlled = $myrow['controlled'];
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/inventory.png" title="' . _('Inventory') .
'" alt="" /><b>' . ' ' . $StockID . ' - ' . $myrow['description'] . ' : ' . _('in units of') . ' : ' . $myrow['units'] . '</b></p>';
$Its_A_KitSet_Assembly_Or_Dummy =False;
if ($myrow[2]=='K'){
$Its_A_KitSet_Assembly_Or_Dummy =True;
prnMsg( _('This is a kitset part and cannot have a stock holding') . ', ' . _('only the total quantity on outstanding sales orders is shown'),'info');
} elseif ($myrow[2]=='A'){
$Its_A_KitSet_Assembly_Or_Dummy =True;
prnMsg(_('This is an assembly part and cannot have a stock holding') . ', ' . _('only the total quantity on outstanding sales orders is shown'),'info');
} elseif ($myrow[2]=='D'){
$Its_A_KitSet_Assembly_Or_Dummy =True;
prnMsg( _('This is an dummy part and cannot have a stock holding') . ', ' . _('only the total quantity on outstanding sales orders is shown'),'info');
}
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">';
echo '<div class="centre"><input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo _('Stock Code') . ':<input type="text" name="StockID" size="21" value="' . $StockID . '" maxlength="20" />';
echo ' <input type="submit" name="ShowStatus" value="' . _('Show Stock Status') . '" />';
$sql = "SELECT locstock.loccode,
locations.locationname,
locstock.quantity,
locstock.reorderlevel,
locstock.bin,
locations.managed
FROM locstock INNER JOIN locations
ON locstock.loccode=locations.loccode
WHERE locstock.stockid = '" . $StockID . "'
ORDER BY locations.locationname";
$ErrMsg = _('The stock held at each location cannot be retrieved because');
$DbgMsg = _('The SQL that was used to update the stock item and failed was');
$LocStockResult = DB_query($sql, $db, $ErrMsg, $DbgMsg);
echo '<br />
<table class="selection">';
if ($Its_A_KitSet_Assembly_Or_Dummy == True){
$TableHeader = '<tr>
<th>' . _('Location') . '</th>
<th>' . _('Demand') . '</th>
</tr>';
} else {
$TableHeader = '<tr>
<th>' . _('Location') . '</th>
<th>' . _('Bin Location') . '</th>
<th>' . _('Quantity On Hand') . '</th>
<th>' . _('Re-Order Level') . '</th>
<th>' . _('Demand') . '</th>
<th>' . _('In Transit') . '</th>
<th>' . _('Available') . '</th>
<th>' . _('On Order') . '</th>
</tr>';
}
echo $TableHeader;
$j = 1;
$k=0; //row colour counter
while ($myrow=DB_fetch_array($LocStockResult)) {
if ($k==1){
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k=1;
}
$sql = "SELECT SUM(salesorderdetails.quantity-salesorderdetails.qtyinvoiced) AS dem
FROM salesorderdetails INNER JOIN salesorders
ON salesorders.orderno = salesorderdetails.orderno
WHERE salesorders.fromstkloc='" . $myrow['loccode'] . "'
AND salesorderdetails.completed=0
AND salesorders.quotation=0
AND salesorderdetails.stkcode='" . $StockID . "'";
$ErrMsg = _('The demand for this product from') . ' ' . $myrow['loccode'] . ' ' . _('cannot be retrieved because');
$DemandResult = DB_query($sql,$db,$ErrMsg,$DbgMsg);
if (DB_num_rows($DemandResult)==1){
$DemandRow = DB_fetch_row($DemandResult);
$DemandQty = $DemandRow[0];
} else {
$DemandQty =0;
}
//Also need to add in the demand as a component of an assembly items if this items has any assembly parents.
$sql = "SELECT SUM((salesorderdetails.quantity-salesorderdetails.qtyinvoiced)*bom.quantity) AS dem
FROM salesorderdetails INNER JOIN salesorders
ON salesorders.orderno = salesorderdetails.orderno
INNER JOIN bom
ON salesorderdetails.stkcode=bom.parent
INNER JOIN stockmaster
ON stockmaster.stockid=bom.parent
WHERE salesorders.fromstkloc='" . $myrow['loccode'] . "'
AND salesorderdetails.quantity-salesorderdetails.qtyinvoiced > 0
AND bom.component='" . $StockID . "'
AND stockmaster.mbflag='A'
AND salesorders.quotation=0";
$ErrMsg = _('The demand for this product from') . ' ' . $myrow['loccode'] . ' ' . _('cannot be retrieved because');
$DemandResult = DB_query($sql,$db,$ErrMsg,$DbgMsg);
if (DB_num_rows($DemandResult)==1){
$DemandRow = DB_fetch_row($DemandResult);
$DemandQty += $DemandRow[0];
}
//Also the demand for the item as a component of works orders
$sql = "SELECT SUM(qtypu*(woitems.qtyreqd - woitems.qtyrecd)) AS woqtydemo
FROM woitems INNER JOIN worequirements
ON woitems.stockid=worequirements.parentstockid
INNER JOIN workorders
ON woitems.wo=workorders.wo
AND woitems.wo=worequirements.wo
WHERE workorders.loccode='" . $myrow['loccode'] . "'
AND worequirements.stockid='" . $StockID . "'
AND workorders.closed=0";
$ErrMsg = _('The workorder component demand for this product from') . ' ' . $myrow['loccode'] . ' ' . _('cannot be retrieved because');
$DemandResult = DB_query($sql,$db,$ErrMsg,$DbgMsg);
if (DB_num_rows($DemandResult)==1){
$DemandRow = DB_fetch_row($DemandResult);
$DemandQty += $DemandRow[0];
}
if ($Its_A_KitSet_Assembly_Or_Dummy == False){
$sql="SELECT SUM(purchorderdetails.quantityord*(CASE WHEN purchdata.conversionfactor IS NULL THEN 1 ELSE purchdata.conversionfactor END) -
purchorderdetails.quantityrecd*(CASE WHEN purchdata.conversionfactor IS NULL THEN 1 ELSE purchdata.conversionfactor END))
FROM purchorders LEFT JOIN purchorderdetails
ON purchorders.orderno=purchorderdetails.orderno
LEFT JOIN purchdata ON purchorders.supplierno=purchdata.supplierno
AND purchorderdetails.itemcode=purchdata.stockid
WHERE purchorderdetails.itemcode='" . $StockID . "'
AND purchorders.intostocklocation='" . $myrow['loccode'] . "'
AND (purchorders.status<>'Cancelled'
AND purchorders.status<>'Pending'
AND purchorders.status<>'Rejected'
AND purchorders.status<>'Completed')";
$ErrMsg = _('The quantity on order for this product to be received into') . ' ' . $myrow['loccode'] . ' ' . _('cannot be retrieved because');
$QOOResult = DB_query($sql,$db,$ErrMsg, $DbgMsg);
if (DB_num_rows($QOOResult)==1){
$QOORow = DB_fetch_row($QOOResult);
$QOO = $QOORow[0];
} else {
$QOO = 0;
}
//Also the on work order quantities
$sql = "SELECT SUM(woitems.qtyreqd-woitems.qtyrecd) AS qtywo
FROM woitems INNER JOIN workorders
ON woitems.wo=workorders.wo
WHERE workorders.closed=0
AND workorders.loccode='" . $myrow['loccode'] . "'
AND woitems.stockid='" . $StockID . "'";
$ErrMsg = _('The quantity on work orders for this product to be received into') . ' ' . $myrow['loccode'] . ' ' . _('cannot be retrieved because');
$QOOResult = DB_query($sql,$db,$ErrMsg, $DbgMsg);
if (DB_num_rows($QOOResult)==1){
$QOORow = DB_fetch_row($QOOResult);
$QOO += $QOORow[0];
}
$InTransitSQL="SELECT SUM(shipqty-recqty) as intransit
FROM loctransfers
WHERE stockid='" . $StockID . "'
AND shiploc='".$myrow['loccode']."'";
$InTransitResult=DB_query($InTransitSQL, $db);
$InTransitRow=DB_fetch_array($InTransitResult);
if ($InTransitRow['intransit']!='') {
$InTransitQuantityOut=-$InTransitRow['intransit'];
} else {
$InTransitQuantityOut=0;
}
$InTransitSQL="SELECT SUM(-shipqty+recqty) as intransit
FROM loctransfers
WHERE stockid='" . $StockID . "'
AND recloc='".$myrow['loccode']."'";
$InTransitResult=DB_query($InTransitSQL, $db);
$InTransitRow=DB_fetch_array($InTransitResult);
if ($InTransitRow['intransit']!='') {
$InTransitQuantityIn=-$InTransitRow['intransit'];
} else {
$InTransitQuantityIn=0;
}
if (($InTransitQuantityIn+$InTransitQuantityOut) < 0) {
$Available = $myrow['quantity'] - $DemandQty + ($InTransitQuantityIn+$InTransitQuantityOut);
} else {
$Available = $myrow['quantity'] - $DemandQty;
}
echo '<td>' . $myrow['locationname'] . '</td>
<td><input type="text" name="BinLocation' . $myrow['loccode'] . '" value="' . $myrow['bin'] . '" maxlength="10" size="11" onchange="ReloadForm(UpdateBinLocations)"/></td>';
printf('<td class="number">%s</td>
<td class="number">%s</td>
<td class="number">%s</td>
<td class="number">%s</td>
<td class="number">%s</td>
<td class="number">%s</td></tr>',
locale_number_format($myrow['quantity'], $DecimalPlaces),
locale_number_format($myrow['reorderlevel'], $DecimalPlaces),
locale_number_format($DemandQty, $DecimalPlaces),
locale_number_format($InTransitQuantityIn+$InTransitQuantityOut, $DecimalPlaces),
locale_number_format($Available, $DecimalPlaces),
locale_number_format($QOO, $DecimalPlaces)
);
if ($Serialised ==1){ /*The line is a serialised item*/
echo '<td><a target="_blank" href="' . $RootPath . '/StockSerialItems.php?Serialised=Yes&Location=' . $myrow['loccode'] . '&StockID=' .$StockID . '">' . _('Serial Numbers') . '</a></td></tr>';
} elseif ($Controlled==1){
echo '<td><a target="_blank" href="' . $RootPath . '/StockSerialItems.php?Location=' . $myrow['loccode'] . '&StockID=' .$StockID . '">' . _('Batches') . '</a></td></tr>';
}
} else {
/* It must be a dummy, assembly or kitset part */
printf('<td>%s</td>
<td class="number">%s</td>
</tr>',
$myrow['locationname'],
locale_number_format($DemandQty, $DecimalPlaces));
}
//end of page full new headings if
}
//end of while loop
echo '<tr>
<td></td>
<td><input type="submit" name="UpdateBinLocations" value="' . _('Update Bins') . '" /></td>
</tr>
</table>';
if (isset($_GET['DebtorNo'])){
$DebtorNo = trim(mb_strtoupper($_GET['DebtorNo']));
} elseif (isset($_POST['DebtorNo'])){
$DebtorNo = trim(mb_strtoupper($_POST['DebtorNo']));
} elseif (isset($_SESSION['CustomerID'])){
$DebtorNo=$_SESSION['CustomerID'];
}
if ($DebtorNo) { /* display recent pricing history for this debtor and this stock item */
$sql = "SELECT stockmoves.trandate,
stockmoves.qty,
stockmoves.price,
stockmoves.discountpercent
FROM stockmoves
WHERE stockmoves.debtorno='" . $DebtorNo . "'
AND stockmoves.type=10
AND stockmoves.stockid = '" . $StockID . "'
AND stockmoves.hidemovt=0
ORDER BY stockmoves.trandate DESC";
/* only show pricing history for sales invoices - type=10 */
$ErrMsg = _('The stock movements for the selected criteria could not be retrieved because') . ' - ';
$DbgMsg = _('The SQL that failed was');
$MovtsResult = DB_query($sql, $db, $ErrMsg, $DbgMsg);
$k=1;
while ($myrow=DB_fetch_array($MovtsResult)) {
if ($LastPrice != $myrow['price']
OR $LastDiscount != $myrow['discount']) { /* consolidate price history for records with same price/discount */
if (isset($qty)) {
$DateRange=ConvertSQLDate($FromDate);
if ($FromDate != $ToDate) {
$DateRange .= ' - ' . ConvertSQLDate($ToDate);
}
$PriceHistory[] = array($DateRange, $qty, $LastPrice, $LastDiscount);
$k++;
if ($k > 9) {
break; /* 10 price records is enough to display */
}
if ($myrow['trandate'] < FormatDateForSQL(DateAdd(date($_SESSION['DefaultDateFormat']),'y', -1))) {
break; /* stop displaying pirce history more than a year old once we have at least one to display */
}
}
$LastPrice = $myrow['price'];
$LastDiscount = $myrow['discountpercent'];
$ToDate = $myrow['trandate'];
$qty = 0;
}
$qty += $myrow['qty'];
$FromDate = $myrow['trandate'];
}
if (isset($qty)) {
$DateRange = ConvertSQLDate($FromDate);
if ($FromDate != $ToDate) {
$DateRange .= ' - '.ConvertSQLDate($ToDate);
}
$PriceHistory[] = array($DateRange, $qty, $LastPrice, $LastDiscount);
}
if (isset($PriceHistory)) {
echo '<br />
<table class="selection">
<tr>
<th colspan="4"><font color="navy" size="2">' . _('Pricing history for sales of') . ' ' . $StockID . ' ' . _('to') . ' ' . $DebtorNo . '</font></th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Date Range') . '</th>
<th>' . _('Quantity') . '</th>
<th>' . _('Price') . '</th>
<th>' . _('Discount') . '</th>
</tr>';
$j = 0;
$k = 0; //row colour counter
foreach($PriceHistory as $PreviousPrice) {
$j--;
if ($j < 0 ){
$j = 11;
echo $TableHeader;
}
if ($k==1){
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k=1;
}
printf('<td>%s</td>
<td class="number">%s</td>
<td class="number">%s</td>
<td class="number">%s%%</td>
</tr>',
$ph[0],
locale_number_format($PreviousPrice[1],$DecimalPlaces),
locale_number_format($PreviousPrice[2],$_SESSION['CompanyRecord']['decimalplaces']),
locale_number_format($PreviousPrice[3]*100,2));
}
echo '</table>';
}
//end of while loop
else {
echo '<p>'._('No history of sales of') . ' ' . $StockID . ' ' . _('to') . ' ' . $DebtorNo;
}
}//end of displaying price history for a debtor
echo '<br /><a href="' . $RootPath . '/StockMovements.php?StockID=' . $StockID . '">' . _('Show Movements') . '</a>
<br /><a href="' . $RootPath . '/StockUsage.php?StockID=' . $StockID . '">' . _('Show Usage') . '</a>
<br /><a href="' . $RootPath . '/SelectSalesOrder.php?SelectedStockItem=' . $StockID . '">' . _('Search Outstanding Sales Orders') . '</a>
<br /><a href="' . $RootPath . '/SelectCompletedOrder.php?SelectedStockItem=' . $StockID . '">' . _('Search Completed Sales Orders') . '</a>';
if ($Its_A_KitSet_Assembly_Or_Dummy ==False){
echo '<br /><a href="' . $RootPath . '/PO_SelectOSPurchOrder.php?SelectedStockItem=' . $StockID . '">' . _('Search Outstanding Purchase Orders') . '</a>';
}
echo '</div></form>';
include('includes/footer.inc');
?>