forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShipments.php
524 lines (420 loc) · 19 KB
/
Shipments.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
include('includes/DefineShiptClass.php');
include('includes/session.php');
if (isset($_POST['ETA'])){$_POST['ETA'] = ConvertSQLDate($_POST['ETA']);};
$Title = _('Shipments');
$ViewTopic = 'Shipments';
$BookMark = '';
include('includes/header.php');
include('includes/SQL_CommonFunctions.inc');
if (isset($_GET['NewShipment']) and $_GET['NewShipment']=='Yes'){
unset($_SESSION['Shipment']->LineItems);
unset($_SESSION['Shipment']);
}
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/magnifier.png" title="' . _('Search') .
'" alt="" />' . ' ' . $Title . '</p>';
if (!isset($_SESSION['SupplierID']) AND !isset($_SESSION['Shipment']) AND !isset($_GET['SelectedShipment'])){
prnMsg( _('To set up a shipment') . ', ' . _('the supplier must first be selected from the Select Supplier page'), 'error');
echo '<table class="selection">
<tr><td class="menu_group_item">
<li><a href="'. $RootPath . '/SelectSupplier.php">' . _('Select the Supplier') . '</a></li>
</td></tr></table></div>';
include('includes/footer.php');
exit;
}
if (isset($_GET['SelectedShipment'])){
if (isset($_SESSION['Shipment'])){
unset ($_SESSION['Shipment']->LineItems);
unset ($_SESSION['Shipment']);
}
$_SESSION['Shipment'] = new Shipment;
/*read in all the guff from the selected shipment into the Shipment Class variable - the class code is included in the main script before this script is included */
$ShipmentHeaderSQL = "SELECT shipments.supplierid,
suppliers.suppname,
shipments.eta,
suppliers.currcode,
shipments.vessel,
shipments.voyageref,
shipments.closed
FROM shipments INNER JOIN suppliers
ON shipments.supplierid = suppliers.supplierid
WHERE shipments.shiptref = '" . $_GET['SelectedShipment'] . "'";
$ErrMsg = _('Shipment').' '. $_GET['SelectedShipment'] . ' ' . _('cannot be retrieved because a database error occurred');
$GetShiptHdrResult = DB_query($ShipmentHeaderSQL, $ErrMsg);
if (DB_num_rows($GetShiptHdrResult)==0) {
prnMsg ( _('Unable to locate Shipment') . ' '. $_GET['SelectedShipment'] . ' ' . _('in the database'), 'error');
include('includes/footer.php');
exit();
}
if (DB_num_rows($GetShiptHdrResult)==1) {
$MyRow = DB_fetch_array($GetShiptHdrResult);
if ($MyRow['closed']==1){
prnMsg( _('Shipment No.') .' '. $_GET['SelectedShipment'] .': '.
_('The selected shipment is already closed and no further modifications to the shipment are possible'), 'error');
include('includes/footer.php');
exit;
}
$_SESSION['Shipment']->ShiptRef = $_GET['SelectedShipment'];
$_SESSION['Shipment']->SupplierID = $MyRow['supplierid'];
$_SESSION['Shipment']->SupplierName = $MyRow['suppname'];
$_SESSION['Shipment']->CurrCode = $MyRow['currcode'];
$_SESSION['Shipment']->ETA = $MyRow['eta'];
$_SESSION['Shipment']->Vessel = $MyRow['vessel'];
$_SESSION['Shipment']->VoyageRef = $MyRow['voyageref'];
/*now populate the shipment details records */
$LineItemsSQL = "SELECT purchorderdetails.podetailitem,
purchorders.orderno,
purchorderdetails.itemcode,
purchorderdetails.itemdescription,
purchorderdetails.deliverydate,
purchorderdetails.glcode,
purchorderdetails.qtyinvoiced,
purchorderdetails.unitprice,
stockmaster.units,
purchorderdetails.quantityord,
purchorderdetails.quantityrecd,
purchorderdetails.stdcostunit,
stockmaster.actualcost as stdcost,
purchorders.intostocklocation
FROM purchorderdetails INNER JOIN stockmaster
ON purchorderdetails.itemcode=stockmaster.stockid
INNER JOIN purchorders
ON purchorderdetails.orderno=purchorders.orderno
WHERE purchorderdetails.shiptref='" . $_GET['SelectedShipment'] . "'";
$ErrMsg = _('The lines on the shipment cannot be retrieved because'). ' - ' . DB_error_msg();
$LineItemsResult = DB_query($LineItemsSQL, $ErrMsg);
if (DB_num_rows($GetShiptHdrResult)==0) {
prnMsg ( _('Unable to locate lines for Shipment') . ' '. $_GET['SelectedShipment'] . ' ' . _('in the database'), 'error');
include('includes/footer.php');
exit();
}
if (DB_num_rows($LineItemsResult) > 0) {
while ($MyRow=DB_fetch_array($LineItemsResult)) {
if ($MyRow['stdcostunit']==0){
$StandardCost =$MyRow['stdcost'];
} else {
$StandardCost =$MyRow['stdcostunit'];
}
$_SESSION['Shipment']->LineItems[$MyRow['podetailitem']] = new LineDetails(
$MyRow['podetailitem'],
$MyRow['orderno'],
$MyRow['itemcode'],
$MyRow['itemdescription'],
$MyRow['qtyinvoiced'],
$MyRow['unitprice'],
$MyRow['units'],
$MyRow['deliverydate'],
$MyRow['quantityord'],
$MyRow['quantityrecd'],
$StandardCost);
} /* line Shipment from shipment details */
DB_data_Seek($LineItemsResult,0);
$MyRow=DB_fetch_array($LineItemsResult);
$_SESSION['Shipment']->StockLocation = $MyRow['intostocklocation'];
} //end of checks on returned data set
}
} // end of reading in the existing shipment
if (!isset($_SESSION['Shipment'])){
$_SESSION['Shipment'] = new Shipment;
$SQL = "SELECT suppname,
currcode,
decimalplaces AS currdecimalplaces
FROM suppliers INNER JOIN currencies
ON suppliers.currcode=currencies.currabrev
WHERE supplierid='" . $_SESSION['SupplierID'] . "'";
$ErrMsg = _('The supplier details for the shipment could not be retrieved because');
$Result = DB_query($SQL,$ErrMsg);
$MyRow = DB_fetch_array($Result);
$_SESSION['Shipment']->SupplierID = $_SESSION['SupplierID'];
$_SESSION['Shipment']->SupplierName = $MyRow['suppname'];
$_SESSION['Shipment']->CurrCode = $MyRow['currcode'];
$_SESSION['Shipment']->CurrDecimalPlaces = $MyRow['currdecimalplaces'];
$_SESSION['Shipment']->ShiptRef = GetNextTransNo (31);
}
if (isset($_POST['Update'])
OR (isset($_GET['Add'])
AND $_SESSION['Shipment']->Closed==0)) { //user hit the update button
$InputError = 0;
if (isset($_POST['Update'])){
if (!Is_Date($_POST['ETA'])){
$InputError=1;
prnMsg( _('The date of expected arrival of the shipment must be entered in the format') . ' ' .$_SESSION['DefaultDateFormat'], 'error');
} elseif (Date1GreaterThanDate2($_POST['ETA'],Date($_SESSION['DefaultDateFormat']))==0){
$InputError=1;
prnMsg( _('An expected arrival of the shipment must be a date after today'), 'error');
} else {
$_SESSION['Shipment']->ETA = FormatDateForSQL($_POST['ETA']);
}
if (mb_strlen($_POST['Vessel'])<2){
prnMsg( _('A reference to the vessel of more than 2 characters is expected'), 'error');
}
if (mb_strlen($_POST['VoyageRef'])<2){
prnMsg( _('A reference to the voyage (or HAWB in the case of air-freight) of more than 2 characters is expected'), 'error');
}
} elseif(mb_strlen($_SESSION['Shipment']->Vessel)<2
OR mb_strlen($_SESSION['Shipment']->VoyageRef)<2){
prnMsg(_('Cannot add purchase order lines to the shipment unless the shipment is first initiated - hit update to setup the shipment first'),'info');
$InputError = 1;
}
if ($InputError==0 AND !isset($_GET['Add'])){ //don't update vessel and voyage on adding a new PO line to the shipment
$_SESSION['Shipment']->Vessel = $_POST['Vessel'];
$_SESSION['Shipment']->VoyageRef = $_POST['VoyageRef'];
}
/*The user hit the update the shipment button and there are some lines on the shipment*/
if ($InputError == 0 AND (isset($_SESSION['Shipment']) OR isset($_GET['Add']))){
$SQL = "SELECT shiptref FROM shipments WHERE shiptref =" . $_SESSION['Shipment']->ShiptRef;
$Result = DB_query($SQL);
if (DB_num_rows($Result)==1){
$SQL = "UPDATE shipments SET vessel='" . $_SESSION['Shipment']->Vessel . "',
voyageref='". $_SESSION['Shipment']->VoyageRef . "',
eta='" . $_SESSION['Shipment']->ETA . "'
WHERE shiptref ='" . $_SESSION['Shipment']->ShiptRef . "'";
} else {
$SQL = "INSERT INTO shipments (shiptref,
vessel,
voyageref,
eta,
supplierid)
VALUES ('" . $_SESSION['Shipment']->ShiptRef . "',
'" . $_SESSION['Shipment']->Vessel . "',
'". $_SESSION['Shipment']->VoyageRef . "',
'" . $_SESSION['Shipment']->ETA . "',
'" . $_SESSION['Shipment']->SupplierID . "')" ;
}
/*now update or insert as necessary */
$Result = DB_query($SQL);
/*now check that the delivery date of all PODetails are the same as the ETA as the shipment */
foreach ($_SESSION['Shipment']->LineItems as $LnItm) {
if (DateDiff(ConvertSQLDate($LnItm->DelDate),ConvertSQLDate($_SESSION['Shipment']->ETA),'d')!=0){
$SQL = "UPDATE purchorderdetails
SET deliverydate ='" . $_SESSION['Shipment']->ETA . "'
WHERE podetailitem='" . $LnItm->PODetailItem . "'";
$Result = DB_query($SQL);
$_SESSION['Shipment']->LineItems[$LnItm->PODetailItem]->DelDate = $_SESSION['Shipment']->ETA;
}
}
prnMsg( _('Updated the shipment record and delivery dates of order lines as necessary'), 'success');
echo '<br />';
} //error traps all passed ok
} //user hit Update
if (isset($_GET['Add'])
AND $_SESSION['Shipment']->Closed==0
AND $InputError==0){
$SQL = "SELECT purchorderdetails.orderno,
purchorderdetails.itemcode,
purchorderdetails.itemdescription,
purchorderdetails.unitprice,
purchorderdetails.stdcostunit,
stockmaster.actualcost as stdcost,
purchorderdetails.quantityord,
purchorderdetails.quantityrecd,
purchorderdetails.deliverydate,
stockmaster.units,
stockmaster.decimalplaces,
purchorderdetails.qtyinvoiced
FROM purchorderdetails INNER JOIN stockmaster
ON purchorderdetails.itemcode=stockmaster.stockid
WHERE purchorderdetails.podetailitem='" . $_GET['Add'] . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
/*The variable StdCostUnit gets set when the item is first received and stored for all future transactions with this purchase order line - subsequent changes to the standard cost will not therefore stuff up variances resulting from the line which may have several entries in GL for each delivery drop if it has already been set from a delivery then use it otherwise use the current system standard */
if ($MyRow['stdcostunit']==0){
$StandardCost = $MyRow['stdcost'];
}else {
$StandardCost = $MyRow['stdcostunit'];
}
$_SESSION['Shipment']->Add_To_Shipment($_GET['Add'],
$MyRow['orderno'],
$MyRow['itemcode'],
$MyRow['itemdescription'],
$MyRow['qtyinvoiced'],
$MyRow['unitprice'],
$MyRow['units'],
$MyRow['deliverydate'],
$MyRow['quantityord'],
$MyRow['quantityrecd'],
$StandardCost,
$MyRow['decimalplaces']);
}
if (isset($_GET['Delete']) AND $_SESSION['Shipment']->Closed==0){ //shipment is open and user hit delete on a line
$_SESSION['Shipment']->Remove_From_Shipment($_GET['Delete']);
}
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>', _('Shipment Details'), '</legend>
<field>
<label for="ShiptRef">' . _('Shipment').': </label>
<fieldtext>' . $_SESSION['Shipment']->ShiptRef . '</fieldtext>
</field>
<field>
<label>' . _('From'). '</label
<fieldtext>' . $_SESSION['Shipment']->SupplierName . '</fieldtext>
</field>';
echo '<field>
<label for="Vessel">' . _('Vessel Name /Transport Agent'). ': </label>
<input type="text" name="Vessel" maxlength="50" size="50" value="' . $_SESSION['Shipment']->Vessel . '" />
</field>
<field>
<label for="VoyageRef">' . _('Voyage Ref / Consignment Note').': </label>
<input type="text" name="VoyageRef" maxlength="20" size="20" value="' . $_SESSION['Shipment']->VoyageRef . '" />
</field>';
if (isset($_SESSION['Shipment']->ETA)){
$ETA = $_SESSION['Shipment']->ETA;
} else {
$ETA ='';
}
echo '<field>
<label for="ETA">' . _('Expected Arrival Date (ETA)'). ': </label>';
if (isset($_SESSION['Shipment']->ETA)) {
echo '<input type="date" name="ETA" maxlength="10" size="11" value="' . $ETA . '" />';
} else {
echo '<input type="date" name="ETA" maxlength="10" size="11" value="' . FormatDateForSQL(DateAdd(Date($_SESSION['DefaultDateFormat']), 'm', 1)) . '" />';
}
echo '<field>';
echo '<field>
<label for="StockLocation">' . _('Into Stock Location').':</label>';
if (count($_SESSION['Shipment']->LineItems)>0){
if (!isset($_SESSION['Shipment']->StockLocation)){
$SQL = "SELECT purchorders.intostocklocation
FROM purchorders INNER JOIN purchorderdetails
ON purchorders.orderno=purchorderdetails.orderno AND podetailitem = '" . key($_SESSION['Shipment']->LineItems) . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
$_SESSION['Shipment']->StockLocation = $MyRow[0];
$_POST['StockLocation']=$_SESSION['Shipment']->StockLocation;
} else {
$_POST['StockLocation']=$_SESSION['Shipment']->StockLocation;
}
}
if (!isset($_SESSION['Shipment']->StockLocation)){
echo '<select name="StockLocation">';
$SQL = "SELECT loccode, locationname FROM locations";
$ResultStkLocs = DB_query($SQL);
while ($MyRow=DB_fetch_array($ResultStkLocs)){
if (isset($_POST['StockLocation'])){
if ($MyRow['loccode'] == $_POST['StockLocation']){
echo '<option selected="selected" value="' . $MyRow['loccode'] . '">' . $MyRow['locationname'] . '</option>';
} else {
echo '<option value="' . $MyRow['loccode'] . '">' . $MyRow['locationname'] . '</option>';
}
} elseif ($MyRow['loccode']==$_SESSION['UserStockLocation']){
echo '<option selected="selected" value="' . $MyRow['loccode'] . '">' . $MyRow['locationname'] . '</option>';
} else {
echo '<option value="' . $MyRow['loccode'] . '">' . $MyRow['locationname'] . '</option>';
}
}
if (!isset($_POST['StockLocation'])){
$_POST['StockLocation'] = $_SESSION['UserStockLocation'];
}
echo '</select>';
} else {
$SQL = "SELECT locationname FROM locations WHERE loccode='" . $_SESSION['Shipment']->StockLocation . "'";
$ResultStkLocs = DB_query($SQL);
$MyRow=DB_fetch_array($ResultStkLocs);
echo '<input type="hidden" name="StockLocation" value="'.$_SESSION['Shipment']->StockLocation.'" />';
echo '<fieldtext>', $MyRow['locationname'], '</fieldtext>';
}
echo '</field>
</fieldset>';
if (count($_SESSION['Shipment']->LineItems)>0){
/* Always display all shipment lines */
echo '<table class="selection">';
echo '<tr><th colspan="9"><h3>' . _('Order Lines On This Shipment'). '</h3></th></tr>';
$TableHeader = '<tr>
<th>' . _('Order'). '</th>
<th>' . _('Item'). '</th>
<th>' . _('Quantity'). '<br />' . _('Ordered'). '</th>
<th>' . _('Units'). '</th>
<th>' . _('Quantity') . '<br />' . _('Received'). '</th>
<th>' . _('Quantity') . '<br />' . _('Invoiced'). '</th>
<th>' . $_SESSION['Shipment']->CurrCode .' '. _('Price') . '</th>
<th>' . _('Current'). '<br />' . _('Std Cost'). '</th>
</tr>';
echo $TableHeader;
/*show the line items on the shipment with the quantity being received for modification */
$RowCounter =0;
foreach ($_SESSION['Shipment']->LineItems as $LnItm) {
if ($RowCounter==15){
echo $TableHeader;
$RowCounter =0;
}
$RowCounter++;
echo '<tr class="striped_row">
<td>' . $LnItm->OrderNo . '</td>
<td>' . $LnItm->StockID .' - '. $LnItm->ItemDescription. '</td><td class="number">' . locale_number_format($LnItm->QuantityOrd,$LnItm->DecimalPlaces) . '</td>
<td>' . $LnItm->UOM . '</td>
<td class="number">' . locale_number_format($LnItm->QuantityRecd,$LnItm->DecimalPlaces) . '</td>
<td class="number">' . locale_number_format($LnItm->QtyInvoiced,$LnItm->DecimalPlaces) . '</td>
<td class="number">' . locale_number_format($LnItm->UnitPrice, $_SESSION['Shipment']->CurrDecimalPlaces) . '</td>
<td class="number">' . locale_number_format($LnItm->StdCostUnit,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?Delete=' . $LnItm->PODetailItem . '">' . _('Delete'). '</a></td>
</tr>';
}//for each line on the shipment
echo '</table>';
}//there are lines on the shipment
echo '<div class="centre">
<input type="submit" name="Update" value="'. _('Update Shipment Details') . '" />
</div>';
if (!isset($_POST['StockLocation'])) {
$_POST['StockLocation'] =$_SESSION['Shipment']->StockLocation;
}
$SQL = "SELECT purchorderdetails.podetailitem,
purchorders.orderno,
purchorderdetails.itemcode,
purchorderdetails.itemdescription,
purchorderdetails.unitprice,
purchorderdetails.quantityord,
purchorderdetails.quantityrecd,
purchorderdetails.deliverydate,
stockmaster.units,
stockmaster.decimalplaces
FROM purchorderdetails INNER JOIN purchorders
ON purchorderdetails.orderno=purchorders.orderno
INNER JOIN stockmaster
ON purchorderdetails.itemcode=stockmaster.stockid
WHERE qtyinvoiced=0
AND purchorders.status <> 'Cancelled'
AND purchorders.status <> 'Rejected'
AND purchorders.supplierno ='" . $_SESSION['Shipment']->SupplierID . "'
AND purchorderdetails.shiptref=0
AND purchorders.intostocklocation='" . $_POST['StockLocation'] . "'";
$Result = DB_query($SQL);
if (DB_num_rows($Result)>0){
echo '<table cellpadding="2" class="selection">';
echo '<tr>
<th colspan="7"><h3>' . _('Possible Order Lines To Add To This Shipment') . '</h3></th>
</tr>';
$TableHeader = '<tr>
<th>' . _('Order') . '</th>
<th>' . _('Item') . '</th>
<th>' . _('Quantity') . '<br />' . _('Ordered') . '</th>
<th>' . _('Units') . '</th>
<th>' . _('Quantity') . '<br />' . _('Received') . '</th>
<th>' . _('Delivery') . '<br />' . _('Date') . '</th>
</tr>';
echo $TableHeader;
/*show the PO items that could be added to the shipment */
$RowCounter =0;
while ($MyRow=DB_fetch_array($Result)){
if ($RowCounter==15){
echo $TableHeader;
$RowCounter =0;
}
$RowCounter++;
echo '<tr class="striped_row">
<td>' . $MyRow['orderno'] . '</td>
<td>' . $MyRow['itemcode'] . ' - ' . $MyRow['itemdescription'] . '</td>
<td class="number">' . locale_number_format($MyRow['quantityord'],$MyRow['decimalplaces']) . '</td>
<td>' . $MyRow['units'] . '</td>
<td class="number">' . locale_number_format($MyRow['quantityrecd'],$MyRow['decimalplaces']) . '</td>
<td class="number">' . ConvertSQLDate($MyRow['deliverydate']) . '</td>
<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?' . 'Add=' . $MyRow['podetailitem'] . '">' . _('Add') . '</a></td>
</tr>';
}
echo '</table>';
}
echo '</div>
</form>';
include('includes/footer.php');
?>