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

$formname = 'Discount Given';
$maindb = 'discgive';
$printfile = "discgive_print.php";
$file = 'discgive.php';
$source = "DG";
$grid = '';
$keyfld = "vouchno";
$focusfield = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
mysqli_select_db($conn,$zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

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

    $a = 1;
    $vouchno = $_POST["vouchno"];
    $code = $_POST["code"];
    $vouchdt = $_POST["vouchdt"];
    //$acno = $_POST["acno"];
    $name = $_POST["name"];
    $amount = $_POST["amount"];
    $narr1 = $_POST["narr1"];
    $narr2 = $_POST["narr2"];
    $amtinwords = $_POST["amtinwords"];

    $mvouch = $qvouch;
    if ($a == 1) {


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

        ########### ACTRAN########## 
        $grpcode = '012';
        $damount = $amount * -1;
        $pamount = $amount;
        ### FOR  Discount #####


        mysqli_query($conn,"INSERT INTO `actran`(`vouchno`, `date`,`trnno`, `accode`, `grpcode`, `amount`,`source`)
                VALUES ('$mvouch', '$vouchdt', '001', '14', '$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', '$name', '010',  '$pamount', '$source')") or die(mysqli_error($conn));


        if ($addflag == 1) {
            mysqli_query($conn,"INSERT INTO `$maindb`(`vouchno`,`vouchdt`, `name`, `amount`,`narr1`,`narr2`, `amtwords`)
                                       VALUES('$qvouch','$vouchdt','$name', '$amount',  '$narr1', '$narr2', '$amtinwords')") or die(mysqli_error($conn));
            insmess();

            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");

//            header("refresh: 1; $file?mode=true");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn,"UPDATE `$maindb` SET `vouchdt`='$vouchdt', `name`='$name',  `amount`='$amount',`narr1`='$narr1',
                        `narr2`='$narr2', `amtwords`='$amtinwords' 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);
//
    $qrystaff = mysqli_query($conn,"SELECT `name`, `accode` FROM `accmast` where accode = '$get[name]'")or die(mysqli_error($conn));
    $fecstaff = mysqli_fetch_array($qrystaff);
}



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

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

include("includes/linkshead.php");
?>
<script src="MyScript/discgive_search.js" type="text/javascript"></script>
<script src="MyScript/discgive.js" type="text/javascript"></script>

</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 col-sm-12 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <!--<i class="fa fa-times"></i>-->
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-sm-4  text-right pad">
                                <label class="label-control marg">Voucher No</label>
                            </div>
                            <div class="col-lg-2 col-sm-8  pad">
                                <input type="text" class="form-control form " readonly="" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" required>
                            </div>
                            <div class="col-lg-3 col-sm-4  text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-sm-8  pad">
                                <input type="date" class="form-control form focusfield" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-sm-4 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-sm-8 pad">
                                <select class="form-control form select2" name="name" id="name" required autofocus>
                                    <option value="<?php Echo ($addflag != 1) ? $fecstaff["accode"] : ""; ?>"><?php Echo ($addflag != 1) ? $fecstaff["name"] : ""; ?> </option>

                                    <?php
//                                    echo "<option value=''> </option>";
                                    $getstaff = mysqli_query($conn,"SELECT `name`, `accode` FROM `accmast` where `grpcode` = '010' order by name")or die(mysqli_error($conn));
                                    while ($showstaff = mysqli_fetch_array($getstaff)) {
                                        echo "<option value='$showstaff[accode]'>$showstaff[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-sm-4  text-right pad">
                                <label class="label-control marg">Amount</label> <br>

                            </div>
                            <div class="col-lg-2 col-sm-8  pad">
                                <input type="text" class="form-control form amount text-right" id="amount" name="amount" value="<?php Echo ($addflag != 1) ? $get["amount"] : 0; ?>" required>

                            </div>

                            <div class="col-lg-6 col-sm-8  pad">

                                <textarea name="amtinwords"  class="form-control form amtinwords" readonly><?php echo ($addflag != 1) ? $get['amtwords'] : ''; ?></textarea>
                                <!--<input type="text" class="form-control form input-lg rsword"  style="min-height: 71px" name="rsword" value="<?php // echo ($addflag != 1) ? rsword(round($get['amount'])) : '';        ?>" >-->
                                <!--                                <?php
//                                $amtwords = rsword(round($get['amount']));
                                ?>
                                                                <textarea class="form-control form input-lg rsword"  style="min-height: 71px" name="rsword" >
                                <?php
//                                    echo $amtwords;
                                ?>
                                                                </textarea>-->

                            </div>
                        </div>

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

                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
<!--                        <script>
                            $("#hprint").hide();
                        </script>-->
                    </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 <?= $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 No.</option>
                                <option value="2">Voucher Date</option>
                                <option value="3">Name</option>
                                <option value="4">Amount</option>
                                <option value="5">Narration1</option>
                                <option value="6">Narration2</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 No.</option>
                                <option value="2">Voucher Date</option>
                                <option value="3">Name</option>
                                <option value="4">Amount</option>
                                <option value="5">Narration1</option>
                                <option value="6">Narration2</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 table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr" style="background-color: #64CCFC">
                                    <th style="width:10%">Voucher No.</th>
                                    <th style="width:10%">Voucher Date</th>
                                    <th style="width:30%">Name</th>
                                    <th style="width:10%">Amount</th>
                                    <th style="width:15%">Narration1</th>
                                    <th style="width:15%">Narration2</th>

                                    <th style="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>
