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

$formname = 'Dismantle';
$maindb = 'dismantle';
//$trandb = 'inwtran';
$stkdb = 'transacn';
$printfile = "dismantle_print.php";
$file = 'dismantle.php';
//$trandbexist = 1;
$source = "DISM";
$keyfld = "vouchno";
$keycond = "";
$para = "";
$myyear = $_SESSION["tit_" . $dbname];


$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

mysqli_select_db($conn, $zdb);

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

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

    $a = 1;
    $vouchdt = trim($_POST["vouchdt"]);
    $voucher = trim($_POST["voucher"]);
    $mono = $_POST["monno"];
    $godown = trim($_POST["godown"]);

    $barcode = $_POST["abarcode"];
    $itemcode = $_POST["aitemcode"];
    $itemname = $_POST["aitemname"];
    $gram = $_POST["agram"];
    $hsncode = $_POST["ahsncode"];
    $unit = $_POST["aunit"];
    $batch = $_POST["abatch"];
    $qty = $_POST["aqty"];
    $remark = $_POST["aremark"];

    $lbarcode = $_POST["lbarcode"];
    $litemcode = $_POST["litemcode"];
    $litemname = $_POST["litemname"];
    $lgram = $_POST["lgram"];
    $lhsncode = $_POST["lhsncode"];
    $lunit = $_POST["lunit"];
    $lbatch = $_POST["lbatch"];
    $lqty = $_POST["lqty"];
    $lremark = $_POST["lremark"];


//    if ($godown == $fseries) {
//        $a = 0;
//        browse("Godown Cannot be Same");
//    }


    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$stkdb` WRITE , `product` WRITE ")or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
        }
//        $mvouch = $qvouch;
//        $mlen = strlen($qvouch);
        $mvouch = $qvouch;



        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE vouchno='$mvouch' AND `source`='$source'") or die(mysqli_error($conn));

        $group = $source . $mvouch;
        
        $gitemgrp = mysqli_query($conn, "SELECT `barcode`,`prodgrp`,`cost`,`mrp` FROM `product` WHERE `barcode`= '$barcode'") or die(mysqli_error($conn));
        $fitemgrp = mysqli_fetch_array($gitemgrp);
        $itemgrp = $fitemgrp[prodgrp];
        $rate = $fitemgrp[rate];
        $mrp = $fitemgrp[mrp];
        $mqty = $qty * -1;

        mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`, `barcode`, `gram`, `unit`,
                            `qty`, `source`, `batch`,`rate`,`mrp`,`monno`,`group`,`ordno`,`itemgrp`)
                            VALUES('$myyear', '$godown', '$mvouch', '$vouchdt', '$itemcode',  '$barcode', '$gram', '$unit',
                                   '$mqty', '$source', '$batch', '$rate', '$mrp', '$mono', '$group', '','$itemgrp')") or die(mysqli_error($conn));

        $lgitemgrp = mysqli_query($conn, "SELECT `barcode`,`prodgrp`,`cost`,`mrp` FROM `product` WHERE `barcode`= '$lbarcode'") or die(mysqli_error($conn));
        $lfitemgrp = mysqli_fetch_array($lgitemgrp);
        $litemgrp = $fitemgrp[prodgrp];
        $lrate = $fitemgrp[rate];
        $lmrp = $fitemgrp[mrp];

        mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`, `barcode`, `gram`, `unit`,
                            `qty`, `source`, `batch`,`rate`,`mrp`,`monno`,`group`,`ordno`,`itemgrp`)
                            VALUES('$myyear', '$godown', '$mvouch', '$vouchdt', '$litemcode',  '$lbarcode', '$lgram', '$lunit',
                                   '$lqty', '$source', '$lbatch', '$lrate', '$lmrp', '$mono', '$group', '','$litemgrp')") or die(mysqli_error($conn));



        
        


        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `voucher`,`vouchdt`, `mono`, `godown`, `itemcode`, `itemname`, `barcode`, `gram`, hsncode,
                                                  unit, batch, qty, remark, `litemcode`, `litemname`, `lbarcode`, `lgram`, lhsncode,
                                                  lunit, lbatch, lqty, lremark)
                            VALUES('$mvouch',  '$newvoucher', '$vouchdt', '$mono', '$godown', '$itemcode', '$itemname', '$barcode', '$gram', '$hsncode',
                                   '$unit', '$batch', '$qty','$remark', '$litemcode', '$litemname', '$lbarcode', '$lgram', '$lhsncode',
                                   '$lunit', '$lbatch', '$lqty','$lremark')") or die(mysqli_error($conn));

        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `voucher`='$voucher',`godown`='$godown', `mono`='$mono', `itemcode`='$itemcode', 
                                              `itemname`='$itemname', `barcode`='$barcode', `gram`='$gram', hsncode='$hsncode',
                                               unit='$unit', batch='$batch', qty='$qty', remark='$remark', `litemcode`='$litemcode',
                                              `litemname`='$litemname', `lbarcode`='$lbarcode', `lgram`='$lgram', `lhsncode`='$lhsncode',
                                              `lunit`='$lunit', `lbatch`='$lbatch', `lqty`='$lbatch', lremark='$lremark' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }

        mysqli_query($conn, "UNLOCK TABLES")or die(mysqli_error($conn));
        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        } else {
            updmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
    }
}


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

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

$senable = "";
$cenable = "";

/* DELETE */
if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$stkdb` WRITE ")or die(mysqli_error($conn));

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES")or die(mysqli_error($conn));
}

