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

//$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];
mysqli_select_db($conn, $zdb);
$zdir = substr($zroot, 0, -4);
//browse($zdir);

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 . $_POST["frmyear"];
    $toyear = $zdir . $_POST["toyear"];
    /*     * **********Copying **************** */
    browse($fryear);
    browse($toyear);
//    die();
    mysqli_select_db($conn, $fryear);
    $acqry = mysqli_query($conn, "SELECT * FROM `$cacmst` order by accode") or die(mysqli_error($conn));
    $grpmqry = mysqli_query($conn, "SELECT * FROM `$cgrpmast`") or die(mysqli_error($conn));
    $grpheadqry = mysqli_query($conn, "SELECT * FROM `$cgrphead`") or die(mysqli_error($conn));
    $stateqry = mysqli_query($conn, "SELECT * FROM `$cstate` order by code") or die(mysqli_error($conn));
    $sltypeqry = mysqli_query($conn, "SELECT * FROM `$csaletype`") or die(mysqli_error($conn));
    $unitqry = mysqli_query($conn, "SELECT * FROM `$cunit`") or die(mysqli_error($conn));
    $transqry = mysqli_query($conn, "SELECT * FROM `$ctransport`") or die(mysqli_error($conn));
//    $courqry = mysqli_query($conn, "SELECT * FROM `$ccourier`") or die(mysqli_error($conn));
    $areaqry = mysqli_query($conn, "SELECT * FROM `$carea`") or die(mysqli_error($conn));
    $narrqry = mysqli_query($conn, "SELECT * FROM `$cnarration`") or die(mysqli_error($conn));
    $godownqry = mysqli_query($conn, "SELECT * FROM `$cgodown`") or die(mysqli_error($conn));
    $prodgrpqry = mysqli_query($conn, "SELECT * FROM `$cprodgrp`") or die(mysqli_error($conn));
    $prodqry = mysqli_query($conn, "SELECT * FROM `$cproduct`") or die(mysqli_error($conn));
    $stkitqry = mysqli_query($conn, "SELECT *,SUM(qty) as tqty FROM  `$ctransacn`  GROUP BY godown, barcode") or die(mysqli_error($conn));

//print table($stkitqry);
//die();

    $cnt = 0;
    $source = 'OP';
    $group = $source . '1';
    $yearno = $_POST["toyear"];
    $vouchdt = '0000-00-00';
    while ($rowst = mysqli_fetch_assoc($stkitqry)) {
        $cnt++;
//        $source = $rowst["source"];
        $groupt = $rowst["group"];
        $vouchno = '';
        $srno = $cnt;
//        $brandd = $rowst["brand"];
        $itmtcode = $rowst["itemcode"];
        $barcode = $rowst["barcode"];
        $godown = $rowst["godown"];
        $itemgrp = $rowst["itemgrp"];
        $tqty = $rowst["tqty"];
        $mrp = $rowst["mrp"];
        $rate = $rowst["rate"];
        $amount = $rowst["amount"];
        $netamt = $rowst["netamt"];
        $accode = '';
        $ordno = '1';
        $cost = $rowst["cost"];
        $unit = $rowst["unit"];
        $color = $rowst["color"];
        $gram = $rowst["gram"];
        $series = $rowst["series"];
        $inwtype = $rowst["inwtype"];
        $monno = '04';

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

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


    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>