<?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];
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` 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' ") 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"];

            if (!empty($mparty)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `accode`, `debit`, `credit`)
                                            VALUES('$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`(date, vouchno, source,  trnno, accode, grpcode, amount,prodtype,actamt)
                              VALUES ('$vouchdt','$mvouch', '$source', '$ctr', '$mparty', '$mgrpcode', '$accamt','$prodtype','$accamt')");
            }
        }


        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `remark1`, `remark2`)
                                        VALUES('$mvouch', '$vouchdt', '$remark1', '$remark2')") or die(mysqli_error($conn));
        } else {
            $mvouch = $qvouch;

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `remark1`='$remark1', `remark2`='$remark2'
                        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);

}

$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");
    delete($zdb, "$trandb", "$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/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>
                    <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  style="background: #337AB7;">
                                    <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]' 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' 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" 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>
            </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" 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=" table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #78909C">
                                    <!--<th>Sr. No.</th>-->
                                    <th>Voucher</th>
                                    <th>Date</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` ") or die(mysqli_error($conn));
                                while ($pmain = mysqli_fetch_array($srchresult)) {
                                    $ctr++;
                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$pmain[$keyfld]'>";
                                    echo "<td align='center'>$pmain[vouchno]</td>";
                                    echo "<td align='left'>$pmain[vouchdt]</td>";
                                    echo "<td align='center'><a href=\"$file?mode=edit&$keyfld=$pmain[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                    echo "</tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
