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

$username = $_SESSION["username"];
$cacmst = "accmast";
$cgrpmast = "grpmast";
$cgrphead = "grphead";
$csaletype = "saletype";
$cexpense = "expense";
$cunit = "unit";
$ccolor = "color";
$citem = "item";
$citemgrp = "itemgrp";
$citemsubgrp = "itemsubgrp";
//$cnarration = "narration";
//$ctype = "type";
$cstkitem = "transacn";
$ctrans = "transport";
$crawmat = "rawmat";
$cstate = "state";

$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 . $_POST["frmyear"];
    $toyear = $zdir . $_POST["toyear"];
    /*     * **********Copying **************** */
    browse($fryear);
    browse($toyear);
//    die();
    mysqli_select_db($conn, $fryear);
    $acqry = mysqli_query($conn, "SELECT * FROM `$cacmst`") or die(mysqli_error($conn));
    $grpmqry = mysqli_query($conn, "SELECT * FROM `$cgrpmast`") or die(mysqli_error($conn));
    $grpheadqry = mysqli_query($conn, "SELECT * FROM `$cgrphead`") or die(mysqli_error($conn));
//    $brandqry = mysqli_query($conn, "SELECT * FROM `$cbrand`") or die(mysqli_error($conn));
//    $godownqry = mysqli_query($conn, "SELECT * FROM `$cgodown`") or die(mysqli_error($conn));
    $itemqry = mysqli_query($conn, "SELECT * FROM `$citem`") or die(mysqli_error($conn));
    $rawqry = mysqli_query($conn, "SELECT * FROM `$crawmat`") or die(mysqli_error($conn));
    $itemgrpqry = mysqli_query($conn, "SELECT * FROM `$citemgrp`") or die(mysqli_error($conn));
    $itemsubgrpqry = mysqli_query($conn, "SELECT * FROM `$citemsubgrp`") or die(mysqli_error($conn));
    $unitqry = mysqli_query($conn, "SELECT * FROM `$cunit`") or die(mysqli_error($conn));
    $colorqry = mysqli_query($conn, "SELECT * FROM `$ccolor`") or die(mysqli_error($conn));
//    $typeqry = mysqli_query($conn, "SELECT * FROM `$ctype`") or die(mysqli_error($conn));
    $expnqry = mysqli_query($conn, "SELECT * FROM `$cexpense`") or die(mysqli_error($conn));
    $transqry = mysqli_query($conn, "SELECT * FROM `$ctrans`") or die(mysqli_error($conn));
    $sltypeqry = mysqli_query($conn, "SELECT * FROM `$csaletype`") or die(mysqli_error($conn));
    $stateqry = mysqli_query($conn, "SELECT * FROM `$cstate`") or die(mysqli_error($conn));
//    if ($compcd > 10) {
    $stkmtqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty 
                            FROM `rawsacn` GROUP BY itemcode") or die(mysqli_error($conn));

    $stkitqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty 
                            FROM `$cstkitem` GROUP BY itemcode") or die(mysqli_error($conn));
