<?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 = 'invmain';
$trandb = 'invtran';

$zroot = $_SESSION["zdbroot"];
$formname = 'Sales Summary Paperwise Report';

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


//$voucher = $_POST["vouchno"];
    $companyname = "MAHARASHTRA BHARTEEYA SHIKSHAN MANDAL";

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


    $medmcond = "";
    $sessncond = '';
    $sectcond = '';
    $examcond = '';

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

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

    if (!empty($section)) {
        $sectcond = " AND a.`section` = '$section' ";
    }
//
    $qmain = mysqli_query($conn, "SELECT * from `$maindb` where `vouchno` != ''   "
            . "$examcond $medmcond $sessncond $sectcond")or die(mysqli_error($conn));
    
    $fmain = mysqli_fetch_array($qmain);
    $medmcode = $fmain[medmcode];
    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;
            border-left-style:none;
            border-right-style:none;
            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:solid;
            border-right-style:none;
            border-left-style:none;
            border-bottom-style:solid;
            border-width:2px;
        }

        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 "<h3 class='text-center'><b>$companyname</b></h3>";
                    ?>
                </div>

                <table style="width:100%">
                    <tr>
                        <td style="width:50%"><h4><b>SALES SUMMARY - PACKET WISE</b></h4></td>
                        <td style="width:5s0%"></td>
                    </tr>
                </table>
                <br>
                <?php
//                $fmain = mysqli_fetch_array($qmain)
                ?>

                <table>
                    <tr class="border">
                        <td style="width:5%;" class="padd"><b>Sr.No</b></td>
                        <td style="width:10%;" class="padd"><b>Medium</b></td>
                        <td style="width:7%;" class="padd"><b>First</b></td>
                        <td style="width:7%;" class="padd"><b>Second</b></td>
                        <td style="width:7%;" class="padd"><b>Third</b></td>
                        <td style="width:7%;" class="padd"><b>Fourth</b></td>
                        <td style="width:7%;" class="padd"><b>Fifth</b></td>
                        <td style="width:8%;" class="padd"><b>Sixth</b></td>
                        <td style="width:8%;" class="padd"><b>Seventh</b></td>
                        <td style="width:8%;" class="padd"><b>Eight</b></td>
                        <td style="width:8%;" class="padd"><b>Ninth</b></td>
                        <td style="width:8%;" class="padd"><b>Tenth</b></td>
                        <td style="width:10%;" class="padd mtd"><b>Total</b></td>
                    </tr>

                    <?php
                    $ctr = 0;
                    $totfpack = 0;

                    $gpack = mysqli_query($conn, "SELECT a.*,b.vouchno,SUM(b.pq1) as p1,SUM(b.pq2) as p2,SUM(b.pq3) as p3,SUM(b.pq4) as p4,
                            SUM(b.pq5) as p5,SUM(b.pq6) as p6,SUM(b.pq7) as p7,SUM(b.pq8) as p8,SUM(b.pq9) as p9,
                            SUM(b.pq10) as p10 FROM `invmain` a,`invtran` b WHERE a.vouchno = b.vouchno  and a.medmcode` = $medmcode
                             $examcond $sessncond $sectcond ") or die(mysqli_error($conn));



                    while ($fpack = mysqli_fetch_array($gpack)) {

//                        $getmedm = mysqli_query($conn, "SELECT * FROM `medium` where  `code` = $fpack[medmcode] group by $fpack[medmcode] 
//                                order by $fpack[medmcode] ") or die (mysqli_error($conn));
//                        $shmedm = mysqli_fetch_array($getmedm);
                        $gmedm = mysqli_query($conn, "SELECT `code`,`name` FROM `medium` WHERE `code` = $fpack[medmcode] ")or die(mysqli_error($conn));
                        $fmedm = mysqli_fetch_array($gmedm);

                        $totfpack = $fpack[p1] + $fpack[p2] + $fpack[p3] + $fpack[p4] + $fpack[p5] +
                                $fpack[p6] + $fpack[p7] + $fpack[p8] + $fpack[p9] + $fpack[p10];

                        $ctr++;
                        echo "<tr>";
                        echo "<td class='padd'>$ctr</td>";
                        echo "<td class='padd'>$fmedm[name]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p1]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p2]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p3]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p4]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p5]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p6]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p7]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p8]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p9]</td>";
                        echo "<td style='text-align:right'class='padd'>$fpack[p10]</td>";
                        echo "<td style='text-align:right'class='padd'>$totfpack</td>";
                        echo "</tr>";



                        echo "<tr>";
                        echo "<td class='padd border2'></td>";
                        echo "<td style='text-align:right'  class='padd border2'><b>Total</b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "<td style='text-align:center' class='padd border2'><b></b></td>";
                        echo "</tr>";
                        ?>
                    </table>
                    <?php
//                }
                    ?>
                </div>
            </div>
            <?php include('includes/linksfoot.php'); ?>
        </body>
        </html>
    <?php
    }
}
?>

