<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>

		 <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>



<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>
<!-- Page wrapper  -->
        <div class="page-wrapper">
            <!-- Bread crumb -->
            <div class="row page-titles">
                <div class="col-md-5 align-self-center">
                    <h3 class="text-primary"></h3> </div>
                <div class="col-md-7 align-self-center">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                        <li class="breadcrumb-item active">Stock Report</li>
                    </ol>
                </div>
            </div>
            <!-- End Bread crumb -->
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card" id="export_table">
                            <div class="card-body" id="print">
							
							<h3>Stock Report</h3>
    
<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">



   
           
		
            <?php 
			$whr='';
			if(isset($_POST["Search"]))
			{
			$item_id=$_POST["item_id"];
			if($item_id<>'')
			{
			$whr=" where item_id='$item_id' and session_login_id = '".$_SESSION['login_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 where  session_login_id = '".$_SESSION['login_id']."' 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 id="tableWrap">
					<div id="printableArea">

                     <div class="panel">
					
					<p>&nbsp;</p>
					  
					 <table width="100%" class="table table-bordered" id="export">

                     <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 session_login_id = '".$_SESSION['login_id']."' and 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 session_login_id = '".$_SESSION['login_id']."' and 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 session_login_id = '".$_SESSION['login_id']."' and 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 session_login_id = '".$_SESSION['login_id']."' and 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">
             <input type="hidden" name="rahul" id="rahul" value="<?='Total Stock of '.$total_stock?>" /> 
            <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>
			
			
			</tr>

           </table>
		   </div>
    <?php }
	if($i==0)
	{
	echo '<span style="color:#FF0000">No Data Found</span>';
	}
	?>
	

		<br />
        





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

<br />

                        </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
          </div>
<?php include 'footer.php' ?>
 <script src="../js/lib/datatables/datatables.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
    <script src="../js/lib/datatables/cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>
    <script src="../js/lib/datatables/datatables-init.js"></script>
	<script>
	$(document).ready(function() {
  $("#btnExport").click(function(e) {
    e.preventDefault();

    //getting data from our table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('export_table');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');

    var a = document.createElement('a');
    a.href = data_type + ', ' + table_html;
    a.download = document.getElementById('rahul').value + '.xls';
    a.click();
  });
});
</script>