<?php session_name('CAKEPHP'); session_start();
error_reporting(FALSE);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$title = _('Purchase Order Register');
$PageName ='Purchase Order Register';	
?>
<script language="javascript" type="text/javascript">

function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
function showItems(str)
{
    if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?partName="+str,true);
xmlhttp.send();
}



function showsitename160(str)
{

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	var data=xmlhttp.responseText.split("__");	
	document.getElementById("storedata7").innerHTML=data[0];
	  }
  }
  xmlhttp.open("GET","get_sitenamedata2.php?site="+str,true);
	xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

	
}
</script>

<form method="post" action="">


  <h2 align="center">Dispatch Register</h2>
  <table width="100%" align="center">
    <tr>
      <td>Branch</td>
      <td><select name="site" id="site" onChange="showsitename160(this.value)">
          <option value="">Select Site</option>
          <?php 		
if($_SESSION['AssignedBranch']!='')
	  {	  	
      $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."'
    and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }		
		while($site_row = DB_fetch_array($site_sql))
		{
			$p='';
			if($site_row['loccode'] == $_POST['site'])	
			$p="selected";
		?>
          <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
          <?php } ?>
        </select>
      </td>
      <td>Warehouse</td>
      <td><?php
		$sql_store = "select id, StoreCode, StoreName from storetype where CompanyId='".$_SESSION['ACGCompanyId']."' and Store_Site in ('".$_SESSION['AssignedBranch']."') order by StoreCode ASC";
		$resultStkLocs = DB_query($sql_store,$db);
		?>
          <select id="storedata7" name="storecode">
            <option value="000">--Select--</option>
            <option value="00" <?php if($_REQUEST['storecode']=='00') { echo 'selected'; } else { echo '';} ?>>All</option>
            <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
				$p='';
				if($_REQUEST['storecode']==$myrow['id'])
				$p="selected";
		?>
            <option value="<?php echo $myrow['id']; ?>" <?php echo $p; ?>><?php echo $myrow['StoreName']; ?></option>
            <?php }
		?>
        </select></td>
      <td> Party </td>
      <td><select name="party_id" id="party_id">
          <option value="0">Select Party</option>
          <option value="00" <?php if($_REQUEST['party_id']=='00') { echo 'selected'; } else { echo '';} ?>>All</option>
          <?php 
      $party=DB_query("Select id,client_code,c_name from clientmaster",$db);
	  while($party_row=DB_fetch_array($party))
	  {
	  $r='';
				if($_REQUEST['party_id']==$party_row['id'])
				$r="selected";
	  ?>
          <option value="<?php echo $party_row['id'];?>"<?php echo $r; ?>><?php echo $party_row['c_name'];?></option>
          <?php 	
	  }
      ?>
        </select>
      </td>
    </tr>
    <tr>
      <td class="tabledata1">Site</td>
      <td ><select name="to_site" id="site_to"  style="width:150px;" onChange="getaddress(this.value)">
          <option value="0" >---Select---</option>
          <option value="00" <?php if($_REQUEST['to_site']=='00') { echo 'selected'; } else { echo '';} ?>>All</option>
          <?php
			$sql="select id, StoreCode, StoreName from locations_to";
			$result = DB_query($sql, $db);
			while($myrow = DB_fetch_array($result))
			{ 
			$h='';
				if($_REQUEST['to_site']==$myrow['id'])
				$h="selected";
			?>
          <option  value="<?php echo $myrow['id']; ?>"<?php echo $h; ?>><?php echo  $myrow['StoreName']; ?></option>
          <?php }
			?>
        </select>
      </td>
      <td>From</td>
      <td><input type="text" name="from_date" id="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker" /></td>
      <td>To</td>
      <td><input type="text" name="to_date" id="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker" /></td>
    </tr>
    <!-- <td>Item</th>
    <td id="itemid">
    <select name="itemCode" id="itemCode">
	<option value="">Select ITEM</option>
    <?php 
	$itemSql=DB_query("Select id,StockID,item_name from item_master",$db);
	while($itemRow=DB_fetch_array($itemSql))
	{?>
    <option value="<?php echo $itemRow['id'];?>"<?php echo($_REQUEST['itemCode']==$itemRow['id']?'selected':'')?>><?php echo $itemRow['StockID']." - ".$itemRow['item_name'];?></option>
	<?php 	
	}
	?>    	
    </select></td>-->
  <td align="center"><p align="center">
    <input type="submit" name="view" value="View" onClick="return validation1();" />
    <!--<input type="submit" name="print" value="Print">
    <input type="submit" name="cancel" value="Cancel">-->
  </p></td>
  </tr>
  </table>
