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

$formname = 'Karigar';
$maindb = 'accmast';
$file = 'karigar.php';
$grid = '';
$grp = '006';
$keyfld = "accode";
$keycond = " AND `grpcode` = '006' ";
$trandbexist = 0;
$focusfld = "name";
$printfile = 'karlst.php';

$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;
    $name = $_POST["name"];
    $add1 = $_POST["add1"];
    $add2 = $_POST["add2"];
    $add3 = $_POST["add3"];
    $city = $_POST["city"];
    $pin = $_POST["pin"];
    $station = $_POST["station"];
    $country = $_POST["country"];
    $tel = $_POST["tel"];
    $mobile = $_POST["mobile"];
    $mobile2 = $_POST["mobile2"];
    $fax = $_POST["fax"];
    $email = $_POST["email"];
    $website = $_POST["website"];
    $contpers = $_POST["contpers"];
    $vattin = $_POST["vattin"];
    $csttin = $_POST["csttin"];
    $openbal = $_POST["openbal"];
    $dc = $_POST["dc"];
    $discount = $_POST["discount"];
//    $mgrpcode = '010';
    $mvouch = $qvouch;

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

    if (isset($_POST["accode"])) {
        $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `accode`= '$code' AND accode != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result1) > 0) {
            $a1 = 0;
            browse("Code Already Exist");
            header("refresh: ; $file?$keyfld=$_GET[$keyfld]");
        }
    }
    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");
            header("refresh: ; $file?$keyfld=$_GET[$keyfld]");
        }
    }


    if ($a == 1) {
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`( accode, `name`, `add1`, `add2`, `add3`,`city`, `pin`, `station`, `country`,`tel`, `mobile`,
                    `mobile2`, `fax`,`email`, `website`, `contpers`, `vattin`,`csttin`, `openbal`, `dc`, `discount`,`grpcode`)
                    Values ('$qvouch','$name', '$add1', '$add2','$add3','$city', '$pin','$station','$country', '$tel','$mobile',
                    '$mobile2', '$fax','$email','$website', '$contpers','$vattin','$csttin', '$openbal','$dc','$discount',
                    '$grp')") or die(mysqli_error($conn));
            insmess();
//            $mvouch = $qvouch;
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            mysqli_query($conn, "UPDATE `$maindb` SET  `name`='$name', `add1`='$add1',`add2`='$add2' ,`add3`='$add3',  `city`='$city', 
                                `pin`='$pin',`station`='$station',`country`='$country',  `tel`='$tel', `mobile`='$mobile',
                                `mobile2`='$mobile2' ,`fax`='$fax',  `email`='$email', `website`='$website',`contpers`='$contpers' ,
                                `vattin`='$vattin',  `csttin`= '$csttin', openbal='$openbal', `dc`='$dc', `discount` ='$discount'
                                 where accode = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE grpcode = '$grp' AND $keyfld='$qvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}


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

<body>
    <form method="POST" class="form-horizontal" role="form" action='' name="customer">
        <div class="row" style="">
            <div class="col-lg-6" style=''>
                <div class="box dark">
                    <header>
                        <div class="icons"><i class="fa fa-edit"></i></div>
                        <h5 style="font-family: cipher-regular,helvetica; "><?= $formname ?></h5>
                        <!-- .toolbar -->
                        <div class="toolbar">
                            <ul class="nav">
                                <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" style="height:580px; font-family: cipher-regular,helvetica;">
                        <div class="form-horizontal"><br>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;"> Name </label>
                                <div class="col-lg-9">
                                    <input type="text" name="name" id='name' autofocus="" placeholder="Name" value="<?php echo ($addflag != 1) ? $get['name'] : ''; ?>" style="height: 30px;" class="form-control form"  required="">                                    
                               </div> 
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;"> Address </label>
                                <div class="col-lg-9"> 
                                    <input type="text" name="add1"   autofocus="" placeholder="Address1" value="<?php echo ($addflag != 1) ? $get['add1'] : ''; ?>" style="height: 30px;" class="form-control form">
                                    <input type="text" name="add2"   autofocus="" placeholder="Address2" value="<?php echo ($addflag != 1) ? $get['add2'] : ''; ?>" style="height: 30px;" class="form-control form">
                                    <input type="text" name="add3"  autofocus="" placeholder="Address3" value="<?php echo ($addflag != 1) ? $get['add3'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">City </label>
                                <div class="col-lg-5"> 
                                    <input type="text" name="city"   autofocus="" placeholder="City" value="<?php echo ($addflag != 1) ? $get['city'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div>
                                <label class="control-label col-lg-4"  style="width: 50px;font-family: cipher-regular,helvetica;">Pin </label>
                                <div class="col-lg-3"> 
                                    <input type="text" name="pin"  autofocus="" placeholder="Pin" value="<?php echo ($addflag != 1) ? $get['pin'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Station  </label>
                                <div class="col-lg-5"> 
                                    <input type="text" name="station"  autofocus="" placeholder="Station" value="<?php echo ($addflag != 1) ? $get['station'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Country/Region </label>
                                <div class="col-lg-5"> 
                                    <input type="text" name="country"  autofocus="" placeholder="Country/Region" value="<?php echo ($addflag != 1) ? $get['country'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Tel  </label>
                                <div class="col-lg-5"> 
                                    <input type="text" name="tel"  autofocus="" placeholder="Tel" value="<?php echo ($addflag != 1) ? $get['tel'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Mobile 1  </label>
                                <div class="col-md-3"> 
                                    <input type="number" name="mobile"  autofocus="" placeholder="Mobile no.1" value="<?php echo ($addflag != 1) ? $get['mobile'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                                <label class="control-label col-lg-4"  style="width: 103px;font-family: cipher-regular,helvetica;"> Mobile 2 </label>
                                <div class="col-md-3"> 
                                    <input type="number" name="mobile2"  autofocus="" placeholder="Mobile no.2" value="<?php echo ($addflag != 1) ? $get['mobile2'] : ''; ?>" style="height: 30px; width: 142px;" class="form-control form">
                                </div> 
                            </div>  
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Fax </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="fax"  autofocus="" placeholder="Fax" value="<?php echo ($addflag != 1) ? $get['fax'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Email </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="email"  autofocus="" placeholder="Email" value="<?php echo ($addflag != 1) ? $get['email'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Website </label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="website"  autofocus="" placeholder="Website" value="<?php echo ($addflag != 1) ? $get['website'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Contact Person</label>
                                <div class="col-lg-8"> 
                                    <input type="text" name="contpers"  autofocus="" placeholder="Contact person" value="<?php echo ($addflag != 1) ? $get['contpers'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;"> VAT TIN NO </label>
                                <div class="col-lg-5"> 
                                    <input type="text" name="vattin"  autofocus="" placeholder="Vattin.no" value="<?php echo ($addflag != 1) ? $get['vattin'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">CST TIN NO </label>
                                <div class="col-lg-5"> 
                                    <input type="text" name="csttin"  autofocus="" placeholder="Csttin" value="<?php echo ($addflag != 1) ? $get['csttin'] : ''; ?>" style="height: 30px;" class="form-control form">
                                </div> 
                            </div>
                            </div><!---TESTING-->
                        <div class="form-group">
                            <label class="control-label col-lg-4" id="name" style="width: 150px;font-family: cipher-regular,helvetica;">Opening Balance </label>
                                <div class="col-md-3"> 
                                    <input type="number" name="openbal" autofocus="" placeholder="Openbal" value="<?php echo ($addflag != 1) ? $get['openbal'] : ''; ?>" style=" width:180px; height: 30px; margin:0px;" class="form-control form">
                                </div>
                                <div class="col-md-3">
                                    <input type="text" name="dc" value ="<?php echo ($addflag == 1) ? $get['dc'] : "D"; ?>" style=" width:50px; height: 30px;" class="form-control form">                                
                                </div>  
                            </div>
                            <div class="form-group">
                                <label class="control-label col-lg-4"  style="width: 150px;font-family: cipher-regular,helvetica;">Discount </label>
                                <div class="col-lg-5"> 
                                    <input type="number" name="discount"  autofocus="" placeholder="Discount" value="<?php echo ($addflag != 1) ? $get['discount'] : ''; ?>" style="height: 30px; width:75px;" class="form-control form">
                                </div> 
                            </div>
                        
                        
                        </div>
                    <div id="formfoot" style="width:100%;">                        
            <h1><?php include("includes/buttons.php"); ?></h1>
        </div>
                    </div> 
                </div>
            
            </div> 
        
         

        <!--SEARCH PART-->

        <div class="modal fade in slacker-modal" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
            <div class="modal-dialog modal-slacker">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myLargeModalLabel"></h4>
                    </div>
                    <div class="modal-body" style="height: 100%;white-space: nowrap;">
                        <div class="row">

                            <div class="col-md-12">

                                <div class="col-lg-12">

                                    <div class="box">
                                        <div id="collapse4" class="body">
                                            <table id="dataTable" class="table table-bordered table-condensed table-hover table-striped">
                                                <thead>
                                                    <tr>
                                                        <th align='left'>Name</th>
                                                        
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld!='' $keycond ") or die(mysqli_error($conn));
                                                    while ($row = mysqli_fetch_array($srchresult)) {
                                                        echo "<tr>";
                                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\">$row[name]</a></td>";
                                                        echo "</tr>";
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                            <script>
                                                $(function () {
                                                    metisTable();
                                                    metisSortable();
                                                });
                                            </script>

                                        </div>

                                    </div>

                                </div>

                            </div><!-- /.row -->

                        </div>
                    </div>  
                    <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                        <!--"This is where i want to load my salemain.php"-->
                    </div>
                </div>
            </div>
        </div>
    </form>

    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>