<?php 
session_name('CAKEPHP'); session_start();
error_reporting(0);
//echo "<pre>";
$_REQUEST=$_SESSION['stock_print'];
//print_r($_REQUEST);
$PageSecurity = 3;
 include ('includes/session.inc');
 include ('includes/function_lib.php');
 include('includes/fun-php.php');
 include('includes/fun-HRClass.php');

 ?>

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

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

$branch=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
$store=$s->getField('storetype','id',$_SESSION['store'],'StoreName');
$Compamies=DB_query("select * from companies",$db);
$CompamiesRow=DB_fetch_array($Compamies);
$sql1 = "SELECT * FROM companies where CompanyId='".$_SESSION['ACGCompanyId']."'";
  $sqlres = DB_query($sql1,$db);
  $companyRow = DB_fetch_array($sqlres);
  $s=new db_query(); 

?>

<!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" media="print">
  @page port {size: portrait;}
@page land {size: landscape;}
 .portrait {page: port;}
.wrapper {page: land;}

</style>

<style type="text/css"> 
@page {size: portrait; margin:10px;}

@media print{
  *{box-sizing:border-box;}
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: 30px;
  font-weight:bold;
  text-transform: uppercase;
}
h2{
  font-size: 18px; line-height: 28px;
  font-weight:normal;
}
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;
  float: left; position: relative;
}
 .div_first .left
 {
    width:16%; text-align:center; height:auto; vertical-align:middle; height:auto; padding: 1% 0; float:left;
  }
  .div_first .middle
  {
    width:68%; text-align:center; padding: 1% 0; margin:0 auto; float: left;
  }
  .div_first .right
  {
    width:16%; text-align:center; padding: 1% 0; float: left;
  }

.div_first .full{text-align: center; border: 0;}
.div_second, .div_third{
  width: 100%;
  height:auto;
  border-top: #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;}
.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:96%;}
.mt27{margin-top:4px; width:66%;}
.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 */
*{box-sizing:border-box;}
body{ margin:0px; padding:0px; background-color:#fff; 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;}

#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: 32px;
  font-weight:bold;
  text-transform: uppercase;
}
h2{
  font-size: 18px; line-height: 26px;
  font-weight:normal;
}
.div_first, .div_first .full{
  width: 100%; height: auto;
  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:16%; text-align:center; height:auto; vertical-align:middle; padding: 1% 0; 
}
.div_first .middle
{
  width:68%; text-align:center; padding: 1% 0; position: relative; margin:0 auto;
}
.div_first .right
{
  width:16%; text-align:center; padding:1% 0; 
}
.div_second, .div_third, .div_forth{
  width: 100%; height: auto;
  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:96%;}
