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

$action = "gendemd_fr.php";
$formname = 'Generate Demand Form';
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

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

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

if (isset($_POST["submit"])) {
    $a = 1;
    $count = $_POST["items"];
    $c1 = $_POST["c1"];
    $c2 = $_POST["c2"];
    $c3 = $_POST["c3"];
    $c4 = $_POST["c4"];
    $c5 = $_POST["c5"];
    $c6 = $_POST["c6"];
    $c7 = $_POST["c7"];
    $c8 = $_POST["c8"];
    $c9 = $_POST["c9"];
    $c10 = $_POST["c10"];

    $cntr = 0;
    for ($j = 0; $j <= $count; $j++) {
        $mcheck = $_POST["check_$j"];
        $saccode = $_POST["saccode_$j"];
        $ncust = $_POST["ncust_$j"];
        $vouchdt = $_POST["date"];
        $fexam = $_POST["fexam"];
        $texam = $_POST["texam"];
        $section = $_POST["section"];
        $session = $_POST["session"];



        if (!empty($saccode)) {

            $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `examcode`='$fexam' AND `section`='$section' 
                                  AND `session`='$session' and  accode = '$saccode'") or die(mysqli_error($conn));
            $get = mysqli_fetch_array($result);
            $oldvouch = $get['vouchno'];
            $semi = $get['semi'];
//            browse($oldvouch);

            $fmed = mysqli_query($conn, "SELECT `medmcode` FROM `accmast` WHERE accode = '$saccode' and grpcode = '010'") or die(mysqli_error($conn));
            $gmedm = mysqli_fetch_array($fmed);
            $medium = $get['medmcode'];
//            $nofpack = $get['nopack'];
            $rem1 = $get['rem1'];
            $rem2 = $get['rem2'];


            $getvouch = mysqli_query($conn, "SELECT max(increvouch)as newvouch FROM `$maindb`") or die(mysqli_error($conn));
            $fetvouch = mysqli_fetch_array($getvouch);
            $increvouch = $fetvouch['newvouch'] + 1;
            $newvouch = $myyear . $increvouch;



            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$oldvouch'") or die(mysqli_error($conn));

            $nofpack = 0;
            mysqli_query($conn, "DELETE FROM `$trandb` WHERE `vouchno`='$newvouch'") or die(mysqli_error($conn));
            
            while ($ftran = mysqli_fetch_array($gettran)) {
                $subname = $ftran['subjcode'];
                $subject = $ftran['subjname'];
                $srno = $ftran['srno'];
                $first = $ftran['pq1'];
                $second = $ftran['pq2'];
                $third = $ftran['pq3'];
                $fourth = $ftran['pq4'];
                $pq5 = $ftran['pq5'];
                $pq6 = $ftran['pq6'];
                $pq7 = $ftran['pq7'];
                $pq8 = $ftran['pq8'];
                $pq9 = $ftran['pq9'];
                $pq10 = $ftran['pq10'];
                $pqrelm = $ftran['pqrelm'];

                if ($c1 == 1) {
                    
                } else {
                    $first = 0;
                }

                if ($c2 == 1) {
                    
                } else {
                    $second = 0;
                }

                if ($c3 == 1) {
                    
                } else {
                    $third = 0;
                }

                if ($c4 == 1) {
                    
                } else {
                    $fourth = 0;
                }


                if ($c5 == 1) {
                    
                } else {
                    $pq5 = 0;
                }

                if ($c6 == 1) {
                    
                } else {
                    $pq6 = 0;
                }

                if ($c7 == 1) {
                    
                } else {
                    $pq7 = 0;
                }

                if ($c8 == 1) {
                    
                } else {
                    $pq8 = 0;
                }

                if ($c9 == 1) {
                    
                } else {
                    $pq9 = 0;
                }

                if ($c10 == 1) {
                    
                } else {
                    $pq10 = 0;
                }


                if ($first < 10) {
                    $first = 0;
                } else {
                    $nofpack++;
                }

                if ($second < 10) {
                    $second = 0;
                } else {
                    $nofpack++;
                }

                if ($third < 10) {
                    $third = 0;
                } else {
                    $nofpack++;
                }

                if ($fourth < 10) {
                    $fourth = 0;
                } else {
                    $nofpack++;
                }

                if ($pq5 < 10) {
                    $pq5 = 0;
                } else {
                    $nofpack++;
                }

                if ($pq6 < 10) {
                    $pq6 = 0;
                } else {
                    $nofpack++;
                }

                if ($pq7 < 10) {
                    $pq7 = 0;
                } else {
                    $nofpack++;
                }

                if ($pq8 < 10) {
                    $pq8 = 0;
                } else {
                    $nofpack++;
                }

                if ($pq9 < 10) {
                    $pq9 = 0;
                } else {
                    $nofpack++;
                }

                if ($pq10 < 10) {
                    $pq10 = 0;
                } else {
                    $nofpack++;
                }

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `increvouch`, `vouchdt`, `srno`, `subjcode`, `subjname`,`pq1`, `pq2`, `pq3`, `pq4`, `pq5`, 
                                               `pq6`, `pq7`, `pq8`, `pq9`, `pq10`, `pqrelm`, `section`)
                VALUES ('$newvouch', '$increvouch', '$vouchdt', '$srno', '$subname', '$subject',  '$first', '$second', '$third', '$fourth',
                        '$pq5', '$pq6', '$pq7', '$pq8', '$pq9', '$pq10', '$pqrelm', '$section')") or die(mysqli_error($conn));
            }
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `year`, `increvouch`, `vouchdt`, `accode`, `custcode`, `medmcode`,
                                               `examcode`, `section`, `session`,`nopack`,`rem1`,`rem2`,`semi`)
                                     VALUES ('$newvouch', '$zyear', '$increvouch','$vouchdt', '$saccode', '$ncust', '$medium', 
                                            '$texam', '$section', '$session', '$nofpack','$rem1','$rem2', '$semi')") or die(mysqli_error($conn));
        }
    }
    browse('Generated Successfully');
}

