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

$formname = 'Other Accounts';
$maindb = 'accmast';
$file = 'otherac.php';
$grid = '';
$keyfld = "accode";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'othlst.php';
//$grpcode = "001";
//$keycond = " AND `grpcode` = '001' ";
$myyear = $_SESSION[$myyear];

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

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $username = $_SESSION["username_" . $dbname];

    $mgrpcode = $_POST["group"];
    $name = $_POST["name"];
    $add1 = mysqli_real_escape_string($conn, $_POST["add1"]);
    $add2 = mysqli_real_escape_string($conn, $_POST["add2"]);
    $add3 = mysqli_real_escape_string($conn, $_POST["add3"]);
    $city = $_POST["city"];
    $pin = $_POST["pin"];
    $state = $_POST["state"];
    $statecode = $_POST["statecode"];
    $coun = $_POST["country"];
    $oms = $_POST["oms"];
    $mob = $_POST["mobile"];
    $tel = $_POST["tel"];
    $fax = $_POST["fax"];
    $email = $_POST["email"];
    $web = $_POST["web"];
    $cpers1 = $_POST["contpers"];
    $vat = $_POST["vattin"];
    $cst = $_POST["csttin"];
    $pan = $_POST["pan"];
    $openbal = $_POST["openbal"];
    $opendc = $_POST["dc"];
    $depric = $_POST["depric"];

    $mvouch = $qvouch;
    $increcode = $myyear . $mvouch;


    if ($name == " ") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=true");
        }
    }

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND accode != '$mvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
        }
    }


    if ($a == 1) {

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) 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';
            }
        }
        $qrygrp = mysqli_query($conn, "SELECT * FROM `grpmast` WHERE `grpcode`= '$mgrpcode'") or die(mysqli_error($conn));
        $getgrp = mysqli_fetch_object($qrygrp);
        $init = $getgrp->initial;

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(accode, `username`, `name`, `add1`, `add2`, `add3`, `city`, `pin`, `state`, `statecode`, `country`,
                                                                     mobile ,tel, fax, `email`,`website`, `contpers`, `vattin`, `csttin`, `pan`, openbal, `dc`, `oms`,
                                                                     `grpcode`, `init`, depric,gstno)
                                                              VALUES('$qvouch', '$username', '$name', '$add1', '$add2', '$add3', '$city', '$pin', '$state', '$statecode', '$coun',
                                                                    '$mob', '$tel', '$fax', '$email', '$web', '$cpers1', '$vat', '$cst', '$pan', '$openbal', '$opendc', '$oms',
                                                                    '$mgrpcode', '$init', '$depric','$cst')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=last");
        } else {
            $mvouch = $qvouch;

            mysqli_query($conn, "UPDATE `$maindb` SET `username`='$username', `name`='$name', `init`='$init', `add1`='$add1', `add2`='$add2', `add3`='$add3',
                                                                    `city`='$city', `pin`='$pin', `state`='$state', `statecode`='$statecode', `country`='$coun', mobile= '$mob',
                                                                    `tel`='$tel', `fax`='$fax', mobile='$mob', `email`='$email', `website`='$web', `contpers`='$cpers1', `vattin`='$vat',
                                                                    `csttin`='$cst', gstno='$cst', `pan`='$pan', openbal='$openbal', `dc`='$opendc', `grpcode`='$mgrpcode', depric='$depric', `oms`='$oms' 
                                                                    WHERE accode = '$mvouch' ") or die(mysqli_error($conn));

            updmess();

//            header("refresh: 1; $file?vouchno=$mvouch");
//            header("refresh: 1; $grid");
        }
    }
}

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);

    $gethead = mysqli_query($conn, "SELECT * FROM `grpmast` WHERE grpcode='$get[grpcode]'")or die(mysqli_error($conn));
    $head = mysqli_fetch_array($gethead);
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function () {
        $("body").on('keyup, change', '.dc', validation);
        $("body").on("change", ".state", getStateCode);

        function validation() {
            var val = $(".dc").val();
            if (val != 'C' && val != 'D') {
                $(".dc").val("");
                $(".openbal").val("");
                alert("Only D or C should be entered");
            }
        }

        var addflg = $(".addflg").val();
        var maccode = $(".accode").val();

        if (addflg != 1){
        if (maccode > 50){
            $("#hdelete").show();
            $("#hedit").show();
        }else{
            $("#hedit").hide();
            $("#hdelete").hide();
        }
        }

    function getStateCode() {
        var state = $(".state").val();
        $.get('get_stcode.php', {state: state}, function (data) {
            $(".statecode").val(data);

            if (data != 27) {
                $(".oms").prop("checked", true);
            }else{
                $(".oms").prop("checked", false);
            }
    
        });

    }


    });
</script>

