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

$formname = 'Bewa / Widow Entry';
$maindb = 'widow';
// $trandb = 'widow';
$printfile = "";
$file = 'widow.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "";

$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

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

$zroot = $_SESSION["zdir"];

$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
//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");
$rettime = date("h:i:sa");
$usertype = $_SESSION["usertype_" . $dbname];
//end

date_default_timezone_set("Asia/Calcutta");   //India time (GMT+5:30)

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $count = $_POST["items"];
    $mvouch = $qvouch;

    if ($a == 1) {

        #################################TRAN PART #################################

        mysqli_query($conn, "DELETE FROM `$maindb` WHERE `$keyfld`='$mvouch' $keycond") or die(mysqli_error($conn));
        // mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' and `source`='$source' $keycond") or die(mysqli_error($conn));
        //        mysqli_query($conn, "DELETE FROM $zroot.$combstkdb WHERE `$keyfld`='$mvouch' AND `source`='$source' and `compcode`='$compcd'")or die(mysqli_error($conn));


        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $srl = $_POST["srno_$i"];
            $memcode = $_POST["memcode_$i"];
            $memname = $_POST["memname_$i"];
            $mobile = $_POST["mobile_$i"];
            $address = $_POST["address_$i"];

            if (!empty($itemname)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$maindb`(`srno`, series, `vouchno`, `memcode`, `memname`, `mobile`, `address`)
                                        VALUES( '$srl', '$fseries', '$vouchno', '$memcode', '$memname', '$mobile', '$address')") or die(mysqli_error($conn));

                updmess();
                header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
                ##########sTOCK pART########
                // mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, series, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`,color)
                //                         VALUES( '$srl', '$fseries', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$rate', '$amount','$color')") or die(mysqli_error($conn));

                //                ##########Combine Stock Part########
                //                mysqli_query($conn, "INSERT INTO $zroot.$combstkdb(`srno`, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`)
                //                                        VALUES( '$srl', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$barcode', '$item', '$trqty', '$unit', '$mrp', '$rate', '$amount')") or die(mysqli_error($conn));
            }
        }

        #################################TRAN PART END #################################


        ############################## MAIN ######################################################
        //     if ($addflag == 1) {
        //         mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, series, `vouchdt`,`formem`, `memcode` , `add2`,
        //          `add3`,`mob2`, `adduser`, `adddate`, `addtime`, `timespan`,`remark1`,`remark2`,`totamt`, `billamt`,`retdate`, `rettime`, `advance`, `bal`, `days`)
        //                                  VALUES ('$qvouch', '$fseries', '$vouchdt',  '$formem', '$memcode' , '$add2', 
        //                                  '$add3', '$mob2','$adduser','$adddate','$addtime', '$timespan', '$remark1', '$remark2'
        //                                  ,'$totamt', '$billamt', '$retdate' , '$rettime', '$advance', '$bal', '$days')") or die(mysqli_error($conn));
        //         insmess();
        //         $mvouch = $qvouch; //1
        //         header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        //     } else {
        //         $mvouch = $qvouch; //1
        //         $editdate = date("Y-m-d");
        //         $edittime = date("h:i:sa");

        //         mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt',  `formem` = '$formem' ,	`memcode` = '$memcode', 
        //                              `add2`='$add2', `add3`='$add3', `mob2`='$mob2',`edituser`='$adduser',
        //                             `edittime`='$edittime',`editdate`='$editdate', `timespan`='$timespan' , `remark1`='$remark1'
        //                             ,`remark2`='$remark2' ,`totamt`='$totamt' ,`billamt`='$billamt',`retdate`='$retdate', `rettime`='$rettime',
        //                              `advance`='$advance',`bal`='$bal', `days`='$days' WHERE `vouchno`= '$mvouch' $keycond") or die(mysqli_error($conn));
        //         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"])) {
    //    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    //    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcondn = " vouchno = '$_GET[vouchno]' $keycond";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' $keycond";
    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
    // delnew($zdb, "$trandb", "$delcondn", "$file?mode=last&series=$fseries");
    // delnew($zdb, "$stkdb", "$delcond", "$file?mode=last&series=$fseries");
}

include("includes/linkshead.php");
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("#hadd").hide();
        $("#hprint").hide();
        $("#hdelete").hide();
        $("#hprevious").hide();
        $("#hnext").hide();
        $("#hsearch").hide();
    });
</script>
<script src="MyScript/widow.js" type="text/javascript"></script>
<script src="MyScript/rentitem_search.js" type="text/javascript"></script>
<script src="MyScript/checkdate.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 class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>

                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="10%">Member Code</th>
                                        <th width="30%">Member Name</th>
                                        <th width="20%">Mobile No</th>
                                        <th width="35%">Address</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $trate = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `vouchno`!='' ORDER BY `srno`") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            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><input type='text' class='form-control form text-right memcode'  name='memcode_$ctr' value='$tran[memcode]'>  </td>";
                                            echo "<td><input type='text' class='form-control form text-right memname'   name='memname_$ctr'        value='$tran[memname]'>   </td>";
                                            echo "<td><input type='text' class='form-control form text-right mobile'   name='mobile_$ctr'        value='$tran[mobile]'>   </td>";
                                            echo "<td><input type='text' class='form-control form text-right address' name='address_$ctr'      value='$tran[address]'> </td>";
                                            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><input type="text" class="form-control form text-right memcode" name="memcode_<?= $pctr ?>" value=""></td>
                                            <td><input type="text" class="form-control form text-right memname" name="memname_<?= $pctr ?>" value=""></td>
                                            <td><input type="text" class="form-control form text-right mobile" name="mobile_<?= $pctr ?>" value=""></td>
                                            <td><input type="text" class="form-control form text-right address" name="address_<?= $pctr ?>" value="" readonly></td>
                                        </tr>
                                    <?php
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>
                        </div>

                        <div class="row">
                            <div class="col-lg-1">
                                <button type="button" class="btn btn-square form" style="background-color: #78909C" 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 class="row">
                        <div class="col-lg-7 col-md-5 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control text-size marg">Total Bewa / Widow</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control form text-right totwid" name="totwid" value="<?php echo ($addflag != 1) ? $tran['totwid'] : ""; ?>" readonly>
                        </div>
                        <div class="col-lg-1 col-md-5 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control text-size marg">Total Amount</label>
                        </div>
                        <div class="col-lg-2 col-md-1 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control form text-right totamt" name="totamt" value="<?php echo ($addflag != 1) ? $tran['totamt'] : ""; ?>" readonly>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 col-md-5 col-sm-9 col-xs-9 pad">

                <?php

                include("includes/buttons.php");

                ?>


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


    <?php include("includes/linksfoot.php"); ?>
</body>

</html>