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

$formname = 'Invoice';
$maindb = 'invmain';
$trandb = 'invtran';
$accdb  = 'actran';
$printfile = "entinvoice_print.php";
$file = 'entinvoice.php';
$trandbexist = 1;
$grid = '';
$keyfld = "vouchno";
$focusfield = "name";
$source = "SL";
$salecode = 30;

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

// $fseries = $_SESSION["series_" . $dbname];
$keycond = "";

$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$usertype = $_SESSION["usertype"];

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

if (isset($_POST["submit"])) {
    $a = 1;
    $qvouch = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $agent = $_POST["agent"];
    $nofpass = $_POST["nofpass"];
    $subtot = $_POST["subtot"];
    $misc = $_POST["misc"];
    $courier = $_POST["courier"];
    $totamt = $_POST["totamt"];
    // $country = strtoupper($_POST["country"]);

    $count = $_POST["items"];

    $mvouch = $qvouch;


    if ($a == 1) {

        mysqli_query($conn, "LOCK TABLE `$maindb`,`$trandb` WRITE");

        ############################## MAIN  ######################################################
        if ($addflag == 1) {

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

            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `agent`, `nofpass`, `totamt`, `misc`, `subtot`, `courier`)
                                                VALUES('$qvouch', '$vouchdt', '$agent', '$nofpass', '$totamt','$misc', '$subtot', '$courier')") 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  `vouchdt`='$vouchdt', `agent`='$agent', `nofpass`='$nofpass', `totamt`='$totamt', `misc`='$misc', `subtot`='$subtot',
            `courier`='$courier'  WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }


        ############################## VISATRAN  ######################################################
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));

        for ($i = 0; $i <= $count; $i++) {
            $cnt++;
            $msrno = $_POST["srno_$i"];
            $passport = $_POST["passport_$i"];
            $paxname = $_POST["paxname_$i"];
            $mofano = $_POST["mofano_$i"];
            $mofano2 = $_POST["mofano2_$i"];
            $submissiondt = $_POST["submissiondt_$i"];
            $memcash = $_POST["memcash_$i"];
            $mofamt = $_POST["mofamt_$i"];
            $stampamt = $_POST["stampamt_$i"];
            $imigamt = $_POST["imigamt_$i"];
            $miscamt = $_POST["miscamt_$i"];

            if (!empty($passport)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`,passport, paxname, mofano, mofano2, `submissiondt`, `memcash`, `vouchno`, `vouchdt`, `mofamt`, `stampamt`, `imigamt`, `miscamt`)
                                                    VALUES('$cnt', '$passport', '$paxname', '$mofano', '$mofano2', '$submissiondt', '$memcash', '$mvouch', '$vouchdt', '$mofamt', '$stampamt', '$imigamt', '$miscamt')") or die(mysqli_error($conn));
            }
        }

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

        if ($totamt > 0) {
            $minus_amt = ($totamt * -1);

            // sale part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                             VALUES ('$vouchdt','$mvouch','$source', '001', '$salecode', '005', '$totamt' )") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                             VALUES ('$vouchdt','$mvouch','$source', '002', '$agent', '010', '$minus_amt' )") or die(mysqli_error($conn));
        }

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


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

    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno` = '$get[vouchno]' ORDER BY `srno`") or die(mysqli_error($conn));

    $rd = "readonly";
}

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

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

    $delcondn = " $keyfld = '$_GET[$keyfld]' ";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' $keycond";

    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcondn", "$file?mode=last");
    delnew($zdb, "$accdb", "$delcond", "$file?mode=last");

}

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

</head>

