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

$keyfld = "vouchno";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);


$formname = 'Discount Received';
$maindb = 'discrecd';
$source = 'DR';
$purcode = '000001';
$keycond = "";
$mtype = "P";
$file = 'discrecd.php';

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $code = $_POST["code"];
    $party = $_POST["party"];
    $narr1 = $_POST["narr1"];
    $narr2 = $_POST["narr2"];
    $amount = $_POST["amount"];

    $mvouch = $qvouch;


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

//        mysqli_query($conn, "DELETE FROM `billset` WHERE `bnkvouch`='$mvouch' and `source` = '$source'") or die(mysqli_error($conn));
//        for ($i = 0; $i <= $counter; $i++) {
//            $msetoff = $_POST["setoff_" . $i];
//
//            if (!empty($msetoff)) {
////      repl &tndbf->yearno    with ttt->yearno
////      repl &tndbf->type      with mtype
////      repl &tndbf->vouchno   with ttt->vouchno
////      repl &tndbf->billno    with ttt->billno
////      repl &tndbf->setoff    with ttt->setoff
////      repl &tndbf->party     with thisform.txtaccode.value
////      repl &tndbf->source    with thisform.source
////      repl &tndbf->bnkvouch  with thisform.txtvouch.value
//        
//                mysqli_query($conn, "INSERT INTO `billset`(`yearno`, `type`, `vouchno`, `billno`, `setoff`, `party`,`source`, bnkvouch)
//                VALUES ('$myyear', '$mtype', '$mvouch', '$mvouch', '$setoff',  '$party', '$source', '$mvouch')") or die(mysqli_error($conn));
//            }

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

        ########### ACTRAN########## 

        $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', '1', '$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);

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

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


    $dsource = 'DR';

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

    $delcond1 = " source = '$dsource' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "actran", "$delcond1", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/discount.js" type="text/javascript"></script>
<script src="MyScript/school.js" type="text/javascript"></script>
<script src="MyScript/discrecd_search.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-4 col-xs-7  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" class="form-control text-size form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" readonly>
                            </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-4 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'); ?>" 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" >Supplier</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2 school"  name="party" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $gsupp["accode"] : ""; ?>"><?php Echo ($addflag != 1) ? $gsupp["name"] : ""; ?></option>
                                    <?php
                                    $gsupp = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode`='009' order by name")or die(mysqli_error($conn));
                                    while ($fsupp = mysqli_fetch_array($gsupp)) {
                                        Echo "<option value='$fsupp[accode]'>$fsupp[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-3 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right 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-8 col-md-8 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>
    </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">Code</option>
                                <option value="3">School</option>
                                <option value="4">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">Code</option>
                                <option value="3">School</option>
                                <option value="4">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>
                                <tr style="background-color:#6082BF;">
                                    <th width="10%">Voucher No.</th>
                                    <th width="20%">Code</th>
                                    <th width="40%">School</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>
