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

$formname = 'Opening Balance ';
$maindb = 'accmast';
$file = 'openbal.php?mode=last&form=openbal';

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_GET["mode"]) == "true") {
    $addflag = 1;
} else {
    $addflag = 0;
}

if (isset($_POST['submit'])) {
    $totdebit = $_POST["totdebit"];
    $totcredit = $_POST["totcredit"];
    $count = $_POST["count"];

    for ($i = 0; $i < $count; $i++) {
        $accode = $_POST['accode_' . $i];
        $debit = $_POST['debit_' . $i];
        $credit = $_POST['credit_' . $i];
    
        if ($debit > 0) {
            $mopenbal = $debit;
            $mdc = "D";
        } else {
            if ($credit > 0) {
                $mopenbal = $credit;
                $mdc = "C";
            } else {
                $mopenbal = 0.00;
            }
        }

        mysqli_query($conn, "UPDATE `$maindb` SET openbal='$mopenbal', `dc`='$mdc' WHERE accode='$accode' ") or die(mysqli_error($conn));
    }
    updmess();
    header("refresh: 1; $file");
}

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

<script>
    $(document).ready(function () {
        $('#data').on('blur change', '.cal, .cal2', calculateSum);

        $("#cmbgrp").change(function () {
            var cmbgrp = $('#cmbgrp').val();
            $.ajax({
                type: "GET",
                url: "opnbalsrch.php",
                data: {code: cmbgrp},
                success: function (result) {
                    $("#data").html(result);
                }
            });
        });

        function calculateSum() {
            var dsum = 0;
            $(".cal").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    dsum += parseFloat(this.value);
                }
            });
            $("#totdebit").val(dsum.toFixed(2));


            var csum = 0;
            $(".cal2").each(function () {
                if (!isNaN(this.value) && this.value.length != 0) {
                    csum += parseFloat(this.value);
                }
            });
            $("#totcredit").val(csum.toFixed(2));

            var $row = $(this).closest("tr");
            var mname = $row.find('.cal').val();
            var mname2 = $row.find('.cal2').val();

            if (mname != "" || mname != 0) {
                $($row.find('.cal2')).attr("readonly", true);
            } else {
                $($row.find('.cal2')).removeAttr("readonly");
            }


            if (mname2 != "" || mname2 != 0) {
                $($row.find('.cal')).attr("readonly", true);
            } else {
                $($row.find('.cal')).removeAttr("readonly");
            }

        }

        $('#submit').click(function () {
            $('#bill').submit();
        });
    });

    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.ctrlKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#submit").click();
        event.preventDefault();
        return false;
    });
</script>
<!-- main content start-->
<div class="panel panel-widget forms-panel my-div width col-lg-9" >
    <div class="forms">
        <div class="form-grids widget-shadow" data-example-id="basic-forms"> 
            <div class="form-title">
                <h4><?php echo $formname ?> </h4>
            </div>
            <form name="" class="validation" id="form" action="" method="post">
                <div class="row">
                    <div class="form-body">
                        <div class="form-group has-error">
                            <label class="col-sm-2 control-label">A/C Group</label>
                            <div class="col-sm-9">
                                <select id="cmbgrp" name="cmbgrp" style="height:35px;"  class="cmbgrp form-control">
                                    <option value=""></option>
                                    <option value="">All</option>
                                    <?php
                                    $mown = mysqli_query($conn, "SELECT grpcode, `name` FROM `grpmast` ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mown)) {
                                        echo "<option value=$trow[grpcode]>$trow[name]</option>";
                                    }
                                    ?>  

                                </select>
                                <input type="text" id="custhidden" hidden>
                            </div>                            
                        </div>
                    </div>
                </div>
                <div class="row" style="overflow-y:scroll;height: 300px">
                    <div style="overflow: auto; border: 0px ;">
                        <div style="float:left;">
                            <div id='data' style="margin-right: 47px;margin-left: 41px;"></div>                  
                        </div> 
                    </div>
                </div>
                <div style="overflow: auto; ">
                    <table class="table-condensed" style="margin: 0 auto;">
                        <tr>
                            <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                                <button id='submit' type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                                    <i class="fa fa-check" style="color:#ffffff"></i>
                                </button>
                            </td>
                            <td class=" buttonform tooltip-bottom" id="hexit" style="display: <? Echo $exitdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Exit">
                                <button id='exit' type="button" class="btn btn-pad btn-square " style="background-color:#cccccc; "  onClick="window.location.href = '<?= 'openbal.php' ?>'">
                                    <i class="fa fa-power-off" style="color:red"></i>
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </form> 
        </div>
    </div>
</div>            
<?php
include("includes/linksfoot.php");
//include("includes/valid.php");
?>
</body>
</html>
