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

$zyear1 = $_SESSION["zdbyear_" . $dbname];
$zyear = substr($zyear1, -4);
$zroot = $_SESSION["zdir"];

//$dbyear = "msp_year";
$compcd = $_SESSION["comp_" . $dbname];

browse($compcd);

$username = $_SESSION["username"];
$cacmst = "accmast";
$cstate = "state";
$carea = "area";
$cgrpmast = "grpmast";
$cgrphead = "grphead";
$csaletype = "gsttype";
$cnarration = "narration";
$ccompmast = "compmast";
$cgodown = "godown";
$cunit = "unit";
$cvehicle = "vehicle";
$ctransport = "transport";
$ccourier = "courier";
$cslab = "slab";
$ccolor = "colour";
$cprodgrp = "prodgrp";
$cproduct = "product";
$citem = "item";
$cpacking = "packing";
$cfinprod = "finprod";
$cfinrelprod = "finrelprod";
$cfinprodraw = "finprodraw";
$cfinprodpack = "finprodpack";
$ctransacn = "transacn";
$crawsacn = "rawsacn";
$cpacksacn = "packsacn";

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$zdir = substr($zroot, 0, -4);
$mtodate = date("Y-m-d");

//browse($zdir);

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

if (isset($_POST["save"])) {
    $fryear = $zdir . $compcd . '_' . $_POST["frmyear"];
    $toyear = $zdir . $compcd . '_' . $_POST["toyear"];
    /*     * **********Copying **************** */
    browse($fryear);
    browse($toyear);
//    die();
    mysqli_select_db($conn, $fryear);
    $acqry = mysqli_query($conn, "SELECT * FROM `$cacmst` order by accode") 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));
    $stateqry = mysqli_query($conn, "SELECT * FROM `$cstate` order by code") or die(mysqli_error($conn));
    $sltypeqry = mysqli_query($conn, "SELECT * FROM `$csaletype`") or die(mysqli_error($conn));
    $unitqry = mysqli_query($conn, "SELECT * FROM `$cunit`") or die(mysqli_error($conn));
    $transqry = mysqli_query($conn, "SELECT * FROM `$ctransport`") or die(mysqli_error($conn));
//    $courqry = mysqli_query($conn, "SELECT * FROM `$ccourier`") or die(mysqli_error($conn));
    $areaqry = mysqli_query($conn, "SELECT * FROM `$carea`") or die(mysqli_error($conn));
    $narrqry = mysqli_query($conn, "SELECT * FROM `$cnarration`") or die(mysqli_error($conn));
    $godownqry = mysqli_query($conn, "SELECT * FROM `$cgodown`") or die(mysqli_error($conn));
    $prodgrpqry = mysqli_query($conn, "SELECT * FROM `$cprodgrp`") or die(mysqli_error($conn));
    $prodqry = mysqli_query($conn, "SELECT * FROM `$cproduct`") or die(mysqli_error($conn));
    $stkitqry = mysqli_query($conn, "SELECT *, SUM(qty) as tqty FROM  `$ctransacn` GROUP BY godown, itemcode") or die(mysqli_error($conn));

//    print table($stkitqry);
//die();
    //    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` 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` FROM `invmain`
