<!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();
if ($_SESSION['company'] == "") {
    exit("Please Login");
}
if ($_SESSION['CURRENT_USER'] == "") {
    exit("Please Login");
}


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

$sql = "SELECT SDATE,REFNO,brand_name,book_no,company,LCNO,sum(COST*REC_QTY) as COST,sum(acc_cost*REC_QTY) as acc_COST,SUP_NAME,SUM(REC_QTY) AS 
    TOTQTY,pi_no from viewpur where SDATE>='" . $_GET["DT1"] . "' AND SDATE<='" . $_GET["DT2"] . "'   AND cancel=0   ";

if ($_GET['cuscode'] != "") {
    $sql .= " and SUP_CODE = '" . $_GET['cuscode'] . "' ";
}
if ($_GET["cmbcat"] != "All") {
    $sql .= " and type ='" . $_GET["cmbcat"] . "'";
}
if ($_GET["cmbbrand"] != "All") {
    $sql .= " and brand_name ='" . $_GET["cmbbrand"] . "'";
}
if ($_GET["department"] != "All") {
    $sql .= " and DEPARTMENT ='" . $_GET["department"] . "'";
}
if ($_SESSION["company"] != 'M') {
    $sql .= " and company = '" . $_SESSION["company"] . "' ";
}

$sql .= "  group by SDATE,REFNO,brand_name,LCNO order by REFNO ";
// echo $sql;
$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>Lc No</td>
    <td><b>KG</td>
    <td><b>BRAND NAME</td>
    <td><b>TOT QTY</td>
    <td><b>AMOUNT</td>
</tr>";
while ($row = mysqli_fetch_array($result)) {
    if ($row['company'] != $company) {
        $sql_para = "select * from invpara where flag='" . $row['company'] . "'";
        $result_para = mysqli_query($GLOBALS['dbinv'], $sql_para);
        $row_para = mysqli_fetch_array($result_para);

        echo "<tr>
                <td colspan='5'><b>" . $row['company'] . " - " . $row_para['COMPANY'] . "</b></td>
            </tr>";
    }

    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['book_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']);

    $company = $row['company'];
}

echo "<tr> <td></td> <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>