//    }
//    print table($stkitqry);
//    $opstkqry = mysqli_query($conn, "SELECT *,SUM(opening) as tqty 
//                            FROM `$cstkitem` GROUP BY itemcode") or die(mysqli_error($conn));
//      $getit = mysqli_fetch_assoc($stkitqry);
//    $balqty = $get["tqty"];
    browse('rawmat');
    while ($rowraw = mysqli_fetch_assoc($rawqry)) {
        $rawsrno = $rowraw["srno"];
        $rawcode = $rowraw["code"];
        $rawname = $rowraw["name"];
        $rawtype = $rowraw["type"];
        $rawrate = $rowraw["rate"];
        $rawhsncode = $rowraw["hsncode"];
        $rawgstperc = $rowraw["gstperc"];
        $rawinactive = $rowraw["inactive"];



        mysqli_select_db($conn, $toyear);
        $chkraw = mysqli_query($conn, "SELECT * FROM `$crawmat` WHERE `code`='$rawcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkraw) > 0) {
            mysqli_query($conn, "UPDATE `$crawmat` SET `name`='$rawname', `type`='$rawtype', `hsncode`='$rawhsncode',gstperc='$rawgstperc'
                                                        WHERE code='$rawcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$crawmat Select * from $fryear.$crawmat where code='$rawcode'") or die(mysqli_error($conn));
        }
    }

    browse('state');
    while ($rowstate = mysqli_fetch_assoc($stateqry)) {
        $statecode = $rowstate["code"];
        $statename = $rowstate["name"];
        $statestcode = $rowstate["stcode"];



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

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


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

    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 **************** */
    browse('accmast');
    while ($row = mysqli_fetch_assoc($acqry)) {
        $accode = $row["accode"];
        $name = mysqli_real_escape_string($conn, $row["name"]);
//        $consname = $row["consname"];
        $add1 = mysqli_real_escape_string($conn, $row["add1"]);
        $add2 = mysqli_real_escape_string($conn, $row["add2"]);
        $add3 = mysqli_real_escape_string($conn, $row["add3"]);
//        $add4 = $row["add4"];
        $grpcode = $row["grpcode"];
        $custtype = $row["custtype"];
        $custcode = $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["closbc"];
        $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"];
//        $transport = $row["transport"];
        $lbt = $row["lbt"];
//        $discount = $row["discount"];
//        $commperc = $row["commperc"];
//        $ledgfrom = $row["ledgfrom"];
//        $ledgto = $row["ledgto"];

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


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

        $topen = $nopen + $balamt;

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

        $topen = abs($topen);


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



        $mamt = 0;
        if ($mgtype != "I" && $mgtype != "E") {
            $getamtqry = mysqli_query($conn, "SELECT 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;
        }

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

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

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

        mysqli_select_db($conn, $toyear);
        $chkac = mysqli_query($conn, "SELECT * FROM `$cacmst` WHERE accode = '$accode'");
        if (mysqli_num_rows($chkac) > 0) {
            mysqli_query($conn, "UPDATE `$cacmst` SET `add1`='$add1', `add2`='$add2', `add3`='$add3',
                                           `grpcode`='$grpcode', `custtype`='$custtype', `custcode`='$custcode', `deptcode`='$deptcode', 
                                           `nameprin`='$nameprin', `type`='$type', `init`='$init', `bankinit`='$bankinit', `tinno`='$tinno', `city`='$city',  
                                           `pin`='$pin', `country`='$country',  `tel`='$tel', `fax`='$fax', `mobile`='$mobile',
                                           `mobile2`='$mobile2', `email`='$email', openbal='$topen', `dc`='$tdc', `owner`='$owner',credlimit='$creditlimit',
                                           `website`='$website', `contpers`='$contpers', `vattin`='$vattin', `csttin`='$csttin',
                                           `pattern`='$pattern', closbal='$closbal', `closdc`='$closbdc', `pfund`='$pfund',
                                           `saltype`='$saltype', `salwise`='$salwise', salmon='$salmon', travmon='$travmon',
                                            convmon='$convmon', `p_tax`='$p_tax', `leaveflag`='$leaveflag', `leavecode`='$leavecode',
                                           `esic`='$esic', salary='$salary', active='$active', `username`='$username', 
                                           `pan`='$pan', staff='$staff', transporter ='$transporter', `state`='$state', `statecode`='$statecode',`gstno`='$gstno', `increcode`='$increcode',
                                           `lbt`='$lbt' 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`(`accode`,`name`, `add1`, `add2`, `add3`, `grpcode`, `custtype`, `custcode`, `deptcode`,
//                                               `nameprin`, `type`, `init`, `bankinit`, `tinno`, `city`, `pin`, `country`, `station`, `tel`, `fax`,
//                                                `mobile`, `mobile2`, `email`, openbal, `dc`, `owner`, credlimit, `website`, `contpers`, 
//                                               `vattin`, `csttin`, `pattern`, closbal, `closdc`, `pfund`, `saltype`, `salwise`, salmon, 
//                                                travmon, convmon, `p_tax`, `leaveflag`, `leavecode`, `esic`, salary, active, `username`, `pan`,
//                                                staff, transporter, `lbt`,`state`, `statecode`,`gstno`, `increcode`)
//   
//                                        VALUES('$accode', '$name', '$add1', '$add2', '$add3', '$grpcode', '$custtype', '$custcode', '$deptcode',
//                                               '$nameprin', '$type', '$init', '$bankinit', '$tinno', '$city', '$pin', '$country', '$station', '$tel', '$fax', 
//                                               '$mobile', '$mobile2', '$email', '$topen', '$tdc', '$owner', '$website', '$creditlimit', '$contpers', 
//                                               '$vattin', '$csttin', '$pattern', '$closbal', '$closbdc', '$pfund', '$saltype', '$salwise', '$salmon', 
//                                               '$travmon', '$convmon', '$p_tax', '$leaveflag', '$leavecode', '$esic', '$salary', '$active', '$username', '$pan', 
//                                               '$staff', '$transporter', '$lbt', '$state', '$statecode','$gstno', '$increcode')") or die(mysqli_error($conn));
        }
    }
    
    
     $cnt2 = 0;
    mysqli_select_db($conn, $toyear);
    mysqli_query($conn, "Delete from rawsacn where source = 'OP'") or die(mysqli_error($conn));
    $vouchdt = '0000-00-00';
    while ($rowmt = mysqli_fetch_assoc($stkmtqry)) {
        $cnt2++;

        $yearno = $rowmt["yearno"];
        $source = "OP";
        $series = $rowmt["series"];
        $vouchno = $rowmt["itemcode"];
//        $vouchdt = $mtodate;
        $barcode = $rowmt["barcode"];
        $itemcode = $rowmt["itemcode"];
        $qty = $rowmt["tqty"];
        $mrp = $rowmt["mrp"];
        $ratebfdisc = $rowmt["ratebfdisc"];
        $rate = $rowmt["rate"];
        $amount = $rowmt["amount"];
        $ordno = $rowmt["ordno"];
        $cost = $rowmt["cost"];
        $unit = $rowmt["unit"];
        $group = "OP" . $itemcode;


//         mysqli_select_db($conn, $fryear);
//        $transacnqry = mysqli_query($conn, "SELECT SUM(qty) AS tqty FROM `transacn` WHERE `itemcode`='$itemcodet' and `godown` = '$godownt' ") or die(mysqli_error($conn));
//        $transacnqry = mysqli_query($conn, "SELECT SUM(qty) AS tqty FROM `transacn` group by itemcode,godown ") or die(mysqli_error($conn));
//        $gettra = mysqli_fetch_assoc($transacnqry);
//        $balqty = $gettra["tqty"];


        mysqli_select_db($conn, $toyear);
//        $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 WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' AND `source` ='$source'") or die(mysqli_error($conn));
//        } else {
        if ($qty != 0) {
            mysqli_query($conn, "INSERT INTO `rawsacn`(`yearno`, `srno`, `source`, `series`, `vouchno`, `vouchdt`, 
                                                 `barcode`, `itemcode`, `qty`, `mrp`,
                                                 `ratebfdisc`, `rate`, `amount`, `ordno`, `cost`, `unit`,
                                                 `group` )
                                         VALUES ('$yearno', '$srno', '$source', '$series', '$vouchno', '$vouchdt', 
                                                 '$barcode', '$itemcode', '$qty', '$mrp', 
                                                 '$ratebfdisc', '$rate', '$amount', '$ordno', '$cost', '$unit',
                                                 '$group')") or die(mysqli_error($conn));
//        }
        }
    }

    $cnt = 0;
    mysqli_select_db($conn, $toyear);
    mysqli_query($conn, "Delete from transacn where source = 'OP'") or die(mysqli_error($conn));
    $vouchdt = '0000-00-00';
    while ($rowst = mysqli_fetch_assoc($stkitqry)) {
        $cnt++;

        $yearno = $rowst["yearno"];
        $source = "OP";
        $series = $rowst["series"];
        $vouchno = $rowst["itemcode"];
//        $vouchdt = $mtodate;
        $barcode = $rowst["barcode"];
        $itemcode = $rowst["itemcode"];
        $qty = $rowst["tqty"];
        $mrp = $rowst["mrp"];
        $ratebfdisc = $rowst["ratebfdisc"];
        $rate = $rowst["rate"];
        $amount = $rowst["amount"];
        $ordno = $rowst["ordno"];
        $cost = $rowst["cost"];
        $unit = $rowst["unit"];
        $group = "OP" . $itemcode;


//         mysqli_select_db($conn, $fryear);
//        $transacnqry = mysqli_query($conn, "SELECT SUM(qty) AS tqty FROM `transacn` WHERE `itemcode`='$itemcodet' and `godown` = '$godownt' ") or die(mysqli_error($conn));
//        $transacnqry = mysqli_query($conn, "SELECT SUM(qty) AS tqty FROM `transacn` group by itemcode,godown ") or die(mysqli_error($conn));
//        $gettra = mysqli_fetch_assoc($transacnqry);
//        $balqty = $gettra["tqty"];


        mysqli_select_db($conn, $toyear);
//        $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 WHERE `itemcode`='$itmtcode' and `shade`='$shadet' and `actbatch`='$actbatcht' AND `source` ='$source'") or die(mysqli_error($conn));
//        } else {
        if ($qty != 0) {
            mysqli_query($conn, "INSERT INTO `$cstkitem`(`yearno`, `srno`, `source`, `series`, `vouchno`, `vouchdt`, 
                                                 `barcode`, `itemcode`, `qty`, `mrp`,
                                                 `ratebfdisc`, `rate`, `amount`, `ordno`, `cost`, `unit`,
                                                 `group` )
                                         VALUES ('$yearno', '$srno', '$source', '$series', '$vouchno', '$vouchdt', 
                                                 '$barcode', '$itemcode', '$qty', '$mrp', 
                                                 '$ratebfdisc', '$rate', '$amount', '$ordno', '$cost', '$unit',
                                                 '$group')") or die(mysqli_error($conn));
//        }
        }
    }

    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));
        }
    }

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

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

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

    browse('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"];
        $trnsemail = $rowtrns["email"];


        mysqli_select_db($conn, $toyear);
        $chktrans = mysqli_query($conn, "SELECT * FROM `$ctrans` WHERE `code`='$trnscode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chktrans) > 0) {
            mysqli_query($conn, "UPDATE `$ctrans` 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'  WHERE code = '$trnscode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$ctrans Select * from $fryear.$ctrans 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('godown');
//    while ($rowgdwn = mysqli_fetch_assoc($godownqry)) {
//        $gdwncode = $rowgdwn["code"];
//        $gdwnname = $rowgdwn["name"];
//        $gdwnadd1 = $rowgdwn["add1"];
//        $gdwnadd2 = $rowgdwn["add2"];
//        $gdwnadd3 = $rowgdwn["add3"];
//        $gdwncity = $rowgdwn["city"];
//        $gdwnpin = $rowgdwn["pin"];
//        $gdwncntry = $rowgdwn["country"];
//        $gdwntel = $rowgdwn["tel"];
//        $gdwnfax = $rowgdwn["fax"];
//        $gdwnmobile = $rowgdwn["mobile"];
//        $gdwnmobile2 = $rowgdwn["mobile2"];
//        $gdwnemail = $rowgdwn["email"];
//
//
//        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 `name`='$gdwnname', `add1`='$gdwnadd1', `add2`='$gdwnadd2', `add3`='$gdwnadd3',
//                                           `city`='$gdwncity', `pin`='$gdwnpin', `country`='$gdwncntry',  `tel`='$gdwntel', `fax`='$gdwnfax', `mobile`='$gdwnmobile',
//                                           `mobile2`='$gdwnmobile2', `email`='$gdwnemail'  WHERE code = '$gdwncode'") or die(mysqli_error($conn));
//        } else {
//            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));
//        }
//    }

    while ($rowitm = mysqli_fetch_assoc($itemqry)) {
        $itmcode = $rowitm["code"];
        $itmname = $rowitm["name"];
        $itmunit = $rowitm["unit"];
        $itmprodtype = $rowitm["prodtype"];
        $itmitemgrp = $rowitm["itemgrp"];
        $itmitemsubgrp = $rowitm["itemsubgrp"];
        $itmcost = $rowitm["cost"];
        $itmrate = $rowitm["rate"];
        $itmmsp = $rowitm["msp"];
        $itmmrp = $rowitm["mrp"];
        $itmopening = $rowitm["opening"];
        $itmnmlabels = $rowitm["nmlabels"];
        $itmhsncode = $rowitm["hsncode"];
        $itmgstperc = $rowitm["gstperc"];
        $itmnameprint = $rowitm["nameprint"];


        mysqli_select_db($conn, $toyear);
        $chkitem = mysqli_query($conn, "SELECT * FROM `$citem` WHERE `code`='$itmcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkitem) > 0) {
            mysqli_query($conn, "UPDATE `$citem` SET `name`='$itmname',`itemgrp`='$itmitemgrp',`unit`='$itmunit',
                                             `cost`='$itmcost',`msp`='$itmmsp',`mrp`='$itmmrp',`rate`='$itmrate',
                                             `opening`='$itmopening',`prodtype`='$itmprodtype',
                                             `itemsubgrp`='$itmitemsubgrp',`hsncode`='$itmhsncode',`gstperc`='$itmgstperc',
                                             `nameprint`='$itmnameprint' WHERE `code`='$itmcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$citem Select * from $fryear.$citem where code='$itmcode'") 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 ('$itmcode','$itmname','$itmitemgrp','$itmunit','$itmcost','$itmmsp', '$itmmrp','$itmrate',
//                                            '$itmprodtype','$itmitemsubgrp','$itmhsncode', '$itmgstperc', '$itmnameprint')") or die(mysqli_error($conn));
        }
    }
//    
//

    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));
        }
    }
    
    while ($rowcolor = mysqli_fetch_assoc($colorqry)) {
        $colorcode = $rowcolor["code"];
        $colorname = $rowcolor["name"];

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

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

    while ($rowitsubgrp = mysqli_fetch_assoc($itemsubgrpqry)) {
        $itsbgrpcode = $rowitsubgrp["code"];
        $itsbgrpname = $rowitsubgrp["name"];
        $itsbgrpitemgrp = $rowitsubgrp["itemgrp"];

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

//            mysqli_query($conn, "INSERT INTO `$citemsubgrp`(`code`, `name`, `itemgrp`)
//                                     VALUES('$itsbgrpcode', '$itsbgrpname', '$itsbgrpitemgrp')") or die(mysqli_error($conn));
        }
    }
    browse('itemgroup');

    while ($rowitgrp = mysqli_fetch_assoc($itemgrpqry)) {
        $itgrpcode = $rowitgrp["code"];
        $itgrpname = $rowitgrp["name"];
        $itgrphsncode = $rowitgrp["hsncode"];
        $itgrpgstperc = $rowitgrp["gstperc"];

        mysqli_select_db($conn, $toyear);
        $chktypest = mysqli_query($conn, "SELECT * FROM `$citemgrp` WHERE `code`='$itgrpcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chktypest) > 0) {
            mysqli_query($conn, "UPDATE `$citemgrp` SET `name`='$itgrpname',`hsncode`='$itgrphsncode',`gstperc`='$itgrpgstperc' WHERE code='$typcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$citemgrp Select * from $fryear.$citemgrp where code='$itgrpcode'") or die(mysqli_error($conn));

//            mysqli_query($conn, "INSERT INTO `$citemgrp`(`code`, `name`, `hsncode`, `gstperc`)
//                                     VALUES('$itgrpcode', '$itgrpname', '$itgrphsncode', '$itgrpgstperc')") or die(mysqli_error($conn));
        }
    }

    while ($stkitm = mysqli_fetch_assoc($stkitqry)) {
        $stkitemcode = $stkitm["itemcode"];
        $balqty = $stkitm["tqty"];
        mysqli_select_db($conn, $fryear);
        $chkopenqty = mysqli_query($conn, "SELECT opening, code FROM `$citem` WHERE `code`='$stkitemcode'") or die(mysqli_error($conn));
        $fetopenit = mysqli_fetch_array($chkopenqty);
        $openqty = $fetopenit["opening"];

        $totqty = $openqty + $balqty;
        mysqli_select_db($conn, $toyear);
        mysqli_query($conn, "UPDATE `$citem` SET `opening`='$totqty' WHERE code='$stkitemcode'") or die(mysqli_error($conn));
    }

    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' and finyear>'$zyear' group by `finyear`  ORDER BY `lastdate`") OR die(mysqli_error($conn));
                                while ($rowt = mysqli_fetch_array($tyear)) {
                                    echo "<option value=\"$rowt[finyear]\">$rowt[finyear]</option>";
                                }
                                ?>
                            </select>
                        </div>

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