//                         order by vouchdt)") or die(mysqli_error($conn));

    $fyear = $_POST["frmyear"];
    $tyear = $_POST["toyear"];
    
    

    if ($compcd > 100) {
//    for outstanding receivalbe
        mysqli_select_db($conn, $fryear);

        mysqli_query($conn, "create temporary table cmain 
                SELECT `yearno`,`type`,`vouchno`, `billdate`,`billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                      `billdt`, `billamt`, `accode` FROM $fryear.`prevbill` WHERE 
                      `type` = 'R'
                      union all
                    SELECT '$fyear' AS `yearno`, 'R' AS `type`, `vouchno`, `vouchdt` as billdate,`vouchno` as billno,
                    DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdt`,  `billamt`, `party` as accode  FROM $fryear.`salemain` where `cashcred` != 'C'
                    ") or die(mysqli_error($conn));

//$subtmp1 = mysqli_query($conn, "SELECT party, name FROM cmain group by party order by name ") or die(mysqli_error($conn));

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

//    print table($subtmp1);
//    die();
        mysqli_query($conn, "Delete from `$toyear`.`prevbill` WHERE `frmprev` = 'Y'  and type = 'R'") 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"];

            $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 > 1) {
                $counter++;
                mysqli_select_db($conn, $toyear);

                mysqli_query($conn, "Insert into $toyear.prevbill (`yearno`, `accode`, `type`, `srno`, `billno`, `billdate`, `billamt`, `chalno`, `vouchno`, `cashcred`, `frmprev`)
                                               VALUES ('$qyear', '$qparty', 'R', '$qsrno', '$qbill', '$mbilldate', '$balamt', '', '$qvouch', '', 'Y') ") OR die(mysqli_error($conn));
            }
        }
//    for outstanding payable
//    mysqli_select_db($conn, $fryear);
        mysqli_query($conn, "create temporary table cmain2
                SELECT `yearno`,`type`,`vouchno`, `billdate`,`billno`, DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                      `billdt`, `billamt`, `accode` FROM $fryear.`prevbill` WHERE 
                      `type` = 'P'
                      union all
                    SELECT '$fyear' AS `yearno`, 'P' AS `type`, `vouchno`, `vouchdt` as billdate,`invno` as billno,
                    DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdt`,  `billamt`, `party` as accode  FROM $fryear.`purmain` where `cashcred` != 'C'
                    ") or die(mysqli_error($conn));

//$subtmp1 = mysqli_query($conn, "SELECT party, name FROM cmain group by party order by name ") or die(mysqli_error($conn));

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

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

        while ($psubtmp = mysqli_fetch_assoc($subtmp2)) {
            $qparty2 = $psubtmp["accode"];
            $qyear2 = $psubtmp["yearno"];
            $qbill2 = $psubtmp["billno"];
            $qosclear2 = $psubtmp["osclear"];
            $qpacklistno2 = $psubtmp["packlistno"];
            $qtotcartons2 = $psubtmp["totcartons"];
            $mbilldate2 = $psubtmp["billdate"];
            $qvouch2 = $psubtmp["vouchno"];
            $qamt2 = $psubtmp["billamt"];
            $qtype2 = $psubtmp["type"];
            $qsrno2 = $psubtmp["srno"];

            $totadj2 = 0;
            $totset2 = 0;
            mysqli_select_db($conn, $fryear);

//        $trn2 = mysqli_query($conn, "SELECT * FROM $fryear.`billsett` WHERE `party` = '$qparty2' AND `type` = '$qtype2' and 
//                                   `yearno` = '$qyear2' AND `vouchno` = '$qvouch2'") or die(mysqli_error($conn));
////                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = '$mtype' ") or die(mysqli_error($conn));
//
//        $tranqry2 = mysqli_fetch_assoc($trn2);
//        if (mysqli_num_rows($trn2) > 0) {
//            $totset2 = $tranqry2["setoff"];
//        }

            $trn22 = mysqli_query($conn, " SELECT SUM(`setoff`) AS `totadj`, nodays FROM $fryear.`billsett` WHERE 
                                     `yearno` = '$qyear2' AND `vouchno` = '$qvouch2' AND `type` = '$qtype2' and 
                                     `party` = '$qparty2' LIMIT 0,1 ") or die(mysqli_error($conn));

            $tranqry22 = mysqli_fetch_assoc($trn22);
            $totadj2 = $tranqry22["totadj"];
//        $amtadj2 = ($totadj2 - $totset2);
//        $setoff2 = $totset2;
            $balamt2 = $qamt2 - $totadj2;

            $nodays2 = $mbilldate2 - $mvouchdt;
//                print $nodays;


            if ($balamt2 > 1) {
                $counter++;
                mysqli_select_db($conn, $toyear);

                mysqli_query($conn, "Insert into $toyear.prevbill (`yearno`, `accode`, `type`, `srno`, `billno`, `billdate`, `billamt`, `chalno`, `vouchno`, `cashcred`, `frmprev`)
                                               VALUES ('$qyear2', '$qparty2', 'P', '$qsrno2', '$qbill2', '$mbilldate2', '$balamt2', '', '$qvouch2', '', 'Y') ") 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 `$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));
        }
    }



