
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

session_start();
date_default_timezone_set('Asia/Colombo');
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">


    <title><?php echo $_GET['type']?> REPORT</title>

    <style>
        .center {
            text-align: center;
        }

        .right {
            text-align: right;
        }

        .tb  {
           width: 1250px;
           border: 1px solid black;


           border-collapse: collapse;
       }
        
       .tb td  {

           border-top: 1px solid black;
           border-left: 1px solid black;
           font-size:15px;

           border-collapse: collapse;
       }
       .tb th  {

           border-top: 1px solid black;
           border-left: 1px solid black;


           border-collapse: collapse;
       }
       .tb2  {
           
           border: 1px solid black;


           border-collapse: collapse;
       }
       .tb2 td  {

           border-top: 1px solid black;
           border-left: 1px solid black;
           font-size:18px;

           border-collapse: collapse;
       }
       .tb2 th  {

           border-top: 1px solid black;
           border-left: 1px solid black;


           border-collapse: collapse;
       }

       .tb1 {

           border: 1px solid #000000;
       }
 

   </style>

</head>
<?php
require_once ("connection_sql.php");

 

$sql_invpara = "SELECT * from invpara";
$result_invpara = $conn->query($sql_invpara);
$row_invpara = $result_invpara->fetch(); 



?>
<body>
    <center id="itemdetails">
    <table width="1250px;" cellspacing="0" border="0">

        <tr>
            <th class="center"  colspan=5 ><?php echo $row_invpara['COMPANY']; ?></th>
        </tr> 
        
        <tr> 
            <th class='center' colspan='5'> <?php echo $_GET['type']?> REPORT - (<?php echo date('Y-m-d H:i:s');?>)</th> 
        </tr>
         
        <tr> 
        <?php 
         if($_GET['brandname'] !=""){?>
            <th class='center' colspan='5'>Brand Wise - <?php echo $_GET['brandname']?>  </th> 
           <?php } ?>
        </tr> 
        <tr> 
        <?php 
         if($_GET['dep'] !=""){?>
            <th class='center' colspan='5'>Department Wise - (<?php echo $_GET['dep']?> ) </th> 
           <?php } ?>
        </tr> 
        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td align="right">&nbsp;</td>
        </tr>
        
        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        
        <tr>

            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
       
    </table>