</head>
<body>
    <form action='' method='POST'>
        <div class="row">
            <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body" style="margin-top: 10x">
                            <!--<input type="hidden" size="6" maxlength="6" id="mvouch" maxlength="6" name="mvouch" value="<?php // echo ($addflag != 1) ? $get['vouchno'] : $qvouch;             ?>" />-->      
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-7 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form focusfield" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required autofocus>
                                <input type="hidden" class="form-control form accode"  value="<?php Echo ($addflag != 1) ? $get["accode"] : "$qvouch"; ?>">
                                <input type="hidden" class="form-control form addflg"  value="<?php Echo $addflag; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Group</label>
                            </div>
                            <div class="col-lg-7 col-md-6 col-sm-6 col-xs-6 pad"> 
                                <select name="group" class="form-control form" style="height:33px;" required>
                                    <option value="<?php echo ($addflag != 1) ? $head['grpcode'] : ""; ?>"><?php echo ($addflag != 1) ? $head['name'] : ""; ?></option>
                                    <?php
                                    $getgrp = mysqli_query($conn, "SELECT grpcode, `name` FROM `grpmast` WHERE `name`!='' AND `grpcode`!='001' AND `grpcode`!='002' AND `grpcode`!='009' AND `grpcode`!='010' ORDER BY `name`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_object($getgrp)) {
                                        echo "<option value='$row->grpcode'>$row->name</option>";
                                    }
                                    ?>
                                </select>
                            </div> 
                        </div> 
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Address</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="add1" value="<?php Echo ($addflag != 1) ? $get["add1"] : ""; ?>" >
                                <input type="text" class="form-control form" name="add2" value="<?php Echo ($addflag != 1) ? $get["add2"] : ""; ?>" >
                                <input type="text" class="form-control form" name="add3" value="<?php Echo ($addflag != 1) ? $get["add3"] : ""; ?>" >
                            </div>
                        </div> 
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">City</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad"> 
                                <input type="text" size="40" name="city" class="form-control form" value="<?php echo ($addflag != 1) ? $get['city'] : ""; ?>">
                            </div> 
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Pin</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-7 col-xs-7 pad">
                                <input type="text" maxlenght="8" size="8" name="pin" class="form-control form" value="<?php echo ($addflag != 1) ? $get['pin'] : ""; ?>">
                            </div>
                        </div> 

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">State</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select name="state" class="form-control form state select2" >
                                    <option value="<?php echo ($addflag != 1) ? $get['state'] : ""; ?>"><?php echo ($addflag != 1) ? $get['state'] : " "; ?></option>
                                    <?php
                                    $mown4 = mysqli_query($conn, "SELECT `code`, `name` FROM `state` ORDER BY name") or die(mysqli_error($conn));
                                    while ($trow4 = mysqli_fetch_array($mown4)) {
                                        echo "<option value='$trow4[name]'>$trow4[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">State Code</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" class="form-control form number statecode"  name="statecode" value="<?php echo ($addflag != 1) ? $get["statecode"] : ""; ?>" readonly>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Country</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad"> 
                                <input type="text" size="40" name="country" class="form-control form" value="<?php echo ($addflag != 1) ? $get['country'] : ""; ?>"/>
                            </div> 
                        </div> 
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" size="40" name="mobile" class="form-control form" value="<?php echo ($addflag != 1) ? $get['mobile'] : ""; ?>" />
                            </div> 
                        </div> 
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Telephone</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="text" size="40" name="tel" class="form-control form" value="<?php echo ($addflag != 1) ? $get['tel'] : ""; ?>" />
                            </div> 
                        </div> 
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">E-Mail</label>
                            </div>
                            <div class="col-lg-7 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="email" size="40" name="email" class="form-control form" value="<?php echo ($addflag != 1) ? $get['email'] : ""; ?>"/>
                            </div> 
                        </div> 
                    
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Contact Person</label>
                            </div>
                            <div class="col-lg-7 col-md-8 col-sm-8 col-xs-8 pad"> 
                                <input type="text" size="40" name="contpers" class="form-control form" value="<?php echo ($addflag != 1) ? $get['contpers'] : ""; ?>"/>
                            </div> 
                        </div> 
                    
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">GST No.</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad"> 
                                <input type="text" size="40" name="csttin" class="form-control form" value="<?php echo ($addflag != 1) ? $get['csttin'] : ""; ?>"/>
                            </div> 
                        </div> 

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Pan No.</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad"> 
                                <input type="text" size="40" name="pan" class="form-control form" value="<?php echo ($addflag != 1) ? $get['pan'] : ""; ?>"/>
                            </div> 
                        </div> 

                        
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Opening Balance</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"> 
                                <input type="number" style="text-align: right;" step="any" name="openbal" autofocus="" placeholder="Openbal" value="<?php echo ($addflag != 1) ? $get['openbal'] : ''; ?>" class="form-control form openbal">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" name="dc" value ="<?php echo ($addflag != 1) ? $get['dc'] : "D"; ?>" class="form-control form dc">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Out State</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox" class="form-control form oms" <?php
                                                                                        if ($get['oms'] == "1") {
                                                                                            echo "checked";
                                                                                        }
                                                                                        ?> value="1" name="oms" onclick="return false;">
                            </div>
                        </div>

<!--                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Depriciation</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"> 
                                <input type="number" style="text-align: right;" step="any" name="depric" placeholder="" value="<?php echo ($addflag != 1) ? $get['depric'] : ''; ?>" class="form-control form depric">
                            </div>
                        </div>-->

                    </div>
                    <div class="box-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <!--SEARCH PART-->

    <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 table-bordered">
                            <thead>
                                <tr style="" class="mySearchTr">
                                    <th>Name</th>
                                    <th>Address</th>
                                    <th>station</th>
                                    <th>Mobile</th>
                                    <th>Email</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`  WHERE `grpcode`> '002'") or die(mysqli_error($conn));
                                while ($row = mysqli_fetch_array($srchresult)) {

                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                    echo "<td align='left'>$row[name]</td>";
                                    echo "<td align='left'>$row[add1]</td>";
                                    echo "<td align='left'>$row[station]</td>";
                                    echo "<td align='left'>$row[mobile]</td>";
                                    echo "<td align='left'>$row[email]</td>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$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>        
