<?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 mysqli_current_db() {
    $r = mysqli_query($conn, "SELECT DATABASE()") or die(mysqli_error($conn));
    browse("Current DB->" . mysqli_result($r, 0));
}

if (isset($_POST["save"])) {
    $fryear = $zdir . $compcd . '_' . $_POST["frmyear"];
    $toyear = $zdir . $compcd . '_' . $_POST["toyear"];
    /*     * **********Copying **************** */
    browse($fryear);
    browse($toyear);
//    die();
//    mysqli_select_db($conn, $fryear);
    $acqry = mysqli_query($conn, "SELECT * FROM $fryear.`$cacmst` where accode not in ( Select accode from $toyear.$cacmst)") or die(mysqli_error($conn));
//    print table($acqry);    
////    die();
    $fyear = $_POST["frmyear"];
    $tyear = $_POST["toyear"];


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

    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>