<?php 
session_name('CAKEPHP'); session_start();
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">

<h1>PO v/s MRN and Balance Qty Report</h1>
<script type="text/javascript">
/*function showSlidingDiv(i){
$("#slidingDiv_"+i).animate({"height": "toggle"}, { duration: 700 });}

function showSlidingDivAll(){
var cnt=document.getElementById("count").value;
for(var i=0;i<cnt;i++){
$("#slidingDiv_"+i).animate({"height": "toggle"}, { duration: 700 }); }}*/

function showSlidingDiv1(i)
{
	var style = document.getElementById("slidingDiv_"+i).style;
	style.display = (style.display=="block") ? "none":"block";
}
function showSlidingDivAll(i)
{
	var cnt=document.getElementById("count").value;
	for(var i=0;i<cnt;i++){
	var style = document.getElementById("slidingDiv_"+i).style;
	style.display = (style.display=="block") ? "none":"block"; }
}
function chkvalidate()
{
	var project = document.getElementById("site").value;
	if(project==''){
		alert("Please select project name.");
		return false;
	}
	return true;
}

</script>
<form action="" method="post" id="po_mrn">

<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right">Branch <span style="color:#FF0000">*</span></td>
    <td width="20%">
	<?php		
  $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql1=DB_query($ss1,$db);	 
	?>	
	<select name="site">
	<option value="">select</option>
			<?php		
	 			 while($site_row=DB_fetch_array($site_sql1)) 
				  {
	  				$p='';
					if($_REQUEST['site']==$site_row['loccode'])
					$p="selected";
	 		 ?>
      	  <option value="<?php echo $site_row['loccode'];?>" <?php echo $p;?>><?php echo $site_row['locationname'];?></option>
        <?php } ?>
	</select>	
	</td>
	<td width="13%" align="right">Vendor</td>
	<td width="20%">
	<select name="vendor">
		<option value="">Select Vendor</option>
		<?php $v_sql=DB_query("Select id,vendor_code,vendor_name from suppliers  where site='".$_REQUEST['site']."'",$db);
	 		 while($v_row=DB_fetch_array($v_sql)) 
	 		 {
	  			$p='';
				if($_REQUEST['vendor']==$v_row['id'])
				$p="selected";
	 		 ?>
	 		 <option value="<?php echo $v_row['id'];?>" <?php echo $p;?>><?php echo str_replace(array("M/s ","M/S"),'',$v_row['vendor_name']);?></option>
	 	 <?php } ?>
	</select>
	</td>
    <td width="13%" align="right">Material</td>
    <td><select name="item_code">
			<option value="">Select Material</option>
			<?php 
	 		 $stock_sql=DB_query("SELECT b.* FROM addmrnqty a,item_master b WHERE a.mst_itemid=b.id GROUP BY a.mst_itemid",$db);
	 		 while($stock_row=DB_fetch_array($stock_sql)) 
	 		 {
	  			$p='';
				if($_REQUEST['item_code']==$stock_row['id'])
				$p="selected";
	 		 ?>
	 		 <option value="<?php echo $stock_row['id'];?>" <?php echo $p;?>><?php echo $stock_row['StockID']."-".$stock_row['item_name'];?></option>
	 	 <?php } ?>	
	</select>	
	</td>
</tr>
<tr class="row1">
    <td align="right">PO No.</td>
    <td>
		<select name="po_orderno">
			<option value="">Select PO No.</option>
			<?php 
	 		 $stock_sql=DB_query("Select * from purchorders  where site='".$_REQUEST['site']."'",$db);
	 		 while($stock_row=DB_fetch_array($stock_sql)){
	  			$p='';
				if($_REQUEST['po_orderno']==$stock_row['id'])
				$p="selected";?>
	 		 <option value="<?php echo $stock_row['id'];?>" <?php echo $p;?>><?php echo $stock_row['purchase_ordNo'];?></option>
	 	 <?php } ?>
	</select>
	</td>
  	<td align="right">PO From Date</td>
    <td><input type="text" name="fromdate" id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>"  onchange="javascript:checkdate(this.value)" class="datepicker" /></td>
    <td align="right">PO To Date</td>
    <td><input type="text" name="todate" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>"  onchange="javascript:checkdate(this.value)" class="datepicker"  />
    	<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'>
    		<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'>
	</td>
  </tr>
  <!--<tr>
  <td>&nbsp;</td>
    <td><input name="include" type="checkbox" value="include" />&nbsp;&nbsp;&nbsp;<strong>Include Zero Balance Qty</strong></td>
  </tr>-->
  <tr class="row1">
    <td colspan="6" style="text-align:center;"><input type="submit" value="Search" class="search" name="view" onclick="return chkvalidate()"/></td>
  </tr>
