<?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 `examcode` = '$exam' ";
    }

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

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

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

    $qry = mysqli_query($conn, "SELECT * FROM $maindb ") or die(mysqli_error($conn));
    $fetch = mysqli_fetch_array($qry);

    $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
//        $sumqry = mysqli_query($conn, "SELECT `vouchno`,`subjcode`, `subjname`,`section`, SUM(pq1) as qpq1, SUM(pq2) as qpq2,
//                               SUM(pq3) as qpq3, SUM(pq4) as qpq4, SUM(pq5) as qpq5, SUM(pq6) as qpq6,SUM(pq7) as qpq7,
//                               SUM(pq8) as qpq8, SUM(pq9) as qpq9, SUM(pq10) as qpq10, SUM(pqrelm) as qpqrelm 
//                               FROM demdtran where vouchno in (select vouchno from demdmain where vouchno != ''
//                               $examcond $sesscond $sectcond $medcond )group by `subjcode` order by `subjname` ") or die(mysqli_error($conn));
//        $gsumqry = mysqli_query($conn, "SELECT * FROM demdtran where pq1 > 3 and pq2 > 3 and pq3 > 3 and pq4 > 3 and pq5 > 3 and 
//                               pq6 > 3 and pq7 > 3 and pq8 > 3 and  pq9 > 3 and pq10 > 3 and pqrelm > 3 and vouchno in 
//                               (select vouchno from demdmain where vouchno != '' $examcond $sesscond $sectcond 
//                               $medcond ) group by `subjcode` order by `subjname` ") or die(mysqli_error($conn));
//
//        $getno = mysqli_num_rows($gsumqry);
//
//        mysqli_query($conn, "create temporary table qtypress (srno BIGINT(9), vouchno BIGINT(9), subjname VARCHAR(20),
//                                                      section VARCHAR(20),pq1 BIGINT(10),pq2 BIGINT(10),pq3 BIGINT(10),pq4 BIGINT(10),
//                                                      pq5 BIGINT(10),pq6 BIGINT(10),pq7 BIGINT(10),pq8 BIGINT(10),pq9 BIGINT(10),
//                                                      pq10 BIGINT(10),pqrelm BIGINT(10))") or die(mysqli_error($conn));
//
//        $ctr = 0;
//        while ($ftran = mysqli_fetch_array($gsumqry)) {
//            $ctr++;
//
//            $vouchno = $ftran['vouchno'];
//            $subjname = $ftran['subjname'];
//            $section = $ftran['section'];
//            $pq1 = $ftran['pq1'];
//            $pq2 = $ftran['pq2'];
//            $pq3 = $ftran['pq3'];
//            $pq4 = $ftran['pq4'];
//            $pq5 = $ftran['pq5'];
//            $pq6 = $ftran['pq6'];
//            $pq7 = $ftran['pq7'];
//            $pq8 = $ftran['pq8'];
//            $pq9 = $ftran['pq9'];
//            $pq10 = $ftran['pq10'];
//            $pqrelm = $ftran['pqrelm'];
//
//            mysqli_query($conn, "insert into qtypress (srno ,vouchno , subjname ,section , pq1, pq2, pq3, pq4, pq5, pq6, pq7, pq8, pq9, pq10, pqrelm) 
//                values ('$ctr', '$vouchno', '$subjname', '$section', '$pq1', '$pq2', '$pq3', '$pq4', '$pq5', '$pq6', '$pq7',
//                        '$pq8', '$pq9', '$pq10', '$pqrelm')")or die(mysqli_error($conn));
//        }
//
////        $getsum = mysqli_query($conn, 'SELECT srno, vouchno, subjname, section, 
////                               SUM(`pq1`) as `qpq1`,
////                               SUM(`pq2`) as `qpq2`,
////                               SUM(`pq3`) as `qpq3`,
////                               SUM(`pq4`) as `qpq4`,
////                               SUM(`pq5`) as `qpq5`,
////                               SUM(`pq6`) as `qpq6`,
////                               SUM(`pq7`) as `qpq7`,
////                               SUM(`pq8`) as `qpq8`,
////                               SUM(`pq9`) as `qpq9`,
////                               SUM(`pq10`) as `qpq10`,
////                               SUM(`pqrelm`) as `qpqrelm` FROM `qtypress` ORDER BY `srno`')or die(mysqli_error($conn));
//
//
//        if ($getno == 0) {
//            browse("No Record Found");
//            echo "<script>window.close()</script>";
//        }

        if ($session == 'M') {
            $sessn = "Morning";
        } elseif ($session == 'A') {
            $sessn = "Afternoon";
        }

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

        $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code` = '$exam'") or die(mysqli_error($conn));
        $shexam = mysqli_fetch_array($gexam);
        ?>
        <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<span style="padding-right:8px;"></span>:
                                &nbsp;<?php echo $shexam['name']; ?></b></td>
                        <td style="width:30%" ><b>Section<span style="padding-right:8px;"></span>:
                                &nbsp;&nbsp;<?php echo $sectn; ?></b></td>
                        <td style="width:30%" ><b>Session<span style="padding-right:8px;"></span>:
                                &nbsp;&nbsp;<?php echo $sessn; ?></b></td>
                        <td style="width:10%"><b></b></td>
                    </tr>

                    <tr>
                        <td style="width:40%"></td>
                        <td style="width:40%"></td>
                        <td style="width:20%"></td>
                    </tr>

                    <tr>
                        <td style="width:40%"></td>
                        <td style="width:40%"></td>
                        <td style="width:20%"></td>
                    </tr>
                    <tr>
                        <td style="width:40%"><b>Exam<span style="padding-right:58px;"></span>:
                                &nbsp;&nbsp;<?php echo $shexam['name']; ?></b></td>
                        <td style="width:40%"></td>
                        <td style="width:20%"><b>Date<span style="padding-right:25px;"></span>:
                                &nbsp;&nbsp;<?php echo Date("d-m-Y"); ?></b></td>
                    </tr>
                </table>               
                <br>
                <br>
                <table class=" mtable">
                    <?php
                    if ($section == 'S') {
                        ?> 
                        <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>
                            <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>
                            <td class="mtd padd" style="width:10%"><b>Total</b></td>
                        </tr>

                        <?php
                        $gsumqry = mysqli_query($conn, "SELECT `vouchno`,`subjcode`, `subjname`,`section`, pq5, pq6, pq7, pq8, pq9, pq10
                                                FROM demdtran where vouchno in (select vouchno from demdmain where 
                                                vouchno != '' $examcond $sesscond $sectcond $medcond ) group by `subjcode`
                                                order by `subjname` ") or die(mysqli_error($conn));

                       

                        $sctr = $totamt = $tot5 = $totqpq = 0;
                        $t5 = $t6 = $t7 = $t8 = $t9 = $t10 = 0;

                        while ($ftran = mysqli_fetch_array($gsumqry)) {
                            $sctr++;
                            $vouchno = $ftran['vouchno'];
                            $subjname = $ftran['subjname'];
                           browse($subjname);
                            $section = $ftran['section'];
                            $pq5 = $ftran['pq5'];
                            $pq6 = $ftran['pq6'];
                            $pq7 = $ftran['pq7'];
                            $pq8 = $ftran['pq8'];
                            $pq9 = $ftran['pq9'];
                            $pq10 = $ftran['pq10'];

                            mysqli_query($conn, "insert into qtypress_s (`srno` ,`vouchno` , `subjname` ,`section` , `pq5`, `pq6`, `pq7`, `pq8`, `pq9`, `pq10`)
                                values ('$sctr', '$vouchno', '$subjname', '$section', '$pq5', '$pq6', '$pq7', '$pq8', '$pq9', '$pq10')")or die(mysqli_error($conn));
                        }

                        $sumqry = mysqli_query($conn, 'SELECT srno, vouchno, subjname, section, SUM(`pq5`) as `qpq5`,
                               SUM(`pq6`) as `qpq6`, SUM(`pq7`) as `qpq7`,SUM(`pq8`) as `qpq8`,SUM(`pq9`) as `qpq9`,
                               SUM(`pq10`) as `qpq10` FROM `qtypress_s` ORDER BY `srno`')or die(mysqli_error($conn));
                                               
                        $ctr_s = 0;
                        while ($tran = mysqli_fetch_array($sumqry)) {

                            $subject = $tran['subjname'];

                            if ($tran['qpq5'] < 3) {
                                $qpq5 = 0;
                            } elseif ($tran['qpq5'] > 3) {
                                $qpq5 = $tran['qpq5'];
                            }
                            if ($tran['qpq6'] < 3) {
                                $qpq6 = 0;
                            } elseif ($tran['qpq6'] > 3) {
                                $qpq6 = $tran['qpq6'];
                            }
                            if ($tran['qpq7'] < 3) {
                                $qpq7 = 0;
                            } elseif ($tran['qpq7'] > 3) {
                                $qpq7 = $tran['qpq7'];
                            }
                            if ($tran['qpq8'] < 3) {
                                $qpq8 = 0;
                            } elseif ($tran['qpq8'] > 3) {
                                $qpq8 = $tran['qpq8'];
                            }
                            if ($tran['qpq9'] < 3) {
                                $qpq9 = 0;
                            } elseif ($tran['qpq9'] > 3) {
                                $qpq9 = $tran['qpq9'];
                            }
                            if ($tran['qpq10'] < 3) {
                                $qpq10 = 0;
                            } elseif ($tran['qpq10'] > 3) {
                                $qpq10 = $tran['qpq10'];
                            }

                            $totqpq = $qpq5 + $qpq6 + $qpq7 +
                                    $qpq8 + $qpq9 + $qpq10;

                            $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>";
                            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>";
                            echo "<td class='mtd padd'><b>$totqpq</b></td>";
                            echo "</tr>";

                            $totamt = $totamt + $totqpq;
                            $t5 = $t5 + $qpq5;
                            $t6 = $t6 + $qpq6;
                            $t7 = $t7 + $qpq7;
                            $t8 = $t8 + $qpq8;
                            $t9 = $t9 + $qpq9;
                            $t10 = $t10 + $qpq10;
                        }
                        echo "<tr>";
                        echo "<td class='mtable padd'></td>";
                        echo "<td class='mtable padd'></td>";
                        echo "<td class='mtd padd'><b>Total</b></td>";
                        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>";
                        echo "<td class='mtd padd'><b>$totamt</b></td>";
                        echo"</tr>";
                    }
                    ?>
                </table>
                <table>
                    <?php
                    if ($section == 'P') {
                        ?> 
                        <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:15%"><b>Session</b></td>
                            <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>
                            <td class="mtd padd" style="width:11%"><b>Total</b></td>
                        </tr>
                        <?php
                        $gsumqry = mysqli_query($conn, "SELECT  vouchno, subjcode, subjname, section, pq1, pq2, pq3, pq4  FROM `demdtran` 
                                                WHERE vouchno != '' and vouchno in (select vouchno from demdmain where vouchno != ''
                                                $examcond $sesscond $sectcond $medcond ) group by `subjcode` order by `subjname` ") or die(mysqli_error($conn));

                        mysqli_query($conn, "create temporary table qtypress_p (srno BIGINT(9), vouchno BIGINT(9), subjname VARCHAR(50),
                                     section VARCHAR(20),pq1 BIGINT(10),pq2 BIGINT(10),pq3 BIGINT(10),pq4 BIGINT(10))") or die(mysqli_error($conn));


                        $pctr = 0;
                        while ($ftran = mysqli_fetch_array($gsumqry)) {
                            $pctr++;

                            $vouchno = $ftran['vouchno'];
                            $subjname = $ftran['subjname'];
                            $section = $ftran['section'];
                            $pq1 = $ftran['pq1'];
                            $pq2 = $ftran['pq2'];
                            $pq3 = $ftran['pq3'];
                            $pq4 = $ftran['pq4'];

                            mysqli_query($conn, "insert into qtypress_p (srno ,vouchno , subjname ,section , pq1, pq2, pq3, pq4)
                                             values ('$pctr', '$vouchno', '$subjname', '$section', '$pq1', '$pq2', '$pq3', '$pq4')")or die(mysqli_error($conn));
                        }
                        $sumqry = mysqli_query($conn, 'SELECT srno, vouchno, subjname, section, SUM(`pq1`) as `qpq1`,
                               SUM(`pq2`) as `qpq2`, SUM(`pq3`) as `qpq3`,SUM(`pq4`) as `qpq4` FROM `qtypress_p` ORDER BY `srno`')or die(mysqli_error($conn));

                        $totamt = $tot5 = $totqpq = 0;
                        $t1 = $t2 = $t3 = $t4 = 0;
                        $ctr_p = 0;
                        while ($tran = mysqli_fetch_array($sumqry)) {

                            $subjname = $tran['subjname'];

                            if ($tran['qpq1'] < 3) {
                                $qpq1 = 0;
                            } elseif ($tran['qpq1'] > 3) {
                                $qpq1 = $tran['qpq1'];
                            }
                            if ($tran['qpq2'] < 3) {
                                $qpq2 = 0;
                            } elseif ($tran['qpq2'] > 3) {
                                $qpq2 = $tran['qpq2'];
                            }
                            if ($tran['qpq3'] < 3) {
                                $qpq3 = 0;
                            } elseif ($tran['qpq3'] > 3) {
                                $qpq3 = $tran['qpq3'];
                            }
                            if ($tran['qpq4'] < 3) {
                                $qpq4 = 0;
                            } elseif ($tran['qpq4'] > 3) {
                                $qpq4 = $tran['qpq4'];
                            }


                            $totqpq = $qpq1 + $qpq2 + $qpq3 + $qpq4;
                            $ctr_p++;
                            echo "<tr>";
                            echo "<td class='mtable padd'>$ctr_p</td>";
                            echo "<td class='mtable padd'>$subjname</td>";
                            echo "<td class='mtable padd'>$sessn</td>";
                            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>";
                            echo "<td class='mtd padd'>$totqpq</td>";
                            echo "</tr>";

                            $totqpq = $totqpq + $qpq1 + $qpq2 + $qpq3 + $qpq4;
                            $totamt = $totamt + $totqpq;
                            $t1 = $t1 + $qpq1;
                            $t2 = $t2 + $qpq2;
                            $t3 = $t3 + $qpq3;
                            $t4 = $t4 + $qpq4;
                        }
                        echo "<tr>";
                        echo "<td class='mtable padd'></td>";
                        echo "<td class='mtable padd'></td>";
                        echo "<td class='mtd padd'><b>Total</b></td>";
                        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>";
                        echo "<td class='mtd padd'><b>$totamt</b></td>";
                        echo"</tr>";
                    }
                    ?>
                </table>

                <table>
                    <?php
                    if ($section == 'M') {
                        ?> 
                        <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:20%"><b>Session</b></td>
                            <td class="mtd padd" style="width:25%"><b>Prelims</b></td>
                            <td class="mtd padd" style="width:25%"><b>Total</b></td>
                        </tr>
                        <?php
                        $gsumqry = mysqli_query($conn, "SELECT srno, vouchno, subjname, section, pqrelm  FROM demdtran WHERE vouchno !='' and vouchno in 
                               (select vouchno from demdmain where vouchno != '' $examcond $sesscond $sectcond $medcond ) group by `subjcode` order by `subjname` ") or die(mysqli_error($conn));

                        mysqli_query($conn, "create temporary table qtypress_m (srno BIGINT(9), vouchno BIGINT(9), subjname VARCHAR(50),
                                                      section VARCHAR(20), pqrelm BIGINT(10))") or die(mysqli_error($conn));


                        $prctr = 0;
                        while ($ftran = mysqli_fetch_array($gsumqry)) {
                            $prctr++;

                            $vouchno = $ftran['vouchno'];
                            $subjname = $ftran['subjname'];
                            $section = $ftran['section'];
                            $pqrelm = $ftran['pqrelm'];

                            mysqli_query($conn, "insert into qtypress_m (srno ,vouchno , subjname ,section , pqrelm)
                                             values ('$prctr', '$vouchno', '$subjname', '$section', '$pq1', '$pq2', '$pq3', '$pq4')")or die(mysqli_error($conn));
                        }
                        $sumqry = mysqli_query($conn, 'SELECT srno, vouchno, subjname, section, SUM(`pqrelm`) as `qpqrelm`
                                              FROM `qtypress_m` ORDER BY `srno`')or die(mysqli_error($conn));

                        $totamt = $ctr = $tot5 = $totqpq = 0;
                        $ctr_pr = 0;

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

                            $subjname = $tran['subjname'];
                            $pqrelm = $tran['pqrelm'];
                            $amount = $tran['amount'];


                            $totqpq = $qpq1 + $qpq2;
                            $ctr_pr++;

                            echo "<tr>";
                            echo "<td class='mtable padd'>$ctr_pr</td>";
                            echo "<td class='mtable padd'>$subjname</td>";
                            echo "<td class='mtable padd'>$sessn</td>";
                            echo "<td class='mtd padd'>$pqrelm</td>";
                            echo "<td class='mtd padd'>$amount</td>";
                            echo "</tr>";

                            $totamt = $totamt + $amount;
                        }
                        echo "<tr>";
                        echo "<td class='mtable padd'></td>";
                        echo "<td class='mtable padd'></td>";
                        echo "<td class='mtd padd'><b>Total</b></td>";
                        echo "<td class='mtd padd'><b>$totamt</b></td>";
                        echo "<td class='mtd padd'><b>$totamt</b></td>";
                        echo"</tr>";
                    }
                    ?> 
                </table>
            </div>
        </div>
        <?php include('includes/linksfoot.php'); ?>
    </body>
    </html>
<?php } ?>