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

$formname = 'Item';
$maindb = 'item';
//$trandb = 'itemsub';
//$stkdb = 'transacn';

$printfile = "itemprint.php";
$file = 'item.php';
$trandbexist = 0;
$keyfld = "code";
$mgrpcode = "OP";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
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"];
    $itemgrp = $_POST["itemgrp"];
    $itemsubgrp = $_POST["itemsubgrp"];
    $cost = $_POST["cost"];
    $rate = $_POST["rate"];
    $msp = $_POST["msp"];
    $mrp = $_POST["mrp"];
    $opening = $_POST["opening"];
    $nmlabels = $_POST["nmlabels"];
    $count = $_POST["items"];


    $mvouch = $qvouch;

    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");
        }
    }

    if ($a == 1) {

//        #################################TRAN PART #################################
//
//        mysqli_query($conn,"DELETE FROM `$trandb` WHERE `code`='$mvouch'") or die(mysqli_error($conn));
//
//        for ($i = 0; $i < $count; $i++) {
//            $msrno = $_POST["srno_$i"];
//            $mgodown = $_POST["godown_$i"];
//            $mbatch = $_POST["batch_$i"];
//            $mqty = $_POST["qty_$i"];
//
//            if (!empty($mgodown)) {
//                mysqli_query($conn,"INSERT INTO `$trandb`(`code`,`srno`, `godown`, `batch`,`qty` )
//                                          VALUES('$mvouch', '$msrno', '$mgodown', '$mbatch', '$mqty')") or die(mysqli_error($conn));
//            }
//        }
//        #################################STOCK PART #################################
//
//        mysqli_query($conn,"DELETE FROM `$stkdb` WHERE `vouchno`='$mvouch' AND `source`='$mgrpcode'")or die(mysqli_error($conn));
//
//        for ($i = 0; $i < $count; $i++) {
//            $msrno = $_POST["srno_$i"];
//            $mgodown = $_POST["godown_$i"];
//            $mbatch = $_POST["batch_$i"];
//            $mqty = $_POST["qty_$i"];
//
//            $yearno = $myyear;
//            $group = $mgrpcode . $mvouch;
//            $vouchdt = date("Y-m-d");
//            $ord = "0";
//            $actbatch = $mgrpcode . $mvouch . $mbatch;
//
//            mysqli_query($conn,"INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `vouchno`,`srno`, `itemgrp`, `itemcode`,`itemname`,
//                            `batch`, `qty`, `godown`, `actbatch`, `ord`)
//                            VALUES('$yearno','$group','$mgrpcode', '$mvouch', '$msrno', '$itemgrp', '$mvouch', '$name', 
//                            '$mbatch', '$mqty', '$mgodown', '$mbatch', '$ord')") 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`)
                                        VALUES($qvouch, '$name', '$unit', '$itemgrp','$itemsubgrp', '$prodtype', '$cost', '$rate', '$msp','$mrp','$opening', '$nmlabels')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn,"UPDATE `$maindb` SET `name`='$name', `unit`='$unit', `itemgrp`='$itemgrp', `itemsubgrp`='$itemsubgrp', `prodtype`='$prodtype', 
                                 `cost`='$cost', `rate`='$rate', `msp`='$msp', `mrp`='$mrp', `opening`='$opening', `nmlabels`='$nmlabels' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
            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);
}
$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
</head>
<body>
    <form action='' name="invoice" method='POST'>
        <!-- Default box -->
        <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12  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 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>
                    <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 Sub Group</label>
                        </div>
                        <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                            <select class="form-control form select2" name="itemsubgrp" required >
                                <option value="<?php echo ($addflag != 1) ? $fitemsub['code'] : ""; ?>"><? echo ($addflag != 1) ? $fitemsub['name'] : ""; ?></option>
                                <?php
                                $gitemsub = mysqli_query($conn,"SELECT * FROM `itemsubgrp`")or die(mysqli_error($conn));
                                while ($mitemsub = mysqli_fetch_object($gitemsub)) {
                                    Echo "<option value='$mitemsub->code'>$mitemsub->name</option>";
                                }
                                ?>
                            </select>
                        </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">Unit</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class="form-control form select2" name="unit" style="width:100%"> 
                                <option value=" <?php echo $gunit['code'] ?>"><?php echo $gunit['name'] ?></option>
                                <?php
                                $funit = mysqli_query($conn,"SELECT `code`,`name` FROM `unit`")or die(mysqli_error($conn));
                                while ($unit1 = mysqli_fetch_object($funit)) {
                                    Echo "<option value='$unit1->code'>$unit1->name</option>";
                                }
                                ?>
                            </select>
                        </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">Product Type</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                            <select class="form-control form select2" name="prodtype" style="width:100%"> 
                                <option value="<?php Echo ($addflag != 1) ? $get["prodtype"] : ""; ?>">
                                    <?php
                                    if ($get["prodtype"] == "P") {
                                        Echo "PERFUME";
                                    } elseif ($get["prodtype"] == "O") {
                                        Echo "OTHERS";
                                    }
                                    ?>
                                </option>
                                <option value="P">PERFUMES</option>
                                <option value="O">OTHERS</option>
                            </select>
                        </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">Cost</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form cost numeric" name="cost" value="<? Echo ($addflag != 1 ) ? $get[cost] : ""; ?>">
                        </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">Rate</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="rate" value="<? Echo ($addflag != 1 ) ? $get[rate] : ""; ?>">
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">Minimum Selling Price</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="msp" value="<? Echo ($addflag != 1 ) ? $get[msp] : ""; ?>">
                        </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 text-danger marg">MRP</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="mrp" value="<? Echo ($addflag != 1 ) ? $get[mrp] : ""; ?>">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <hr>
                        </div>
                        <b>STOCK</b>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <hr>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">Opening</label>
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form rate numeric" name="opening" value="<? Echo ($addflag != 1 ) ? $get[opening] : ""; ?>">
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 numeric pad">
                            <label class="label-control text-danger marg">No. Of Labels</label>
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type='text' id="nolabel" name='nolabel' maxlength="4" class='form-control' style='width:70px;'<?php echo ($addflag != 1) ? '' : 'readonly'; ?>>                                                       
                        </div>
                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <button <?php if ($addflag != 1) { ?> onclick="window.open('blankbar.php?barcode=<?= $get[itemcode] ?>&number=' + document.invoice.nolabel.value + '&name=' + document.invoice.name.value + '&mrp=' + document.invoice.mrp.value + '&msp=' + document.invoice.msp.value , 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes')" <?php
                            } else {
                                echo "disabled";
                            }
                            ?> ><i class="fa fa-th-large btn"></i></button>
                        </div>

                    </div>
                </div>
                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </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">
                            <thead>
                                <tr style="background-color: #78909C;">
                                    <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">Group</th>
                                    <th style="width:70px;  text-align:left">Sub Group</th>
                                    <th style="width:10px;  text-align:left">Unit</th>
                                    <th style="width:10px;  text-align:left">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn,"SELECT `code`,`name`,itemgrp, itemsubgrp,`unit` FROM `$maindb` ORDER BY `code`")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;'>$ssubgroup[name]</td>";
                                        echo "<td style='width:10px;  text-align:left; padding-left:20px;'>$sunit[name]</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>
