<?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 Payments</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 Payments Report ";
    
    
    $table = "";
      $table .=  "<h3>" .  $row_rspara['COMPANY']  . "</h3>";
      $table .=  "<h4>" .  $txtdes  . "</h4>"; 
      $table .=  "<h4>" .  ($_GET['dtfrom'].' To '.$_GET['dtto'])  . "</h4>"; 
      $table .=  "<h4>" .  $_GET['type']  . "</h4>"; 
  
      
      if($_GET['type']=="Settlement_Wise"){
       
        $table .= "<table class='table' >
		
          	<tr>
                <th class='center'  style='width: 150px;'>Pay No</th>
                <th class='center'  style='width: 150px;'>Pay Date</th> 
                <th class='center'  style='width: 250px;'>Purchase No</th>
                <th class='center'  style='width: 250px;'>Invoice No</th>   
                <th class='center'  style='width: 150px;'>Pay Type</th>    		
                <th class='center'  style='width: 150px;'>Paid</th>    		
            </tr>";
     
            $sql_rsPrInv = "select  * from a_pay_invdetails where    COMPANY = '" . $_SESSION['company'] .  "' and CANCELL='0' AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "'";
            
            if($_GET["c_code"] !=''){
                $sql_rsPrInv .= "   and CUSCODE = '" . $_GET["c_code"].  "' " ; 
            }
            
            $sql_rsPrInv .= "   ORDER BY CUSCODE,SDATE,REFNO  " ; 
             $i=0;
            //  echo $sql_rsPrInv;
            $SUPPLIER ==""; 
            foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
             
                        $sql_vendor = "select * from vendor where CODE='".$row_rsPrInv['CUSCODE']."'";
                        $result_vendor = $conn->query($sql_vendor);  
                        $row_vendor = $result_vendor->fetch();
                        
                        if($SUPPLIER!=$row_rsPrInv['CUSCODE']){
                           if($i !=0){
                               $table .= "<tr>
                                 <td colspan='5'><b>   </b></td>
                                <td colspan='1' align=right><b>  ". number_format($PAID, 2, ".", ",") ."   </b></td>
                                </tr>";
                                
                           } 
                            $i = 1;
                           $table .= "<tr>
                                    <td colspan='6'><b>  ". $row_vendor["NAME"] ."   </b></td> </tr>";
                             $PAID =$row_rsPrInv['PAID'];
                             
                        }else{
                            $PAID +=$row_rsPrInv['PAID'];
                        }
                       
    
                    $table .= "<tr><td>" . $row_rsPrInv["REFNO"] . "</td>
			                    <td>" . $row_rsPrInv["SDATE"] . "</td>
			                    <td>" . $row_rsPrInv["INVNO"] . "</td> 
			                    <td>" . $row_rsPrInv["invoice"] . "</td> 
			                    <td>" . $row_rsPrInv["PAY_TYPE"] . "</td> 
			                    <td  class='right'>" . number_format($row_rsPrInv['PAID'], 2, ".", ",") . "</td>"; 
                    $table .= "</tr>";
                    
                $SUPPLIER= $row_rsPrInv["CUSCODE"];
                    
                $PAID_TOT +=$row_rsPrInv['PAID'];
            }
         
    
    
                $table .= "<tr><td colspan=5>&nbsp;</td>
                    <th  class='right'>" . number_format($PAID, 2, ".", ",") . "</th></tr>";
                $table .= "<tr><td colspan=5>&nbsp;</td>
                    <th  class='right'>" . number_format($PAID_TOT, 2, ".", ",") . "</th></tr>";
            
        }
        
        if($_GET['type']=="Purchase_Wise"){
            
            $table .= "<table class='table'  >
		
          	<tr>
                <th class='center'  style='width: 150px;'>Purchase No</th>
                <th class='center'  style='width: 100px;'>Purchase Date</th> 
                <th class='center'  style='width: 350px;'>Supplier</th>  
                <th class='center'  style='width: 150px;'>Invoice No</th>  
                <th class='center'  style='width: 150px;'>Grand Total</th>  
                <th class='center'  style='width: 150px;'>Balance</th>  
                <th class='center'  style='width: 150px;'>Pay No</th> 
                <th class='center'  style='width: 100px;'>Pay Date</th> 
                <th class='center'  style='width: 150px;'>Pay Type</th>    	
                <th class='center'  style='width: 150px;'>Paid</th>    		
            </tr>";
     
            $sql_rsPrInv = "select  * from view_purmas_payinvdetails where    COMPANY = '" . $_SESSION['company'] .  "' and CANCELL='0' and CANCEL='0' AND SDATE >='" . $_GET["dtfrom"] . "' and SDATE <='" . $_GET["dtto"] . "'";
            
            if($_GET["c_code"] !=''){
                $sql_rsPrInv .= "   and SUP_CODE = '" . $_GET["c_code"].  "' " ; 
            }
            
            $sql_rsPrInv .= "   ORDER BY SDATE,REFNO,PAY_DATE,PAY_REFNO desc " ; 
             $i=0;
           
           
            
    $k = 0;
    $j = 0;
            foreach ($conn->query($sql_rsPrInv) as $row_rsPrInv) {
            
                if ($ORDNO != $row_rsPrInv["REFNO"]) {
                    
                     $sql_cbal = "select * from c_bal where REFNO='".$row_rsPrInv["REFNO"]."'";
                    $result_cbal = $conn->query($sql_cbal);  
                    $row_cbal = $result_cbal->fetch();
    
                        if ($k == 1) {
                            $table .= "<tr><th colspan=9></th>
                                    <td align=\"right\"><b >" . number_format($cashpay,2) . "</b></td></tr>";
                            $k = 0;
                        }
                        
                         
                        if ($k == 0) {
                            $cashpay=0;
                            $sdate = $row_rsPrInv["SDATE"];
                            $table .= "<tr>
                                                <td>" . $row_rsPrInv["REFNO"] . "</td>
                                                <td>" . $sdate . "</td>
                                                <td>" . $row_rsPrInv["SUP_CODE"] . " " . $row_rsPrInv["SUP_NAME"] . "</td>
                                                <td>" . $row_rsPrInv["pi_no"] . "</td>
                                                <td align=\"right\">" . number_format($row_rsPrInv["AMOUNT"],2) . "</td>
                                                <td align=\"right\">" . number_format($row_cbal["BALANCE"],2) . "</td>"; 
    
                            $ORDNO = $row_rsPrInv["REFNO"];
                            $k = 1;
                            $j = 0;
                        }
    
                       $cashpay =  $row_rsPrInv['PAID'];
                    }else{
                        $cashpay = $cashpay + $row_rsPrInv['PAID'];
                    }
    
                    if ($j != 0) {
                        $table .= "<tr>
                            <td colspan=6></td>";
                    } else {
                        $j = 1;
                    }
                    $table .= " <td>" . $row_rsPrInv["PAY_REFNO"] . "</td>
                                <td>" . $row_rsPrInv["PAY_DATE"] . "</td> 
                                <td>" . $row_rsPrInv["PAY_TYPE"] . "</td> 
                                <td align=right>" . number_format($row_rsPrInv['PAID'], 2, ".", ",")  . "</td> ";  
                          
              
                    $table .= "</tr>";
                    
                    
                    $cashpay_tot=$cashpay_tot+$row_rsPrInv['PAID'];
                }
                
             $table .= "<tr>
                            <td align=\"right\" colspan='9'><b></b></td>
                            <td align=\"right\"><b>" . number_format($cashpay, 2, ".", ","). "</b></td> 
                        </tr>";
        
        $table .= "<tr>
                            <td align=\"right\" colspan='9'><b>TOTAL</b></td>
                            <td align=\"right\"><b>" . number_format($cashpay_tot, 2, ".", ","). "</b></td> 
                        </tr>";
        }
      
      
      $table .= "</table> 
</body>
</html>";

    echo $table;

    