<?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 = 'SALE SUMMARY AMOUNTWISE';

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


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


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

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

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

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

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

    $gmmedm = mysqli_query($conn, "SELECT * from `medium` where `code` != '' $medmcond")or die(mysqli_error($conn));

    $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code` = '$exam'") or die(mysqli_error($conn));
    $fexam = mysqli_fetch_array($gexam);
    $exname = $fexam['name'];

    $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;
        }

        .right{
            text-align: right;
        }


        table{
            width:100%;
        }

    </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'>$zcomp[compname]</h3>";
                    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:50%"><h5><b>SALES SUMMARY - AMOUNT WISE</b></h5></td>
                        <td style="width:50%"><h5><b>Exam - <?= $exname?></b></h5></td>
                    </tr>
                </table>
                <br>
                <?php
                ?>

                <table>
                    <tr class="border">
                        <td style="width:10%;" class="padd "><b>Sr.No</b></td>
                        <td style="width:13%;" class="padd"><b>Medium</b></td>
                        <td style="width:19%;" class="padd right"><b>Primary</b></td>
                        <td style="width:19%;" class="padd right"><b>Secondry</b></td>
                        <td style="width:19%;" class="padd right"><b>Prelim</b></td>
                        <td style="width:20%;" class="padd right"><b>Total</b></td>
                    </tr>

                    <?php
                    $ctr = 0;
                    $pamt = 0;
                    $samt = 0;
                    $pramt = 0;
                    $totrow = $totpramt = $totsramt = $totpmamt = 0;

                    while ($fmedm = mysqli_fetch_array($gmmedm)) {


                        if ($section != '') {
                            if ($section == 'P') {
                                $gpramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'P'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
                            } else if ($section == 'S') {

                                $gsramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'S'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
                            } else if ($section == 'M') {
                                $gpmamt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'M'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
                            }
                        } else {
                            $gpramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'P'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));

                            $gsramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'S'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));

                            $gpmamt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'M'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
                        }



                        $fpramt = mysqli_fetch_array($gpramt);
                        $fsramt = mysqli_fetch_array($gsramt);
                        $fpmamt = mysqli_fetch_array($gpmamt);

                        $totrow = $fpramt['amount'] + $fsramt['amount'] + $fpmamt['amount'];

                        if ($fpramt['amount'] < 3) {
                            $fpramt['amount'] = 0;
                        }
                        if ($fsramt['amount'] < 3) {
                            $fsramt['amount'] = 0;
                        }
                        if ($fpmamt['amount'] < 3) {
                            $fpmamt['amount'] = 0;
                        }


                        $ctr++;


                        echo "<tr>";
                        echo "<td class='padd'>$ctr</td>";
                        echo "<td class='padd'>$fmedm[name]</td>";
                        echo "<td class='padd right'>$fpramt[amount]</td>";
                        echo "<td class='padd right'>$fsramt[amount]</td>";
                        echo "<td class='padd right'>$fpmamt[amount]</td>";
                        echo "<td class='padd right'>$totrow</td>";
                        echo "</tr>";

                        $totpramt = $totpramt + $fpramt['amount'];
                        $totsramt = $totsramt + $fsramt['amount'];
                        $totpmamt = $totpmamt + $fpmamt['amount'];
                    }
                    $grtot = $totpramt + $totsramt + $totpmamt;

                    echo "<tr>";
                    echo "<td class='padd'></td>";
                    echo "<td style='text-align:right'  class='padd'><b>Total</b></td>";
                    echo "<td style='text-align:right' class='padd border2'><b>$totpramt</b></td>";
                    echo "<td style='text-align:right' class='padd border2'><b>$totsramt</b></td>";
                    echo "<td style='text-align:right' class='padd border2'><b>$totpmamt</b></td>";
                    echo "<td style='text-align:right' class='padd border2'><b>$grtot</b></td>";
                    echo "</tr>";
                    ?>
                </table>
                <?php ?>
            </div>
        </div>
        <?php include('includes/linksfoot.php'); ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {


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


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

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

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

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

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

    $gmmedm = mysqli_query($conn, "SELECT * from `medium` where `code` != '' $medmcond")or die(mysqli_error($conn));


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

    $contents .= "Sr.No, Medium, Primary, Secondry, Prelim, Total,\n";

    $ctr = 0;
    $pamt = 0;
    $samt = 0;
    $pramt = 0;
    $totrow = $totpramt = $totsramt = $totpmamt = 0;

    while ($fmedm = mysqli_fetch_array($gmmedm)) {


        if ($section != '') {
            if ($section == 'P') {
                $gpramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'P'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
            } else if ($section == 'S') {

                $gsramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'S'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
            } else if ($section == 'M') {
                $gpmamt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'M'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
            }
        } else {
            $gpramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'P'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));

            $gsramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'S'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));

            $gpmamt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'M'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
        }



        $fpramt = mysqli_fetch_array($gpramt);
        $fsramt = mysqli_fetch_array($gsramt);
        $fpmamt = mysqli_fetch_array($gpmamt);

        $totrow = $fpramt['amount'] + $fsramt['amount'] + $fpmamt['amount'];

        if ($fpramt['amount'] < 3) {
            $fpramt['amount'] = 0;
        }
        if ($fsramt['amount'] < 3) {
            $fsramt['amount'] = 0;
        }
        if ($fpmamt['amount'] < 3) {
            $fpmamt['amount'] = 0;
        }


        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $fmedm['name'] . ",";
        $contents .= $fpramt['amount'] . ",";
        $contents .= $fsramt['amount'] . ",";
        $contents .= $fpmamt['amount'] . ",";
        $contents .= $totrow . "\n";

        $totpramt = $totpramt + $fpramt['amount'];
        $totsramt = $totsramt + $fsramt['amount'];
        $totpmamt = $totpmamt + $fpmamt['amount'];
    }

    $grtot = $totpramt + $totsramt + $totpmamt;

    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $totpramt . ",";
    $contents .= $totsramt . ",";
    $contents .= $totpmamt . ",";
    $contents .= $grtot . "\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;
    }
}
if (isset($_POST["subpdf"])) {


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


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

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

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

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

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

    $gmmedm = mysqli_query($conn, "SELECT * from `medium` where `code` != '' $medmcond")or die(mysqli_error($conn));


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



    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->Cell(278, 7, "$zcomp[compname]", 1, 0, 'C');
    $pdf->Ln();
    $pdf->Cell(278, 7, "$zcomp[add1]" . "$zcomp[add2]", 1, 0, 'C');
    $pdf->Ln();
    $pdf->Cell(278, 7, "$zcomp[add3]", 1, 0, 'C');
    $pdf->Ln();
    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(278, 7, "SALES SUMMARY - AMOUNT WISE", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);
    $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
    $pdf->Cell(54, 7, "Medium", 1, 0, '', 1);
    $pdf->Cell(54, 7, "Primary", 1, 0, '', 1);
    $pdf->Cell(54, 7, "Secondary", 1, 0, '', 1, 0, 'R');
    $pdf->Cell(54, 7, "Prelim", 1, 0, '', 1, 0, 'R');
    $pdf->Cell(54, 7, "Total", 1, 0, '', 1, 0, 'R');
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $ctr = 0;
    $pamt = 0;
    $samt = 0;
    $pramt = 0;
    $totrow = $totpramt = $totsramt = $totpmamt = 0;

    while ($fmedm = mysqli_fetch_array($gmmedm)) {


        if ($section != '') {
            if ($section == 'P') {
                $gpramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'P'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
            } else if ($section == 'S') {

                $gsramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'S'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
            } else if ($section == 'M') {
                $gpmamt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'M'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
            }
        } else {
            $gpramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'P'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));

            $gsramt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'S'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));

            $gpmamt = mysqli_query($conn, "SELECT SUM(billamt)as amount from `invmain` where `vouchno` != '' and `medmcode` = '$fmedm[code]' AND `section` = 'M'
                                              $examcond  $sessncond ") or die(mysqli_error($conn));
        }



        $fpramt = mysqli_fetch_array($gpramt);
        $fsramt = mysqli_fetch_array($gsramt);
        $fpmamt = mysqli_fetch_array($gpmamt);

        $totrow = $fpramt['amount'] + $fsramt['amount'] + $fpmamt['amount'];

        if ($fpramt['amount'] < 3) {
            $fpramt['amount'] = 0;
        }
        if ($fsramt['amount'] < 3) {
            $fsramt['amount'] = 0;
        }
        if ($fpmamt['amount'] < 3) {
            $fpmamt['amount'] = 0;
        }


        $ctr++;


        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(8, 7, "$ctr", 1);
        $pdf->Cell(54, 7, "$fmedm[name]", 1);
        $pdf->Cell(54, 7, "$fpramt[amount]", 1);
        $pdf->Cell(54, 7, "$fsramt[amount]", 1);
        $pdf->Cell(54, 7, "$fpmamt[amount]", 1);
        $pdf->Cell(54, 7, "$totrow", 1);
        $pdf->Ln();

        $totpramt = $totpramt + $fpramt['amount'];
        $totsramt = $totsramt + $fsramt['amount'];
        $totpmamt = $totpmamt + $fpmamt['amount'];
    }
    $grtot = $totpramt + $totsramt + $totpmamt;

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(255, 0, 0);
    $pdf->Cell(8, 7, " ", 1, 0, '', 1);
    $pdf->Cell(54, 7, "Total", 1, 0, '', 1);
    $pdf->Cell(54, 7, "$totpramt", 1, 0, '', 1);
    $pdf->Cell(54, 7, "$totsramt", 1, 0, '', 1);
    $pdf->Cell(54, 7, "$totpmamt", 1, 0, '', 1);
    $pdf->Cell(54, 7, "$grtot", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0, '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $pdf->Output("Sale Summary AmountWise.pdf", "D");
}
?>            

