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

$formname = "Company Setup";
$grid = 'menu.php';
if (isset($_GET["mode"]) == "true") {
    $addflag = 1;
    $gen1 = mysqli_query($conn, "SELECT MAX(cono) AS maxcode FROM company 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';
    }
} else {
    $addflag = 0;
    $qvouch = $_GET["vouchno"];
}

$mvouch = $qvouch;


if (isset($_POST["submit"])) {
    mysqli_query($conn, "LOCK TABLES `company` WRITE") OR die(mysqli_error($conn));

    $a = 1;
    $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']);
    $state = mysqli_real_escape_string($conn, $_POST['state']);
    $statecode = mysqli_real_escape_string($conn, $_POST['statecode']);
    $city = mysqli_real_escape_string($conn, $_POST['city']);
    $pin = mysqli_real_escape_string($conn, $_POST['pin']);
    $mob = $_POST['mobile'];
    $mob2 = $_POST['mobile2'];
    $contpers = $_POST['contpers'];
    $tel = $_POST['tel'];
    $fax = $_POST['fax'];
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $bank = mysqli_real_escape_string($conn, $_POST['bank']);
    $branch = mysqli_real_escape_string($conn, $_POST['branch']);
    $acno = mysqli_real_escape_string($conn, $_POST['acno']);
    $ifsc = mysqli_real_escape_string($conn, $_POST['ifsc']);
    $bemail = mysqli_real_escape_string($conn, $_POST['ccemail']);
    $vatno = mysqli_real_escape_string($conn, $_POST['vattin']);
    $cstno = mysqli_real_escape_string($conn, $_POST['csttin']);
    $gstno = mysqli_real_escape_string($conn, $_POST['gstno']);
    $panno = mysqli_real_escape_string($conn, $_POST['pan']);
    $servno = mysqli_real_escape_string($conn, $_POST['servtax']);

    if ($a == 1){
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `company`(cono, `compname`, `add1`, `add2`, `add3`, `state`,`statecode`,`city`, pin, `tel`,`mobile`, `mobile2`, `contpers`,
                                              `fax`, `email`,`ccemail`,`vattinno`,`csttinno`,`gstno`,`servtaxno`,`panno`,`bank,`branch``,`acno`,`ifsc`)
                                         VALUES($qvouch, '$name', '$add1', '$add2', '$add3', '$state','$statecode','$city','$pin','$tel','$mob','$mob2', '$contpers',
                                               '$fax', '$email','$bemail' ,'$vatno', '$cstno','$gstno','$servno','$panno','$bank','$branch','$acno','$ifsc')") or die(mysqli_error($conn));
        } else {
           
            mysqli_query($conn, "UPDATE `company` SET `compname`='$name', `add1`='$add1', `add2`='$add2', `add3`='$add3',`state`='$state',`statecode`='$statecode',
                                `city` ='$city', pin='$pin', `tel` = '$tel', `mobile` = '$mob', `mobile2` = '$mob2', `contpers` = '$contpers', `fax`='$fax', `email`='$email',
                                `ccemail`='$bemail',`vattinno`='$vatno',`csttinno`='$cstno',`servtaxno`='$servno', 
                                `panno`='$panno' ,`bank`='$bank',`branch`='$branch',`acno`='$acno',`ifsc`='$ifsc',`gstno`='$gstno' WHERE cono = '$mvouch' ") or die(mysqli_error($conn));
        }
    }


    mysqli_query($conn, "UNLOCK TABLES") OR die(mysqli_error($conn));

    if ($addflag == 1) {
        insmess();
        header("refresh: 1; company.php?mode=true");
    } else {
        updmess();
        header("refresh: 1; company.php?vouchno=$mvouch");
    }
}

