<?php
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 = 'Packet Report';

if (isset($_POST["view"])) {

    $school = $_POST["name"];
    $center = $_POST["center"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $exam = $_POST["exam"];
    $medium = $_POST["medium"];

    $sessncond = '';
    $sectcond = '';
    $schlcond = '';
    $centcond = '';
    $examcond = '';

    if (!empty($exam)) {
        $examcond = " AND `examcode` = '$exam' ";
    }

    if (!empty($medium)) {
        $medmcond = " AND `medmcode` = '$medium' ";
    }

    if (!empty($school)) {
        $schlcond = " AND `accode` = '$school' ";
    }

    if (!empty($session)) {
        $sessncond = " AND `session` = '$session' ";
    }

    if (!empty($section)) {
        $sectcond = " AND `section` = '$section' ";
    }

    $gmain = mysqli_query($conn, "SELECT * from `demdmain` where `vouchno` != ''  and year = '$myyear'
            $examcond $medmcond $schlcond $sessncond $sectcond order by medmcode, examcode ")or die(mysqli_error($conn));


    $getno = mysqli_num_rows($gmain);

    if ($getno == 0) {
        browse("No Record Found");
        echo "<script>window.close()</script>";
    }

    $comp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
    $zcomp = mysqli_fetch_assoc($comp);

    include("includes/linkshead.php");
    ?>
    <link href="MyStyle/rep_format.css" rel="stylesheet">
    <style>

        body{
            font-size: 12px;

        }
        .mtable{
            border:1px solid black;
            boder-collapse:collapse;
        }
        .mtd{
            border:1px solid black;
            boder-collapse:collapse;
            text-align:right;
        }

        td,th,.t4{
            padding:0px;
            font-size:12px;
            font-size:12px;
        }

        .padd{
            padding:7px;
        }

        .border{
            border-top-style:solid;
            border-right-style:none;
            border-left-style:none;
            border-bottom-style:solid;
            border-width:2px;
        }
        .border2{
            border-top-style:dotted;
            border-right-style:none;
            border-left-style:none;
            border-bottom-style:dotted;
            border-width:1px;
        }

        table{
            width:100%;
        }
        hr { 
            border-style: inset;
            border-width: 2px;
        } 

    </style>
    </head>
    <body>    
        <div class="page">
            <div class="col-lg-11"></div>
            <div class="col-lg-1">
                <a  class="btn btn-primary goback"  onclick="window.close()">Go Back</a>
            </div>

            <!--<div class="subpage" style="margin-top:5px;width:100%;">-->
            <div class="row">
                <?php
                echo "<h2 class='text-center'>$zcomp[compname]</h2>";
                echo "<h4 class='text-center'>$zcomp[add1] $zcomp[add1] </h4>";
                echo "<h4 class='text-center'>$zcomp[add3]</h4>";
                ?>
            </div>

            <table style="width:100%">
                <tr>
                    <td style="width:30%"><h4><b>PACKET REPORT</b></h4></td>
                    <td style="width:70%"></td>
                </tr>
            </table>
            <?php
            $tmedmcode = '';
            $texamcode = '';

            $medmexam = '';


            $totpackmor = $totpackaft = $gtotpack = 0;

            while ($fetmain = mysqli_fetch_array($gmain)) {

                $medmcode = $fetmain['medmcode'];
                $examcode = $fetmain['examcode'];
                $schl = $fetmain['accode'];
                $section = $fetmain['section'];

                $medmexam = $medmcode . $examcode;

                if ($medmexam != $tmedmexam) {

                    if ($gtotpack != '') {
                        echo "<tr>";
                        echo "<td class='padd'></td>";
                        echo "<td class='padd'></td>";
                        echo "<td style='text-align:right;  '  class='padd'><b>Total</b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b>$totpackmor</b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b>$totpackaft</b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b>$gtotpack</b></td>";
                        echo "</tr>";
                    }
                    $totpackmor = $totpackaft = $gtotpack = 0;


                    $medmqry = mysqli_query($conn, "SELECT * from `medium` where `code` = '$medmcode' ") or die(mysqli_error($conn));
                    $medmfet = mysqli_fetch_array($medmqry);
                    $medmname = $medmfet['name'];
                    $tmedmcode = $medmfet['code'];

                    $examqry = mysqli_query($conn, "SELECT * from `exam` where `code` = '$examcode' ") or die(mysqli_error($conn));
                    $examfet = mysqli_fetch_array($examqry);
                    $examname = $examfet['name'];
                    $texamcode = $examfet['code'];

                    $tmedmexam = $medmexam;
                    ?>
                    <table>
                        <br>
                        <tr> 
                            <td style="width:30%"><h4><b>Medium :<?php echo $medmname; ?></b></h4></td>
                            <td style="width:70%"><h4><b>Exam :<?php echo $examname; ?></b></h4></td>
                        </tr>
                    </table>
                    <table>
                        <tr class="border">
                            <td style="width:5%;" class="padd"><b>Sr.No.</b></td>
                            <td style="width:10%; text-align:center" class="padd"><b>Code</b></td>
                            <td style="width:41%" class="padd"><b>School Name</b></td>
                            <td style="width:14%; text-align:center" class="padd"><b>Morning Session</b></td>
                            <td style="width:15%; text-align:center" class="padd"><b>Afternoon Session</b></td>
                            <td style="width:15%; text-align:right" class="padd"><b>Total Packets</b></td>
                        </tr>

                        <?php
                        $ctr = 0;
                    }

                    $packqry = mysqli_query($conn, "SELECT a.accode, a.custcode, b.name from `demdmain` a, accmast b where a.accode = b.accode and 
                                        a.`examcode` = '$examcode' and a.`medmcode` = '$medmcode' and a.`accode` = '$schl' and
                                        a.`section` = '$section' group by a.accode ")or die(mysqli_error($conn));
                    $ctr = $ctr + 1;

//                    $packtot = $totpackmor = $totpackaft = $gtotpack = 0;
                    while ($fetpack = mysqli_fetch_array($packqry)) {
                        $accode = $fetpack['accode'];

                        echo "<tr>";
                        echo "<td class='padd'>$ctr</td>";
                        echo "<td  style='text-align:center'class='padd'>$fetpack[custcode]</td>";
                        echo "<td class='padd'>$fetpack[name]</td>";


                        $sessmor = mysqli_query($conn, "SELECT `nopack` from `demdmain` where accode = '$accode' and 
                                                    `examcode` = '$examcode' and `medmcode` = '$medmcode' and `session` = 'M'") or die(mysqli_error($conn));
                        $fetmor = mysqli_fetch_array($sessmor);
                        $packmor = $fetmor['nopack'];

                        $sessaft = mysqli_query($conn, "SELECT `nopack` from `demdmain` where accode = '$accode' and 
                                                    `examcode` = '$examcode' and `medmcode` = '$medmcode' and `session` != 'M'") or die(mysqli_error($conn));
                        $fetaft = mysqli_fetch_array($sessaft);
                        $packaft = $fetaft['nopack'];

                        if ($packmor < 5) {
                            $packmor = 0;
                        }
                        if ($packaft < 5) {
                            $packaft = 0;
                        }

                        $packtot = $packmor + $packaft;

                        echo "<td  style='text-align:center' class='padd'>$packmor</td>";
                        echo "<td  style='text-align:center' class='padd'>$packaft</td>";
                        echo "<td  style='text-align:center' class='padd'>$packtot</td>";
                        echo "</tr>";

                        $totpackmor = $totpackmor + $packmor;
                        $totpackaft = $totpackaft + $packaft;
                        $gtotpack = $gtotpack + $packtot;
                    }
                }
                echo "<tr>";
                echo "<td class='padd'></td>";
                echo "<td class='padd'></td>";
                echo "<td style='text-align:right'  class='padd'><b>Total</b></td>";
                echo "<td style='text-align:center' class='padd border2'><b>$totpackmor</b></td>";
                echo "<td style='text-align:center' class='padd border2'><b>$totpackaft</b></td>";
                echo "<td style='text-align:center' class='padd border2'><b>$gtotpack</b></td>";
                echo "</tr>";
                ?>
                <?php
                ?>
            </table>

        </div>
        <!--</div>-->
        <?php include('includes/linksfoot.php'); ?>
    </body>
    </html>
<?php } ?>