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

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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$zroot = $_SESSION["zdbroot"];

if (isset($_POST["view"])) {
    $formname = 'QTY FOR PRESS';

//$voucher = $_POST["vouchno"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $exam = $_POST["exam"];
    $medium = $_POST["medium"];

    $sectcond2 = '';

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

    if (!empty($session)) {
        $sesscond = " AND a.`session` = '$session' ";
    }

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

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

    $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:4px;
            font-size:12px;
            font-size:12px;
        }
        .padd{
            padding:7px;
        }
        table{
            width:100%;
        }
        hr { 
            /*border:1px solid black;*/
            border-style: inset;
            border-width: 2px;
        } 
        page {
            width: 210mm;
            min-height: 297mm;
            padding: 1mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            background: white;
        }
        .subpage {
            padding-right: 0.5cm;
            padding-left: 0.5cm;
            /* padding: 0cm; */
            /* border: 1px solid; */
            height: 380mm;
            outline: 2cm #ffffff solid;
        }
    </style>
    </head>
    <body>    
        <?php
        if ($session == 'M') {
            $sessn = "Morning";
        } elseif ($session == 'A') {
            $sessn = "Afternoon";
        }

        if ($section == 'P') {
            $sectn = "Primary";
        } elseif ($section == 'S') {
            $sectn = "Secondary";
        } elseif ($section == 'M') {
            $sectn = "Preliem";
        }

        $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code` = '$exam'") or die(mysqli_error($conn));
        $shexam = mysqli_fetch_array($gexam);
       
        $gmedium = mysqli_query($conn, "SELECT `code`,`name` FROM `medium` WHERE `code` = '$medium'") or die(mysqli_error($conn));
        $fmedm = mysqli_fetch_array($gmedium);
        ?>
        <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 "<h5 class='text-center'> REG.NO. BOMOBAY P.T.ACT 1950/F 17203 PUNE /SOCIETY'S REG.ACT 1860 MAHARASHTRA/ 330 - 2000/ PUNE</h5>";
                    echo "<h5 class='text-center'>$zcomp[add1] $zcomp[add2] </h5>";
                    echo "<h5 class='text-center'>$zcomp[add3]</h5>";
                    ?>
                </div>
                <hr>       
                <h3 class='text-center'><b>QTY. FOR PRESS</b></h3>

                <table style="width:100%" >
                    <tr>
                        <td style="width:30%"><b>Specific Exam</b><span style="padding-right:8px;"></span>:
                                &nbsp;<?php echo $shexam['name']; ?></td>
                        <td style="width:30%" ><b>Section</b><span style="padding-right:8px;"></span>:
                                &nbsp;&nbsp;<?php echo $sectn; ?></td>
                        <td style="width:30%" ><b>Session</b><span style="padding-right:8px;"></span>:
                                &nbsp;&nbsp;<?php echo $sessn; ?></td>
                    </tr>

                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>

                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><b>Exam</b><span style="padding-right:58px;"></span>:
                                &nbsp;&nbsp;<?php echo $shexam['name']; ?></td>
                        <td><b>Medium</b>&nbsp;&nbsp;:
                            &nbsp;&nbsp;<?php echo $fmedm['name']; ?></td>
                        <td><b>Date</b><span style="padding-right:25px;"></span>:
                                &nbsp;&nbsp;<?php echo Date("d-m-Y"); ?></td>
                    </tr>
                </table>               
                <br>
                <br>
                <table class=" mtable">
                    <tr>
                        <td class="mtable padd" style="width:5%"><b>Sr.</b></td>
                        <td class="mtable padd" style="width:25%"><b>Subject</b></td>
                        <td class="mtable padd" style="width:12%"><b>Session</b></td>
                        <?php
                        if ($section == 'P') {
                            ?> 
                            <td class="mtd padd" style="width:11%"><b>First</b></td>
                            <td class="mtd padd" style="width:11%"><b>Second</b></td>
                            <td class="mtd padd" style="width:11%"><b>Third</b></td>
                            <td class="mtd padd" style="width:11%"><b>Fourth</b></td>
                            <?php
                        }
                        if ($section == 'S') {
                            ?> 
                            <td class="mtd padd" style="width:8%"><b>Fifth</b></td>
                            <td class="mtd padd" style="width:8%"><b>Sixth</b></td>
                            <td class="mtd padd" style="width:8%"><b>Seventh</b></td>
                            <td class="mtd padd" style="width:8%"><b>Eight</b></td>
                            <td class="mtd padd" style="width:8%"><b>Ninth</b></td>
                            <td class="mtd padd" style="width:8%"><b>Tenth</b></td>
                            <?php
                        }
                        if ($section == 'M') {
                            ?> 
                            <td class="mtd padd" style="width:25%"><b>Prelim</b></td>
                            <?php
                        }
                        ?> 

                        <td class="mtd padd" style="width:10%"><b>Total</b></td>
                    </tr>

                    <?php
                    
$string = '0123456789';
$string_shuffled = str_shuffle($string);
$pass = substr($string_shuffled, 1, 6);                    
$qtypress = 'qx' . $pass ;


                    
                    mysqli_query($conn, "CREATE TEMPORARY TABLE $qtypress (SELECT a.*, b.srno, b.subjcode, b.subjname, 
                                b.pq1, b.pq2, b.pq3, b.pq4, b.pq5, b.pq6, b.pq7, b.pq8, b.pq9, b.pq10, b.pqrelm, 
                                00000000.00 as tot from demdmain a, demdtran b where a.vouchno = b.vouchno 
                                and a.year = '$myyear'
                                $examcond $sesscond $sectcond $medcond order by b.subjname) ") or die(mysqli_error($conn));
                    
//                    $tab = mysqli_query($conn, "SELECT * from $qtypress ");
//                    print table($tab);

                    mysqli_query($conn, "update $qtypress set pq1 = 0 where pq1 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq2 = 0 where pq2 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq3 = 0 where pq3 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq4 = 0 where pq4 < 2") or die(mysqli_error($conn));

                    mysqli_query($conn, "update $qtypress set pq5 = 0 where pq5 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq6 = 0 where pq6 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq7 = 0 where pq7 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq8 = 0 where pq8 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq9 = 0 where pq9 < 2") or die(mysqli_error($conn));
                    mysqli_query($conn, "update $qtypress set pq10 = 0 where pq10 < 2") or die(mysqli_error($conn));

                    mysqli_query($conn, "update $qtypress set pqrelm = 0 where pqrelm < 2") or die(mysqli_error($conn));

                    $sumqry = mysqli_query($conn, "SELECT srno, subjcode, subjname, sum(pq1) as pq1, sum(pq2) as pq2, sum(pq3) as pq3, sum(pq4) as pq4,
                                           sum(pq5) as pq5, sum(pq6) as pq6, sum(pq5) as pq5, sum(pq6) as pq6, sum(pq7) as pq7, sum(pq8) as pq8,
                                           sum(pq9) as pq9, sum(pq10) as pq10, sum(pqrelm) as pqrelm from $qtypress group by subjcode order by subjname") 
                                           or die(mysqli_error($conn));
                    
//                    print table($sumqry);
//                    die();
                    
                    $ctr_s = 0;
                    $t1 = $t2 = $t3 = $t4 = $t5 = 0;
                    $t6 = $t7 = $t8 = $t9 = $t10 = 0;
                    $tprelm = 0;

                    while ($tran = mysqli_fetch_array($sumqry)) {

                        $subject = $tran['subjname'];

                        if ($tran['pq1'] < 2) {
                            $qpq1 = '';
                        } else {
                            $qpq1 = $tran['pq1'];
                        }

                        if ($tran['pq2'] < 2) {
                            $qpq2 = '';
                        } else {
                            $qpq2 = $tran['pq2'];
                        }

                        if ($tran['pq3'] < 2) {
                            $qpq3 = '';
                        } else {
                            $qpq3 = $tran['pq3'];
                        }

                        if ($tran['pq4'] < 2) {
                            $qpq4 = '';
                        } else {
                            $qpq4 = $tran['pq4'];
                        }

                        if ($tran['pq5'] < 2) {
                            $qpq5 = '';
                        } else {
                            $qpq5 = $tran['pq5'];
                        }

                        if ($tran['pq6'] < 2) {
                            $qpq6 = '';
                        } else {
                            $qpq6 = $tran['pq6'];
                        }

                        if ($tran['pq7'] < 2) {
                            $qpq7 = '';
                        } else {
                            $qpq7 = $tran['pq7'];
                        }

                        if ($tran['pq8'] < 2) {
                            $qpq8 = '';
                        } else {
                            $qpq8 = $tran['pq8'];
                        }

                        if ($tran['pq9'] < 2) {
                            $qpq9 = '';
                        } else {
                            $qpq9 = $tran['pq9'];
                        }

                        if ($tran['pq10'] < 2) {
                            $qpq10 = '';
                        } else {
                            $qpq10 = $tran['pq10'];
                        }

                        if ($tran['pqrelm'] < 2) {
                            $pqrelm = '';
                        } else {
                            $pqrelm = $tran['pqrelm'];
                        }


                        $ctr_s++;
                        echo "<tr>";
                        echo "<td class='mtable padd'>$ctr_s</td>";
                        echo "<td class='mtable padd'>$subject</td>";
                        echo "<td class='mtable padd'>$sessn</td>";
                        if ($section == 'P') {
                            echo "<td class='mtd padd'>$qpq1</td>";
                            echo "<td class='mtd padd'>$qpq2</td>";
                            echo "<td class='mtd padd'>$qpq3</td>";
                            echo "<td class='mtd padd'>$qpq4</td>";
                            $totqpq = $qpq1 + $qpq2 + $qpq3 + $qpq4;
                        }
                        if ($section == 'S') {
                            echo "<td class='mtd padd'>$qpq5</td>";
                            echo "<td class='mtd padd'>$qpq6</td>";
                            echo "<td class='mtd padd'>$qpq7</td>";
                            echo "<td class='mtd padd'>$qpq8</td>";
                            echo "<td class='mtd padd'>$qpq9</td>";
                            echo "<td class='mtd padd'>$qpq10</td>";
                            $totqpq = $qpq5 + $qpq6 + $qpq7 + $qpq8 + $qpq9 + $qpq10;
                        }
                        if ($section == 'M') {
                            echo "<td class='mtd padd'>$pqrelm</td>";
                            $totqpq = $pqrelm;
                        }
                        echo "<td class='mtd padd'><b>$totqpq</b></td>";
                        echo "</tr>";

                        $totamt = $totamt + $totqpq;
                        $t1 = $t1 + $qpq1;
                        $t2 = $t2 + $qpq2;
                        $t3 = $t3 + $qpq3;
                        $t4 = $t4 + $qpq4;
                        $t5 = $t5 + $qpq5;
                        $t6 = $t6 + $qpq6;
                        $t7 = $t7 + $qpq7;
                        $t8 = $t8 + $qpq8;
                        $t9 = $t9 + $qpq9;
                        $t10 = $t10 + $qpq10;
                        $tprelm = $tprelm + $pqrelm;
                    }
                    echo "<tr>";
                    echo "<td class='mtable padd'></td>";
                    echo "<td class='mtable padd'></td>";
                    echo "<td class='mtd padd'><b>Total</b></td>";
                    if ($section == 'P') {
                        echo "<td class='mtd padd'><b>$t1</b></td>";
                        echo "<td class='mtd padd'><b>$t2</b></td>";
                        echo "<td class='mtd padd'><b>$t3</b></td>";
                        echo "<td class='mtd padd'><b>$t4</b></td>";
                    }
                    if ($section == 'S') {
                        echo "<td class='mtd padd'><b>$t5</b></td>";
                        echo "<td class='mtd padd'><b>$t6</b></td>";
                        echo "<td class='mtd padd'><b>$t7</b></td>";
                        echo "<td class='mtd padd'><b>$t8</b></td>";
                        echo "<td class='mtd padd'><b>$t9</b></td>";
                        echo "<td class='mtd padd'><b>$t10</b></td>";
                    }
                    if ($section == 'M') {
                        echo "<td class='mtd padd'><b>$tprelm</b></td>";
                    }
                    echo "<td class='mtd padd'><b>$totamt</b></td>";
                    echo"</tr>";
                }
                ?>
            </table>
        </div>
    </div>
    <?php include('includes/linksfoot.php'); ?>
</body>
</html>