////    /*     * **********accmast **************** */
//
    $profqry = mysqli_query($conn, "SELECT amount FROM $fryear.`profit`") or die(mysqli_error($conn));
    $fetprof = mysqli_fetch_array($profqry);
    $mprof = $fetprof["amount"];
    browse('accmast');
    while ($row = mysqli_fetch_array($acqry)) {
        $accode = $row["accode"];
//        if($accode > 50){
//            browse($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 = $row["custcode"];
        $deptcode = $row["deptcode"];
        $nameprin = $row["nameprin"];
        $type = $row["type"];
        $init = $row["init"];
        $bankinit = $row["bankinit"];
        $tinno = $row["tinno"];
        $city = $row["city"];
        $pin = $row["pin"];
        $country = $row["country"];
        $station = $row["station"];
        $tel = $row["tel"];
        $fax = $row["fax"];
        $mobile = $row["mobile"];
        $mobile2 = $row["mobile2"];
        $email = $row["email"];
        $openbal = $row["openbal"];
        $dc = $row["dc"];
        $owner = $row["owner"];
        $creditlimit = $row["credlimit"];
        $website = $row["website"];
        $contpers = $row["contpers"];
        $vattin = $row["vattin"];
        $csttin = $row["csttin"];
        $pattern = $row["pattern"];
        $closbal = $row["closbal"];
        $closdc = $row["closdc"];
        $currbal = $row["currbal"];
        $currdc = $row["currdc"];
        $pfund = $row["pfund"];
        $saltype = $row["saltype"];
        $salwise = $row["salwise"];
        $salmon = $row["salmon"];
        $travmon = $row["travmon"];
        $convmon = $row["convmon"];
        $p_tax = $row["p_tax"];
        $leaveflag = $row["leaveflag"];
        $leavecode = $row["leavecode"];
        $esic = $row["esic"];
        $salary = $row["salary"];
        $active = $row["active"];
        $username = $row["username"];
        $pan = $row["pan"];
        $staff = $row["staff"];
        $transporter = $row["transporter"];
        $state = $row["state"];
        $statecode = $row["statecode"];
        $gstno = $row["gstno"];
        $increcode = $row["increcode"];
        $credlimit = $row["credlimit"];
        $percent = $row["percent"];
//        $transport = $row["transport"];
        $lbt = $row["lbt"];
//        $discount = $row["discount"];
//        $commperc = $row["commperc"];
//        $ledgfrom = $row["ledgfrom"];
//        $ledgto = $row["ledgto"];
        $disc1 = $row["disc1"];
        $disc2 = $row["disc2"];
        $disc3 = $row["disc3"];
        $disc4 = $row["disc4"];
        $gdisc1 = $row["gdisc1"];
        $gdisc2 = $row["gdisc2"];
        $gdisc3 = $row["gdisc3"];
        $gdisc4 = $row["gdisc4"];
        $oms = $row["oms"];

        if ($dc == "D") {
            $nopen = abs($openbal) * -1;
        } else {
            $nopen = abs($openbal);
        }

        if ($currdc == "D") {
            $currbal = abs($currbal) * -1;
        } else {
            $currbal = abs($currbal);
        }

        mysqli_select_db($conn, $fryear);
        $actranqry = mysqli_query($conn, "SELECT SUM(amount) AS amt FROM `actran` WHERE accode='$accode' AND `chqret` !='Y'") or die(mysqli_error($conn));
        $get = mysqli_fetch_array($actranqry);
        $balamt = $get["amt"];

        if ($grpcode != '050') {
            if ($accode == 23) {
                $topen = $nopen + $balamt + $currbal;
            } else {
                $topen = $nopen + $balamt;
            }
        } else {
            $topen = $nopen + $balamt + $currbal;
        }
        if ($topen > 0) {
            $tdc = "C";
        } else {
            $tdc = "D";
        }
        $topen = abs($topen);

        $currbal = 0;
        $currdc = "D";

        $closbal = 0;
        $closdc = "D";

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

        if ($mgtype != "I" && $mgtype != "E") {
            
        } else {
            $topen = 0;
            $tdc = "D";
        }

        mysqli_select_db($conn, $toyear);
        $chkac = mysqli_query($conn, "SELECT * FROM `$cacmst` WHERE accode = '$accode'") or die(mysqli_error($conn));

        if (mysqli_num_rows($chkac) > 0) {
            if ($accode != '23' && $accode != '24') {
                mysqli_query($conn, "UPDATE `$cacmst` SET `name`='$name', `type`='$type',  grpcode='$grpcode',  `oms`='$oms', 
                                             openbal = '$topen', `dc`='$tdc', currbal='$currbal', currdc='$currdc', closbal='$closbal', closdc='$closdc'
                                             WHERE accode = '$accode'") or die(mysqli_error($conn));
            } else {
                if ($accode == '23') {
                    mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal= 0, `dc`='D', currbal='0', currdc='D', closbal='0',closdc='D' WHERE accode = '$accode'") or die(mysqli_error($conn));
//                    mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal= '$topen', `dc`='D'  WHERE accode = '24' ") 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));

            if ($accode != '23' && $accode != '24') {
                mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal='$topen', `dc`='$tdc', currbal='$currbal', currdc='$currdc', closbal='$closbal',closdc='$closdc' WHERE accode = '$accode'") or die(mysqli_error($conn));
            } else {
                if ($accode == '23') {
//                    mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal= 0, `dc`='D', currbal='0', currdc='D', closbal='0',closdc='D' WHERE accode = '$accode'") or die(mysqli_error($conn));
//                    mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal='$topen', `dc`= 'D'  WHERE accode = '24' ") or die(mysqli_error($conn));
                }
            }
        }
    }

//    to carry forward stock   //////////////////////////////////
    
    mysqli_select_db($conn, $fryear);

    mysqli_query($conn, "drop temporary table if exists stkdbf ") or die(mysqli_error($conn));
    mysqli_query($conn, "create temporary table stkdbf
                            SELECT sum(a.qty) as sqty, a.godown, a.itemcode, b.name, b.cost, sum(a.qty * b.cost) as closstk FROM transacn a, product b 
                                   WHERE a.vouchdt <= '$mtodate' and a.itemcode = b.code group by a.godown, a.itemcode") or die(mysqli_error($conn));

    $getstk = mysqli_query($conn, "SELECT sum(closstk) as clstk FROM `stkdbf` ") or die(mysqli_error($conn));
    $fetstk = mysqli_fetch_object($getstk);
    $stkval = $fetstk->clstk;

    mysqli_query($conn, "update $toyear.accmast set openbal='$stkval', dc = 'D' WHERE accode = '24' ") or die(mysqli_error($conn));

    
//    
    browse('state');
    while ($rowstate = mysqli_fetch_assoc($stateqry)) {
        $stcode = $rowstate["code"];
        $ststcode = $rowstate["stcode"];
        $stname = $rowstate["name"];

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

//            mysqli_query($conn,  "INSERT INTO `$cunit`(`code`, `name`)
//                                     VALUES('$unitcode', '$unitname')") or die(mysqli_error($conn));
        }
    }
    browse('gsttype');

    while ($rowsltype = mysqli_fetch_assoc($sltypeqry)) {
        $slsrl = $rowsltype["srl"];
        $slcode = $rowsltype["code"];
        $slname = $rowsltype["name"];
        $slper = $rowsltype["per"];
        $slshortname = $rowsltype["shortname"];
        $sldiscperc = $rowsltype["discperc"];

        mysqli_select_db($conn, $toyear);
        $chksltype = mysqli_query($conn, "SELECT * FROM `$csaletype` WHERE `code`='$slcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chksltype) > 0) {
            mysqli_query($conn, "UPDATE `$csaletype` SET `name`='$slname', `per`='$slper', `shortname`='$slshortname', `discperc`='$sldiscperc' WHERE code='$slcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$csaletype Select * from $fryear.$csaletype where code='$slcode'") or die(mysqli_error($conn));

//            mysqli_query($conn,  "INSERT INTO `$csaletype`(`code`, `name`, `per`, `shortname`, `discperc`)
//                                     VALUES('$slcode', '$slname', '$slper', '$slshortname', '$sldiscperc')") or die(mysqli_error($conn));
        }
    }
    browse('unit');

    while ($rowunit = mysqli_fetch_assoc($unitqry)) {
        $unitcode = $rowunit["code"];
        $unitname = $rowunit["name"];

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

//            mysqli_query($conn, "INSERT INTO `$cunit`(`code`, `name`)
//                                     VALUES('$unitcode', '$unitname')") or die(mysqli_error($conn));
        }
    }
    browse('godown');

    while ($rowgdwn = mysqli_fetch_assoc($godownqry)) {
        $gdwncode = $rowgdwn["code"];
        $gdwngcode = $rowgdwn["gcode"];
        $gdwnname = $rowgdwn["name"];
        $gdwnadd1 = $rowgdwn["add1"];
        $gdwnadd2 = $rowgdwn["add2"];
        $gdwnadd3 = $rowgdwn["add3"];
        $gdwncity = $rowgdwn["city"];
        $gdwnpin = $rowgdwn["pin"];
        $gdwncpers = $rowgdwn["cpers"];
        $gdwntel = $rowgdwn["tel"];
        $gdwnfax = $rowgdwn["fax"];
        $gdwnmob = $rowgdwn["mob"];

        mysqli_select_db($conn, $toyear);
        $chkgodown = mysqli_query($conn, "SELECT * FROM `$cgodown` WHERE `code`='$gdwncode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkgodown) > 0) {
            mysqli_query($conn, "UPDATE `$cgodown` SET `gcode`='$gdwngcode',`name`='$gdwnname', `add1`='$gdwnadd1', `add2`='$gdwnadd2', `add3`='$gdwnadd3',
                                               `city`='$gdwncity', `pin`='$gdwnpin', `cpers`='$gdwncpers',  `tel`='$gdwntel', `mob`='$gdwnmob'
                                                WHERE code = '$gdwncode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cgodown Select * from $fryear.$cgodown where code='$gdwncode'") or die(mysqli_error($conn));

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


    browse('transport');
    while ($rowtrns = mysqli_fetch_assoc($transqry)) {
        $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["tel"];
        $trnsfax = $rowtrns["fax"];
        $trnsmobile = $rowtrns["mobile"];
        $trnsmobile2 = $rowtrns["mobile2"];
        $trnscontpers = $rowtrns["contpers"];
        $trnscsttin = $rowtrns["csttin"];
        $trnsvattin = $rowtrns["vatttin"];
        $trnsemail = $rowtrns["email"];

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

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

    browse('area');

    while ($rowarea = mysqli_fetch_assoc($areaqry)) {
        $areacode = $rowarea["code"];
        $areaname = $rowarea["area"];

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

//            mysqli_query($conn, "INSERT INTO `$carea`(`code`, `name`)
//                                     VALUES('$brndcode', '$brndname')") or die(mysqli_error($conn));
        }
    }
    browse('narr');

    while ($rownarr = mysqli_fetch_assoc($narrqry)) {
        $narrcode = $rownarr["code"];
        $narrname1 = $rownarr["narr1"];
        $narrname2 = $rownarr["narr2"];

        mysqli_select_db($conn, $toyear);
        $chknarr = mysqli_query($conn, "SELECT * FROM `$cnarr` WHERE `code`='$narrcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chknarr) > 0) {
            mysqli_query($conn, "UPDATE `$cnarr` SET `narr1`='$narrname1', `narr2`='$narrname2' WHERE code='$narrcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cnarr Select * from $fryear.$cnarr where code='$narrcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cnarr`(`code`, `name`)
//                                     VALUES('$brndcode', '$brndname')") or die(mysqli_error($conn));
        }
    }
//    browse('colr');
//
//    while ($rowcolr = mysqli_fetch_assoc($colrqry)) {
//        $colrcode = $rowcolr["code"];
//        $colrcolrcode = $rowcolr["colrcode"];
//        $colrname = $rowcolr["name"];
//
//        mysqli_select_db($conn, $toyear);
//        $chkcolr = mysqli_query($conn, "SELECT * FROM `$ccolor` WHERE `code`='$colrcode'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkcolr) > 0) {
//            mysqli_query($conn, "UPDATE `$ccolor` SET `name`='$colrname' WHERE code='$colrcode'") or die(mysqli_error($conn));
//        } else {
//            mysqli_query($conn, "Insert into $toyear.$ccolor Select * from $fryear.$ccolor where code='$colrcode'") or die(mysqli_error($conn));
//
////            mysqli_query($conn, "INSERT INTO `$ccolr`(`code`, `name`)
////                                     VALUES('$brndcode', '$brndname')") or die(mysqli_error($conn));
//        }
//    }

    browse('prodgrp');
    while ($rowprodgrp = mysqli_fetch_assoc($prodgrpqry)) {
        $prodgrpsrl = $rowprodgrp["srl"];
        $prodgrpcode = $rowprodgrp["code"];
        $prodgrpname = $rowprodgrp["name"];
        $prodgrphsncode = $rowprodgrp["hsncode"];
        $prodgrpgstperc = $rowprodgrp["gstperc"];

        mysqli_select_db($conn, $toyear);
        $chkprodgrp = mysqli_query($conn, "SELECT * FROM `$cprodgrp` WHERE `code`='$prodgrpcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkprodgrp) > 0) {
            mysqli_query($conn, "UPDATE `$cprodgrp` SET `name`='$prodgrpname',hsncode='$prodgrphsncode',gstperc='$prodgrpgstperc' WHERE code='$prodgrpcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cprodgrp Select * from $fryear.$cprodgrp where code='$prodgrpcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cprodgrp`(`code`, `name`)
//                                     VALUES('$prodgrpcode', '$prodgrpname')") or die(mysqli_error($conn));
        }
    }

    browse('product');
    while ($rowproduct = mysqli_fetch_assoc($prodqry)) {
        $pdcode = $rowproduct["code"];
        $prodcode = $rowproduct["prodcode"];
        $name = $rowproduct["name"];

        $gram = $rowproduct["gram"];
        $packing = $rowproduct["packing"];
        $colour = $rowproduct["colour"];
        $barcode = $rowproduct["barcode"];

        $unit = $rowproduct["unit"];
        $prodgrp = $rowproduct["prodgrp"];
        $hsncode = $rowproduct["hsncode"];
        $gstperc = $rowproduct["gstperc"];

        $cost = $rowproduct["cost"];
        $sellprice = $rowproduct["rate"];
        $mrp = $rowproduct["mrp"];
        $msp = $rowproduct["msp"];
        $gel = $rowproduct["gel"];
        $type = $rowproduct["type"];
        $approxkg = $rowproduct["approxkg"];
        $phytest = $rowproduct["phytest"];
        $nametoprint = $rowproduct["nametoprint"];

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

//            mysqli_query($conn, "INSERT INTO `$citem`(`code`, `name`, `itemgrp`, `unit`, `cost`, `msp`, `mrp`, `rate`, 
//                                              `prodtype`, `itemsubgrp`, 
//                                              `hsncode`, `gstperc`, `nameprint`)
//                                    VALUES ('$prodctcode','$prodctname','$prodctitemgrp','$prodctunit','$prodctcost','$prodctmsp', '$prodctmrp','$prodctrate',
//                                            '$prodctprodtype','$prodctitemsubgrp','$prodcthsncode', '$prodctgstperc', '$prodctnameprint')") or die(mysqli_error($conn));
        }
    }


    $cnt = 0;
    $source = 'OP';
    $group = $source . '1';
    $yearno = $_POST["toyear"];
    $vouchdt = '0000-00-00';

    mysqli_select_db($conn, $toyear);
    mysqli_query($conn, "Delete from $toyear.$ctransacn where source ='$source' ") or die(mysqli_error($conn));

    mysqli_select_db($conn, $fryear);
    while ($rowst = mysqli_fetch_assoc($stkitqry)) {
        $cnt++;
//        $source = $rowst["source"];
        $groupt = $rowst["group"];
        $vouchno = '';
        $srno = $cnt;
//        $brandd = $rowst["brand"];
        $itmtcode = $rowst["itemcode"];
        $barcode = $rowst["barcode"];
        $godown = $rowst["godown"];
        $itemgrp = $rowst["itemgrp"];
        $tqty = $rowst["tqty"];
        $mrp = $rowst["mrp"];
        $rate = $rowst["rate"];
        $amount = $rowst["amount"];
        $netamt = $rowst["netamt"];
        $accode = '';
        $ordno = '1';
        $cost = $rowst["cost"];
        $unit = $rowst["unit"];
        $color = $rowst["color"];
        $gram = $rowst["gram"];
        $series = $rowst["series"];
        $inwtype = $rowst["inwtype"];
        $monno = '04';

        mysqli_select_db($conn, $toyear);
        mysqli_query($conn, "INSERT INTO $toyear.`$ctransacn`(`yearno`, `srno`, `source`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,
                                                 `godown`, `color`, `itemgrp`, `qty`, `mrp`, `rate`, `amount`, `netamt`, `accode`, `ordno`, `cost`, `unit`,
                                                 `group`, `batch`, `gram`, `monno`, `series`, `inwtype`)
                                         VALUES ('$yearno', '$srno', '$source', '0', '$vouchdt', '$barcode', '$itmtcode',
                                                 '$godown', '$color', '$itemgrp', '$tqty', '$mrp', '$rate', '$amount', '$netamt', '$accode', '$ordno', '$cost', '$unit',
                                                 '$group', '', '$gram', '$monno', '$series', '$inwtype')") or die(mysqli_error($conn));
    }

    echo "Success";
//    browse("Processing Complete");
//    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' 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>