1<?php
2/* GoodsReceived.php */
3/* Entry of items received against purchase orders */
4
5/* Session started in header.php for password checking and authorisation level check */
6include('includes/DefinePOClass.php');
7include('includes/DefineSerialItems.php');
8include('includes/session.php');
9include('includes/SQL_CommonFunctions.inc');
10
11/*The identifier makes this goods received session unique so cannot get confused
12 * with other sessions of goods received on the same machine/browser
13 * The identifier only needs to be unique for this php session, so a
14 * unix timestamp will be sufficient.
15 */
16
17if (empty($_GET['identifier'])) {
18	$identifier=date('U');
19} else {
20	$identifier=$_GET['identifier'];
21}
22$Title = _('Receive Purchase Orders');
23$ViewTopic = 'Inventory';
24$BookMark = 'GoodsReceived';
25include('includes/header.php');
26
27echo '<a href="'. $RootPath . '/PO_SelectOSPurchOrder.php">' . _('Back to Purchase Orders'). '</a>
28	<br />';
29
30if (isset($_GET['PONumber']) AND $_GET['PONumber']<=0 AND !isset($_SESSION['PO'.$identifier])) {
31	/* This page can only be called with a purchase order number for invoicing*/
32	echo '<div class="centre">
33			<a href= "' . $RootPath . '/PO_SelectOSPurchOrder.php">' . _('Select a purchase order to receive') . '</a>
34		</div>
35		<br />' .  _('This page can only be opened if a purchase order has been selected. Please select a purchase order first');
36
37	include ('includes/footer.php');
38	exit;
39} elseif (isset($_GET['PONumber'])
40			AND !isset($_POST['Update'])) {
41/*Update only occurs if the user hits the button to refresh the data and recalc the value of goods recd*/
42
43	$_GET['ModifyOrderNumber'] = intval($_GET['PONumber']);
44	include('includes/PO_ReadInOrder.inc');
45} elseif (isset($_POST['Update'])
46			OR isset($_POST['ProcessGoodsReceived'])) {
47
48/* if update quantities button is hit page has been called and ${$Line->LineNo} would have be
49 set from the post to the quantity to be received */
50
51	foreach ($_SESSION['PO'.$identifier]->LineItems as $Line) {
52		$RecvQty = round(filter_number_format($_POST['RecvQty_' . $Line->LineNo]),$Line->DecimalPlaces);
53		if (!is_numeric($RecvQty)) {
54			$RecvQty = 0;
55		}
56		$_SESSION['PO'.$identifier]->LineItems[$Line->LineNo]->ReceiveQty = $RecvQty;
57		if (isset($_POST['Complete_' . $Line->LineNo])) {
58			$_SESSION['PO'.$identifier]->LineItems[$Line->LineNo]->Completed = 1;
59		} else {
60			$_SESSION['PO'.$identifier]->LineItems[$Line->LineNo]->Completed = 0;
61		}
62	}
63}
64
65if ($_SESSION['PO'.$identifier]->Status != 'Printed') {
66	prnMsg( _('Purchase orders must have a status of Printed before they can be received').'.<br />' .
67		_('Order number') . ' ' . $_GET['PONumber'] . ' ' . _('has a status of') . ' ' . _($_SESSION['PO'.$identifier]->Status), 'warn');
68	include('includes/footer.php');
69	exit;
70}
71
72// Always display quantities received and recalc balance for all items on the order
73echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
74	'/images/supplier.png" title="', // Icon image.
75	_('Receive'), '" /> ', // Icon title.
76	_('Receive Purchase Order'), ' : ', $_SESSION['PO'.$identifier]->OrderNo, ' ', _('from'), ' ', $_SESSION['PO'.$identifier]->SupplierName, '</p>';// Page title.
77
78echo '<form action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '?identifier=', urlencode($identifier), '" id="form1" method="post">',
79	'<div>',
80	'<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
81
82if (!isset($_POST['ProcessGoodsReceived'])) {
83	if (!isset($_POST['DefaultReceivedDate']) AND !isset($_SESSION['PO' . $identifier]->DefaultReceivedDate)) {
84		/* This is meant to be the date the goods are received - it does not make sense to set this to the date that we requested delivery in the purchase order - I have not applied your change here Tim for this reason - let me know if I have it wrong - Phil */
85		$_POST['DefaultReceivedDate'] = Date($_SESSION['DefaultDateFormat']);
86		$_SESSION['PO' . $identifier]->DefaultReceivedDate = $_POST['DefaultReceivedDate'];
87	} else {
88		if (isset($_POST['DefaultReceivedDate']) AND is_date($_POST['DefaultReceivedDate'])) {
89			$_SESSION['PO' . $identifier]->DefaultReceivedDate = $_POST['DefaultReceivedDate'];
90		} elseif(isset($_POST['DefaultReceivedDate']) AND !is_date($_POST['DefaultReceivedDate'])) {
91			prnMsg(_('The default received date is not a date format'),'error');
92			$_POST['DefaultReceivedDate'] = Date($_SESSION['DefaultDateFormat']);
93		}
94	}
95	if (!isset($_POST['SupplierReference'])) {
96		$_POST['SupplierReference'] = '';
97	} else {
98		if (isset($_POST['SupplierReference']) AND mb_strlen(trim($_POST['SupplierReference']))>30) {
99			prnMsg(_('The supplier\'s delivery note no should not be more than 30 characters'),'error');
100		} else {
101			$_SESSION['PO' . $identifier]->SupplierReference = $_POST['SupplierReference'];
102		}
103	}
104	$SupplierReference = isset($_SESSION['PO' . $identifier]->SupplierReference)? $_SESSION['PO' . $identifier]->SupplierReference: $_POST['SupplierReference'];
105
106	echo '<table class="selection">
107			<tr>
108				<td>' .  _('Date Goods/Service Received'). ':</td>
109				<td><input type="text" class="date" maxlength="10" size="11" onchange="return isDate(this, this.value, '."'".
110			$_SESSION['DefaultDateFormat']."'".')" name="DefaultReceivedDate" value="' . $_SESSION['PO' . $identifier]->DefaultReceivedDate . '" /></td>
111				<td>' . _("Supplier's Reference") . ':</td>
112				<td><input type="text" name="SupplierReference" value="' . $SupplierReference. '" maxlength="30" size="20"  onchange="ReloadForm(form1.Update)"/></td>
113			</tr>
114		</table>
115		<br />',
116		'<table cellpadding="2" class="selection">
117			<tr><th colspan="2">&nbsp;</th>
118				<th class="centre" colspan="4"><b>', _('Supplier Units'), '</b></th>
119				<th>&nbsp;</th>
120				<th class="centre" colspan="6"><b>', _('Our Receiving Units'), '</b></th>';
121	if ($_SESSION['ShowValueOnGRN'] == 1) {
122		echo '<th colspan="2">&nbsp;</th>';
123	}
124	echo	'</tr>
125			<tr>
126				<th>', _('Item Code'), '</th>
127				<th>', _('Supplier') . '<br />'. _('Item'), '</th>
128				<th>', _('Description'), '</th>
129				<th>', _('Quantity Ordered'), '</th>
130				<th>', _('Units'), '</th>
131				<th>', _('Already Received'), '</th>
132				<th>', _('Conversion Factor'), '</th>
133				<th>', _('Quantity Ordered'), '</th>
134				<th>', _('Units'), '</th>
135				<th>', _('Already Received'), '</th>
136				<th>', _('Delivery Date'), '</th>
137				<th>', _('This Delivery') . '<br />' . _('Quantity'), '</th>
138				<th>', _('Completed'), '</th>';
139
140	if ($_SESSION['ShowValueOnGRN'] == 1) {
141		echo '<th>', _('Price'), '</th>
142				<th>', _('Total Value') . '<br />' . _('Received'), '</th>';
143	}
144
145	echo '</tr>';
146	/*show the line items on the order with the quantity being received for modification */
147
148	$_SESSION['PO'.$identifier]->Total = 0;
149}
150
151if (count($_SESSION['PO'.$identifier]->LineItems)>0 and !isset($_POST['ProcessGoodsReceived'])) {
152
153	foreach ($_SESSION['PO'.$identifier]->LineItems as $LnItm) {
154
155	/*  if ($LnItm->ReceiveQty==0) {   /*If no quantities yet input default the balance to be received
156			$LnItm->ReceiveQty = $LnItm->QuantityOrd - $LnItm->QtyReceived;
157		}
158	*/
159
160	/*Perhaps better to default quantities to 0 BUT.....if you wish to have the receive quantities
161	default to the balance on order then just remove the comments around the 3 lines above */
162
163	//Setup & Format values for LineItem display
164
165		$LineTotal = ($LnItm->ReceiveQty * $LnItm->Price );
166		$_SESSION['PO'.$identifier]->Total = $_SESSION['PO'.$identifier]->Total + $LineTotal;
167		$DisplaySupplierQtyOrd = locale_number_format($LnItm->Quantity/$LnItm->ConversionFactor,$LnItm->DecimalPlaces);
168		$DisplaySupplierQtyRec = locale_number_format($LnItm->QtyReceived/$LnItm->ConversionFactor,$LnItm->DecimalPlaces);
169		$DisplayQtyOrd = locale_number_format($LnItm->Quantity,$LnItm->DecimalPlaces);
170		$DisplayQtyRec = locale_number_format($LnItm->QtyReceived,$LnItm->DecimalPlaces);
171		$DisplayLineTotal = locale_number_format($LineTotal,$_SESSION['PO'.$identifier]->CurrDecimalPlaces);
172		 if ($LnItm->Price > 1) {
173			$DisplayPrice = locale_number_format($LnItm->Price,$_SESSION['PO'.$identifier]->CurrDecimalPlaces);
174		} else {
175			$DisplayPrice = locale_number_format($LnItm->Price,4);
176		}
177
178
179		//Now Display LineItem
180		$SupportedImgExt = array('png','jpg','jpeg');
181		$imagefile = reset((glob($_SESSION['part_pics_dir'] . '/' . $LnItm->StockID . '.{' . implode(",", $SupportedImgExt) . '}', GLOB_BRACE)));
182		if ($imagefile) {
183			$ImageLink = '<a href="' . $imagefile . '" target="_blank">' .  $LnItm->StockID . '</a>';
184		} else {
185			$ImageLink = $LnItm->StockID;
186		}
187
188		echo '<tr class="striped_row">
189			<td>' . $ImageLink . '</td>
190			<td>' . $LnItm->Suppliers_PartNo . '</td>
191			<td>' . $LnItm->ItemDescription . '</td>
192			<td class="number">' . $DisplaySupplierQtyOrd . '</td>
193			<td>' . $LnItm->SuppliersUnit . '</td>
194			<td class="number">' . $DisplaySupplierQtyRec . '</td>
195			<td class="number">' . $LnItm->ConversionFactor . '</td>
196			<td class="number">' . $DisplayQtyOrd . '</td>
197			<td>' . $LnItm->Units . '</td>
198			<td class="number">' . $DisplayQtyRec . '</td>
199			<td>' . $LnItm->ReqDelDate . '</td>
200			<td class="number">';
201
202		if ($LnItm->Controlled == 1) {
203
204			echo '<input type="hidden" name="RecvQty_' . $LnItm->LineNo . '" autofocus="autofocus" value="' . locale_number_format($LnItm->ReceiveQty,$LnItm->DecimalPlaces) . '" /><a href="GoodsReceivedControlled.php?identifier=' . $identifier . '&amp;LineNo=' . $LnItm->LineNo . '">' . locale_number_format($LnItm->ReceiveQty,$LnItm->DecimalPlaces) . '</a></td>';
205
206		} else {
207			echo '<input type="text" class="number" name="RecvQty_' . $LnItm->LineNo . '" pattern="(?:^\d{1,3}(?:\.?\d{3})*(?:,\d{1,})?$)|(?:^\d{1,3}(?:,?\d{3})*(?:\.\d{1,})?$)|(?:^\d{1,3}(?:\s?\d{3})*(?:\.\d{1,})?$)|(?:^\d{1,3}(?:\s?\d{3})*(?:,\d{1,})?$)|(?:^(\d{1,2},)?(\d{2},)*(\d{3})(\.\d+)?|(\d{1,3})(\.\d+)?$)" title="' . _('Enter the quantity to receive against this order line as a number') . '"
208maxlength="10" size="10" value="' . locale_number_format(round($LnItm->ReceiveQty,$LnItm->DecimalPlaces),$LnItm->DecimalPlaces) . '" /></td>';
209		}
210		echo '<td><input type="checkbox" name="Complete_'. $LnItm->LineNo . '"';
211		if ($LnItm->Completed ==1) {
212			echo ' checked';
213		}
214		echo ' /></td>';
215
216		if ($_SESSION['ShowValueOnGRN'] == 1) {
217			echo '<td class="number">', $DisplayPrice, '</td>',
218				'<td class="number">', $DisplayLineTotal, '</td>';
219		}
220
221
222		if ($LnItm->Controlled == 1) {
223			if ($LnItm->Serialised==1) {
224				echo '<td><a href="GoodsReceivedControlled.php?identifier=' . $identifier . '&amp;LineNo=' . $LnItm->LineNo . '">' .
225					_('Enter Serial Nos'). '</a></td>';
226			} else {
227				echo '<td><a href="GoodsReceivedControlled.php?identifier=' . $identifier . '&amp;LineNo=' . $LnItm->LineNo . '">' .
228					_('Enter Batches'). '</a></td>';
229			}
230		}
231		echo '</tr>';
232	}//foreach(LineItem)
233	$DisplayTotal = locale_number_format($_SESSION['PO'.$identifier]->Total,$_SESSION['PO'.$identifier]->CurrDecimalPlaces);
234	if ($_SESSION['ShowValueOnGRN'] == 1) {
235		echo '<tr>
236				<td class="number" colspan="14"><b>', _('Total value of goods received'), '</b></td>
237				<td class="number"><b>',  $DisplayTotal, '</b></td>
238			</tr>';
239	}
240	echo '</table>';
241
242}//If count(LineItems) > 0
243
244
245/************************* LINE ITEM VALIDATION ************************/
246
247/* Check whether trying to deliver more items than are recorded on the purchase order
248(+ overreceive allowance) */
249
250$DeliveryQuantityTooLarge = 0;
251$NegativesFound = false;
252$InputError = false;
253
254if (isset($_POST['DefaultReceivedDate']) AND !is_date($_POST['DefaultReceivedDate'])) {
255	$InputError = true;
256	prnMsg(_('The goods received date is not a date format'),'error');
257
258}
259
260if (isset($_POST['SupplierReference']) AND mb_strlen(trim($_POST['SupplierReference']))>30) {
261	$InputError = true;
262	prnMsg(_('The delivery note of suppliers should not be more than 30 characters'),'error');
263}
264if (count($_SESSION['PO'.$identifier]->LineItems)>0) {
265
266	foreach ($_SESSION['PO'.$identifier]->LineItems as $OrderLine) {
267
268		if ($OrderLine->ReceiveQty+$OrderLine->QtyReceived > $OrderLine->Quantity * (1+ ($_SESSION['OverReceiveProportion'] / 100))) {
269			$DeliveryQuantityTooLarge =1;
270			$InputError = true;
271		}
272		if ($OrderLine->ReceiveQty < 0 AND $_SESSION['ProhibitNegativeStock']==1) {
273
274			$SQL = "SELECT locstock.quantity
275						FROM locstock
276						WHERE locstock.stockid='" . $OrderLine->StockID . "'
277						AND loccode= '" . $_SESSION['PO'.$identifier]->Location . "'";
278
279			$CheckNegResult = DB_query($SQL);
280			$CheckNegRow = DB_fetch_row($CheckNegResult);
281			if ($CheckNegRow[0]+$OrderLine->ReceiveQty<0) {
282				$NegativesFound=true;
283				prnMsg(_('Receiving a negative quantity that results in negative stock is prohibited by the parameter settings. This delivery of stock cannot be processed until the stock of the item is corrected.'),'error',$OrderLine->StockID . ' Cannot Go Negative');
284			}
285		} /*end if ReceiveQty negative and not allowed negative stock */
286	} /* end loop around the items received */
287} /* end if there are lines received */
288
289if ($_SESSION['PO'.$identifier]->SomethingReceived()==0 AND isset($_POST['ProcessGoodsReceived'])) { /*Then dont bother proceeding cos nothing to do ! */
290
291	prnMsg(_('There is nothing to process') . '. ' . _('Please enter valid quantities greater than zero'),'warn');
292	echo '<div class="centre"><input type="submit" name="Update" value="' . _('Update') . '" /></div>';
293
294} elseif ($NegativesFound) {
295
296	prnMsg(_('Negative stocks would result by processing a negative delivery - quantities must be changed or the stock quantity of the item going negative corrected before this delivery will be processed.'),'error');
297
298	echo '<div class="centre"><input type="submit" name="Update" value="' . _('Update') . '" />';
299
300} elseif ($DeliveryQuantityTooLarge==1 AND isset($_POST['ProcessGoodsReceived'])) {
301
302	prnMsg(_('Entered quantities cannot be greater than the quantity entered on the purchase invoice including the allowed over-receive percentage'). ' ' . '(' . $_SESSION['OverReceiveProportion'] .'%)','error');
303	echo '<br />';
304	prnMsg(_('Modify the ordered items on the purchase invoice if you wish to increase the quantities'),'info');
305	echo '<div class="centre"><input type="submit" name="Update" value="' . _('Update') . '" />';
306
307}  elseif (isset($_POST['ProcessGoodsReceived']) AND $_SESSION['PO'.$identifier]->SomethingReceived()==1 AND $InputError == false) {
308
309/* SQL to process the postings for goods received... */
310/* Company record set at login for information on GL Links and debtors GL account*/
311
312
313	if ($_SESSION['CompanyRecord']==0) {
314		/*The company data and preferences could not be retrieved for some reason */
315        echo '</div>';
316        echo '</form>';
317		prnMsg(_('The company information and preferences could not be retrieved') . ' - ' . _('see your system administrator') , 'error');
318		include('includes/footer.php');
319		exit;
320	}
321
322/*Now need to check that the order details are the same as they were when they were read into the Items array. If they have changed then someone else must have altered them */
323// Otherwise if you try to fullfill item quantities separately will give error.
324	$SQL = "SELECT itemcode,
325					glcode,
326					quantityord,
327					quantityrecd,
328					qtyinvoiced,
329					shiptref,
330					jobref
331			FROM purchorderdetails
332			WHERE orderno='" . (int) $_SESSION['PO'.$identifier]->OrderNo . "'
333			AND completed=0
334			ORDER BY podetailitem";
335
336	$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Could not check that the details of the purchase order had not been changed by another user because'). ':';
337	$DbgMsg = _('The following SQL to retrieve the purchase order details was used');
338	$Result=DB_query($SQL, $ErrMsg, $DbgMsg);
339
340	$Changes=0;
341	$LineNo=1;
342	if(DB_num_rows($Result)==0) {//Those goods must have been received by another user. So should destroy the session data and show warning to users
343		prnMsg(_('This order has been changed or invoiced since this delivery was started to be actioned').' . '._('Processing halted'),'error');
344		echo '<div class="centre"><a href="' . $RootPath . '/PO_SelectOSPurchOrder.php">' .
345			_('Select a different purchase order for receiving goods against') . '</a></div>';
346		unset($_SESSION['PO'.$identifier]->LineItems);
347		unset($_SESSION['PO'.$identifier]);
348		unset($_POST['ProcessGoodsReceived']);
349		echo '</div>';
350		echo '</form>';
351		include ('includes/footer.php');
352		exit;
353	}
354	while ($myrow = DB_fetch_array($Result)) {
355
356		if ($_SESSION['PO'.$identifier]->LineItems[$LineNo]->GLCode != $myrow['glcode'] OR
357			$_SESSION['PO'.$identifier]->LineItems[$LineNo]->ShiptRef != $myrow['shiptref'] OR
358			$_SESSION['PO'.$identifier]->LineItems[$LineNo]->JobRef != $myrow['jobref'] OR
359			$_SESSION['PO'.$identifier]->LineItems[$LineNo]->QtyInv != $myrow['qtyinvoiced'] OR
360			$_SESSION['PO'.$identifier]->LineItems[$LineNo]->StockID != $myrow['itemcode'] OR
361			$_SESSION['PO'.$identifier]->LineItems[$LineNo]->Quantity != $myrow['quantityord'] OR
362			$_SESSION['PO'.$identifier]->LineItems[$LineNo]->QtyReceived != $myrow['quantityrecd']) {
363
364
365			prnMsg(_('This order has been changed or invoiced since this delivery was started to be actioned') . '. ' . _('Processing halted') . '. ' . _('To enter a delivery against this purchase order') . ', ' . _('it must be re-selected and re-read again to update the changes made by the other user'),'warn');
366
367			if ($debug==1) {
368				echo '<table class="selection">
369					<tr>
370						<td>' . _('GL Code of the Line Item') . ':</td>
371						<td>' . $_SESSION['PO'.$identifier]->LineItems[$LineNo]->GLCode . '</td>
372						<td>' . $myrow['glcode'] . '</td>
373					</tr>
374					<tr>
375						<td>' . _('ShiptRef of the Line Item') . ':</td>
376						<td>' . $_SESSION['PO'.$identifier]->LineItems[$LineNo]->ShiptRef . '</td>
377						<td>' . $myrow['shiptref'] . '</td>
378					</tr>
379					<tr>
380						<td>' . _('Contract Reference of the Line Item') . ':</td>
381						<td>' . $_SESSION['PO'.$identifier]->LineItems[$LineNo]->JobRef . '</td>
382						<td>' . $myrow['jobref'] . '</td>
383					</tr>
384					<tr>
385						<td>' . _('Quantity Invoiced of the Line Item') . ':</td>
386						<td>' . locale_number_format($_SESSION['PO'.$identifier]->LineItems[$LineNo]->QtyInv,$_SESSION['PO'.$identifier]->LineItems[$LineNo]->DecimalPlaces) . '</td>
387						<td>' . $myrow['qtyinvoiced'] . '</td>
388					</tr>
389					<tr>
390						<td>' . _('Stock Code of the Line Item') . ':</td>
391						<td>' .  $_SESSION['PO'.$identifier]->LineItems[$LineNo]->StockID . '</td>
392						<td>' . $myrow['itemcode'] . '</td>
393					</tr>
394					<tr>
395						<td>' . _('Order Quantity of the Line Item') . ':</td>
396						<td>' . locale_number_format($_SESSION['PO'.$identifier]->LineItems[$LineNo]->Quantity,$_SESSION['PO'.$identifier]->LineItems[$LineNo]->DecimalPlaces) . '</td>
397						<td>' . $myrow['quantityord'] . '</td>
398					</tr>
399					<tr>
400						<td>' . _('Quantity of the Line Item Already Received') . ':</td>
401						<td>' . locale_number_format($_SESSION['PO'.$identifier]->LineItems[$LineNo]->QtyReceived,$_SESSION['PO'.$identifier]->LineItems[$LineNo]->DecimalPlaces) . '</td>
402						<td>' . locale_number_format($myrow['quantityrecd'],$_SESSION['PO'.$identifier]->LineItems[$LineNo]->DecimalPlaces) . '</td>
403					</tr>
404					</table>';
405			}
406			echo '<div class="centre"><a href="' . $RootPath . '/PO_SelectOSPurchOrder.php">' .
407				_('Select a different purchase order for receiving goods against') . '</a></div>';
408			echo '<div class="centre"><a href="' . $RootPath . '/GoodsReceived.php?PONumber=' . $_SESSION['PO'.$identifier]->OrderNo . '">' .  _('Re-read the updated purchase order for receiving goods against'). '</a></div>';
409			unset($_SESSION['PO'.$identifier]->LineItems);
410			unset($_SESSION['PO'.$identifier]);
411			unset($_POST['ProcessGoodsReceived']);
412            echo '</div>';
413            echo '</form>';
414			include ('includes/footer.php');
415			exit;
416		}
417		$LineNo++;
418	} /*loop through all line items of the order to ensure none have been invoiced */
419
420	DB_free_result($Result);
421
422/* *********************** BEGIN SQL TRANSACTIONS *********************** */
423
424	$Result = DB_Txn_Begin();
425/*Now Get the next GRN - function in SQL_CommonFunctions*/
426	$GRN = GetNextTransNo(25);
427
428	$PeriodNo = GetPeriod($_POST['DefaultReceivedDate']);
429	$_POST['DefaultReceivedDate'] = FormatDateForSQL($_POST['DefaultReceivedDate']);
430	$OrderCompleted = true; //assume all received and completed - now test in case not
431	foreach ($_SESSION['PO'.$identifier]->LineItems as $OrderLine) {
432		if ($OrderLine->Completed ==0) {
433			$OrderCompleted = false;
434		}
435		if ($OrderLine->ReceiveQty !=0 AND $OrderLine->ReceiveQty!='' AND isset($OrderLine->ReceiveQty)) {
436
437			$LocalCurrencyPrice = ($OrderLine->Price / $_SESSION['PO'.$identifier]->ExRate);
438
439			if ($OrderLine->StockID!='') { //Its a stock item line
440				/*Need to get the current standard cost as it is now so we can process GL jorunals later*/
441				$SQL = "SELECT materialcost + labourcost + overheadcost as stdcost,mbflag
442							FROM stockmaster
443							WHERE stockid='" . $OrderLine->StockID . "'";
444				$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The standard cost of the item being received cannot be retrieved because');
445				$DbgMsg = _('The following SQL to retrieve the standard cost was used');
446				$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
447
448				$myrow = DB_fetch_row($Result);
449				if($myrow[1] != 'D') {
450					if ($OrderLine->QtyReceived==0) { //its the first receipt against this line
451						$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = $myrow[0];
452					}
453					$CurrentStandardCost = $myrow[0];
454					/*Set the purchase order line stdcostunit = weighted average / standard cost used for all receipts of this line
455				 		This assures that the quantity received against the purchase order line multiplied by the weighted average of standard
456				 		costs received = the total of standard cost posted to GRN suspense*/
457					$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = (($CurrentStandardCost * $OrderLine->ReceiveQty) + ($_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost * $OrderLine->QtyReceived)) / ($OrderLine->ReceiveQty + $OrderLine->QtyReceived);
458				} elseif ($myrow[1] == 'D') { //it's a dummy part which without stock.
459					$Dummy = true;
460					if($OrderLine->QtyReceived == 0) {//There is
461						$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = $LocalCurrencyPrice;
462					}
463				}
464
465			} elseif ($OrderLine->QtyReceived==0 AND $OrderLine->StockID=='') {
466				/*Its a nominal item being received */
467				/*Need to record the value of the order per unit in the standard cost field to ensure GRN account entries clear */
468				$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost = $LocalCurrencyPrice;
469			}
470
471			if ($OrderLine->StockID=='' OR !empty($Dummy)) { /*Its a NOMINAL item line */
472				$CurrentStandardCost = $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost;
473			}
474
475/*Now the SQL to do the update to the PurchOrderDetails */
476
477			if ($OrderLine->ReceiveQty >= ($OrderLine->Quantity - $OrderLine->QtyReceived)) {
478				$SQL = "UPDATE purchorderdetails SET quantityrecd = quantityrecd + '" . $OrderLine->ReceiveQty . "',
479													stdcostunit='" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost . "',
480													completed=1
481						WHERE podetailitem = '" . $OrderLine->PODetailRec . "'";
482			} else {
483				$SQL = "UPDATE purchorderdetails SET
484												quantityrecd = quantityrecd + '" . $OrderLine->ReceiveQty . "',
485												stdcostunit='" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost . "',
486												completed='" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->Completed . "'
487										WHERE podetailitem = '" . $OrderLine->PODetailRec . "'";
488			}
489
490			$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The purchase order detail record could not be updated with the quantity received because');
491			$DbgMsg = _('The following SQL to update the purchase order detail record was used');
492			$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
493
494
495			if ($OrderLine->StockID !='' AND !isset($Dummy)) { /*Its a stock item so use the standard cost for the journals */
496				$UnitCost = $CurrentStandardCost;
497			} else {  /*otherwise its a nominal PO item so use the purchase cost converted to local currency */
498				$UnitCost = $OrderLine->Price / $_SESSION['PO'.$identifier]->ExRate;
499			}
500
501/*Need to insert a GRN item */
502
503			$SQL = "INSERT INTO grns (grnbatch,
504									podetailitem,
505									itemcode,
506									itemdescription,
507									deliverydate,
508									qtyrecd,
509									supplierid,
510									stdcostunit,
511									supplierref)
512							VALUES ('" . $GRN . "',
513								'" . $OrderLine->PODetailRec . "',
514								'" . $OrderLine->StockID . "',
515								'" . DB_escape_string($OrderLine->ItemDescription) . "',
516								'" . $_POST['DefaultReceivedDate'] . "',
517								'" . $OrderLine->ReceiveQty . "',
518								'" . $_SESSION['PO'.$identifier]->SupplierID . "',
519								'" . $CurrentStandardCost . "',
520								'" . trim($_POST['SupplierReference']) ."')";
521
522			$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('A GRN record could not be inserted') . '. ' . _('This receipt of goods has not been processed because');
523			$DbgMsg =  _('The following SQL to insert the GRN record was used');
524			$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
525
526			if ($OrderLine->StockID!='') { /* if the order line is in fact a stock item */
527
528/* Update location stock records - NB  a PO cannot be entered for a dummy/assembly/kit parts */
529
530/* Need to get the current location quantity will need it later for the stock movement */
531				$SQL="SELECT locstock.quantity
532								FROM locstock
533								WHERE locstock.stockid='" . $OrderLine->StockID . "'
534								AND loccode= '" . $_SESSION['PO'.$identifier]->Location . "'";
535
536				$Result = DB_query($SQL);
537				if (DB_num_rows($Result)==1) {
538					$LocQtyRow = DB_fetch_row($Result);
539					$QtyOnHandPrior = $LocQtyRow[0];
540				} else {
541					/*There must actually be some error this should never happen */
542					$QtyOnHandPrior = 0;
543				}
544
545				$SQL = "UPDATE locstock
546							SET quantity = locstock.quantity + '" . $OrderLine->ReceiveQty . "'
547						WHERE locstock.stockid = '" . $OrderLine->StockID . "'
548						AND loccode = '" . $_SESSION['PO'.$identifier]->Location . "'";
549
550				$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The location stock record could not be updated because');
551				$DbgMsg =  _('The following SQL to update the location stock record was used');
552				$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
553
554	/* Insert stock movements - with unit cost */
555
556				$SQL = "INSERT INTO stockmoves (stockid,
557												type,
558												transno,
559												loccode,
560												trandate,
561												userid,
562												price,
563												prd,
564												reference,
565												qty,
566												standardcost,
567												newqoh)
568									VALUES (
569										'" . $OrderLine->StockID . "',
570										25,
571										'" . $GRN . "',
572										'" . $_SESSION['PO'.$identifier]->Location . "',
573										'" . $_POST['DefaultReceivedDate'] . "',
574										'" . $_SESSION['UserID'] . "',
575										'" . $LocalCurrencyPrice . "',
576										'" . $PeriodNo . "',
577										'" . $_SESSION['PO'.$identifier]->SupplierID . " (" . DB_escape_string($_SESSION['PO'.$identifier]->SupplierName) . ") - " .$_SESSION['PO'.$identifier]->OrderNo . "',
578										'" . $OrderLine->ReceiveQty . "',
579										'" . $_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->StandardCost . "',
580										'" . ($QtyOnHandPrior + $OrderLine->ReceiveQty) . "'
581										)";
582
583				$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('stock movement records could not be inserted because');
584				$DbgMsg =  _('The following SQL to insert the stock movement records was used');
585				$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
586
587				/*Get the ID of the StockMove... */
588				$StkMoveNo = DB_Last_Insert_ID('stockmoves','stkmoveno');
589				/* Do the Controlled Item INSERTS HERE */
590
591				if ($OrderLine->Controlled ==1) {
592					foreach($OrderLine->SerialItems as $Item) {
593						/* we know that StockItems return an array of SerialItem (s)
594						 We need to add the StockSerialItem record and
595						 The StockSerialMoves as well */
596						//need to test if the controlled item exists first already
597							$SQL = "SELECT COUNT(*) FROM stockserialitems
598									WHERE stockid='" . $OrderLine->StockID . "'
599									AND loccode = '" . $_SESSION['PO'.$identifier]->Location . "'
600									AND serialno = '" . $Item->BundleRef . "'";
601							$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Could not check if a batch or lot stock item already exists because');
602							$DbgMsg =  _('The following SQL to test for an already existing controlled but not serialised stock item was used');
603							$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
604							$AlreadyExistsRow = DB_fetch_row($Result);
605							if (trim($Item->BundleRef) != '') {
606								if ($AlreadyExistsRow[0]>0) {
607									if ($OrderLine->Serialised == 1) {
608										$SQL = "UPDATE stockserialitems SET quantity = '" . $Item->BundleQty . "'";
609									} else {
610										$SQL = "UPDATE stockserialitems SET quantity = quantity + '" . $Item->BundleQty . "'";
611									}
612									$SQL .= "WHERE stockid='" . $OrderLine->StockID . "'
613											 AND loccode = '" . $_SESSION['PO'.$identifier]->Location . "'
614											 AND serialno = '" . $Item->BundleRef . "'";
615								} else {
616									$SQL = "INSERT INTO stockserialitems (stockid,
617																			loccode,
618																			serialno,
619																			qualitytext,
620																			expirationdate,
621																			quantity)
622																		VALUES ('" . $OrderLine->StockID . "',
623																			'" . $_SESSION['PO'.$identifier]->Location . "',
624																			'" . $Item->BundleRef . "',
625																			'',
626																			'" . FormatDateForSQL($Item->ExpiryDate) . "',
627																			'" . $Item->BundleQty . "')";
628								}
629
630								$ErrMsg =  _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock item record could not be inserted because');
631								$DbgMsg =  _('The following SQL to insert the serial stock item records was used');
632								$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
633
634								/* end of handle stockserialitems records */
635
636							/** now insert the serial stock movement **/
637							$SQL = "INSERT INTO stockserialmoves (stockmoveno,
638																	stockid,
639																	serialno,
640																	moveqty)
641															VALUES (
642																'" . $StkMoveNo . "',
643																'" . $OrderLine->StockID . "',
644																'" . $Item->BundleRef . "',
645																'" . $Item->BundleQty . "'
646																)";
647							$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock movement record could not be inserted because');
648							$DbgMsg = _('The following SQL to insert the serial stock movement records was used');
649							$Result = DB_query($SQL, $ErrMsg, $DbgMsg, true);
650							if ($_SESSION['QualityLogSamples']==1) {
651								CreateQASample($OrderLine->StockID,$Item->BundleRef, '', 'Created from Purchase Order', 0, 0);
652							}
653						}//non blank BundleRef
654					} //end foreach
655				}
656			} /*end of its a stock item - updates to locations and insert movements*/
657
658			/* Check to see if the line item was flagged as the purchase of an asset */
659			if ($OrderLine->AssetID !='' AND $OrderLine->AssetID !='0') { //then it is an asset
660
661				/*first validate the AssetID and if it doesn't exist treat it like a normal nominal item  */
662				$CheckAssetExistsResult = DB_query("SELECT assetid,
663															datepurchased,
664															costact
665													FROM fixedassets
666													INNER JOIN fixedassetcategories
667													ON fixedassets.assetcategoryid=fixedassetcategories.categoryid
668													WHERE assetid='" . $OrderLine->AssetID . "'");
669				if (DB_num_rows($CheckAssetExistsResult)==1) { //then work with the assetid provided
670
671					/*Need to add a fixedassettrans for the cost of the asset being received */
672					$SQL = "INSERT INTO fixedassettrans (assetid,
673														transtype,
674														transno,
675														transdate,
676														periodno,
677														inputdate,
678														fixedassettranstype,
679														amount)
680									VALUES ('" . $OrderLine->AssetID . "',
681											25,
682											'" . $GRN . "',
683											'" . $_POST['DefaultReceivedDate'] . "',
684											'" . $PeriodNo . "',
685											'" . Date('Y-m-d') . "',
686											'" . _('cost') . "',
687											'" . $CurrentStandardCost * $OrderLine->ReceiveQty . "')";
688					$ErrMsg = _('CRITICAL ERROR! NOTE DOWN THIS ERROR AND SEEK ASSISTANCE The fixed asset transaction could not be inserted because');
689					$DbgMsg = _('The following SQL to insert the fixed asset transaction record was used');
690					$Result = DB_query($SQL,$ErrMsg, $DbgMsg, true);
691
692					/*Now get the correct cost GL account from the asset category */
693					$AssetRow = DB_fetch_array($CheckAssetExistsResult);
694					/*Over-ride any GL account specified in the order with the asset category cost account */
695					$_SESSION['PO'.$identifier]->LineItems[$OrderLine->LineNo]->GLCode = $AssetRow['costact'];
696					/*Now if there are no previous additions to this asset update the date purchased */
697					if ($AssetRow['datepurchased']=='0000-00-00') {
698						/* it is a new addition as the date is set to 0000-00-00 when the asset record is created
699						 * before any cost is added to the asset
700						 */
701						$SQL = "UPDATE fixedassets
702									SET datepurchased='" . $_POST['DefaultReceivedDate'] . "',
703										cost = cost + " . ($CurrentStandardCost * $OrderLine->ReceiveQty)  . "
704									WHERE assetid = '" . $OrderLine->AssetID . "'";
705					} else {
706							$SQL = "UPDATE fixedassets SET cost = cost + " . ($CurrentStandardCost * $OrderLine->ReceiveQty)  . "
707									WHERE assetid = '" . $OrderLine->AssetID . "'";
708					}
709					$ErrMsg = _('CRITICAL ERROR! NOTE DOWN THIS ERROR AND SEEK ASSISTANCE. The fixed asset cost and date purchased was not able to be updated because:');
710					$DbgMsg = _('The following SQL was used to attempt the update of the cost and the date the asset was purchased');
711					$Result = DB_query($SQL,$ErrMsg, $DbgMsg, true);
712
713				} //assetid provided doesn't exist so ignore it and treat as a normal nominal item
714			} //assetid is set so the nominal item is an asset
715
716			/* If GLLink_Stock then insert GLTrans to debit the GL Code  and credit GRN Suspense account at standard cost*/
717			if ($_SESSION['PO'.$identifier]->GLLink==1 AND $OrderLine->GLCode !=0) {
718				/*GLCode is set to 0 when the GLLink is not activated this covers a situation where the GLLink is now active but it wasn't when this PO was entered */
719
720				/*first the debit using the GLCode in the PO detail record entry*/
721				$SQL = "INSERT INTO gltrans (type,
722											typeno,
723											trandate,
724											periodno,
725											account,
726											narrative,
727											amount)
728									VALUES (
729											25,'" .
730											$GRN . "','" .
731											$_POST['DefaultReceivedDate'] . "','" .
732											$PeriodNo . "','" .
733											$OrderLine->GLCode . "','" .
734											_('PO') . ' ' . $_SESSION['PO'.$identifier]->OrderNo . ': ' . $_SESSION['PO'.$identifier]->SupplierID . ' - ' . $OrderLine->StockID . ' - ' . DB_escape_string($OrderLine->ItemDescription) . ' x ' . $OrderLine->ReceiveQty . " @ " . locale_number_format($CurrentStandardCost,$_SESSION['CompanyRecord']['decimalplaces']) . "','" .
735											$CurrentStandardCost * $OrderLine->ReceiveQty . "')";
736
737				$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The purchase GL posting could not be inserted because');
738				$DbgMsg = _('The following SQL to insert the purchase GLTrans record was used');
739				$Result = DB_query($SQL,$ErrMsg, $DbgMsg, true);
740
741				/* If the CurrentStandardCost != UnitCost (the standard at the time the first delivery was booked in,  and its a stock item, then the difference needs to be booked in against the purchase price variance account */
742
743				/*now the GRN suspense entry*/
744				$SQL = "INSERT INTO gltrans (type,
745											typeno,
746											trandate,
747											periodno,
748											account,
749											narrative,
750											amount)
751									VALUES (25,'" .
752											$GRN . "','" .
753											$_POST['DefaultReceivedDate'] . "','" .
754											$PeriodNo . "','" .
755											$_SESSION['CompanyRecord']['grnact'] . "','" .
756											_('PO') . ' ' . $_SESSION['PO'.$identifier]->OrderNo . ': ' . $_SESSION['PO'.$identifier]->SupplierID . ' - ' . $OrderLine->StockID . ' - ' . DB_escape_string($OrderLine->ItemDescription) . ' x ' . $OrderLine->ReceiveQty . ' @ ' . locale_number_format($UnitCost,$_SESSION['CompanyRecord']['decimalplaces']) . "','" .
757											-$UnitCost * $OrderLine->ReceiveQty . "')";
758
759				$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The GRN suspense side of the GL posting could not be inserted because');
760				$DbgMsg = _('The following SQL to insert the GRN Suspense GLTrans record was used');
761				$Result = DB_query($SQL, $ErrMsg, $DbgMsg,true);
762
763			} /* end of if GL and stock integrated and standard cost !=0 */
764		} /*Quantity received is != 0 */
765	} /*end of OrderLine loop */
766
767	if ($_SESSION['PO'.$identifier]->AllLinesReceived()==1 OR $OrderCompleted) { //all lines on the purchase order are now completed
768		$StatusComment=date($_SESSION['DefaultDateFormat']) .' - ' . _('Order Completed on entry of GRN')  . '<br />' . $_SESSION['PO'.$identifier]->StatusComments;
769		$sql="UPDATE purchorders
770				SET status='Completed',
771				stat_comment='" . $StatusComment . "'
772				WHERE orderno='" . $_SESSION['PO'.$identifier]->OrderNo . "'";
773		$result=DB_query($sql);
774	}
775
776	if ($_SESSION['PO'.$identifier]->GLLink==1) {
777		EnsureGLEntriesBalance(25, $GRN);
778	}
779
780	$Result = DB_Txn_Commit();
781	$PONo = $_SESSION['PO'.$identifier]->OrderNo;
782	unset($_SESSION['PO'.$identifier]->LineItems);
783	unset($_SESSION['PO'.$identifier]);
784	unset($_POST['ProcessGoodsReceived']);
785
786	echo '<br />
787		<div class="centre">
788			'. prnMsg(_('GRN number'). ' '. $GRN .' '. _('has been processed'),'success') . '
789			<br />
790			<br />
791			<a href="PDFGrn.php?GRNNo='.$GRN .'&amp;PONo='.$PONo.'">' .  _('Print this Goods Received Note (GRN)') . '</a>
792			<br />
793			<br />
794			<a href="PDFQALabel.php?GRNNo='.$GRN .'&amp;PONo='.$PONo.'">' .  _('Print QA Labels for this Receipt') . '</a>
795			<br />
796			<br />
797			<a href="' . $RootPath . '/PO_SelectOSPurchOrder.php">' . _('Select a different purchase order for receiving goods against'). '</a>
798		</div>';
799/*end of process goods received entry */
800    echo '</div>';
801    echo '</form>';
802	include('includes/footer.php');
803	exit;
804
805} else { /*Process Goods received not set so show a link to allow mod of line items on order and allow input of date goods received*/
806
807	echo '<br />
808		<div class="centre">
809			<a href="' . $RootPath . '/PO_Header.php?ModifyOrderNumber=' .$_SESSION['PO'.$identifier]->OrderNo . '">' . _('Modify Order Items'). '</a>
810		</div>
811		<br />
812		<div class="centre">
813			<input type="submit" name="Update" value="' . _('Update') . '" />
814			<br />
815			<br />
816			<input type="submit" name="ProcessGoodsReceived" value="' . _('Process Goods Received') . '" />
817		</div>';
818}
819echo '</div>';
820echo '</form>';
821include('includes/footer.php');
822?>
823