<?php
session_start();
include './connection_sql.php';
if ($_SESSION["CURRENT_USER"] == "") { 
    exit('Please Logging Again..');
} 
if ($_SESSION['company'] == "") { 
    exit('Please Logging Again..');
} 
?>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Supllier Outstanding</title>


    <style>
        @media print {
            a[href]:after {
                content: none !important;
            }
        }
        a:link, a:visited {

            text-decoration: none;
            color:#000000;
        }
        

        a:hover {
            text-decoration: underline;
        }
        body {
            color: #333;
            font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
            font-size: 12px;
            line-height: 1.42857;
        }
        .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
            border-top:1px solid #DDDDDD;
            line-height:1.42857;
            padding:2px;
            vertical-align:top;
        }
        .right {
            text-align: right;
        }    
        .center {
            text-align: center;
        }
    </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>

<body> 
    <?php
   
    require_once './gl_posting.php';


   $sql_rspara = "select * from a_dep_mas where COMCODE='".$_SESSION['company']."'";
        $result_rspara = $conn->query($sql_rspara);  
        if (!$row_rspara = $result_rspara->fetch()) {
            exit();
        }
   
    $txtdes = "Supplier Outstanding Report ";
    
    
    $table = "";
      $table .=  "<h3>" .  $row_rspara['COMPANY']  . "</h3>";
      $table .=  "<h4>" .  $txtdes  . "</h4>"; 

    
  
      
        $table .= "<table class='table' >
		
      	<tr>
            <th class='center'  style='width: 150px;'>Purchase No</th>
            <th class='center'  style='width: 150px;'>Purchase Date</th> 
            <th class='center'  style='width: 250px;'>Invoice No</th>
            <th class='center'  style='width: 150px;'>Grand Total</th>   
            <th class='center'  style='width: 150px;'>Paid</th>   
            <th class='center'  style='width: 150px;'>Balance</th>     		
        </tr>";
 
        $sql_rsPrInv = "select  * from view_cbal where       trn_type='ARN' and cancell='0' AND BALANCE>1";
        
        if($_GET["c_code"] !=''){
            $sql_rsPrInv .= "   and CUSCODE = '" . $_GET["c_code"].  "' " ; 
        }
        // company = '" . $_SESSION['company'] .  "'
        $sql_rsPrInv .= "   ORDER BY CUSCODE,SDATE  " ; 
         $i=0;
     $SUPPLIER ==""; 
        foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
             
                        
                        if($SUPPLIER!=$row_rsPrInv['CUSCODE']){
                           if($i !=0){
                               $table .= "<tr>
                                 <td colspan='3'><b>   </b></td>
                                <td colspan='1' align=right><b>  ".number_format($AMOUNT, 2, ".", ",") ."   </b></td>
                                <td colspan='1' align=right><b>  ". number_format($PAID, 2, ".", ",") ."   </b></td>
                                <td colspan='1' align=right><b>  ". number_format($BALANCE, 2, ".", ",") ."   </b></td>
                                </tr>";
                                
                           } 
                            $i = 1;
                           $table .= "<tr>
                                    <td colspan='6'><b>  ". $row_rsPrInv["NAME"] ."   </b></td> </tr>";
                             $AMOUNT =$row_rsPrInv['AMOUNT'];
                            $PAID =($row_rsPrInv['AMOUNT']-$row_rsPrInv['BALANCE']);
                            $BALANCE =$row_rsPrInv['BALANCE'];
                             
                        }else{
                            $AMOUNT +=$row_rsPrInv['AMOUNT'];
                            $PAID +=($row_rsPrInv['AMOUNT']-$row_rsPrInv['BALANCE']);
                            $BALANCE +=$row_rsPrInv['BALANCE'];
                        }
                       
    
                    $table .= "<tr><td>" . $row_rsPrInv["REFNO"] . "</td>
			                    <td>" . $row_rsPrInv["SDATE"] . "</td>
			                    <td>" . $row_rsPrInv["pi_no"] . "</td> 
			                    <td  class='right'>" . number_format($row_rsPrInv['AMOUNT'], 2, ".", ",") . "</td>
			                    <td  class='right'>" . number_format(($row_rsPrInv['AMOUNT']-$row_rsPrInv['BALANCE']), 2, ".", ",") . "</td>
			                    <td  class='right'>" . number_format($row_rsPrInv['BALANCE'], 2, ".", ",") . "</td>"; 
                    $table .= "</tr>";
                    
            $SUPPLIER= $row_rsPrInv["CUSCODE"];
                
            $AMOUNT_TOT +=$row_rsPrInv['AMOUNT'];
            $PAID_TOT +=($row_rsPrInv['AMOUNT']-$row_rsPrInv['BALANCE']);
            $BALANCE_TOT +=$row_rsPrInv['BALANCE'];
        }
     
    
    
         $table .= "<tr><td colspan=3>&nbsp;</td>
    		<th  class='right'>" . number_format($AMOUNT, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($PAID, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($BALANCE, 2, ".", ",") . "</th> </tr>";
    		
    		 $table .= "<tr><td colspan=3>&nbsp;</td>
    		<th  class='right'>" . number_format($AMOUNT_TOT, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($PAID_TOT, 2, ".", ",") . "</th>
    		<th  class='right'>" . number_format($BALANCE_TOT, 2, ".", ",") . "</th> </tr>";
    		 
    
    
    $table .= "</table>
 
</body>
</html>";

    echo $table;

    