<?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";


$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 = mysqli_real_escape_string($conn, $_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"]);
    $station = $_POST["station"];
    $pin = $_POST["pin"];
    $coun = $_POST["country"];
    $mob = $_POST["mobile"];
    $tel = $_POST["tel"];
    $fax = $_POST["fax"];
    $email = $_POST["email"];
    $web = $_POST["web"];
    $cpers1 = $_POST["contpers"];
    $gstno = $_POST["gstno"];
    $vat = $_POST["vattin"];
    $cst = $_POST["csttin"];
    $pan = $_POST["pan"];
    $openbal = $_POST["openbal"];
    $opendc = $_POST["dc"];

    // browse($qvouch);

    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 != '$qvouch'") 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` WHERE $keyfld !='' 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`, `station`, `pin`, `country`, mobile ,tel,
                                                fax, `email`,`website`, `contpers`, `vattin`, `gstno`, `csttin`, `pan`, openbal, `dc`,
                                                `grpcode`, `init`)
                                        VALUES($qvouch, '$username', '$name', '$add1', '$add2', '$add3', '$station', '$pin', '$coun', '$mob' , '$tel',
                                               '$fax', '$email', '$web', '$cpers1', '$vat', '$gstno', '$cst', '$pan', '$openbal', '$opendc',
                                               '$mgrpcode', '$init')") 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',`station`='$station', `pin`='$pin', `country`='$coun', mobile= '$mob',tel='$tel', 
                         fax='$fax', mobile='$mob', `email`='$email', `website`='$web', `contpers`='$cpers1', `gstno`='$gstno', `vattin`='$vat', `csttin`='$cst', `pan`='$pan', openbal='$openbal', `dc`='$opendc',
                         `grpcode`='$mgrpcode' WHERE accode = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");

//            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' ") 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);
    $keycond = "&grpcode=$get[grpcode]";
}

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

        function validation() {
            var val = $(".dc").val();
            if (val != 'C' && val != 'D') {
                $(".dc").val("");
                $(".openbal").val("");
                alert("Only D or C should be entered");
            }
        }
    });
</script>
<script src="MyScript/otherac_search.js" type="text/javascript"></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>
                            </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' 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 address">
                                <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">Station</label>
                            </div>
                            <div class="col-lg-5 col-md-4 col-sm-7 col-xs-7 pad"> 
                                <input type="text" size="40" name="station" class="form-control form" value="<?php echo ($addflag != 1) ? $get['station'] : ""; ?>">
                            </div> 
                            <div class="col-lg-1 col-md-3 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-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 number" 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 number" 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">Website</label>
                            </div>
                            <div class="col-lg-7 col-md-8 col-sm-8 col-xs-8 pad"> 
                                <input type="text" size="40" name="web" class="form-control form" value="<?php echo ($addflag != 1) ? $get['website'] : ""; ?>"/>
                            </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-7 col-md-8 col-sm-8 col-xs-8 pad"> 
                                <input type="text" size="40" name="gstno" class="form-control form" value="<?php echo ($addflag != 1) ? $get['gstno'] : ""; ?>"/>
                            </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">VAT TIN No</label>
                            </div>
                            <div class="col-lg-7 col-md-8 col-sm-8 col-xs-8 pad"> 
                                <input type="text" size="40" name="vattin" class="form-control form" value="<?php echo ($addflag != 1) ? $get['vattin'] : ""; ?>"/>
                            </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">CST TIN No</label>
                            </div>
                            <div class="col-lg-7 col-md-8 col-sm-8 col-xs-8 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-7 col-md-8 col-sm-8 col-xs-8 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>
                    <div class="box-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <!--SEARCH PART-->
    <div class="modal fade bs-example-modal-lg searchModal" 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="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Name</option>
                                <option value="3">Address</option>
                                <option value="4">Mobile</option>                               
                                <option value="2">Group</option>
                                <option value="5">A/c. Code</option>

                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Name</option>
                                <option value="3">Address</option>
                                <option value="4">Mobile</option>
                                <option value="2">Group</option>
                                <option value="5">A/c. Code</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr style="background-color:#1ABC9C;"">
                                    <th width="35%">Name</th>
                                    <th width="20%">Address</th>
                                    <th width="10%">Mobile</th>
                                    <th width="25%">Group</th>
                                    <th width="10%">A/c. Code</th>
                                    <th width="5%">Action</th>

                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>

                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>        
