<?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"];
$zdir = substr($zroot, 0, -4);

//$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];
//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 . $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));
    $colrqry = mysqli_query($conn, "SELECT * FROM `$ccolor`") 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));
    $itemqry = mysqli_query($conn, "SELECT * FROM `$citem`") or die(mysqli_error($conn));
    $packingqry = mysqli_query($conn, "SELECT * FROM `$cpacking`") or die(mysqli_error($conn));
    $finprodqry = mysqli_query($conn, "SELECT * FROM `$cfinprod`") or die(mysqli_error($conn));
    $finrelprodqry = mysqli_query($conn, "SELECT * FROM `$cfinrelprod`") or die(mysqli_error($conn));
    $finprodrawqry = mysqli_query($conn, "SELECT * FROM `$cfinprodraw`") or die(mysqli_error($conn));
    $finprodpackqry = mysqli_query($conn, "SELECT * FROM `$cfinprodpack`") or die(mysqli_error($conn));
//    $slabqry = mysqli_query($conn,  "SELECT * FROM `$cslab`") or die(mysqli_error($conn));
//    $compmastqry = mysqli_query($conn,  "SELECT * FROM `$ccompmast`") or die(mysqli_error($conn));
//    $godownqry = mysqli_query($conn, "SELECT * FROM `$cgodown`") or die(mysqli_error($conn));
//    $stkitqry = mysqli_query($conn, "SELECT *,sum(qty) as tqty  FROM `$ctransacn` group by itemcode,godown,series") or die(mysqli_error($conn));
//    $stkrawqry = mysqli_query($conn, "SELECT *,sum(qty) as tqty  FROM `$crawsacn` group by itemcode,godown") or die(mysqli_error($conn));
//    $stkpackqry = mysqli_query($conn, "SELECT *,sum(qty) as tqty  FROM `$cpacksacn` group by itemcode,godown") or die(mysqli_error($conn));
//    $vehicleqry = mysqli_query($conn,  "SELECT * FROM `$cvehicle`") or die(mysqli_error($conn));
//    print table($stkrawqry);
//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"];


//    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 `paidby` != 'C'
                    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.`deexmain` where `paidby` != 'C'
                    UNION all
                    SELECT '$fyear' AS `yearno`, 'R' AS `type`, `vouchno`, `invdate` as billdate,`vouchno` as billno,
                    DATE_FORMAT(`invdate`, '%d-%m-%Y') AS `billdt`,  `billamt`, `partyname` as accode   FROM $fryear.`salemainexp`
                    ") 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.`rawmain` where `paidby` != 'C'
                    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.`puotmain` where `paidby` != '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"];
        $closbdc = $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);
        }


        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"];