include("includes/linkshead.php");
?>
<script src="MyScript/dismantle.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/dismantle_search.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/search_item.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<!--<script src="MyScript/dismantle_search.js" type="text/javascript"></script>-->
</head>
<body>
    <form action='' method='POST'>
        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form" style="float: text-right;font-weight: bold;font-size: 18px;color:red;" ><i class="fa fa-close"></i></button>
            <div style='width:100%;' id='browdata'>
            </div>
        </div>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php Echo $formname; ?></a></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-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus style="width: 110%">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="voucher" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly>
                                <input type="hidden" name="monno" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>" >
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <select name="godown" class="form-control form godown select2" required>
                                    <option value="<?php echo ($addflag != 1) ? $getgodown['code'] : " "; ?>"><?php echo ($addflag != 1) ? $getgodown['gcode'] : " "; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT * FROM `godown`") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[code]>$trow[gcode]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table-striped table-condensed table-bordered" width="100%"><strong>Add</strong> 
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th style="width: 12%;">Barcode</th>
                                        <th style="width: 8%;">Item code</th> 
                                        <th style="width: 12%;">Item Name</th> 
                                        <th style='width: 8%'>Hsncode</th>
                                        <th style='width: 6%'>Gram</th>
                                        <th style='width: 5%'>Unit</th>
                                        <th style='width: 5%'>Batch</th>
                                        <th style='width: 6%' class="text-right">Qty.</th>
                                        <th style='width: 12%'>Remark</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        Echo "<tr>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='abarcode' class='barcode form-control form' value='$get[barcode]' required>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='aitemcode' required='true' class='itemcode form-control  form' value='$get[itemcode]' readonly tabindex='-1' required>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='aitemname' required='true' class='itemname form-control  form' value='$get[itemname]' tabindex='-1' readonly required>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='ahsncode' required='true' class='hsncode form-control  form' value='$get[hsncode]' tabindex='-1' readonly required>";
                                        Echo "</td>";
                                        Echo "<td><input type='text' name='agram' class='gram form-control  form' value='$get[gram]' tabindex='-1' readonly></td>";
                                        Echo "<td><input type='text' name='aunit' class='unit form-control  form' value='$get[unit]' tabindex='-1' readonly></td>";
                                        Echo "<td><input type='text' name='abatch' class='batch form-control   form' value='$get[batch]' ></td>";
                                        Echo "<td><input type='number' name='aqty' class='qty form-control  text-right form' step='any' value='$get[qty]' ></td>";
                                        Echo "<td><input type='text' name='aremark' class=' form-control   form' value='$get[remark]' ></td>";
                                        Echo "</tr>";
                                    } else {
                                        ?>
                                        <tr>                                    
                                            <td><input type='text' name='abarcode' class='barcode  form-control form' value='' required></td> 
                                            <td>
                                                <input type='text' name='aitemcode' required='true' class='itemcode form-control  form' tabindex='-1' value='' readonly required>
                                            </td>
                                            <td>
                                                <input type='text' name='aitemname' required='true' class='itemname form-control  form' tabindex='-1' value='' readonly required>
                                            </td>
                                            <td>
                                                <input type='text' name='ahsncode' required='true' class='hsncode form-control  form' tabindex='-1' value='' readonly required>
                                            </td>
                                            <td><input type='text' name='agram' class='gram form-control  form' value='' tabindex='-1' readonly></td>
                                            <td><input type='text' name='aunit' class='unit form-control  form' value='' tabindex='-1' readonly></td>
                                            <td><input type='text' name='abatch' class='batch form-control   form'  value='' ></td>
                                            <td><input type='number' name='aqty' class='qty form-control  text-right form' step="any" value='0.00' ></td>
                                            <td><input type='text' name='aremark' class=' form-control   form' value='' ></td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="table-responsive">
                            <table class="table-striped table-condensed table-bordered" width="100%"><strong>Less</strong>
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th style="width: 12%;">Barcode</th>
                                        <th style="width: 8%;">Item code</th> 
                                        <th style="width: 12%;">Item Name</th> 
                                        <th style='width: 8%'>Hsncode</th>
                                        <th style='width: 6%'>Gram</th>
                                        <th style='width: 5%'>Unit</th>
                                        <th style='width: 5%'>Batch</th>
                                        <th style='width: 6%' class="text-right">Qty.</th>
                                        <th style='width: 12%'>Remark</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody2">
                                    <?php
                                    if ($addflag != 1) {
                                        Echo "<tr>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='lbarcode' class='barcode form-control form' value='$get[lbarcode]' required>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='litemcode' required='true' class='itemcode form-control  form' value='$get[litemcode]' tabindex='-1' readonly required>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='litemname' required='true' class='itemname form-control  form' value='$get[litemname]' tabindex='-1' readonly required>";
                                        Echo "</td>";
                                        Echo "<td>";
                                        Echo "<input type='text' name='lhsncode' required='true' class='hsncode form-control  form' value='$get[lhsncode]' tabindex='-1' readonly required>";
                                        Echo "</td>";
                                        Echo "<td><input type='text' name='lgram' class='gram form-control  form' value='$get[lgram]' tabindex='-1' readonly></td>";
                                        Echo "<td><input type='text' name='lunit' class='unit form-control  form' value='$get[lunit]' tabindex='-1' readonly></td>";
                                        Echo "<td><input type='text' name='lbatch' class='batch form-control   form' value='$get[lbatch]'  ></td>";
                                        Echo "<td><input type='number' name='lqty' class='qty form-control  text-right form' step='any'  value='$get[lqty]' ></td>";
                                        Echo "<td><input type='text' name='lremark' class=' form-control   form' value='$get[lremark]'></td>";
                                        Echo "</tr>";
                                    } else {
                                        ?>
                                        <tr>                                    
                                            <td><input type='text' name='lbarcode' class='barcode  form-control form' value='' required></td> 
                                            <td>
                                                <input type='text' name='litemcode' required='true' class='itemcode form-control  form' tabindex='-1' value='' readonly required>
                                            </td>
                                            <td>
                                                <input type='text' name='litemname' required='true' class='itemname form-control  form' value='' tabindex='-1' readonly required>
                                            </td>
                                            <td>
                                                <input type='text' name='lhsncode' required='true' class='hsncode form-control  form' value='' tabindex='-1' readonly required>
                                            </td>
                                            <td><input type='text' name='lgram' class='gram form-control  form' value='' tabindex='-1' readonly></td>
                                            <td><input type='text' name='lunit' class='unit form-control  form' value='' tabindex='-1' readonly></td>
                                            <td><input type='text' name='lbatch' class='batch form-control   form'  value='' ></td>
                                            <td><input type='number' name='lqty' class='qty form-control  text-right form' step="any" value='0.00' ></td>
                                            <td><input type='text' name='lremark' class=' form-control   form' value='' ></td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade itemModal" 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">
                    <!--<input type="hidden" value="<?php // Echo $fseries       ?>" class="form-control series">-->
                    <input type="hidden" value="<?php Echo $get[vouchno]; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php Echo $para ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-control ifield">
                                <option value="1">Name</option>
                                <option value="2">Barcode</option>
                                <option value="3">Itemcode</option>
                                <option value="4">Gram</option>
                                <option value="5">Unit</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control ioperator">
                                <option value=""></option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" value="" class="form-control isearch">
                        </div>
                        <div class="col-lg-3">
                            <input type="button" class="btn btn-primary iload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 iloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 inoRecord 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 class="mySearchTr">
                                    <th width="20%">Name</th>
                                    <th width="20%">Barcode</th>
                                    <th width="15%">Itemcode</th>
                                    <th width="15%">Gram</th>
                                    <th width="10%">Unit</th>
                                    <th width="10%">Rate</th>
                                </tr>
                            </thead>
                            <tbody class="iTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary iprevious" value="Previous">
                            <input type="button"  class="btn btn-primary inext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <div class="modal fade searchModal bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" style="margin-top: 15%">
            <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">
                    <input type="hidden" value="<?php Echo $fseries ?>" class="form-control series">
                    <input type="hidden" value="<?php Echo $get[vouchno]; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php Echo $para ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>  
                                <option value="2">Date</option>
                                <option value="3">Godown</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">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Godown</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 class="mySearchTr">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Godown</th>
                                    <th>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 sprevious" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
</html>