<?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 = 'School Strenght For Printers';

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

    $exam = $_POST["exam"];
    $medium = $_POST["medium"];

    $medmcond = '';
    $examcond = '';

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

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



    $gmain = mysqli_query($conn, "SELECT a.*, b.srno, b.subjcode, b.subjname,sum(b.pq1 + b.pq2 + b.pq3 + b.pq4) as psum,
                          sum(b.pq5 + b.pq6 + b.pq7 + b.pq8 + b.pq9 + b.pq10) AS ssum, SUM(b.pqrelm) AS msum 
                          from `demdmain` a, `demdtran` b where a.vouchno = b.vouchno $examcond $medmcond 
                          group by a.examcode, a.medmcode, b.subjcode order by a.examcode, a.medmcode, b.subjcode")
            or die(mysqli_error($conn));

    $getno = mysqli_num_rows($gmain);

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



    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;
        } 
        .center{
            text-align: center;
        }
        .right{
            text-align: right;
        }

    </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 "<h3 class='text-center'><b>$companyname</b></h3>";
                ?>
            </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
            $ctr = 0;
            $totctr = 0;
            $texamcode = '';
            $tmedmcode = '';

            $totpmor = $totpaft = $totsmor = $totsaft = $totplmor = $totplaft = $grtot = 0; 

            while ($fetmain = mysqli_fetch_array($gmain)) {
                $totctr++;

                $mexamcode = $fetmain['examcode'];
                $mmedmcode = $fetmain['medmcode'];

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

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


                if ($fetmain[session] == 'M' && $fetmain[section] == 'P') {
                    $mprimmor = $fetmain['psum'];
                }

                if ($fetmain[session] == 'A' && $fetmain[section] == 'P') {
                    $mprimaft = $fetmain['psum'];
                }

                if ($fetmain[session] == 'M' && $fetmain[section] == 'S') {
                    $msecmor = $fetmain[ssum];
                }

                if ($fetmain[session] == 'A' && $fetmain[section] == 'S') {
                    $msecaft = $fetmain[ssum];
                }

                if ($fetmain[session] == 'M' && $fetmain[section] == 'M') {
                    $mprlmor = $fetmain[msum];
                }

                if ($fetmain[session] == 'A' && $fetmain[section] == 'M') {
                    $mprlaft = $fetmain[msum];
                }

                if ($tmedmcode == $mmedmcode && $texamcode == $mexamcode) {

  
                } else {
                    if($totctr != '1'){
                        echo "<table class='border' width=100%>";
                        echo "<tr>";
                        echo"<td width='30%' class='text-center' style='font-size:14px'><b>Total</b></td>";
                        echo"<td width='10%' class='text-right'><b>$totpmor</b></td>";
                        echo"<td width='10%' class='text-right'><b>$totpaft</b></td>";
                        echo"<td width='10%' class='text-right'><b>$totsmor</b></td>";
                        echo"<td width='10%' class='text-right'><b>$totsaft</b></td>";
                        echo"<td width='10%' class='text-right'><b>$totplmor</b></td>";
                        echo"<td width='10%' class='text-right'><b>$totplaft</b></td>";
                        echo"<td width='10%' class='text-right'><b>$grtot</b></td>";
                        echo "</tr>";
                        echo "</table>";

                    }
                    ?>
        
                    <table>
                        <br>
                        <tr> 
                            <td style="width:30%"><h4><b>Medium :<?php echo $medmname[name]; ?></b></h4></td>
                            <td style="width:70%"><h4><b>Exam :<?php echo $examname[name]; ?></b></h4></td>
                        </tr>
                    </table>
                    <table class="border">
                        <tr >
                            <td style="width:4%;" class="padd"><b></b></td>
                            <td style="width:23%;" class="padd center"><b></b></td>
                            <td style="width:20%;" class="padd center" colspan="2"><b>Primary</b></td>
                            <td style="width:20%;" class="padd center" colspan="2"><b>Secondry</b></td>
                            <td style="width:20%;" class="padd center" colspan="2"><b>Prelims</b></td>
                            <td style="width:13%; text-align:right" class="padd"><b></b></td>
                        </tr>
                        <tr >
                            <td  class="padd"><b>Sr.</b></td>
                            <td style="text-align:left" class="padd"><b>Subject</b></td>
                            <td class="padd right"><b>Morning</b></td>
                            <td class="padd right"><b>Afternoon</b></td>
                            <td class="padd right"><b>Morning</b></td>
                            <td class="padd right"><b>Afternoon</b></td>
                            <td class="padd right"><b>Morning</b></td>
                            <td class="padd right"><b>Afternoon</b></td>
                            <td style="width:14%; text-align:right" class="padd"><b>Total</b></td>
                        </tr>
                    </table>

                    <?php
                    $texamcode = $mexamcode;
                    $tmedmcode = $mmedmcode;
                }
                ?>
                <?php
                $rowtot = 0;

                if ($mprimor < 40) {
                    $mprimor = 0;
                }
                if ($mpriaft < 40) {
                    $mpriaft = 0;
                }
                if ($msecmor < 40) {
                    $msecmor = 0;
                }
                if ($msecaft < 40) {
                    $msecaft = 0;
                }
                if ($mprlmor < 40) {
                    $mprlmor = 0;
                }
                if ($mprlaft < 40) {
                    $mprlaft = 0;
                }

                $rowtot = $rowtot + $mpriaft + $mprimor + $msecaft + $msecmor + $mprlaft + $mprlmor;
                $ctr++;
                echo "<table>";
                echo "<tr>";
                echo "<td width='4%'   class='padd'>$ctr</td>";
                echo "<td width='26%'  style='text-align:left' class='padd'>$fetmain[subjname]</td>";
                echo "<td width='10%'  class='padd right'>$mprimor</td>";
                echo "<td width='10%'  class='padd right'>$mpriaft</td>";
                echo "<td width='10%'  class='padd right'>$msecmor</td>";
                echo "<td width='10%'  class='padd right'>$msecaft</td>";
                echo "<td width='10%'  class='padd right'>$mprlmor</td>";
                echo "<td width='10%'  class='padd right'>$mprlaft</td>";
                echo "<td width='10%'  class='padd right'>$rowtot</td>";
                echo "</tr>";
                echo "</table>";


                $totpmor = $totpmor + $mprimor ; 
                $totpaft = $totpaft + $mpriaft ; 
                $totsmor = $totsmor + $msecmor ; 
                $totsaft = $totsaft + $msecaft ; 
                $totplmor = $totplmor + $mprlmor ; 
                $totplaft = $totplaft + $mprlaft ; 

                $grtot =  + $totplaft + $totplmor + $totsaft + $totsmor + $totpaft + $mprimor ;  

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


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

    $exam = $_POST["exam"];
    $medium = $_POST["medium"];

    $medmcond = '';
    $examcond = '';

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

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


    $gmain = mysqli_query($conn, "SELECT a.*, b.srno, b.subjcode, b.subjname,sum(b.pq1 + b.pq2 + b.pq3 + b.pq4) as psum,
                          sum(b.pq5 + b.pq6 + b.pq7 + b.pq8 + b.pq9 + b.pq10) AS ssum, SUM(b.pqrelm) AS msum 
                          from `demdmain` a, `demdtran` b where a.vouchno = b.vouchno $examcond $medmcond 
                          group by a.session, a.medmcode, a.examcode, b.subjcode order by a.examcode,b.subjcode,a.medmcode")
            or die(mysqli_error($conn));

    $getno = mysqli_num_rows($gmain);



    $contents.= "PACKET REPORT,\n";

    $ctr = 0;
    while ($fetmain = mysqli_fetch_array($gmain)) {

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

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


        if ($fetmain[session] == 'M' && $fetmain[section] == 'P') {
            $mprimmor = $fetmain[psum];
        }

        if ($fetmain[session] == 'A' && $fetmain[section] == 'P') {
            $mprimaft = $fetmain[psum];
        }

        if ($fetmain[session] == 'M' && $fetmain[section] == 'S') {
            $msecmor = $fetmain[ssum];
        }

        if ($fetmain[session] == 'A' && $fetmain[section] == 'S') {
            $msecaft = $fetmain[ssum];
        }

        if ($fetmain[session] == 'M' && $fetmain[section] == 'M') {
            $mprlmor = $fetmain[msum];
        }

        if ($fetmain[session] == 'A' && $fetmain[section] == 'M') {
            $mprlaft = $fetmain[msum];
        }




        $contents.= "Medium: $medmname[name], Exam: $examname[name],\n";

        $contents.= "Sr., Subject, Primaary Morning, Primaary Afternoon,Secondry Morning, Secondry Afternoon,Prelim Morning, Prelim Afternoon, Total,\n";


        $rowtot = 0;


        if ($mprimor < 40) {
            $mprimor = 0;
        }
        if ($mpriaft < 40) {
            $mpriaft = 0;
        }
        if ($msecmor < 40) {
            $msecmor = 0;
        }
        if ($msecaft < 40) {
            $msecaft = 0;
        }
        if ($mprlmor < 40) {
            $mprlmor = 0;
        }
        if ($mprlaft < 40) {
            $mprlaft = 0;
        }

        $rowtot = $rowtot + $mpriaft + $mprimor + $msecaft + $msecmor + $mprlaft + $mprlmor;
        $ctr++;

        $contents.= $ctr . ",";
        $contents.= "$fetmain[subjname]" . ",";
        $contents.= $mprimor . ",";
        $contents.= $mpriaft . ",";
        $contents.= $msecmor . ",";
        $contents.= $msecaft . ",";
        $contents.= $mprlmor . ",";
        $contents.= $mprlaft . ",";
        $contents.= $rowtot . "\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;
    }
}
?>     
