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

$formname = 'Product';
$maindb = 'item';
$printfile = "itemprint.php";
$file = 'itemmain.php';
$trandbexist = 1;
$trandb = 'prodsize';
$keyfld = "code";
$mgrpcode = "OP";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$zroot = $_SESSION["zdir"];
$compcd = $_SESSION["comp_" . $dbname];
$username = $_SESSION["username_" . $dbname];
mysqli_select_db($conn, $zdb);

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

if (isset($_POST["submit"])) {

    $a = 1;
    $name = $_POST["name"];
    $unit = $_POST["unit"];
    $prodtype = $_POST["prodtype"];
    $pcsper = $_POST["pcsper"];
    $totpcs = $_POST["totpcs"];
    $pcspera = $_POST["pcspera"];
    $totpcsa = $_POST["totpcsa"];
    $itemgrp = $_POST["itemgrp"];
    $itemsubgrp = $_POST["itemsubgrp"];
    $cost = $_POST["cost"];
    $selrate = $_POST["selrate"];
    $msp = $_POST["msp"];
    $mrp = $_POST["mrp"];
    $opening = $_POST["opening"];
    $nmlabels = $_POST["nmlabels"];
    $hsncode = $_POST["hsncode"];
    $gstperc = $_POST["gstperc"];
    $nameprint = $_POST["nameprint"];
    $totamt = $_POST["totamt"];
    $subname = $_POST["subname"];
    $kargname = $_POST["kargname"];
    $itemdesc = $_POST["itemdesc"];

    $count = $_POST["items"];


    $mvouch = $qvouch;
    //BROWSE('YAHA');
    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND code != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?$keyfld=$qvouch&mode=last");
        }
    }
    //BROWSE('FIR');
    if ($a == 1) {


        mysqli_query($conn, "DELETE FROM `$trandb` WHERE code = '$qvouch' ") or die(mysqli_error($conn));

        $cnt = 0;
        $cntamt = 0;
        for ($i = 0; $i <= $count; $i++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $size = $_POST["size_$i"];
            $cost = $_POST["cost_$i"];
            $rate = $_POST["rate_$i"];

            if ($size != '') {
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`, code, `size`, `cost`, `rate`)
                                                    VALUES('$cnt', '$qvouch', '$size', '$cost', '$rate')") or die(mysqli_error($conn));
            }
        }
        ############################## MAIN  ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO $maindb(`code`,  `name`, `unit`, `itemgrp`, `itemsubgrp`, `prodtype`,  `cost`, `rate`,`msp`,`mrp`,`opening`,
                                               `nmlabels`, `hsncode`,`gstperc`,`nameprint`,totpcs,pcsper,totpcsa,pcspera,totamt,subname,kargname,itemdesc, `adduser`)
                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$selrate', '$msp','$mrp','$opening',
                                               '$nmlabels','$hsncode','$gstperc','$nameprint','$totpcs', '$pcsper','$totpcsa', '$pcspera','$totamt','$subname','$kargname','$itemdesc', '$username')") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE $maindb SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
                                 `cost`='$cost', `rate`='$selrate', `msp`='$msp', `mrp`='$mrp', `opening`='$opening', `nmlabels`='$nmlabels',totamt='$totamt',
                                 `hsncode`='$hsncode', `gstperc`='$gstperc',`nameprint`='$nameprint',totpcs='$totpcs',pcsper='$pcsper',totpcsa='$totpcsa',itemdesc='$itemdesc',
                                  pcspera='$pcspera',subname='$subname',kargname='$kargname', `edituser`='$username' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
        }

        ######################### TRAN ###############################





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

if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getunit = mysqli_query($conn, "SELECT * FROM `unit` WHERE `code`='$get[unit]'") or die(mysqli_error($conn));
    $gunit = mysqli_fetch_array($getunit);

    $finditemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE `code`='$get[itemgrp]'") or die(mysqli_error($conn));
    $fitemgrp = mysqli_fetch_array($finditemgrp);

    $finditemsub = mysqli_query($conn, "SELECT * FROM `itemsubgrp` WHERE `code`='$get[itemsubgrp]'") or die(mysqli_error($conn));
    $fitemsub = mysqli_fetch_array($finditemsub);
}

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, transacn WRITE") or die(mysqli_error($conn));
    $chkdel = mysqli_query($conn, "Select itemcode from `transacn` where `itemcode`='$_GET[$keyfld]'") or die(mysqli_error($conn));
    $cntdel = mysqli_num_rows($chkdel);
    if ($cntdel > 0) {
        browse("Item Exist in Transactions");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
    mysqli_query($conn, "UNLOCK TABLES") or die(mysqli_error($conn));
}

include("includes/linkshead.php");
?>
<script type="text/javascript">
    $(document).ready(function () {
        $("body").on("change", ".itemgrp", gethsn);

        function gethsn() {
            var itemgrp = $('.itemgrp').val();
            $.post('gethsn.php', {
                itemgrp: itemgrp
            }, function (data) {
                $('.hsncode').val(data.hsn);
                $('.gstperc').val(data.gst);
            }, 'json');
        }
    });
</script>
<script src="MyScript/itemmain.js" type="text/javascript"></script>
<style>
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }
</style>
</head>

<body>
    <form action='' method='POST' enctype="multipart/form-data" name="invoice">

        <!-- Default box -->
        <div class="col-lg-6 col-md-6 col-sm-11 col-xs-11  pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php echo $formname; ?></h3>
                    <div class="actions pull-right">
                        <i class="fa fa-chevron-down"></i>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Code</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <input type="text" class="form-control form" name="code" value="<?php echo ($addflag != 1) ? $get["code"] : " $qvouch"; ?>" required readonly>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Name</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                            <input type="text" class="form-control form item" name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" required autofocus>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control marg">Item Group</label>
                        </div>
                        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                            <select class="form-control itemgrp form select2" name="itemgrp" required>
                                <option value="<?php echo ($addflag != 1) ? $fitemgrp['code'] : ""; ?>"><? echo ($addflag != 1) ? $fitemgrp['name'] : ""; ?></option>
                                <?php
                                $gitemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp`") or die(mysqli_error($conn));
                                while ($itemgrp = mysqli_fetch_object($gitemgrp)) {
                                    echo "<option value='$itemgrp->code'>$itemgrp->name</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <?php if ($_SESSION["compchng"] > 0) { ?>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="color: #78909C">HSN Code</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" step="any" class="form-control form hsncode" name="hsncode" value="<?php echo ($addflag != 1) ? $get["hsncode"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg" style="color: #78909C">GST &nbsp; %</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 pad">
                                <input type="text" step="any" class="form-control form text-right gstperc" name="gstperc" value="<?php echo ($addflag != 1) ? $get["gstperc"] : "0"; ?>">
                            </div>
                        </div>
                    <?php } ?>

                    <div class="row">
                        <div class="col-lg-1 pad text-right">
                        </div>
                        <div class="col-lg-11 col-md-6 col-sm-6 col-xs-6 pad text-right">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-11 col-xs-11 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="float: right">
                        <h4>SIZE</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%"></th>
                                    <th width="5%" class='text-right'>Sr. No.</th>
                                    <th width="15%" class=''>Size</th>
                                    <th width="15%" class='text-right'>Purchase Rate</th>
                                    <th width="15%" class='text-right'>Selling Rate</th>
                                </tr>
                            </thead>
                            <tbody class="tbody2">
                                <?php
                                if ($addflag != 1) {
                                    $ctr = 0;
                                    $gettran = mysqli_query($conn, "SELECT * FROM $trandb WHERE `code`='$get[code]' ") or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        echo "<tr>";
                                        echo "<td  class='text-center'><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                        echo "<td><input type='text' class='form-control form size' maxlength='10' name='size_$ctr' value='$tran[size]'></td>";
                                        echo "<td><input type='number' step='any' class='form-control form text-right cost' name='cost_$ctr' value='$tran[cost]'></td>";
                                        echo "<td><input type='number' step='any' class='form-control form text-right rate' name='rate_$ctr' value='$tran[rate]'></td>";
                                        echo "</tr>";
                                        $ctr++;
                                    }
                                } else {
                                    $pctr = 0;
                                    ?>
                                    <tr>
                                        <td class='text-center'><i class="fa fa-close remove"></i></td>
                                        <td><input type="text" class="form-control form text-right srno" name="srno_0" value="1">
                                        <td><input type="text" class="form-control form size" maxlength="10" name="size_0" value="">
                                        <td><input type="number" step="any" class="form-control form text-right cost" name="cost_0" value="0">
                                        <td><input type="number" step="any" class="form-control form text-right rate" name="rate_0" value="0">
                                    </tr>

                                    <?php
                                    $pctr++;
                                }
                                ?>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />

                            </tbody>
                        </table>
                        <button type="button" class="btn btn-primary btn-square form" id="addnew">
                            <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>
                        </button>

                    </div>

                </div>

            </div>
        </div>
    </form>

    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class=" table-responsive">
                        <table id="example1" class="table table-striped table-bordered">
                            <thead>
                                <tr style="" class="mySearchTr">
                                    <th style="width:10px;  text-align:left">Code</th>
                                    <th style="width:70px;  text-align:left">Name</th>
                                    <th style="width:70px;  text-align:left">Item Group</th>
                                    <th style="width:70px;  text-align:left">HSN Code</th>
                                    <th style="width:10px;  text-align:left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` ORDER BY `name`") or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $srcunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` where `code`='$row[unit]' ORDER BY `name`") or die(mysqli_error($conn));
                                        $sunit = mysqli_fetch_array($srcunit);

                                        $srcgroup = mysqli_query($conn, "SELECT `code`,`name` FROM `itemgrp` where `code`='$row[itemgrp]' ORDER BY `name`") or die(mysqli_error($conn));
                                        $sgroup = mysqli_fetch_array($srcgroup);

                                        $srcsubgroup = mysqli_query($conn, "SELECT `code`,`name` FROM `itemsubgrp` where `code`='$row[itemsubgrp]' ORDER BY `name`") or die(mysqli_error($conn));
                                        $ssubgroup = mysqli_fetch_array($srcsubgroup);

                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$row[code]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$row[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$sgroup[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$row[hsncode]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'><a href=\"$file?mode=edit&$keyfld=$row[code]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>