<?php 
require("../connection.php");
include("../user.php");
$user_id=$_SESSION['user'];
$username=$_SESSION['user_name'];
$branch_id=$_SESSION['branch_id'];
$ho_access=$_SESSION['ha'];
$brch=$_REQUEST['brch'];
//echo $catgryid;
?>
<?php 
    $b=mysqli_query($con,"select id,branch_id,user_id  from branch_mapping where user_id='$user_id'");
    $bch=mysqli_fetch_array($b);
    $brnh=$bch['branch_id'];
    $c=mysqli_query($con,"select id,location  from location_master where id='$brnh'");
    $bh=mysqli_fetch_array($c);
    $br=$bh['location'];
?>
<?php  
    $cat_name=mysqli_query($con,"select id,location  from location_master where id='$brch'");
    $catname=mysqli_fetch_array($cat_name);
?>
    <h4><center>BRANCH-<b style="color:blue;text-transform:uppercase;"><?php echo  $catname['location']; ?></b></center></h4>
    <div>
        <input type="submit" name="myButtonControlID" onclick="Export()" class="btn btn-primary" value="EXCEL">
        <button id='btn' type="button" style="margin-left:10px;" class="btn btn-primary" onclick='printDiv();'><i class="fa fa-print">-Print</i></button>
    </div>
    <div id="DivIdToPrint">
        <table id="tblData" class="table table-striped table-bordered" >
            <thead>
                <tr>
                    <th>S.No</th>
                    <th>PO-DATE</th>
                    <th>MATERIAL NAME</th>
                    <!--<th>DIVISION</th>-->
                    <!--<th>LOCATION</th>-->
                    <!--<th>DEPARTMENT</th>-->
                    <!--<th>CATEGORY</th>-->
                    <!--<th>CATEGORY-DESCRIPTION</th>-->
                    <th>QUANTITY</th>
                    <th>VALUE</th>
                    <th>AMOUNT</th>
                    <th>DEPRICATION PERCENTAGE</th>
                    <th>DEPRICATION VALUE</th>
    				<th>DEPRECIATION ORIGINAL AMOUNT</th>
                    <th>EXPIRY DATE</th>
                    <th>FA-SERIAL NO</th>
                    <th>REMARKS</th>
                </tr>
            </thead>
            <tbody>
		    <?php 
	            $brc=$catname['location']; 
	            if($ho_access=="yes")
	            {
		            $stck=mysqli_query($con,"select * from item_master where branch='$brc'");
		          //  $stck=mysqli_query($con,"select * from item_master");
                	$s=1;
            		while($type=mysqli_fetch_array($stck))
    				{
    				    $mat_id = $type['material_name'];
			    $mat_query = mysqli_query($con, "SELECT category_type FROM category_master WHERE id = $mat_id");
			 $mat_row = mysqli_fetch_array($mat_query);
    		?>
                <tr>
                    <td><?PHP echo $s++;?></td>
                    <td><?php echo $type['po_date'];?></td>
                    <td><?php echo $mat_row['category_type'];?></td>
                    <!--<td><?php echo $type['division'];?></td>-->
                    <!--<td><?php echo $type['location'];?></td>-->
                    <!--<td><?php echo $type['department'];?></td>-->
                    <!--<td><?php echo $type['category'];?></td>-->
                    <!--<td><?php echo $type['category_description'];?></td>-->
                    <td><?php echo $type['quantity'];?></td>
                    <td><?php echo $type['value'];?></td>
                    <td><?php echo $type['amount'];?></td>
                    <td><?php echo $type['deprication_percentage'];?></td>
                    <td><?php echo $type['deprication_value'];?></td>
        			<td><?php echo $type['amount']-$type['deprication_value'] ;?></td>
                    <td><?php echo $type['expiry_date'];?></td>
                    <td><?php echo $type['fa_serial_no'];?></td>
                    <td><?php echo $type['remarks'];?></td>
                </tr>
			<?php
			}
        	}
        	else
        	{
    		  //  $stck=mysqli_query($con,"select * from item_master where location='$brc' and location='$br'");
      		    $stck=mysqli_query($con,"select * from item_master");
	            $s=1;
		        while($type=mysqli_fetch_array($stck))
				{
		    ?>
                <tr>
                    <td><?PHP echo $s++;?></td>
                    <td><?php echo $type['po_date'];?></td>
                    <!--<td><?php echo $type['division'];?></td>-->
                    <!--<td><?php echo $type['location'];?></td>-->
                    <!--<td><?php echo $type['department'];?></td>-->
                    <!--<td><?php echo $type['category'];?></td>-->
                    <!--<td><?php echo $type['category_description'];?></td>-->
                    <td><?php echo $type['quantity'];?></td>
                    <td><?php echo $type['value'];?></td>
                    <td><?php echo $type['amount'];?></td>
                    <td><?php echo $type['deprication_percentage'];?></td>
                    <td><?php echo $type['deprication_value'];?></td>
        			<td><?php echo $type['amount']-$type['deprication_value'] ;?></td>
                    <td><?php echo $type['expiry_date'];?></td>
                    <td><?php echo $type['fa_serial_no'];?></td>
                    <td><?php echo $type['remarks'];?></td>
                </tr>
			<?php
			}
	        }
	        ?>
		    </tbody>
        </table>
    </div>
    <script src="../js/jquery.table2excel.min.js" type="text/javascript"></script>
    <script>
        function Export() {
            $("#tblData").table2excel({
                filename: "Table.xls"
            });
        }
    </script>
    <script>
        function printDiv() 
        {
          var divToPrint=document.getElementById('DivIdToPrint');
          var newWin=window.open('','Print-Window');
          newWin.document.open();
          newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
          newWin.document.close();
          setTimeout(function(){newWin.close();},10);
        }
    </script>
    <script>
        $(document).ready(function() {
            $("#tblData").DataTable().destroy();
    	
            var tableData = $('#tblData').DataTable();
            var tablekeywords = [];
            tableData.column(2).data().unique().each(function (d) {
                tablekeywords.push(d);
            });
        
            // Add autocomplete to the search box of the table
            $('#tblData_filter input').autocomplete({
                source: tablekeywords,
                select: function (event, ui) {
                    $(this).val(ui.item.value);
                    tableData.search(ui.item.value).draw();
                    return false;
                }
            });
        });
    </script>