//        browse($balamt);

        $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 sum(amount) as tamt FROM $fryear.actran WHERE accode='$accode' and retdate = '0000-00-00' ") 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;
            $topen = 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";
            }
        }

        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) {
            mysqli_query($conn, "UPDATE `$cacmst` SET `name`='$name', `add1`='$add1', `add2`='$add2', `add3`='$add3', `state`='$state', `type`='$type', `tel`='$tel',
                        `statecode`='$statecode', `city`='$city', `pin`='$pin', mobile= '$mob', `email`='$email',  `contpers`='$cpers1', openbal='$topen', 
                        `dc`='$tdc', grpcode='$grpcode', `gstno`='$gstno', `pan`='$pan', `oms`='$oms', `credlimit`='$credlimit',
                         currbal='$currbal',currdc='$currdc',closbal='$closbal',closdc='$closbdc', `disc1`='$disc1', `disc2`='$disc2', `disc3`='$disc3', `disc4`='$disc4',
                         `gdisc1`='$gdisc1', `gdisc2`='$gdisc2', `gdisc3`='$gdisc3', `gdisc4`='$gdisc4',`oms`='$oms' 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',currbal='$currbal',currdc='$currdc',closbal='$closbal',closdc='$closbdc' WHERE accode = '$accode'") or die(mysqli_error($conn));

//            mysqli_query($conn,  "INSERT INTO `$cacmst`(`grpcode`, `accode`, `custcode`, `name`, `nameprin`, `type`,
//                                                      `init`, `bankinit`, `add1`, `add2`, `add3`, `tinno`,
//                                                      `city`, `pin`, `state`, `statecode`, `country`, `tel`,
//                                                      `fax`, `mobile`, `email`, `openbal`, `dc`, `owner`,
//                                                      `credlimit`, `website`, `contpers`, `vattin`, `csttin`,
//                                                      `pattern`, `closbal`, `closdc`, `station`, `salary`,
//                                                      `mobile2`, `deptcode`, `pan`, `p_tax`, `pfund`,
//                                                      `salmon`, `esic`, `active`, `tdsperc`, `travmon`,
//                                                      `convmon`, `leavecode`, `leaveflag`, `salwise`, 
//                                                      `saltype`, `custtype`, `lbt`, `transporter`, `currbal`, 
//                                                      `currdc`, `gstno`, `vehno`, `creditac`, `area`, `oms`, `percent`)
//   
//                                        VALUES('$grpcode', '$accode', '$custcode', '$name', '$nameprin', '$type',
//                                        '$init', '$bankinit', '$add1', '$add2', '$add3', '$tinno', 
//                                        '$city', '$pin', '$state', '$statecode', '$country', '$tel',
//                                        '$fax', '$mobile', '$email', '$topen', '$tdc', '$owner', 
//                                        '$credlimit', '$website', '$contpers', '$vattin', '$csttin',
//                                        '$pattern', '$closbal', '$closdc', '$station', '$salary', 
//                                        '$mobile2', '$deptcode', '$pan', '$p_tax', '$pfund',
//                                        '$salmon', '$esic', '$active', '$tdsperc', '$travmon', 
//                                        '$convmon', '$leavecode', '$leaveflag', '$salwise',
//                                        '$saltype', '$custtype', '$lbt', '$transporter', '$currbal', 
//                                        '$currdc', '$gstno', '$vehno', '$creditac', '$area', '$oms', '$percent')") 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('courier');

    while ($rowcour = mysqli_fetch_assoc($courqry)) {
        $courcode = $rowcour["code"];
        $courname = $rowcour["name"];
        $couradd1 = $rowcour["add1"];
        $couradd2 = $rowcour["add2"];
        $couradd3 = $rowcour["add3"];
        $courcity = $rowcour["city"];
        $courpin = $rowcour["pin"];
        $courcntry = $rowcour["country"];
        $courtel = $rowcour["tel"];
        $courfax = $rowcour["fax"];
        $courmobile = $rowcour["mob"];
        $courmobile2 = $rowcour["mobile2"];
        $courcontpers = $rowcour["contpers"];
        $courcsttin = $rowcour["csttin"];
        $courvattin = $rowcour["vatttin"];
        $couremail = $rowcour["email"];


        mysqli_select_db($conn, $toyear);
        $chktrans = mysqli_query($conn, "SELECT * FROM `$ccourier` WHERE `code`='$courcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chktrans) > 0) {
            mysqli_query($conn, "UPDATE `$ccourier` SET `name`='$courname', `add1`='$couradd1', `add2`='$couradd2', `add3`='$couradd3',mob='$courmobile',
                                                `city`='$courcity', `pin`='$courpin',   `tel`='$courtel', `fax`='$courfax',`email`='$couremail'
                                                 WHERE code = '$courcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$ccourier Select * from $fryear.$ccourier where code='$courcode'") 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('$courcode', '$courname', '$couradd1', '$couradd2', '$couradd3', 
//                                               '$courcity', '$courpin', '$courcntry',  '$courtel', '$courfax', 
//                                               '$courmobile', '$courmobile2', '$couremail' )") 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 `prodcode`='$prodcode', `name`='$name', `gram`='$gram', `packing`='$packing', `colour`='$colour',
                                               `barcode`='$barcode', `unit`='$unit', `prodgrp`='$prodgrp', `hsncode`='$hsncode', `gstperc`='$gstperc',
                                               `cost`='$cost', `rate`='$sellprice', `nametoprint`='$nametoprint',`mrp`='$mrp', `msp`='$msp',
                                               `type`='$type', `gel`='$gel' 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));
        }
    }

    browse('item');
    while ($rowitem = mysqli_fetch_assoc($itemqry)) {
        $itcode = $rowitem["code"];
        $name = $rowitem["name"];
        $btclname = $rowitem["btclname"];
        $unit = $rowitem["unit"];
        $itemgrp = $rowitem["itemgrp"];
        $cost = $rowitem["cost"];
        $rate = $rowitem["rate"];
        $count = $rowitem["items"];
        $hsncode = $rowitem["hsncode"];
        $gstperc = $rowitem["gstperc"];



        mysqli_select_db($conn, $toyear);
        $chkitem = mysqli_query($conn, "SELECT * FROM `$citem` WHERE `code`='$itcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkitem) > 0) {
            mysqli_query($conn, "UPDATE `$citem` SET `name`='$name', `btclname`='$btclname',`unit`='$unit', `itemgrp`='$itemgrp', 
                                 `cost`='$cost', `rate`='$rate',`hsncode`='$hsncode', `gstperc`='$gstperc' WHERE `code`='$itcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$citem Select * from $fryear.$citem where code='$itcode'") 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));
        }
    }
    browse('packing');
    while ($rowpacking = mysqli_fetch_assoc($packingqry)) {
        $pkcode = $rowpacking["code"];
        $name = $rowpacking["name"];
        $unit = $rowpacking["unit"];
        $packinggrp = $rowpacking["packgrp"];
        $cost = $rowpacking["cost"];
        $rate = $rowpacking["rate"];
        $hsncode = $rowpacking["hsncode"];
        $gstperc = $rowpacking["gstperc"];

        mysqli_select_db($conn, $toyear);
        $chkpacking = mysqli_query($conn, "SELECT * FROM `$cpacking` WHERE `code`='$pkcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkpacking) > 0) {
            mysqli_query($conn, "UPDATE `$cpacking` SET `name`='$name', `unit`='$unit', `packgrp`='$packinggrp', 
                                 `cost`='$cost', `rate`='$rate',`hsncode`='$hsncode', `gstperc`='$gstperc' WHERE `code`='$pkcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cpacking Select * from $fryear.$cpacking where code='$pkcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cpacking`(`code`, `name`, `packinggrp`, `unit`, `cost`, `msp`, `mrp`, `rate`, 
//                                              `prodtype`, `packingsubgrp`, 
//                                              `hsncode`, `gstperc`, `nameprint`)
//                                    VALUES ('$prodctcode','$prodctname','$prodctpackinggrp','$prodctunit','$prodctcost','$prodctmsp', '$prodctmrp','$prodctrate',
//                                            '$prodctprodtype','$prodctpackingsubgrp','$prodcthsncode', '$prodctgstperc', '$prodctnameprint')") or die(mysqli_error($conn));
        }
    }
    browse('finprod');
    while ($rowfinprod = mysqli_fetch_assoc($finprodqry)) {
        $fpdcode = $rowfinprod["code"];
        $name = $rowfinprod["name"];
        $rprodcode = $rowfinprod["rprodcode"];
        $gram = $rowfinprod["gram"];
        $approxkg = $rowfinprod["approxkg"];
        $phytest = $rowfinprod["phytest"];
        $totpercqty = $rowfinprod["totpercqty"];
        mysqli_select_db($conn, $toyear);
        $chkfinprod = mysqli_query($conn, "SELECT * FROM `$cfinprod` WHERE `code`='$fpdcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkfinprod) > 0) {
            mysqli_query($conn, "UPDATE `$cfinprod` SET `finprodcode`='$fpdcode', `name`='$name', `gram`='$gram', `rprodcode`='$rprodcode', `approxkg`='$approxkg',
                       `phytest`='$phytest',`totpercqty`='$totpercqty' WHERE `code`='$fpdcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cfinprod Select * from $fryear.$cfinprod where code='$fpdcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$cfinprod`(`code`, `name`, `finprodgrp`, `unit`, `cost`, `msp`, `mrp`, `rate`, 
//                                              `prodtype`, `finprodsubgrp`, 
//                                              `hsncode`, `gstperc`, `nameprint`)
//                                    VALUES ('$prodctcode','$prodctname','$prodctfinprodgrp','$prodctunit','$prodctcost','$prodctmsp', '$prodctmrp','$prodctrate',
//                                            '$prodctprodtype','$prodctfinprodsubgrp','$prodcthsncode', '$prodctgstperc', '$prodctnameprint')") or die(mysqli_error($conn));
        }
    }
    browse('finrelprod');
    while ($rowfinrelprod = mysqli_fetch_assoc($finrelprodqry)) {
        $rlsrno = $rowfinrelprod["srno"];
        $pdrcode = $rowfinrelprod["code"];
        $prodcode = $rowfinrelprod["prodcode"];
        $prodname = $rowfinrelprod["prodname"];
        $netwt = $rowfinrelprod["netwt"];
        $tapproxkg = $rowfinrelprod["approxkg"];
        $approxltr = $rowfinrelprod["approxltr"];

        mysqli_select_db($conn, $toyear);
//        $chkfinrelprod = mysqli_query($conn, "SELECT * FROM $toyear.$cfinrelprod WHERE `code`='$prodcode' and srno") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkfinrelprod) > 0) {
//            mysqli_query($conn, "UPDATE `$cfinrelprod` SET `srno`='$rlsrno',`code`='$prodcode',`prodcode`='$prodcode',
//                                                   `prodname`='',`netwt`=[value-6],`approxkg`=[value-7],`approxltr`=[value-8]
//                                                    WHERE `code`='$fpdcode'") or die(mysqli_error($conn));
//        } else {
        mysqli_query($conn, "Delete from $toyear.$cfinrelprod where code='$pdrcode' and srno ='$rlsrno'") or die(mysqli_error($conn));

        mysqli_query($conn, "Insert into $toyear.$cfinrelprod Select * from $fryear.$cfinrelprod where code='$pdrcode' and srno ='$rlsrno'") or die(mysqli_error($conn));
//        }
    }
    browse('finprodraw');
    while ($rowfinprodraw = mysqli_fetch_assoc($finprodrawqry)) {
        $pdrrcode = $rowfinprodraw["code"];
        $rsrno = $rowfinprodraw["srno"];
        $rawmat = $rowfinprodraw["rawmat"];
        $runit = $rowfinprodraw["unit"];
        $rqty = $rowfinprodraw["qty"];
        $rcost = $rowfinprodraw["cost"];
        $ramount = $rowfinprodraw["amount"];

        mysqli_select_db($conn, $toyear);
//        $chkfinprodraw = mysqli_query($conn, "SELECT * FROM $toyear.$cfinprodraw WHERE `code`='$prodcode' and srno") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkfinprodraw) > 0) {
//            mysqli_query($conn, "UPDATE `$cfinprodraw` SET `srno`='$rlsrno',`code`='$prodcode',`prodcode`='$prodcode',
//                                                   `prodname`='',`netwt`=[value-6],`approxkg`=[value-7],`approxltr`=[value-8]
//                                                    WHERE `code`='$fpdcode'") or die(mysqli_error($conn));
//        } else {
        mysqli_query($conn, "Delete from $toyear.$cfinprodraw where code='$pdrrcode' and srno ='$rsrno'") or die(mysqli_error($conn));

        mysqli_query($conn, "Insert into $toyear.$cfinprodraw Select * from $fryear.$cfinprodraw where code='$pdrrcode' and srno ='$rsrno'") or die(mysqli_error($conn));
//        }
    }
    browse('finprodpack');
    while ($rowfinprodpack = mysqli_fetch_assoc($finprodpackqry)) {
        $pdrpkcode = $rowfinprodpack["code"];
        $psrno = $rowfinprodpack["srno"];
        $packing = $rowfinprodpack["packing"];
        $punit = $rowfinprodpack["unit"];
        $pqty = $rowfinprodpack["qty"];
        $pcost = $rowfinprodpack["cost"];
        $pamount = $rowfinprodpack["amount"];
        $prelprod = $rowfinprodpack["relprod"];

        mysqli_select_db($conn, $toyear);
//        $chkfinprodpack = mysqli_query($conn, "SELECT * FROM $toyear.$cfinprodpack WHERE `code`='$prodcode' and srno") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkfinprodpack) > 0) {
//            mysqli_query($conn, "UPDATE `$cfinprodpack` SET `srno`='$rlsrno',`code`='$prodcode',`prodcode`='$prodcode',
//                                                   `prodname`='',`netwt`=[value-6],`approxkg`=[value-7],`approxltr`=[value-8]
//                                                    WHERE `code`='$fpdcode'") or die(mysqli_error($conn));
//        } else {
        mysqli_query($conn, "Delete from $toyear.$cfinprodpack where code='$pdrpkcode' and srno ='$psrno'") or die(mysqli_error($conn));

        mysqli_query($conn, "Insert into $toyear.$cfinprodpack Select * from $fryear.$cfinprodpack where code='$pdrpkcode' and srno ='$psrno'") or die(mysqli_error($conn));
//        }
    }


    $cnt = 0;
    $source = 'OP';
    $group = $source . '1';
    $yearno = $_POST["toyear"];
    $vouchdt = '0000-00-00';
    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, "Delete from $toyear.$ctransacn where barcode='$barcode' and source ='$source' and godown= '$godown' and series='$series'") or die(mysqli_error($conn));

//        $chkstmast = mysqli_query($conn, "SELECT * FROM `$cstkitem` WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' and source = '$source'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkstmast) > 0) {
//            mysqli_query($conn, "UPDATE `$cstkitem` SET `yearno`='$yearno',`group`='$groupt',`source`='OP',
//                                               `type`='$type',`vouchno`='$vouchno',`vouchdt`='$vouchdt',`srno`='$srno',
//                                               `brand`='$brandd',`itemcode`='$itmtcode',`itemname`='$itmtname',
//                                               `shade`='$shadet',`batch`='$batcht',`taka`='$takat',`qty`='$qtyt',
//                                               `totqty`='$totqtyt',`actqty`='$actqtyt',`flag`='$flagqt',`flagqty`='$flagqtyt',
//                                               `ord`='$ordt',`godown`='$godownt',`actbatch`='$actbatcht',`salemode`='$salemodet'
//                                                WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' AND `source` ='$source'") or die(mysqli_error($conn));
//        } else {
        mysqli_query($conn, "INSERT INTO `$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));
//        }
    }


    $cnt2 = 0;
    while ($rowraw = mysqli_fetch_assoc($stkrawqry)) {
        $cnt2++;
//        $source = $rowraw["source"];
        $groupt = $rowraw["group"];
        $vouchno = '';
        $srno = $cnt2;
        $itmtcode = $rowraw["itemcode"];
        $barcode = $rowraw["barcode"];
        $godown = $rowraw["godown"];
        $itemgrp = $rowraw["itemgrp"];
        $tqty = $rowraw["tqty"];
        $mrp = $rowraw["mrp"];
        $rate = $rowraw["rate"];
        $amount = $rowraw["amount"];
        $netamt = $rowraw["netamt"];
        $accode = '';
        $ordno = '1';
        $cost = $rowraw["cost"];
        $unit = $rowraw["unit"];
        $color = $rowraw["color"];
        $gram = $rowraw["gram"];
        $series = $rowraw["series"];
        $inwtype = $rowraw["inwtype"];
        $monno = '04';

        mysqli_select_db($conn, $toyear);
        mysqli_query($conn, "Delete from $toyear.$crawsacn where itemcode='$itmtcode' and source ='$source' and godown= '$godown'") or die(mysqli_error($conn));

//        $chkstmast = mysqli_query($conn, "SELECT * FROM `$cstkitem` WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' and source = '$source'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkstmast) > 0) {
//            mysqli_query($conn, "UPDATE `$cstkitem` SET `yearno`='$yearno',`group`='$groupt',`source`='OP',
//                                               `type`='$type',`vouchno`='$vouchno',`vouchdt`='$vouchdt',`srno`='$srno',
//                                               `brand`='$brandd',`itemcode`='$itmtcode',`itemname`='$itmtname',
//                                               `shade`='$shadet',`batch`='$batcht',`taka`='$takat',`qty`='$qtyt',
//                                               `totqty`='$totqtyt',`actqty`='$actqtyt',`flag`='$flagqt',`flagqty`='$flagqtyt',
//                                               `ord`='$ordt',`godown`='$godownt',`actbatch`='$actbatcht',`salemode`='$salemodet'
//                                                WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' AND `source` ='$source'") or die(mysqli_error($conn));
//        } else {
        mysqli_query($conn, "INSERT INTO `$crawsacn`(`yearno`, `srno`, `source`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,
                                                 `godown`, `color`, `itemgrp`, `qty`, `mrp`, `rate`, `amount`,  `accode`, `ordno`, `cost`, `unit`,
                                                 `group`, `batch`, `gram`, `monno`, `series`)
                                         VALUES ('$yearno', '$srno', '$source', '0', '$vouchdt', '$barcode', '$itmtcode',
                                                 '$godown', '$color', '$itemgrp', '$tqty', '$mrp', '$rate', '$amount', '$accode', '$ordno', '$cost', '$unit',
                                                 '$group', '', '$gram', '$monno', '$series')") or die(mysqli_error($conn));
//        }
    }

    $cnt3 = 0;
    while ($rowpack = mysqli_fetch_assoc($stkpackqry)) {
        $cnt3++;
//        $source = $rowpack["source"];

        $groupt = $rowpack["group"];
        $vouchno = '';
        $srno = $cnt3;
        $itmtcode = $rowpack["itemcode"];
        $barcode = $rowpack["barcode"];
        $godown = $rowpack["godown"];
        $itemgrp = $rowpack["itemgrp"];
        $tqty = $rowpack["tqty"];
        $mrp = $rowpack["mrp"];
        $rate = $rowpack["rate"];
        $amount = $rowpack["amount"];
        $netamt = $rowpack["netamt"];
        $accode = '';
        $ordno = '1';
        $cost = $rowpack["cost"];
        $unit = $rowpack["unit"];
        $color = $rowpack["color"];
        $gram = $rowpack["gram"];
        $series = $rowpack["series"];
        $inwtype = $rowpack["inwtype"];
        $monno = '04';

        mysqli_select_db($conn, $toyear);
        mysqli_query($conn, "Delete from $toyear.$cpacksacn where itemcode='$itmtcode' and source ='$source' and godown= '$godown'") or die(mysqli_error($conn));

//        $chkstmast = mysqli_query($conn, "SELECT * FROM `$cstkitem` WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' and source = '$source'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($chkstmast) > 0) {
//            mysqli_query($conn, "UPDATE `$cstkitem` SET `yearno`='$yearno',`group`='$groupt',`source`='OP',
//                                               `type`='$type',`vouchno`='$vouchno',`vouchdt`='$vouchdt',`srno`='$srno',
//                                               `brand`='$brandd',`itemcode`='$itmtcode',`itemname`='$itmtname',
//                                               `shade`='$shadet',`batch`='$batcht',`taka`='$takat',`qty`='$qtyt',
//                                               `totqty`='$totqtyt',`actqty`='$actqtyt',`flag`='$flagqt',`flagqty`='$flagqtyt',
//                                               `ord`='$ordt',`godown`='$godownt',`actbatch`='$actbatcht',`salemode`='$salemodet'
//                                                WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' AND `source` ='$source'") or die(mysqli_error($conn));
//        } else {
        mysqli_query($conn, "INSERT INTO `$cpacksacn`(`yearno`, `srno`, `source`, `vouchno`, `vouchdt`, `barcode`, `itemcode`,
                                                 `godown`, `color`, `itemgrp`, `qty`, `mrp`, `rate`, `amount`,  `accode`, `ordno`, `cost`, `unit`,
                                                 `group`, `batch`, `gram`, `monno`, `series`)
                                         VALUES ('$yearno', '$srno', '$source', '0', '$vouchdt', '$barcode', '$itmtcode',
                                                 '$godown', '$color', '$itemgrp', '$tqty', '$mrp', '$rate', '$amount', '$accode', '$ordno', '$cost', '$unit',
                                                 '$group', '', '$gram', '$monno', '$series')") 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>