</form>
<script type="text/javascript">
function validation() { //alert(document.getElementById("site").value);
		if(document.getElementById("site").value=='')
		{
			alert("Please Select Branch First");
			(document.getElementById("site")).focus();
			return false;
		}
		else if(document.getElementById("storecode").value=='000')
		{
			alert("Please Select Warehouse");
			(document.getElementById("storecode")).focus();
			return false;
		}
		else if(document.getElementById("ptype").value=='')
		{
			alert("Please Select Purchase Type");
			(document.getElementById("ptype")).focus();
			return false;
		}
		else if(document.getElementById("from_date").value=='')
		{
			alert("Please Select From Date");
			(document.getElementById("from_date")).focus();
			return false;
		}
		else if(document.getElementById("to_date").value=='')
		{
			alert("Please Select To Date");
			document.getElementById("to_date").focus();
			return false;
		}
	}
	</script>
<?php 
if($_REQUEST['view']!='')
{
 
 $filter=array();
		
		$fromDate=$_REQUEST['from_date'];
		$fromDateBrk=explode("/",$fromDate);
		$fromDate=$fromDateBrk[0];
		$fromMonth=$fromDateBrk[1];
		$fromYear=$fromDateBrk[2];
		$From=$fromYear."-".$fromMonth."-".$fromDate;
		
		$toDate=$_REQUEST['to_date'];
		$toDateBrk=explode("/",$toDate);
		$toDate=$toDateBrk[0];
		$toMonth=$toDateBrk[1];
		$toYear=$toDateBrk[2];
		$To=$toYear."-".$toMonth."-".$toDate;	
		 
		if($_REQUEST['site']!='')
		$filter[]="log_dispatch.site='".$_REQUEST['site']."'";
		if($_REQUEST['storecode']!='' && $_REQUEST['storecode']!='00')
		$filter[]="log_dispatch.store='".$_REQUEST['storecode']."'";	
		if($_REQUEST['storecode']=='00')
		$filter[]="1=1";
		if($_REQUEST['party_id']!='' && $_REQUEST['party_id']!='00')
		$filter[]="log_dispatch.party_id='".$_REQUEST['party_id']."'";	
		if($_REQUEST['party_id']=='00')
		$filter[]="1=1";
		if($_REQUEST['to_site']!='' && $_REQUEST['to_site']!='00')
		$filter[]="log_dispatch.to_site='".$_REQUEST['to_site']."'";	
		if($_REQUEST['to_site']=='00')
		$filter[]="1=1";
		if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
		$filter[]="log_dispatch.dis_date between '".$From."' and '".$To."'";		
		//print_r($filter);exit();
	    $sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
		
		$sitename1=$s->getField('suppliers','id',$_REQUEST['vendor'],'vendor_name');
		$storeNames=$s->getField('storetype','id',$_REQUEST['storecode'],'StoreName');
		if($_REQUEST['ptype']=="1") { $purchType = 'Local'; } elseif($_REQUEST['ptype']=="2") { $purchType = 'Import'; } elseif($_REQUEST['ptype']=="3") { $purchType = 'FOC'; }
		
		if($_REQUEST['site']!='' && $_REQUEST['vendor']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['storecode']!='')
	{
	$message="Showing Records of Branch ".$sitename."  , vendor '".$sitename1."', Warehouse '".$storeNames."' From  ".$From." to ".$To."";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='' && $_REQUEST['storecode']=='')
	{
	$message="Showing Records of Branch ".$sitename." ";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records   From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']==''  && $_REQUEST['storecode']=='' && $_REQUEST['vendor']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of vendor   ".$sitename1." ";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']!='' && $_REQUEST['storecode']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of Branch ".$sitename.", Warehouse '".$storeNames."' and vendor '".$sitename1."'";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['storecode']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  vendor '".$sitename1."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['storecode']!='')
	{
	$message="Showing Records  of vendor '".$sitename1."', Warehouse '".$storeNames."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  Branch ".$sitename."  From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']==''  && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of   Branch ".$sitename."";
	}
	
	//$_SESSION['mess']=$message;
		
		if($filter != '')
		{
		 $filter1 = implode(" and ",$filter);
		}
		
		if($filter1!='')
	{	
	     	
			$sql="SELECT log_dispatch.*,log_dispatch.id as main_id, log_dispatchqty.* from log_dispatch INNER JOIN log_dispatchqty on(log_dispatch.id = log_dispatchqty.dis_id) where $filter1 and log_dispatch.CompanyId='".$_SESSION['ACGCompanyId']."' group by log_dispatch.id order by log_dispatch.dis_date ";
	 }
	
 //echo $sql;
				
	 //$_SESSION['data1']=$sql;
}

