- modules/fotolab updated imagej to current version & some cod fixes to make it work
[care2x.git] / Care2007 / modules / weberp / GoodsReceived.php
blob28f1e5b9c2def3f76b56915fb4a9ed81fb7befef
1 <?php
2 /* $Revision: 1.27 $ */
4 $PageSecurity = 11;
6 /* Session started in header.inc for password checking and authorisation level check */
7 include('includes/DefinePOClass.php');
8 include('includes/DefineSerialItems.php');
9 include('includes/session.inc');
10 include('includes/SQL_CommonFunctions.inc');
12 $title = _('Receive Purchase Orders');
13 include('includes/header.inc');
15 echo '<A HREF="'. $rootpath . '/PO_SelectOSPurchOrder.php?' . SID . '">' . _('Back to Purchase Orders'). '</A><BR>';
17 if ($_GET['PONumber']<=0 AND !isset($_SESSION['PO'])) {
18 /* This page can only be called with a purchase order number for invoicing*/
19 echo '<CENTER><A HREF="' . $rootpath . '/PO_SelectOSPurchOrder.php?' . SID . '">'.
20 _('Select a purchase order to receive').'</A></CENTER>';
21 echo '<BR>'. _('This page can only be opened if a purchase order has been selected') . '. ' . _('Please select a purchase order first');
22 include ('includes/footer.inc');
23 exit;
24 } elseif (isset($_GET['PONumber']) AND !isset($_POST['Update'])) {
25 /*Update only occurs if the user hits the button to refresh the data and recalc the value of goods recd*/
27 $_GET['ModifyOrderNumber'] = $_GET['PONumber'];
28 include('includes/PO_ReadInOrder.inc');
29 } elseif (isset($_POST['Update']) OR isset($_POST['ProcessGoodsReceived'])) {
31 /* if update quantities button is hit page has been called and ${$Line->LineNo} would have be
32 set from the post to the quantity to be received in this receival*/
34 foreach ($_SESSION['PO']->LineItems as $Line) {
35 $RecvQty = $_POST['RecvQty_' . $Line->LineNo];
36 if (!is_numeric($RecvQty)){
37 $RecvQty = 0;
39 $_SESSION['PO']->LineItems[$Line->LineNo]->ReceiveQty = $RecvQty;
43 /* Always display quantities received and recalc balance for all items on the order */
46 echo '<CENTER><FONT SIZE=4><B><U>'. _('Receive purchase order'). ' '. $_SESSION['PO']->OrderNo .' '. _('from'). ' ' . $_SESSION['PO']->SupplierName . ' </U></B></FONT></CENTER><BR>';
47 echo '<FORM ACTION="' . $_SERVER['PHP_SELF'] . '?' . SID . '" METHOD=POST>';
49 echo '<CENTER><TABLE CELLPADDING=2 COLSPAN=7 BORDER=0>
50 <TR><TD class="tableheader">' . _('Item Code') . '</TD>
51 <TD class="tableheader">' . _('Description') . '</TD>
52 <TD class="tableheader">' . _('Quantity') . '<BR>' . _('Ordered') . '</TD>
53 <TD class="tableheader">' . _('Units') . '</TD>
54 <TD class="tableheader">' . _('Already Received') . '</TD>
55 <TD class="tableheader">' . _('This Delivery') . '<BR>' . _('Quantity') . '</TD>
56 <TD class="tableheader">' . _('Price') . '</TD>
57 <TD class="tableheader">' . _('Total Value') . '<BR>' . _('Received') . '</TD>';
60 echo '<TD>&nbsp;</TD>
61 </TR>';
62 /*show the line items on the order with the quantity being received for modification */
64 $_SESSION['PO']->total = 0;
65 $k=0; //row colour counter
67 if (count($_SESSION['PO']->LineItems)>0){
68 foreach ($_SESSION['PO']->LineItems as $LnItm) {
70 if ($k==1){
71 echo '<tr bgcolor="#CCCCCC">';
72 $k=0;
73 } else {
74 echo '<tr bgcolor="#EEEEEE">';
75 $k=1;
78 /* if ($LnItm->ReceiveQty==0){ /*If no quantites yet input default the balance to be received
79 $LnItm->ReceiveQty = $LnItm->QuantityOrd - $LnItm->QtyReceived;
83 /*Perhaps better to default quantities to 0 BUT.....if you wish to have the receive quantites
84 default to the balance on order then just remove the comments around the 3 lines above */
86 //Setup & Format values for LineItem display
88 $LineTotal = ($LnItm->ReceiveQty * $LnItm->Price );
89 $_SESSION['PO']->total = $_SESSION['PO']->total + $LineTotal;
90 $DisplayQtyOrd = number_format($LnItm->Quantity,$LnItm->DecimalPlaces);
91 $DisplayQtyRec = number_format($LnItm->QtyReceived,$LnItm->DecimalPlaces);
92 $DisplayLineTotal = number_format($LineTotal,2);
93 $DisplayPrice = number_format($LnItm->Price,2);
96 //Now Display LineItem
97 echo '<TD><FONT size=2>' . $LnItm->StockID . '</FONT></TD>';
98 echo '<TD><FONT size=2>' . $LnItm->ItemDescription . '</TD>';
99 echo '<TD ALIGN=RIGHT><FONT size=2>' . $DisplayQtyOrd . '</TD>';
100 echo '<TD><FONT size=2>' . $LnItm->Units . '</TD>';
101 echo '<TD ALIGN=RIGHT><FONT size=2>' . $DisplayQtyRec . '</TD>';
102 echo '<TD ALIGN=RIGHT><FONT size=2>';
104 if ($LnItm->Controlled == 1) {
106 echo '<input type=hidden name="RecvQty_' . $LnItm->LineNo . '" value="' . $LnItm->ReceiveQty . '"><a href="GoodsReceivedControlled.php?' . SID . '&LineNo=' . $LnItm->LineNo . '">' . number_format($LnItm->ReceiveQty,$LnItm->DecimalPlaces) . '</a></TD>';
108 } else {
110 echo '<input type=text name="RecvQty_' . $LnItm->LineNo . '" maxlength=10 SIZE=10 value="' . $LnItm->ReceiveQty . '"></TD>';
114 echo '<TD ALIGN=RIGHT><FONT size=2>' . $DisplayPrice . '</TD>';
115 echo '<TD ALIGN=RIGHT><FONT size=2>' . $DisplayLineTotal . '</FONT></TD>';
118 if ($LnItm->Controlled == 1) {
119 if ($LnItm->Serialised==1){
120 echo '<TD><a href="GoodsReceivedControlled.php?' . SID . '&LineNo=' . $LnItm->LineNo . '">'.
121 _('Enter Serial Nos'). '</a></TD>';
122 } else {
123 echo '<TD><a href="GoodsReceivedControlled.php?' . SID . '&LineNo=' . $LnItm->LineNo . '">'.
124 _('Enter Batches'). '</a></TD>';
128 echo '</TR>';
130 }//foreach(LineItem)
131 }//If count(LineItems) > 0
133 $DisplayTotal = number_format($_SESSION['PO']->total,2);
134 echo '<TR><TD COLSPAN=7 ALIGN=RIGHT><B>' . _('Total value of goods received'). '</B></TD>
135 <TD ALIGN=RIGHT><FONT SIZE=2><B>'. $DisplayTotal. '</B></FONT></TD>
136 </TR></TABLE>';
138 $SomethingReceived = 0;
139 if (count($_SESSION['PO']->LineItems)>0){
140 foreach ($_SESSION['PO']->LineItems as $OrderLine) {
141 if ($OrderLine->ReceiveQty>0){
142 $SomethingReceived =1;
147 /************************* LINE ITEM VALIDATION ************************/
149 /* Check whether trying to deliver more items than are recorded on the purchase order
150 (+ overreceive allowance) */
152 $DeliveryQuantityTooLarge = 0;
154 $InputError = false;
156 if (count($_SESSION['PO']->LineItems)>0){
158 foreach ($_SESSION['PO']->LineItems as $OrderLine) {
160 if ($OrderLine->ReceiveQty+$OrderLine->QtyReceived > $OrderLine->Quantity * (1+ ($_SESSION['OverReceiveProportion'] / 100))){
161 $DeliveryQuantityTooLarge =1;
162 $InputError = true;
168 if ($SomethingReceived==0 AND isset($_POST['ProcessGoodsReceived'])){ /*Then dont bother proceeding cos nothing to do ! */
170 prnMsg(_('There is nothing to process') . '. ' . _('Please enter valid quantities greater than zero'),'warn');
172 } elseif ($DeliveryQuantityTooLarge==1 AND isset($_POST['ProcessGoodsReceived'])){
174 prnMsg(_('Entered quantities cannot be greater than the quantity entered on the purchase invoice including the allowed over-receive percentage'). ' ' . '(' . $_SESSION['OverReceiveProportion'] .'%)','error');
175 echo '<BR>';
176 prnMsg(_('Modify the ordered items on the purchase invoice if you wish to increase the quantities'),'info');
178 } elseif (isset($_POST['ProcessGoodsReceived']) AND $SomethingReceived==1 AND $InputError == false){
180 /* SQL to process the postings for goods received... */
181 /* Company record set at login for information on GL Links and debtors GL account*/
184 if ($_SESSION['CompanyRecord']==0){
185 /*The company data and preferences could not be retrieved for some reason */
186 prnMsg(_('The company infomation and preferences could not be retrieved') . ' - ' . _('see your system administrator') , 'error');
187 include('includes/footer.inc');
188 exit;
191 /*Now need to check that the order details are the same as they were when they were read into the Items array. If they've changed then someone else must have altered them */
192 // Otherwise if you try to fullfill item quantities separately will give error.
193 $SQL = 'SELECT itemcode,
194 glcode,
195 quantityord,
196 quantityrecd,
197 qtyinvoiced,
198 shiptref,
199 jobref
200 FROM purchorderdetails
201 WHERE orderno=' . (int) $_SESSION['PO']->OrderNo . '
202 AND completed=0
203 ORDER BY podetailitem';
205 $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'). ':';
206 $DbgMsg = _('The following SQL to retrieve the purchase order details was used');
207 $Result=DB_query($SQL,$db, $ErrMsg, $DbgMsg);
209 $Changes=0;
210 $LineNo=1;
212 while ($myrow = DB_fetch_array($Result)) {
214 if ($_SESSION['PO']->LineItems[$LineNo]->GLCode != $myrow['glcode'] OR
215 $_SESSION['PO']->LineItems[$LineNo]->ShiptRef != $myrow['shiptref'] OR
216 $_SESSION['PO']->LineItems[$LineNo]->JobRef != $myrow['jobref'] OR
217 $_SESSION['PO']->LineItems[$LineNo]->QtyInv != $myrow['qtyinvoiced'] OR
218 $_SESSION['PO']->LineItems[$LineNo]->StockID != $myrow['itemcode'] OR
219 $_SESSION['PO']->LineItems[$LineNo]->Quantity != $myrow['quantityord'] OR
220 $_SESSION['PO']->LineItems[$LineNo]->QtyReceived != $myrow['quantityrecd']) {
223 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');
225 if ($debug==1){
226 echo '<TABLE BORDER=1>';
227 echo '<TR><TD>' . _('GL Code of the Line Item') . ':</TD>
228 <TD>' . $_SESSION['PO']->LineItems[$LineNo]->GLCode . '</TD>
229 <TD>' . $myrow['glcode'] . '</TD></TR>';
230 echo '<TR><TD>' . _('ShiptRef of the Line Item') . ':</TD>
231 <TD>' . $_SESSION['PO']->LineItems[$LineNo]->ShiptRef . '</TD>
232 <TD>' . $myrow['shiptref'] . '</TD></TR>';
233 echo '<TR><TD>' . _('Contract Reference of the Line Item') . ':</TD>
234 <TD>' . $_SESSION['PO']->LineItems[$LineNo]->JobRef . '</TD>
235 <TD>' . $myrow['jobref'] . '</TD>
236 </TR>';
237 echo '<TR><TD>' . _('Quantity Invoiced of the Line Item') . ':</TD>
238 <TD>' . $_SESSION['PO']->LineItems[$LineNo]->QtyInv . '</TD>
239 <TD>' . $myrow['qtyinvoiced'] . '</TD></TR>';
240 echo '<TR><TD>' . _('Stock Code of the Line Item') . ':</TD>
241 <TD>'. $_SESSION['PO']->LineItems[$LineNo]->StockID . '</TD>
242 <TD>' . $myrow['itemcode'] . '</TD></TR>';
243 echo '<TR><TD>' . _('Order Quantity of the Line Item') . ':</TD>
244 <TD>' . $_SESSION['PO']->LineItems[$LineNo]->Quantity . '</TD>
245 <TD>' . $myrow['quantityord'] . '</TD></TR>';
246 echo '<TR><TD>' . _('Quantity of the Line Item Already Received') . ':</TD>
247 <TD>' . $_SESSION['PO']->LineItems[$LineNo]->QtyReceived . '</TD>
248 <TD>' . $myrow['quantityrecd'] . '</TD></TR>';
249 echo '</TABLE>';
251 echo "<CENTER><A HREF='$rootpath/PO_SelectOSPurchOrder.php?" . SID . "'>".
252 _('Select a different purchase order for receiving goods against').'</A></CENTER>';
253 echo "<CENTER><A HREF='$rootpath/GoodsReceived.php?" . SID . '&PONumber=' .
254 $_SESSION['PO']->OrderNumber . '">'. _('Re-read the updated purchase order for receiving goods against'). '</A></CENTER>';
255 unset($_SESSION['PO']->LineItems);
256 unset($_SESSION['PO']);
257 unset($_POST['ProcessGoodsReceived']);
258 include ("includes/footer.inc");
259 exit;
261 $LineNo++;
262 } /*loop through all line items of the order to ensure none have been invoiced */
264 DB_free_result($Result);
267 /************************ BEGIN SQL TRANSACTIONS ************************/
269 $Result = DB_query('BEGIN',$db);
270 /*Now Get the next GRN - function in SQL_CommonFunctions*/
271 $GRN = GetNextTransNo(25, $db);
273 $PeriodNo = GetPeriod($_POST['DefaultReceivedDate'], $db);
274 $_POST['DefaultReceivedDate'] = FormatDateForSQL($_POST['DefaultReceivedDate']);
276 foreach ($_SESSION['PO']->LineItems as $OrderLine) {
278 if ($OrderLine->ReceiveQty !=0 AND $OrderLine->ReceiveQty!='' AND isset($OrderLine->ReceiveQty)) {
280 $LocalCurrencyPrice = ($OrderLine->Price / $_SESSION['PO']->ExRate);
281 /*Update SalesOrderDetails for the new quantity received and the standard cost used for postings to GL and recorded in the stock movements for FIFO/LIFO stocks valuations*/
283 if ($OrderLine->StockID!='') { /*Its a stock item line */
284 /*Need to get the current standard cost as it is now so we can process GL jorunals later*/
285 $SQL = "SELECT materialcost + labourcost + overheadcost as stdcost
286 FROM stockmaster
287 WHERE stockid='" . DB_escape_string($OrderLine->StockID) . "'";
288 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The standard cost of the item being received cannot be retrieved because');
289 $DbgMsg = _('The following SQL to retrieve the standard cost was used');
290 $Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
292 $myrow = DB_fetch_row($Result);
294 if ($OrderLine->QtyReceived==0){ //its the first receipt against this line
295 $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost = $myrow[0];
297 $CurrentStandardCost = $myrow[0];
299 /*Set the purchase order line stdcostunit = weighted average standard cost used for all receipts of this line
300 This assures that the quantity received against the purchase order line multiplied by the weighted average of standard
301 costs received = the total of standard cost posted to GRN suspense*/
302 $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost = (($CurrentStandardCost * $OrderLine->ReceiveQty) + ($_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost *$OrderLine->QtyReceived)) / ($OrderLine->ReceiveQty + $OrderLine->QtyReceived);
304 } elseif ($OrderLine->QtyReceived==0 AND $OrderLine->StockID=="") {
305 /*Its a nominal item being received */
306 /*Need to record the value of the order per unit in the standard cost field to ensure GRN account entries clear */
307 $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost = $LocalCurrencyPrice;
310 if ($OrderLine->StockID=='') { /*Its a NOMINAL item line */
311 $CurrentStandardCost = $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost;
314 /*Now the SQL to do the update to the PurchOrderDetails */
316 if ($OrderLine->ReceiveQty >= ($OrderLine->Quantity - $OrderLine->QtyReceived)){
317 $SQL = "UPDATE purchorderdetails SET
318 quantityrecd = quantityrecd + " . $OrderLine->ReceiveQty . ",
319 stdcostunit=" . $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost . ",
320 completed=1
321 WHERE podetailitem = " . $OrderLine->PODetailRec;
322 } else {
323 $SQL = "UPDATE purchorderdetails SET
324 quantityrecd = quantityrecd + " . $OrderLine->ReceiveQty . ",
325 stdcostunit=" . $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost . ",
326 completed=0
327 WHERE podetailitem = " . $OrderLine->PODetailRec;
330 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The purchase order detail record could not be updated with the quantity received because');
331 $DbgMsg = _('The following SQL to update the purchase order detail record was used');
332 $Result = DB_query($SQL,$db, $ErrMsg, $DbgMsg, true);
335 if ($OrderLine->StockID !=''){ /*Its a stock item so use the standard cost for the journals */
336 $UnitCost = $CurrentStandardCost;
337 } else { /*otherwise its a nominal PO item so use the purchase cost converted to local currecny */
338 $UnitCost = $OrderLine->Price / $_SESSION['PO']->ExRate;
341 /*Need to insert a GRN item */
343 $SQL = "INSERT INTO grns (grnbatch,
344 podetailitem,
345 itemcode,
346 itemdescription,
347 deliverydate,
348 qtyrecd,
349 supplierid,
350 stdcostunit)
351 VALUES (" . $GRN . ",
352 " . $OrderLine->PODetailRec . ",
353 '" . DB_escape_string($OrderLine->StockID) . "',
354 '" . DB_escape_string($OrderLine->ItemDescription) . "',
355 '" . $_POST['DefaultReceivedDate'] . "',
356 " . DB_escape_string($OrderLine->ReceiveQty) . ",
357 '" . DB_escape_string($_SESSION['PO']->SupplierID) . "',
358 " . $CurrentStandardCost . ')';
360 $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');
361 $DbgMsg = _('The following SQL to insert the GRN record was used');
362 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
364 if ($OrderLine->StockID!=''){ /* if the order line is in fact a stock item */
366 /* Update location stock records - NB a PO cannot be entered for a dummy/assembly/kit parts */
368 /* Need to get the current location quantity will need it later for the stock movement */
369 $SQL="SELECT locstock.quantity
370 FROM locstock
371 WHERE locstock.stockid='" . DB_escape_string($OrderLine->StockID) . "'
372 AND loccode= '" . DB_escape_string($_SESSION['PO']->Location) . "'";
374 $Result = DB_query($SQL, $db);
375 if (DB_num_rows($Result)==1){
376 $LocQtyRow = DB_fetch_row($Result);
377 $QtyOnHandPrior = $LocQtyRow[0];
378 } else {
379 /*There must actually be some error this should never happen */
380 $QtyOnHandPrior = 0;
383 $SQL = "UPDATE locstock
384 SET quantity = locstock.quantity + " . $OrderLine->ReceiveQty . "
385 WHERE locstock.stockid = '" . DB_escape_string($OrderLine->StockID) . "'
386 AND loccode = '" . DB_escape_string($_SESSION['PO']->Location) . "'";
388 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The location stock record could not be updated because');
389 $DbgMsg = _('The following SQL to update the location stock record was used');
390 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
393 /* If its a stock item still .... Insert stock movements - with unit cost */
395 $SQL = "INSERT INTO stockmoves (stockid,
396 type,
397 transno,
398 loccode,
399 trandate,
400 price,
401 prd,
402 reference,
403 qty,
404 standardcost,
405 newqoh)
406 VALUES ('" . DB_escape_string($OrderLine->StockID) . "',
408 " . $GRN . ", '" . DB_escape_string($_SESSION['PO']->Location) . "',
409 '" . $_POST['DefaultReceivedDate'] . "',
410 " . $LocalCurrencyPrice . ",
411 " . $PeriodNo . ",
412 '" . DB_escape_string($_SESSION['PO']->SupplierID) . " (" . DB_escape_string($_SESSION['PO']->SupplierName) . ") - " .$_SESSION['PO']->OrderNo . "',
413 " . $OrderLine->ReceiveQty . ",
414 " . $_SESSION['PO']->LineItems[$OrderLine->LineNo]->StandardCost . ",
415 " . ($QtyOnHandPrior + $OrderLine->ReceiveQty) . ")";
417 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('stock movement records could not be inserted because');
418 $DbgMsg = _('The following SQL to insert the stock movement records was used');
419 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
421 /*Get the ID of the StockMove... */
422 $StkMoveNo = DB_Last_Insert_ID($db,'stockmoves','stkmoveno');
423 /* Do the Controlled Item INSERTS HERE */
425 if ($OrderLine->Controlled ==1){
426 foreach($OrderLine->SerialItems as $Item){
427 /* we know that StockItems return an array of SerialItem (s)
428 We need to add the StockSerialItem record and
429 The StockSerialMoves as well */
430 //need to test if the controlled item exists first already
431 $SQL = "SELECT COUNT(*) FROM stockserialitems
432 WHERE stockid='" . DB_escape_string($OrderLine->StockID) . "'
433 AND loccode = '" . DB_escape_string($_SESSION['PO']->Location) . "'
434 AND serialno = '" . DB_escape_string($Item->BundleRef) . "'";
435 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Could not check if a batch or lot stock item already exists because');
436 $DbgMsg = _('The following SQL to test for an already existing controlled but not serialised stock item was used');
437 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
438 $AlreadyExistsRow = DB_fetch_row($Result);
439 if (trim($Item->BundleRef) != ""){
440 if ($AlreadyExistsRow[0]>0){
441 if ($OrderLine->Serialised == 1) {
442 $SQL = 'UPDATE stockserialitems SET quantity = ' . $Item->BundleQty . ' ';
443 } else {
444 $SQL = 'UPDATE stockserialitems SET quantity = quantity + ' . $Item->BundleQty . ' ';
446 $SQL .= "WHERE stockid='" . DB_escape_string($OrderLine->StockID) . "'
447 AND loccode = '" . DB_escape_string($_SESSION['PO']->Location) . "'
448 AND serialno = '" . DB_escape_string($Item->BundleRef) . "'";
449 } else {
450 $SQL = "INSERT INTO stockserialitems (stockid,
451 loccode,
452 serialno,
453 quantity)
454 VALUES ('" . DB_escape_string($OrderLine->StockID) . "',
455 '" . DB_escape_string($_SESSION['PO']->Location) . "',
456 '" . DB_escape_string($Item->BundleRef) . "',
457 " . $Item->BundleQty . ")";
460 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock item record could not be inserted because');
461 $DbgMsg = _('The following SQL to insert the serial stock item records was used');
462 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
464 /** end of handle stockserialitems records */
466 /** now insert the serial stock movement **/
467 $SQL = "INSERT INTO stockserialmoves (stockmoveno,
468 stockid,
469 serialno,
470 moveqty)
471 VALUES (" . $StkMoveNo . ",
472 '" . DB_escape_string($OrderLine->StockID) . "',
473 '" . DB_escape_string($Item->BundleRef) . "',
474 " . $Item->BundleQty . ")";
475 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The serial stock movement record could not be inserted because');
476 $DbgMsg = _('The following SQL to insert the serial stock movement records was used');
477 $Result = DB_query($SQL, $db, $ErrMsg, $DbgMsg, true);
478 }//non blank BundleRef
479 } //end foreach
481 } /*end of its a stock item - updates to locations and insert movements*/
483 /* If GLLink_Stock then insert GLTrans to debit the GL Code and credit GRN Suspense account at standard cost*/
484 if ($_SESSION['PO']->GLLink==1 AND $OrderLine->GLCode !=0){ /*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 */
486 /*first the debit using the GLCode in the PO detail record entry*/
488 $SQL = "INSERT INTO gltrans (type,
489 typeno,
490 trandate,
491 periodno,
492 account,
493 narrative,
494 amount)
495 VALUES (25,
496 " . $GRN . ",
497 '" . $_POST['DefaultReceivedDate'] . "',
498 " . $PeriodNo . ",
499 " . $OrderLine->GLCode . ",
500 'PO: " . DB_escape_string($_SESSION['PO']->OrderNo) . " " . DB_escape_string($_SESSION['PO']->SupplierID) . " - " . DB_escape_string($OrderLine->StockID) . " - " . DB_escape_string($OrderLine->ItemDescription) . " x " . DB_escape_string($OrderLine->ReceiveQty) . " @ " . number_format($CurrentStandardCost,2) . "',
501 " . $CurrentStandardCost * $OrderLine->ReceiveQty . ")";
503 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The purchase GL posting could not be inserted because');
504 $DbgMsg = _('The following SQL to insert the purchase GLTrans record was used');
505 $Result = DB_query($SQL,$db,$ErrMsg, $DbgMsg, true);
507 /* 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 */
510 /*now the GRN suspense entry*/
511 $SQL = "INSERT INTO gltrans (type,
512 typeno,
513 trandate,
514 periodno,
515 account,
516 narrative,
517 amount)
518 VALUES (25,
519 " . $GRN . ",
520 '" . $_POST['DefaultReceivedDate'] . "',
521 " . $PeriodNo . ",
522 " . $_SESSION['CompanyRecord']['grnact'] . ", '" .
523 _('PO') . ': ' . $_SESSION['PO']->OrderNo . ' ' . DB_escape_string($_SESSION['PO']->SupplierID) . ' - ' . DB_escape_string($OrderLine->StockID) . ' - ' . DB_escape_string($OrderLine->ItemDescription) . ' x ' . $OrderLine->ReceiveQty . ' @ ' . number_format($UnitCost,2) . "',
524 " . -$UnitCost * $OrderLine->ReceiveQty . ")";
526 $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The GRN suspense side of the GL posting could not be inserted because');
527 $DbgMsg = _('The following SQL to insert the GRN Suspense GLTrans record was used');
528 $Result = DB_query($SQL,$db, $ErrMsg, $DbgMsg,true);
530 } /* end of if GL and stock integrated and standard cost !=0 */
531 } /*Quantity received is != 0 */
532 } /*end of OrderLine loop */
534 $SQL='COMMIT';
535 $Result = DB_query($SQL,$db);
537 unset($_SESSION['PO']->LineItems);
538 unset($_SESSION['PO']);
539 unset($_POST['ProcessGoodsReceived']);
541 echo '<BR>'. _('GRN number'). ' '. $GRN .' '. _('has been processed').'<BR>';
542 echo "<A HREF='$rootpath/PO_SelectOSPurchOrder.php?" . SID . "'>" . _('Select a different purchase order for receiving goods against'). '</A>';
543 /*end of process goods received entry */
544 include('includes/footer.inc');
545 exit;
547 } 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*/
549 echo "<BR><CENTER><A HREF='$rootpath/PO_Items.php?=" . SID . "'>" . _('Modify Order Items'). '</A></CENTER>';
551 if (!isset($_POST['DefaultReceivedDate'])){
552 $_POST['DefaultReceivedDate'] = Date($_SESSION['DefaultDateFormat']);
554 echo '<TABLE><TR><TD>'. _('Date Goods/Service Received'). ':</TD><TD><INPUT TYPE=text MAXLENGTH=10 SIZE=10 name=DefaultReceivedDate value="' . $_POST['DefaultReceivedDate'] . '"></TD></TR>';
556 echo '</TABLE><CENTER><INPUT TYPE=SUBMIT NAME=Update Value=' . _('Update') . '><P>';
557 echo '<INPUT TYPE=SUBMIT NAME="ProcessGoodsReceived" Value="' . _('Process Goods Received') . '"></CENTER>';
560 echo '</FORM>';
562 include('includes/footer.inc');