<?php
session_start();
include './connection_sql.php';
if ($_SESSION["CURRENT_USER"] == "") { 
    exit('Please Logging Again..');
} 
if ($_SESSION['company'] == "") { 
    exit('Please Logging Again..');
} 
 
date_default_timezone_set('Asia/Colombo');

$html = "<!DOCTYPE html>
    <head>
        <meta charset='utf-8' />
        <title>Payment Entry</title>
        <style>
            .spn {
                border-top: 1px solid !important; 

            }
            .box {
                border : 1px solid !important;
            }
        </style>
        <style>


            .bottom  {
                border-bottom: 1px solid #000000;
            }
            .table1 {
                border-collapse: collapse;
            }
            .table1, td, th {

                font-family: Arial, Helvetica, sans-serif;
                padding: 5px;
            }
            .table1 th {
                font-weight: bold;
                font-size: 12px;
            }
            .table1 td {
                font-size: 12px;
                border-bottom: none;
                border-top: none;
            }

            .head {
                font-size: 15px !important;
            }

            p {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
            } 
             .right {
            text-align: right;
        }  
        .left {
            text-align: left;
        }
        .center {
            text-align: center;
        }
        </style>
    </head>";


include './connection_sql.php';
$sql = "Select * from a_paymas where tmp_no='" . $_GET['tmp_no'] . "' and COMPANY='" . $_SESSION["company"] . "'";
$result = $conn->query($sql);

if (!$row = $result->fetch()) {
    exit();
}

 
            
$sql_invpara = "SELECT * from invpara where flag='".$_SESSION['company']."'";  
$result_invpara = $conn->query($sql_invpara);
if (!$row_invpara = $result_invpara->fetch()) {
    exit();
}
$sql = "Select * from view_ledger where l_refno='" . $row['REFNO'] . "' and l_flag1='CRE'";
$resultb = $conn->query($sql);

if ($rowb = $resultb->fetch()) {
    //exit();
}


$sql_ven = "Select * from view_vendor where c_code='" . $row['VEN_CODE'] . "' ";
$result_ven = $conn->query($sql_ven);
$row_ven = $result_ven->fetch();
//<tr>
//                 <th class='bottom head' align='center' >Tel  : " . $row_invpara['TELE'] . " </th>
//            </tr>
$html .= "<body> 
        

        <table   style='width: 660px;' class='table1'>

            <tr>
                 <th class='bottom head' colspan='3'>" . $row_invpara['COMPANY'] . "</th> 
            </tr>
    
            <tr>               
               <th align='center' colspan='4'>" . $row_invpara['ADD1'] . " " . $row_invpara['ADD2'] . ", TEL: " . $row_invpara['TELE'] . "</th> 
            </tr>

            

            <tr>
              <th colspan='4' ><center style='font-size:15px;'>PAYMENT VOUCHER</th>
               
            </tr>
            </table>
            <table   style='width: 660px;' class='table1'>

            <tr>
                <th  align=right>To</th>
                <td  align=left>" . $row['Barer'] . "</td>
                <th  align=right>Voucher No</th>   
                <td  align=right>" . $row['REFNO'] . "</td>   
            </tr>

            <tr>
                <th  align=right>Address</th>
                <td  align=left>" . $row_ven['c_add'] . "</td>
                <th  align=right>Date</th>   
                <td  align=right>" . $row['BDATE'] . "</td>   
            </tr>
            <tr>
                <th  align=right>Bank</th>
                <td  align=left>" . $rowb['C_NAME'] . "</td>
                <th  align=right>Pay Type</th>
                <td  align=right>" . $row['PAY_TYPE'] . "</td>
                
            </tr>";
            
            if($row['PAY_TYPE']=='CHEQUE'){
            $html .= " <tr>
                <th align=right>Cheque No</th>   
                <td align=left>" . $row['CHENO'] . "</td>   
                <th align=right>Cheque Date</th>   
                <td align=right>" . $row['chqdt'] . "</td>   
            </tr>";
            }
            
             $html .= " <tr>
                <th  align=right>Naration</th>   
                <td  align=left>" . $row['NARATION'] . "</td>   
            </tr>";
$html .= "</table>";
$html .= "<table style='width: 660px;'  border=1 class='table1'>
            
           <tr>
                <th  style='width: 100px;'>Code</th>
                <th style='width: 360px;'>Naration</th>
                <th style='width: 80px;'>Pay Amount</th>
           </tr>";


$mtot = 0;
$sql = "select * from a_ledger where l_refno = '" . $row['REFNO'] . "' and l_flag1 = 'DEB'";
foreach ($conn->query($sql) as $row1) {
    $html .= "<tr>
        <td>" . $row1['L_CODE'] . "</td>
        <td>" . wordwrap($row1['L_LMEM'] , 120, "<br>\n<br>\n") . "</td>
        <td class='right'>" . number_format($row1['L_AMOUNT'], 2, ".", ",") . "</td>
        </tr>";
    $mtot = $mtot + $row1['L_AMOUNT'];
}


$html .= "<tr><th  colspan='2' class='right'>Total </th><th  class='right'>" . number_format($mtot, 2, ".", ",") . "</th>";

  
$html .= "</table><br>";

$sql3 = "select * from a_pay_invdetails where REFNO = '" . $row['REFNO'] . "' ";
$result3 = $conn->query($sql3);
if ($row3 = $result3->fetch()) {

    $html .= "<table style='width: 660px;'  border=1 class='table1'>
                
               <tr><th  style='width: 100px;'>Purchase No</th>
               <th style='width: 360px;'>Invoice No</th>
               <th style='width: 80px;'>Pay Amount</th></tr>";
    
    
    $mtot1=0;
    $sql3 = "select * from a_pay_invdetails where REFNO = '" . $row['REFNO'] . "' ";
    foreach ($conn->query($sql3) as $row3) {
        $html .= "<tr>
            <td>" . $row3['INVNO'] . "</td>
            <td>" . $row3['invoice'] . "</td>
            <td class='right'>" . number_format($row3['PAID'], 2, ".", ",") . "</td>
            </tr>";
            
            $mtot1 = $mtot1 + $row3['PAID'];
    }
    
    $html .= "<tr><th  colspan='2'> </th><th  class='right'>" . number_format($mtot1, 2, ".", ",") . "</th>";
    
    
    $html .= "</table><br>";

}
$html .= " <table style='width: 660px;' class='table1'>
             
            <tr>
                <td class='spn'>Prepared By</td>
                <td>&nbsp;</td>
                <td class='spn'>Authorized By</td>
                <td>&nbsp;</td>
                <td class='spn'>Received By</td>
            </tr>
        </table>";


require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

echo $html;


if ($_GET['action'] == "save") {
////
// include autoloader

//     $dompdf->loadHtml($html);



// // Render the HTML as PDF
//     $dompdf->render();

// // Output the generated PDF to Browser
//     $pdf = $dompdf->output();
//     $pdf_name = str_replace("/", "-", intval(substr($row['REFNO'], 5, 8))) . "_" . (date('Ymd g i'));


//     $file_location = "pdfReports/" . $pdf_name . ".pdf";
//     file_put_contents($file_location, $pdf);

//     $sql = "insert into a_docs (loc,file_name,user_nm,folder,las_modifi,loc1,loc2,loc3,refno) values ('" . $file_location . "','" . $pdf_name . ".pdf" . "','','','" . date('Y-m-d') . "','','','','" . $row['REFNO'] . "')";
//     $result = $conn->query($sql);
}
