<!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=utf-8" />
        <title>AR Report</title>

        <style>
            table
            {
                border-collapse:collapse;
            }
            table, td, th
            {

                font-family:Arial, Helvetica, sans-serif;
                padding:5px;
            }
            th
            {
                font-weight:bold;
                font-size:14px;

            }
            td
            {
                font-size:13px;

            }
        </style>
        <style type="text/css">
            <!--
            .style1 {
                color: #0000FF;
                font-weight: bold;
                font-size: 24px;
            }
            -->
        </style>
    </head>

    <body> 

        <?php
        require_once("connectioni.php");
        
         date_default_timezone_set('Asia/Colombo'); 
session_start(); 









        $sql_head = "select * from invpara where flag='".$_SESSION['company']."'";   
        $result_head = mysqli_query($GLOBALS['dbinv'],$sql_head);
        $row_head = mysqli_fetch_array($result_head);

        echo "<center><span class=\"style1\">" . $row_head["COMPANY"] . "</span></center><br>";

        $heading = "<center>AR Report From  " . $_GET["DT1"] . " To ". $_GET["DT2"] . " <br><br>Brand - ".$_GET["cmbbrand"] ."";
        echo $heading;

         if($_GET["cmbcat"] == "Pending") {
           $sql = "SELECT * from s_trn where SDATE>='" . $_GET["DT1"] . "' AND SDATE<='" . $_GET["DT2"] . "' and LEDINDI='ARN'     ";
               $sql .=" and seri_no !='0'";
               $sql .="  group by   REFNO ";
         
      // echo $sql;
        
        $result = mysqli_query($GLOBALS['dbinv'],$sql) ; 
        echo "<table border='1' width=1000 ><tr> <td><b>DATE</td> <td><b>AR NO</td><td><b>Supplier</td><td><b>Brand</td><td><b>LC NO</td>  </tr>";
        while ($row = mysqli_fetch_array($result)) {   
            $sql_head1 = "select * from s_purmas where REFNO='".$row['REFNO']."'";
            $result_head1 = mysqli_query($GLOBALS['dbinv'],$sql_head1);
            $row_head1 = mysqli_fetch_array($result_head1);
        
            echo "<tr> <td>" . $row['SDATE'] . "</td>  <td>" . $row['indate'] . "</td> <td>" . $row['REFNO'] . "</td><td>" . $row_head1['SUP_NAME'] . "</td><td>" . $row_head1['brand'] . "</td><td>" . $row_head1['LCNO'] . "</td>      </tr>";
        }
        
  

        echo "</table><br>";
         }else{
            $sql = "SELECT SDATE,indate,REFNO,brand_name,LCNO,pi_no,sum(COST*REC_QTY) as COST,sum(acc_cost*REC_QTY) as acc_COST,indate,SUP_NAME,SUM(REC_QTY) AS TOTQTY from viewpur where SDATE>='" . $_GET["DT1"] . "' AND SDATE<='" . $_GET["DT2"] . "'   AND cancel=0  ";
         if ($_GET["cmbcat"] != "All") {
            // if($_GET["cmbcat"] = "Pending") {
            //      $sql .=" and seri_no !='0'";
            // }else{
                 $sql .=" and type ='" . $_GET["cmbcat"] . "'";
            // }
       
        }
        
        if ($_GET["cmbbrand"] != "All") {
             $sql .=" and brand_name ='" . $_GET["cmbbrand"] . "'";
        }
         if ($_GET["department"] != "All") {
             $sql .=" and DEPARTMENT ='" . $_GET["department"] . "'";
        }

         
        $sql .="  group by SDATE,REFNO,brand_name,LCNO order by REFNO "; 
        $result = mysqli_query($GLOBALS['dbinv'],$sql) ;  
        echo "<table border='1' width=1000 ><tr> <td><b>DATE</td> <td><b>INV DATE</td> <td><b>AR NO</td> <td><b>Supplier</td><td><b>Inv No</td><td><b>BRAND NAME</td><td><b>TOT QTY</td><td><b>AMOUNT</td></tr>";
        while ($row = mysqli_fetch_array($result)) {      
           
           
        echo "<tr> <td>" . $row['SDATE'] . "</td><td>" . $row['SDATE'] . "</td><td>" . $row['REFNO'] . "</td> <td>" . $row['SUP_NAME'] . "</td> <td>" . $row['pi_no'] . "</td> <td>" . $row['brand_name'] . "</td>  <td>" . $row['TOTQTY'] . "</td>  <td  align='right'> " . number_format($row['acc_COST'], "2",".",",") . "</td>    </tr>";
        $cos = $cos+($row['acc_COST']);
        $TOTQTY = $TOTQTY+($row['TOTQTY']);
        
         
        }
        
 echo "<tr> <td></td> <td></td> <td></td>  <td></td><td></td><td></td><td align='right'><b>" . number_format($TOTQTY, "2",".",",")   . "</td> <td align='right'><b>" . number_format($cos, "2",".",",")   . "</td>    </tr>";

        echo "</table><br>";
         }


        
        ?>



    </body>
</html>
