<?php session_start(); ?>
<!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>AGAIN REPORT</title>

        <style>
            table
            {
                border-collapse:collapse;
            }
            table, td, th
            {
                border:1px solid black;
                font-family:Arial, Helvetica, sans-serif;
                padding:5px;
            }
            th
            {
                font-weight:bold;
                font-size:14px;

            }
            td
            {
                font-size:14px;

            }
        </style>

    </head>

    <body>


        <?php
        require_once("connectioni.php");

        $sql_head = "select * from invpara";
        $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>";

        echo "<center><h3>AGAIN Report As At: " . date('yy-m-d'). "</h3><br>";

			 
            $sql_salma = "select * from s_salma where  (GRAND_TOT-TOTPAY)>1  ";
            
             if ($_GET["rep_name"] != "All") {
                $sql_salma .= " and SAL_EX like '" . Trim($_GET["rep_name"]) . "%'";
            }
             if ($_GET["c_code"] != "") {
                $sql_salma .= " and C_CODE like '" . Trim($_GET["c_code"]) . "%'";
            }
             $sql_salma .= " and CANCELL='0' order by SDATE desc";
             
       
            $result_salma = mysqli_query($GLOBALS['dbinv'], $sql_salma);
            
            echo "<table border=1 width=1000><tr>
		<th>Date</th><th>Inv No.</th><th>Cus.code</th><th>Customer</th><th>Days</th><th>Invoice Value</td><th>Outstanding</td><th>30 Days</th><th>60 Days</th><th>90 Days</th><th>120 Days</th><th>Over 120 Days</th></tr>";
            $AMOUNT = 0;
            $totbal = 0;
            $belowA = 0;
            $belowB = 0;
            $belowC = 0;
            $belowD = 0;
            $overD = 0;

            while ($rows = mysqli_fetch_array($result_salma)) {

                $date1 = $rows["SDATE"];
//                $date2 = date("Y-m-d");
                // $date2 = $_GET["dtddate"];
                $date2 = date('yy-m-d'); 
                $diff = abs(strtotime($date2) - strtotime($date1));
                $days = floor($diff / (60 * 60 * 24));

                $bal = $rows["GRAND_TOT"] - $rows["TOTPAY"];
                if ($bal > 0.5) {


                    if ($days < 30) {
                        $belowA += $bal;
                        echo "<tr>";
                        echo "<td>" . $rows["SDATE"] . "</td>";
                        echo "<td>" . $rows["REF_NO"] . "</td>";
                        echo "<td>" . $rows["C_CODE"] . "</td>";
                        echo "<td>" . $rows["CUS_NAME"] . "</td>";
                        echo "<td align=\"right\">" . $days . "</td>";
                        echo "<td align=\"right\">" . number_format($rows["GRAND_TOT"], 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "</tr>";
                    } else if ($days < 60) {
                        $belowB += $bal;
                        echo "<tr>";
                        echo "<td>" . $rows["SDATE"] . "</td>";
                        echo "<td>" . $rows["REF_NO"] . "</td>";
                        echo "<td>" . $rows["C_CODE"] . "</td>";
                        echo "<td>" . $rows["CUS_NAME"] . "</td>";
                        echo "<td align=\"right\">" . $days . "</td>";
                        echo "<td align=\"right\">" . number_format($rows["GRAND_TOT"], 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "</tr>";
                    } else if ($days < 90) {
                        $belowC += $bal;
                        echo "<tr>";
                        echo "<td>" . $rows["SDATE"] . "</td>";
                        echo "<td>" . $rows["REF_NO"] . "</td>";
                        echo "<td>" . $rows["C_CODE"] . "</td>";
                        echo "<td>" . $rows["CUS_NAME"] . "</td>";
                        echo "<td align=\"right\">" . $days . "</td>";
                        echo "<td align=\"right\">" . number_format($rows["GRAND_TOT"], 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "</tr>";
                    } else if ($days < 120) {
                        $belowD += $bal;
                        echo "<tr>";
                        echo "<td>" . $rows["SDATE"] . "</td>";
                        echo "<td>" . $rows["REF_NO"] . "</td>";
                        echo "<td>" . $rows["C_CODE"] . "</td>";
                        echo "<td>" . $rows["CUS_NAME"] . "</td>";
                        echo "<td align=\"right\">" . $days . "</td>";
                        echo "<td align=\"right\">" . number_format($rows["GRAND_TOT"], 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "</tr>";
                    } else if ($days > 120) {
                        $overD += $bal;
                        echo "<tr>";
                        echo "<td>" . $rows["SDATE"] . "</td>";
                        echo "<td>" . $rows["REF_NO"] . "</td>";
                        echo "<td>" . $rows["C_CODE"] . "</td>";
                        echo "<td>" . $rows["CUS_NAME"] . "</td>";
                        echo "<td align=\"right\">" . $days . "</td>";
                        echo "<td align=\"right\">" . number_format($rows["GRAND_TOT"], 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">0.0</td>";
                        echo "<td align=\"right\">" . number_format($bal, 2, ".", ",") . "</td>";
                        echo "</tr>";
                    }

                    $AMOUNT = $AMOUNT + $rows["GRAND_TOT"];
                    $totbal = $totbal + $bal;
                     
                }
            }

            echo "<tr>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td align=\"right\">&nbsp;</td>";
            echo "<td align=\"right\">&nbsp;</td>";
            echo "<td align=\"right\">&nbsp;</td>";
            echo "<td align=\"right\"><b>" . number_format($AMOUNT, 2, ".", ",") . "</b></td>";
            echo "<td align=\"right\"><b>" . number_format($totbal, 2, ".", ",") . "</b></td>";
            echo "<td align=\"right\"><b>" . number_format($belowA, 2, ".", ",") . "</b></td>";
            echo "<td align=\"right\"><b>" . number_format($belowB, 2, ".", ",") . "</b></td>";
            echo "<td align=\"right\"><b>" . number_format($belowC, 2, ".", ",") . "</b></td>";
            echo "<td align=\"right\"><b>" . number_format($belowD, 2, ".", ",") . "</b></td>";
            echo "<td align=\"right\"><b>" . number_format($overD, 2, ".", ",") . "</b></td>";
            echo "</tr>";



           

            echo "</table>";

        ?>


    </body>
</html>