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

$formname = 'Demand Form';
$maindb = 'demdmain';
$trandb = 'demdtran';

$file = 'dmndfrm.php';
$keyfld = "vouchno";
$keyfld2 = "increvouch";
$printfile = "dmndfrm_print.php";
$admin = $_SESSION["advouchnomin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
$zroot = $_SESSION["zdir"];
$pyear = $_SESSION["pyear"];

mysqli_select_db($conn, $zdb);
$zyear = right($zdb, 4);

//include("includes/vouchno_generate.php");
include("includes/increvouch_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $code = $_POST["code"];
    $school = $_POST["school"];
    $medium = $_POST["medium"];
    $exam = $_POST["exam"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $nopack = $_POST["nopack"];
    $rem1 = $_POST["remark"];
    $rem2 = $_POST["remark2"];
    $semi = $_POST["semi"];


    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `examcode`='$exam' AND `section`='$section' AND `session`='$session' and medmcode='$medium' and
                          custcode = '$code' and vouchno != '$qvouch' and year != '$pyear'") or die(mysqli_error($conn));
    $get = mysqli_num_rows($result);

    if ($get > 0) {
        $a = 0;
        browse("This Demand Form Already Exist");
        header("refresh: ; $file?mode=last");
    }

    if ($_POST['count'] == '') {
        $count = $_POST["items"];
    } else {
        $count = $_POST["count"];
    }

    if ($_POST['count2'] == '') {
        $count2 = $_POST["items2"];
    } else {
        $count2 = $_POST["count2"];
    }

    if ($_POST['count3'] == '') {
        $count3 = $_POST["items3"];
    } else {
        $count3 = $_POST["count3"];
    }



    if ($school == "") {
        $a = 0;
        browse("School Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }
    if ($medium == "") {
        $a = 0;
        browse("Medium Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }
    if ($exam == "") {
        $a = 0;
        browse("Medium Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }
    if ($section == "") {
        $a = 0;
        browse("Medium Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }
    if ($session == "") {
        $a = 0;
        browse("Medium Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }
    if ($nopack == "") {
        $a = 0;
        browse("Medium Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    if ($a == 1) {
//        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE,`subject` write") or die(mysqli_error($conn));
//#################################TRAN PART #################################
//        if ($addflag == 1) {
//
//            $gen1 = mysqli_query($conn, "SELECT MAX(increvouch) AS maxcode FROM `$maindb` WHERE $keyfld !=''  LIMIT 0,1 ") or die(mysqli_error($conn));
//
//            if (mysqli_num_rows($gen1) > 0) {
//                $vvouch = mysqli_fetch_assoc($gen1);
//                $increvouch = $vvouch["maxcode"] + 1;
//            } else {
//                $increvouch = '51';
//            }
//            $qvouch = $myyear . $increvouch;
//        }
//        $mvouch = $qvouch;
//
//        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `vouchno`='$mvouch' and `section`='P'") or die(mysqli_error($conn));
//
//        $cntr = 0;
//        for ($j = 0; $j <= $count; $j++) {
//
//            $srno = $_POST["srno_$j"];
//            $subject = $_POST["subject_$j"];
//            $first = $_POST["first_$j"];
//            $second = $_POST["second_$j"];
//            $third = $_POST["third_$j"];
//            $fourth = $_POST["fourth_$j"];
//
//            $query = "SELECT  `code`, `name` FROM `subject` WHERE `name`='$subject'";
//            $result = mysqli_query($conn, $query)or die(mysqli_error($conn));
//            $r = mysqli_fetch_assoc($result);
//            $subcode = $r['code'];
//
//            if (!empty($subject)) {
//                $cntr++;
//                mysqli_query($conn, "INSERT INTO `$trandb`(`increvouch`,`vouchno`,`vouchdt`, `srno`, `subjcode`, `subjname`,`pq1`, `pq2`, `pq3`, `pq4`,`section`)
//                VALUES ('$increvouch','$mvouch', '$vouchdt', '$cntr', '$subcode', '$subject',  '$first', '$second', '$third', '$fourth','P')") or die(mysqli_error($conn));
//            }
//        }
//
//#################################TRAN PART 3 #################################
//
//        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `vouchno`='$mvouch' and `section`='S'") or die(mysqli_error($conn));
//
//        $cntr2 = 0;
//        for ($k = 0; $k <= $count2; $k++) {
//            $srno2 = $_POST["srno2_$k"];
//            $subject2 = $_POST["subject2_$k"];
//            $fifth = $_POST["fifth_$k"];
//            $sixth = $_POST["sixth_$k"];
//            $seventh = $_POST["seventh_$k"];
//            $eight = $_POST["eight_$k"];
//            $ninth = $_POST["ninth_$k"];
//            $tenth = $_POST["tenth_$k"];
//
//            $query2 = "SELECT  `code`, `name` FROM `subject` WHERE `name`='$subject2'";
//            $result2 = mysqli_query($conn, $query2)or die(mysqli_error($conn));
//            $r2 = mysqli_fetch_assoc($result2);
//            $subcode2 = $r2['code'];
//
//            if (!empty($subject2)) {
//                $cntr2++;
//                mysqli_query($conn, "INSERT INTO `$trandb`(`increvouch`,`vouchno`, `vouchdt`, `srno`, `subjcode`, `subjname`, `pq5`, `pq6`, `pq7`, `pq8`, `pq9`, `pq10`,`section`)
//                            VALUES ('$increvouch','$mvouch', '$vouchdt', '$cntr2', '$subcode2', '$subject2', '$fifth', '$sixth', '$seventh', '$eight', '$ninth', '$tenth','S')") or die(mysqli_error($conn));
//            }
//        }
//
//        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `vouchno`='$mvouch' and `section`='M'") or die(mysqli_error($conn));
//
//        $cntr3 = 0;
//        for ($i = 0; $i <= $count3; $i++) {
//            $srno3 = $_POST["srno3_$i"];
//            $subject3 = $_POST["subject3_$i"];
//            $pqrelm = $_POST["pqrelm_$i"];
//
//            $query3 = "SELECT  `code`, `name` FROM `subject` WHERE `name`='$subject3'";
//            $result3 = mysqli_query($conn, $query3)or die(mysqli_error($conn));
//            $r3 = mysqli_fetch_assoc($result3);
//            $subcode3 = $r['code'];
//
//            if (!empty($subject3)) {
//                $cntr3++;
//                mysqli_query($conn, "INSERT INTO `$trandb`(`increvouch`,`vouchno`, `vouchdt`,`srno`, `subjcode`, `subjname`,`pqrelm`,`section`)
//                            VALUES ('$increvouch','$mvouch', '$vouchdt', '$cntr3', '$subcode3', '$subject3','$pqrelm','M')") or die(mysqli_error($conn));
//            }
//        }
//
//#################################TRAN PART END #################################
//############################## MAIN ######################################################
//        if ($addflag == 1) {
//
//
//            mysqli_query($conn, "INSERT INTO `$maindb`(`increvouch`,`vouchno`, `year`,`vouchdt`, `accode`, `custcode`, `medmcode`,
//                                               `examcode`, `section`, `session`,`nopack`,`rem1`,`rem2`,`main`,`semi`)
//                                     VALUES ('$increvouch', '$mvouch', '$zyear', '$vouchdt', '$school', '$code', '$medium', 
//                                            '$exam', '$section', '$session', '$nopack','$rem1','$rem2','1', '$semi')") or die(mysqli_error($conn));
//
//            insmess();
//            $mvouch = $qvouch; //1
//
//
//
//
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
//        } else {
//            $mvouch = $qvouch; //1
//
//            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `accode`='$school', `custcode`='$code',
//                        `medmcode`='$medium', `examcode`='$exam', `section`='$section', `session`='$session',
//                        `nopack`='$nopack',`rem1`='$rem1', `rem2`='$rem2', `semi` = '$semi' WHERE `vouchno`= '$mvouch' ")
//                    or die(mysqli_error($conn));
//            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
//        }
//        mysqli_query($conn, "UNLOCK TABLES");
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
    $gaccode = $get['accode'];
    $gmedmcode = $get['medmcode'];
    $gexamcode = $get['examcode'];

    $fschool = mysqli_query($conn, "SELECT accode, name FROM `accmast` WHERE accode='$gaccode'") or die(mysqli_error($conn));
    $gschool = mysqli_fetch_array($fschool);

    $fmed = mysqli_query($conn, "SELECT code, name FROM `medium` WHERE code='$gmedmcode'") or die(mysqli_error($conn));
    $gmed = mysqli_fetch_array($fmed);

    $fexm = mysqli_query($conn, "SELECT code, name FROM `exam` WHERE code='$gexamcode'") or die(mysqli_error($conn));
    $gexm = mysqli_fetch_array($fexm);
}
$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {

//    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.5.7/shim.js"></script>
<script src="MyScript/dmndfrm_2.js" type="text/javascript"></script>
<script src="MyScript/dmndfrm_search.js" type="text/javascript"></script>
<script src="MyScript/school.js" type="text/javascript"></script>
<!--<link href="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.5.7/core.js" rel="text/javascript">-->
<style>
    .colr{
        border: 1px solid red;
    }
    input[type=number]::-webkit-inner-spin-button, 
    input[type=number]::-webkit-outer-spin-button { 
        -webkit-appearance: none; 
        margin: 0; 
    }
</style>
</head>
<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <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" style="color:inherit"><?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-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <?php
                                if ($get["increvouch"] == 0) {
                                    ?>
                                    <input type="text" class="form-control text-size form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" readonly>
                                    <?php
                                } else {
                                    ?>
                                    <input type="text" class="form-control text-size form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["increvouch"] : "$qvouch"; ?>" readonly>
                                    <?php
                                }
                                ?>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form"  name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="color: #6082BF;">Code</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form custcode"  name="code" value="<?php Echo ($addflag != 1) ? $get["custcode"] : '' ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="color: #6082BF;">School</label>
                            </div>
                            <div class="col-lg-8 col-md-6 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 school"  name="school" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $gschool["accode"] : ""; ?>"><?php Echo ($addflag != 1) ? $gschool["name"] : ""; ?></option>
                                    <?php
                                    $gschl = mysqli_query($conn, "SELECT accode, name FROM `accmast` where `grpcode`='010' order by name")or die(mysqli_error($conn));
                                    while ($fschl = mysqli_fetch_array($gschl)) {
                                        Echo "<option value='$fschl[accode]'>$fschl[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Medium</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 medium"  name="medium" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $gmed["code"] : ""; ?>"><?php Echo ($addflag != 1) ? $gmed["name"] : ""; ?></option>
                                    <?php
                                    if ($addflag == 1) {
                                        $gmedm = mysqli_query($conn, "SELECT * FROM `medium` order by name")or die(mysqli_error($conn));
                                        while ($fmedm = mysqli_fetch_array($gmedm)) {
                                            Echo "<option value='$fmedm[code]'>$fmedm[name]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Exam</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 exam"  name="exam" style="width: 100%" required>
                                    <?php
                                    $getexam = mysqli_query($conn, "SELECT `examcode` FROM `demdmain` ORDER BY `srl` DESC LIMIT 0,1 ")or die(mysqli_error($conn));
                                    $fexam = mysqli_fetch_array($getexam);
                                    $examcode = $fexam['examcode'];

                                    $gexam = mysqli_query($conn, "SELECT code, name FROM exam WHERE code = '$examcode' ")or die(mysqli_error($conn));
                                    $pexam = mysqli_fetch_array($gexam);
                                    $exam = $pexam['name'];
                                    $fexamcode = $pexam['code'];
                                    ?>
                                    <option value="<?php Echo ($addflag != 1) ? $gexm["code"] : "$fexamcode"; ?>"><?php Echo ($addflag != 1) ? $gexm["name"] : "$exam"; ?></option>
                                    <?php
                                    if ($addflag == 1) {

                                        $gtexm = mysqli_query($conn, "SELECT * FROM `exam` order by name")or die(mysqli_error($conn));
                                        while ($ftexm = mysqli_fetch_array($gtexm)) {
                                            Echo "<option value='$ftexm[code]'>$ftexm[name]</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Section</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-8 col-xs-8 pad">
                                <?php
                                $getsec = mysqli_query($conn, "SELECT `section` FROM `demdmain` ORDER BY `srl` DESC LIMIT 0,1 ")or die(mysqli_error($conn));
                                $fsec = mysqli_fetch_array($getsec);
                                $sec = $fsec['section'];
                                if ($addflag == 1) {
                                    ?>
                                    <select class="form-control form select2 section"  name="section" style="width: 100%" required>

                                        <option value=""></option>
                                        <option value="P" <?php
                                        if ($sec == 'P') {
                                            echo 'selected="selected"';
                                        }
                                        ?>>Primary</option>
                                        <option value="S" <?php
                                        if ($sec == 'S') {
                                            echo 'selected="selected"';
                                        }
                                        ?>>Secondary</option>
                                        <option value="M" <?php
                                        if ($sec == 'M') {
                                            echo 'selected="selected"';
                                        }
                                        ?>>Preliums</option>
                                    </select>
                                <?php } else {
                                    ?>
                                    <input type='hidden' class="form-control form  section"  name="section" value="<?php Echo ($addflag != 1) ? $get["section"] : '' ?>"required>

                                    <?php
                                    $section = $get['section'];
                                    switch ($section) {
                                        case 'P':
                                            $showsection = 'Primary';
                                            break;
                                        case 'S':
                                            $showsection = 'Secondary';
                                            break;

                                        case 'M':
                                            $showsection = 'Preliums';
                                            break;
                                    }
                                    ?>
                                    <input type = 'text' class = "form-control form  " name = "" value = "<?php Echo ($addflag != 1) ? $showsection : '' ?>" readonly>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Session</label>
                            </div>
                            <div class="col-lg-4 col-md-2 col-sm-8 col-xs-8 pad">
                                <?php
                                $getsess = mysqli_query($conn, "SELECT `session` FROM `demdmain` ORDER BY `srl` DESC LIMIT 0,1 ")or die(mysqli_error($conn));
                                $fsess = mysqli_fetch_array($getsess);
                                $sess = $fsess['session'];
                                if ($addflag == 1) {
                                    ?>
                                    <select class="form-control form select2 session"  name="session" style="width: 100%" required>
                                        <option value=""></option>
                                        <option value="M" <?php
                                        if ($sess == 'M') {
                                            echo 'selected="selected"';
                                        }
                                        ?>>Morning</option>
                                        <option value="A" <?php
                                        if ($sess == 'A') {
                                            echo 'selected="selected"';
                                        }
                                        ?>>Afternoon</option>
                                    </select>
                                <?php } else {
                                    ?>
                                    <input type='hidden' class="form-control form  session"  name="session" value="<?php Echo ($addflag != 1) ? $get["session"] : '' ?>"required>
                                    <?php
                                    $session = $get['session'];
                                    switch ($session) {
                                        case 'M':
                                            $showsession = 'Morning';
                                            break;
                                        case 'A':
                                            $showsession = 'Afternoon';
                                            break;
                                    }
                                    ?>
                                    <input type = 'text' class = "form-control form" name = "" value = "<?php Echo ($addflag != 1) ? $showsession : '' ?>" readonly>
                                <?php }
                                ?>
                            </div>

                        </div>
                        <?php
                        if ($addflag == 1) {
                            ?>
                            <div class = "row">
                                <div class = "col-lg-7 col-md-7 col-sm-7 col-xs-7 text-right pad">
                                    <input type = "button" class = "btn btn-primary proceed form" style = "color: white;background-color: #6082BF" value = "Proceed">
                                </div>
                            </div>
                            <?php
                        }
                        ?>
                        <div class="row">
                            <div class="col-lg-3 col-md-5 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">No of Packets</label>
                            </div>
                            <div class="col-lg-2 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right nopack" name="nopack" value="<?php Echo ($addflag != 1) ? $get[nopack] : "0"; ?>" readonly>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Remark</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="remark" value="<?php Echo ($addflag != 1) ? $get["rem1"] : ""; ?>" >
                                <input type="text" class="form-control form " name="remark2" value="<?php Echo ($addflag != 1) ? $get["rem2"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Semi</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-5 col-xs-5 pad">
                                <input type="text" class="form-control form " name="semi" value="<?php Echo ($addflag != 1) ? $get["semi"] : ""; ?>" >
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-7 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>

                    </div>
                </div>
                <div class="panel-body">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="float: right">
                        <div class="primary">
                            <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="3%"></th>
                                        <th width="7%" class="text-right">Sr</th>
                                        <th width="45%">Subject</th>
                                        <th width="9%" class="text-right">First</th>
                                        <th width="9%" class="text-right">Second</th>
                                        <th width="9%" class="text-right">Third</th>
                                        <th width="9%" class="text-right">Fourth</th>
                                    </tr>
                                </thead>

                                <?php
                                if ($addflag != 1) {
                                    if ($section == 'P') {
                                        ?>
                                        <tbody class="tbody">
                                            <?php
                                            $ctr = 0;
                                            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' and `section`='P' ORDER BY `srno`")or die(mysqli_error($conn));
                                            while ($tran = mysqli_fetch_array($gettran)) {

                                                Echo "<tr>";
                                                Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                                Echo "<td><input type='text' class='form-control form text-right srno'    name='srno_$ctr'  id='srno'    value='$tran[srno]' readonly></td>";
                                                Echo "<td>";
                                                Echo "<select class='form-control form subject select2' name='subject_$ctr' style='width:100%' >";
                                                Echo "<option value='$tran[subjname]'>$tran[subjname]</option>";
                                                $getmsub = mysqli_query($conn, "SELECT `subjcode`,`subjname` FROM `medprim` order by subjname")or die(mysqli_error($conn));
                                                while ($msub = mysqli_fetch_array($getmsub)) {
                                                    Echo "<option value='$msub[subjname]'>$msub[subjname]</option>";
                                                }
                                                Echo "</select>";
                                                echo "<input type='hidden' name='subname_$ctr' class='subname' value='$tran[subjcode]'>";
                                                Echo "</td>";
                                                Echo "<td class='p1'><input type='text' class='form-control form text-right first'   id='first_$ctr'    name='first_$ctr'  step='any'   value='$tran[pq1]' autocomplete='off'></td>";
                                                Echo "<td class='p2'><input type='text' class='form-control form text-right second'  id='second_$ctr' name='second_$ctr' step='any'  value='$tran[pq2]' autocomplete='off'></td>";
                                                Echo "<td class='p3'><input type='text' class='form-control form text-right third'  id='third_$ctr' name='third_$ctr' step='any'  value='$tran[pq3]' autocomplete='off'></td>";
                                                Echo "<td class='p4'><input type='text' class='form-control form text-right fourth'  id='fourth_$ctr' name='fourth_$ctr'  step='any' value='$tran[pq4]' autocomplete='off'></td>";
                                                Echo "</tr>";
                                                $ctr++;
                                            }
                                            ?>
                                        </tbody>
                                        <?php
                                    }
                                } else {
                                    ?>
                                    <tbody class="prim tbody">

                                    </tbody>
                                    <?
                                }
                                ?>
                            </table>

                            <button type="button"  class="btn btn-success btn-square form"  id="addnew">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />
                        </div>
                        <div class="secondary">
                            <table class="table table-striped table-condensed table-bordered" style="width: 100%">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="3%"></th>
                                        <th width="7%" class="text-right">Sr</th>
                                        <th width="37%">Subject</th>
                                        <th width="9%" class="text-right">Fifth</th>
                                        <th width="9%" class="text-right">Sixth</th>
                                        <th width="9%" class="text-right">Seventh</th>
                                        <th width="9%" class="text-right">Eight</th>
                                        <th width="9%" class="text-right">Ninth</th>
                                        <th width="9%" class="text-right">Tenth</th>
                                    </tr>
                                </thead>
                                <?php
                                if ($addflag != 1) {
                                    if ($section == 'S') {
                                        ?>
                                        <tbody class="tbody2">

                                            <?php
                                            $ctr2 = 0;
                                            $gettran2 = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' and `section`='S' ORDER BY `srno`")or die(mysqli_error($conn));
                                            while ($tran2 = mysqli_fetch_object($gettran2)) {

                                                Echo "<tr>";
                                                Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                                Echo "<td><input type='text' class='form-control form text-right srno2'    name='srno2_$ctr2'      value='$tran2->srno'></td>";
                                                Echo "<td>";
                                                Echo "<select class='form-control form subject2 select2' name='subject2_$ctr2' style='width:100%' >";

                                                Echo "<option value='$tran2->subjname'>$tran2->subjname</option>";
                                                $getmsub4 = mysqli_query($conn, "SELECT `subjcode`,`subjname` FROM `medseco` order by subjname") or die(mysqli_error($conn));
                                                while ($msub4 = mysqli_fetch_object($getmsub4)) {
                                                    Echo "<option value='$msub4->subjname'>$msub4->subjname</option>";
                                                }
                                                Echo "</select>";
                                                echo "<input type='hidden' class='subname2' name='subname2_$ctr2' value='$tran2->subjcode'>";
                                                Echo "</td>";
                                                Echo "<td class='p5'><input type='text' step='any' class='form-control form text-right fifth' id='fifth_$ctr2'       name='fifth_$ctr2'     value='$tran2->pq5' autocomplete='off'></td>";
                                                Echo "<td class='p6'><input type='text' step='any' class='form-control form text-right sixth' id='sixth_$ctr2'  name='sixth_$ctr2'   value='$tran2->pq6' autocomplete='off'></td>";
                                                Echo "<td class='p7'><input type='text' step='any' class='form-control form text-right seventh' id='seventh_$ctr2' name='seventh_$ctr2'   value='$tran2->pq7' autocomplete='off'></td>";
                                                Echo "<td class='p8'><input type='text' step='any' class='form-control form text-right eight' id='eight_$ctr2'  name='eight_$ctr2'   value='$tran2->pq8' autocomplete='off'></td>";
                                                Echo "<td class='p9'><input type='text' step='any' class='form-control form text-right ninth' id='ninth_$ctr2'  name='ninth_$ctr2'   value='$tran2->pq9' autocomplete='off'></td>";
                                                Echo "<td class='p10'><input type='text' step='any' class='form-control form text-right tenth' id='tenth_$ctr2'  name='tenth_$ctr2'   value='$tran2->pq10' autocomplete='off'></td>";
                                                Echo "</tr>";
                                                $ctr2++;
                                            }
                                            ?>
                                        </tbody>

                                        <?php
                                    }
                                } else {
                                    ?>
                                    <tbody class="sec tbody2">

                                    </tbody>
                                    <?
                                }
                                ?>
                            </table>

                            <button type="button"  class="btn btn-success btn-square form"  id="addnew2">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items2" name="items2" value="<?php echo ($addflag != 1) ? $ctr2 : 1; ?>" />
                        </div>
                        <div class="prelium">
                            <table class="table table-striped table-condensed table-bordered" style="width: 100%">
                                <thead>
                                    <tr style="background-color: #6082BF;">
                                        <th width="3%"></th>
                                        <th width="5%" class="text-right">Sr</th>
                                        <th width="22%">Subject</th>
                                        <th width="10%" class="text-right">Prelium</th>
                                    </tr>
                                </thead>

                                <?php
                                if ($addflag != 1) {
                                    if ($section == 'M') {
                                        ?>
                                        <tbody class="tbody3">
                                            <?php
                                            $ctr3 = 0;
                                            $gettran3 = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' and `section`='M' ORDER BY `srno`")or die(mysqli_error($conn));
                                            while ($tran3 = mysqli_fetch_array($gettran3)) {

                                                Echo "<tr>";
                                                Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                                Echo "<td><input type='text' class='form-control form text-right srno3'    name='srno3_$ctr3'      value='$tran3[srno]'></td>";
                                                Echo "<td>";
                                                Echo "<select class='form-control form subject3 select2' name='subject3_$ctr3' style='width:100%' >";
                                                Echo "<option value='$tran3[subjname]'>$tran3[subjname]</option>";
                                                $getmsub1 = mysqli_query($conn, "SELECT `subjcode`,`subjname` FROM `medprel` order by subjname")or die(mysqli_error($conn));
                                                while ($msub1 = mysqli_fetch_array($getmsub1)) {
                                                    Echo "<option value='$msub1[subjname]'>$msub1[subjname]</option>";
                                                }
                                                Echo "</select>";
                                                echo "<input type='hidden' class='subname3' name='subname3_$ctr3' value='$tran3[subjcode]'>";
                                                Echo "</td>";
                                                Echo "<td class='prel'><input type='text' step='any' class='form-control form text-right pqrelm'  id='pqrelm_$ctr3'  name='pqrelm_$ctr3'  autocomplete='off'    value='$tran3[pqrelm]'></td>";
                                                Echo "</tr>";
                                                $ctr3++;
                                            }
                                            ?>
                                        </tbody>
                                        <?php
                                    }
                                } else {
                                    ?>
                                    <tbody class="pre tbody3">

                                    </tbody>
                                    <?
                                }
                                ?>
                                </tbody>
                            </table>

                            <button type="button"  class="btn btn-success btn-square form"  id="addnew3">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items3" name="items3" value="<?php echo ($addflag != 1) ? $ctr3 : 1; ?>" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="2">Code</option>
                                <option value="1">Vouchno</option>
                                <option value="3">School</option>
                                <option value="4">Medium</option>
                                <option value="5">Exam</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Vouchno</option>
                                <option value="2">Code</option>
                                <option value="3">School</option>
                                <option value="4">Medium</option>
                                <option value="5">Exam</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#6082BF;">
                                    <th width="10%">Vouchno</th>
                                    <th width="10%">Code</th>
                                    <th width="40%">School</th>
                                    <th width="15%">Medium</th>
                                    <th width="15%">Exam</th>
                                    <th width="10%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
