<?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>Creditor Current Status</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;
                border-bottom:none;
                border-top:none;
            }
        </style>

    </head>

    <body>

 <?php
        require_once("connectioni.php");
        $rep = trim($_GET["rep"]);

        $sql_head = "select * from invpara";
        $result_head = mysqli_query($GLOBALS['dbinv'], $sql_head);
        $row_head = mysqli_fetch_array($result_head);
        
         $sql_head1 = "select * from vendor where CODE ='".$_GET["l_code"]."'";
        $result_head1 = mysqli_query($GLOBALS['dbinv'], $sql_head1);
        $row_head1 = mysqli_fetch_array($result_head1);
        


        $tb = "<center><span class=\"style1\">" . $row_head["COMPANY"] . "</span></center><br>";
        $tb .= "<center>" . $row_head1['NAME'] . "<table border=1><tr>";

        $tb .= "<center>Creditor Current Status";

        $tb .= "<tr>";
        $tb .= "<th width=\"100\"  background=\"\">REF NO</th>";
        $tb .= "<th width=\"100\"  background=\"\">Date</th>";
        $tb .= "<th width=\"300\"  background=\"\">Name</th>";
        $tb .= "<th width=\"100\"  background=\"\">Invoice No</th>";
        $tb .= "<th width=\"175\"  background=\"\">Gross With 15% VAT</th>";
        $tb .= "<th width=\"150\"  background=\"\"> VAT</th>";
        $tb .= "<th width=\"200\"  background=\"\">Net</th>";
        $tb .= "<th width=\"100\"  background=\"\">Days</th>";
        $tb .= "<th width=\"150\"  background=\"\"> Return Amount</th>";
        $tb .= "<th width=\"150\"  background=\"\"> Balance</th>";
       
        if ($_GET['checked1'] == '1' && $_GET['checked2'] == '1'){

             $sql = "Select sum(S_gross) as gross, sum(Vat2) as vatamo,sum(S_Net) as snet ,SUP_NAME,SUP_CODE,SDATE,REFNO,LCNO,BALANCE from view_sordtrn_spurmas_new where SUP_CODE='" . $_GET['c_code'] . "' and SDATE >= '" . $_GET['fromdt'] . "' and SDATE <= '" . $_GET['todt'] . "' GROUP BY REFNO order by SUP_CODE,REFNO ";  //echo $sql;
             
        }
        
       else if ($_GET['checked1'] == '1') {
           
             $sql = "Select sum(S_gross) as gross, sum(Vat2) as vatamo,sum(S_Net) as snet ,SUP_NAME,SUP_CODE,SDATE,REFNO,LCNO,BALANCE from view_sordtrn_spurmas_new where SUP_CODE='" . $_GET['c_code'] . "' and SDATE <= '" . $_GET['from_date'] . "' GROUP BY REFNO order by SUP_CODE ";
     
        }else if ($_GET['checked2'] == '1'){
             $sql = "Select sum(S_gross) as gross, sum(Vat2) as vatamo,sum(S_Net) as snet ,SUP_NAME,SUP_CODE,SDATE,REFNO,LCNO,BALANCE from view_sordtrn_spurmas_new where SDATE >= '" . $_GET['fromdt'] . "' and SDATE <= '" . $_GET['todt'] . "' GROUP BY REFNO order by SUP_CODE ";  //echo $sql;
             
        }
        else {
             $sql = "Select  sum(S_gross) as gross, sum(Vat2) as vatamo,sum(S_Net) as snet ,SUP_NAME,SUP_CODE,SDATE,REFNO,LCNO,BALANCE from view_sordtrn_spurmas_new where  SDATE <= '" . $_GET['from_date'] . "' and flag1='NS' GROUP BY REFNO order by SUP_CODE ";
              
        }

        $result = mysqli_query($GLOBALS['dbinv'], $sql);

        $mbrand = "";
        $totNetAmount = "0";
        $totAmount = "0";
        $totvat2 = "0";
        $totreturngrn="0";
        $minReturn="0";
        $totreturn_Amount = "0";


        $GVTOT = 0.00;
        $GVVAT = 0.00;
        $GVNBT = 0.00;
        $GVRTNN=0.00;
        
        $SW = 1;

        while ($row = mysqli_fetch_array($result)) {
            
            
            $sqlreturn = "Select  return1 from c_bal where  REFNO = '" . $row['REFNO'] . "'";
            $result_return = mysqli_query($GLOBALS['dbinv'], $sqlreturn);
            $row_return = mysqli_fetch_array($result_return);
              
              //echo $sqlreturn;
            
         $dates1=date("Y-m-d");
         $invoiceDate = $row['SDATE'];
         $TodayDate = $dates1;
         
         $date1 = date_create($invoiceDate);
         $date2 = date_create($TodayDate);
         $diff = date_diff($date1, $date2);
         
           $vat1=0;
           $net1=0;
           
            
          
           
           
           
        if($mbrand !=$row['SUP_CODE']){
            
            if ($SW == 1){
               
                $SW = 5;
            }else{
                 $tb .= "<tr><th colspan=\"4\" ; align=\"left\">". ''."</th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVTOT,2) . "    </th></th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVVAT,2) . "    </th><th colspan=\"1\" ; align=\"right\">" . number_format($GVNBT,2) . "    </th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\">" . number_format("-".$GVRTN,2) . "    </th></th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVRTNN,2) . "  </tr>";        
                 $tb .= "<tr><th colspan=\"4\" ; align=\"left\">". ''."</th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVRTNSUM,2) . "    </th></th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\">  </th><th colspan=\"1\" ; align=\"right\">  </th></tr>";        
            
            }
             $tb .= "<tr><th colspan=\"10\" ; align=\"left\">". $row['SUP_CODE']."   " . $row['SUP_NAME'] . "    </th>";
              $mbrand =$row['SUP_CODE'];
              
                $GVTOT = 0;
                $GVVAT = 0;
                $GVNBT = 0;
                $GVRTN = 0;
                $GVRTNSUM = 0;
                
                $GVRTNN=0;
                
                  
              
               
        }
        

        
        $GVTOT = $row['gross'] + $GVTOT;
        $GVVAT = $row['vatamo'] + $GVVAT;
        $GVNBT = $row['snet'] + $GVNBT;
        $GVRTN = $row_return['return1'] + $GVRTN;
        $GVRTNSUM=$GVTOT-$GVRTN;
        $minReturn= $row['BALANCE']-$row_return['return1'];
        $GVRTNN=$GVRTNN+$minReturn;
        
         $totreturn_Amount=$totreturn_Amount+$minReturn;

  
 // echo $minReturn;
            $tb .= "<tr><td>" . $row['REFNO'] . "</td>";
            $tb .= "<td>" . $row['SDATE'] . "</td>";
            $tb .= "<td>" . $row['SUP_NAME'] . "</td>";
            $tb .= "<td>" . $row['LCNO'] . "</td>";
            $tb .= "<td align=\"right\">" .number_format($row['gross'], 2, ".", ","). "</td>";
            $tb .= "<td align=\"right\">" .number_format($row['vatamo'], 2, ".", ","). "</td>";
            $tb .= "<td align=\"right\">" .number_format($row['snet'], 2, ".", ","). "</td>";
            $tb .= "<td align=\"right\">" . $diff->format("%R%a days") . "</td>";
            $tb .= "<td align=\"right\" sty>" .number_format("-".$row_return['return1'], 2, ".", ","). "</td>";
            $tb .= "<td align=\"right\">" .number_format($minReturn, 2, ".", ","). "</td>";
            $totAmount = $totAmount + $row['gross'];
            $totNet = $totNet + $row['snet'];
            $totvat2 = $totvat2+$row['vatamo'];
            $totreturngrn=$totreturngrn+$row_return['return1'];
            
            $minReturn=$totAmount-$totreturngrn;
            
            
            
           
            
            $mbrand !=$row['SUP_NAME'];
            $mrefNo !=$row['REFNO'];
             
             
            $tb .= "</tr>";
            
        }
        
        
       
          $tb .= "<tr><th colspan=\"4\" ; align=\"left\">". ''."</th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVTOT,2) . "    </th></th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVVAT,2) . "    </th><th colspan=\"1\" ; align=\"right\">" . number_format($GVNBT,2) . "   </th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\">" . number_format("-".$GVRTN,2) . "    </th><th colspan=\"1\" ; align=\"right\">" . number_format($GVRTNN,2) . "</th></tr>";
          $tb .= "<tr><th colspan=\"4\" ; align=\"left\">". ''."</th><th colspan=\"1\" ; align=\"right\">" .  number_format($GVRTNSUM,2) . "    </th></th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\">   </th><th colspan=\"1\" ; align=\"right\">    </th><th colspan=\"1\" ; align=\"right\"></th><th colspan=\"1\" ; align=\"right\"></th></tr>";
        
        $tb .= "<tr><th></th>";
        $tb .= "<th></th>";
        $tb .= "<th></th>";
        $tb .= "<th></th>";
        $tb .= "<th >".number_format($totAmount, 2, ".", ",")."</th>";
        $tb .= "<th>".number_format($totvat2, 2, ".", ",")."</th>";
        $tb .= "<th>".number_format($totNet, 2, ".", ",")."</th>";
        $tb .= "<th></th>";
        $tb .= "<th>".number_format($totreturngrn, 2, ".", ",")."</th>";
        $tb .= "<th>".number_format($totreturn_Amount, 2, ".", ",")."</th>";
        
        $tb .= "</table>";
        
        echo $tb;
        ?>
    </body>
</html>

