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

$formname = 'Container ETA Entry';
$maindb = 'shipmain';
$accdb  = 'actran';

$printfile = "shipperreg.php";
$file = 'shipperent.php';
$trandbexist = 0;
$grid = '';
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";
$keyfld = "vouchno";
$source = 'ETA';

$focusfield = "name";
$username = $_SESSION["username_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

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

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

    $a = 1;
    $qvouch = $_POST["vouchno"];
    foreach ($_POST as $key => $value) {
        $exclude_fld = ["test"];
        if (in_array($key, $exclude_fld)) {
            continue;
        }
        $$key = $value;
        // echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
    }
    $relcheck = $_POST["relcheck"];
    $releasedt = $_POST["releasedt"];
    $mvouch = $qvouch;

    // $docvalue = $_POST["docvalue"];

    if ($a == 1) {

        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);
                $increvouch = $vvouch["maxcode"] + 1;
            } else {
                $increvouch = '1';
            }
            $qvouch = $increvouch;
        }
        $mvouch = $qvouch;

        if ($relcheck == 1) {
            if ($releasedt == "0000-00-00") {
                $releasedt = date('Y-m-d');
            }
        } else {
            $releasedt == "0000-00-00";
        }

        ///////////    accounts part - actran 
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source`='$source'") OR die(mysqli_error($conn));

        $minus_amt = $inramt * -1;
        $minus_doc = $docvalue * -1;

        // expense part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, curr, feamt, roe)
                                            VALUES ('$vouchdt','$mvouch', '$source', '001', '28', '007', '$minus_amt', 'USD', '$minus_doc', '$roe')") or die(mysqli_error($conn));
                        
        // expense part to person in charge
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, curr, feamt, roe)
                                          VALUES ('$vouchdt','$mvouch', '$source', '002', '49','017', '$inramt', 'USD', '$docvalue', '$roe')") or die(mysqli_error($conn));

        
        ///////////////////////////////

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `code`, `vouchdt`, `name`, `container`, `eta`, `live`, `status`,`remark1`, `remark2`,
                                                        invno, logo, delvdt, totwt, invamt, relcheck, releasedt, docvalue, roe, inramt)
                                                VALUES('$mvouch', '$code', '$vouchdt', '$name', '$container', '$eta', '$live', '$status', '$remark1', '$remark2', 
                                                       '$invno', '$logo', '$delvdt', '$totwt', '$invamt', '$relcheck', '$releasedt', '$docvalue', '$roe', '$inramt')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET `code`='$code', `vouchdt`='$vouchdt', `name`='$name', `container`='$container', invno='$invno', logo='$logo', delvdt='$delvdt',
                                                     `eta`='$eta', `live`='$live', `status`='$status', `remark1`='$remark1', totwt='$totwt', invamt='$invamt',
                                                    `remark2`='$remark2', `relcheck`='$relcheck', `releasedt`='$releasedt', `docvalue`='$docvalue', `roe`='$roe',
                                                    `inramt`='$inramt'  where vouchno = '$mvouch' $keycond") 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' $keycond ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $ghall = mysqli_query($conn, "SELECT * FROM accmast WHERE accode = '$get[name]' ") or die(mysqli_error($conn));
    $fhall = mysqli_fetch_object($ghall);
}

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

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

    $delcondn = " $keyfld = '$_GET[$keyfld]' $keycond";

    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
}

include("includes/linkshead.php");
?>

<!-- <script src="MyScript/shipperent.js" type="text/javascript"></script> -->
<script src="MyScript/shipperent_search.js" type="text/javascript"></script>
</head>
<style>
    .relcheck{
        width:27px;
        margin-top: -1px;
    }
</style>
<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <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-3 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-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get["vouchno"] : $qvouch; ?>" readonly>
                                <input type="hidden" class="form-control form code" name="code" value="<?php echo ($addflag != 1) ? $get["code"] : ""; ?>">
                            </div>

                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="date" name="vouchdt" class="form-control focusfield form vouchdt " value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>" required>
                                <input type="hidden" name="releasedt" class="form-control focusfield form releasedt " value="<?php echo ($addflag != 1) ? $get["releasedt"] : "0000-00-00"; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Release</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox"  class="form-control form relcheck" <?php
                                if ($get['relcheck'] == "1") {
                                    Echo "checked";
                                }
                                ?> value="1" name="relcheck" >                         
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Shipper</label>
                            </div>
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form name select2" name="name" id="name" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['name'] : ""; ?>"><?php echo ($addflag != 1) ? $fhall->name : ""; ?></option>
                                    <?php
                                    $gethall = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode='040' ") or die(mysqli_error($conn));
                                    while ($fethall = mysqli_fetch_object($gethall)) {
                                        echo "<option value='$fethall->accode'>$fethall->name</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Container</label>
                            </div>
                            <div class="col-lg-4 col-md-8 col-sm-8 col-xs-8 pad">
                                <input list='container' name='container' class='container form-control form' value='<?php echo ($addflag != 1) ? $get["container"] : ""; ?>'>
                                <datalist id='container' char='#'>
                                    <option><?php
                                        $gcontainer = mysqli_query($conn, 'SELECT * FROM `shipmain`') or die(mysqli_error($conn));
                                        while ($fcont = mysqli_fetch_array($gcontainer)) {
                                            echo "<option value='$fcont[container]'></option>";
                                        }
                                        ?>
                                </datalist>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Invoice No.</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form invno" name="invno" value="<?php echo ($addflag != 1) ? $get["invno"] : ""; ?>" >
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Logo</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form logo" name="logo" value="<?php echo ($addflag != 1) ? $get["logo"] : ""; ?>" >
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Total Wt.</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="number" step="any" class="form-control form text-right totwt" name="totwt" value="<?php echo ($addflag != 1) ? $get["totwt"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Invoice Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="number" step="any" class="form-control text-right form invamt" name="invamt" value="<?php echo ($addflag != 1) ? $get["invamt"] : ""; ?>" >
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">ETA</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form eta" name="eta" value="<?php echo ($addflag != 1) ? $get["eta"] : ""; ?>" required>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Delivery Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form delvdt" name="delvdt" value="<?php echo ($addflag != 1) ? $get["delvdt"] : ""; ?>" >
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Line</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input list='live' name='live' class='live form-control form' value='<?php echo ($addflag != 1) ? $get["live"] : ""; ?>'>
                                <datalist id='live' char='#'>
                                    <option><?php
                                        $glive = mysqli_query($conn, 'SELECT * FROM `shipmain`') or die(mysqli_error($conn));
                                        while ($flive = mysqli_fetch_array($glive)) {
                                            echo "<option value='$flive[live]'></option>";
                                        }
                                        ?>
                                </datalist>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Status</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input list='status' name='status' class='status form-control form' value='<?php echo ($addflag != 1) ? $get["status"] : ""; ?>'>
                                <datalist id='status' char='#'>
                                    <option><?php
                                        $gstatus = mysqli_query($conn, 'SELECT * FROM `shipmain`') or die(mysqli_error($conn));
                                        while ($fstatus = mysqli_fetch_array($gstatus)) {
                                            echo "<option value='$fstatus[status]'></option>";
                                        }
                                        ?>
                                </datalist>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">DOC VALUE $</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right docvalue" name="docvalue" value="<?php echo ($addflag != 1) ? $get["docvalue"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">ROE</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right roe" name="roe" value="<?php echo ($addflag != 1) ? $get["roe"] : ""; ?>" >
                            </div>

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

                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Remarks</label>
                            </div>
                            <div class="col-lg-6 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark1" class="form-control form remark1" value="<?php echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 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-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark2" class="form-control form remark2" value="<?php echo ($addflag != 1) ? $get["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">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Vouchno</option>
                                <option value="2">Date</option>
                                <option value="3">Name</option>
                                <option value="4">Container</option>
                                <option value="5">ETA</option>
                                <option value="6">Line</option>
                                <option value="7">Status</option>
                                <option value="8">Invoice</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">Vouchno</option>
                                <option value="2">Date</option>
                                <option value="3">Name</option>
                                <option value="4">Container</option>
                                <option value="5">ETA</option>
                                <option value="6">Line</option>
                                <option value="7">Status</option>
                                <option value="8">Invoice</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" style="border-radius: 5px" 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" style="background-color: #1cbc9c;">
                                    <th width="3%">Voucher No</th>
                                    <th width="7%">Date</th>
                                    <th width="7%">Invoice</th>
                                    <th width="7%">Shipper</th>
                                    <th width="7%">Container</th>
                                    <th width="7%">ETA</th>
                                    <th width="7%">Line</th>
                                    <th width="7%">Status</th>
                                    <th width="5%">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" style="border-radius: 5px" class="btn btn-primary sprev" value="Previous">
                            <input type="button" style="border-radius: 5px" 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>