include("includes/linkshead.php");
?>
<script src="MyScript/gendemd.js" type="text/javascript"></script>
</head>
<body>
    <form method='POST' >
        <!-- Default box -->
        <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname; ?></h3>
                    <div class="actions pull-right">
<!--                        <i class="fa fa-chevron-down"></i>
                        <i class="fa fa-times"></i>-->
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right">
                            <label class="control-label marg">From Exam</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                            <select  style="height:34px;"  name="fexam" class="form-control select2 fexam" required>
                                <option></option>
                                <?php
                                $mown = mysqli_query($conn, "SELECT * FROM `exam` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($trow = mysqli_fetch_array($mown)) {
                                    echo "<option value=$trow[code]>$trow[name]</option>";
                                }
                                ?>
                            </select>                          
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right">
                            <label class="control-label marg">Exam</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                            <select  style="height:34px;"  name="texam" class="form-control select2 texam" required>
                                <option></option>
                                <?php
                                $mown1 = mysqli_query($conn, "SELECT * FROM `exam` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($trow1 = mysqli_fetch_array($mown1)) {
                                    echo "<option value=$trow1[code]>$trow1[name]</option>";
                                }
                                ?>
                            </select>                          
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Section</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                            <select class="form-control select2 section"  name="section" style="width: 100%" required>
                                <option value=""></option>
                                <option value="P">Primary</option>
                                <option value="S">Secondary</option>
                                <option value="M">Preliums</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Session</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                            <select class="form-control select2 session"  name="session" style="width: 100%" required>
                                <option value=""></option>
                                <option value="M">Morning</option>
                                <option value="A">Afternoon</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right">
                            <label class="control-label marg">Date</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 pad">
                            <input type="date" size="80" name="date" value="<?php echo date('Y-m-d'); ?>" required class="form-control"/>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="custmodal"  role="dialog" aria-labelledby="myLargeModalLabel" >
                    <div class="modal-dialog modal-lg" style="margin-top: 5%;">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                                <div class="row">
                                    <div class="col-lg-2 text-right padi">
                                        <label class="label-control">Search By Code</label>
                                    </div>
                                    <div class="col-lg-2 padi" >
                                        <input type='text' style="" name='search' class="form-control tsearch">    
                                    </div>
                                    <div class="col-lg-2 padi" >
                                        <input type="submit" id='submit' name="submit" class="btn btn-pad btn-square" style="background-color:#6082BF;color:#ffffff" value="Submit">
                                    </div>

                                </div>
                            </div>
                            <div class="modal-body">
                                <div class="table-responsive schooldiv" style="height: 300px;">

                                </div>
                                <input type="hidden" id="items" name="items" value="" >
                            </div>
                            <div class="modal-footer"></div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <div class="text-center pad">
                        <input type="button" id='proceed' class="btn btn-pad btn-square proceed" style="background-color:#008D4C;color:#ffffff" value="Proceed">

                        <input tye="button" id='cancel' type="reset" class="btn btn-pad btn-square " style="background-color:red;color:#ffffff" onClick="window.location.href = '<?= 'default.php' ?>'" value="Exit">

                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 selectdiv pad" style="display:none">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Select how many standards to include.</h3>
                    <div class="actions pull-right">
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row chkselection">
                        <div class="col-lg-1 pad text-right">
                            <label class='label-control' style='font-size: 21px'>5</label>
                        </div>
                        <div class='col-lg-1 pad'>
                            <input type='checkbox' class='form-control' value='1' style='width: 17px'>    
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
