<?php
ini_set('session.gc_maxlifetime', 30 * 60 * 60 * 60);
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>GIN Report</title>

        <style>
            .heading {
                color: #0000FF;
                font-weight: bold;
                font-size: 24px;
            }

            .heading1 {
                color: #000000;
                font-weight: bold;
                font-size: 18px;
            }
            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:15px;

            }
            td
            {
                font-size:15px;

            }
        </style>

    </head>

    <body> <center> 


            <?php
            include_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=\"heading\">" . $row_head["COMPANY"] . "</span></center><br>";

            $phrase = "";
            
            if ($_GET['department'] != "All")  {
                $sql = "select DESCRIPTION from s_stomas where CODE = '" .$_GET['department']. "'";
                $result = mysqli_query($GLOBALS['dbinv'], $sql);
                $row = mysqli_fetch_array($result);
                $phrase = " for " . $row['DESCRIPTION'];
            }

            echo "<center><span class=\"heading1\">Outstanding GIN Report $phrase<span class=\"heading\"></center><br>";

            echo "<table  width=1000 border=1>";
            echo "<tr>
                       <th align=center>Date</th>
                       <th align=center>REF NO</th>
                       <th align=center>Department</th>
                       <th align=center>Grand Total</th>
                       <th align=center>Total Paid</th>
                       <th align=center>Outstanding</th>
                  </tr>";

            $sql = "SELECT * FROM s_salma_gin where ((CANCELL = '0' AND GRAND_TOT > TOTPAY) OR (CANCELL = '0' AND TRN_TYPE = 'GINNR'))";


            if ($_GET['department'] != "All") {
                $sql .= " and C_CODE='" . $_GET["department"] . "'";
            }else{
                $sql .= " and C_CODE!='01'";
            }

            $sql .= " order by SDATE, CUS_NAME";

//            echo $sql;

            $result = mysqli_query($GLOBALS['dbinv'], $sql);
            $grandTot = 0;
            $totPay = 0;
            while ($row = mysqli_fetch_array($result)) {

                $grandTot += $row["GRAND_TOT"];
                $totPay += $row["TOTPAY"];

                echo "<tr>
                                <td>" . $row["SDATE"] . "</td>
                                <td>" . $row["REF_NO"] . "</td>
                                <td>" . $row["CUS_NAME"] . "</td>
                                <td align=right>" . number_format($row["GRAND_TOT"], 2, ".", ",") . "</td>
                                <td align=right>" . number_format($row["TOTPAY"], 2, ".", ",") . "</td>    
                                <td align=right>" . number_format($row["GRAND_TOT"]-$row["TOTPAY"], 2, ".", ",") . "</td>
                              </tr>";
            }
            echo "<tr><td colspan=4 align=right><b>" . number_format($grandTot, 2, ".", ",") . "</b></td><td align=right><b>" . number_format($totPay, 2, ".", ",") . "</b></td><td align=right><b>" . number_format($grandTot-$totPay, 2, ".", ",") . "</b></td></tr>";
            echo "</table>";
            ?>


    </body>
</html>
