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

$formname = "Payment To Agent";
$maindb = "agentin";
$accdb = "actran";

$file = "agentin.php";
$grid = "";
$source = "TOAG";
$keyfld = "vouchno";
$trandbexist = 0;
$printfile = "agentin_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$fseries = $_SESSION["series"];
$keycond = "";

$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

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

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

    $a = 1;

    $vouchdt = $_POST["vouchdt"];
    $agent = $_POST["agent"];
    $inr = $_POST["inr"];
    $roe = $_POST["roe"];
    $dollar = $_POST["dollar"];
    $rmb = $_POST["rmb"];
    $rmbamt = $_POST["rmbamt"];
    $amount = $_POST["amount"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $dollarrecd = $_POST["dollarrecd"];
    $conchgs = $_POST["conchgs"];
    $inrtormb = $_POST["inrtormb"];
    $bankname = $_POST["bankname"];

    $mvouch = $_POST["vouchno"];

    if ($bankname == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?vouchno=$_GET[vouchno]");
        }
    }

    if ($a == 1) {

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


        ######################## ACCOUNTS PART ######################################

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

        // if ($inr > 0) {
        $minus_amt = ($inr * -1);
        $minus_rmbamt = ($rmbamt * -1);

        $getbankinit = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        ;
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];

        // cash part inr
        mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`,
                                                                    `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
                                                            VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit',
                                                                    '$bankname', '002', '$inr', '$rmbamt', '$inrtormb', 'I')") or die(mysqli_error($conn));

        // party part inr 
        mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`, 
                                                                    `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
                                                            VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit',
                                                                    '$agent', '044', '$minus_amt', '$minus_rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));

        // cash part rmb
        // mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`,
        //                                                         `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
        //                                              VALUES ('$vouchdt', '$mvouch', '$source', '001', '$binit',
        //                                                      '$bankname', '002', '$inr', '$rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));

        // // party part rmb
        // mysqli_query($conn, "INSERT INTO `$accdb`(`date`, `vouchno`, `source`, `trnno`, `bankinit`, 
        //                                                             `accode`, `grpcode`, `amount`, `feamt`, `roe`, `type`)
        //                                                     VALUES ('$vouchdt', '$mvouch', '$source', '002', '$binit',
        //                                                             '$agent', '044', '$minus_amt', '$minus_rmbamt', '$inrtormb', 'R')") or die(mysqli_error($conn));
        // }

        ######################## ACCOUNTS PART ENDED ######################################

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `agent`, `party`, `inr`, `roe`, `dollar`,
                                                                     `rmb`, `rmbamt`, `remark1`, `remark2`, `dollarrecd`, 
                                                                     `conchgs`, `inrtormb`, `bankname`)
                                                             VALUES ('$mvouch', '$vouchdt', '$agent', '$agent', '$inr', '$roe', '$dollar',
                                                                     '$rmb', '$rmbamt', '$remark1', '$remark2', '$dollarrecd',
                                                                     '$conchgs', '$inrtormb', '$bankname')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', agent='$agent', party='$agent', `inr`='$inr',  `roe`='$roe' , `dollar`='$dollar',
                                                                     `rmb`='$rmb', `rmbamt`='$rmbamt', `remark1`='$remark1', `remark2`='$remark2',
                                                                     `dollarrecd`='$dollarrecd', `conchgs`='$conchgs', `inrtormb`='$inrtormb', 
                                                                     `bankname`='$bankname' 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' $keycond") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getbank = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = '$get[bankname]'") or die(mysqli_error($conn));
    $banknm = mysqli_fetch_array($getbank);

    $getag = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode` = '$get[agent]' ") or die(mysqli_error($conn));
    $fetag = mysqli_fetch_array($getag);

}

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

/* DELETE */
if (isset($_GET["delete"])) {

    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `accmast` WRITE, `$accdb` WRITE ") or die(mysqli_error($conn));

    $delcond2 = "`vouchno` = '$_GET[vouchno]'";
    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last");

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]'";
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES");

}

include("includes/linkshead.php");
?>
<script src="MyScript/agentin.js?a=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/agentin_search.js?b=<?= mt_rand() ?>" type="text/javascript"></script>
<script src="MyScript/shortkeys.js?c=<?= mt_rand() ?>" type="text/javascript"></script>
</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                        <input type="hidden" value="<?= $zStartDate ?>" class="zstdate">
                        <input type="hidden" value="<?= $zEndDate ?>" class="zenddate">
                    </div>

                    <div class="panel-body">

                        <div class="row">
                            <div class="col-lg-4 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-2 col-xs-2 pad">
                                <input type="text" class="form-control form vouchno" name="vouchno" id="vouchno"
                                    value="<?php echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required
                                    readonly>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt"
                                    id="vouchdt"
                                    value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required
                                    autofocus>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Cash / Bank - From</label>
                            </div>
                            <div class="col-lg-6 col-md-7 col-sm-8 col-xs-8 pad">
                                <select class="form-control form select2" name="bankname" id="bankname"
                                    style='width:100%' required>
                                    <option value="<?php echo ($addflag != 1) ? $banknm['accode'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $banknm['name'] : ""; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` WHERE `grpcode` = '002' order by `name` ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[accode]>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Agent - To</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-7 col-xs-7 pad">
                                <select class="form-control form select2" id="agent" name="agent" id="agent"
                                    style="width:100%" required>
                                    <option value="<?php echo ($addflag != 1) ? $fetag['accode'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $fetag['name'] : ""; ?></option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE `grpcode` = '044' order by `name` ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value='$trow[accode]'>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">INR</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="inr" id="inr"
                                    value="<?php echo ($addflag != 1) ? $get['inr'] : 0; ?>"
                                    class="form-control form inr text-right">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Dollar </label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="dollar"
                                    value="<?php echo ($addflag != 1) ? $get['dollar'] : 0; ?>"
                                    class="form-control form dollar text-right" style="color:red;font:bolder">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Conversion INR To Dollar</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="roe" id="roe"
                                    value="<?php echo ($addflag != 1) ? $get['roe'] : 0; ?>" style="color:blue;"
                                    class="form-control form roe text-right" tabindex="-1" readonly>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Dollar Received </label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="dollarrecd"
                                    value="<?php echo ($addflag != 1) ? $get['dollarrecd'] : 0; ?>"
                                    class="form-control form dollarrecd text-right" style="font:bolder">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Conversion Charges (USD)</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="conchgs"
                                    value="<?php echo ($addflag != 1) ? $get['conchgs'] : 0; ?>"
                                    class="form-control form conchgs text-right" style="font:bolder" tabindex="-1"
                                    readonly>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Conversion Dollar To RMB</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="rmb" value="<?php echo ($addflag != 1) ? $get['rmb'] : 0; ?>"
                                    class="form-control form rmb text-right">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">RMB Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="rmbamt"
                                    value="<?php echo ($addflag != 1) ? $get['rmbamt'] : 0; ?>"
                                    class="form-control form rmbamt text-right" style="color:red;font:bolder">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">INR To RMB</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="inrtormb"
                                    value="<?php echo ($addflag != 1) ? $get['inrtormb'] : 0; ?>"
                                    class="form-control form inrtormb text-right" style="color:#99582a;font:bolder"
                                    tabindex="-1" readonly>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Remark</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-7 col-xs-7 pad">
                                <input type="text" name="remark1"
                                    value="<?php echo ($addflag != 1) ? $get['remark1'] : ''; ?>"
                                    class="form-control form remark1">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" name="remark2"
                                    value="<?php echo ($addflag != 1) ? $get['remark2'] : ''; ?>"
                                    class="form-control form remark2">
                            </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">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <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">Agent</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">Agent</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 class="mySearchTr">
                                    <th width="15%">Voucher</th>
                                    <th width="25%">Date</th>
                                    <th width="25%">Agent</th>
                                    <th width="14%">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>