<body>

    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                    </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-1 col-md-1 col-sm-1 col-xs-1 pad ">
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" tabindex="-1" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 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" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Agent </label>
                            </div>
                            <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                                <select name="agent" class="form-control form select2 agent" style="width: 90%">
                                    <option value="<?php echo ($addflag != 1) ? $get["agent"] : ""; ?>"><?php echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = '040' ORDER BY name") or die(mysqli_error($conn));
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        echo "<option value='$party[accode]'>$party[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="panel panel-default">

                                <div class="panel-body">
                                    <div class='table-responsive' style="overflow-y:scroll; height: 350px">
                                        <table class="table table-striped table-condensed table-bordered" width="80%">
                                            <thead class="mySearchTr">
                                                <tr>
                                                    <th style="width: 2%"></th>
                                                    <th style="width: 4%;text-align: center;">Sr.</th>
                                                    <th style="width: 10%;">Passport</th>
                                                    <th style="width: 20%;">Pax Name</th>
                                                    <th style="width: 10%;text-align: right;">Stamp Charges</th>
                                                    <th style="width: 10%;text-align: right;">Emig Charges</th>
                                                    <th style="width: 10%;text-align: right;">Mofa Amt.</th>
                                                    <th style="width: 10%;text-align: right;">Other</th>
                                                    <th style="width: 10%;text-align: right;">Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody id="tabledata">
                                                <?php
                                                if ($addflag != 1) {
                                                    $ctr = 0;
                                                    while ($tran = mysqli_fetch_array($gettran)) {
                                                        echo "<tr>";
                                                        echo "<td align='center' valign='top'><span class='remove fa fa-times ' name='remove' value='remove' style='display:none;'></span></td>";
                                                        echo "<td><input type='text' value='$tran[srno]'  name='srno_$ctr'  class='form-control form text-right' style='text-align:center;' readonly></td>";
                                                        echo "<td>";
                                                        echo "<input type='text' value='$tran[passport]' name='passport_$ctr' class='form-control form passport' >";
                                                        echo "</td>";
                                                        echo "<td>";
                                                        echo "<input type='text' value='$tran[paxname]' name='paxname_$ctr' class='form-control form paxname' readonly>";
                                                        echo "</td>";
                                                        echo "<td>";
                                                        echo "<input type='number' value='$tran[stampamt]' name='stampamt_$ctr' class='form-control form stampamt' style='text-align:right;' readonly >";
                                                        echo "</td>";
                                                        echo "<td>";
                                                        echo "<input type='number' value='$tran[imigamt]' name='imigamt_$ctr' class='form-control form imigamt' style='text-align:right;' readonly >";
                                                        echo "</td>";
                                                        echo "<td>";
                                                        echo "<input type='number' value='$tran[mofamt]' name='mofamt_$ctr' class='form-control form mofamt' style='text-align:right;' readonly >";
                                                        echo "</td>";
                                                        echo "<td>";
                                                        echo "<input type='number' value='$tran[miscamt]' name='miscamt_$ctr' class='form-control form miscamt' style='text-align:right;' readonly >";
                                                        echo "</td>";
                                                        echo "<td>";
                                                        echo "<input type='number' value='$tran[memcash]' name='memcash_$ctr' class='form-control form memcash' style='text-align:right;' readonly >";
                                                        echo "</td>";
                                                        echo "</tr>";
                                                        $ctr++;
                                                    }
                                                } else {
                                                    $pctr = 0;
                                                    $yctr = 0;

                                                    $yctr++;
                                                    echo "<tr>";
                                                    echo "<td align='center'><span class='remove fa fa-times ' name='remove' value='remove' style=''></span></td>";
                                                    echo "<td  align='left'><input type='text' size='6' maxlength='6' name='srno_$pctr' class='sno form-control form' style='text-align:center' value='$yctr' readonly></td>";

                                                    echo "<td><input type='text'   name='passport_$pctr' class='passport form-control form' style='text-align:left;' value='' /></td>";
                                                    echo "<td><input type='text'   name='paxname_$pctr' class='paxname form-control form' style='text-align:left;' value='' readonly /></td>";
                                                    echo "<td><input type='number'   name='stampamt_$pctr' class='stampamt form-control form' style='text-align:left;' value='' readonly /></td>";
                                                    echo "<td><input type='number'   name='imigamt_$pctr' class='imigamt form-control form' style='text-align:left;' value='' readonly /></td>";
                                                    echo "<td><input type='number'   name='mofamt_$pctr' class='mofamt form-control form' style='text-align:left;' value='' readonly /></td>";
                                                    echo "<td><input type='number'   name='miscamt_$pctr' class='miscamt form-control form' style='text-align:left;' value='' readonly /></td>";
                                                    echo "<td><input type='number' name='memcash_$pctr' class='memcash form-control form' style='text-align:right;' value='' readonly /></td>";
                                                    echo "</tr>";
                                                    $pctr++;
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <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 class="col-lg-12 col-xs-12 pad">
                                        <div class="col-lg-5 pad"></div>
                                        <div class="col-lg-3 col-xs-5 pad text-right">
                                            <label class="label-control marg text-right">No. of Passports</label>
                                        </div>
                                        <div class="col-lg-1 col-xs-3 pad">
                                            <input type="number" step="any" name="nofpass" class="form-control form text-right nofpass" value="<?php echo ($addflag != 1) ? $get['nofpass'] : "0"; ?>" readonly>
                                        </div>
                                        <div class="col-lg-1 col-xs-1 pad text-right">
                                            <label class="label-control marg">Sub Total</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-3 pad">
                                            <input type="number" step="any" name="subtot" class="form-control form text-right subtot" value="<?php echo ($addflag != 1) ? $get['subtot'] : "0"; ?>" readonly>
                                        </div>
                                    </div>
                                    <div class="col-lg-12 col-xs-12 pad">
                                        <div class="col-lg-10 col-xs-1 pad text-right">
                                            <label class="label-control marg">Add Courier</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-3 pad">
                                            <input type="number" step="any" name="courier" class="form-control form text-right courier" value="<?php echo ($addflag != 1) ? $get['courier'] : "0"; ?>">
                                        </div>
                                    </div>
                                    <div class="col-lg-12 col-xs-12 pad marg">
                                        <div class="col-lg-10 col-xs-1 pad text-right">
                                            <label class="label-control marg">Add Misc. Charges</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-3 pad">
                                            <input type="number" step="any" name="misc" class="form-control form text-right misc" value="<?php echo ($addflag != 1) ? $get['misc'] : "0"; ?>">
                                        </div>
                                    </div>
                                    <div class="col-lg-12 col-xs-12 pad">
                                        <div class="col-lg-10 col-xs-1 pad text-right">
                                            <label class="label-control marg">Total</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-3 pad">
                                            <input type="number" step="any" name="totamt" class="form-control form text-right totamt" value="<?php echo ($addflag != 1) ? $get['totamt'] : "0"; ?>" readonly>
                                        </div>
                                    </div>
                                </div>
                            </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</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" style="border-radius: 5px" 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="10%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="10%">Agent</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" style="border-radius: 5px" value="Previous">
                            <input type="button" class="btn btn-primary snext" style="border-radius: 5px" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>