<?php
session_name('CAKEPHP'); session_start();
$PageSecurity = 3;
 include ('includes/session.inc');
 include ('includes/function_lib.php');
 include('includes/fun-php.php');
 include('includes/fun-HRClass.php');
function getField($table,$condition,$val,$field)
{
  	 global $db;
   	 $sql=DB_query("select ".$field." from ".$table." where ".$condition." ='".$val."'",$db);
	 $r=DB_fetch_array($sql);
   	 return ($r[$field]);
}

global $db; 
$Compamies=DB_query("select * from companies",$db);
$CompamiesRow=DB_fetch_array($Compamies);
$CompamiesRow['CompanyId'];
//$branch=$s->GetFieldValue('locations',"CompanyId='".$CompamiesRow['Branch_Permission']."'",'locationname');
 $branch=getField('locations','CompanyId',$CompamiesRow['Branch_Permission'],'locationname'); 

$sqlq=$_SESSION['asset_issue_rep'];
   $sql1 = "SELECT * FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'";
  $sqlres = DB_query($sql1,$db);
  $companyRow = DB_fetch_array($sqlres); 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<title>Stock Summary Report</title>
<style type="text/css"> 
@print {size:A4; margin:20px 20px;}

@media print{
body{ margin:0px; padding:0px;  font-size:12px; line-height: 18px; font-family: Arial, Helvetica, sans-serif; color:#000; font-weight:normal;}

h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}

h1{
  font-size: 20px; line-height: 24px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;
  color:#006699;  
}
h2{
  font-size: 18px; line-height: 22px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;
}
thead{display: table-header-group;}
.wrapper{width:99%; height: auto; float:left; border:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_first, .div_first .full{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;
  border-top: #000000 1px solid;*/
  /*border-left: #000000 1px solid;
  border-right: #000000 1px solid;*/
  float: left; position: relative;
}
 .div_first .left
        {
          width:10%; text-align:left; height:auto; vertical-align:middle; height:auto; padding: 10px; position: absolute; left: 0; top: 0;
        }
        .div_first .middle
        {
          width:60%; text-align:center; padding: 10px; margin:0 auto; position: relative;
        }
        .div_first .right
        {
          width:20%;text-align:right; padding: 10px; position: absolute; right: 0; top: 0;
        }

.div_first .full{text-align: center; border: 0;}
.div_second, .div_third{
  width: 100%;
  height:auto;
  /*border-top: #000000 1px solid;*/
  /*border-bottom: #000000 1px solid;*/
  /*border-left: #000000 1px solid;
  border-right: #000000 1px solid;*/
  float: left;
}
.div_second table tr th, .div_second table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr th{text-align: left; border-top:#000000 1px solid;}
.div_second table tr th:last-child, .div_second table tr td:last-child{border-right:0;}
.div_second table tr:last-child td{border-bottom:0;}
.div_second table tr td > table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr td > table tr td:last-child{border-right:0;}
.div_second table tr td > table tr:last-child td{border-bottom:0;}

.div_third table tr th, .div_third table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_third table tr th{background-color:transparent; text-align: left;}
.div_third table tr th:last-child, .div_third table tr td:last-child{border-right:none;}
.div_third table tr:last-child td{border-bottom:none;}

.f12{ font-weight: bold; color:#000; font-size:12px;}
.f11{color:#000; font-size:12px; line-height:18px;}
.f14{color:#000; font-size:18px;}
.mt26{margin-top:4px; width:90%;}
.mt27{margin-top:0px; width:62%;}
.mt28{margin-top:0px; width:100%;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.wdt1{width:8%;}
.wdt2{width:100px;}
.wdt3{width: 66px;}
.wdt4{width: 80px;}
.wdt5{width: 52px;}
.wdt6{width: 60px;}
}

@media screen{
/* CSS Document */
body{ margin:0px; padding:0px; background-color:#fff; font-size:12px; font-family: Arial, Helvetica, sans-serif; color:#000; font-weight:normal;}
h1,h2,h3,h4,h5,p{ margin:0px; padding:0px;}
p{ text-align:left;}

#foo{width:1070px; height: auto; margin:0px auto;}
.wrapper{width:1070px; height: auto; margin:10px auto; border-top:#000000 1px solid;}

h1{
  font-size: 24px; line-height: 28px;
  font-weight:bold;
  font-family:Arial, Helvetica, sans-serif !important;
  color:#006699;  
}
h2{
  font-size: 18px; line-height: 22px;
  font-weight:normal;
  font-family:Arial, Helvetica, sans-serif !important;  
}
.div_first, .div_first .full{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;
  border-top: #000000 1px solid;*/
  border-left: #000000 1px solid;
  border-right: #000000 1px solid;
  float: left; position: relative; display: table;
}
.div_first .full{text-align: center; border: 0; margin:10px 0 0;}
.div_first .left, .div_first .middle, .div_first .right{float: left; display: table-cell; vertical-align: middle;}
.div_first .left
        {
          width:8%; text-align:center; height:auto; vertical-align:middle; padding: 1%; 
        }
        .div_first .middle
        {
          width:78%; text-align:center; padding: 1%; position: relative; margin:0 auto;
        }
        .div_first .right
        {
          width:8%; text-align:center; padding:1%; 
        }
.div_second, .div_third, .div_forth{
  width: 100%; height: auto;
  /*border-bottom: #000000 1px solid;*/
  border-top: #000000 1px solid;
  border-left: #000000 1px solid;
  border-right: #000000 1px solid;
  float: left;
}
.div_second table tr th, .div_second table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr th{background-color: #ccc; text-align: left;}
.div_second table tr th:last-child, .div_second table tr td:last-child{border-right:0;}
/*.div_second table tr:last-child td{border-bottom:0;}*/
.div_second table tr td > table tr td{border-right:#000000 1px solid; border-bottom:#000000 1px solid;}
.div_second table tr td > table tr td:last-child{border-right:0;}
.div_second table tr td > table tr:last-child td{border-bottom:0;}

.div_third table tr th, .div_third table tr td{ border-bottom:#000 1px solid; border-right:#000 1px solid;}
.div_third table tr th{background-color: #ccc; text-align: left;}
.div_third table tr th:last-child, .div_third table tr td:last-child{border-right:none;}
/*.div_third table tr:last-child td{border-bottom:none;}*/

.f12{font-weight: bold; color:#000; font-size:12px;}
.f11{ color:#000; font-size:12px; line-height:18px;}
.f14{ color:#000; font-size:18px;}
.mt26{margin-top:4px; width:90%;}
.mt27{margin-top:0px; width:52%;}
.mt28{margin-top:0px; width:90%;}
.border_top{border-top:#000 1px solid;}
.border_bottom{border-bottom:#000 1px solid;}
.wdt1{width:10%;}
.wdt2{width:180px;}
.wdt3{width: 66px;}
.wdt4{width: 120px;}
.wdt5{width: 52px;}
.wdt6{width: 70px;}
}
</style>
</head>
<body>
<div id="foo">
  <p style="text-align:right;"><input type="button" onclick="javascript:toggle_visibility('foo');" value="Print" /></p>
</div>
<div class="wrapper">

<div class="div_first">
<?php $logoImage=($CompamiesRow['CompanyLogo']=='')?'Final_Logo_RM.png':$CompamiesRow['CompanyLogo']; ?>
      <div class="left"><img src="Setup/CompanyLogo/<?php echo $CompamiesRow['CompanyLogo']; ?>" class="mt26" /></div>
      <div class="middle"><h1>Ayodhya Eye Hospital</h1>
      <span class="f12">A Unit of Mani Mandir Sewa Nyas Samiti</span> <br>
       <span class="f12">Tulsibari, Praikrma Marg, Ayodhya - 224123, Dist. - Faizabad</span>
       <div class="full"><h2>Asset Stock Summary Report</h2>
       <span class="f11"><?php echo "<span class='f12'>Phone No.:</span> ".$CompamiesRow['Phone'].", <span class='f12'>Fax No.:</span> ".$CompamiesRow['fax'].", <span class='f12'>Email ID.:</span> ".$CompamiesRow['email'].", <span class='f12'>Website:</span> ".$CompamiesRow['website']; ?><br>
        <?php $messeg=$_SESSION['mess']; ?><?php echo $messeg; ?>
        </span></div>
      </div>
      <div class="right"><img src="images/eye-logo-blue.png" vspace="10" width="100%" /></div>
</div>
<div class="div_second">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <thead>
    <tr class="f12">
    <th style="text-align:left;" width="3%">S.No.</th>
    <th style="text-align:left;" width="11%">Asset Code</th>
    <th style="text-align:left;">Asset Name</th>
	<th style="text-align:left;" width="10%">UOM</th>
    <th width="10%" style="text-align:left;">Opening Balance Qty.</th>
    <th width="10%" style="text-align:left;">Received Qty.</th>
	<th width="10%" style="text-align:left;">In Process Qty.</th>
    <th width="10%" style="text-align:left;">Issued Qty.</th>
    <th width="10%" style="text-align:left;">Closing Balance Qty.</th>	
  </tr>
  <!-- <tr class="f12">
    <th style="text-align:left;">QTY.</th>
    <th style="text-align:left;">QTY.</th>
    <th style="text-align:left;">QTY.</th>
    <th style="text-align:left;">QTY.</th>
    <th style="text-align:left;">QTY.</th>
  </tr> -->
 </thead>
<?php 
	$from_date=$_REQUEST['fromdate'];
	$to_date=$_REQUEST['todate'];	
	$from_date_brk=explode('/',$from_date);
	$from=$from_date_brk[2]."-".$from_date_brk[1]."-".$from_date_brk[0];
	
	$to_date_brk=explode('/',$to_date);
	$to=$to_date_brk[2]."-".$to_date_brk[1]."-".$to_date_brk[0];
	
	$site=$_REQUEST['site'];
	$store=$_REQUEST['store_code'];
	$function_filter[]=$site;
	$function_filter[]=$store;	
	$function_filter[]=$from;
	$function_filter[]=$to;
	
	function privious_opening($function_filter,$item)
	{
		 global $db;
		 $receive=array();
		 $issueqty=array();	
		 $site=$function_filter[0];
		 $store=$function_filter[1];
		 $fdate1= strtotime(date("Y-m-d", strtotime($function_filter[2])) . " -1 day");
		 $fdate= date("Y-m-d",$fdate1);
	  
		/**************************************  Issue *********************************************/
		$sql1="Select SUM(IA.issue_qty) as issue_qty from tpl_asset_transfer_issue I left join 
				tpl_asset_transfer_issue_details IA on(I.id=IA.issue_id) 
				WHERE I.cancel_issue=0 and I.site='".$site."' and IA.asset_id in('".$item."') and I.issue_date<='".$fdate."'";
		$sqlissue=DB_query($sql1,$db);	
		while($addissue=DB_fetch_array($sqlissue))
		{
			$issueqty[]=$addissue['issue_qty'];
		}			
		
		/**************************************  site to site receive *********************************************/
		
		$sql2="Select SUM(B.received_qty) as received_qty
			   from tpl_asset_transfer_receive A left join tpl_asset_transfer_receive_details B on(A.id=B.receive_id) WHERE
			   A.receive_site = '".$site."' and A.receive_date <= '".$fdate."' and B.asset_id in ('".$item."')";
		
		$sqlsite_site1=DB_query($sql2,$db);
		while($addsitetositereceive=DB_fetch_array($sqlsite_site1))
		{
			$receive[]=$addsitetositereceive['received_qty'];
		}
			
		/*******************************************  Opening ******************************************************/
		
		$sql3 ="Select opening_date as date, stock as reciveqty, rate, id as itemid from opening_stock_for_toolsplants_machine 
			   where site = '".$site."' and opening_date <= '".$function_filter[3]."' and machine_id in ('".$item."')";
		
		$sqlopen=DB_query($sql3,$db);
		while($addopen=DB_fetch_array($sqlopen))
		{
		$receive[]=$addopen['reciveqty'];	
		}
		$receive=array_sum($receive);
		$issued=array_sum($issueqty);
		$receive1=str_replace(",","",$receive);
		$issued1=str_replace(",","",$issued);
		$total=$receive1-$issued1;
		$qty=$total;	   
		
		return $qty;   
	}		
	               
	function openingBalance($function_filter,$item)
	{
		global $db;
		$site=$function_filter[0];
		$store=$function_filter[1];
		$from=$function_filter[2];
		$to=$function_filter[3];								
		if($site!='')
		$filter[]="site = '".$site."'";
		if($store!='')
		$filter[]="store = '".$store."'";
		if($from!='--' && $to!='--')
		$filter[]="opening_date between '".$from."' and '".$to."'";
		if($item!='')
		$filter[]="machine_id = '".$item."'";
		
		$filter=implode(' and ',$filter);
		$sql1="Select stock, rate from opening_stock_For_Toolsplants_machine where $filter";
	
		$result1=DB_query($sql1,$db);
		$myrow1=DB_fetch_array($result1);
		return $myrow1['stock'];
	}             

	function issueSlip($function_filter,$item)
	{
		global $db;
		$site=$function_filter[0];
		$store=$function_filter[1];
		$from=$function_filter[2];
		$to=$function_filter[3];
		if($site!='')
		$filter[]="I.site = '".$site."'";
		if($store!='')
		$filter[]="I.mst_storeid = '".$store."'";
		if($item!='')
		$filter[]="IA.asset_id = '".$item."'";
		if($from!='--')
		$filter[]="I.issue_date between '".$from."' and '".$to."'";
		
		$filter=implode(' and ',$filter);
	
		$sql1="Select SUM(IA.issue_qty) from tpl_asset_transfer_issue I left join 
		 tpl_asset_transfer_issue_details IA on(I.id=IA.issue_id) where I.cancel_issue=0 and $filter";
		
		$result1=DB_query($sql1,$db);
		$myrow1=DB_fetch_array($result1);
		
		return $myrow1['SUM(IA.issue_qty)'];
	}
	
	function receivedSiteToSite($function_filter,$item)
	{
		global $db;
		$site=$function_filter[0];
		$store=$function_filter[1];
		$from=$function_filter[2];
		$to=$function_filter[3];
		
		if($site!='')
		$filter[]="A.receive_site = '".$site."'";
		if($store!='')
		$filter[]="A.store = '".$store."'";
		if($from!='--')
		$filter[]="A.receive_date between '".$from."' and '".$to."'";
		if($item!='')
		$filter[]="B.asset_id = '".$item."'";
		
		$filter=implode(' and ',$filter);
		
		$sql1="Select SUM(B.received_qty) 
		 from tpl_asset_transfer_receive A left join tpl_asset_transfer_receive_details B
		 on(A.id=B.receive_id) where $filter";
		$result1=DB_query($sql1,$db);
		$myrow1=DB_fetch_array($result1);
		return $myrow1['SUM(B.received_qty)'];
	}	
	
	function InProcess($function_filter,$item)
	{
		global $db;
		$site=$function_filter[0];
		$store=$function_filter[1];
		$from=$function_filter[2];
		$to=$function_filter[3];
		
		if($site!='')
		$filter[]="A.receive_site = '".$site."'";
		if($store!='')
		$filter[]="A.store = '".$store."'";
		if($from!='--')
		$filter[]="A.receive_date between '".$from."' and '".$to."' ";
		if($item!='')
		$filter[]="B.asset_id = '".$item."'";
		
		$filter=implode(' and ',$filter);
		
		$sql1="Select SUM(B.received_qty), SUM(B.issue_qty) 
		 from tpl_asset_transfer_receive A left join tpl_asset_transfer_receive_details B
		 on(A.id=B.receive_id) where $filter";
		$result1=DB_query($sql1,$db);
		$myrow1=DB_fetch_array($result1);
		return ($myrow1['SUM(B.issue_qty)']-$myrow1['SUM(B.received_qty)']);
	}

   $sql1=$_SESSION['sql1'];
  $sql=DB_query($sql1,$db);
  $j=1;
 $i=0;
 $flag23=0;
  while($row1=DB_fetch_array($sql))
  {
 if($flag23==0)
 {
 	$divisor=29;
 }
      $privious_open=privious_opening($function_filter,$row1['id']); 
     	
	  $out_1=issueSlip($function_filter,$row1['id']);
	  $in_2=receivedSiteToSite($function_filter,$row1['id']);
	  $InProcess = InProcess($function_filter,$row1['id']);	 
     
     $opening_stock=number_format($privious_open,5);
	 $opening_stock;
     $received=number_format(($in_2),5);
     $issueded=number_format(($out_1),5);
 
    $invalue=($privious_open+$in_2);
    $invalue1=number_format($invalue,5);
	$invalue2=str_replace(",","",$invalue1);
    $outvalue=($out_1);
	
	$outvalue1=number_format($outvalue,5);
	$outvalue2=str_replace(",","",$outvalue1);
    
    $tot=number_format(($invalue2-$outvalue2),5); 
   $i++;
   
  ?>   

    <tr>
        <td><?php echo $j;?></td>
        <td><?php echo $row1['code'];?></td>
	    <td><?php echo $row1['name'];?></td>
	    <td><?php echo $s->getField('unitsofmeasure','unitid',$row1['unit'],'unitname');?></td>
	    <td style="text-align:right;"><?php if($opening_stock=='') { echo "0"; } else {	echo $opening_stock; }?></td>
	   <!-- <th><center><?php //if($opening_rate=='') { echo "0"; } else {  echo $opening_rate;}?></center></th>
	    <th><center><?php //if($opening_value=='') { echo "0"; } else { echo $opening_value;}?></center></th>-->
	    <td style="text-align:right;"><?php if($received=='') { echo "0"; } else {	echo $received; }?></td>
	   <!--<th><center><?php // if($inrate=='') { echo "0"; } else {  echo $inrate;}?></center></th>
	    <th><center><?php //if($inwards_qtydata['total']=='') { echo "0"; } else { echo $inwards_qtydata['total'];}?></center></th>-->
	    <td style="text-align:right;"><?php if($InProcess=='') { echo "0"; } else {	echo $InProcess; }?></td>
	   <!--<th><center><?php // if($inrate=='') { echo "0"; } else {  echo $inrate;}?></center></th>
	    <th><center><?php //if($inwards_qtydata['total']=='') { echo "0"; } else { echo $inwards_qtydata['total'];}?></center></th>-->
	    <td style="text-align:right;"><?php if($issueded=='') { echo "0"; } else {	echo $issueded; }?></td>
	  <!-- <th><center><?php //if( $outrate=='') { echo "0"; } else {  echo  $outrate;}?></center></th>
	    <th><center><?php //if($outwards_qtydata['totalout']=='') { echo "0"; } else { echo $outwards_qtydata['totalout'];}?></center></th>-->
	    <td style="text-align:right;"><?php if($tot=='') { echo "0"; } else {	echo $tot; }?></td>
	  <!-- <th><center><?php //if($closeqty['closing_rate']=='') { echo "0"; } else {  echo $closeqty['closing_rate'];}?></center></th>
	    <th><center><?php //if($closeqty['closing_value']=='') { echo "0"; } else { echo $closeqty['closing_value'];} ?></center></th>-->
  </tr>   
<?php 
if($i%$divisor==0)
{
$divisor=$divisor+34;
$flag23=$i;

?>
<p style="text-align:center; font-size:14px;"><?php echo $messeg; ?></p>
<?php echo $header;  }	
	
	$j++;
} 
?>
</table>
</div>
</div>

<script language="javascript" type="text/javascript">
  function toggle_visibility(id) {
	
	 
       var e = document.getElementById(id);
	      e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
  
</script>
</div>
</body>
</html>
