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

$formname       = 'Damage';

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

$printfile      = "damprint.php";
$file           = 'damage.php';
$trandbexist    = 1;
$keyfld         = "vouchno";
$source         = "DM";

$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;
    $invno      = $_POST["invno"];
    $vouchdt    = $_POST["vouchdt"];
    $godown     = $_POST["godown"];
    $subtotal   = $_POST["subtotal"];
    $rem1       = $_POST["rem1"];
    $rem2       = $_POST["rem2"];

    $count      = $_POST["items"];
    
    $mvouch = $qvouch;

    $salecode = 11;
    
    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND vouchno != '$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 `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));
        $group      = $source . $mvouch;

        $totqty = 0;
        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno      = $_POST["srno_$i"];
            $mitem      = $_POST["item_$i"];
            $mbatch     = $_POST["batch_$i"];
            $mqty       = $_POST["qty_$i"];
            $mrate      = $_POST["rate_$i"];
            $mamount    = $_POST["amount_$i"];
            $mactbatch   = $source . $mvouch . $mbatch;

            $mstqty = $mqty * -1;

            $totqty = $totqty + $mqty ;

            if (!empty($mitem)) {
                $srl = $srl + 1;
                
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`,`vouchdt`, `srno`, `itemcode`,`batch`, `qty`, `rate`, `amount`)
                                    VALUES('$mvouch', '$vouchdt', '$srl', '$mitem', '$mbatch', '$mqty', '$mrate', '$mamount')") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `$stkdb`(`yearno`, `group`, `source`, `vouchno`,`vouchdt`,
                            `srno`, `itemcode`, `batch`, `qty`, `godown`, `actbatch`,`ord`)
                     VALUES('$yearno','$group','$source', '$qvouch','$vouchdt', '$srl', '$mitem', '$mbatch', '$mstkqty', 
                            '$godown', '$mactbatch','$ord')") or die(mysqli_error($conn));
               
            }
        }
        
        ############################## MAIN ######################################################
        
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `godown`, `subtotal`, `rem1`, `rem2`, `totqty` )
                                       VALUES($qvouch, '$vouchdt', '$godown', '$subtotal', '$rem1', '$rem2', '$totqty' )") 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 `vouchno`='$mvouch',`vouchdt`='$vouchdt', `rem1`='$rem1', `rem2`='$rem2', 
                        `godown`='$godown',`subtotal`='$subtotal', `totqty`='$totqty'  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);

}

$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");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/purchase.js" 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-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:#1ABC9C"><?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-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-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </div>
                            <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-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>">
                            </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-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" name="godown">
                                    <option value="<?php Echo ($addflag != 1) ? $get["godown"] : ""; ?>"><?php Echo ($addflag != 1) ? $get["godown"] : ""; ?></option>
                                    <?php
                                    $ggodown = mysqli_query($conn, "SELECT gdcode FROM `godown`")or die(mysqli_error($conn));
                                    while ($godown = mysqli_fetch_array($ggodown)) {
                                        Echo "<option value='$godown[gdcode]'>$godown[gdcode]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <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">Remark</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="rem1" value="<?php Echo ($addflag != 1) ? $get["rem1"] : ""; ?>">
                                <input type="text" class="form-control form" name="rem2" value="<?php Echo ($addflag != 1) ? $get["rem2"] : ""; ?>">
                            </div>
                        </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-heading">
<!--                        <h3 class="panel-title">Item Details</h3>-->
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered" width="100%">
                                <thead  style="background: #1ABC9C;">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="4%" class='text-right'>Sr.</th>
                                        <th width="30%">Item</th>
                                        <th width="10%">Batch</th>
                                        <th width="10%" class='text-right'>Qty.</th>
                                        <th width="10%" class='text-right'>Rate</th>
                                        <th width="15%" class='text-right'>Amount</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 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)) {
                                            $finditem   = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $fitem      = mysqli_fetch_array($finditem);
                                            
                                            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='$tran[srno]'></td>";
                                            
                                            Echo "<td>";
                                            Echo "<select class='form-control form item select2' name='item_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn, 'SELECT * FROM `item`')or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[code]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            
                                            Echo "<td><input type='text' class='form-control form batch'     name='batch_$ctr'       value='$tran[batch]'></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 text-right rate'      name='rate_$ctr'        value='$tran[rate]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right amount'    name='amount_$ctr'      value='$tran[amount]'></td>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_0" value="1"></td>
                                            <td>
                                                <select class="form-control form item select2" name="item_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn, "SELECT * FROM `item`")or die(mysqli_error($conn));
                                                    while ($item = mysqli_fetch_array($gitem)) {
                                                        Echo "<option value='$item[code]'>$item[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form batch"   name="batch_0"      value="0"></td>
                                            <td><input type="text" class="form-control form text-right qty"     name="qty_0"        value="0"></td>
                                            <td><input type="text" class="form-control form text-right rate"    name="rate_0"       value="0"></td>
                                            <td><input type="text" class="form-control form text-right amount"  name="amount_0"     value="0"></td>
                                        </tr>  
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 pad">
<!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <button type="button" class="btn btn-success btn-square form"  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 : 1; ?>" />  
                        </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-heading">
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row text-right pad">
                            <div class="col-lg-4 col-xs-4 text-right pad"></div>
                            <div class="col-lg-2 col-xs-2 text-right pad"></div>
                            <div class="col-lg-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Total</label>
                            </div>
                            <div class="col-lg-2">
                                <input type="text" class="form-control form subtotal text-right" value="<? Echo ($addflag != 1) ? $get[subtotal] : ""; ?>" name="subtotal" readonly>
                            </div>
                        </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 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 id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #1ABC9C;">
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Godown</th>
                                    <th>Amount</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                 {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='right'>$row[vouchno]</td>";
                                        echo "<td align='right'>$row[vouchdt]</td>";
                                        echo "<td align='left'>$row[godown]</td>";
                                        echo "<td align='right'>$row[subtotal]</td>";
                                        echo "<td align='center'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><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>
</html>
