<?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>
        <div class="col-lg-6 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="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="float: right">
                        <h4>SALE</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead  style="background: #78909C;">
                                <tr>
                                    <th width="5%">Bill No.</th>
                                    <th width="15%" class=''>Date</th>
                                    <th width="40%"class=''>Party</th>
                                    <th width="10%"class='text-right'>Qty.</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%"class='text-right'>Amount</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                
                                    $gettran = mysqli_query($conn, "SELECT a.*,b.*,DATE_FORMAT(a.vouchdt,'%d-%m-%Y')as date  FROM `saletran` a,`salemain` b WHERE a.`item`='$qvouch'  and a.vouchno = b.vouchno
                                                            order by b.vouchdt DESC limit 0,3")or die(mysqli_error($conn));
                                    $count = mysqli_num_rows($gettran);
                                    if($count > 0){
                                    while ($tran = mysqli_fetch_array($gettran)) {
                                        $gitem = mysqli_query($conn, "SELECT code,name FROM `item` WHERE `code`='$tran[item]'") or die(mysqli_error($conn));
                                        $fitem = mysqli_fetch_array($gitem);
                                        
                                        $gparty = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$tran[party]'") or die(mysqli_error($conn));
                                        $fparty = mysqli_fetch_array($gparty);

                                        Echo "<tr>";
                                        Echo "<td>$tran[vouchno]</td>";
                                        Echo "<td>$tran[date]</td>";
                                        Echo "<td>$fparty[name]</td>";
                                        Echo "<td class='text-right'>$tran[qty]</td>";
                                        Echo "<td class='text-right'>$tran[rate]</td>";
                                        Echo "<td class='text-right'>$tran[amount]</td>";
                                        Echo "</tr>";
                                    }
                                    }
                                ?>
                            </tbody>
                        </table>
                        <!-- purchase -->
                        <h4>PURCHASE</h4>
                        <table class="table table-striped table-condensed table-bordered" width="50%">
                            <thead  style="background: #78909C;">
                                <tr>
                                    <th width="5%">Bill No.</th>
                                    <th width="15%" class=''>Date</th>
                                    <th width="40%"class=''>Party</th>
                                    <th width="10%"class='text-right'>Qty.</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%" class='text-right'>Amount</th>
                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                
                                    $gettran2 = mysqli_query($conn, "SELECT a.* , b.*,DATE_FORMAT(a.vouchdt,'%d-%m-%Y')as date FROM `purtran` a,`purmain` b WHERE a.`itemcode`='$qvouch' and a.vouchno = b.vouchno
                                                               order by b.vouchdt DESC limit 0,3")or die(mysqli_error($conn));
                                    $count2 = mysqli_num_rows($gettran2);
                                    if($count2 > 0){
                                    while ($tran2 = mysqli_fetch_array($gettran2)) {
                                        $gitem2 = mysqli_query($conn, "SELECT code,name FROM `item` WHERE `code`='$tran2[itemcode]'") or die(mysqli_error($conn));
                                        $fitem2 = mysqli_fetch_array($gitem2);
                                        
                                        $gparty2 = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$tran2[party]'") or die(mysqli_error($conn));
                                        $fparty2 = mysqli_fetch_array($gparty2);

                                        Echo "<tr>";
                                        Echo "<td>$tran2[vouchno]</td>";
                                        Echo "<td>$tran2[date]</td>";
                                        Echo "<td>$fparty2[name]</td>";
                                        Echo "<td class='text-right'>$tran2[qty]</td>";
                                        Echo "<td class='text-right'>$tran2[rate]</td>";
                                        Echo "<td class='text-right'>$tran2[amount]</td>";
                                        Echo "</tr>";
                                    }
                                    }
                                ?>
                            </tbody>
                        </table>
                        
                    </div>
                </div>

            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg searchModal" 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="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Name</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Name</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#6082BF;">
                                    <th width="80%">Name</th>
                                    <th width="20%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
