<!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: 1000px;
            border: 1px solid black;


            border-collapse: collapse;
        }

        .tb td {

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

            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>
    <script language="javascript" type="text/javascript">
        <!--
        /****************************************************
         Author: Eric King
         Url: http://redrival.com/eak/index.shtml
         This script is free to use as long as this info is left in
         Featured on Dynamic Drive script library (http://www.dynamicdrive.com)
         ****************************************************/
        var win = null;

        function NewWindow(mypage, myname, w, h, scroll, pos) {
            if (pos == "random") {
                LeftPosition = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100;
                TopPosition = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100;
            }
            if (pos == "center") {
                LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
                TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
            } else if ((pos != "center" && pos != "random") || pos == null) {
                LeftPosition = 0;
                TopPosition = 20
            }
            settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
            win = window.open(mypage, myname, settings);
        }

        // -->
    </script>
</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>
    <table width="1000px;" 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>
            <th class='center' colspan='5'> <?php echo $_GET['dtfrom'] ?> To - <?php echo $_GET['dtto'] ?></th>
        </tr>


        <tr>
            <?php
            if ($_GET['Txtcusco'] != "") {

                ?>
                <th class='center' colspan='5'>Customer Wise - (<?php echo $_GET['Txtcusco'] ?> )</th>
            <?php } ?>
        </tr>
        <tr>
            <?php
            if ($_GET['company'] != "") {
                $sql_sto = "SELECT * from s_stomas where CODE='" . $_GET['company'] . "'";
                $result_sto = $conn->query($sql_sto);
                $row_sto = $result_sto->fetch();
                ?>
                <th class='center' colspan='5'>Company Wise - (<?php echo $row_sto['DESCRIPTION'] ?> )</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'] == "SUMMERY") { ?>
        <table class="tb" width="1000px;">
            <tr>

                <th width="30px;">NO</th>
                <th width="100px;">COMPANY</th>
                <th width="120px;">GRAND TOTAL</th>
                <th width="100px;">PAID</th>
                <th width="100px;">BALANCE</th>
            </tr>
            <?php
            $i = 1;
            $part = "";
            $BAL = 0;
            $sql1 = "Select sum(GRAND_TOT) AS GRAND_TOT,sum(TOTPAY) AS TOTPAY,sum(GRAND_TOT-TOTPAY) AS BAL,DEPARTMENT from s_salma  where CANCELL='0' and TRN_TYPE='INV' AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "'";

            if ($_GET['company'] != "") {
                $sql1 .= " and DEPARTMENT='" . $_GET['company'] . "'";
            }
            if ($_GET['Txtcusco'] != "") {
                $sql1 .= " and C_CODE='" . $_GET['Txtcusco'] . "'";
            }


            $sql1 .= " group by Brand";


            $i = 1;
            foreach ($conn->query($sql1) as $row1) {
                $sql_sto1 = "SELECT * from s_stomas where CODE='" . $row1['DEPARTMENT'] . "'";
                $result_sto1 = $conn->query($sql_sto1);
                $row_sto1 = $result_sto1->fetch();
                ?>
                <tr style="font-size:13px;">

                    <td class="center" style='font-size:20px;'><?php echo $i ?></td>
                    <td class="left" style='font-size:20px;'><?php echo $row_sto1['DESCRIPTION']; ?> </td>
                    <td align="right"
                        style='font-size:20px;'><?php echo number_format($row1['GRAND_TOT'], 2, ".", ","); ?></td>
                    <td align="right"
                        style='font-size:20px;'><?php echo number_format($row1['TOTPAY'], 2, ".", ","); ?></td>
                    <td align="right"
                        style='font-size:20px;'><?php echo number_format($row1['BAL'], 2, ".", ","); ?></td>
                </tr>
                <?php
                $i = $i + 1;
                $company = $row1['DEPARTMENT'];

                $GTOT = $GTOT + $row1['GRAND_TOT'];
                $TOTPAY = $TOTPAY + $row1['TOTPAY'];
                $BAL = $BAL + $row1['BAL'];
            }
            ?>


            <tr>
                <td align="right" colspan='2' style='font-size:20px;'><b></b></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($GTOT, 2, ".", ","); ?><b/></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($TOTPAY, 2, ".", ","); ?><b/></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($BAL, 2, ".", ","); ?><b/></td>
            </tr>


        </table>
    <?php } ?>

    <!--==================================================================================-->
    <?php if ($_GET['type'] == "BRAND WISE") { ?>
        <table class="tb" width="1000px;">
            <tr>

                <th width="30px;">NO</th>
                <th width="100px;">BRAND</th>
                <th width="120px;">SALE</th>
                <th width="100px;">RETURN</th>
                <th width="100px;">CREDIT NOTE</th>
                <th width="100px;">BALANCE</th>
                <th width="100px;">SHOP %</th>
                <th width="100px;">COUN %</th>
                <th width="100px;">FOR %</th>
                <th width="100px;">GM %</th>
                <th width="100px;">LABOUR %</th>
            </tr>
            <?php
            $i = 1;
            $BAL = 0;
            $BALANCE = 0;
            $sql1 = "select * from brand_mas where act ='1'";
            if ($_GET['brand'] != "All") {
                $sql1 .= " and barnd_name='" . $_GET['brand'] . "'";
            }

            $i = 1;
            foreach ($conn->query($sql1) as $row1) {

                $sql_sal = "Select sum(subtot) AS PRICE,BRAND from view_salma_sinvo  where CANCELL='0'  AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "' AND BRAND='" . $row1['barnd_name'] . "'";
                if ($_GET['company'] != "") {
                    $sql_sal .= " and company='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql_sal .= " and C_CODE='" . $_GET['Txtcusco'] . "'";
                }
                $sql_sal .= " group by BRAND";
                $result_sal = $conn->query($sql_sal);
                $rowsal = $result_sal->fetch();

                $sql_cnt = "SELECT SUM(AMOUNT) AS AMOUNT from c_bal where brand='" . $row1['barnd_name'] . "' and trn_type='CNT' AND CANCELL='0' AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "' ";
                if ($_GET['company'] != "") {
                    $sql_cnt .= " and company='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql_cnt .= " and CUSCODE='" . $_GET['Txtcusco'] . "'";
                }
                $result_cnt = $conn->query($sql_cnt);
                $row_cnt = $result_cnt->fetch();

                $sql_grn = "SELECT SUM(AMOUNT) AS AMOUNT from c_bal where brand='" . $row1['barnd_name'] . "' and trn_type='GRN' AND CANCELL='0' AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "' ";
                if ($_GET['company'] != "") {
                    $sql_grn .= " and company='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql_grn .= " and CUSCODE='" . $_GET['Txtcusco'] . "'";
                }
                $result_grn = $conn->query($sql_grn);
                $row_grn = $result_grn->fetch();

                $BALANCE = $rowsal['PRICE'] - $row_cnt['AMOUNT'] - $row_grn['AMOUNT'];

                if ($BALANCE != '0.00') {
                    $DIS1 = "0";
                    $DIS2 = "0";
                    $DIS3 = "0";
                    $DIS4 = "0";
                    $DIS5 = "0";

                    if ($row1['barnd_name'] == "CHARGING") {
                        $DIS1 = $BALANCE * 50 / 100;
                        $DIS2 = $BALANCE * 25 / 100;
                        $DIS3 = $BALANCE * 0 / 100;
                        $DIS4 = $BALANCE * 0 / 100;
                        $DIS5 = $BALANCE * 25 / 100;
                    } else if ($row1['barnd_name'] == "STAFF COMMISION") {
                        $DIS1 = $BALANCE * 20 / 100;
                        $DIS2 = $BALANCE * 30 / 100;
                        $DIS3 = $BALANCE * 3 / 100;
                        $DIS4 = $BALANCE * 2 / 100;
                        $DIS5 = $BALANCE * 65 / 100;
                    } else if ($row1['barnd_name'] == "USED ITEM") {
                        $DIS1 = $BALANCE * 80 / 100;
                        $DIS2 = $BALANCE * 5 / 100;
                        $DIS3 = $BALANCE * 3 / 100;
                        $DIS4 = $BALANCE * 2 / 100;
                        $DIS5 = $BALANCE * 10 / 100;
                    } else {
                        $DIS1 = "0";
                        $DIS2 = "0";
                        $DIS3 = "0";
                        $DIS4 = "0";
                        $DIS5 = "0";
                    }


                    ?>

                    <tr style="font-size:13px;">

                        <td class="center" style='font-size:20px;'><?php echo $i ?></td>
                        <td class="left" style='font-size:20px;'><?php echo $row1['barnd_name']; ?> </td>
                        <td align="right"
                            style='font-size:20px;'><?php echo number_format($rowsal['PRICE'], 2, ".", ","); ?></td>
                        <td align="right"
                            style='font-size:20px;'><?php echo number_format($row_grn['AMOUNT'], 2, ".", ","); ?></td>
                        <td align="right"
                            style='font-size:20px;'><?php echo number_format($row_cnt['AMOUNT'], 2, ".", ","); ?></td>
                        <td align="right"
                            style='font-size:20px;'><?php echo number_format($BALANCE, 2, ".", ","); ?></td>
                        <td align="right" style='font-size:20px;'><?php echo number_format($DIS1, 2, ".", ","); ?></td>
                        <td align="right" style='font-size:20px;'><?php echo number_format($DIS2, 2, ".", ","); ?></td>
                        <td align="right" style='font-size:20px;'><?php echo number_format($DIS3, 2, ".", ","); ?></td>
                        <td align="right" style='font-size:20px;'><?php echo number_format($DIS4, 2, ".", ","); ?></td>
                        <td align="right" style='font-size:20px;'><?php echo number_format($DIS5, 2, ".", ","); ?></td>
                    </tr>
                    <?php

                    $i = $i + 1;

                    $GTOT = $GTOT + $rowsal['PRICE'];
                    $CRN = $CRN + $row_cnt['AMOUNT'];
                    $GRN = $GRN + $row_grn['AMOUNT'];

                    $DIS1_TOT += $DIS1;
                    $DIS2_TOT += $DIS2;
                    $DIS3_TOT += $DIS3;
                    $DIS4_TOT += $DIS4;
                    $DIS5_TOT += $DIS5;
                }
            }
            ?>

            <tr>
                <td align="right" colspan='2' style='font-size:20px;'><b></b></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($GTOT, 2, ".", ","); ?><b/></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($GRN, 2, ".", ","); ?><b/></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($CRN, 2, ".", ","); ?><b/></td>
                <td align="right" style='font-size:24px;'>
                    <b><?php echo number_format($GTOT - $CRN - $GRN, 2, ".", ","); ?><b/></td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($DIS1_TOT, 2, ".", ","); ?><b/>
                </td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($DIS2_TOT, 2, ".", ","); ?><b/>
                </td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($DIS3_TOT, 2, ".", ","); ?><b/>
                </td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($DIS4_TOT, 2, ".", ","); ?><b/>
                </td>
                <td align="right" style='font-size:24px;'><b><?php echo number_format($DIS5_TOT, 2, ".", ","); ?><b/>
                </td>
            </tr>


        </table>

    <?php } ?>


    <!--==================================================================================-->
    <?php if ($_GET['type'] == "BRAND MONTH WISE"){ ?>
    <table class="tb" width="1000px;">

        <?php

        $month1 = date("m", strtotime($_GET["dtfrom"]));
        $month1_y = date("Y", strtotime($_GET["dtfrom"]));

        $month2 = date("m", strtotime($_GET["dtto"]));
        $month2_y = date("Y", strtotime($_GET["dtto"]));

        $startedDate = $_GET["dtfrom"];
        $endDate = $_GET["dtto"];

        $date1 = new DateTime($startedDate);
        $date2 = new DateTime($endDate);
        $interval = $date1->diff($date2);
        $count = $interval->format('%m');

        $newstartedDate = date_create($startedDate);
        $newstartedDatetable = date_create($startedDate);
        $heading1color = '#bb98a1';
        echo "<tr class='info'>                                 
                <td width='40' class='fixed-side' colspan='2'></td>";
        $monthcolor = 'yellow';
        for ($x = 0; $x <= $count; $x++) {
            echo "  <td align='center' colspan='4' style='background-color:$monthcolor'>" . date_format($newstartedDate, "Y-m") . "</td>";
            $newstartedDate = date_add($newstartedDate, date_interval_create_from_date_string("1 month"));

        }
        echo "</tr>";
        echo "<tr class='info'>                                 
                <td width='40' class='fixed-side' style='background-color:$heading1color'>No</td>                          
                <td align='left'  class='fixed-side' style='background-color:$heading1color'>Brand</td> ";
        $headingcolor = '#e9e9af';
        for ($x = 0; $x <= $count; $x++) {
            echo "  <td align='center' style='background-color:$headingcolor;font-size:12px;'>SALE</td>";
            echo "  <td align='center' style='background-color:$headingcolor;font-size:12px;'>RETURN</td>";
            echo "  <td align='center' style='background-color:$headingcolor;font-size:12px;'>CREDIT NOTE</td>";
            echo "  <td align='center' style='background-color:$headingcolor;font-size:12px;'>BALANCE</td>";
        }

        echo "</tr>";


        $i = 1;
        $BAL = 0;
        $BALANCE = 0;
        $sql1 = "select * from brand_mas where act ='1'";
        if ($_GET['brand'] != "All") {
            $sql1 .= " and barnd_name='" . $_GET['brand'] . "'";
        }

        $i = 1;
        foreach ($conn->query($sql1) as $row1) {


            echo "<tr>                                 
                    <td width='40' class='fixed-side' style='background-color:$descolor;font-size:12px;'>" . $i . "</td>                        
                    <td align=left class='fixed-side' style='background-color:$descolor;font-size:12px;'>" . $row1['barnd_name'] . "</td>   ";

            for ($x = 0; $x <= $count; $x++) {

                $sql_sal = "Select sum(subtot) AS PRICE,BRAND from view_salma_sinvo  where CANCELL='0'  AND month(SDATE)>='" . $month1 . "' AND year(SDATE)>='" . $month1_y . "' AND month(SDATE) <='" . $month2 . "' AND year(SDATE) <='" . $month2_y . "'  AND BRAND='" . $row1['barnd_name'] . "'";
                if ($_GET['company'] != "") {
                    $sql_sal .= " and company='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql_sal .= " and C_CODE='" . $_GET['Txtcusco'] . "'";
                }
                $sql_sal .= " group by BRAND";
                $result_sal = $conn->query($sql_sal);
                $rowsal = $result_sal->fetch();

                $sql_cnt = "SELECT SUM(AMOUNT) AS AMOUNT from c_bal where brand='" . $row1['barnd_name'] . "' and trn_type='CNT' AND CANCELL='0' AND month(SDATE)>='" . $month1 . "' AND year(SDATE)>='" . $month1_y . "' AND month(SDATE) <='" . $month2 . "' AND year(SDATE) <='" . $month2_y . "' ";
                if ($_GET['company'] != "") {
                    $sql_cnt .= " and company='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql_cnt .= " and CUSCODE='" . $_GET['Txtcusco'] . "'";
                }
                $result_cnt = $conn->query($sql_cnt);
                $row_cnt = $result_cnt->fetch();

                $sql_grn = "SELECT SUM(AMOUNT) AS AMOUNT from c_bal where brand='" . $row1['barnd_name'] . "' and trn_type='GRN' AND CANCELL='0' AND month(SDATE)>='" . $month1 . "' AND year(SDATE)>='" . $month1_y . "' AND month(SDATE) <='" . $month2 . "' AND year(SDATE) <='" . $month2_y . "'  ";
                if ($_GET['company'] != "") {
                    $sql_grn .= " and company='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql_grn .= " and CUSCODE='" . $_GET['Txtcusco'] . "'";
                }
                $result_grn = $conn->query($sql_grn);
                $row_grn = $result_grn->fetch();

                $BALANCE = $rowsal['PRICE'] - $row_cnt['AMOUNT'] - $row_grn['AMOUNT'];

                // if($BALANCE!='0.00' ){

                echo "  <td align='right' colspan='1' >" . number_format($rowsal['PRICE'], 2, ".", ",") . "</td>";
                echo "  <td align='right' colspan='1' >" . number_format($row_grn['AMOUNT'], 2, ".", ",") . "</td>";
                echo "  <td align='right' colspan='1' >" . number_format($row_cnt['AMOUNT'], 2, ".", ",") . "</td>";
                echo "  <td align='right' colspan='1' >" . number_format($BALANCE, 2, ".", ",") . "</td>";


                $month1 = $month1 + 1;
                $month2 = $month2 + 1;

                $GTOT = $GTOT + $rowsal['PRICE'];
                $CRN = $CRN + $row_cnt['AMOUNT'];
                $GRN = $GRN + $row_grn['AMOUNT'];

                // }
            }

            $i = $i + 1;
            $month1 = date("m", strtotime($_GET["dtfrom"]));
            $month2 = date("m", strtotime($_GET["dtto"]));

            echo "</tr>";


        }


        echo "  <td align='center' colspan='1' style='background-color:$valuecolor;font-size:12px;'></td>
                <td align='center' colspan='1' style='background-color:$valuecolor;font-size:12px;'></td>  ";
        echo "  <td align='right' colspan='1' style='background-color:$valuecolor;font-size:19px;'><b>" . number_format($GTOT, 2) . "</b></td>";
        echo "  <td align='right' colspan='1' style='background-color:$valuecolor;font-size:19px;'><b>" . number_format($GRN, 2) . "</b></td>";
        echo "  <td align='right' colspan='1' style='background-color:$valuecolor;font-size:19px;'><b>" . number_format($CRN, 2) . "</b></td>";
        echo "  <td align='right' colspan='1' style='background-color:$valuecolor;font-size:19px;'><b>" . number_format($GTOT - $CRN - $GRN, 2) . "</b></td>";


        echo "</table>";


        echo " </table>";


        } ?>


        <!--==================================================================================-->
        <?php if ($_GET['type'] == "DETAIL") { ?>
            <table class="tb" width="1000px;">
                <tr>

                    <th width="30px;">NO</th>
                    <th width="100px;">REFNO</th>
                    <th width="50px;">DATE</th>
                    <th width="300px;">CUSTOMER</th>
                    <th width="300px;">REMARK</th>

                    <th width="120px;">GRAND TOTAL</th>
                    <th width="100px;">PAID</th>
                    <th width="100px;">BALANCE</th>


                </tr>
                <?php
                $i = 1;
                $part = "";
                $BAL = 0;
                $sql1 = "Select * from s_salma  where CANCELL='0' and TRN_TYPE='INV' AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "'";

                if ($_GET['company'] != "") {
                    $sql1 .= " and DEPARTMENT='" . $_GET['company'] . "'";
                }
                if ($_GET['Txtcusco'] != "") {
                    $sql1 .= " and C_CODE='" . $_GET['Txtcusco'] . "'";
                }

                $sql1 .= " order by DEPARTMENT,REF_NO";


                $i = 0;
                foreach ($conn->query($sql1) as $row1) {
                    $sql_sto1 = "SELECT * from s_stomas where CODE='" . $row1['DEPARTMENT'] . "'";
                    $result_sto1 = $conn->query($sql_sto1);
                    $row_sto1 = $result_sto1->fetch();
                    ?>
                    <tr style="font-size:13px;">

                        <?php

                        if ($company != $row1['DEPARTMENT']) {
                            if ($i != 0) {
                                echo "<tr>
                                 <td colspan='5'><b>   </b></td>
                                <td colspan='1' align=right><b>  " . number_format($GTOT, 2, ".", ",") . "   </b></td>
                                <td colspan='1' align=right><b>  " . number_format($TOTPAY, 2, ".", ",") . "   </b></td>
                                <td colspan='1' align=right><b>  " . number_format($BAL, 2, ".", ",") . "   </b></td>
                                </tr>";

                            }
                            $i = 1;
                            echo "<tr>
                                    <td colspan='7'><b>  " . $row_sto1['DESCRIPTION'] . "   </b></td> </tr>";
                            $GTOT = $row1['GRAND_TOT'];
                            $TOTPAY = $row1['TOTPAY'];
                            $BAL = ($row1['GRAND_TOT'] - $row1['TOTPAY']);

                        } else {
                            $GTOT = $GTOT + $row1['GRAND_TOT'];
                            $TOTPAY = $TOTPAY + $row1['TOTPAY'];
                            $BAL = $BAL + ($row1['GRAND_TOT'] - $row1['TOTPAY']);
                        }

                        ?>
                        <td class="center" style='font-size:12px;'><?php echo $i ?></td>
                        <td><a onfocus="this.blur()"
                               onclick="NewWindow('../sales_inv_display.php?refno=<?php echo $row1['REF_NO']; ?>&amp;trn_type=INV','mywin','900','700','yes','center');return false"
                               class="INV" href=""><?php echo $row1['REF_NO']; ?></a></td>
                        <td class="left" style='font-size:12px;'><?php echo $row1['SDATE']; ?> </td>
                        <td class="left" style='font-size:14px;'><?php echo $row1['C_CODE']; ?>
                            &nbsp;-&nbsp;<?php echo $row1['CUS_NAME']; ?> </td>
                        <td class="left" style='font-size:12px;'><?php echo $row1['note']; ?> </td>
                        <td align="right"
                            style='font-size:14px;'><?php echo number_format($row1['GRAND_TOT'], 2, ".", ","); ?></td>
                        <td align="right"
                            style='font-size:14px;'><?php echo number_format($row1['TOTPAY'], 2, ".", ","); ?></td>
                        <td align="right"
                            style='font-size:14px;'><?php echo number_format($row1['GRAND_TOT'] - $row1['TOTPAY'], 2, ".", ","); ?></td>
                    </tr>
                    <?php
                    $i = $i + 1;
                    $company = $row1['DEPARTMENT'];

                    $GTOT_TOT = $GTOT_TOT + $row1['GRAND_TOT'];
                    $TOTPAY_TOT = $TOTPAY_TOT + $row1['TOTPAY'];
                    $BAL_TOT = $BAL_TOT + ($row1['GRAND_TOT'] - $row1['TOTPAY']);
                }
                ?>


                <tr>
                    <td align="right" colspan='5' style='font-size:20px;'><b></b></td>
                    <td align="right" style='font-size:20px;'><b><?php echo number_format($GTOT, 2, ".", ","); ?><b/>
                    </td>
                    <td align="right" style='font-size:20px;'><b><?php echo number_format($TOTPAY, 2, ".", ","); ?><b/>
                    </td>
                    <td align="right" style='font-size:20px;'><b><?php echo number_format($BAL, 2, ".", ","); ?><b/>
                    </td>
                </tr>

                <tr>
                    <td align="right" colspan='5' style='font-size:20px;'><b>Total</b></td>
                    <td align="right" style='font-size:20px;'><b><?php echo number_format($GTOT_TOT, 2, ".", ","); ?>
                            <b/></td>
                    <td align="right" style='font-size:20px;'><b><?php echo number_format($TOTPAY_TOT, 2, ".", ","); ?>
                            <b/></td>
                    <td align="right" style='font-size:20px;'><b><?php echo number_format($BAL_TOT, 2, ".", ","); ?><b/>
                    </td>
                </tr>
            </table>


        <?php } ?>
        <!--===================================================================================-->


        <!-- ************************************************************************** -->
</center>
</body>

</html>
