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

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

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

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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$comp = $_SESSION["comp_" . $dbname];
$zStartDate = getzdates($zyear, 'S', $comp);
$zEndDate = getzdates($zyear, 'E', $comp);
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchdt = $_POST["vouchdt"];
    $qvouch = $_POST["vouchno"];
    
    $billno = $_POST["billno"];
    $gst = $_POST["gst"];
    $remark1 = mysqli_real_escape_string($conn, $_POST["remark1"]);
    $remark2 = mysqli_real_escape_string($conn, $_POST["remark2"]);
    $totcredit = $_POST["totcredit"];
    $totdebit = $_POST["totdebit"];
    $count = $_POST["items"];
    $count2 = $_POST["items2"];
//browse($zStartDate);
//browse($zEndDate);
    $mvouch = $qvouch;

    $salecode = 11;


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

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` 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 `$trandb2` WHERE `$keyfld`='$mvouch' ") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch' ") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$accdb`  WHERE `$keyfld`='$mvouch' and source = '$source' ") 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"];
            $mot = $_POST["ot_$i"];
            $remarktran = mysqli_real_escape_string($conn, $_POST["remarktran_$i"]);
            if (!empty($mparty)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `accode`, `debit`, `credit`, `remarktran`,ot)
                                            VALUES('$mvouch', '$vouchdt', '$msrno', '$mparty', '$mdebit', '$mcredit', '$remarktran','$mot')") 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`(date, vouchno, source,  trnno, accode, grpcode, amount)
                              VALUES ('$vouchdt','$mvouch', '$source', '$ctr', '$mparty', '$mgrpcode', '$accamt')");
            }
        }

        $srl2 = 0;
        for ($j = 0; $j < $count2; $j++) {
            $msrno2 = $_POST["srno2_$j"];
            $mdescrip = mysqli_real_escape_string($conn, $_POST["descrip_$j"]);
            $mamount = $_POST["amount_$j"];
            $srl2++;

            if (!empty($mdescrip)) {
                mysqli_query($conn, "INSERT INTO `$trandb2`(`vouchno`, `vouchdt`, `srno`, `descrip`, `amount`)
                                            VALUES('$mvouch', '$vouchdt', '$srl2', '$mdescrip', '$mamount')") or die(mysqli_error($conn));
            }
        }


        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `branch`, `remark1`, `remark2`,`totcredit`,`totdebit`,`billno`,`gst`,`adduser`,`adddate`,`addtime`)
                                        VALUES('$mvouch', '$vouchdt', '$branch', '$remark1', '$remark2','$totcredit','$totdebit','$billno','$gst','$adduser','$adddate','$addtime')") 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', `branch`='$branch', `remark1`='$remark1', `remark2`='$remark2',
                        `totcredit`='$totcredit',`totdebit`='$totdebit',`billno`='$billno',`gst`='$gst',`edituser`='$adduser', `editdate`='$editdate', `edittime`='$edittime'  WHERE `vouchno`= '$mvouch'") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            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'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

