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

$formname = 'Damaged';
$maindb = 'dammain';
$trandb = 'damtran';
$stkdb = 'transacn';


$printfile = "damaged_print.php";
$file = 'damaged.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "DM";

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


$zroot = $_SESSION["zdir"];

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $godown = $_POST["godown"];
    $remark = $_POST["remark"];
    $remark2 = $_POST["remark2"];

    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;



    if ($a == 1) {

#################################TRAN PART #################################



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

        $srl = 0;
        $batch = date("Y-m-d") . $mvouch;
        for ($i = 0; $i <= $count; $i++) {
            $srno = $_POST["srno_$i"];
            $barcode = $_POST["barcode_$i"];
            $prodtype = $_POST["prodtype_$i"];
            $brand = $_POST["brand_$i"];
            $hsncode = $_POST["hsncode_$i"];
            $size = $_POST["size_$i"];
            $prodname = $_POST["prodname_$i"];
            $quality = $_POST["quality_$i"];
            $qty = $_POST["qty_$i"];
            $stkqty = $_POST["stqty_$i"];
            $unit = $_POST["unit_$i"];
            $group = $source . $barcode;


            $mqty = $qty * -1;

            $group = $source . $mvouch;

            if (!empty($prodname)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`source`, `yearno`, `vouchno`, `vouchdt`, `srno`, `prodcode`, `prodtype`, 
                                                           `brand`, `size`, `prodname`, `quality`, `qty`, `hsncode`, `unit`,
                                                           `group`, `stockqty`, `batch`)
                                                    VALUES('$source', '$myyear', '$mvouch', '$vouchdt', '$srl', '$barcode', '$prodtype',
                                                           '$brand', '$size', '$prodname', '$quality', '$qty', '$hsncode', '$unit',
                                                           '$group', '$stkqty', '$batch')") or die(mysqli_error($conn));

                ############Stock Part#########
                mysqli_query($conn, "INSERT INTO `$stkdb`(`source`, `yearno`, `vouchno`, `vouchdt`, `srno`, `prodcode`, `prodtype`, 
                                                           `brand`, `size`, `prodname`, `quality`, `qty`, godown, hsncode, unit,
                                                           `group`, `stockqty`, `batch`)
                                                    VALUES('$source', '$myyear', '$mvouch', '$vouchdt', '$srl', '$barcode', '$prodtype',
                                                           '$brand', '$size', '$prodname', '$quality', '$mqty', '$godown', '$hsncode', '$unit',
                                                           '$group', '$stkqty', '$batch')") or die(mysqli_error($conn));

                $totqty = $totqty + $qty;
            }
        }

#################################TRAN PART END #################################
############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `godown`, totqty,remark,remark2)
                                               VALUES ($qvouch, '$vouchdt',  '$godown', '$totqty','$remark','$remark2')") 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 `vouchdt`='$vouchdt', `godown`='$godown', totqty='$totqty',remark='$remark',remark2='$remark2' WHERE `vouchno`= '$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);

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

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

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

