 <?php extract($_POST);
$PageSecurity = 1;
 
global $db;?>


<?php

if(isset($_POST['Save']))
{
for($i=0;$i<count($_REQUEST['mstitemid']);$i++)
{
       $totalqty=$_REQUEST['mrnqty'][$i]+$_REQUEST['shortclose'][$i];
        if($_REQUEST['poqty'][$i]==$totalqty)
		{$status="Receive"; }
		 else {$status='Pending';}
		 $update=DB_query("update purchorderdetails set
		 close_qty='".$_REQUEST['shortclose'][$i]."' ,status='".$status."' 
		 where mst_itemid='".$_REQUEST['mstitemid'][$i]."' 
		 and id='".$_REQUEST['orderno']."'",$db);

}

//echo "select * from purchorderdetails where orderno='".$_REQUEST['orderno']."'";

$check=DB_query("select * from purchorderdetails where id='".$_REQUEST['orderno']."'",$db);
$numrow=DB_num_rows($check);$row=0;
while($num=DB_fetch_array($check))
{
if($num['status']=='Receive')
{$row=$row+1;}}


if($numrow==$row)
{
 $update1=DB_query("update purchorders set	Po_recive_status='Receive',po_close_by='".$_SESSION['UserID']."'  where  orderno='".$_REQUEST['orderno']."'",$db);
}

echo "<script>location.href='index.php?route=POModule/DispPurchOrder&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";




}








?>




 
<?php 

  $sql_po = "Select * from purchorders where id='".$_GET['closeid']."'";
 //echo $sql_po ;
	$result=DB_query($sql_po,$db);
	$myrow=DB_fetch_array($result);
	//echo "<pre>";
	//print_r($myrow);
	
	
	$orddate=$myrow['orddate'];
	$PO_Orderno=$myrow['PO_Orderno'];
	$ammndmnt_no=$myrow['ammndmnt_no'];
	$date1=$myrow['date1'];
	$date2=$myrow['date2'];
	$version1=$myrow['version1'];
	$revised=$myrow['revised'];
	$deliveryby=$myrow['deliveryby'];
	$deliverydate=$myrow['delivery_date'];
	$initiator=$myrow['initiator'];
	$requisitionno=  $s->GetFieldValue('materialrequisitions',"requisitionno ='".$myrow['mst_reqino']."'" ,'Reqno');
	//$mst_supplierno=  $s->GetFieldValue('venderquotations',"id ='".$myrow['mst_vendor_quotesid']."'" ,'mst_supplierno');
 	//$vernder_quotation =$s->GetFieldValue('venderquotations',"id ='".$myrow['mst_vendor_quotesid']."'" ,'vernder_quotation');
	//$sitecode=  $s->GetFieldValue('venderquotations',"id ='".$myrow['mst_vendor_quotesid']."'" ,'sitecode');
	
	
	
	
	
	$site_sql="Select * from locations where loccode='".$sitecode."'";
	$site_res=DB_query($site_sql,$db);
	$site_row=DB_fetch_array($site_res);
	$vendor_detail = "Select * from suppliers where id='".$mst_supplierno."'";
	$result_vendor=DB_query($vendor_detail,$db);
	$myrow_vendor=DB_fetch_array($result_vendor);
	$vendor_code=$myrow_vendor['vendor_code'];
	$vendor_name=$myrow_vendor['vendor_name'];
	$phone1=$myrow_vendor['phone1'];  
	$adrress1=$myrow_vendor['adrress1'];
	$adrress2=$myrow_vendor['adrress2'];
	$address3=$myrow_vendor['address3'];
	$phone2=$myrow_vendor['phone2'];  
	$mobile=$myrow_vendor['mobile'];
	$tin_no=$myrow_vendor['tin_no'];  
	$fax=$myrow_vendor['fax'];
    $email=$myrow_vendor['email'];	                           
	$vendor_quotation_no=$myrow['vendor_quotation_no'];
	$dateprinted=$myrow['dateprinted'];
	$stat_comment=$myrow['stat_comment'];
	$vendor_payment_terms=$myrow_vendor['payterms'];

 ?>
 

 <div align="center"><h3>PO Close</h3></div>	  	  

 <table width="100%" border="1" cellpadding="2" cellspacing="1" align="center">
  <tr>
    <td width="56%" ><table width="100%" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <th width="35%"><strong>PO Date </strong></th>
        <td width="65%"><?php echo ConvertSQLDate($orddate);?>
		
		</td>
      </tr>
      <tr>
        <th><strong>PO Number </strong></th>
        <td><?php if($_REQUEST['AMDPOID']!=''){echo $ammndmnt_no;}else{echo $PO_Orderno;}?>     </td>
      </tr>
      <tr>
        <th><strong>Delivery Date </strong></th>
        <td><?php echo ConvertSQLDate($deliverydate);?></td>
      </tr>
      <tr>
        <th><strong>Initiated By </strong></th>
        <td><?php echo $initiator;?></td>
      </tr>
      <tr>
        <th nowrap="nowrap"><strong>Requisition Number </strong></th>
        <td><?php echo $requisitionno;?> </td>
      </tr>
      <tr>
        <th><strong>Requisition Date </strong></th>
		<?php $req_sql=DB_query("Select prdate from materialrequisitions where Reqno='".$requisitionno."'",$db);
			$req_row=DB_fetch_array($req_sql);
		?>
        <td><?php echo ConvertSQLDate($req_row['prdate']);?> </td>
      </tr>
      <tr>
        <th nowrap="nowrap"><strong>Vendor Quotation Number </strong></th>
        <td><?php echo  $vernder_quotation;?> </td>
      </tr>
      <tr>
        <th nowrap="nowrap"><strong>Status</strong></th>
        <td><?php echo $myrow['status1']; ?></td>
      </tr>
      <tr>
        <th nowrap="nowrap"><strong>Status Comments </strong></th>
        <td><?php echo $stat_comment1;?></td>
      </tr>
    </table></td>
    <td width="44%" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="2">
  
	  <tr>
        <th nowrap="nowrap"><strong>Vendor Code </strong></th>
        <td><?php echo $vendor_code;?>  </td>
      </tr>
      <tr>
        <th nowrap="nowrap"><strong>Vendor Name </strong></th>
        <td><?php echo $vendor_name;?>  </td>
      </tr>
      <tr>
        <th rowspan="3"><strong>Adddress</strong></th>
        <td><?php echo $adrress1;?>  </td>
      </tr>
      <tr>
        <td><?php echo $adrress2;?>  </td>
      </tr>
      <tr>
        <td><?php echo $address3;?>  </td>
      </tr>
      <tr>
        <th><strong>Phone</strong></th>
        <td><?php echo $phone1;?>      </td>
      </tr>
      <tr>
        <th><strong>Mobile</strong></th>
        <td><?php echo $mobile;?>   </td>
      </tr>
      <tr>
        <th scope="row"><strong>Tin No. </strong></th>
        <td><?php echo $tin_no;?>    </td>
      </tr>
      <tr>
        <th scope="row" nowrap="nowrap"><strong>Payment Terms </strong></th>
		<?php 
			 $pay_sql="Select terms from paymentterms where termsindicator='".$vendor_payment_terms."'";
			$pay_res=DB_query($pay_sql,$db);
			$pay_row=DB_fetch_array($pay_res);
		?>
        <td><?php echo $pay_row['terms'];?>      </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td colspan="2"><table width="100%" border="0" cellspacing="1" cellpadding="2">
      <?php 
	  		$site_sql="Select * from locations where loccode='".$sitecode."'";
			$site_res=DB_query($site_sql,$db);
			$site_row=DB_fetch_array($site_res);
	  ?>
	  <tr>
        <th><strong>Site Code</strong></th>
        <td><?php echo $sitecode;?>     </td>
      </tr>
      <tr>
        <th><strong>Site Name </strong></th>
        <td><?php echo $site_row['locationname'];?>        </td>
      </tr>
      <tr>
        <th rowspan="3"><strong>Address</strong></th>
        <td><?php echo $site_row['deladd1'];?>   </td>
      </tr>
      <tr>
        <td><?php echo $site_row['deladd2'];?>  </td>
      </tr>
      <tr>
        <td><?php echo $site_row['deladd3'];?>        </td>
      </tr>
      <tr>
        <th><strong>Phone</strong></th>
        <td><?php echo $site_row['phone1'];?>          </td>
      </tr>
      <tr>
        <th><strong>Mobile</strong></th>
        <td><?php echo $site_row['mobile'];?>         </td>
      </tr>
      <tr>
        <th><strong>Tin No. </strong></th>
        <td><?php echo $site_row['tin_no'];?>          </td>
      </tr>
      <tr>
        <th><strong>Delivery By </strong></th>
        <td><?php echo $deliveryby;?>        </td>
      </tr>
    </table></td>
   </tr>
</table>
 
 
 
 
 
 
<form action="" method="post">
	 	<div align="center">
  <table width="100%" border="0" cellpadding="5">
    <tr>
      <th><strong>Item Code</strong></th>
      <th><strong>Item Name</strong></th>
      <th><strong>Item No</strong></th>
      
      <th><strong>Unit</strong></th>
	    <th><strong>Quantity</strong></th>
      <th><strong>Rate</strong></th>
	   <th><strong>MRN Quantity</strong></th>
	   <th><strong>Max Close Qty</strong></th>
	   <th><strong>Short close Qty</strong>
	   <input type="hidden" name="orderno" value="<?php echo $_REQUEST['closeid']; ?>" />
	   
	   </th>
    
					

     
    </tr>
	 
	 	<?php 
		
  
 
$sql="SELECT * FROM purchorderdetails WHERE id='".$_REQUEST['closeid']."' and  status='Pending'"; 
//echo $sql;
$result=DB_query($sql,$db);
$i=1;
while($myrow=DB_fetch_array($result))
{
//echo "<pre>";
//print_r($myrow);
if($i%2==0)
{$bg="#CCCCCC";}
else
$bg="#C8BECB";
?>
    <tr bgcolor="<?php echo $bg;?>">
       <td><?php echo $myrow['prod_id'];?>
	   <input type="hidden" name="mstitemid[]" value="<?php echo $myrow['mst_itemid'];?>" />
	    </td>
    <td><?php echo $myrow['prod_dec'];?> </td>
    <td><?php echo $myrow['item_no']; ?></td>
    
      <td><?php
	   $itemid=$s->getField('item_master','id',$myrow['mst_itemid'],'unit'); 
	  $unitname=$s->getField('unitsofmeasure','unitid',$itemid,'unitname');
	  echo $unitname;?>
        </td>
    <?php $query_item=DB_query("select * from item_master where StockID='".$myrow['prod_id']."'", $db);
		  	$item_row=DB_fetch_array($query_item);
		   ?>
   
    <td><?php echo $myrow['quatity'];?> <input type="hidden" name="poqty[]" value="<?php echo $myrow['quatity'];?>" /></td>
		
		
    <td><?php echo $myrow['rate'];?></td>
	
	<?php  
	
	   $check_last_mrn="Select sum(reciveqty),POQty from addmrnqty  inner join addmrn on 
	  addmrn.id=addmrnqty.issue_id 	  where addmrn.PO_WO_MTO= '".$_GET['closeid']."'
	  and addmrn.site='".$_REQUEST['site']."'
	   and mst_itemid='".$myrow['mst_itemid']."'";
	$last_mrn_res=DB_query($check_last_mrn,$db);
	$last_mrn_row=DB_fetch_array($last_mrn_res);
	$shortqty=DB_query("select close_qty from purchorderdetails where 
	orderno='".$_GET['closeid']."' and mst_itemid='".$myrow['mst_itemid']."'",$db);
	$short_qty=DB_fetch_array($shortqty);
	if($short_qty['close_qty']=='') $closeqty1=0; else $closeqty1=$short_qty['close_qty'];
	//echo $closeqty1;
	if($last_mrn_row['POQty']!='' && $last_mrn_row['sum(reciveqty)']!='')
	{
	$pending_qty=$last_mrn_row['POQty']-($last_mrn_row['sum(reciveqty)']+$closeqty1);
	$mrnqty=$last_mrn_row['sum(reciveqty)'];
	}
	else{$pending_qty=$myrow['quatity']-$closeqty1;
	$mrnqty=0;
	
	}	
	?>
	<td><?php echo $mrnqty;?><input type="hidden" name="mrnqty[]" value="<?php echo $mrnqty;?>" />     </td>
	<td><input type="hidden" name="shortclose1[]" id="shortclode1_<?php echo $i; ?>"   value="<?php echo $pending_qty; ?>"  />
	<?php echo $pending_qty; ?>
	</td>
	<td>
	<input type="text" name="shortclose[]" id="shortclode_<?php echo $i; ?>"   value="<?php echo $pending_qty; ?>"  onkeyup="caltotal(<?php echo $i; ?>)"/> </td>

      
   

      <?php 
	  
	  
$i++;
}?>
     
    </tr>

	
	
  </table>
  
  
  
  </div>
  <div>
  <table width="100%">
  <tr>
<th><strong>Remarks</strong></th>
<td><textarea name="reason" style="width:100%" ></textarea></td></tr>	
</table>
  </div>
  <p align="center"><input type="submit" name="Save" value="Save" /></p>
  </form>

 
<script>
function caltotal(i)
{
var closeqty=parseFloat(document.getElementById("shortclode_"+i).value);
var closeqty1=parseFloat(document.getElementById("shortclode1_"+i).value);
if(closeqty>closeqty1)
{
alert("Short Qty Should be less or equal then Max qty");
document.getElementById("shortclode_"+i).value=closeqty1;
return false;
}
return true;

}

</script>
