<?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>Customer Ledger</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:12px;

            }
            td
            {
                font-size:11px;

            }
        </style>

    </head>

    <body> <center> 


            <?php
            require_once("./connection.php");

            $sql = "delete  from tmp_cus_ledg";    
            $result = mysql_query($sql,$dbinv);
            
            echo mysql_error($dbinv);
            
            $sql_head = "select * from invpara";
            $result_head = mysql_query($sql_head,$dbinv);
            $row_head = mysql_fetch_array($result_head);

            echo "<center><span class=\"style1\">" . $row_head["COMPANY"] . "</span></center><br>";
            
//            $from = "2016-05-01";
//            $to = "2016-10-10";
            
            $from = $_GET["dateFrom"];
            $to = $_GET["dateTo"];

//            $cuscode = "123";
//            $cusName = "xyz";
            
            $cuscode = $_GET["cuscode"];
            $cusName = $_GET["cusname"];
            
            $sql = "SELECT * from s_salma where cancell!='1' and SDATE between '$from' and '$to' and C_CODE = '$cuscode'";
            $result = mysql_query($sql,$dbinv);
            
            while($row = mysql_fetch_array($result)){
                $userdata[]= "('".$row["REF_NO"]."','".$row["SDATE"]."','Invoice',".$row["GRAND_TOT"].",'".$_SESSION["CURRENT_USER"]."')";
            }
           
        
            $sql = "SELECT * from s_sttr where ST_DATE between '$from' and '$to' and cus_code = '$cuscode'";
            $result = mysql_query($sql,$dbinv);
            
            
            echo mysql_error($dbinv);
            
            while($row = mysql_fetch_array($result)){
                
                $userdata[]="('".$row["ST_REFNO"]."', '".$row["ST_DATE"]."','Receipt','".$row["ST_PAID"]."','".$_SESSION["CURRENT_USER"]."')";
            }
            
             $sql = "insert into tmp_cus_ledg(ref_no, date, type, amount, user_id) values" . implode(',', $userdata); 
             $result = mysql_query($sql,$dbinv);
            
            echo "<center>Customer Code :   $cuscode </center><br>";
            echo "<center>Customer Name :   $cusName </center><br>";
            echo "<center>Period :   $from to $to </center><br>";

            echo "<table  width=600 border=1>";
            echo "<tr><td align=center width=400><b>Ref No</b></td><td align=center><b>Date</b></td><td><b>Document</b></td><td><b>Debit</b></td><td><b>Credit</b></td><td><b>Balance</b></td></tr>";
            
            
            $sql = "SELECT * from tmp_cus_ledg order by date, type";
            $result = mysql_query($sql,$dbinv);
            
            $balance = 0;
            $amt = 0;
            $totDeb = 0;
            $totCre = 0;
            while ($row = mysql_fetch_array($result)) {
                $type = $row["type"];
                
                echo "<tr><td>" . $row["ref_no"] . "</td><td align=right>" . $row["date"] . "</td><td align=right>" . $type . "</td>";
                $amt = $row["amount"];
                if ($type == "Invoice"){
                    $balance += $amt;
                    $totDeb += $amt;
                    echo "<td align=right>" . number_format($amt, 2, ".", ",") . "</td><td align=right></td>"; 
                }else{
                    $balance -= $amt;
                    $totCre += $amt;
                    echo "<td align=right></td><td align=right>" . number_format($amt, 2, ".", ",") . "</td>"; 
                }
                echo "<td align=right>" . number_format($balance, 2, ".", ",") . "</td></tr>";
            }
        echo "<tr><td colspan=3></td><td align=right><b>" . number_format($totDeb, 2, ".", ",") . "</b></td><td align=right><b>" . number_format($totCre, 2, ".", ",") . "</b></td><td align=right><b>" . number_format($balance, 2, ".", ",") . "</b></td></tr>";

            echo "</table>";
            ?>


    </body>
</html>