if ($addflag == 0) {
    $getvouch = $qvouch;
    $result = mysqli_query($conn, "SELECT * FROM `company` WHERE cono='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

include("includes/files.php");
?>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#email").blur(function() {
                    var email = $("#email").val();
                    if (email != 0) {
                        if (isValidEmailAddress(email)) {
                            $("#validEmail").css({
                                "background-image": "url('validYes.png')"
                            });
                            $('#save').attr('disabled', false);
                        }
                        else {
                            $("#validEmail").css({
                                "background-image": "url('validNo.png')"
                            });
                            alert("INVALID Email");
                            $('#save').attr('disabled', true);
                            $('#email').focus();
                        }
                    }
                    else {
                        $("#validEmail").css({
                            "background-image": "none"
                        });
                    }
                });
            });
            function isValidEmailAddress(emailAddress) {
                var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                return pattern.test(emailAddress);
            }
        </script>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#ccemail").blur(function() {
                    var email = $("#ccemail").val();
                    if (email != 0) {
                        if (isValidEmailAddress(email)) {
                            $("#validEmail2").css({
                                "background-image": "url('validYes.png')"
                            });
                            $('#save').attr('disabled', false);
                        }
                        else {
                            $("#validEmail2").css({
                                "background-image": "url('validNo.png')"
                            });
                            alert("INVALID Cc-email");
                            $('#save').attr('disabled', true);
                            $('#ccemail').focus();
                        }
                    }
                    else {
                        $("#validEmail2").css({
                            "background-image": "none"
                        });
                    }
                });
            });
            function isValidEmailAddress(emailAddress) {
                var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
                return pattern.test(emailAddress);
            }
        </script>


    </head>
    <body onLoad="document.boq.vouchdt.focus();">
        <form action=''  name="customer" method="POST">
            <div class="row">
                <div class="col-lg-6" style='width:50%;float:center;'>
                    <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><input 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 = ''" 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-4"> Name </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" maxlength="40" id='name' name="name" value="<?php echo ($addflag == 0) ? $get["compname"] : ""; ?>" class="form-control" required autofocus>
                                    </div> 
                                </div> 
                                <div class="form-group">
                                    <label class="control-label col-lg-4"> Address </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" maxlength="40" id='name' name="add1" value="<?php echo ($addflag == 0) ? $get["add1"] : ""; ?>" class="form-control" >
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4"> </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" maxlength="40" id='name' name="add2" value="<?php echo ($addflag == 0) ? $get["add2"] : ""; ?>" class="form-control" >
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4"> </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" maxlength="40" id='name' name="add3" value="<?php echo ($addflag == 0) ? $get["add3"] : ""; ?>" class="form-control">
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">State</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" maxlength="25" name="state" class="form-control" style="width: 50%;" value="<?php echo ($addflag == 0) ? $get['state'] : "MUMBAI"; ?>" />


                                    </div> 
                                </div> 
                                <div class="form-group">
                                    <label class="control-label col-lg-4"> State Code</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" maxlength="25" name="statecode" class="form-control" style="width: 50%;" value="<?php echo ($addflag == 0) ? $get['statecode'] : "MUMBAI"; ?>" />


                                    </div> 
                                </div> 
                                <div class="form-group">
                                    <label class="control-label col-lg-4"> City </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" maxlength="25" name="city" class="form-control" style="width: 50%;" value="<?php echo ($addflag == 0) ? $get['city'] : "MUMBAI"; ?>" />


                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4"> Pin </label>
                                    <div class="col-lg-8"> 

                                        <input type="text" maxlength="8" size="8" name="pin" class="form-control" style="width: 80px;" value="<?php echo ($addflag == 0) ? $get['pin'] : ""; ?>" />

                                    </div> 
                                </div> 
                               
                                <div class="form-group">
                                    <label class="control-label col-lg-4"> Telephone </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="tel" maxlength="40" class="form-control" value="<?php echo ($addflag == 0) ? $get['tel'] : ""; ?>" />
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4"> Fax </label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="fax" maxlength="40" class="form-control" value="<?php echo ($addflag == 0) ? $get['fax'] : ""; ?>"/>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4"> Email</label>
                                    <div class="col-lg-8"> 
                                        <input type="email" size="40" id="email" name="email" maxlength="40" class="form-control" value="<?php echo ($addflag == 0) ? $get['email'] : ""; ?>" /><span id="validEmail"></span>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">Cc Email</label>
                                    <div class="col-lg-8"> 
                                        <input type="email" size="40" id="ccemail" name="ccemail" maxlength="40" class="form-control" value="<?php echo ($addflag == 0) ? $get['ccemail'] : ""; ?>" /><span id="validEmail2"></span>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">VAT TIN</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="vattin" class="form-control" value="<?php echo ($addflag == 0) ? $get['vattinno'] : ""; ?>"/>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">CST TIN</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="csttin" class="form-control"  value="<?php echo ($addflag == 0) ? $get['csttinno'] : ""; ?>"/>
                                    </div> 
                                </div> 
                                <div class="form-group">
                                    <label class="control-label col-lg-4">GST NO</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="gstno" class="form-control"  value="<?php echo ($addflag == 0) ? $get['gstno'] : ""; ?>"/>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">Pan No.</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="pan"  class="form-control" value="<?php echo ($addflag == 0) ? $get['panno'] : ""; ?>"/>
                                    </div> 
                                </div> 

                                <div class="form-group">
                                    <label class="control-label col-lg-4">Service Tax No.</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="servtax" class="form-control" value="<?php echo ($addflag == 0) ? $get['servtaxno'] : ""; ?>"/>
                                    </div>
                                </div> 

                                 <div class="form-group">
                                    <label class="control-label col-lg-4">Contact Person</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="contpers" class="form-control" value="<?php echo ($addflag == 0) ? $get['contpers'] : ""; ?>" />
                                    </div> 
                                </div> 
                                
                                 <div class="form-group">
                                    <label class="control-label col-lg-4">Mobile</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" maxlength="15" name="mobile" class="form-control" style="width: 40%;" value="<?php echo ($addflag == 0) ? $get['mobile'] : ""; ?>" />
                                        
                                    </div> 
                                </div> 
                                
                                <div class="form-group">
                                    <label class="control-label col-lg-4">Mobile 2</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" maxlength="15" name="mobile2" class="form-control" style="width: 40%;" value="<?php echo ($addflag == 0) ? $get['mobile2'] : ""; ?>" />

                                    </div> 

                                </div> 
                                <div class="form-group">
                                    <label class="control-label col-lg-4">Bank</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="bank" class="form-control" value="<?php echo ($addflag == 0) ? $get['bank'] : ""; ?>" />
                                    </div> 
                                </div>  
                                <div class="form-group">
                                    <label class="control-label col-lg-4">BRANCH</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" size="40" name="branch" class="form-control" style="width: 50%;"  value="<?php echo ($addflag == 0) ? $get['branch'] : ""; ?>" />
                                    </div> 
                                </div>  
                                <div class="form-group">
                                    <label class="control-label col-lg-4">Account No</label>
                                    <div class="col-lg-8"> 
                                        <input type="text" maxlength="25" name="acno" class="form-control" style="width: 50%;" value="<?php echo ($addflag == 0) ? $get['acno'] : ""; ?>" />

                                    </div> 

                                </div> 
                                <div class="form-group">
                                    <label class="control-label col-lg-4">IFSC Code</label>
                                    <div class="col-lg-8"> 
                                        <input type="text"  name="ifsc" maxlength="25" class="form-control" style="width: 50%;" value="<?php echo ($addflag == 0) ? $get['ifsc'] : ""; ?>" />

                                    </div> 

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



        </form>
        <?php
include("includes/files_footer.php");
?>
<!--        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
        <script src="js/chosen.jquery.js" type="text/javascript"></script>
        <script type="text/javascript"> $(".chzn-select").chosen();
            $(".chzn-select-deselect").chosen({allow_single_deselect: true});</script>-->

    </body>
</html>
