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

$zyear1 = $_SESSION["zdbyear_" . $dbname];
$zyear = substr($zyear1, -4);
$zdir = "epi";
$dbyear = "epi_year";
$compcd = $_SESSION["comp_" . $dbname];


$username = $_SESSION["username"];
$cacmst = "accmast";

$cgrpmast = "grpmast";
$cgrphead = "grphead";

$ccenter = "center";
$cmedium = "medium";
$cmedprim = "medprim";
$cmedseco = "medseco";
$cmedprel = "medprel";

$csubject = "subject";
$cdistrict = "district";
$cexam = "exam";
$ccourier = "courier";
$cstdepot = "stdepot";
$cpaprate = "paprate";
$zroot = $_SESSION["zdir"];
$zdir = substr($zroot, 0, -4);

$zdb = $_SESSION["zdbyear_" . $dbname];
//browse($zdb);
mysqli_select_db($conn, $zdb);

function mysql_current_db() {
    $r = mysqli_query($conn, "SELECT DATABASE()") or die(mysqli_error($conn));
    //browse("Current DB->".mysql_result($r,0));
}

if (isset($_POST["save"])) {
    $fryear = $zdir . '1_' . $_POST["frmyear"];
    $toyear = $zdir . '1_' . $_POST["toyear"];
//    $fryear = "apps4rea_" . $zdir . '_1_' . $_POST["frmyear"];
//    $toyear = "apps4rea_" . $zdir . '_1_' . $_POST["toyear"];

    $fyear = $_POST["frmyear"];
    $tyear = $_POST["toyear"];
    /*     * **********Copying **************** */
    browse($fryear);
    browse($toyear);
//    die();
    mysqli_select_db($conn, $fryear);
    $acqry = mysqli_query($conn, "SELECT * FROM `$cacmst`") or die(mysqli_error($conn));
    $grpmqry = mysqli_query($conn, "SELECT * FROM `$cgrpmast`") or die(mysqli_error($conn));
    $grpheadqry = mysqli_query($conn, "SELECT * FROM `$cgrphead`") or die(mysqli_error($conn));
    $centerqry = mysqli_query($conn, "SELECT * FROM `$ccenter`") or die(mysqli_error($conn));
    $mediumqry = mysqli_query($conn, "SELECT * FROM `$cmedium`") or die(mysqli_error($conn));
    $medprimqry = mysqli_query($conn, "SELECT * FROM `$cmedprim`") or die(mysqli_error($conn));
    $medsecoqry = mysqli_query($conn, "SELECT * FROM `$cmedseco`") or die(mysqli_error($conn));
    $medprelqry = mysqli_query($conn, "SELECT * FROM `$cmedprel`") or die(mysqli_error($conn));
    $subjectqry = mysqli_query($conn, "SELECT * FROM `$csubject`") or die(mysqli_error($conn));
    $districtqry = mysqli_query($conn, "SELECT * FROM `$cdistrict`") or die(mysqli_error($conn));
    $examqry = mysqli_query($conn, "SELECT * FROM `$cexam`") or die(mysqli_error($conn));
    $courierqry = mysqli_query($conn, "SELECT * FROM `$ccourier`") or die(mysqli_error($conn));
    $stdepotqry = mysqli_query($conn, "SELECT * FROM `$cstdepot`") or die(mysqli_error($conn));
    $paprateqry = mysqli_query($conn, "SELECT * FROM `$cpaprate`") or die(mysqli_error($conn));

    $demandqry = mysqli_query($conn, "SELECT * FROM `demdmain` where vouchno not in (Select `demdvouch` from `invmain`) order by vouchno") or die(mysqli_error($conn));

    while ($rowdemd = mysqli_fetch_array($demandqry)) {


        $year = $rowdemd['year'];
        $vouchno = $rowdemd['vouchno'];
        $vouchdt = $rowdemd['vouchdt'];
        $custcode = $rowdemd['custcode'];
        $accode = $rowdemd['accode'];
        $billno = $rowdemd['billno'];
        $medmcode = $rowdemd['medmcode'];
        $examcode = $rowdemd['examcode'];
        $section = $rowdemd['section'];
        $session = $rowdemd['session'];
        $nopack = $rowdemd['nopack'];
        $rem1 = $rowdemd['rem1'];
        $rem2 = $rowdemd['rem2'];
        $main = $rowdemd['main'];
        $semi = $rowdemd['semi'];
        $yearno = $rowdemd['yearno'];

        mysqli_select_db($conn, $toyear);

        $chkdemand = mysqli_query($conn, "Select * from `demdmain` where `vouchno`='$vouchno'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkdemand) > 0) {
            mysqli_query($conn, "UPDATE `demdmain` SET `year`='$year', `vouchdt`='$vouchdt', `custcode`='$custcode', 
                                                `accode`='$accode', `billno`='$billno', `medmcode`='$medmcode', `examcode`='$examcode', `section`='$section',
                                                `session`='$session', `nopack`='$nopack', `rem1`='$rem1', `rem2`='$rem2', `main`='$main',
                                                `semi`='$semi', `yearno`='$yearno'  WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "INSERT INTO `demdmain`(`year`, `vouchno`, `increvouch`, `vouchdt`, `custcode`, 
                                                `accode`, `billno`, `medmcode`, `examcode`, `section`,
                                                `session`, `nopack`, `rem1`, `rem2`, `main`, `semi`, `yearno`)
                                         VALUES('$year', '$vouchno', '', '$vouchdt', '$custcode', 
                                                '$accode', '$billno', '$medmcode', '$examcode', '$section',
                                                '$session', '$nopack', '$rem1', '$rem2', '$main', '$semi', '$yearno')") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "Delete from `demdtran` where vouchno='$vouchno'") or die(mysqli_error($conn));
        mysqli_select_db($conn, $fryear);

        $demdtrn = mysqli_query($conn, "SELECT * FROM `demdtran` where vouchno = '$vouchno'") or die(mysqli_error($conn));

        while ($rwdmdtrn = mysqli_fetch_array($demdtrn)) {

            $srno = $rwdmdtrn['srno'];
            $subjcode = $rwdmdtrn['subjcode'];
            $subjname = $rwdmdtrn['subjname'];
            $section = $rwdmdtrn['section'];
            $pq1 = $rwdmdtrn['pq1'];
            $pq2 = $rwdmdtrn['pq2'];
            $pq3 = $rwdmdtrn['pq3'];
            $pq4 = $rwdmdtrn['pq4'];
            $pq5 = $rwdmdtrn['pq5'];
            $pq6 = $rwdmdtrn['pq6'];
            $pq7 = $rwdmdtrn['pq7'];
            $pq8 = $rwdmdtrn['pq8'];
            $pq9 = $rwdmdtrn['pq9'];
            $pq10 = $rwdmdtrn['pq10'];
            $pqrelm = $rwdmdtrn['pqrelm'];
            mysqli_select_db($conn, $toyear);

            mysqli_query($conn, "INSERT INTO `demdtran`( `vouchno`, `increvouch`, `vouchdt`, `srno`, `subjcode`, `subjname`, `section`,
                                                 `pq1`, `pq2`, `pq3`, `pq4`, `pq5`, `pq6`, `pq7`, `pq8`, `pq9`, `pq10`, `pqrelm`)
                                         VALUES('$vouchno', '', '$vouchdt', '$srno', '$subjcode', '$subjname', '$section',
                                                 '$pq1', '$pq2', '$pq3', '$pq4', '$pq5', '$pq6', '$pq7', '$pq8', '$pq9', '$pq10', '$pqrelm')") or die(mysqli_error($conn));
        }
    }

    mysqli_select_db($conn, $fryear);


//    previous bills for receive 
    mysqli_query($conn, "create temporary table getdbf as  (SELECT `yearno`,`type`,`vouchno`, `billdate`,`billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                      `billdt`, `billamt`, `accode`, `examcode` FROM `prevbill` WHERE 
                      `type` = 'S' order by billdate)
                       UNION ALL
                       (SELECT '$fyear' AS `yearno`, 'S' AS `type`, `vouchno`, `vouchdt` as billdate,`vouchno` as billno,
                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdt`,  `billamt`, `accode`, `examcode` FROM `invmain`
                         order by vouchdt)") or die(mysqli_error($conn));

    $subtmp1 = mysqli_query($conn, "SELECT * FROM `getdbf` order by billdate") or die(mysqli_error($conn));

//    print table($subtmp1);
//    die();
    mysqli_query($conn, "Delete from `$toyear`.`prevbill` WHERE `frmprev` = 'Y'") or die(mysqli_error($conn));

    while ($subtmp = mysqli_fetch_assoc($subtmp1)) {
        $qparty = $subtmp["accode"];
        $qyear = $subtmp["yearno"];
        $qbill = $subtmp["billno"];
        $qosclear = $subtmp["osclear"];
        $qpacklistno = $subtmp["packlistno"];
        $qtotcartons = $subtmp["totcartons"];
        $mbilldate = $subtmp["billdate"];
        $qvouch = $subtmp["vouchno"];
        $qamt = $subtmp["billamt"];
        $qtype = $subtmp["type"];
        $qsrno = $subtmp["srno"];
        $qexamcode = $subtmp["examcode"];

        if ($qbill == 5320) {
            browse('a');
            browse($qexamcode);
        }

        $totadj = 0;
        $totset = 0;
        mysqli_select_db($conn, $fryear);

        $trn = mysqli_query($conn, "SELECT * FROM $fryear.`billsett` WHERE `party` = '$qparty' AND `type` = '$qtype' and 
                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch'") or die(mysqli_error($conn));
//                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = '$mtype' ") or die(mysqli_error($conn));

        $tranqry = mysqli_fetch_assoc($trn);
        if (mysqli_num_rows($trn) > 0) {
            $totset = $tranqry["setoff"];
        }

        $trn2 = mysqli_query($conn, " SELECT SUM(`setoff`) AS `totadj`, nodays FROM $fryear.`billsett` WHERE 
                                     `yearno` = '$qyear' AND `vouchno` = '$qvouch' AND `type` = '$qtype' and 
                                     `party` = '$qparty' LIMIT 0,1 ") or die(mysqli_error($conn));



        $tranqry2 = mysqli_fetch_assoc($trn2);
        $totadj = $tranqry2["totadj"];
        $amtadj = ($totadj - $totset);
        $setoff = $totset;
        $balamt = $qamt - $totadj;

        $nodays = $mbilldate - $mvouchdt;
//                print $nodays;


        if ($balamt > 0) {
            $counter++;

            mysqli_select_db($conn, $fryear);
            if ($qexamcode == '') {
                if ($qyear = $fyear) {
                    $getexm = mysqli_query($conn, "SELECT `examcode` FROM `invmain` WHERE `year` = '$fyear' and vouchno='$qvouch'") or die(mysqli_error($conn));
                    $num = mysqli_num_rows($getexm);
                    if ($num > 0) {
                        $get = mysqli_fetch_array($getexm);
                        $qexamcode = $get['examcode'];
                        if ($qbill == 5320) {
                            browse('1');
                            browse($qexamcode);
                            browse($qyear);
                            browse($fyear);
                        }
                    }
                } else {
                    $getpexm = mysqli_query($conn, "SELECT * FROM `prevbill` WHERE `yearno` = '$qyear' and vouchno='$qvouch'") or die(mysqli_error($conn));
                    $getp = mysqli_fetch_array($getpexm);
                    $qexamcode = $getp['examcode'];
                    if ($qbill == 5320) {
                        browse('2');
                        browse($qexamcode);
                    }
                }
            }
            mysqli_select_db($conn, $toyear);

            mysqli_query($conn, "Insert into $toyear.prevbill (`yearno`, `accode`, `type`, `srno`, `billno`, `billdate`, `billamt`, `chalno`, `vouchno`, `cashcred`, `examcode`, `billdate_a`, `frmprev`)
                                               VALUES ('$qyear', '$qparty', '$qtype', '$qsrno', '$qbill', '$mbilldate', '$balamt', '', '$qvouch', '', '$qexamcode', '', 'Y') ") OR die(mysqli_error($conn));
        }
    }

//    previous bills for giving 
//    mysqli_query($conn, "create temporary table getdbf2 as  (SELECT `yearno`,`type`,`vouchno`, `billdate`,`billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
//                      `billdt`, `billamt`, `accode` FROM `prevbill` WHERE 
//                      `type` = 'P' order by billdate)
//                       UNION ALL
//                       (SELECT '$fyear' AS `yearno`, 'P' AS `type`, `vouchno`, `vouchdt` as billdate,`vouchno` as billno,
//                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdt`,  `billamt`, `accode` FROM `purmain`
//                       order by vouchdt)") or die(mysqli_error($conn));
//
//    $subtmp2 = mysqli_query($conn, "SELECT * FROM `getdbf2` order by billdate") or die(mysqli_error($conn));
//
//
//
//    while ($sbtmp = mysqli_fetch_assoc($subtmp2)) {
//        $qparty = $sbtmp["accode"];
//        $qyear = $sbtmp["yearno"];
//        $qbill = $sbtmp["billno"];
//        $qosclear = $sbtmp["osclear"];
//        $qpacklistno = $sbtmp["packlistno"];
//        $qtotcartons = $sbtmp["totcartons"];
//        $mbilldate = $sbtmp["billdt"];
//        $qvouch = $sbtmp["vouchno"];
//        $qamt = $sbtmp["billamt"];
//        $qtype = $sbtmp["type"];
//        $qsrno = $sbtmp["srno"];
//
//
//        $totadj = 0;
//        $totset = 0;
//        mysqli_select_db($conn, $fryear);
//
//        $trn = mysqli_query($conn, "SELECT * FROM $fryear.`billsett` WHERE `source` = '$msource' AND
//                                   `bnkvouch` = '$mvouch' $binitcond AND `party` = '$qparty' AND `type` = '$qtype' and 
//                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch'") or die(mysqli_error($conn));
////                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = '$mtype' ") or die(mysqli_error($conn));
//
//        $tranqry = mysqli_fetch_assoc($trn);
//        if (mysqli_num_rows($trn) > 0) {
//            $totset = $tranqry["setoff"];
//        }
//
//        $trn2 = mysqli_query($conn, " SELECT SUM(`setoff`) AS `totadj`, nodays FROM $fryear.`billsett` WHERE 
//                                     `yearno` = '$qyear' AND `vouchno` = '$qvouch' AND `type` = '$qtype' and 
//                                     `party` = '$qparty' LIMIT 0,1 ") or die(mysqli_error($conn));
//
//
//
//        $tranqry2 = mysqli_fetch_assoc($trn2);
//        $totadj = $tranqry2["totadj"];
//        $amtadj = ($totadj - $totset);
//        $setoff = $totset;
//        $balamt = $qamt - $totadj;
//
//        $nodays = $mbilldate - $mvouchdt;
////                print $nodays;
//
//
//        if ($qamt - $amtadj > 0) {
//            $counter++;
//
//
//            mysqli_select_db($conn, $toyear);
//
//            mysqli_query($conn, "Insert into $toyear.prevbill (`yearno`, `accode`, `type`, `srno`, `billno`, `billdate`, `billamt`, `chalno`, `vouchno`, `cashcred`, `examcode`, `billdate_a`, `frmprev`)
//                                               VALUES ('$qyear', '$qparty', '$qtype', '$qsrno', '$qbill', '$mbilldate', '$balamt', '', '$qvouch', '', '', '', 'Y') ") OR die(mysqli_error($conn));
//        }
//    }

    browse('paprate');

    while ($rowpaprate = mysqli_fetch_array($paprateqry)) {
        $srno = $rowpaprate["srno"];
//        $code = $rowpaprate["code"];
        $subjcode = mysqli_real_escape_string($conn, $rowpaprate["subjcode"]);
        $subjname = mysqli_real_escape_string($conn, $rowpaprate["subjname"]);
        $examcode = mysqli_real_escape_string($conn, $rowpaprate["examcode"]);
        $examname = mysqli_real_escape_string($conn, $rowpaprate["examname"]);
        $section = mysqli_real_escape_string($conn, $rowpaprate["section"]);
        $rt1 = $rowpaprate["rt1"];
        $rt2 = $rowpaprate["rt2"];
        $rt3 = $rowpaprate["rt3"];
        $rt4 = $rowpaprate["rt4"];
        $rt5 = $rowpaprate["rt5"];
        $rt6 = $rowpaprate["rt6"];
        $rt7 = $rowpaprate["rt7"];
        $rt8 = $rowpaprate["rt8"];
        $rt9 = $rowpaprate["rt9"];
        $rt10 = $rowpaprate["rt10"];
        $rtprelm = $rowpaprate["rtprelm"];
        $ppackrate = $rowpaprate["ppackrate"];
        $spackrate = $rowpaprate["spackrate"];
        $mpackrate = $rowpaprate["mpackrate"];

        mysqli_select_db($conn, $toyear);
        $chkpaprate = mysqli_query($conn, "SELECT * FROM `$cpaprate` WHERE `subjcode`='$subjcode' and examcode='$examcode' and `section`='$section' and srno = '$srno'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkpaprate) > 0) {
            mysqli_query($conn, "UPDATE `$cpaprate` SET `subjcode`='$subjcode',
                    `subjname`='$subjname',
                    `examcode`='$examcode',`examname`='$examname',
                                                    `section`='$section',
                    `rt1`='$rt1',`rt2`='$rt2',`rt3`='$rt3',`rt4`='$rt4',
                            `rt5`='$rt5',`rt6`='$rt6',`rt7`='$rt7',`rt8`='$rt8',`rt9`='$rt9',
                            `rt10`='$rt10',`rtprelm`='$rtprelm',`ppackrate`='$ppackrate',`spackrate`='$spackrate',
                            `mpackrate`='$mpackrate' WHERE subjcode='$subjcode' and `srno`='$srno'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cpaprate Select * from $fryear.$cpaprate where subjcode='$subjcode' and `srno`='$srno'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cpaprate`(`subjcode`,
//                                                `subjname`, `examcode`,
//                                                `examname`, `section`,
//                                                `rt1`, `rt2`, `rt3`, `rt4`,
//                                                `rt5`, `rt6`, `rt7`, `rt8`, `rt9`, `rt10`, `rtprelm`,
//                                                `ppackrate`, `spackrate`, `mpackrate`, `srno`)
//                                        VALUES('$subjcode',
//                                                '$subjname', '$examcode',
//                                                '$examname', '$section', 
//                                                '$rt1', '$rt2','$rt3','$rt4',
//                                                '$rt5','$rt6','$rt7','$rt8','$rt9','$rt10','$rtprelm',
//                                                '$ppackrate', '$spackrate', '$mpackrate', '$srno')") or die(mysqli_error($conn));
        }
    }

    browse('center');

    while ($rowcenter = mysqli_fetch_array($centerqry)) {
        $mvouch = $rowcenter["vouchno"];
        $name = $rowcenter["name"];
        $code = $rowcenter["code"];
        $centcode = $rowcenter["centcode"];
        $add1 = mysqli_real_escape_string($conn, $rowcenter["add1"]);
        $add2 = mysqli_real_escape_string($conn, $rowcenter["add2"]);
        $add3 = mysqli_real_escape_string($conn, $rowcenter["add3"]);
        $city = $rowcenter["city"];
        $pin = $rowcenter["pin"];
        $coun = $rowcenter["country"];
        $station = $rowcenter["station"];
        $mob = $rowcenter["mobile"];
        $tel = $rowcenter["tel"];
        $email = $rowcenter["email"];
        $web = $rowcenter["website"];
        $fax = $rowcenter["fax"];

        mysqli_select_db($conn, $toyear);
        $chkcenter = mysqli_query($conn, "SELECT * FROM `$ccenter` WHERE `code`='$code'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkcenter) > 0) {
            mysqli_query($conn, "UPDATE `$ccenter` SET `name`='$name',`code`='$code', `add1`='$add1', `add2`='$add2', `add3`='$add3', `city`='$city', `pin`='$pin', `station`='$station', `country`='$coun', mobile= '$mob',telno='$tel',
                         mobile='$mob', `email`='$email', `website`='$web', `fax`='$fax', code='$code', `centcode`='$centcode' WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$ccenter Select * from $fryear.$ccenter where code='$code'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$ccenter`(`code`,`vouchno`, `name`, `centcode`, `add1`, `add2`, `add3`, `city`, `pin`, `station`, `country`, mobile ,telno,
//                                                 `email`,`website`,`fax`)
//                                        VALUES('$code', '$mvouch', '$name', '$centcode', '$add1', '$add2', '$add3', '$city', '$pin', '$station', '$coun', '$mob' , '$tel',
//                                                '$email', '$web','$fax')") or die(mysqli_error($conn));
        }
    }
    browse('subject');

    while ($rowsubject = mysqli_fetch_array($subjectqry)) {
        $name = $rowsubject["name"];
        $code = $rowsubject["code"];

        mysqli_select_db($conn, $toyear);
        $chksubject = mysqli_query($conn, "SELECT * FROM `$csubject` WHERE `code`='$code'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chksubject) > 0) {
            mysqli_query($conn, "UPDATE `$csubject` SET `name`='$name'  WHERE code='$code'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$csubject Select * from $fryear.$csubject where code='$code'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$csubject`(`code`,`name`)
//                                        VALUES('$code', '$name')") or die(mysqli_error($conn));
        }
    }
    browse('district');

    while ($rowdistrict = mysqli_fetch_array($districtqry)) {
        $name = $rowdistrict["name"];
        $code = $rowdistrict["code"];

        mysqli_select_db($conn, $toyear);
        $chkdistrict = mysqli_query($conn, "SELECT * FROM `$cdistrict` WHERE `code`='$code'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkdistrict) > 0) {
            mysqli_query($conn, "UPDATE `$cdistrict` SET `name`='$name'  WHERE code='$code'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cdistrict Select * from $fryear.$cdistrict where code='$code'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cdistrict`(`code`,`name`)
//                                        VALUES('$code', '$name')") or die(mysqli_error($conn));
        }
    }
    browse('exam');

    while ($rowexam = mysqli_fetch_array($examqry)) {
        $name = $rowexam["name"];
        $code = $rowexam["code"];
        $xignore = $rowexam["xignore"];

        mysqli_select_db($conn, $toyear);
        $chkexam = mysqli_query($conn, "SELECT * FROM `$cexam` WHERE `code`='$code'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkexam) > 0) {
            mysqli_query($conn, "UPDATE `$cexam` SET `name`='$name', `xignore`='$xignore'  WHERE code='$code'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cexam Select * from $fryear.$cexam where code='$code'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cexam`(`code`,`name`, `xignore`)
//                                        VALUES('$code', '$name', '$xignore')") or die(mysqli_error($conn));
        }
    }
    browse('medium');

    while ($rowmedium = mysqli_fetch_array($mediumqry)) {
        $name = $rowmedium["name"];
        $code = $rowmedium["code"];

        mysqli_select_db($conn, $toyear);
        $chkmedium = mysqli_query($conn, "SELECT * FROM `$cmedium` WHERE `code`='$code'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkmedium) > 0) {
            mysqli_query($conn, "UPDATE `$cmedium` SET `name`='$name'  WHERE code='$code'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cmedium Select * from $fryear.$cmedium where code='$code'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cmedium`(`code`,`name`)
//                                        VALUES('$code', '$name')") or die(mysqli_error($conn));
        }
    }
    browse('mediumprelium');

    while ($rowmediumprel = mysqli_fetch_array($medprelqry)) {
        $srno = $rowmediumprel["srno"];
        $code = $rowmediumprel["code"];
        $subjcode = $rowmediumprel["subjcode"];
        $subjname = $rowmediumprel["subjname"];
        $fprel = $rowmediumprel["fprel"];

        mysqli_select_db($conn, $toyear);
        $chkmediumprel = mysqli_query($conn, "SELECT * FROM `$cmedprel` WHERE `code`='$code' and srno = '$srno'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkmediumprel) > 0) {
            mysqli_query($conn, "UPDATE `$cmedprel` SET `subjcode`='$subjcode', `subjname`='$subjname', `fprel`='$fprel'  WHERE code='$code' and `srno`='$srno'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cmedprel Select * from $fryear.$cmedprel where code='$code' and srno = '$srno'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cmedprel`(`code`,`subjcode`, `subjname`, `fprel`, `srno`)
//                                        VALUES('$code', '$subjcode', '$subjname', '$fprel', '$srno')") or die(mysqli_error($conn));
        }
    }
    browse('mediumprimary');

    while ($rowmediumprim = mysqli_fetch_array($medprimqry)) {
        $srno = $rowmediumprim["srno"];
        $code = $rowmediumprim["code"];
        $subjcode = $rowmediumprim["subjcode"];
        $subjname = $rowmediumprim["subjname"];
        $f1 = $rowmediumprim["f1"];
        $f2 = $rowmediumprim["f2"];
        $f3 = $rowmediumprim["f3"];
        $f4 = $rowmediumprim["f4"];

        mysqli_select_db($conn, $toyear);
        $chkmediumprim = mysqli_query($conn, "SELECT * FROM `$cmedprim` WHERE `code`='$code' and srno = '$srno'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkmediumprim) > 0) {
            mysqli_query($conn, "UPDATE `$cmedprim` SET `subjcode`='$subjcode', `subjname`='$subjname', `f1`='$f1', `f2`='$f2',`f3`='$f3',`f4`='$f4'  WHERE code='$code' and `srno`='$srno'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cmedprim Select * from $fryear.$cmedprim where code='$code' and srno = '$srno'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cmedprim`(`code`,`subjcode`, `subjname`, `f1`, `f2`, `f3`, `f4`, `srno`)
//                                        VALUES('$code', '$subjcode', '$subjname', '$f1', '$f2', '$f3', '$f4', '$srno')") or die(mysqli_error($conn));
        }
    }
    browse('mediumsecondary');

    while ($rowmediumsec = mysqli_fetch_array($medsecoqry)) {
        $srno = $rowmediumsec["srno"];
        $code = $rowmediumsec["code"];
        $subjcode = $rowmediumsec["subjcode"];
        $subjname = $rowmediumsec["subjname"];
        $f5 = $rowmediumsec["f5"];
        $f6 = $rowmediumsec["f6"];
        $f7 = $rowmediumsec["f7"];
        $f8 = $rowmediumsec["f8"];
        $f9 = $rowmediumsec["f9"];
        $f10 = $rowmediumsec["f10"];

        mysqli_select_db($conn, $toyear);
        $chkmediumseco = mysqli_query($conn, "SELECT * FROM `$cmedseco` WHERE `code`='$code' and srno = '$srno'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkmediumseco) > 0) {
            mysqli_query($conn, "UPDATE `$cmedseco` SET `subjcode`='$subjcode', `subjname`='$subjname',  `f5`='$f5', `f6`='$f6',
                                              `f7`='$f7', `f8`='$f8', `f9`='$f9', `f10`='$f10'  WHERE code='$code' and `srno`='$srno'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cmedseco Select * from $fryear.$cmedseco where code='$code' and srno = '$srno'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cmedseco`(`code`,`subjcode`, `subjname`, `f5`,`f6`,`f7`,`f8`, `f9`, `f10`,`srno`)
//                                        VALUES('$code', '$subjcode', '$subjname', '$f5','$f6','$f7','$f8', '$f9', '$f10', '$srno')") or die(mysqli_error($conn));
        }
    }

    browse('grphead');
    while ($rowhead = mysqli_fetch_assoc($grpheadqry)) {
        $grphdsrno = $rowhead["srno"];
        $grphdname = $rowhead["name"];
        $grphdtype = $rowhead["type"];
        $grphdsubtype = $rowhead["subtype"];
        $grphdcode = $rowhead["code"];


        mysqli_select_db($conn, $toyear);
        $chkgrhdpmast = mysqli_query($conn, "SELECT * FROM `$cgrphead` WHERE `code`='$grphdcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkgrhdpmast) > 0) {
            mysqli_query($conn, "UPDATE `$cgrphead` SET `name`='$grphdname', `type`='$grphdtype', `subtype`='$grphdsubtype'
                                                        WHERE code='$grphdcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cgrphead Select * from $fryear.$cgrphead where code='$grphdcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cgrphead`(`srno`, `code`, `name`, `type`, `subtype`)
//                                          VALUES('$grphdsrno', '$grphdcode', '$grphdname', '$grphdtype', '$grphdsubtype')") or die(mysqli_error($conn));
        }
    }

    browse('grpmast');
    while ($row2 = mysqli_fetch_assoc($grpmqry)) {
        $grpcode = $row2["grpcode"];
        $name2 = $row2["name"];
        $type = $row2["type"];
        $subtype = $row2["subtype"];
        $add_info = $row2["add_info"];
        $initial = $row2["initial"];
        $headcode = $row2["headcode"];

        mysqli_select_db($conn, $toyear);
        $chkgrpmast = mysqli_query($conn, "SELECT * FROM `$cgrpmast` WHERE `grpcode`='$grpcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkgrpmast) > 0) {
            mysqli_query($conn, "UPDATE `$cgrpmast` SET `name`='$name2', `type`='$type', `subtype`='$subtype', `add_info`='$add_info', `initial`='$initial',
                                                         headcode='$headcode' WHERE grpcode='$grpcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cgrpmast Select * from $fryear.$cgrpmast where grpcode='$grpcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cgrpmast`(`grpcode`, `name`, `type`, `subtype`, `add_info`, `initial`, headcode)
//                                                 VALUES('$grpcode', '$name2', '$type', '$subtype', '$add_info', '$initial', '$headcode')") or die(mysqli_error($conn));
        }
    }
    
//    $profqry = mysqli_query($conn, "SELECT amount FROM $fryear.`profit`") or die(mysqli_error($conn));
//    $fetprof = mysqli_fetch_array($profqry);
//    $mprof = $fetprof["amount"];

    mysqli_select_db($conn, $fryear);
    mysqli_query($conn, "insert into `acmt` (`amt`, `accode`) SELECT SUM(amount) AS amt,accode FROM $fryear.`actran` WHERE `chqret` !='Y' and retdate = '0000-00-00'  group by accode") or die(mysqli_error($conn));

    browse('accmast');
    while ($row = mysqli_fetch_assoc($acqry)) {
        $accode = $row["accode"];
        $name = mysqli_real_escape_string($conn, $row["name"]);
//        $consname = $row["consname"];
        $add1 = mysqli_real_escape_string($conn, $row["add1"]);
        $add2 = mysqli_real_escape_string($conn, $row["add2"]);
        $add3 = mysqli_real_escape_string($conn, $row["add3"]);
//        $add4 = $row["add4"];
        $grpcode = $row["grpcode"];
        $custtype = $row["custtype"];
        $custcode = mysqli_real_escape_string($conn, $row["custcode"]);
        $deptcode = mysqli_real_escape_string($conn, $row["deptcode"]);
        $type = mysqli_real_escape_string($conn, $row["type"]);
        $init = mysqli_real_escape_string($conn, $row["init"]);
        $bankinit = mysqli_real_escape_string($conn, $row["bankinit"]);
        $tinno = mysqli_real_escape_string($conn, $row["tinno"]);
        $city = mysqli_real_escape_string($conn, $row["city"]);
        $pin = mysqli_real_escape_string($conn, $row["pin"]);
        $country = mysqli_real_escape_string($conn, $row["country"]);
        $station = mysqli_real_escape_string($conn, $row["station"]);
        $tel = mysqli_real_escape_string($conn, $row["tel"]);
        $fax = mysqli_real_escape_string($conn, $row["fax"]);
        $mobile = mysqli_real_escape_string($conn, $row["mobile"]);
        $mobile2 = mysqli_real_escape_string($conn, $row["mobile2"]);
        $email = mysqli_real_escape_string($conn, $row["email"]);
        $openbal = mysqli_real_escape_string($conn, $row["openbal"]);
        $dc = mysqli_real_escape_string($conn, $row["dc"]);
        $owner = mysqli_real_escape_string($conn, $row["owner"]);
        $creditlimit = mysqli_real_escape_string($conn, $row["credlimit"]);
        $website = mysqli_real_escape_string($conn, $row["website"]);
        $contpers = mysqli_real_escape_string($conn, $row["contpers"]);
        $vattin = mysqli_real_escape_string($conn, $row["vattin"]);
        $csttin = mysqli_real_escape_string($conn, $row["csttin"]);
        $pattern = mysqli_real_escape_string($conn, $row["pattern"]);
        $closbal = mysqli_real_escape_string($conn, $row["closbal"]);
        $closbdc = mysqli_real_escape_string($conn, $row["closbc"]);
        $pfund = mysqli_real_escape_string($conn, $row["pfund"]);
        $saltype = mysqli_real_escape_string($conn, $row["saltype"]);
        $salwise = mysqli_real_escape_string($conn, $row["salwise"]);
        $salmon = mysqli_real_escape_string($conn, $row["salmon"]);
        $travmon = mysqli_real_escape_string($conn, $row["travmon"]);
        $convmon = mysqli_real_escape_string($conn, $row["convmon"]);
        $p_tax = mysqli_real_escape_string($conn, $row["p_tax"]);
        $leaveflag = mysqli_real_escape_string($conn, $row["leaveflag"]);
        $leavecode = mysqli_real_escape_string($conn, $row["leavecode"]);
        $esic = mysqli_real_escape_string($conn, $row["esic"]);
        $salary = mysqli_real_escape_string($conn, $row["salary"]);
        $active = mysqli_real_escape_string($conn, $row["active"]);
        $username = mysqli_real_escape_string($conn, $row["username"]);
        $pan = mysqli_real_escape_string($conn, $row["pan"]);
        $staff = mysqli_real_escape_string($conn, $row["staff"]);
        $transporter = mysqli_real_escape_string($conn, $row["transporter"]);
//        $transport = mysqli_real_escape_string($conn, $row["transport"]);
        $lbt = mysqli_real_escape_string($conn, $row["lbt"]);
        $medmcode = mysqli_real_escape_string($conn, $row["medmcode"]);
        $district = mysqli_real_escape_string($conn, $row["district"]);
        $courcode = mysqli_real_escape_string($conn, $row["courcode"]);
        $stcode = mysqli_real_escape_string($conn, $row["stcode"]);
        $centcode = mysqli_real_escape_string($conn, $row["centcode"]);
        $bankacno = mysqli_real_escape_string($conn, $row["bankacno"]);
        $grpaccode = mysqli_real_escape_string($conn, $row["grpaccode"]);


        if ($dc == "D") {
            $nopen = abs($openbal) * -1;
        } else {
            $nopen = abs($openbal);
        }
        if ($accode == "51") {
            mysql_current_db();
        }
        mysqli_select_db($conn, $fryear);
        $actranqry = mysqli_query($conn, "SELECT amt FROM `acmt` WHERE accode='$accode'") or die(mysqli_error($conn));
        $get = mysqli_fetch_array($actranqry);
        $balamt = $get["amt"];

        $topen = $nopen + $balamt;

        if ($topen > 0) {
            $tdc = "C";
        } else {
            $tdc = "D";
        }

        $topen = abs($topen);

        $gettypeqry = mysqli_query($conn, "SELECT * FROM $fryear.$cgrpmast WHERE grpcode='$grpcode'") or die(mysqli_error($conn));
        $fettype = mysqli_fetch_array($gettypeqry);
        $mgtype = $fettype['type'];



        $mamt = 0;
        if ($mgtype != "I" && $mgtype != "E") {
            $getamtqry = mysqli_query($conn, "SELECT amt as tamt FROM acmt WHERE accode='$accode'") or die(mysqli_error($conn));
            $fetamt = mysqli_fetch_array($getamtqry);
            $mamt = $fetamt['tamt'];
        } else {
            $mamt = 0;
        }
        if ($grpcode == '050' and $percent > 0) {
            $mpl = $mprof * ($percent / 100);
        } else {
            $mpl = 0;
        }

        if ($mgtype != "I" && $mgtype != "E") {
            switch ($accode) {
                case '7':
                    $closbal = $openbal + $currbal;
                    break;
                case '23':
                    $closbal = $openbal + $currbal;
                    break;
                case '24':
                    break;

                default:
                    $currbal = $mamt;
                    $closbal = $openbal + $currbal + $mpl;


                    break;
            }
        } else {
            $currbal = 0.00;
            $closbal = 0.00;
        }

        if ($currbal > 0) {
            $currdc = "C";
        } else {
            $currdc = "D";
        }

        if ($grpcode == '050') {
            if ($closbal < 0) {
                $closbdc = "C";
            } else {
                $closbdc = "D";
            }
        } ELSE {
            if ($closbal > 0) {
                $closbdc = "C";
            } else {
                $closbdc = "D";
            }
        }

        /*        if($accode=="51"){
          browse($balamt);
          browse($nopen);
          browse($topen);
          browse($tdc);
          } */

        mysqli_select_db($conn, $toyear);
        $chkac = mysqli_query($conn, "SELECT * FROM `$cacmst` WHERE accode = '$accode'");
        if (mysqli_num_rows($chkac) > 0) {
            mysqli_query($conn, "UPDATE `$cacmst` SET `grpcode`='$grpcode',`custcode`='$custcode',
                                           `name`='$name',`type`='$type',`init`='$init',`bankinit`='$bankinit',
                                           `add1`='$add1',`add2`='$add2',`add3`='$add3',`city`='$city',
                                           `pin`='$pin',`country`='$country',`tel`='$tel',`fax`='$fax',
                                           `mobile`='$mobile',`email`='$email',`openbal`='$topen',`dc`='$tdc',
                                           `website`='$website',`contpers`='$contpers',`vattin`='$vattin',`csttin`='$csttin',
                                           `pattern`='$pattern',`closbal`='$closbal',`closdc`='$closdc',`station`='$station',
                                           `deptcode`='$deptcode',`pan`='$pan',`tdsperc`='$tdsperc',`centcode`='$centcode',
                                           `medmcode`='$medmcode',`district`='$district',`courcode`='$courcode',`bankacno`='$bankacno',
                                           `stno`='$stno',`percent`='$percent',`grpaccode`='$grpaccode',`stcode`='$stcode' WHERE accode = '$accode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cacmst Select * from $fryear.$cacmst where accode='$accode'") or die(mysqli_error($conn));

            mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal='$topen', `dc`='$tdc',closbal='$closbal',closdc='$closbdc' WHERE accode = '$accode'") or die(mysqli_error($conn));
//            mysqli_query($conn, "INSERT INTO `$cacmst`(`grpcode`, `accode`, `custcode`, `name`, `type`, `init`, `bankinit`,
//                                               `add1`, `add2`, `add3`, `city`, `pin`, `country`, `tel`, `fax`,
//                                               `mobile`, `email`, `openbal`, `dc`, `website`, `contpers`, `vattin`, 
//                                               `csttin`, `pattern`, `closbal`, `closdc`, `station`, `deptcode`, 
//                                               `pan`, `tdsperc`, `centcode`, `medmcode`, `district`, `courcode`,
//                                               `bankacno`, `stno`, `percent`, `grpaccode`, `stcode`)
//                                        VALUES('$grpcode', '$accode', '$custcode', '$name', '$type', '$init', '$bankinit',
//                                               '$add1', '$add2', '$add3', '$city', '$pin', '$country', '$tel', '$fax',
//                                               '$mobile', '$email', '$topen', '$tdc', '$website', '$contpers', '$vattin',
//                                               '$csttin', '$pattern', '$closbal', '$closdc', '$station', '$deptcode',
//                                               '$pan', '$tdsperc', '$centcode', '$medmcode', '$district', '$courcode',
//                                               '$bankacno', '$stno', '$percent', '$grpaccode', '$stcode')") or die(mysqli_error($conn));
        }
    }


    while ($rowexpns = mysqli_fetch_assoc($expnqry)) {
        $expsrl = $rowexpns["srl"];
        $expcode = $rowexpns["code"];
        $expname = $rowexpns["name"];

        mysqli_select_db($conn, $toyear);
        $chkexpense = mysqli_query($conn, "SELECT * FROM `$cexpense` WHERE `code`='$expcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkexpense) > 0) {
            mysqli_query($conn, "UPDATE `$cexpense` SET `name`='$expname' WHERE code='$expcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cexpense Select * from $fryear.$cexpense where code='$expcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cexpense`(`code`, `name`)
//                                     VALUES('$expcode', '$expname')") or die(mysqli_error($conn));
        }
    }

    browse('courier');
    while ($rowtrns = mysqli_fetch_assoc($courierqry)) {
        $trnscode = $rowtrns["code"];
        $trnsname = $rowtrns["name"];
        $trnsadd1 = $rowtrns["add1"];
        $trnsadd2 = $rowtrns["add2"];
        $trnsadd3 = $rowtrns["add3"];
        $trnscity = $rowtrns["city"];
        $trnspin = $rowtrns["pin"];
        $trnscntry = $rowtrns["country"];
        $trnstel = $rowtrns["telno"];
        $trnsfax = $rowtrns["fax"];
        $trnsmobile = $rowtrns["mobile"];
        $trnsemail = $rowtrns["email"];


        mysqli_select_db($conn, $toyear);
        $chktrans = mysqli_query($conn, "SELECT * FROM `$ccourier` WHERE `code`='$trnscode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chktrans) > 0) {
            mysqli_query($conn, "UPDATE `$ccourier` SET `name`='$trnsname', `add1`='$trnsadd1', `add2`='$trnsadd2', `add3`='$trnsadd3',
                                           `city`='$trnscity', `pin`='$trnspin', `country`='$trnscntry',  `telno`='$trnstel', `fax`='$trnsfax', `mobile`='$trnsmobile',
                                            `email`='$trnsemail'  WHERE code = '$trnscode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$ccourier Select * from $fryear.$ccourier where code='$trnscode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$ccourier`(`code`, `name`, `add1`, `add2`, `add3`, 
//                                               `city`, `pin`, `country`, `telno`, 
//                                               `fax`, `mobile`,  `email`)
//   
//                                        VALUES('$trnscode', '$trnsname', '$trnsadd1', '$trnsadd2', '$trnsadd3', 
//                                               '$trnscity', '$trnspin', '$trnscntry',  '$trnstel', '$trnsfax', 
//                                               '$trnsmobile',  '$trnsemail' )") or die(mysqli_error($conn));
        }
    }


    browse('stdepot');
    while ($rowgdwn = mysqli_fetch_assoc($stdepotqry)) {
        $gdwncode = $rowgdwn["code"];
        $gdwnname = $rowgdwn["name"];
        $gdwnadd1 = $rowgdwn["add1"];
        $gdwnadd2 = $rowgdwn["add2"];
        $gdwnadd3 = $rowgdwn["add3"];
        $gdwncity = $rowgdwn["city"];
        $gdwnpin = $rowgdwn["pin"];
        $gdwncntry = $rowgdwn["country"];
        $gdwntel = $rowgdwn["telno"];
        $gdwnfax = $rowgdwn["fax"];
        $gdwnmobile = $rowgdwn["mobile"];
        $gdwnmobile2 = $rowgdwn["mobile2"];
        $gdwnemail = $rowgdwn["email"];


        mysqli_select_db($conn, $toyear);
        $chkgodown = mysqli_query($conn, "SELECT * FROM `$cstdepot` WHERE `code`='$gdwncode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkgodown) > 0) {
            mysqli_query($conn, "UPDATE `$cstdepot` SET `name`='$gdwnname', `add1`='$gdwnadd1', `add2`='$gdwnadd2', `add3`='$gdwnadd3',
                                           `city`='$gdwncity', `pin`='$gdwnpin', `country`='$gdwncntry',  `telno`='$gdwntel', `fax`='$gdwnfax', `mobile`='$gdwnmobile',
                                            `email`='$gdwnemail'  WHERE code = '$gdwncode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cstdepot Select * from $fryear.$cstdepot where code='$gdwncode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cstdepot`(`code`, `name`, `add1`, `add2`, `add3`, 
//                                               `city`, `pin`, `country`, `telno`, 
//                                               `fax`, `mobile`, `email`)
//   
//                                        VALUES('$gdwncode', '$gdwnname', '$gdwnadd1', '$gdwnadd2', '$gdwnadd3', 
//                                               '$gdwncity', '$gdwnpin', '$gdwncntry', '$gdwntel', '$gdwnfax', 
//                                               '$gdwnmobile',  '$gdwnemail' )") or die(mysqli_error($conn));
        }
    }


    browse("Year End Process Completed");
    header("refresh: 1;yearend.php");
}
include("includes/linkshead.php");
?>



</head>
<body onLoad="">
    <form action=''  name="yearend" method="POST">
        <div class="panel-body" style="border: 1px solid black;">
            <div class="row">
                <div class="col-lg-12" style=''>
                    <div class="row">
                        <div class="col-lg-2"></div>
                        <div class="col-lg-2 text-right">
                            <label class="control-label">From Year</label>
                        </div>
                        <div class="col-lg-2">
                            <select name="frmyear" class="form-control" >
                                <?php
                                require("includes/dbconnect.php");

                                $fyear = mysqli_query($conn, "SELECT * FROM `compyear` WHERE finyear ='$zyear' group by `finyear` order by `lastdate` DESC") OR die(mysqli_error($conn));
                                while ($rowf = mysqli_fetch_array($fyear)) {
                                    echo "<option value=\"$rowf[finyear]\">$rowf[finyear]</option>";
                                }
                                ?>
                            </select>
                        </div>

                    </div>
                    <br>
                    <div class="row">
                        <div class="col-lg-2"></div>
                        <div class="col-lg-2 text-right">
                            <label class="control-label">To Year</label>
                        </div>
                        <div class="col-lg-2">
                            <select name="toyear" class="form-control" >
                                <?php
//                            require("includes/dbconnect.php");
                                $tyear = mysqli_query($conn, "SELECT * FROM `compyear` WHERE finyear !='$zyear' and finyear >'$zyear'  group by `finyear`  ORDER BY `lastdate`") OR die(mysqli_error($conn));
                                while ($rowt = mysqli_fetch_array($tyear)) {
                                    echo "<option value=\"$rowt[finyear]\">$rowt[finyear]</option>";
                                }
                                ?>
                            </select>
                        </div>

                    </div>
                    <br>
                    <br>
                    <div class="row">
                        <div class="col-lg-4"></div>
                        <div class="col-lg-2">
                            <input type="submit" name="save" value="Proceed" class="btn btn-primary">
                            <!--<input type="button" name="exit" value="Exit" class="btn btn-danger" href="default.php">-->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>