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

$formname = 'Discount Given';
$maindb = 'discgive';
$source = 'DG';
$purcode = '000009';
$keycond = "";
$mtype = "S";
$printfile = "discgvn_print.php";
$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
$usertype = $_SESSION["usertype_" . $dbname];
$username = $_SESSION["username_" . $dbname];
$zroot = $_SESSION["zdir"];
$zroot1 = substr($zroot, 0, -4);
$myyear = right($zdb, 4);
$zyear = right($zdb, 4);

$currcono = $compcd;
$curryear = $myyear;
$zStartDate = getzdates($zyear, 'S', $comp);
$zEndDate = getzdates($zyear, 'E', $comp);

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $qvouch = $_POST["vouchno"];
    $code = $_POST["code"];
    $party = $_POST["party"];
    $narr1 = mysqli_real_escape_string($conn, $_POST["narr1"]);
    $narr2 = mysqli_real_escape_string($conn, $_POST["narr2"]);
    $amount = $_POST["amount"];
    $billflag = $_POST["billflag"];
    $counter = $_POST["counter"];

    $mvouch = $qvouch;

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `actran` WRITE, `billsett` WRITE") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` 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;
        }

        if ($billflag == 1) {
            mysqli_query($conn, "DELETE FROM `billsett` WHERE `bnkvouch`='$mvouch' and `source` = '$source'") or die(mysqli_error($conn));
            for ($i = 0; $i <= $counter; $i++) {

                $myearno = $_POST["yearno_" . $i];
                $mvouchno = $_POST["vouchno_" . $i];
                $mbillno = $_POST["billno_" . $i];
                $msetoff = $_POST["setoff_" . $i];
                $mdays = $_POST["days_" . $i];
                $mbillsrc = $_POST["billsrc_" . $i];
                $mbilldate = $_POST["billdate_" . $i];
                //                browse($mvouchno);
                //                browse($mbillno);
                if (!empty($msetoff) || ($msetoff != 0)) {

                    mysqli_query($conn, "INSERT INTO `billsett`(`yearno`, `type`, `vouchno`, `billno`, `setoff`, `party`,`source`, bnkvouch,billsrc,bnkdate, billdate)
                                                        VALUES ('$myearno', '$mtype', '$mvouchno', '$mbillno', '$msetoff',  '$party', '$source', '$mvouch', '$mbillsrc', '$vouchdt', '$mbilldate')") or die(mysqli_error($conn));
                }
            }
        }

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

        ########### ACTRAN########## 
        //        if ($para == 'G') {
        $grpcode = '012';
        $damount = $amount * -1;
        $pamount = $amount;
        //        } else {
        //            $grpcode = '011';
        //            $damount = $amount;
        //            $pamount = $amount * -1;
        //        }
        ### FOR  Discount #####


        mysqli_query($conn, "INSERT INTO `actran`(`vouchno`, `date`,`trnno`, `accode`, `grpcode`, `amount`,`source`)
                VALUES ('$mvouch', '$vouchdt',  '001', '13', '$grpcode',  '$damount', '$source')") or die(mysqli_error($conn));

        ### for party #####


        mysqli_query($conn, "INSERT INTO `actran`(`vouchno`, `date`,`trnno`, `accode`, `grpcode`, `amount`,`source`)
                VALUES ('$mvouch', '$vouchdt',  '002', '$party', '010',  '$pamount', '$source')") or die(mysqli_error($conn));



        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`yearno`,`vouchno`, `vouchdt`,`accode`, `narr1`,`narr2`,`amount`)
                                     VALUES ('$myyear','$qvouch', '$vouchdt', '$party','$narr1','$narr2','$amount')") 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 `yearno`= '$myyear', `vouchdt`='$vouchdt', `accode`='$party',
                         `narr1`='$narr1', `narr2`='$narr2', `amount`='$amount' WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

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

    $fcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode='$get[accode]'") or die(mysqli_error($conn));
    $gcust = mysqli_fetch_array($fcust);
}
$senable = "";
$cenable = "";
$printdisabled = "none";


if (isset($_GET["delete"])) {

    //    if ($para == 'G') {
    $dsource = 'DG';
    //    } else {
    //        $dsource = 'DR';
    //    }

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

    $delcond1 = " source = '$dsource' and vouchno = '$_GET[vouchno]' ";

    $delcond2 = " source = '$dsource' and bnkvouch = '$_GET[vouchno]' ";

    delnew($zdb, "actran", "$delcond1", "$file?mode=last");
    delnew($zdb, "billsett", "$delcond2", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/discount.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/discgvn_search.js?v=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<style>
    .colr {
        border: 1px solid red;
    }

    input[type=number]::-webkit-inner-spin-button,
    input[type=number]::-webkit-outer-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }
</style>
</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-6 col-md-6 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-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" id="vouchno" class="form-control text-size form vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" readonly>
                                <input type="hidden" class="form-control form billflag" name="billflag" value="0">
                                <input type="hidden" class="form-control form pvar" id="pvar" value="R">
                                <input type="hidden" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                                <input type="hidden" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                                <input type="hidden" class="form-control form pvar" id="source" value="<?= $source ?>">
                                <input type="hidden" class="form-control form pvar" id="dbfname" value="<?= $maindb ?>">
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control vouchdt form" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" autofocus>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Customer</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 party" id="party" name="party" style="width: 100%">
                                    <option value="<?php echo ($addflag != 1) ? $gcust["accode"] : ""; ?>"><?php echo ($addflag != 1) ? $gcust["name"] : ""; ?></option>
                                    <?php
                                    $getcust = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode`='010' or grpcode ='006' order by name") or die(mysqli_error($conn));
                                    while ($fschl = mysqli_fetch_array($getcust)) {
                                        echo "<option value='$fschl[accode]'>$fschl[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form amount text-right" id="amount" name="amount" value="<?php echo ($addflag != 1) ? $get["amount"] : '' ?>">
                            </div>
                        </div>

                        <hr>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Narration</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="narr1" value="<?php echo ($addflag != 1) ? $get["narr1"] : ""; ?>">
                                <input type="text" class="form-control form " name="narr2" value="<?php echo ($addflag != 1) ? $get["narr2"] : ""; ?>">
                            </div>
                        </div>
                    </div>

                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade " tabindex="" id="settmodal" 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">Bill Settlement</h4>
                    </div>
                    <div class="modal-body">
                        <div class=" table-responsive">
                            <div class="toolbar">
                                <div class="row">
                                    <div class="col-lg-2"><label class="label-control marg" style="color:red">Total Remain</label></div>
                                    <div class="col-lg-2"><input type="text" id="remain" name="remain" class="form-control" readonly style=""></div>
                                    <div class="col-lg-2"><label class="label-control marg" style="color:blue">Total Setoff</label></div>
                                    <div class="col-lg-2"><input type="text" id="totsetoff" name="totsetoff" class="form-control" readonly style=""></div>
                                </div>
                            </div>
                            </header>
                            <div id="div-2" class="accordion-body collapse in body">
                                <div style="max-height:300px;overflow: auto; border: 0px solid;">
                                    <div id="data"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer"></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">Voucher No.</option>
                                <option value="2">Customer</option>
                                <option value="3">Amount</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="1">Voucher No.</option>
                                <option value="2">Customer</option>
                                <option value="3">Amount</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 class="mySearchTr">
                                <tr>
                                    <th width="10%">Voucher No.</th>
                                    <th width="20%">Customer</th>
                                    <th width="20%">Amount</th>
                                    <th width="10%">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>