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

$formname = 'Item Update';
$maindb = 'item';
$trandb = 'itemsub';
$proddb = 'product';
$grpdb = 'prodgrp';
$proddbtran = 'prodtran';

$printfile = "itemlst.php";
$invprintfile = "";
$file = 'itemupdent.php';
$trandbexist = 1;
$keyfld = "code";
$focusfield = "code";

$zseries = $_SESSION['user_series'];
$keycond = "";
$msource = "";

$myear = $_SESSION["tit_" . $dbname];
//browse($myear);

$gppdb = 'cepheisy_gpp_1_' . $myear;
$poidb = 'cepheisy_poi_1_' . $myear;
//browse($poidb);
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$usertype = $_SESSION["usertype"];

//include("includes/vouchno_generate.php");
//include("includes/prev&next.php");

//if (isset($_POST["submit"])) {
    $a = 1;
    /*     * Start sale exist check** */
//    $newname = $_POST['newitem'];
//    $oldname = $_POST['olditem'];
////    $orderinv = $_POST['orderinv'];
//    if ($newname == '') {
//        browse('New name cannot be Empty');
//        $a = 0;
//    }
//    if ($oldname == '') {
//        browse('Old name cannot be Empty');
//        $a = 0;
//    }


    $count = $_POST['items'];

    if ($a == 1) {

        $sql = "SHOW TABLES FROM $zdb";
        $result = mysqli_query($conn, $sql);
        $num_tabs = mysqli_num_rows($result);
        $arraytab = 0;
        while ($row = mysqli_fetch_row($result)) {
            $tableNames[$arraytab] = $row[0];
            $arraytab++; //only do this to make sure it starts at index 0
//            $tname = $tableNames[$arraytab];
        }

        foreach ($tableNames as & $tname) {
//            print "<b style='font-size: 25px'>" . $tname . "</b><br>";
//            $gettab = mysqli_query($conn, "SELECT  * from `$tname` WHERE column_name LIKE '%itemname%'") or die(mysqli_error($conn));
            $chkcol = mysqli_query($conn, "SELECT COLUMN_TYPE, EXTRA FROM information_schema.columns 
                                        WHERE  `table_schema` = '$zdb'  AND `table_name` = '$tname' AND  column_name = 'godown'")or die(mysqli_error($conn));
            $tno = mysqli_num_rows($chkcol);
            if ($tno > 0) {
                mysqli_query($conn, "Update $zdb.`$tname` set godown='1' where godown = 'WH1'") or die(mysqli_error($conn));
                
            }
        }



//        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `openstk` WRITE, $poidb.$maindb WRITE, $gppdb.$proddb WRITE, $gppdb.$proddbtran WRITE");
//
//
//        if ($makeinpoi == '1') {
//
//            if ($namesize != '1') {
//                $newname = $name;
//            } else {
//                $newname = $name . ' - ' . $size;
//            }
//
////        for poi
//            $getpoi = mysqli_query($conn, "SELECT * FROM $poidb.`$maindb` WHERE name='$newname'") or die(mysqli_error($conn));
//            if (mysqli_num_rows($getpoi) > 0) {
//                $fetpoi = mysqli_fetch_array($getpoi);
//                $pdpoi = $fetpoi['code'];
//            } else {
//                $genpoi = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM $poidb.`$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
//                if (mysqli_num_rows($genpoi) > 0) {
//                    $vpoi = mysqli_fetch_assoc($genpoi);
//                    $pdpoi = $vpoi["maxcode"] + 1;
//                } else {
//                    $pdpoi = '1';
//                }
//                $getpoiunit = mysqli_query($conn, "SELECT code, name FROM $poidb.`unit` WHERE name='$unit'") or die(mysqli_error($conn));
//                $fetpoiunit = mysqli_fetch_array($getpoiunit);
//                $pdunitpoi = $fetpoiunit['code'];
//
//                $getpoigrp = mysqli_query($conn, "SELECT code, name FROM $poidb.`itemgrp` WHERE name='$itgrpname'") or die(mysqli_error($conn));
//                $fetpoigrp = mysqli_fetch_array($getpoigrp);
//                $pdgrppoi = $fetpoigrp['code'];
//                mysqli_query($conn, "INSERT INTO $poidb.`$maindb`(`code`, `name`, `unit`, `itemgrp`, `nameprin`, `gstperc`, `hsncode`, `grptype`)
//                                    VALUES($pdpoi, '$newname', '$pdunitpoi', '$pdgrppoi', '$nameprin', '$taxperc','$hsncode', 'P')") or die(mysqli_error($conn));
//            }
//        }
//        if ($makeingpp == '1') {
////        for gpp
//
//            $getprod = mysqli_query($conn, "SELECT * FROM $gppdb.`$proddb` WHERE name='$name'") or die(mysqli_error($conn));
//            if (mysqli_num_rows($getprod) > 0) {
//                $fetprod = mysqli_fetch_array($getprod);
//                $pdvouch = $fetprod['code'];
//            } else {
//                $genpd = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM $gppdb.`$proddb` LIMIT 0,1 ") or die(mysqli_error($conn));
//                if (mysqli_num_rows($genpd) > 0) {
//                    $vpd = mysqli_fetch_assoc($genpd);
//                    $pdvouch = $vpd["maxcode"] + 1;
//                } else {
//                    $pdvouch = '1';
//                }
//                $getprodgrp = mysqli_query($conn, "SELECT code, name FROM $gppdb.`$grpdb` WHERE name='$itgrpname'") or die(mysqli_error($conn));
//                $fetprodgrp = mysqli_fetch_array($getprodgrp);
//                $pdgrpcode = $fetprodgrp['code'];
//                mysqli_query($conn, "INSERT INTO $gppdb.`$proddb`(`code`, `name`, `unit`, `prodgrp`)
//                                    VALUES($pdvouch, '$name', '$unit', '$pdgrpcode')") or die(mysqli_error($conn));
//            }
//
//
////        $getpddtl = mysqli_query($conn, "SELECT * FROM $gppdb.`$proddbtran` WHERE code='$pdvouch' and size='$") or die(mysqli_error($conn));
//            mysqli_query($conn, "DELETE FROM $gppdb.`$proddbtran` WHERE code='$pdvouch' and size='$size'") or die(mysqli_error($conn));
//
//            mysqli_query($conn, "INSERT INTO $gppdb.`$proddbtran`(`code`, `prodcode`, `size`, `sizeprod`, `cutavg`, `rate`, `length`, `chest`)
//                                    VALUES('$pdvouch', '$pdvouch', '$size', '$sizeprod', '$cutavg', '$ratepcs', '$length', '$chest')") or die(mysqli_error($conn));
//        }
//
//        if ($addflag == 1) {
//            mysqli_query($conn, "INSERT INTO `$maindb`(`code`, `name`, `size`,  `unit`, `itemgrp`, `levelno`, `nameprin`, `namecstmprin`, `nameprtyprin`,`opstock`, `namesize`, `makeingpp`, `makeinpoi`,
//                                    `minstock`, `maxstock`, `taxperc`,`hsncode`, `bcode`, `orderinv`, `sizeprod`, `cutavg`, `ratepcs`, `length`, `chest`)
//                                    VALUES($qvouch, '$name', '$size', '$unit', '$itemgrp', '$levelno', '$nameprin', '$namecstmprin', '$nameprtyprin','$opstock', '$namesize', '$makeingpp', '$makeinpoi',
//                                    '$minstock', '$maxstock', '$taxperc','$hsncode','$zseries', '$orderinv', '$sizeprod', '$cutavg', '$ratepcs', '$length', '$chest')") or die(mysqli_error($conn));
//            $mvouch = $qvouch; //1
//        } else {
//            mysqli_query($conn, "DELETE FROM `openstk` WHERE `itemname`='$name' and `size`='$size'") or die(mysqli_error($conn));
//            $srl = 0;
//            for ($i = 0; $i < $count; $i++) {
//                $opseries = $_POST["opseries_$i"];
//                $opsercode = $_POST["opsercode_$i"];
//                $opstk = $_POST["opstk_$i"];
//                $srl++;
////                if ($opstk > 0) {
//                mysqli_query($conn, "INSERT INTO `openstk`(`id`,`srno`, `itemcode`, `itemname`, `size`, `actsize`, `unit`, `opstk`, `minstk`, `series`)
//                            VALUES('', '', '$qvouch', '$name', '$size', '$size', '$unit', '$opstk', '', '$opsercode')") or die(mysqli_error($conn));
////                }
////                mysqli_query($conn, "UPDATE `openstk` set `opstk`='$opstk' where `series`='$opsercode' and `itemname`='$name' and `size`='$size'") or die(mysqli_error($conn));
//            }
//
//            $mvouch = $qvouch; //1
//
//            mysqli_query($conn, "UPDATE `$maindb` SET  `name` = '$name', `size` = '$size', `unit` = '$unit', `itemgrp` = '$itemgrp', `namesize`='$namesize',
//                    `levelno` = '$levelno', `nameprin` = '$nameprin', `namecstmprin`='$namecstmprin', `nameprtyprin`='$nameprtyprin',`opstock` = '$opstock', `minstock` = '$minstock', `maxstock` = '$maxstock',
//                    `taxperc` = '$taxperc',`hsncode` = '$hsncode', `bcode`='$zseries', `orderinv`='$orderinv', `makeingpp`='$makeingpp', `makeinpoi`='$makeinpoi',
//                    `sizeprod`='$sizeprod', `cutavg`='$cutavg', `ratepcs`='$ratepcs', `length`='$length', `chest`='$chest' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//        }
        ############################## MAIN  - challan part ######################################################
        mysqli_query($conn, "UNLOCK TABLES");

//        if ($addflag == 1) {
//            insmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
//        } else {
//            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
//        }
    }
//}




include("includes/linkshead.php");
if ($addflag != 1) {
    $read = 'readonly';
}
?>
<script type="text/javascript">
    $(document).ready(function () {
        $("body").on('blur', '.olditem,.newitem', chkSame);
        function chkSame() {
            var olditem = $(".olditem").val(),
                    newitem = $(".newitem").val();
            if (olditem != '' && newitem != '') {
                if (olditem == newitem) {
                    alert("Both Cannot be Same");
                    $(".newitem").val("").trigger('change');
                }
            }
        }
    });
</script>
</head>
<body>
    <div id="preloader">
        <div><img src="assets/img/main.gif"></div>
    </div>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color :#05B2EA;"><?php
                            Echo $formname;
                            if ($usertype == 1) {
                                ?>
                                                                                <!--<input type="button" class="btn btn-danger delmulti" value="Delete Multi">-->
                            <?php } ?>
                        </h3>
                        <div class="actions pull-right" style="margin-right: 50px;">
                            <?php
                            $getmseries = mysqli_query($conn, "SELECT `code`, `name` FROM `series` WHERE `code`='$zseries'") or die(mysqli_error($conn));
                            $mseries = mysqli_fetch_array($getmseries);
                            ?>
                            <label class="label-control"><?php Echo $zseries; ?></label>  <label class="label-control"><?php Echo $mseries['name']; ?></label> 
                        </div>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>

                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class='col-lg-12 col-md-12 col-sm-12 col-xs-13 pad text-center'>
                                <label class='label-control tmarg' style="color:red;font-size:20px">Note : New Name will be Replaced the Old One <br> and Old one will be deleted from Masters</label>
                            </div>
                        </div>
                        <br>
                        <br>
                        <div class='row'>
                            <div class='col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right'>
                                <label class='label-control tmarg'>Old Name</label>
                            </div>
                            <div class='col-lg-6 col-md-9 col-sm-9 col-xs-9 pad'>
                                <select  class="select2 form-control  olditem" name="olditem">
                                    <option value=""></option>
                                    <?php
                                    $getitm = mysqli_query($conn, "SELECT  *  FROM `item` where code != '' group by name order by name")or die(mysqli_error($conn));
                                    while ($gi = mysqli_fetch_array($getitm)) {
                                        Echo "<option value='$gi[name]'>$gi[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class='row'>
                            <div class='col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right'>
                                <label class='label-control tmarg'>New Name</label>
                            </div>
                            <div class='col-lg-6 col-md-9 col-sm-9 col-xs-9 pad'>
                                <select  class="select2 form-control  newitem" name="newitem">
                                    <option value=""></option>
                                    <?php
                                    $getitm2 = mysqli_query($conn, "SELECT  *  FROM `item` where code != '' group by name  order by name")or die(mysqli_error($conn));
                                    while ($gi2 = mysqli_fetch_array($getitm2)) {
                                        Echo "<option value='$gi2[name]'>$gi2[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
<!--                        <hr>
                        <div class="row">

                            <div class='col-lg-3 col-md-1 col-sm-3 col-xs-3 pad text-right'>
                                <label class='label-control tmarg' style="color: red;">Update in POI</label>
                            </div>     
                            <div class='col-lg-1 col-md-1 col-sm-3 col-xs-3 pad'>
                                <input type="checkbox" name="makeinpoi" class="form-control  makeinpoi"  style="height: 25px;width: 25px;" value="1" <?php
                                if ($get['makeinpoi'] == 1) {
                                    echo "checked";
                                }
                                ?> >
                            </div> 
                        </div>
                        <hr>
                        <div class="row">
                            <div class='col-lg-3 col-md-1 col-sm-3 col-xs-3 pad text-right'>
                                <label class='label-control tmarg' style="color: red;">Update in GPP</label>
                            </div>     
                            <div class='col-lg-1 col-md-1 col-sm-3 col-xs-3 pad'>
                                <input type="checkbox" name="makeingpp" class="form-control  makeingpp"  style="height: 25px;width: 25px;" value="1"<?php
                                if ($get['makeingpp'] == 1) {
                                    echo "checked";
                                }
                                ?>> 
                            </div> 
                        </div>-->

                    </div>
                    <div class="panel-footer">
                        <table class="table-condensed" style="margin: 0 auto;">
                            <tr>
                                <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: 
                                    <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Update Entry">
                                    <button id='submit' type='submit' name='submit' value='Update' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                                        <i class="fa fa-check" style="color:#ffffff"></i>
                                    </button>
                                </td>
                                <td class=" buttonform tooltip-bottom" id="scancel" style="display: <? Echo $canceldisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Cancel Entry">
                                    <button id='cancel' type="button" class="btn btn-pad btn-square " style="background-color:#39CCCC; "  onClick="window.location.href = 'default.php'">
                                        <i class="fa fa-times" style="color:#ffffff"></i>
                                    </button>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>

        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