?>
<p align="right"><a href="../../POModule/reports/purchaseorderRegisterPrint2.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&itemId=<?php echo $_REQUEST['itemCode']?>&vendor=<?php echo $_REQUEST['vendor'];?>" target="_blank">Print</a></p>
<?php if($_REQUEST['view']!=''){?>
<div style="width:auto; height:auto; overflow:auto; margin-left:auto; margin-right:auto;">
<div align="center"><strong><?php echo $message; ?></strong> </div>
<br />
<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:solid 1px #666666;">
  <tr>
    <th >S. No. </th>
    <th > Dispatch No</th>
    <th>Dispatch Date</th>
  <th width="10%">Party Name</th>
  <th width="10%">Site Code </th>
    <th>Site Name</th>
	<th>Weigh bill No</th>
    <th style="width:100px;">Weigh bill Amount </th>
    <th style="width:75px;">Part Code</th>
    <th  style="width:50px;">Part Name</th>
	<th>UOM</th>
	<th>Dispatch Qty</th>
	<th>Dispatch Box Qty</th>
  </tr>
  <?php 
 // $sql = "Select * from addissue";
  //zecho $sql;
  $sno=0;
  $result=DB_query($sql,$db);
 	$numrow1=DB_num_rows($result);
	if(DB_num_rows($result)>0){
  while($myrow=DB_fetch_array($result))
  {
  $sno++;	
  ?>
  <tr bgcolor="#FFFFFF">
    <td height="34" ><?php echo $sno;?></td>
    <td><?php echo $myrow['dis_no'];?></td>
    <td><?php echo ConvertSQLDate($myrow['dis_date']); ?></td>
    <td><?php 
	  $vendorname=$s->getField('clientmaster','id',$myrow['party_id'],'c_name'); echo $vendorname;?></td>
	  <td><?php echo $myrow['site'];?>&nbsp;</td>
    <td><?php echo $s->getField('locations','loccode',$myrow['site'],'locationname'); ?>&nbsp;</td>
    <td><?php echo $myrow['way_bill_no']; ?>&nbsp;</td>
    <td><?php echo $myrow['way_bill_amt']; ?>&nbsp;</td>
        <td colspan="9" align="left" valign="top"><table width="100%" height="25" border="0" cellpadding="0" cellspacing="0">
          <?php
    
	   $details_sql="Select * from log_dispatchqty where dis_id='".$myrow['main_id']."'";
	//$details_sql="Select * from purchorderdetails where orderno='".$myrow['orderno']."' 
	//and mst_itemid='".$_REQUEST['itemCode']."'";
	$details_res=DB_query($details_sql,$db);
	$issueqty[]='';
	$req_qty[]='';
	$numrow1=DB_num_rows($details_res);
	if(DB_num_rows($details_res)>0){
	while($details_row=DB_fetch_array($details_res)){?>
          <tr bgcolor="99CCCC">
            <td style="width:103px;" class="jjrs_tabledata1" ><?php echo  $s->getField('item_master','id',$details_row['mst_itemid'],'StockID'); ?></td>
            <td class="jjrs_tabledata1" style="width:90px;"><?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'item_name'); ?></td>
            <td class="jjrs_tabledata1" style="width:62px;"><?php  $unut = $s->getField('item_master','id',$details_row['mst_itemid'],'mst_unitid');
		echo $s->getField('unitsofmeasure','unitid',$unut,'unitname');
		 ?>            </td>
		 <td class="jjrs_tabledata1" style="width:62px;"><span class="jjrs_tabledata1" style="width:62px;">
		   <?php  echo $details_row['dispatchqty']; ?>
		 </span></td>
<td  class="jjrs_tabledata1"><?php
			 $query="SELECT * FROM log_addmrnbox WHERE dis_id='".$myrow['main_id']."' and itemid ='".$details_row['mst_itemid']."'  and dispatch=1";
			 $details_res3=DB_query($query,$db);
echo $numrow3=DB_num_rows($details_res3);			//echo count($box);  ?></td>
          </tr>
          <?php } ?>
          <?php } 
	   else { ?>
          <tr bgcolor="#e7e7e7" style="height: 25px">
            <td class="jjrs_tabledata1" colspan="8" align="center"><strong><span class="jjrs_tabledata1" style="width:90px;"><?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'item_name'); ?></span>No Records</strong></td>
          </tr>
          <?php } ?>
        </table></td>
  </tr>
  <?php  } ?>
  <?php  }
  else 
  { ?>
  <tr colspan= "2" bgcolor="#e7e7e7" style="height: 25px;">
    <td class="jjrs_tabledata1" colspan="8" align="center"><strong>No Records Found</strong></td>
   </tr>
 <?php } ?>
</table>
</div>
<?php } ?>

	</html>