include("includes/linkshead.php");
?>
<script src="MyScript/search_item.js?a=2" type="text/javascript"></script>
<script src="MyScript/damaged.js?a=2" type="text/javascript"></script>
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-1 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-9 col-xs-2 pad">
                                <input type="text" class="form-control text-size form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-2 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-9 col-xs-2 pad">
                                <input type="date" class="form-control form focusfield"  name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus> 
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-2 text-right pad">
                                <label class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-9 col-xs-2 pad">
                                <select class="form-control form  select2 godown" name="godown" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["godown"] : ""; ?>"><?php Echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT `code`,`name` FROM `godown` WHERE `code`!='' ORDER BY NAME")or die(mysqli_error($conn));
                                    Echo "<option value=''></option>";
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        Echo "<option value='$party[code]'>$party[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead class="mySearchTr" >
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="5%">#</th>
                                        <th width="10%">Barcode</th>
                                        <th width="10%">Brand</th>
                                        <th width="10%">Size</th>
                                        <th width="10%">Name</th>
                                        <th width="10%">Quality</th>
                                        <th width="7%">Hsncode</th>
                                        <th width="8%"  class='text-right'>Stock</th>
                                        <th width="8%"  class='text-right'>Qty.</th>
                                        <th width="8%">Unit</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $mctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $getitem1 = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[prodcode]' ")or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($getitem1);
                                            $mctr++;
                                            Echo "<tr>";
                                            Echo "<td><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='$mctr'></td>";
                                            Echo "<td><input type='text' class='form-control form prodtype'         name='prodtype_$ctr' value='$tran[prodtype]' readonly tabindex='-1'></td>";
                                            Echo "<td><input type='text' class='form-control form barcode'          name='barcode_$ctr'  value='$tran[prodcode]'></td>";
                                            Echo "<td><input type='text' class='form-control form brand'            name='brand_$ctr'    value='$fitem[brand]' readonly tabindex='-1'></td>";
                                            Echo "<td><input type='text' class='form-control form size'             name='size_$ctr'     value='$fitem[size]' readonly tabindex='-1'></td>";
                                            Echo "<td><input type='text' class='form-control form prodname'         name='prodname_$ctr' value='$fitem[prodname]' readonly tabindex='-1'></td>";
                                            Echo "<td><input type='text' class='form-control form quality'          name='quality_$ctr'  value='$fitem[quality]' readonly tabindex='-1'></td>";
                                            Echo "<td><input type='text' class='form-control form hsncode'          name='hsncode_$ctr'  value='$tran[hsncode]' readonly tabindex='-1'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right stqty' name='stqty_$ctr'    value='$tran[stockqty]' readonly tabindex='-1'  style='background-color:yellow;color:blue'></td>";

                                            Echo "<td><input type='text' class='form-control form text-right qty'   name='qty_$ctr'      value='$tran[qty]'></td>";
                                            Echo "<td><input type='text' class='form-control form unit'             name='unit_$ctr'     value='$fitem[unit]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        Echo "<tr>";
                                        Echo "<td><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_$pctr'      value='1'></td>";
                                        Echo "<td><input type='text' class='form-control form prodtype'         name='prodtype_$pctr'  value='' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='text' class='form-control form barcode'          name='barcode_$pctr'      value=''></td>";
                                        Echo "<td><input type='text' class='form-control form brand'            name='brand_$pctr'     value='' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='text' class='form-control form size'             name='size_$pctr'      value='' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='text' class='form-control form prodname'         name='prodname_$pctr'  value='' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='text' class='form-control form quality'          name='quality_$pctr'   value='' readonly tabindex='-1'></td>";
                                        Echo "<td><input type='text' class='form-control form hsncode'          name='hsncode_$pctr'   value='' readonly ></td>";
                                        Echo "<td><input type='text' class='form-control form text-right stqty' name='stqty_$pctr'    value='0' readonly tabindex='-1'  style='background-color:yellow;color:blue'></td>";

                                        Echo "<td><input type='text' class='form-control form text-right qty'   name='qty_$pctr'       value='0' ></td>";
                                        Echo "<td><input type='text' class='form-control form unit'             name='unit_$pctr'      value=''></td>";
                                        Echo "</tr>";
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>

                        </div>

                        <div class="row">
                            <div class="col-lg-1" >
                                <button type="button" class="btn btn-square form mySearchTr"  id="addnew">
                                    <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row' ></i>    
                                </button>
                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />  
                            </div> 
                            <div class="col-lg-10 col-md-1 col-sm-3 col-xs-3 text-right pad" style="" >
                                <label class="label-control text-size marg">Total Items &nbsp;</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad" style="">
                                <input type="text" class="form-control form text-right totqty" name="totqty" value="<?php Echo ($addflag != 1) ? $get["totqty"] : "0"; ?>" readonly>&nbsp;
                            </div>
                            <br>
                            <br>
                        </div>
                        <div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Remark</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="remark" value="<?php Echo ($addflag != 1) ? $get["remark"] : ""; ?>" >
                                <input type="text" class="form-control form " name="remark2" value="<?php Echo ($addflag != 1) ? $get["remark2"] : ""; ?>" >
                            </div>
                        </div> 

                        <div class="row">

                        </div>

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

        <div class="col-lg-8 col-md-8 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">Product Name</option>
                            <option value="2">Size</option>
                            <option value="3">Brand</option>
                            <option value="4">Quality</option>
                            <option value="5">Code</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 " width="100%">
                        <thead>
                            <tr class="mySearchTr">
                                <th width="20%">Product Name</th>
                                <th width="10%">Size</th>
                                <th width="10%">Brand</th>
                                <th width="10%">Quality</th>
                                <th width="10%">Itemcode</th>
                                <th width="10%">Stk.</th>
                                <th width="10%">Prod Type</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 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">
                <input type="hidden" value="<?php Echo $zseries ?>" class="form-control series">
                <input type="hidden" value="<?php Echo $get['code']; ?>" 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>

                            <option value="6">Remark</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>
                            <option value="6">Remark</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">
                </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-striped table-condensed table-bordered" width="100%">
                        <thead>
                            <tr class="mySearchTr">
                                <th style="width:13%">Voucher</th>
                                <th style="width:13%">Date</th>
                                <th style="width:13%">Godown</th>
                                <th style="width:13%">Remarks</th>
                                <th style="width:5%">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>
</html>
