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

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

$maindb = 'demdmain';
$trandb = 'demdtran';

$zroot = $_SESSION["zdbroot"];
$formname = 'Unsetlled Receipt';
$file = 'rgbillpend_fr.php';
$zroot = $_SESSION['zdir'];

    $code = $_POST["custcode"];
    $school = $_POST["name"];
    $medium = $_POST["medmcode"];
    $exam = $_POST["exam"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $dtye = $_POST["dtype"];

    if (!empty($code)) {
        $codecond = " AND `custcode` = '$code' ";
    }
    if (!empty($school)) {
        $schoolcond = " AND `accode` = '$school' ";
    }
    if (!empty($medium)) {
        $mediumcond = " AND `medmcode` = '$medium '";
    }
    if (!empty($exam)) {
        $examcond = " AND `examcode` = '$exam' ";
    }
    if (!empty($section)) {
        $sectioncond = " AND `section` = '$section'";
    }
    if (!empty($session)) {
        $sessioncond = " AND `session` = '$session'";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    $getpmain = mysqli_query($conn, "(SELECT vouchno, 'BR' as source, party, custcode, examcode, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, amount FROM `bankrecp` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate' and chqrtn != 1 AND concat(vouchno,'BR')  
                            not in (select concat(bnkvouch,source) from billsett ) order by vouchdt)
                            union all 
                            (SELECT vouchno, 'CR' as source, party, custcode,  examcode,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt , amount FROM `cashrecp` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate'  AND concat(vouchno,'CR')
                            not in (select concat(bnkvouch,source) from billsett ) order by vouchdt) ") or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr</th>
                                <th style="width:8%">Date</th>
                                <th style="width:10%">Voucher</th>
                                <th style="width:6%">Code</th>
                                <th style="width:25%">School Name</th>
                                <th style="width:10%">Section</th>
                                <th style="width:10%">Session</th>
                                <th style="width:9%">Medium</th>
                                <th style="width:10%">Exam</th>
                                <th style="width:8%">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamount = 0;

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

                                $gschool = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
                                $sschool = mysqli_fetch_array($gschool);

                                $gmedm = mysqli_query($conn, "SELECT `code`,`name` FROM `medium` WHERE `code`= '$pmain[medmcode]' ")or die(mysqli_error($conn));
                                $smedm = mysqli_fetch_array($gmedm);

                                $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code`= '$pmain[examcode]' ")or die(mysqli_error($conn));
                                $fexam = mysqli_fetch_array($gexam);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[source] " . ' ' . "$pmain[vouchno]</td>";
                                Echo "<td>$pmain[custcode]</td>";
                                Echo "<td>$sschool[name]</td>";

                                if ($pmain['section'] == 'P') {
                                    $sectn = 'Primary';
                                } elseif ($pmain['section'] == 'S') {
                                    $sectn = 'Secondry';
                                } else {
                                    $sectn = 'Prelims';
                                }
                                Echo "<td>$sectn</td>";
                                if ($pmain['session'] == 'M') {
                                    $sessn = 'Morning';
                                } else {
                                    $sessn = 'Afternoon';
                                }
                                Echo "<td>$sessn</td>";
                                Echo "<td>$smedm[name]</td>";
                                Echo "<td>$fexam[name]</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
                                Echo "</tr>";
                                $totamount=$totamount+$pmain['amount'];
                            }
                            ?>

                        </tbody>
                        <tbody>
                            <tr>
                                <td colspan="10" class="text-right" style="color:red;font-size: 14px;"><b><?=$totamount?></b></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
//
//    $code = $_POST["custcode"];
//    $school = $_POST["name"];
//    $medium = $_POST["medmcode"];
//    $exam = $_POST["exam"];
//    $section = $_POST["section"];
//    $session = $_POST["session"];
//    $dtye = $_POST["dtype"];
//
//    if (!empty($code)) {
//        $codecond = " AND `custcode` = '$code' ";
//    }
//    if (!empty($school)) {
//        $schoolcond = " AND `accode` = '$school' ";
//    }
//    if (!empty($medium)) {
//        $mediumcond = " AND `medmcode` = '$medium '";
//    }
//    if (!empty($exam)) {
//        $examcond = " AND `examcode` = '$exam' ";
//    }
//    if (!empty($section)) {
//        $sectioncond = " AND `section` = '$section'";
//    }
//    if (!empty($session)) {
//        $sessioncond = " AND `session` = '$session'";
//    }
//
//    if (empty($_POST["frmdt"])) {
//        browse("From Date Cannot Be Empty");
//        header("refresh:1; saleitemgrp.php");
//    }
//    if (empty($_POST["todt"])) {
//        browse("To Date Cannot Be Empty");
//        header("refresh:1; saleitemgrp.php");
//    }
//
//    $mfrmdate = trim($_POST["frmdt"]);
//    $mtodate = trim($_POST["todt"]);
//
//    $mfdt = date("d-m-Y", strtotime($mfrmdate));
//    $mtdt = date("d-m-Y", strtotime($mtodate));
//
//    $getpmain = mysqli_query($conn, "(SELECT vouchno, 'BR' as source, party, custcode, examcode, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankrecp` WHERE `vouchdt` >= '$mfrmdate' 
//                             AND `vouchdt` <= '$mtodate' AND vouchno
//                            not in (select bnkvouch from billsett ) order by vouchdt)
//                            union all 
//                            (SELECT vouchno, 'BR' as source, party, custcode,  examcode,  DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `cashrecp` WHERE `vouchdt` >= '$mfrmdate' 
//                             AND `vouchdt` <= '$mtodate' AND vouchno
//                            not in (select bnkvouch from billsett ) order by vouchdt) ") or die(mysqli_error($conn));
//    $get = mysqli_num_rows($getpmain);
//
//
//    $comp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
    $zcomp = mysqli_fetch_assoc($comp);

    $contents .= "Sr.,Date,Bill.No,Code,School Name,Section,Session,Medium,Exam,\n";

    $ctr = 0;

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

        $gschool = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $sschool = mysqli_fetch_array($gschool);

        $gmedm = mysqli_query($conn, "SELECT `code`,`name` FROM `medium` WHERE `code`= '$pmain[medmcode]' ")or die(mysqli_error($conn));
        $smedm = mysqli_fetch_array($gmedm);

        $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code`= '$pmain[examcode]' ")or die(mysqli_error($conn));
        $fexam = mysqli_fetch_array($gexam);

        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $pmain['source'] . ' ' . $pmain['vouchno'] . ",";
        $contents .= $pmain['custcode'] . ",";
        $contents .= $sschool['name'] . ",";

        if ($pmain['section'] == 'P') {
            $sectn = Primary;
        } elseif ($pmain['section'] == 'S') {
            $sectn = Secondry;
        } else {
            $sectn = Prelims;
        }
        $contents .= $sectn . ",";

        if ($pmain['session'] == 'M') {
            $sessn = Morning;
        } else {
            $sessn = Afternoon;
        }

        $contents .= $sessn . ",";
        $contents .= $smedm['name'] . ",";
        $contents .= $fexam['name'] . "\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"])) {
    $code = $_POST["custcode"];
    $school = $_POST["name"];
    $medium = $_POST["medmcode"];
    $exam = $_POST["exam"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $dtye = $_POST["dtype"];

    if (!empty($code)) {
        $codecond = " AND `custcode` = '$code' ";
    }
    if (!empty($school)) {
        $schoolcond = " AND `accode` = '$school' ";
    }
    if (!empty($medium)) {
        $mediumcond = " AND `medmcode` = '$medium '";
    }
    if (!empty($exam)) {
        $examcond = " AND `examcode` = '$exam' ";
    }
    if (!empty($section)) {
        $sectioncond = " AND `section` = '$section'";
    }
    if (!empty($session)) {
        $sessioncond = " AND `session` = '$session'";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    }
    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; saleitemgrp.php");
    }

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND concat(custcode, section , examcode ,medmcode , session)
                            not in(select concat(custcode, section , examcode ,medmcode , session) from invmain )
                            $codecond $schoolcond $mediumcond $examcond $sectioncond $sessioncond ")or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);

    $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code`= '$exam' ")or die(mysqli_error($conn));
    $dexam = mysqli_fetch_array($gexam);

    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('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
    $pdf->Cell(20, 7, "Date", 1, 0, '', 1);
    $pdf->Cell(20, 7, "Bill. No.", 1, 0, '', 1);
    $pdf->Cell(20, 7, "Code", 1, 0, '', 1);
    $pdf->Cell(90, 7, "School Name", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Section", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Session", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Medium", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Exam", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

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

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

        $gschool = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $sschool = mysqli_fetch_array($gschool);

        $gmedm = mysqli_query($conn, "SELECT `code`,`name` FROM `medium` WHERE `code`= '$pmain[medmcode]' ")or die(mysqli_error($conn));
        $smedm = mysqli_fetch_array($gmedm);

        $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code`= '$pmain[examcode]' ")or die(mysqli_error($conn));
        $fexam = mysqli_fetch_array($gexam);

        $ctr++;
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(8, 7, "$ctr", 1);
        $pdf->Cell(20, 7, "$pmain[mydt]", 1);
        $pdf->Cell(20, 7, "$pmain[vouchno]", 1);
        $pdf->Cell(20, 7, "$pmain[custcode]", 1);
        $pdf->Cell(90, 7, "$sschool[name]", 1);
        if ($pmain['section'] == 'P') {
            $sectn = 'Primary';
        } elseif ($pmain['section'] == 'S') {
            $sectn = 'Secondry';
        } else {
            $sectn = 'Prelims';
        }
        $pdf->Cell(30, 7, "$sectn", 1);
        if ($pmain['session'] == 'M') {
            $sessn = 'Morning';
        } else {
            $sessn = 'Afternoon';
        }
        $pdf->Cell(30, 7, "$sessn", 1);
        $pdf->Cell(30, 7, "$smedm[name]", 1);
        $pdf->Cell(30, 7, "$fexam[name]", 1);
        $pdf->Ln();
    }
    $pdf->Output("Pending Bills.pdf", "D");
}
?>