.mt27{margin-top:4px; width:66%;}
.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']=='')?'ssmh-logo.jpeg':$CompamiesRow['CompanyLogo']; ?>
      <!-- <div class="left"><img src="Setup/CompanyLogo/<?php //echo $logoImage;  ?>" class="mt26" /></div> -->
      <div class="left"><img src="images/ssmh-logo.jpeg" class="mt26" /></div>
      <div class="middle"><h1>Shekhar Sharraf Memorial Hospital</h1>
        <div class="f14">RUSA Medical Centre (Aligarh)</div>
        <div class="f12">6Km. Milestone, Agra Road, Aligarh - 202001, U.P. (India)</div>
        <div class="f12">Email : info@rusamedicalcentre.com</div>
        <div class="full"><h2>Stock Summary Report</h2></div>
      </div>
      <div class="right"><img src="images/rusa-logo.jpeg" vspace="10" class="mt27" /></div>
  </div> 
   <?php
     if(isset($_REQUEST['view']))
       {
      
	   				
					$_SESSION['stock_print']=$_REQUEST;
					//echo "<pre>";
	 	           // print_r($_SESSION['stock_print']);
					if($_REQUEST['from_date']=='')
					{
						$_REQUEST['from_date']=$_SESSION['financialYrStart'];
						$from_date=$_REQUEST['from_date'];
						$from=$from_date;
					}else{
						 $from_date=$_REQUEST['from_date'];
						$from_date_brk=explode('/',$from_date);
						 $from=$from_date_brk[2]."-".$from_date_brk[1]."-".$from_date_brk[0];
					}
					if($_REQUEST['to_date']=='')
					{
						$_REQUEST['to_date']=$_SESSION['financialYrEnd'];
						$to_date=$_REQUEST['to_date'];
						$to=$to_date;
					}else{
						$to_date=$_REQUEST['to_date'];
						$to_date_brk=explode('/',$to_date);
						$to=$to_date_brk[2]."-".$to_date_brk[1]."-".$to_date_brk[0];	
					}					
				
					$site=$_REQUEST['site'];
					if($_REQUEST['store1']==0)
					{
					 $store12=array();
					 $store_sql = "SELECT id FROM storetype where Store_Site = '".$_REQUEST['site']."'";
					 $store_sql1=DB_query($store_sql,$db);
					 while($st_rows=DB_fetch_array($store_sql1))
					 {
					 $store12[]=$st_rows['id'];
					 }
					 $store=implode("','",$store12);
					 }
					else
					$store=$_REQUEST['store1'];
					if($_REQUEST['maincat']!='0')
					$filter2[]="CategoryID='".$_REQUEST['maincat']."'";
					if($_REQUEST['subcat']!='0')
					$filter2[]="SubCategoryID='".$_REQUEST['subcat']."'";
					
					 $item=$_REQUEST['item'];	
					 //print_r($item);				
					if($item!=''){
					$item_string=implode("','",$item);
					$filter2[]="id in('".$item_string."')";
					
					 }
					 if($filter2!='')
					 {
					
					 $filter4=implode(' and ',$filter2);
					 }
					 $function_filter[0]=$site;
					$function_filter[1]=$store;	
					$function_filter[2]=$from;
					$function_filter[3]=$to;
					$fdate=$from;
	               
 include('includes/stock_summary_function.php');						
?>

 <?php

 if($_REQUEST['store1']!='0')
  $storedata="store  ".$s->getField('storetype','id',$_REQUEST['store1'],'StoreName');
  else
  $storedata="ALL Store";
  if($_REQUEST['maincat']!='0' && $_REQUEST['store1']!=='0')
  {
  $sub=$s->getField('stockcategory','id',$_REQUEST['subcat'],'categorydescription');
  $cat=$s->getField('stockmaincategory','id',$_REQUEST['maincat'],'stockmaincategoryname');
  }
 /* if($_REQUEST['maincat']!='0')
  {
  $message="Showing Records of  ".$storedata." of Branch -- ".$branch."   
  Category -- ".$cat." -- and  SubCategory -- ".$sub." and 
  From  ".$_REQUEST['from_date']." to ".$_REQUEST['to_date']."";
  }
  elseif($from_date!="" && $to_date!="")
  {
   $message="Showing Records of   ".$storedata."  of Branch --  ".$branch."   
   From   ".$_REQUEST['from_date']." to ".$_REQUEST['to_date']."";
  }
  elseif($from_date=="" && $to_date=="")
  {
   $message="Showing Records of   ".$storedata."  of Branch --  ".$branch."   
   From   ".ConvertSQLDate($_SESSION['from_date'])." to ".ConvertSQLDate($_SESSION['to_date'])."";
  } */

   ?>
   
    <?php //for($k=0; $k<5; $k++){
   
   if($item!=''){
			$item_string=implode("','",$item);
			$filter2[]="id in('".$item_string."')";
		}
   
   
   $item2=$_REQUEST['item'];
	if($item2!=''){
		 $item_string1=implode(",",$item2);
		
	
}

 ?> 
<div class="div_second">
  <p align="center"><?php echo $message; ?></p>

  <table width="100%" border="0" cellspacing="0" cellpadding="3">
  <thead>
  <tr>
    <th width="10%" rowspan="2" style="text-align:center;" class="f12">Item Code</th>
    <th rowspan="2" style="text-align:center;" class="f12">Item Name</th>
    <th colspan="2" align="center" style="text-align:center;" class="f12">Opening Balance</th>
    <th colspan="2" align="center" style="text-align:center;" class="f12">Received</th>
    <th colspan="2" align="center" style="text-align:center;" class="f12">Issued<br/>(Branch & Patient issue)</th>
    <th colspan="4" align="center" style="text-align:center;" class="f12">Variance<br/>(Stock Adjustment)</th>
    <th colspan="2" align="center" style="text-align:center;" class="f12">Closing Balance</th>
    </tr>
  <tr>
    <th width="6%" align="center" style="text-align:center;" class="f12">Primary QTY.</th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Secondary QTY.</th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Primary QTY.</th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Secondary QTY.</th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Primary QTY.</th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Secondary QTY.</th>
    <th width="6%" colspan="2" align="center" style="text-align:center;" class="f12">Plus</th>
    <th width="6%" colspan="2" align="center" style="text-align:center;" class="f12">Minus</th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Primary QTY. </th>
    <th width="6%" align="center" style="text-align:center;" class="f12">Secondary QTY.</th>
  </tr>
  </thead>
  <tbody>
  <?php  
 $subQuery='';       
    if($item_string1!='')
     $subQuery.=" and id IN(".$item_string1.")";

     if($_REQUEST['maincat']!=0)
     	$subQuery.=" and CategoryID='".$_REQUEST['maincat']."'";
     if($_REQUEST['subcat']!=0)
     	$subQuery.=" and SubCategoryID='".$_REQUEST['subcat']."'";
  
  $sql1="select * from item_master where 1=1 ".$subQuery." order by CategoryID,SubCategoryID,item_name ASC ";
  $_SESSION['sqlquery_'.$rd]=$sql1;
     $result1=DB_query($sql1,$db);

  while($row1=DB_fetch_array($result1))
  {
        $confac=$row1['conv_factor'];
      	$op=openingBalance($function_filter,$row1['id']);
      	$pre=previous_stock($row1['id'],$function_filter);
      	$op=$op + $pre;
		$in_1=receivedMRN($function_filter,$row1['id']);
		$out_1=issueSlip($function_filter,$row1['id']);
		$out_2=issueSiteToSite($function_filter,$row1['id']);    
		$in_2=receivedSiteToSite($function_filter,$row1['id']);
		$out_3=issueMatarialReturn($function_filter,$row1['id']);
		$in_3=receivedReturn($function_filter,$row1['id']);  
		$out_4=Rgpchallan($function_filter,$row1['id']);
		$out_5=Nrgp($function_filter,$row1['id']);
		$in_4=Rgpreturn($function_filter,$row1['id']);  
		$in_5=FinishedMR($function_filter,$row1['id']);
		$in_6=MaterialConReceived($function_filter,$row1['id']);
		$out_6=MaterialConIssue($function_filter,$row1['id']);
		$in_7=PhysicalStockPlus($function_filter,$row1['id']);
		$out_7=PhysicalStockMinus($function_filter,$row1['id']);
		$out_8=dispatchIssue($function_filter,$row1['id']);
		$out_9=jobWorkChallan57Issue($function_filter,$row1['id']);
		$in_8=jobWorkChallan57Received($function_filter,$row1['id']);
	    $out_10=invoice($function_filter,$row1['id']);
	     $out_11=drugissue($function_filter,$row1['id']);
	    $in_9 = direct_Purchase_billbook($function_filter,$row1['id']);
	    $in_10 = receivedSRN($function_filter,$row1['id']);

	  	$in_11 = saleReturn($function_filter,$row1['id']);
      $in_12=storereceipt($function_filter,$row1['id']);
	   
		$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_7 + $in_8 + $in_9 + $in_10 +$in_11+$in_12);
		
		$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5 + $out_6 + $out_7 + $out_8 + $out_9 + $out_10+$out_11);
		$tot=$invalue-$outvalue;
		$stockallitems=$tot;
      		$opening_stock=$op;
       $con_factor=((float)$confac==0)?1:$confac;
      

      $opening_stock_sec= ($op * $con_factor);

      $received_qty=($in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_8 + $in_9+$in_10 + $in_11);

       $received_qty_sec=($received_qty * $con_factor);
	   $issueded_qty=($out_1+$out_2+$out_3+$out_4+$out_5 + $out_6 + $out_8 + $out_9 + $out_10);

	   $issueded_qty_sec=($issueded_qty * $con_factor);
	 
       $_SESSION['opening_'.$rd]=$opening_stock;
  
    if((float)$stockallitems!=0)
    {
      if($row1['SubCategoryID']!=$subC_id)
      {
      	$subC_id=$row1['SubCategoryID'];
   ?>
  <tr>
    <td colspan="16" class="f12"><b><?php echo $s->GetFieldValue('stockmaincategory','id="'.$row1['CategoryID'].'"','stockmaincategoryname');?>&nbsp;<img src="images/orange-right-arrow-button.png" height="20" width="20" />&nbsp;<?php echo $s->GetFieldValue('stockcategory','id="'.$row1['SubCategoryID'].'"','categorydescription')?></b></td>
    </tr>
    <?php }  ?>
  <tr class="f11">
   <td><?php echo $row1['StockID']; ?></td>
    <td><?php echo $row1['item_name']; ?></td>  
    <td style="text-align:right"><?php if($opening_stock=='') { echo "0"; } else { echo number_format($opening_stock,2); }?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['mst_unitid'],'unitname'); ?></td>
    <td style="text-align:right"><?php if($opening_stock_sec=='') { echo "0"; } else { echo number_format($opening_stock_sec,2);}?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['conv_unit'],'unitname'); ?></td>
    <td style="text-align:right"><?php if($received_qty=='') { echo "0"; } else {  echo number_format($received_qty,2); }?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['mst_unitid'],'unitname'); ?></td>
    <td style="text-align:right"><?php if($received_qty_sec=='') { echo "0"; } else { echo number_format($received_qty_sec,2);}?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['conv_unit'],'unitname'); ?></td>
    <td style="text-align:right"><?php if($issueded_qty=='') { echo "0"; } else {  echo number_format($issueded_qty,2); }?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['mst_unitid'],'unitname'); ?></td>
    <td style="text-align:right"><?php if($issueded_qty_sec=='') { echo "0"; } else { echo number_format($issueded_qty_sec,2);}?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['conv_unit'],'unitname'); ?></td>
    <td><?php echo number_format((float)$in_7,2); ?></td>
    <td><?php echo number_format(($in_7 * $con_factor),2) ?></td>
    <td><?php echo number_format((float)$out_7,2); ?></td>
    <td><?php echo number_format(($out_7 * $con_factor),2) ?></td>
    <td style="text-align:right"><?php echo number_format($tot,2); ?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['mst_unitid'],'unitname'); ?></td>
    <td style="text-align:right"><?php  echo number_format(($tot * $con_factor),2); ?>&nbsp;&nbsp;<?php echo $s->getField('unitsofmeasure','unitid',$row1['conv_unit'],'unitname'); ?></td>
  </tr>  
      <?php 

}
}
 
	 ?>
	 </tbody>
    </table>
  <?php 

       } ?>

</div>

</div>
</body>
</html>