<!--==================================================================================-->
<?php if($_GET['type'] =="ALL"){?> 
      <table class="tb" width="1250px;" >
                <tr >
        
                    <th width="30px;">NO</th> 
                    <th width="100px;">STK NO</th>
                    <th width="350px;">DESCRIPTION</th>
                    <th width="150px;">PART NO</th>
                    <th width="120px;">GEN NO</th>  
                    <th width="100px;">OFFICE QTY</th> 
                    <th width="100px;">AAS QTY</th> 
                    <th width="100px;">PVT LTD QTY</th> 
                    <th width="100px;">KADUWELA QTY</th> 
                    <th width="100px;">TOTAL QTY</th>  
                    <th width="100px;">P ORD QTY</th>  
                    <th width="100px;">COST</th>
                    <th width="100px;">SELLING</th>
                    <th width="200px;">STOCK VALUE</th>
                    
                </tr>
                <?php
                
                
                $i=0;  
               $BRAND_NAME="";
                $BAL=0;
                $sql1 = "Select * from s_mas where STK_NO <> ''";
                
                // if (isset($_GET['itemname'])) {
                //     if ($_GET['itemname'] != "") {
                //         $sql1 .= " and DESCRIPT like  '%$letters%'";
                //     }
                // }
                 
                
                
                if($_GET['brandname'] !=""){
                     $sql1.=" and BRAND_NAME='".$_GET['brandname']."'"; 
                }
                // if($_GET['cat'] !=""){
                //      $sql1.=" and Cat1='".$_GET['cat']."'"; 
                // }
                if($_GET['stk_no'] !=""){
                     $sql1.=" and STK_NO='".$_GET['stk_no']."'"; 
                } 
                if($_GET['check'] =="on"){
                     $sql1.=" and QTYINHAND >0"; 
                }
                
                
                $sql1.=" order by  BRAND_NAME ";
                
                $QTYINHAND_TOT=0;
                $QTYINHAND=0;
                foreach ($conn->query($sql1) as $row1) {
                        
                    
                    ?>
                    <tr style="font-size:11px;">
         
        
                        
                        
                       <?php  
                      
                         $sql_STO1 = "SELECT sum(QTYINHAND) AS QTYINHAND from s_submas where STO_CODE='01' AND STK_NO='".$row1['STK_NO']."'";
                        $result_STO1 = $conn->query($sql_STO1);
                        $row_STO1 = $result_STO1->fetch(); 
                        
                        $sql_STO2 = "SELECT sum(QTYINHAND) AS QTYINHAND from s_submas where STO_CODE='02' AND STK_NO='".$row1['STK_NO']."'";
                        $result_STO2 = $conn->query($sql_STO2);
                        $row_STO2 = $result_STO2->fetch(); 
                        
                        $sql_STO3 = "SELECT sum(QTYINHAND) AS QTYINHAND from s_submas where STO_CODE='03' AND STK_NO='".$row1['STK_NO']."'";
                        $result_STO3 = $conn->query($sql_STO3);
                        $row_STO3 = $result_STO3->fetch(); 
                        
                        $sql_STO4 = "SELECT sum(QTYINHAND) AS QTYINHAND from s_submas where STO_CODE='04' AND STK_NO='".$row1['STK_NO']."'";
                        $result_STO4 = $conn->query($sql_STO4);
                        $row_STO4 = $result_STO4->fetch(); 
                        
                        
                        $sql_p=" SELECT sum(ORD_QTY) as ORD_QTY FROM view_ordtrn_smas where cancel='0' and flgLocal='0'  and STK_NO='" . $row1['STK_NO']."'";
                        $result_p = $conn->query($sql_p);
                        $row_p = $result_p->fetch();
                        
                        
                        if($BRAND_NAME!=$row1['BRAND_NAME']){
                           if($i !=0){
                                echo "<tr>
                                    <td colspan='10'><b>   </b></td>
                                    <td colspan='1' align=right><b>  ".number_format($QTYINHAND, 2, ".", ",") ."   </b></td>  
                                     <td colspan='1'><b>   </b></td>
                                            <td colspan='1' align=right><b>  ". number_format($VALUE, 2, ".", ",") ."   </b></td> 
                                      </tr>";
                                
                           } 
                            $i = 1;
                   
                            echo "<tr> <td colspan='14'><b>  ". $row1['BRAND_NAME'] ."   </b></td> </tr>";
                            
                            $COST=$row1['COST'];
                            $QTYINHAND=$row1['QTYINHAND'];
                            $VALUE=($row1['COST']*$row1['QTYINHAND']);
                             
                        }else{
                            $COST=$COST+$row1['COST'];
                            $QTYINHAND=$QTYINHAND+$row1['QTYINHAND'];
                            $VALUE=$VALUE+($row1['COST']*$row1['QTYINHAND']);
                        }
                        
                        ?>
                        <td class="center"><?php echo $i ?></td>
                        <td class="left" ><?php echo $row1['STK_NO']; ?> </td>
                        <td class="left" ><?php echo $row1['DESCRIPT']; ?> </td> 
                        <td class="left" ><?php echo $row1['PART_NO']; ?> </td> 
                        <td class="left" ><?php echo $row1['GRN_NO']; ?> </td>  
                        <td align="right"><?php echo number_format($row_STO1['QTYINHAND'], 2, ".", ","); ?></td>  
                        <td align="right"><?php echo number_format($row_STO2['QTYINHAND'], 2, ".", ","); ?></td>  
                        <td align="right"><?php echo number_format($row_STO3['QTYINHAND'], 2, ".", ","); ?></td>  
                        <td align="right"><?php echo number_format($row_STO4['QTYINHAND'], 2, ".", ","); ?></td>  
                        <td align="right"><?php echo number_format($row1['QTYINHAND'], 2, ".", ","); ?></td>  
                        <td align="right"><?php echo number_format($row_p['ORD_QTY'], 2, ".", ","); ?></td>  
                            <td align="right"><?php echo number_format($row1['COST'], 2, ".", ","); ?></td> 
                            <td align="right"><?php echo number_format($row1['SELLING'], 2, ".", ","); ?></td> 
                            <td align="right"><?php echo number_format($row1['COST']*$row1['QTYINHAND'], 2, ".", ","); ?></td> 
                       
                        
                    </tr>
                    <?php
                    $i=$i+1;
                    $BRAND_NAME=$row1['BRAND_NAME']; 
                     
                    $QTYINHAND_TOT=$QTYINHAND_TOT+$row1['QTYINHAND'];
                    $VALUE_TOT=$VALUE_TOT+($row1['COST']*$row1['QTYINHAND']);
                             
                }
                ?>
           
           
            <tr>
                <td  align="right" style='font-size:20px;' colspan='9'><b></b></td> 
                <td align="right" style='font-size:20px;'><b><?php echo number_format($QTYINHAND, 2, ".", ","); ?><b/></td>  
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1' align=right><b> <?php echo  number_format($VALUE, 2, ".", ",") ; ?><b/></td>
                
                
            </tr>
            <tr>
                <td  align="right" style='font-size:20px;' colspan='9'><b></b></td> 
                <td align="right" style='font-size:20px;'><b><?php echo number_format($QTYINHAND_TOT, 2, ".", ","); ?><b/></td>  
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1' align=right><b> <?php echo  number_format($VALUE_TOT, 2, ".", ",") ; ?><b/></td> 
            </tr>
        </table>
        
   
<?php } ?>
    

 <?php if($_GET['type'] =="DEPARTMENT_WISE"){?> 
   
                <table class="tb" width="1250px;" >
                <tr >
        
                    <th width="30px;">NO</th> 
                    <th width="100px;">STK NO</th>
                    <th width="350px;">DESCRIPTION</th>
                    <th width="150px;">PART NO</th>
                    <th width="120px;">CATEGORY</th>
                    <th width="120px;">DEPARTMENT</th> 
                    <th width="100px;">QTY</th> 
                    <?php if($_GET['type'] =="SELLING"){?> 
                        <th width="100px;">SELLING</th>
                    <?php    }else{?>
                        <th width="100px;">SELLING</th>
                        <th width="100px;">COST</th>
                        <th width="200px;">STOCK VALUE</th>
                    <?php } ?>
                </tr>
                <?php
                
                
                $i=0;  
               $BRAND_NAME="";
                $BAL=0;
                $sql1 = "Select * from viewsubmas where STK_NO <> ''";
                
                // if (isset($_GET['itemname'])) {
                //     if ($_GET['itemname'] != "") {
                //         $sql1 .= " and DESCRIPT like  '%$letters%'";
                //     }
                // }
                 
                
                
                if($_GET['dep'] !=""){
                     $sql1.=" and STO_CODE='".$_GET['dep']."'"; 
                }
                if($_GET['brandname'] !=""){
                     $sql1.=" and BRAND_NAME='".$_GET['brandname']."'"; 
                }
                if($_GET['cat'] !=""){
                     $sql1.=" and Cat1='".$_GET['cat']."'"; 
                }
                if($_GET['stk_no'] !=""){
                     $sql1.=" and STK_NO='".$_GET['stk_no']."'"; 
                } 
                if($_GET['check'] =="on"){
                     $sql1.=" and QTYINHAND >0"; 
                }
                
                
                $sql1.=" order by  BRAND_NAME,STK_NO ";
                
                
                
                foreach ($conn->query($sql1) as $row1) {
                     
                    ?>
                    <tr style="font-size:11px;">
         
        
                        
                        
                       <?php  
                      
                        
                        if($BRAND_NAME!=$row1['BRAND_NAME']){
                           if($i !=0){
                                echo "<tr>
                                    <td colspan='6'><b>   </b></td>
                                    <td colspan='1' align=right><b>  ".number_format($QTYINHAND, 2, ".", ",") ."   </b></td>  ";
                                    if($_GET['type'] !="SELLING"){  
                                        echo "<td colspan='1'><b>   </b></td>
                                            <td colspan='1' align=right><b>  ". number_format($VALUE, 2, ".", ",") ."   </b></td> 
                                            <td colspan='1'><b>   </b></td>";
                                    } else{
                                        echo "<td colspan='1'><b>   </b></td>";
                                    }
                                   
                               echo " </tr>";
                                
                           } 
                            $i = 1;
                            if($_GET['type'] =="SELLING"){
                                    echo "<tr> <td colspan='8'><b>  ". $row1['BRAND_NAME'] ."   </b></td> </tr>";
                            }else{
                                echo "<tr> <td colspan='10'><b>  ". $row1['BRAND_NAME'] ."   </b></td> </tr>";
                            }
                            $COST=$row1['COST'];
                            $QTYINHAND=$row1['QTYINHAND'];
                            $VALUE=($row1['COST']*$row1['QTYINHAND']);
                             
                        }else{
                            $COST=$COST+$row1['COST'];
                            $QTYINHAND=$QTYINHAND+$row1['QTYINHAND'];
                            $VALUE=$VALUE+($row1['COST']*$row1['QTYINHAND']);
                        }
                        
                        ?>
                        <td class="center"><?php echo $i ?></td>
                        <td class="left" ><?php echo $row1['STK_NO']; ?> </td>
                        <td class="left" ><?php echo $row1['DESCRIPT1']; ?> </td> 
                        <td class="left" ><?php echo $row1['PART_NO']; ?> </td> 
                        <td class="left" ><?php echo $row1['Cat1']; ?> </td> 
                        <td class="left" ><?php echo $row1['STONAME']; ?> </td> 
                        <td align="right"><?php echo number_format($row1['QTYINHAND'], 2, ".", ","); ?></td> 
                        <?php if($_GET['type'] =="SELLING"){ ?> 
                            <td align="right"><?php echo number_format($row1['SELLING'], 2, ".", ","); ?></td>  
                         <?php }else{ ?>
                            <td align="right"><?php echo number_format($row1['SELLING'], 2, ".", ","); ?></td> 
                            <td align="right"><?php echo number_format($row1['COST'], 2, ".", ","); ?></td> 
                            <td align="right"><?php echo number_format($row1['COST']*$row1['QTYINHAND'], 2, ".", ","); ?></td> 
                        <?php } ?>
                        
                    </tr>
                    <?php
                    $i=$i+1;
                    $BRAND_NAME=$row1['BRAND_NAME']; 
                     
                    $QTYINHAND_TOT=$QTYINHAND_TOT+$row1['QTYINHAND'];
                    $VALUE_TOT=$VALUE_TOT+($row1['COST']*$row1['QTYINHAND']);
                             
                }
                ?>
           
           
            <tr>
                <td  align="right" style='font-size:20px;' colspan='6'><b></b></td> 
                <td align="right" style='font-size:20px;'><b><?php echo number_format($QTYINHAND, 2, ".", ","); ?><b/></td> 
                <?php if($_GET['type'] !="SELLING"){  ?>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1' align=right><b> <?php echo  number_format($VALUE, 2, ".", ",") ; ?><b/></td>
                <?php } else {?>
                           <td colspan='1'><b>   </b></td>
                <?php } ?>
                
            </tr>
            <tr>
                <td  align="right" style='font-size:20px;' colspan='6'><b></b></td> 
                <td align="right" style='font-size:20px;'><b><?php echo number_format($QTYINHAND_TOT, 2, ".", ","); ?><b/></td> 
                <?php if($_GET['type'] !="SELLING"){  ?>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1'><b>   </b></td>
                        <td colspan='1' align=right><b> <?php echo  number_format($VALUE_TOT, 2, ".", ",") ; ?><b/></td>
                <?php } else {?>
                           <td colspan='1'><b>   </b></td>
                <?php } ?>
            </tr>
        </table>
    
 
  <?php } ?>
  
  
   
    
    <!--===================================================================================-->
    
    
    
 
<!-- ************************************************************************** -->
</center>
    <a  onclick="tableToExcel('itemdetails', 'Report')" class="btn btn-primary btn-sm "> <span class="fa fa-file-excel-o"></span> &nbsp; Export to Excel  	</a> 
    <a class="btn btn-primary btn-sm ">	 <span class="fa fa-file-excel-o"></span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  </a> 
    <a onclick="printArea('printThis');" class="btn btn-warning  btn-sm  "> 	<span class="fa fa-print"></span> &nbsp; Print 	</a> 	 
</body>

</html>
<script type="text/javascript">

function printArea(areaName)
    {
        var printContents = document.getElementById('itemdetails').innerHTML;
        
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = originalContents;

        window.print();

        
    } 
    
    
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
</script>



