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

$formname = 'Opening Balance ';
$maindb = 'accmast';
$file = 'openbal.php';

$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");
}
?>

<?php 
include("includes/files.php");
//include("includes/combo2.php");
?>
        <script>
            $(document).ready(function() {
                $('#data').on('keyup', '.cal2', calculateSum2);
                $('#data').on('keyup', '.cal', 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 sum = 0;
                    $(".cal").each(function() {
                        if (!isNaN(this.value) && this.value.length != 0) {
                            sum += parseFloat(this.value);
                        }
                    });
                    $("#totdebit").val(sum.toFixed(2));

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

                    if (mname2 > 0) {
                        $($row.find('.cal2')).removeAttr("readonly");
                        $($row.find('.cal')).attr("readonly", true);
                    }

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

                function calculateSum2() {
                    var sum = 0;
                    $(".cal2").each(function() {
                        if (!isNaN(this.value) && this.value.length != 0) {
                            sum += parseFloat(this.value);
                        }
                    });
                    $("#totcredit").val(sum.toFixed(2));
                    var $row = $(this).closest("tr");
                    var mname = $row.find('.cal').val();
                    var mname2 = $row.find('.cal2').val();

                    if (mname > 0) {
                        $($row.find('.cal')).removeAttr("readonly");
                        $($row.find('.cal2')).attr("readonly", true);
                   }

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

                    if (mname2 == "0") {
                        $($row.find('.cal')).removeAttr("readonly");
                        $($row.find('.cal2')).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>
    </head>
    <body >
        <!--BEGIN INPUT TEXT FIELDS-->
        <form action=''  name="invoice" method="POST">
        <div class="row">
            <div class="col-lg-12" style='width:100%;float:left;'>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <!--  <li><a href="#">Link</a></li>
                                  <li class="dropdown">
                                      <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                                          <i class="fa fa-th-large"></i>
                                      </a>
                                      <ul class="dropdown-menu">
                                          <li><a href="">q</a></li>
                                          <li><a href="">a</a></li>
                                          <li><a href="">b</a></li>
                                      </ul>
                                  </li>-->
                                <li><input id="submit" type='submit' name='submit' id='save' value='Save' class="btn btn-primary"></li>
                                <li><input class="btn btn-danger" type="button" onClick="window.location.href = '<?= $grid ?>'" value="Exit" /></li>
                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>

                            </ul>
                        </div>
                        <!-- /.toolbar -->
                    </header>
                    <div id="div-1" class="accordion-body collapse in body">
                        <div class="form-horizontal">

                            <div class="form-group">
                                <label class="control-label col-lg-1">Group</label>

                                <div class="col-lg-3">
                                    <select id="cmbgrp" name="cmbgrp" style="height:35px;"  class="godown form-control">
                                        <option></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>
                            <!--<h4><b>Item List</b></h4>-->
                            <br/>
                        </div>
                    <!--</div>-->
<!--                </div>
            </div>-->
<!--            <div class="col-lg-6" style='width:100%;float:left;'>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5>Item List</h5>
                         .toolbar 
                        <div class="toolbar">
                            <ul class="nav">


                                <li>
                                    <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-2">
                                        <i class="fa fa-chevron-up"></i>
                                    </a>
                                </li>
                            </ul>
                        </div>
                         /.toolbar 
                    </header>-->
                    <!--<div id="div-2" class="accordion-body collapse in body">-->
                        <div style="overflow: auto; border: 0px solid;">
                            <div style="float:left;">
                                <div id='data'></div>                  
                            </div> 
                        </div> 

                    </div>
                </div>        
            </div>    
        </div>
        </form>
        <?php include("includes/files_footer.php") ?>

   </body>
</html>
