<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "Amount query";
$file = "amtrep_fr.php";

$zroot = $_SESSION['zdir'];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

if (isset($_POST["view"])) {
    $amount = $_POST["amount"];

    if (!empty($amount)) {
        $amtcond = " AND `amount`='$amount' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `actran` WHERE `bankinit` != '' 
        AND (`source` = 'BR' OR `source` = 'BP' OR `source` = 'TR')  $amtcond  group by source, vouchno ORDER BY  `chqdate`, `vouchno`, `trnno` ")or die(mysqli_error($conn));

//    print table ($getpmain) ;

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container" style="width:55%">
            <?php include ("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width="10%">Chq.Date</th>
                                <th width="13%">Cheque No.</th>
                                <th width="25%" >Bank Name</th>
                                <th width="25%" >Party Name</th>
                                <th width="8%" class="text-right">Amount</th>
                                <th width="5%" >Source</th>
                                <th width="8%" class="text-right">Voucher</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
//                            $ctr = 0;
//                            $totbalnce = 0;
//                            $totinw = 0;
//                            $totout = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                $source = $pmain['source'];
                                $amount = $pmain['amount'];
                                $amount1 = abs($amount);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$pmain[chqdate]</td>";
                                Echo "<td>$pmain[chqno]</td>";
                                if ($source == 'BR') {
                                    $getvouch = mysqli_query($conn, "SELECT * from `bankrecp` WHERE `vouchno` = '$pmain[vouchno]' ");
                                    $fetvouch = mysqli_fetch_array($getvouch);
                                    $bcode    = $fetvouch['bankname'];
                                    $acode    = $fetvouch['party'];
                                    
                                    $getbank = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $bcode ");
                                    $fetbank = mysqli_fetch_array($getbank);
                                    $bname   = $fetbank['name'];

                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $acode ");
                                    $fetparty = mysqli_fetch_array($getparty);
                                    $pname    = $fetparty['name'];
                                } elseif ($source == 'BP') {
                                    $getvouch = mysqli_query($conn, "SELECT * from `bankpay` WHERE `vouchno` = '$pmain[vouchno]' ");
                                    $fetvouch = mysqli_fetch_array($getvouch);
                                    $bcode    = $fetvouch['bankname'];
                                    $acode    = $fetvouch['party'];
                                    
                                    $getbank = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $bcode ");
                                    $fetbank = mysqli_fetch_array($getbank);
                                    $bname   = $fetbank['name'];

                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $acode ");
                                    $fetparty = mysqli_fetch_array($getparty);
                                    $pname    = $fetparty['name'];
                                } else {
                                    $getvouch = mysqli_query($conn, "SELECT * from `contra` WHERE `vouchno` = '$pmain[vouchno]' ");
                                    $fetvouch = mysqli_fetch_array($getvouch);
                                    $bcode    = $fetvouch['frombc'];
                                    $acode    = $fetvouch['tobc'];
                                    
                                    $getbank = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $bcode ");
                                    $fetbank = mysqli_fetch_array($getbank);
                                    $bname   = $fetbank['name'];

                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $acode ");
                                    $fetparty = mysqli_fetch_array($getparty);
                                    $pname    = $fetparty['name'];                                    
                                }
                                Echo "<td >$bname</td>";
                                Echo "<td >$pname</td>";
                                Echo "<td class='text-right' >$amount1</td>";
                                Echo "<td>$source</td>";
                                Echo "<td class='text-right' >$pmain[vouchno]</td>";
                                Echo "</tr>";

//                                $totbalnce = $totbalnce + abs($balnce);1
//                                $totinw = $totinw + $inwqty;
//                                $totout = $totout + $outqty;
                            }
//                            $colr = "blue";
//                            Echo "<tr id='boldfont'>";
//                            Echo "<td style='color: $colr'><h4><b>Total</b></h4></td>";
//                            Echo "<td></td>";
//                            Echo "<td style='color: $colr' class='text-right'><h4><b>$inwqty</b></h4></td>";
//                            Echo "<td style='color: $colr' class='text-right'><h4><b>$outqty</b></h4></td>";
//                            Echo "<td></td>";
//                            Echo "<td style='color: $colr' class='text-right' ><h4><b></b></h4></td>";
//                            Echo "<td style='color: $colr' class='text-right' ><h4><b>$totbalnce</b></h4></td>";
//                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $amount = $_POST["amount"];

    if (!empty($amount)) {
        $amtcond = " AND `amount`='$amount' ";
    }

    $getpmain = mysqli_query($conn, "SELECT * FROM `actran` WHERE `bankinit` != '' 
        AND (`source` = 'BR' OR `source` = 'BP' OR `source` = 'TR')  $amtcond ORDER BY  `chqdate`, `vouchno`, `trnno` ")or die(mysqli_error($conn));


    $contents .= "Chq.Date,Cheque No.,Bank/Party Name,Amount,Voucher\n";

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $getpname = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $pmain[accode] ");
        $pname = mysqli_fetch_array($getpname);
        $amount = $pmain['amount'];
        $amount1 = abs($amount);

        $ctr++;
        $pchqdate = $pmain['chqdate'];
        $pchqno = $pmain['chqno'];
        $pname = $pname['name'];
        $pamount = $amount1;
        $pvouchno = $pmain['vouchno'];

        $contents .= $pchqdate . ",";
        $contents .= $pchqno . ",";
        $contents .= $pname . ",";
        $contents .= $pamount . ",";
        $contents .= $pvouchno . "\n";
    }
    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
if (isset($_POST["subpdf"])) {
    $amount = $_POST["amount"];

    if (!empty($amount)) {
        $amtcond = " AND `amount`='$amount' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `actran` WHERE `bankinit` != '' 
        AND (`source` = 'BR' OR `source` = 'BP' OR `source` = 'TR')  $amtcond ORDER BY  `chqdate`, `vouchno`, `trnno` ")or die(mysqli_error($conn));

    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('P', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(30, 7, "Chq. Date", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Cheque No.", 1, 0, '', 1);
    $pdf->Cell(73, 7, "Bank Party Name", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Amount", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Voucher", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $getpname = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = $pmain[accode] ");
        $pname = mysqli_fetch_array($getpname);
        $amount = $pmain['amount'];
        $amount1 = abs($amount);
        $ctr++;
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(30, 7, "$pmain[chqdate]", 1);
        $pdf->Cell(30, 7, "$pmain[chqno]", 1);
        $pdf->Cell(73, 7, "$pname[name]", 1);
        $pdf->Cell(30, 7, "$amount1", 1);
        $pdf->Cell(30, 7, "$pmain[vouchno]", 1);
        $pdf->Ln();
    }
    $pdf->Output("Amount Query.pdf", "D");
}
?>