<?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"];
$ctransacn = "transacn";
$crawsacn = "rawsacn";
$cpacksacn = "packsacn";
$cprodsacn = "prodsacn";

$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);
    $stkrwqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty FROM  `$cpacksacn` GROUP BY itemcode,godown order by itemcode,godown") or die(mysqli_error($conn));
    $stkitqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty FROM  `$ctransacn` GROUP BY godown,series,itemcode order by itemcode,godown,series") 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"];
        $godown = $rowst["godown"];
        $batch = $rowst["batch"];
//        $vouchdt = $mtodate;
        $barcode = $rowst["barcode"];
        $gram = $rowst["gram"];
        $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 `$ctransacn`(`srno`, `yearno`, `godown`, `series`,`vouchno`, `vouchdt`,
                                                  `itemcode`, `barcode`, `gram`, `unit`, `qty`, `source`, 
                                                  `batch`, `group` , `rate`, `mrp`,
                                                  `ordno`, `monno`)
                                           VALUES('$cnt', '$yearno', '$godown', 'LO','$mvouch', '$vouchdt', 
                                                  '$itemcode', '$barcode', '$gram', '$unit', '$qty', 'OP',
                                                  '$batch', '$group', '$rate', '$mrp', 
                                                  '1', '4')") or die(mysqli_error($conn));
        }
    }

    mysqli_query($conn, "Delete from $cpacksacn where source = 'OP'") or die(mysqli_error($conn));
    $vouchdt = '0000-00-00';
    while ($rowrw = mysqli_fetch_assoc($stkrwqry)) {
        $cnt++;

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


        mysqli_select_db($conn, $toyear);
        if ($qty != 0) {
            mysqli_query($conn, "INSERT INTO `$cpacksacn`(`srno`, `yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`,  `unit`,
                                                  `qty`, `source`, amount, accode, `group` , `rate`, `cost`, `itemgrp`, `ordno`, `monno`)
                            VALUES('$cnt', '$yearno', '$godown', '$mvouch', '$vouchdt', '$itemcode',  '$unit',
                                   '$qty', 'OP', '$amt', '', '$group', '', '', '$itemgrp', '1', '4')") 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>