</table> </form>
<p align="right"><a href="PO_MRN_Qty_Report_print.php" target="_blank" class="search">Print</a></p>

<?php 
if($_REQUEST['view']!=''){
	$site=$_REQUEST['site'];
	$vendor=$_REQUEST['vendor'];
	$item_code=$_REQUEST['item_code'];
	$po_orderno=$_REQUEST['po_orderno'];
	$fromdate=$_REQUEST['fromdate'];
	$todate=$_REQUEST['todate'];
	$fromdate_brk=explode('/',$fromdate);
	$from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0];
	$todate_brk=explode('/',$todate);
	$to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0];
	//echo '<pre>';
	//print_r($_REQUEST);
	$filter='';
	$filter1='';
	if($site!='')
	$filter[]="a.site = '".$site."'";
	if($vendor!='')
	$filter[]="a.vendorId = '".$vendor."'";
	if($po_orderno!='')
	$filter[]="a.id = '".$po_orderno."'";
	if($fromdate!='' && $todate!='')
	$filter[]="a.po_date BETWEEN '".$from."' and '".$to."'";
	if($item_code!='')
	$filter[]="d.mst_itemid ='".$item_code."'";
	if($filter!=''){$filter1=implode(' and ',$filter);}
//echo $filter1;
	if($filter1!='')
	{
	      $sql1="SELECT a.*
FROM purchorders a, purchorderdetails b,addmrn c, addmrnqty d
WHERE a.id=b.parantId AND a.id=c.order_no AND c.id=d.issue_id
AND c.ReceivedAgainst='PO' and $filter1  GROUP BY a.id";
}
if($_REQUEST['site']!='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records of Branch ".$site."   From  ".$from." to ".$to."";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='')
	{
	$message="Showing Records of Branch ".$site." ";
	}
	else if($_REQUEST['site']!=='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records   From  ".$from." to ".$to."";
	}
	
	$_SESSION['mess3']=$message;
$_SESSION['pomrnqty_report']=$sql1;	
}

