<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

		
	
		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        


<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}
.style2 {
	color: #00CC00;
	font-style: italic;
}
.style3 {color: #00CC00}


-->
</style>
</head>
<script type="text/javascript">
 
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

</script>
<script type="text/javascript"> 
 		jQuery(document).ready(function(){
			$('#item_name').autocomplete({source:'../ProgramFile/auto/suggest_POitem.php', minLength:1,
			select: function (event, ui) {
        	$("#item_id").val(ui.item.id); // display the selected id
       		$("#item_name").val(ui.item.value); // display the selected text.			
		    }
			});

}); 
</script>
<script type="text/javascript">
$(function(){
    $('button').click(function(){
        var url='data:application/vnd.ms-excel,' + encodeURIComponent($('#tableWrap').html()) 
        location.href=url
        return false
    })
})
</script>

<body>

<div class="span11" style=" width:83%;">



<div class="12" style=" margin-left:0px; ">



<br />
<br />
<br />

<div class="12" style=" margin-left:0px;">

<div class="panel" style="border:<?php echo THEMECOLOR?> solid 1px; border-radius:0px;">
<div class="entry-head" >
<h4>View Stock Report</h4>
</div>
<p>&nbsp;</p>
<br />
<form name="form1" id="form1" method="post">
  <table width="100%" border="0" align="center">
    <tr>
      <td width="35%" height="49"><div align="right"><strong>Item Name </strong></div></td>
      <td width="2%"><strong>:</strong></td>
      <td width="22%"><input type="text" name="item_name" id="item_name" value="<?=$_POST["item_name"]?>" /><input type="hidden" name="item_id" id="item_id" value="<?=$_POST["item_id"]?>"/></td>
      <td width="41%"><input type="submit" name="Search" value="Search" class="btn btn-info" /></td>
      </tr>
  </table>
</form>
<br />


   <div id="tableWrap">
<div id="printableArea">  
           
		
            <?php 
			$whr='';
			if(isset($_POST["Search"]))
			{
			$item_id=$_POST["item_id"];
			if($item_id<>'')
			{
			$whr=" where item_id='$item_id'";
			}					
			}
			
				$sql_viewtmp="select item_id,item_name,unit_id,unit_name,sell_price,pur_price as unit_price,sum(pur_qty) as pur_qty  from view_purchase_det $whr group by item_id,item_name,unit_id,unit_name,sell_price order by item_name";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=0;
					$sums=0;		
					$total_stock =0;
					$grand_total =0;
					?>
                     <div class="panel">
					<div class="panel-heading">Stock View
					  </div>
					<p>&nbsp;</p>
					 <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
            <th nowrap="nowrap" width="7%">Si No</th>
            <th nowrap="nowrap" width="40%">Item Name</th>
           
            <th nowrap="nowrap" width="20%">Unit Name </th>
            <th width="20%" nowrap="nowrap"> Qty</th>
            <th width="20%" nowrap="nowrap">Purchase Price </th>
            <th nowrap="nowrap" width="20%">Selling  Price </th>
			 <th nowrap="nowrap" width="20%">Total </th>
            </tr>
            <?php
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
					$item_id=$rows["item_id"];
					$unit_id=$rows["unit_id"];
					$sell_price=$rows["sell_price"];
					$pur_qty=$rows["pur_qty"];
					
					$sqlret="select sum(ret_qty) as purret_qty from view_purchasereturn_det where item_id='$item_id' and unit_id='$unit_id' and sell_price='$sell_price'";
					$exeret=mysql_query($sqlret);
					$rowsret=mysql_fetch_assoc($exeret);
					$purret_qty=$rowsret["purret_qty"];
					
					$sqlsel="select sum(qty) as sale_qty from view_sales_mstrdet where item_id='$item_id' and unit_id='$unit_id' and unit_price='$sell_price' and cancel_status=0";
					$exesel=mysql_query($sqlsel);
					$rowssel=mysql_fetch_assoc($exesel);
					$sale_qty=$rowssel["sale_qty"];
					
					$sqlselret="select sum(ret_qty) as saleret_qty from view_saleret_masdet where item_id='$item_id' and unit_id='$unit_id' and sell_price='$sell_price'";
					$exeselret=mysql_query($sqlselret);
					$rowsselret=mysql_fetch_assoc($exeselret);
					$saleret_qty=$rowsselret["saleret_qty"];
					
					$sqldem="select sum(dem_qty) as dem_qty from tbl_itemdamage_details	 where item_id='$item_id' and unit_id='$unit_id' and sell_price='$sell_price'";
					$exedem=mysql_query($sqldem);
					$rowsdem=mysql_fetch_assoc($exedem);
					$dem_qty=$rowsdem["dem_qty"];
					
					
						$stock=($pur_qty+$saleret_qty)-($purret_qty+$sale_qty+$dem_qty);
						$total_stock+= $stock;
						if($stock>0)
						{	
							
			?>
           
            <tr height="32">
            <td nowrap="nowrap" width="7%"><?php echo ++$i;?></td>
           
		    <td nowrap="nowrap" width="40%"><?php echo $rows["item_name"];?></td>
            
            <td nowrap="nowrap" width="20%"><?php echo $rows["unit_name"];?></td>
            <td width="20%" nowrap="nowrap"><?php echo $stock;?></td>
            <td width="20%" nowrap="nowrap"><?php echo $rows["unit_price"];?></td>
            <td nowrap="nowrap" width="20%"><?php echo $rows["sell_price"];?></td>
			 <td nowrap="nowrap" width="20%"><?php echo $total_amount = $stock*$rows["unit_price"]; ?></td>
            </tr> 
			
			
			  <?php
			  $grand_total +=$total_amount;
			   }
			}
			
			?>
			<tr>
			<td colspan="4" align="right"><span><b>Total Quantity:</b></span> <?php echo $total_stock; ?></td>
			<td colspan="4" align="right"><span><b>Total amount:</b></span> <?php echo $grand_total; ?></td>
			</tr>
           </table>
		   </div>
    <?php }
	if($i==0)
	{
	echo '<span style="color:#FF0000">No Data Found</span>';
	}
	?>
	

		<br />
        


</div>
</div>
<br />
<div align="center"><span class="btn btn-info" onClick="printDiv('printableArea')">Print</span> &nbsp;&nbsp;<button class="btn btn-success">Export to Excel</button></div>
</div>

</div>

</body>
</html>
