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

$formname = 'JV';
$maindb = 'jvmain';
$trandb = 'jvtran';
$accdb = 'actran';

$keyfld = "vouchno";
$printfile = "jv_print.php";
$file = 'jv.php';
$source = 'JV';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
$myyear = $_SESSION[$myyear];
$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

//for adding entry and edit timings
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$usertype = $_SESSION["usertype_" . $dbname];
//end

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
//    $total = $_POST["total"];
    $count = $_POST["items"];


    $mvouch = $qvouch;

    $salecode = 11;


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

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' $keycond 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, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch' $keycond") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$accdb`  WHERE `$keyfld`='$mvouch' and source = '$source' $keycond") or die(mysqli_error($conn));

        $ctr = 0;
        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mparty = $_POST["party_$i"];
            $mgrpcode = $_POST["grpcode_$i"];
            $mdebit = $_POST["debit_$i"];
            $mcredit = $_POST["credit_$i"];

            if (!empty($mparty)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(series, `vouchno`, `vouchdt`, `srno`, `accode`, `debit`, `credit`)
                                            VALUES('$fseries', '$mvouch', '$vouchdt', '$msrno', '$mparty', '$mdebit', '$mcredit')") or die(mysqli_error($conn));

                if (($mgrpcode) == '' || empty($mgrpcode)) {
                    $qrygrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$mparty'") or die(mysqli_error($conn));
                    $fetgrp = mysqli_fetch_array($qrygrp);
                    $mgrpcode = $fetgrp['grpcode'];
                }

                if ($mdebit > 0) {
                    $accamt = $mdebit * -1;
                } else {
                    $accamt = $mcredit;
                }
                $ctr++;
                // party part
                mysqli_query($conn, "INSERT INTO `$accdb`(series, date, vouchno, source,  trnno, accode, grpcode, amount,prodtype,actamt)
                              VALUES ('$fseries', '$vouchdt','$mvouch', '$source', '$ctr', '$mparty', '$mgrpcode', '$accamt','$prodtype','$accamt')");
            }
        }


        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(series, `vouchno`, `vouchdt`, `remark1`, `remark2`)
                                        VALUES('$fseries', '$mvouch', '$vouchdt', '$remark1', '$remark2')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `remark1`='$remark1', `remark2`='$remark2',`edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate'
                        WHERE `vouchno`= '$mvouch' $keycond") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        }
    }
}

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

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

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `$accdb` WRITE") or die(mysqli_error($conn));

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

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

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/jv.js" type="text/javascript"></script>
<script src="MyScript/jv_search.js" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<!--<script src="MyScript/shortkeys.js" type="text/javascript"></script>-->
</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-8 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <!--                    <div class="panel-heading">
                                            <h3 class="panel-title" style="color:#78909C "><?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-heading">
                        <?php include("includes/usermention.php");
                        ?>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>

                    <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                    <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 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-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                            </div>
                            <div class="col-lg-1 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-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus>
                            </div>
                        </div>


                    </div>
                </div>
            </div>
            <div class="col-lg-8 col-md-12 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered" width="50%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th width="5%"></th>
                                        <th width="5%" class='text-right'>Sr.</th>
                                        <th width="50%"class='text-left'>Accounts</th>
                                        <th width="20%"class='text-right'>Debit</th>
                                        <th width="20%"class='text-right'>Credit</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' $keycond ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $gparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$tran[accode]'") or die(mysqli_error($conn));
                                            $getparty = mysqli_fetch_array($gparty);

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form party select2 fetchpart_$ctr' name='party_$ctr' style='width:100%'>";
                                            Echo "<option value='$getparty[accode]'>$getparty[name]</option>";
                                            $gexp = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode` > '002' order by name")or die(mysqli_error($conn));
                                            while ($showexp = mysqli_fetch_array($gexp)) {
                                                Echo "<option value='$showexp[accode]'>$showexp[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='number' class='form-control form text-right debit' name='debit_$ctr' value='$tran[debit]'></td>";
                                            Echo "<td><input type='number' class='form-control form text-right credit' name='credit_$ctr' value='$tran[credit]'></td>";
                                            Echo "<input type='hidden' class='form-control form grpcode' name='grpcode_$ctr' value='$getparty[grpcode]'>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_<?= $pctr ?>" value="1"></td>
                                            <td>
                                                <select class="form-control form party select2 " name="party_<?= $pctr ?>" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` where `grpcode` > '002' order by name")or die(mysqli_error($conn));
                                                    while ($showparty = mysqli_fetch_array($getparty)) {
                                                        Echo "<option value='$showparty[accode]'>$showparty[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="number" step="any" class="form-control form text-right debit"   name="debit_<?= $pctr ?>" value="0" ></td>
                                            <td><input type="number" step="any" class="form-control form text-right credit"   name="credit_<?= $pctr ?>" value="0" ></td>
                                    <input type="hidden"     class="form-control form grpcode"   name="grpcode_<?= $pctr ?>" value="" >                                        
                                    </tr>  
                                    <?
                                    $pctr++;
                                }
                                ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-8 pad">
                            <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 : $pctr; ?>" />  

                        </div>

                    </div>
                    <div class="row">
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                            <label class="label-control marg">Remarks</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="remark1" value="<?php Echo ($addflag != 1) ? $get["remark1"] : ""; ?>" >
                            <input type="text" class="form-control form" name="remark2" value="<?php Echo ($addflag != 1) ? $get["remark2"] : ""; ?>" >
                        </div>

                    </div>
                </div>
            </div>

            <div class="col-lg-8 col-md-12 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <?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>
                            </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>
                            </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="10%">Voucher</th>
                                    <th width="10%">Date</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>