/*
else{
$sql1="Select * from purchorders";
}*/?>
<?php if($_REQUEST['view']!=''){ //echo $sql1;?>
<!--<div align="left"><a href="#" onClick="showSlidingDivAll(); return false;"><input type="button" value="Expand/Collapse All" style="background-color:#003399;padding:3px; color:#FFFFFF; margin:0px;" /></a></div>-->

<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
	<tr>     
      <th width="150">MRN No.</th>
      <th width="100">Item Code</th>
      <th>Item Name</th>
      <th width="100">Unit</th>
      <th width="100" class="text-right">PO Qty.</th>
      <th width="100" class="text-right">MRN Qty.</th>
      <th width="100" class="text-right">Free Qty.</th>
      <th width="100" class="text-right">Short Close Qty.</th>
      <th width="120" class="text-right">Pending Qty.</th>      
	</tr>
</table>
<div style="width:100%; height:400px; overflow:auto; margin-left:auto; margin-right:auto;">
<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
	<tr>     
      <th style="padding:0;" height="1" width="150"></th>
      <th style="padding:0;" width="100"></th>
      <th style="padding:0;"></th>
      <th style="padding:0;" width="100"></th>
      <th style="padding:0;" width="100"></th>
      <th style="padding:0;" width="100"></th>
      <th style="padding:0;" width="100"></th>
      <th style="padding:0;" width="100"></th>
      <th style="padding:0;" width="110"></th>      
	</tr>
<?php 
 // echo $sql1; 
$i=0;
$short_close_qty=0;
$result1=DB_query($sql1,$db);
if(DB_num_rows($result1)>0){
while($row1=DB_fetch_array($result1)){?>
<tr bgcolor="#e4e4e4" style="color:#00695f;">
	<td colspan="9">Created Date: <strong><?php echo $row1['created_date']; ?></strong>&nbsp;&nbsp;|&nbsp;&nbsp;Location: <strong><?php  echo $s->getField('locations','loccode',$row1['site'],'locationname');?></strong>&nbsp;&nbsp;|&nbsp;&nbsp;PO No.: <strong><?php echo $row1['purchase_ordNo']; ?></strong>&nbsp;&nbsp;|&nbsp;&nbsp;PO Date: <strong><?php echo ConvertSQLDate($row1['po_date']);?></strong>&nbsp;&nbsp;|&nbsp;&nbsp;Vendor Name: <strong><?php echo $s->getField('suppliers','id',$row1['vendorId'],'vendor_name');?></strong></td>
    </tr>
       <?php 
        $mrn_list_sql= DB_query("select a.*,b.mst_itemid,sum(b.POQty) as POQty,sum(b.reciveqty) as reciveqty,b.short_accessid,sum(b.free_qty) as free_qty from addmrn a , addmrnqty b where a.id=b.issue_id and a.order_no='".$row1['id']."' group by a.id,b.mst_itemid",$db); 
        
       		while($mrn_list_res=DB_fetch_assoc($mrn_list_sql))
       		{
       			
       			 $short_close_qty=$s->GetFieldValue('po_short_close',"po_id='".$mrn_list_res['order_no']."' and item_id='".$mrn_list_res['mst_itemid']."'",'short_close');
       			 $pendingqry = $mrn_list_res['POQty']-$mrn_list_res['reciveqty']+$mrn_list_res['free_qty']-$short_close_qty;
       			 $pendingqty =number_format($pendingqry, 2, '.', ' ');

       ?>
       <tr>	
       	<td><?php echo $mrn_list_res['MRNNo'] ?></td>
       	<td><?php echo $s->getField('item_master','id',$mrn_list_res['mst_itemid'],'StockID'); ?></td>
       	<td><?php echo $s->getField('item_master','id',$mrn_list_res['mst_itemid'],'item_name'); ?></td>
       	<td><?php $unit_id= $s->getField('item_master','id',$mrn_list_res['mst_itemid'],'mst_unitid'); echo  $s->getField('unitsofmeasure','unitid',$unit_id,'unitname'); ?></td>       
       	<td style="text-align:right;"><?php echo $mrn_list_res['POQty'] ?></td>
       	<td style="text-align:right;"><?php echo $mrn_list_res['reciveqty'] ?></td>
       	<td style="text-align:right;"><?php echo $mrn_list_res['free_qty']; ?></td>
       	<td style="text-align:right;"><?php if($short_close_qty!='') { echo $short_close_qty; } else { echo $short_close_qty="0.00";} ?></td>
       	<td style="text-align:right;"><?php echo $pendingqty; ?></td>
       </tr>
    <?php } }  }else { ?>
    <tr><td colspan="9">No Record Found.</td></tr>
    <?php } ?>
  </table>
</div>
<?php } ?>

</div>
</div>
<script>
$(document).ready(function(){
	$('select[name="site"]').change(function(){
		if(this.value!='')
			{
				$.ajax({url:'ajax_po_vs_mrn.php',data:'site='+this.value, method:'post',success:function(response){
					//alert(response);
					var $arr=response.split('__');
					$('select[name="vendor"]').html($arr[0]);
					$('select[name="po_orderno"]').html($arr[1]);
				} });
			}
	});
	$('select[name="vendor"]').change(function(){
		if(this.value!='')
			{
				$.ajax({url:'ajax_po_vs_mrn.php',data:'vendor='+this.value, method:'post',success:function(response){
					
					$('select[name="po_orderno"]').html(response);
				} });
			}
	});

	$('#po_mrn').submit(function(){
		if($('select[name="site"]').val()=='')
		{
			alert("Please Select Site");
			return false;
		}
	})

})
</script>
<script type="text/javascript">
	function checkdate(date)
{
	var from =document.getElementById('from').value;
	var to =document.getElementById('to').value;
	
		var cdate=date.split("/");
		var sdate=from.split("/");
		var edate=to.split("/");

		cdate[1]=cdate[1]-1;
		sdate[1]=sdate[1]-1;
		edate[1]=edate[1]-1;
			 
		var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
		var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
	    var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

	    if (dateS > dateE) {
	        alert("Start Date is not greather then Date End.");
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	        
	    }

	    if (dateC > dateE) {
	        alert("Date Not greather than "+to);
	       document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }

	    if (dateC < dateS) {
	        alert(" Date Not less than "+from);
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }	
	
	
}

</script>