//    $getname = mysqli_query($conn, "SELECT * FROM  `branch` WHERE `code`='$get[branch]'")or die(mysqli_error($conn));
//    $showname = mysqli_fetch_array($getname);
}

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

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

    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb2", "$keyfld", $_GET[$keyfld], "$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/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>

    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <?php include('includes/usermention.php'); ?>
                    </div>
                    <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>
                                <input type="hidden" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                                <input type="hidden" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            </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 class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Bill No.</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="billno" value="<?php Echo ($addflag != 1) ? $get["billno"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">GST</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <select class="form-control form select2" name="gst">
                                    <option
                                    <?php
                                    if ($get['gst'] == 'Y') {
                                        echo "selected=selected";
                                    }
                                    ?> value='Y'>YES</option>
                                    <option
                                    <?php
                                    if ($get['gst'] == 'N') {
                                        echo "selected=selected";
                                    }
                                    ?> value='N'>NO</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-12 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="20%">Accounts</th>
                                        <th width="20%"class='text-right'>Debit</th>
                                        <th width="20%"class='text-right'>Credit</th>
                                        <th width="5%" class="text-center">#</th>
                                        <th width="30%">Remark</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' 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 select2 party' 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]' step='any'></td>";
                                            Echo "<td><input type='number' class='form-control form text-right credit' name='credit_$ctr' value='$tran[credit]'  step='any'></td>";
                                            ?>
                                        <td>
                                            <input type="text" name="ot_<?= $ctr ?>" class="form-control form ot" value="<? Echo $tran["ot"]; ?>">
                                        </td>
                                        <td>
                                            <input type="text" name="remarktran_<?= $ctr ?>" class="form-control form remarktran" value="<? Echo $tran["remarktran"]; ?>">
                                        </td>
                                        <?php
                                        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"  step='any'></td>
                                        <td><input type="number" step="any" class="form-control form text-right credit"   name="credit_<?= $pctr ?>" value="0"  step='any'></td>
                                        <td><input type="text"  class="form-control form  ot"   name="ot_<?= $pctr ?>" value="" ></td>
                                        <td><input type="text"  class="form-control form  remarktran"   name="remarktran_<?= $pctr ?>" value="" ></td>
                                    <input type="hidden"     class="form-control form grpcode"   name="grpcode_<?= $pctr ?>" value="" >                                        
                                    </tr>  
                                    <?
                                    $pctr++;
                                }
                                ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-12 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 class="col-lg-12 pad">
                            <div class="col-lg-5 pad text-right">
                                <label class="label-control marg">Debit</label>
                            </div>
                            <div class="col-lg-1 pad text-right">
                                <input type="text" id="totdebit" name="totdebit" class="text-right form-control form" readonly value="<?php echo ($addflag != 1) ? $get["totdebit"] : ""; ?>" />  
                            </div>
                            <div class="col-lg-1 pad text-right">Credit</div>
                            <div class="col-lg-1 pad text-right">
                                <input type="text" id="totcredit" name="totcredit" class="text-right form-control form"  readonly value="<?php echo ($addflag != 1) ? $get["totcredit"] : ""; ?>" />  
                            </div>
                        </div>

                        <!--detail part2 start-->


                        <!--detail part2 end-->
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-xs-12 pad">
                <div class="table-responsive" style="width:40%">
                    <table class="table table-striped table-condensed table-bordered">
                        <thead  class="mySearchTr">
                            <tr>
                                <th width="5%"></th>
                                <th width="5%" class='text-right'>Sr.</th>
                                <th width="70%">Description</th>
                                <th width="20%"class='text-right'>Amount</th>
                            </tr>
                        </thead>
                        <tbody class="tbody2">
                            <?php
                            if ($addflag != 1) {
                                $ctr2 = 0;
                                $gettran2 = mysqli_query($conn, "SELECT * FROM `$trandb2` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                while ($tran2 = mysqli_fetch_array($gettran2)) {

                                    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='srno2_$ctr2' value='$tran2[srno]'></td>";
                                    Echo "<td><input type='text' class='form-control form  descrip' name='descrip_$ctr2' value='$tran2[descrip]' maxlength='60'></td>";
                                    Echo "<td><input type='number' class='form-control form text-right amount' name='amount_$ctr2' value='$tran2[amount]'></td>";
                                    Echo "</tr >";
                                    $ctr++;
                                }
                            } else {
                                $pctr2 = 0;
                                ?>
                                <tr>
                                    <td><i class="fa fa-close remove"></i></td>
                                    <td><input type="text" class="form-control form text-right srno" name="srno2_<?= $pctr2 ?>" value="1"></td>
                                    <td><input type="text"  class="form-control form  descrip"   name="descrip_<?= $pctr2 ?>" value="" maxlength="60"></td>
                                    <td><input type="number" step="any" class="form-control form text-right amount"   name="amount_<?= $pctr2 ?>" value="0" ></td>
                                </tr>  
                                <?
                                $pctr2++;
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
                <div class="col-lg-12 pad">
                    <button type="button" class="btn btn-success btn-square form"  id="addnew2">
                        <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                    </button>
                    <input type="hidden" id="items2" name="items2" value="<?php echo ($addflag != 1) ? $ctr2 : $pctr2; ?>" />  
                </div>

                <div class="col-lg-1 pad text-right">
                    <label class="label-control marg">Remarks</label>
                </div>
                <div class="col-lg-6 pad">
                    <input type="text" name="remark1" class="form-control form " value="<? Echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                </div>
            </div>
            <div class="col-lg-12 col-xs-12 pad">
                <div class="col-lg-1 pad text-right"></div>
                <div class="col-lg-6 pad">
                    <input type="text" name="remark2" class="form-control form " value="<? Echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                </div>
            </div>
            <div class="col-lg-12 col-xs-12 pad">
                <div class="col-lg-8 pad text-right">
                    <input type="button" id="finishTab" class="btn btn-primary pull-right form" style="background-color:#26A69A;color: #fff;" value="Check & Finish">
                </div>
            </div>

            <div class="col-lg-12 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">Bill No.</option>
                                <option value="3">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">Bill No.</option>
                                <option value="3">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" style="">
                                    <th width="30%">Voucher</th>
                                    <th width="30%">Bill No.</th>
                                    <th width="30%">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>
