<?php $PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$title = _('Issue Register');
$date = date($_SESSION['DefaultDateFormat']);
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmView']=$frmpermission[3];
if($_SESSION['frmView']=='0')
{echo "<strong>You are not authorised to view this form. Contact your administrator</strong>"; exit;}
?>
<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);

	
}
function validation(){

var  site=document.getElementById("site").value;
if(site==''){
alert('Please select Site name.');
return false;
}


var  store=document.getElementById("store").value;
if(store==''){
alert('Please select Store name.');

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="+site,true);
	xmlhttp.send();


return false;
}
 
 
}
</script>
<form method="post" action="" onsubmit="return validation();">
<h2 align="center">Issue Register</h2>
<table width="35%" border="0" cellpadding="2" cellspacing="1" >
  <tr>
      <td>Site<span style="color:#FF0000;">*</span></td>
      <td><select name="site" id="site" onchange="showsitename160(this.value)">
            <option value="">Select Site</option>
            <?php 	
		$site_sql = "SELECT loccode, locationname from locations";
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
			$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>Store<span style="color:#FF0000;">*</span></td>
        <td>
		 <div id="storedata7">
	  <?php 
	  
	  $store_sql = "SELECT id, StoreName,StoreCode FROM storetype where Store_Site = '".$_REQUEST['site']."'";
	   		$store_result = DB_query($store_sql,$db);?>
            <select name="store1" id="store1">
              <option value="0">Select</option>
        <?php while($store_row = DB_fetch_array($store_result)){?>
              <option value="<?php echo $store_row['id'];?>"<?php echo($_REQUEST['store1']==$store_row['id']?'selected':'')?>><?php echo $store_row['StoreName'];?></option>
              <?php }?>
            </select></div>     </td>
      
        <td>From </td>
        <td><input type="text" name="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker" style="width:70px;"></td>
      
        <td>To </td>
        <td><input type="text" name="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker" style="width:70px;"></td>
      
     <!-- <tr>
        <th>Item Category</th>
        <td><select name="main_group"  style="width:100%" id="main_group" onchange="ShowDiv(this.value,'hid-sub_id','item_subcategory.php')">
	<option value="000">---Select---</option>
	<?php
	$query1=DB_query("select * from stockmaincategory",$db); 
	while($result1=DB_fetch_array($query1)) {
	?>
	<option value="<?php echo $result1['stockmaincategoryid'];?>"<?php echo ($_REQUEST['main_group']==$result1['stockmaincategoryid']?'selected':'')?>><?php echo $result1['stockmaincategoryname'];?></option>
	<?php } ?>
	</select>&nbsp;</td>
      </tr>
      <tr>
        <th>Item SubCategory </th>
        <td><div id="hid-sub_id"><select name="sub_group" id="sub_group" style="width:100%">
      <option value="000">---Select---</option>
      <?php
	$query2=DB_query("select * from stockcategory",$db); 
	while($result2=DB_fetch_array($query2)) {
	?>
      <option value="<?php echo $result2['categoryid'];?>"<?php echo ($_REQUEST['sub_group']==$result2['categoryid']?'selected':'')?>><?php echo $result2['categorydescription'];?></option>
      <?php } ?>
    </select></div>&nbsp;</td>
      </tr>
    </table></td>
  </tr>-->

    <td>Issue to </td>
    <td id="itemid">
    <select name="issueTo" id="issueTo">
	<option value="">--Select--</option>
   <?php 
   if($_REQUEST['issueTo']==''){$_REQUEST['issueTo']="";}
	$floor_sql=DB_query("Select * from contractor_master",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{?>
    <option value="<?php echo $floor_row['id'];?>" <?php if($_REQUEST['issueTo']==$floor_row['id']){?> selected="selected"<? } ?>><?php echo $floor_row['contractor_name'];?></option>
	<?php 	
	}
	?>    	
    </select></td>
 
  <td>Item</td>
  <td>
        <select name="itemdata" id="itemdata">
            <option value="">All</option>
            <?php $itemSql=DB_query("Select id,StockID,item_name from item_master ORDER BY item_name ASC",$db);
				  while($itemRow=DB_fetch_array($itemSql)){?>
            <option value="<?php echo $itemRow['id'];?>"<?php echo($_REQUEST['itemdata']==$itemRow['id']?'selected':'')?>><?php echo $itemRow['item_name'];?></option>
            <?php }?>
        </select></td>
</tr>
<tr>
    <td style="text-align:center">
        <input type="submit" name="view" value="View">
</td>
  </tr>
</table>


<?php if($_POST['view'])
{?><p align="right">

<a  href="IssueRegister_print.php?loccode=<?php echo $_REQUEST['site'];?>&todate=<?php echo $_REQUEST['to_date'];?>&fromdate=<?php echo $_REQUEST['from_date'];?>&item_code=<?php echo $_REQUEST['itemdata']?>&issueto=<?php echo $_REQUEST['issueTo'];?>" target="_blank" >Print</a> 

<!--
<a href="tcpdf/Pdf_issue_register_exp.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&item_code=<?php echo $_REQUEST['item']?>" target="_blank">PDF</a>

<a href="issue_register_exp.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&item_code=<?php echo $_REQUEST['item']?>">Export</a>-->
 	&nbsp;&nbsp;<!--<a href="PO_Order_Print.php?qtid=<?php echo $_REQUEST['VQuotesId'];?>">Print</a>-->
 </p>
<?php } ?>
</form>
<?php 
if($_REQUEST['view']!='')
{
	$fromdate=$_REQUEST['from_date'];
	$todate=$_REQUEST['to_date'];
	
	$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];
	$sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
    $storecode=$s->getField('storetype','id',$_REQUEST['store1'],'StoreName');
	$issueto=$s->GetFieldValue('contractor_master',"id='".$_REQUEST['issueTo']."'",'contractor_name');
	$ItemName=$s->GetFieldValue('item_master',"id='".$_REQUEST['itemdata']."'",'item_name');
	$filter=array();
	if($_REQUEST['site']!='')
	$filter[]="addissue.site = '".$_REQUEST['site']."'";
	 if($_REQUEST['issueTo']!='')
	$filter[]="addissue.issueto = '".$_REQUEST['issueTo']."'";	
	if($_REQUEST['store1']!='0')
	$filter[]="addissue.mst_storeid = '".$_REQUEST['store1']."'";
	if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	$filter[]="addissue.issuedate BETWEEN '$from' and '$to'";
	if($_REQUEST['itemdata']!=''){
		$filter[]="issueqty.mst_itemid='".$_REQUEST['itemdata']."'";
	}	
	
	if($filter!='')
	{
		$filter=implode(' and ',$filter);
	}
	
	if($_REQUEST['site']!='' && $_REQUEST['store1']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='' && $_REQUEST['issueTo']=='' && $_REQUEST['itemdata']=='')
	{
	$message="Showing Records of Site ".$sitename."-Store ".$storecode."";	
	}
	
	elseif($_REQUEST['site']!='' && $_REQUEST['store1']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['issueTo']=='' && $_REQUEST['itemdata']=='')
	{
	$message="Showing Records of Site ".$sitename."   From  ".$fromdate." to ".$todate."";
	}
	
	elseif($_REQUEST['site']!='' && $_REQUEST['store1']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['issueTo']!='' && $_REQUEST['itemdata']=='')
	{
	$message="Showing Records of Site ".$sitename." Issue To ".$issueto."   From  ".$fromdate." to ".$todate."";
	}
	
	elseif($_REQUEST['site']!='' && $_REQUEST['store1']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['issueTo']!='' && $_REQUEST['itemdata']!='')
	{
	$message="Showing Records of Site ".$sitename." Issue To ".$issueto." For Item ".$ItemName."   From  ".$fromdate." to ".$todate."";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['store1']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='' && $_REQUEST['issueTo']!='' && $_REQUEST['itemdata']=='')
	{
	$message="Showing Records of Site ".$sitename."-Store ".$storecode." And Issue To ".$issueto."";	
	}

else if($_REQUEST['site']!='' && $_REQUEST['store1']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['issueTo']=='' && $_REQUEST['itemdata']!='')
	{
	$message="Showing Records of Site ".$sitename."-Store ".$storecode." For Item ".$ItemName." From  ".$fromdate." to ".$todate."";	
	}




	if($filter!='')
	{
	
 	   $sql ="Select addissue.* from addissue LEFT JOIN issueqty ON addissue.id=issueqty.issue_id where $filter group by addissue.id order by addissue.issuedate";
	 }
	 else 
	 {
	   $sql ="Select * from addissue  group by addissue.id order by addissue.issuedate";
	 }
	 
	$_SESSION['issuereport22']=$sql;
	$_SESSION['mess22']=$message;


?>
 <?php if($_REQUEST['view']!=''){?>
 <br />
<div align="center"><strong><?php echo $message; ?></strong> </div>
<br />
<? } ?>
<div style="width:970px; overflow:auto;">
  <blockquote>&nbsp;</blockquote>
  <table width="100%" cellpadding="0" cellspacing="0" border="1" style="border:solid 1px #666666;">
    <tr style="background-color:#CCCCCC; height: 30px">
	  <td width="22"   class="jjrs_tablehead"><strong>S.No.</strong></td>
      <td width="56"  class="jjrs_tablehead"><strong>Date</strong></td>
      <td width="124"  class="jjrs_tablehead"><strong>Issue Slip No.</strong></td>
<?php if($_REQUEST['issueTo']=='') {?>
      <td width="100"  class="jjrs_tablehead"><strong>Issued To </strong></td>
<?php } ?>	
<?php if($_REQUEST['itemdata']=='') {?> 
      <td width="158"  class="jjrs_tablehead" style="width:114px;"><strong> Item Name</strong></td>
<?php } ?>	
      <td width="45"  class="jjrs_tablehead" ><strong>UOM</strong></td>
      <td width="75"  class="jjrs_tablehead"><span class="jjrs_tablehead" style="width:76px;"><strong>Issue Qty </strong></span></td>
      <td width="109"   class="jjrs_tablehead" ><strong>Remark</strong></td>
      <!-- <td width="97" class="jjrs_tablehead"  style="width:77px;"><strong>Total Price </strong></td>
	 <td width="102" class="jjrs_tablehead"  style="width:77px;"><strong>Amount</strong></td>
	 -->
    </tr>
    <?php 
 // $sql = "Select * from addissue";
  //zecho $sql;
  
  $i=1;
  $result=DB_query($sql,$db);
  if(DB_num_rows($result)>0){
  while($myrow=DB_fetch_array($result))
  {
  	
  ?>
    <tr bgcolor="#FFFFFF">
	  <td class="jjrs_tablehead"><?php echo $i++ ;?></td>
      <td  style="width:50px;" class="jjrs_tablehead"><?php echo ConvertSQLDate($myrow['issuedate']);?></td>
      <td  style="width:100px;" class="jjrs_tablehead"><?php echo $myrow['issue_slipno'];?></td>
<?php if($_REQUEST['issueTo']=='') {?>
          <td  style="width:100px;" class="jjrs_tablehead"><?php 
		if(!empty($myrow['issueto'])){
			 $contractor_name = $s->GetFieldValue('contractor_master',"id='".$myrow['issueto']."'",'contractor_name') ;
		}
		else
		{
			 $contractor_name = $s->GetFieldValue('empmaster',"id='".$myrow['received_by']."'",'emp_name') ;
		}
		if($contractor_name!='')
		{
			echo $contractor_name;
		}
		else
		{
			echo '&nbsp;';
		}
		?></td>
<?php } ?>
<?php 
$details_sql="Select * from issueqty where issue_id='".$myrow['id']."'";
if($_REQUEST['itemdata']!=''){
 $details_sql .=" AND mst_itemid = '".$_REQUEST['itemdata']."'";
}	

$_SESSION['details_sql']=$details_sql;
$details_res=DB_query($details_sql,$db);
$issueqty='';
$req_qty='';
$grandtot='';
$numrow1=DB_num_rows($details_res);
if($_REQUEST['itemdata']=='') {
?>
      <td colspan="4">
	  <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <?php 

	while($details_row=DB_fetch_array($details_res)){?>
          <tr bgcolor="99CCCC">
            <td width="134" class="jjrs_tabledata1"><?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'item_name'); ?></td>
            <td width="61"   class="jjrs_tabledata1"><?php 
			 $unitname1 = $s->GetFieldValue('item_master',"id='".$details_row['mst_itemid']."'",'unit');
			echo $unitname = $s->GetFieldValue('unitsofmeasure',"unitid='".$unitname1."'",'unitname') ; ?> </td>
            <td width="70"  class="jjrs_tabledata1"  ><span class="jjrs_tabledata1"><?php echo $details_row['issueqty'];
			$issueqty[]=$details_row['issueqty'];
		?></span></td>
            <td width="112"  class="jjrs_tabledata1"><?php echo $site = $s->GetFieldValue('work_master',"id='".$details_row['work_id']."'",'work_name'); ?></td>
            <!--<td width="93" class="jjrs_tabledata1"  style="width:70px;"><?php echo $details_row['totalamount'];?></td>
		<td width="101" class="jjrs_tabledata1"  style="width:70px;"><?php echo $details_row['itemdesc'];?></td>-->
          </tr>
          <?php } ?>
       <tr bgcolor="#e7e7e7" style="height: 25px" >
       <td class="jjrs_tabledata1">&nbsp;</td>
      <td width="45" class="jjrs_tabledata1"><strong>Total</strong></td>
      <td class="jjrs_tabledata1"><strong><?php echo $a[]=array_sum($issueqty); $grandtot[]=array_sum($a);?></strong></td>
	  <td class="jjrs_tabledata1"><strong>&nbsp;</strong></td>
    </tr>
    </table>
	</td>
<?php }else{ ?>

<td colspan="3">
	  <table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php 
	$details_row=DB_fetch_array($details_res)?>
          <tr bgcolor="99CCCC">
            <td width="61"   class="jjrs_tabledata1"><?php 
			 $unitname1 = $s->GetFieldValue('item_master',"id='".$details_row['mst_itemid']."'",'unit');
			echo $unitname = $s->GetFieldValue('unitsofmeasure',"unitid='".$unitname1."'",'unitname') ; ?> </td>
            <td width="70"  class="jjrs_tabledata1"  ><span class="jjrs_tabledata1"><?php echo $details_row['issueqty'];
			$issueqty[]=$details_row['issueqty'];
		?></span></td>
            <td width="112"  class="jjrs_tabledata1"><?php echo $site = $s->GetFieldValue('work_master',"id='".$details_row['work_id']."'",'work_name'); ?></td>
            <!--<td width="93" class="jjrs_tabledata1"  style="width:70px;"><?php echo $details_row['totalamount'];?></td>
		<td width="101" class="jjrs_tabledata1"  style="width:70px;"><?php echo $details_row['itemdesc'];?></td>-->
          </tr>
       <tr bgcolor="#e7e7e7" style="height: 25px" >
      <td width="45" class="jjrs_tabledata1"><strong>Total</strong></td>
      <td class="jjrs_tabledata1"><strong><?php echo $a[]=array_sum($issueqty); $grandtot[]=array_sum($a);?></strong></td>
	  <td class="jjrs_tabledata1"><strong>&nbsp;</strong></td>
    </tr>
    </table>
	</td>
<?php } ?>
    </tr>
<?php } ?>
<?php if($_REQUEST['issueTo']=='') {?>	
	<tr bgcolor="#e7e7e7" style="height: 25px">
		<td class="jjrs_tabledata1" colspan="4">
		<table width="100%">
			<tr><td>&nbsp;</td></tr>
		</table>
		</td>
<?php if($_REQUEST['itemdata']=='') { ?>
		<td colspan="4">
		<table width="100%">
			<tr>
			<td width="240" class="jjrs_tabledata1"><strong>Grand Total</strong></td>
			<td>&nbsp;</td>
			<td class="jjrs_tabledata1"><strong><?php echo $c[]=array_sum($grandtot)?></strong></td>
			<td>&nbsp;</td>
			</tr>
		</table>
		</td>
		<?php }else{ ?>
		<td colspan="3">
		<table width="100%">
			<tr>
			<td width="100" class="jjrs_tabledata1"><strong>Grand Total</strong></td>
			<td class="jjrs_tabledata1"><strong><?php echo $c[]=array_sum($grandtot)?></strong></td>
			<td>&nbsp;</td>
			</tr>
		</table>
		</td>		
		<?php }?> 
	</tr>
<?php } else{ ?>
	</tr>
	<tr bgcolor="#e7e7e7" style="height: 25px">
		<td class="jjrs_tabledata1" colspan="3">
		<table width="100%">
			<tr><td>&nbsp;</td></tr>
		</table>
		</td>
		<?php if($_REQUEST['itemdata']=='') { ?>
		<td colspan="4">
		<table width="100%">
			<tr>
			<td width="260" class="jjrs_tabledata1"><strong>Grand Total</strong></td>
			<td>&nbsp;</td>
			<td class="jjrs_tabledata1"><strong><?php echo $c[]=array_sum($grandtot)?></strong></td>
			<td>&nbsp;</td>
			</tr>
		</table>
		</td>
		<?php }else{ ?>
		<td colspan="3">
		<table width="100%">
			<tr>
			<td width="120" class="jjrs_tabledata1"><strong>Grand Total</strong></td>
			<td class="jjrs_tabledata1"><strong><?php echo $c[]=array_sum($grandtot)?></strong></td>
			<td>&nbsp;</td>
			</tr>
		</table>
		</td>		
		<?php }?> 
	</tr>	
	<?php  }?>
	

	<?php }else{ ?>
    
	   <tr bgcolor="#e7e7e7" style="height: 25px">
      <td class="jjrs_tabledata1" colspan="8" style="color:#FF0000;" align="center"><strong>No Records</strong></td>
    </tr>
	  
      <?php  } } ?>     
  </table>

</div>