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

ini_set('display_errors',1);
error_reporting(E_ERROR);

$zyear1 = $_SESSION["zdbyear_" . $dbname];
$zyear = substr($zyear1, -4);
$zroot = $_SESSION["zdir"];
$zdir = substr($zroot, 0, -4);
ini_set('display_errors', 1);
error_reporting(E_ERROR);

//$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";
$crawgrp = "rawmatgrp";
$citemsubgrp = "itemsubgrp";
//$cnarration = "narration";
//$ctype = "type";
$cstkitem = "transacn";
$cprodsize = "prodsize";
$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));
//}
// browse(1);
if (isset($_POST["save"])) {
    $fryear = $zdir . $compcd . '_' . $_POST["frmyear"];
    $toyear = $zdir . $compcd . '_' . $_POST["toyear"];
    $fyear = $_POST["frmyear"];
    $tyear = $_POST["toyear"];
//    die();
    /*     * **********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));
    $rawgrpqry = mysqli_query($conn, "SELECT * FROM `$crawgrp`") 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));
    $prodsizeqry = mysqli_query($conn, "SELECT * FROM `$cprodsize`") or die(mysqli_error($conn));
//    if ($compcd > 10) {
    $stkmtqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty 
                            FROM `rawsacn` GROUP BY itemcode,color,lot") or die(mysqli_error($conn));

    $stkitqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty 
                            FROM `$cstkitem` GROUP BY itemcode,size") or die(mysqli_error($conn));
    if ($compcd > 100) {
        $mpart = "party";
    } else {

        $mpart = "broker";
    }

//    print "SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as vouchdt, 
//                DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
//                `billdt`,  `billamt`, `accode` , 'PB' as billsrc FROM $fryear.`prevbill` WHERE 
//                `type` = 'R' and accode = 182
//                union all
//                SELECT '$fyear' AS `yearno`, 'S' AS `type`, `vouchno`, `invno` as `billno`, vouchdt,
//                DATE_FORMAT(`invdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `$mpart` as  `accode` , 'SLM' as billsrc FROM `rawmain`
//                WHERE `cashcred` != 'C' and canc_flag!='C' and `$mpart`=182
//                UNION 
//                SELECT '$fyear' AS `yearno`, 'S' AS `type`, `vouchno`, `invno` as `billno`, vouchdt,
//                DATE_FORMAT(`invdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `$mpart` as  `accode`, 'SLFP' as billsrc FROM `gstmain`
//                WHERE `cashcred` != 'C'  and canc_flag!='C' and `$mpart`=182 ";
    mysqli_query($conn, "create temporary table cmain 
                SELECT `yearno`,`type`,`vouchno`, `billno`, billdate as vouchdt, agency,
                DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 
                `billdt`,  `billamt`, `accode` , 'PB' as billsrc FROM $fryear.`prevbill` WHERE 
                `type` in ('R','S')
                union all
                SELECT '$fyear' AS `yearno`, 'S' AS `type`, `vouchno`, `invno` as `billno`, vouchdt,agency,
                DATE_FORMAT(`invdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `$mpart` as  `accode` , 'SLM' as billsrc FROM `rawmain`
                WHERE `cashcred` != 'C' and canc_flag!='C'
                UNION 
                SELECT '$fyear' AS `yearno`, 'S' AS `type`, `vouchno`, `invno` as `billno`, vouchdt,agency,
                DATE_FORMAT(`invdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `$mpart` as  `accode`, 'SLFP' as billsrc FROM `gstmain`
                WHERE `cashcred` != 'C'  and canc_flag!='C'
                ") or die(mysqli_error($conn));

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

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

//    print table($subtmp1);
//    die();
// browse(2);

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

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

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

        $trn = mysqli_query($conn, "SELECT * FROM `billsett` WHERE `source` = '$msource' AND
                                   `bnkvouch` = '$mvouch' AND `party` = '$qparty' and `billsrc` = '$qbillsrc'  AND 
                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = 'S' ") 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 `billsett` WHERE 
                                     `yearno` = '$qyear' AND `type` = 'S' AND `vouchno` = '$qvouch' AND 
                                     `party` = '$qparty' and `billsrc` = '$qbillsrc' 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`,billsrc, actbillamt, agency)
                                               VALUES ('$qyear', '$qparty', 'S', '$qsrno', '$qbill', '$mbilldate', '$balamt', '', '$qvouch', '', 'Y', '$qbillsrc', '$qamt', '$qagency') ") OR die(mysqli_error($conn));
        }
    }
//     die();
// browse(1);

//    for outstanding payable
    mysqli_select_db($conn, $fryear);
    mysqli_query($conn, "create temporary table cmain2
                SELECT '$fyear' AS `yearno`, 'P' AS `type`, `vouchno`, `invno` as  `billno`, vouchdt,

                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `broker` as  `accode`, 'PUM' as billsrc FROM purrawmain

                       WHERE `cashcred` != 'C' 
                       
                        UNION 

                       SELECT '$fyear' AS `yearno`, 'P' AS `type`, `vouchno`, `invno` as  `billno`, vouchdt,

                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`, `netamt` as  `billamt`, `broker` as  `accode`, 'PUFP' as billsrc FROM pgstmain

                       WHERE `cashcred` != 'C'
                           
                       union
                       SELECT '$fyear' AS `yearno`, 'P' AS `type`, `vouchno`, `invno` as  `billno`, vouchdt,

                       DATE_FORMAT(`vouchdt`, '%d-%m-%Y') AS `billdate`,  `billamt`, `party` as  `accode`, 'PUEX' as billsrc FROM purexpmain

                       WHERE `paidby` != 'C'
                           
                       union
                       
                       SELECT `yearno`,`type`,`vouchno`, `billno`,  billdate as  vouchdt,DATE_FORMAT(`billdate`, '%d-%m-%Y') AS 

                      `billdate`, `billamt`, `accode`, 'PB' as billsrc FROM `prevbill` WHERE 

                      `type` = 'P'
                ") 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);
//    
    mysqli_query($conn, "Delete from `$toyear`.`prevbill` WHERE `frmprev` = 'Y' and type = 'P'") or die(mysqli_error($conn));

    while ($subtmp = mysqli_fetch_assoc($subtmp2)) {
        $qparty = $subtmp["accode"];
        $qyear = $subtmp["yearno"];
        $qbill = $subtmp["billno"];
        $qbillsrc = $subtmp["billsrc"];
        $mbilldate = $subtmp["vouchdt"];
        $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 `billsett` WHERE `source` = '$msource' AND
                                   `bnkvouch` = '$mvouch' AND `party` = '$qparty' and `billsrc` = '$qbillsrc'  AND 
                                   `yearno` = '$qyear' AND `vouchno` = '$qvouch' and `type` = 'P' ") 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 `billsett` WHERE 
                                     `yearno` = '$qyear' AND `type` = 'P' AND `vouchno` = '$qvouch' AND 
                                     `party` = '$qparty' and `billsrc` = '$qbillsrc' LIMIT 0,1 ") or die(mysqli_error($conn));

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

        if($qbill == '977' && $qparty == 75){
            browse($qamt);
            browse($totadj);
            browse($balamt);
        }
        $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`,billsrc, actbillamt)
                                               VALUES ('$qyear', '$qparty', 'P', '$qsrno', '$qbill', '$mbilldate', '$balamt', '', '$qvouch', '', 'Y', '$qbillsrc', '$qamt') ") OR die(mysqli_error($conn));
        }
    }

    // die(); ??
//    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('prodsize');
    while ($rowprodsize = mysqli_fetch_assoc($prodsizeqry)) {
        $pdszcode = $rowprodsize["code"];
        $pdszname = $rowprodsize["size"];
        $pdszcost = $rowprodsize["cost"];
        $pdszrate = $rowprodsize["rate"];

        mysqli_select_db($conn, $toyear);
        $chkstate = mysqli_query($conn, "SELECT * FROM `$cprodsize` WHERE `code`='$pdszcode' and size = '$pdszname'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chkstate) > 0) {
//            mysqli_query($conn, "UPDATE `$cprodsize` SET `cost`='$pdszcost', `rate`='$pdszrate' WHERE code='$pdszcode'  and size = '$pdszname'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$cprodsize Select * from $fryear.$cprodsize where code='$pdszcode'") 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"];
        $currbal = $row["currbal"];
        $currdc = $row["currbc"];
        $closbal = $row["closbal"];
        $closdc = $row["closdc"];
        $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 ($currdc == "D") {
            $currbal = abs($currbal) * -1;
        } else {
            $currbal = abs($currbal);
        }

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

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

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

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

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

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


        mysqli_select_db($conn, $toyear);
        $chkac = mysqli_query($conn, "SELECT * FROM `$cacmst` WHERE accode = '$accode'");
        if (mysqli_num_rows($chkac) > 0) {
            if ($accode != '23' && $accode != '24') {
                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', `owner`='$owner',credlimit='$creditlimit', `pfund`='$pfund',
                                           `website`='$website', `contpers`='$contpers', `vattin`='$vattin', `csttin`='$csttin', `pattern`='$pattern',
                                            openbal='$topen', `dc`='$tdc', currbal='$currbal', currdc='$currdc', closbal='$closbal', `closdc`='$closdc', 
                                           `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 {
                if ($accode == '23') {
                    mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal= 0, `dc`='D', currbal='0', currdc='D', closbal='0',closdc='D' WHERE accode = '$accode'") or die(mysqli_error($conn));
                    mysqli_query($conn, "UPDATE $toyear.$cacmst SET openbal= '$topen', `dc`='D'  WHERE accode = '24' ") or die(mysqli_error($conn));
                }
            }
        } else {
            mysqli_query($conn, "Insert into $toyear.$cacmst Select * from $fryear.$cacmst where accode='$accode'") or die(mysqli_error($conn));

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


    $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"];
        $color = $rowmt["color"];
        $lot = $rowmt["lot"];
        $group = "OP" . $itemcode;

        mysqli_select_db($conn, $toyear);
        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`, lot, color )
                                         VALUES ('$yearno', '$srno', '$source', '$series', '$vouchno', '$vouchdt', 
                                                 '$barcode', '$itemcode', '$qty', '$mrp', 
                                                 '$ratebfdisc', '$rate', '$amount', '$ordno', '$cost', '$unit',
                                                 '$group', '$lot', '$color')") 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"];
        $size = $rowst["size"];
//        $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, $toyear);
        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`,size )
                                         VALUES ('$yearno', '$srno', '$source', '$series', '$vouchno', '$vouchdt', 
                                                 '$barcode', '$itemcode', '$qty', '$mrp', 
                                                 '$ratebfdisc', '$rate', '$amount', '$ordno', '$cost', '$unit',
                                                 '$group','$size')") 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));
        }
    }

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

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


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

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

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

//    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='$itgrpcode'") 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));
        }
    }

//    browse('rawgroup');

    while ($rowrawgrp = mysqli_fetch_assoc($rawgrpqry)) {
        $rawgrpcode = $rowrawgrp["code"];
        $rawgrpname = $rowrawgrp["name"];
        $rawgrphsncode = $rowrawgrp["hsncode"];
        $rawgrpgstperc = $rowrawgrp["gstperc"];
        $chg = $rowrawgrp["chg"];

        mysqli_select_db($conn, $toyear);
        $chktypest1 = mysqli_query($conn, "SELECT * FROM `$crawgrp` WHERE `code`='$rawgrpcode'") or die(mysqli_error($conn));
        if (mysqli_num_rows($chktypest1) > 0) {
            mysqli_query($conn, "UPDATE `$crawgrp` SET `name`='$rawgrpname',`hsncode`='$rawgrphsncode',`gstperc`='$rawgrpgstperc',chg='$chg' WHERE code='$rawgrpcode'") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "Insert into $toyear.$crawgrp Select * from $fryear.$crawgrp where code='$rawgrpcode'") or die(mysqli_error($conn));
        }
    }


    browse("Processing Complete");
    header("refresh: 1;yearend.php");
}
include("includes/linkshead.php");
?>

<script type="text/javascript">
    $(document).ready(function () {
        $(".sloader").hide();
        $(".submit").click(function () {
            $(".sloader").show();
            $(".submit").hide();
        });

    });
</script>

</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 sloader text-center">
                    <img src="images/search.gif" alt="loading">       
                </div>
                <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 submit">
                            <!--<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>