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

$maindb = "";
$trandb = "";
$formname = "Expense Form";
$file = "expreg_fr.php";

$zroot = 'apps4rea_epi_root';

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

if (isset($_POST["view"])) {
    $code = $_POST["code"];
    $school = $_POST["name"];
    $medium = $_POST["medmcode"];
    $exam = $_POST["examcode"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $stdepot = $_POST["stdepot"];

    $dtypecond = "";
    $mscopecond = "";

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);




    if (!empty($code)) {
        $codecond = " AND `custcode`= '$code' ";
    }
    if (!empty($school)) {
        $schoolcond = " AND `custcode`= '$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($stdepot)) {
        $stdepotcond = " AND `session`= '$stdepot' ";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    }
    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $codecond $schoolcond $mediumcond $examcond $sectioncond $sessioncond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg" style="font-size:20px;">EPI WEB</label>
                </div>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()" class="btn btn-primary">GO Back</a>
                </div>
            </div>
            <div class='row'>
                <div class='col-lg-2'>
                    <label class="label-control marg">From Date - </label>
                    <label class="label-control marg"><?php Echo $mfrmdate; ?></label>
                </div>
                <div class='col-lg-2'>
                    <label class="label-control marg">From Date - </label>
                    <label class="label-control marg"><?php Echo $mtodate; ?></label>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:10%">Date</th>
                                <th style="width:11%">Code</th>
                                <th style="width:15%">School Name</th>
                                <th style="width:11%">Medium</th>
                                <th style="width:15%">Exam</th>
                                <th style="width:15%">Section</th>
                                <th style="width:11%">Session</th>
                                <th style="width:11%">St Depot</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

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

                                $gschool = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`= $pmain[accode] AND grpcode = '010'")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));
                                $dexam = mysqli_fetch_array($gexam);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[custcode]</td>";
                                Echo "<td>$sschool[name]</td>";
                                if ($pmain[section] == P) {
                                    $sectn = Primary;
                                } else if ($pmain[section] == S) {
                                    $sectn = Secondry;
                                } else {
                                    $sectn = Preliums;
                                }
                                Echo "<td>$sectn</td>";
                                if ($pmain[session] == M) {
                                    $sessn = Morning;
                                } else {
                                    $sesn = Afternoon;
                                }
                                Echo "<td>$sessn</td>";
                                Echo "<td>$smedm[name]</td>";
                                Echo "<td>$dexam[name]</td>";
                                Echo "</tr>";
                                if ($_POST["dtype"] == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th style='background-color: #cccccc;width:10%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:30%;'>Subject</th>";
                                    Echo "<th style='background-color: #cccccc;width:15%;' class='text-right'>First</th>";
                                    Echo "<th style='background-color: #cccccc;width:15%;' class='text-right'>Second</th>";
                                    Echo "<th style='background-color: #cccccc;width:15%;' class='text-right'>Third</th>";
                                    Echo "<th style='background-color: #cccccc;width:15%;' class='text-right'>Fourth</th>";
                                    Echo "<tr>";


                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                    $ctr2 = 0;
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td class=''>$tran[subjname]</td>";
                                        Echo "<td class='numeric' 'text-right'>$tran[pq1]</td>";
                                        Echo "<td class='numeric' 'text-right'>$tran[pq2]</td>";
                                        Echo "<td class='numeric' 'text-right'>$tran[pq3]</td>";
                                        Echo "<td class='numeric' 'text-right'>$tran[pq4]</td>";
                                        Echo "</tr>";
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

if (isset($_POST["excel"])) {
    $code = $_POST["code"];
    $school = $_POST["name"];
    $medium = $_POST["medmcode"];
    $exam = $_POST["examcode"];
    $section = $_POST["section"];
    $session = $_POST["session"];

    $dtypecond = "";
    $mscopecond = "";

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);




    if (!empty($code)) {
        $codecond = " AND `custcode`= '$code' ";
    }
    if (!empty($school)) {
        $schoolcond = " AND `custcode`= '$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["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $codecond $schoolcond $mediumcond $examcond $sectioncond $sessioncond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    $contents.= "Sr.No,Date,Bill.No,Code,School Name,Section,Session,Medium,Exam,\n";

    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $gschool = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`= $pmain[accode] AND grpcode = '010'")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));
        $dexam = mysqli_fetch_array($gexam);

        $pmydt = $pmain[mydt];
        $pvouchno = $pmain[vouchno];
        $pccode = $pmain[custcode];
        $schname = $sschool[name];
        $sname = $smedm[name];
        $dname = $dexam[name];

        $contents.= $ctr . ",";
        $contents.= $pmydt . ",";
        $contents.= $pvouchno . ",";
        $contents.= $pccode . ",";
        $contents.= $schname . ",";
        if ($pmain[section] == P) {
            $sectn = Primary;
        } else if ($pmain[section] == S) {
            $sectn = Secondry;
        } else {
            $sectn = Preliums;
        }
        $contents.= $sectn . ",";
        if ($pmain[session] == M) {
            $sessn = Morning;
        } else {
            $sesn = Afternoon;
        }
        $contents.= $sessn . ",";
        $contents.= $sname . ",";
        $contents.= $dname . "\n";
        $ctr++;

        if ($_POST["dtype"] == 2) {

            $contents.= "Sr.No,Subject,First,Second,Thirf,Fourth,\n";

            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
            $ctr2 = 1;
            while ($tran = mysqli_fetch_array($gettran)) {

                $tsubname = $tran[subjname];
                $tran1 = $tran[pq1];
                $tran2 = $tran[pq2];
                $tran3 = $tran[pq3];
                $tran4 = $tran[pq4];

                $contents.= $ctr2 . ",";
                $contents.= $tsubname . ",";
                $contents.= $tran1 . ",";
                $contents.= $tran2 . ",";
                $contents.= $tran3 . ",";
                $contents.= $tran4 . "\n";
                $ctr2++;
            }
        }
    }

    $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;
    }
}
?>

