<?php   include_once('../config/db.php');   include_once('../config/check.php');  

if (filter_has_var(INPUT_POST, "submit")) 
    try{
            // $date =             htmlspecialchars (strtoupper($_POST['date']));
            // $date =             date("Y-m-d", strtotime($date));
                $status= 1;
                $branch_id                 =    htmlspecialchars ( $_POST['branch']);
                $group_id                  =    htmlspecialchars ( $_POST['group']);
                $group_loan_balance        =    htmlspecialchars ( $_POST['grouploanbalance']);
                $group_arrears             =    htmlspecialchars ( $_POST['grouploanarrears']);

                $client_id                      =  htmlspecialchars ( $_POST['client']);
                $client_loan_balance            =  htmlspecialchars ( $_POST['clientloanbalance']);
                $client_system_savings          =  htmlspecialchars ( $_POST['clientsystemsavings']);

                $product_id                      =  $_POST['item'];
                $quantity                        =  $_POST['unit'];
                $price                          =  $_POST['price'];
                $payable                        =  $_POST['payable'];
                $total_price                    =  htmlspecialchars ( $_POST['ordertotal']);
                $loan_term                      =  htmlspecialchars ( $_POST['loanmonths']);
                $delivery_method                =  htmlspecialchars ( $_POST['deliveryoption']);
                $delivery_location              =  htmlspecialchars ( $_POST['deliveryoptiondescription']);
                $created_by_user_id             = $uid;

                $number                         = count($_POST["item"]); 


                 $sql = 'INSERT INTO orders (  branch_id,client_id,client_loan_balance,
                                              client_system_savings,group_id,group_loan_balance,group_arrears,
                                              loan_term,total_price,status,delivery_method,delivery_location,created_by_user_id,uid ) 
                                    VALUES (  :branch_id,:client_id,:client_loan_balance,:client_system_savings,
                                              :group_id,:group_loan_balance,:group_arrears,:loan_term,:total_price,:status,
                                              :delivery_method,:delivery_location,:created_by_user_id,:suid )';

                 $conn->prepare($sql)->execute ([    'branch_id'=>$branch_id,'client_id'=>$client_id,'client_loan_balance'=>$client_loan_balance,
                                                     'client_system_savings'=>$client_system_savings,
                                                     'group_id'=>$group_id,'group_loan_balance'=>$group_loan_balance,'group_arrears'=>$group_arrears,
                                                     'loan_term'=>$loan_term,'total_price'=>$total_price,'status'=>$status,'delivery_method'=>$delivery_method,
                                                     'delivery_location'=>$delivery_location, 'created_by_user_id'=>$created_by_user_id,
                                                     'suid'=>$uid  ]);
                 $lastid = $conn->lastInsertId(); 


                 for($i=0; $i<$number; $i++)  
                    {  
                         $sql = 'INSERT INTO order_products(trans_id,product_id,quantity,price,total )
                                 VALUES( :trans_id,:product_id,:quantity,:price,:total )'; 

                         $stmt = $conn->prepare($sql)->execute([
                                            'trans_id' =>$lastid,'product_id' =>$product_id[$i],'quantity' =>$quantity[$i],
                                            'price'=>$price[$i],'total'=>$payable[$i]  ]);
                    }


            header("location:order.php");

        } catch(Exception $e) { echo 'Message: ' .$e->getMessage();}

?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('../inc/slink.php');   ?>  
</head>
<style>

     @import url('https://fonts.googleapis.com/css?family=Roboto');

body{
	/* font-family: 'Roboto', sans-serif; */
}
* {
	margin: 0;
	padding: 0;
}
i {
	margin-right: 10px;
}

/*------------------------*/
input:focus,
button:focus,
.form-control:focus{
	outline: none;
	box-shadow: none;
}
.form-control:disabled, .form-control[readonly]{
	background-color: #fff;
}
/*----------step-wizard------------*/
.d-flex{
	/* display: flex; */
}
.justify-content-center{
	justify-content: left;
}
.align-items-center{
	align-items: left;
}

/*---------signup-step-------------*/
.bg-color{
	background-color: #333;
}
.signup-step-container{
	padding: 00px 0px;
	padding-bottom: 0px;
}




    .wizard .nav-tabs {
        position: relative;
        margin-bottom: 0;
        border-bottom-color: transparent;
    }

    .wizard > div.wizard-inner {
        position: relative;
    }

.connecting-line {
    height: 2px;
    background: #e0e0e0;
    position: absolute;
    width: 75%;
    margin: 0 auto;
    left: 0;
    right: 0;
    top: 50%;
    z-index: 1;
}

.wizard .nav-tabs > li.active > a, .wizard .nav-tabs > li.active > a:hover, .wizard .nav-tabs > li.active > a:focus {
    color: #555555;
    cursor: default;
    border: 0;
    border-bottom-color: transparent;
}

span.round-tab {
    width: 30px;
    height: 30px;
    /* line-height: 30px; */
    display: inline-block;
    border-radius: 50%;
    background: #fff;
    z-index: 2;
    position: absolute;
    left: 0;
    text-align: center;
    font-size: 16px;
    color: #0e214b;
    font-weight: 500;
    border: 1px solid #ddd;
}
span.round-tab i{
    color:#555555;
}
.wizard li.active span.round-tab {
        background: #0db02b;
    color: #fff;
    border-color: #0db02b;
}
.wizard li.active span.round-tab i{
    color: #5bc0de;
}
.wizard .nav-tabs > li.active > a i{
	color: #0db02b;
}

.wizard .nav-tabs > li {
    width: 25%;
}

.wizard li:after {
    content: " ";
    position: absolute;
    left: 46%;
    opacity: 0;
    margin: 0 auto;
    bottom: 0px;
    border: 5px solid transparent;
    border-bottom-color: red;
    transition: 0.1s ease-in-out;
}



.wizard .nav-tabs > li a {
    width: 30px;
    height: 30px;
    margin: 20px auto;
    border-radius: 100%;
    padding: 0;
    background-color: transparent;
    /* position: relative; */
    top: 0;
}
.wizard .nav-tabs > li a i{
	position: absolute;
    top: -15px;
    font-style: normal;
    font-weight: 400;
    white-space: nowrap;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 12px;
    font-weight: 700;
    color: #000;
}

    .wizard .nav-tabs > li a:hover {
        background: transparent;
    }

.wizard .tab-pane {
    /* position: relative; 
    padding-top: 0px; */
}


.wizard h3 {
    margin-top: 0;
}
.prev-step,
.next-step{
    font-size: 13px;
    padding: 8px 24px;
    border: none;
    border-radius: 4px;
    margin-top: 15px;
}
.next-step{
	background-color: #0db02b;
}
.skip-btn{
	background-color: #cec12d;
}
.step-head{
    font-size: 20px;
    text-align: center;
    font-weight: 500;
    margin-bottom: 20px;
}
.term-check{
	font-size: 14px;
	font-weight: 400;
}
/* .custom-file {
    position: relative;
    display: inline-block;
    width: 100%;
    height: 40px;
    margin-bottom: 0;
}
.custom-file-input {
    position: relative;
    z-index: 2;
    width: 100%;
    height: 40px;
    margin: 0;
    opacity: 0;
}
.custom-file-label {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    z-index: 1;
    height: 40px;
    padding: .375rem .75rem;
    font-weight: 400;
    line-height: 2;
    color: #495057;
    background-color: #fff;
    border: 1px solid #ced4da;
    border-radius: .25rem;
}
.custom-file-label::after {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 3;
    display: block;
    height: 38px;
    padding: .375rem .75rem;
    line-height: 2;
    color: #495057;
    content: "Browse";
    background-color: #e9ecef;
    border-left: inherit;
    border-radius: 0 .25rem .25rem 0;
} */
.footer-link{
	margin-top: 10px;
}
.all-info-container{
     background-color: #0db02b;

}
.list-content{
	/* margin-bottom: 10px; */
}
.list-content a{
	/* padding: 10px 15px; */
    width: 100%;
    display: inline-block;
    background-color: #f5f5f5;
    position: relative;
    color: #565656;
    font-weight: 400;
    border-radius: 4px;
}
.list-content a[aria-expanded="true"] i{
	transform: rotate(180deg);
}
.list-content a i{
	text-align: right;
    position: absolute;
    top: 15px;
    right: 10px;
    transition: 0.5s;
}
.form-control[disabled], .form-control[readonly], fieldset[disabled] .form-control {
    background-color: #fdfdfd;
}
.list-box{
	padding: 10px;
}
.signup-logo-header .logo_area{
	width: 200px;
}
.signup-logo-header .nav > li{
	padding: 0;
}
.signup-logo-header .header-flex{
	display: flex;
	justify-content: center;
	align-items: center;
}
/*-----------custom-checkbox-----------*/
/*----------Custom-Checkbox---------*/
input[type="checkbox"]{
    position: relative;
    display: inline-block;
    margin-right: 5px;
}
input[type="checkbox"]::before,
input[type="checkbox"]::after {
    position: absolute;
    content: "";
    display: inline-block;   
}
input[type="checkbox"]::before{
    height: 16px;
    width: 16px;
    border: 1px solid #999;
    left: 0px;
    top: 0px;
    background-color: #fff;
    border-radius: 2px;
}
input[type="checkbox"]::after{
    height: 5px;
    width: 9px;
    left: 4px;
    top: 4px;
}
input[type="checkbox"]:checked::after{
    content: "";
    border-left: 1px solid #fff;
    border-bottom: 1px solid #fff;
    transform: rotate(-45deg);
}
input[type="checkbox"]:checked::before{
    background-color: #18ba60;
    border-color: #18ba60;
}






@media (max-width: 767px){
	.sign-content h3{
		font-size: 40px;
	}
	.wizard .nav-tabs > li a i{
		display: none;
	}
	.signup-logo-header .navbar-toggle{
		margin: 0;
		margin-top: 8px;
	}
	.signup-logo-header .logo_area{
		margin-top: 0;
	}
	.signup-logo-header .header-flex{
		display: block;
	}
}


</style>
<body <?php include_once('../inc/snav.php');  ?>>
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('../inc/sbrand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('../inc/snavbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('../inc/smenu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
        <section class="content-header" style="padding-bottom:0px;">
            <p style="color:green; font-size:20px; font-weight:bold; " > <i class="fa fa-plus-circle"></i>New Orders  </p>      
        </section>
<!-- Main content -->

  <section class="content container-fluid" style="padding-top:0px;" > 
        <div class="box box-default box-xs" >
            <div class="box-body">                              
                <section class="signup-step-container">
                    <div class="">
                        <!-- <div class="container"> -->
                        <div class="row  ">
                            <div class="col-md-12">
                                <div class="wizard">
                                    
                                    <div class="wizard-inner">
                                        <div class="connecting-line"></div>
                                        <ul class="nav nav-tabs" role="tablist">
                                            <li role="presentation" class="active">
                                                <a href="#step1" data-toggle="tab" aria-controls="step1" role="tab" aria-expanded="true"><span class="round-tab">1 </span> <i> Group  & Client</i></a>
                                            </li>
                                            <li role="presentation" class="disabled">
                                                <a href="#step2" data-toggle="tab" aria-controls="step2" role="tab" aria-expanded="false"><span class="round-tab">2</span> <i> Add Products </i></a>
                                            </li>
                                            <li role="presentation" class="disabled">
                                                <a href="#step3" data-toggle="tab" aria-controls="step3" role="tab"><span class="round-tab">3</span> <i> Delivery Options </i></a>
                                            </li>
                                            <li role="presentation" class="disabled">
                                                <a href="#step4" data-toggle="tab" aria-controls="step4" role="tab"><span class="round-tab">4</span> <i>Submit </i></a>
                                            </li>
                                        </ul>
                                    </div>
                                    <div class="col-md-1"></div>
                                    <div class="col-md-10">
                                    <form role="form" action="" method="POST" >                         
                                        <div class="tab-content" id="main_form">
                                            <div class="tab-pane active" role="tabpanel" id="step1">
                                                <!-- <h4 class="text-center">Step 1</h4> -->
                                                <div class="row">
                                                    <div class="col-md-3">
                                                        <div class="form-group">
                                                            <label> Select Branch * <? echo $UID;?></label> 
                                                            <select class="form-control js-example-basic-single   "  required=""  name="branch"  >
                                                            <?php $stmt = $conn->prepare("SELECT * FROM branches   ");
                                                                $stmt->execute(); foreach ($stmt as $row) {?>
                                                                <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                                            </select>

                                                            <!-- <label>First and Last Name *</label> 
                                                            <input class="form-control" type="text" name="name" required="" placeholder="">  -->
                                                        </div>
                                                    </div>
                                                    <div class="col-md-3">
                                                        <div class="form-group">
                                                            <label> Select Group *</label> 
                                                            <select class="form-control js-example-basic-single   "  required=""  name="group"  >
                                                            <?php $stmt = $conn->prepare("SELECT * FROM groups   ");
                                                                $stmt->execute(); foreach ($stmt as $row) {?>
                                                                <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                                            </select>

                                                            <!-- <label>First and Last Name *</label> 
                                                            <input class="form-control" type="text" name="name" required="" placeholder="">  -->
                                                        </div>
                                                    </div>
                                                    <div class="col-md-3">
                                                        <div class="form-group">
                                                            <label>Group Loan Balance (KES) </label> 
                                                            <input class="form-control" type="text" name="grouploanbalance" placeholder="Group Loan balance"> 
                                                        </div>
                                                    </div>
                                                    <div class="col-md-3">
                                                        <div class="form-group">
                                                            <label> Group Arrears (KES) * </label> 
                                                            <input class="form-control" type="text" name="grouploanarrears" placeholder=""> 
                                                        </div>
                                                    </div>
                                                    <!-- <hr style="border:solid;border-width:thin;border-color:lightgray;width:90%; margin: auto;"> -->
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label> Select Clients *</label> 
                                                            <select name="client" id="client" class="form-control js-example-basic-single ">
                                                                    <option value="">Select country first</option>
                                                                </select>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label> Client Loan balance *</label> 
                                                            <input class="form-control" type="text"  autocomplete="off" name="clientloanbalance" placeholder=" Client Loan Balance"> 
                                                        </div>
                                                    </div>
                                                    
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label> Client System Savings* </label> 
                                                            <input class="form-control" type="text"  autocomplete="off" name="clientsystemsavings" placeholder=""> 
                                                        </div>
                                                    </div>
                                                    
                                                    
                                                </div>
                                                <ul class="list-inline pull-right">
                                                    <li><button type="button" class="default-btn next-step" style="color:white;">Continue to Next Step <i class="fa fa-arrow-right"></i></button></li>
                                                </ul>
                                            </div>

                                            <!-- step 2 -->
                                                
                                            <div class="tab-pane" role="tabpanel" id="step2">
                                                <!-- <h4 class="text-center">Step 2</h4> -->
                                                    <!-- Search area-->
                                                    <div class="col-md-9">
                                                        <div class="col-md-12">
                                                            
                                                                    <div class="form-group">
                                                                        <!-- <label> SELECT PRODUCT  </label> -->
                                                                        <select class="form-control js-example-basic-single   "  required=""  name="product" class="product" >
                                                                        <?php $stmt = $conn->prepare("SELECT * FROM products   ");
                                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                            <option> </option>
                                                                            <option value="<?php echo $row['id']; ?>"><?php
                                                                                echo $row['name']; ?></option> <?php } ?>
                                                                        </select>
                                                                    </div>
                                                        

                                                            <!-- <div class="">
                                                            <label> Address 1 * </label> 
                                                            <input class="form-control" type="search" name="search" placeholder=""> 
                                                            </div> -->
                                                        </div>

                                                        
                                                        <!-- <div class="col-md-6">
                                                            <div class="form-group">
                                                            <label>City / Town *</label> 
                                                            <input class="form-control" type="text" name="name" placeholder=""> 
                                                            </div>
                                                        </div>
                                                        
                                                        <div class="col-md-6">
                                                            <div class="form-group">
                                                            <label>Registration No.</label> 
                                                            <input class="form-control" type="text" name="name" placeholder=""> 
                                                            </div>
                                                        </div> -->
                                                            <div class="col-md-12">                            
                                                                <table class="table table-bordered table-condesed table-sm table-condensed bg-default  item_table " id="item_table" >
                                                                            <tr  class="bg-primary">
                                                                            <th style="width:20%;">   Item</th>
                                                                            <!-- <th style="width:20%;">   Details   </th> -->
                                                                            <!-- <th style="width:10%;">   Uom  </th> -->
                                                                            <!-- <th style="width:10%;">   Availableunit  </th> -->
                                                                            <th style="width:20%;">   Unit  </th>
                                                                            <th style="width:13%;">   Price  </th>
                                                                            <!-- <th style="width:13%;">   Tax </th> -->
                                                                            <th style="width:17%;">  Total </th>                                                                      
                                                                            <th style="width:2%;" >      
                                                                                <button  type="button" class="btn btn-default btn-xs btn-block add" name="add">  <i class=" "></i> Action </button>
                                                                            </th>
                                                                        </tr>
                                                                </table>
                                                            </div>
                                                    </div>

                                                    <!-- Cart area -->
                                                    <div class="col-md-3">
                                                        <!-- <h3>  <i class="fa fa-shopping-cart"></i> Cart  </h3> -->
                                                        <!-- <h5> <span><input style="border:none;padding:0px;" readonly="true" autocomplete="off" name="orderitems" ></span><span> Items </span></h5>
                                                        -->
                                                        <label> Order Totals </label>
                                                        <input class="form-control" type="text" readonly="true" autocomplete="off" name="ordertotal" placeholder="">
                                                        
                                                        <label> Loan term  (Months) </label>
                                                        <input class="form-control" type="text"  autocomplete="off" name="loanmonths" >

                                                    </div>
                                                    
                                                    <!-- End button -->
                                                    <div class="col-md-12">
                                                        <ul class="list-inline pull-right">
                                                            <li><button type="button" class="default-btn prev-step"  > <i class="fa fa-arrow-left"></i> Back</button></li>
                                                            <!-- <li><button type="button" class="default-btn next-step skip-btn">Skip</button></li> -->
                                                            <li><button type="button" class="default-btn next-step" style="color:white;">Continue <i class="fa fa-arrow-right"></i></button></li>
                                                        </ul>
                                                    </div>
                                            </div>

                                            <!-- Step 3 -->
                                            <div class="tab-pane" role="tabpanel" id="step3">
                                                <!-- <h4 class="text-center">Step 3</h4> -->
                                                
                                                    <div class="col-md-12">
                                                        <div class="form-group">
                                                            <label> Select Devivery Option  *</label> 
                                                            <select class="form-control" name="deliveryoption">
                                                                <option value=1 > In Branch</option>
                                                                <option value=2 > Inhouse </option>
                                                            </select>
                                                            <!-- <input class="form-control" type="text" name="name" placeholder="">  -->
                                                        </div>
                                                    </div>
                                                    <div class="deliveryplace">
                                                        <div class="col-md-12">
                                                            <div class="form-group">
                                                                <label>Description </label> 
                                                                <textarea class="form-control" name="deliveryoptiondescription"></textarea>
                                                                <!-- <input class="form-control" type="text" name="name" placeholder="">  -->
                                                            </div>
                                                        </div>
                                                    </div>
                                                    
                                                    
                                                    
                                                    <ul class="list-inline pull-right">
                                                        <li><button type="button" class="default-btn prev-step"><i class="fa fa-arrow-left"></i> Back</button></li>
                                                        <!-- <li><button type="button" class="default-btn next-step skip-btn">Skip</button></li> -->
                                                        <li><button type="button" class="default-btn next-step" style="color:white;">Continue <i class="fa fa-arrow-right"></i> </button></li>
                                                    </ul>
                                                </div>

                                            <div class="tab-pane" role="tabpanel" id="step4">
                                                <h4 class="text-center"> REVIEW ORDER DETAILS </h4>
                                                <div class="all-info-container">
                                                   
                                                </div>
                                                
                                                <ul class="list-inline pull-right">
                                                    <li><button type="button" class="default-btn prev-step"><i class="fa fa-arrow-left"></i> Back</button></li>
                                                    <li><button type="submit" name="submit" class="btn btn-success btn-lg" style="border-radius:25px;"> Submit your Order  <i class="fa fa-save"></i> </button></li>
                                                </ul>
                                            </div>
                                            <div class="clearfix"></div>
                                        </div>
                                        
                                    </form>
                                    </div>
                                    <div class="col-md-1"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                 </section>
            </div>
        </div>
                                
    </section>

</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('../inc/sfooter.php'); ?>
  </footer>
  
</div>
                         
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('../inc/slinkjs.php') ?>

</body>
</html>
<script>

     // ------------step-wizard-------------
$(document).ready(function () {
    $('.nav-tabs > li a[title]').tooltip();
    
    //Wizard
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {

        var $target = $(e.target);
    
        if ($target.parent().hasClass('disabled')) {
            return false;
        }
    });

    $(".next-step").click(function (e) {

        var $active = $('.wizard .nav-tabs li.active');
        $active.next().removeClass('disabled');
        nextTab($active);

    });
    $(".prev-step").click(function (e) {

        var $active = $('.wizard .nav-tabs li.active');
        prevTab($active);

    });
});

function nextTab(elem) {
    $(elem).next().find('a[data-toggle="tab"]').click();
}
function prevTab(elem) {
    $(elem).prev().find('a[data-toggle="tab"]').click();
}

// group and client 

 $("select[name='group']") .change(function()
    {    

                 
                 var  group = $("select[name='group']").val(); 
                 //alert(product); 
                   $.ajax({
                        async: false, 
                        'type': "POST",                                          
                        'url': "orderajaxclients.php",
                        'data': {group:group},
                        'success': function (html) {  
                                                      // $('#item_table').append(data);
                                                       $('#client').html(html);
                                                      //  calculateTotal (); 
                                                         
                                                        //   $("input[name='orderitems']").val(counter);                                                                                                                                          
                                                  }     
                        });  

     });


    

         
     // Load system
        var counter=0; 
        $(".deliveryplace").hide();

        // $(document).on('change', '.product', function(){ deliveryoption
            $("select[name='product']") .change(function(){    

                 counter++; 
                 var  product = $("select[name='product']").val(); 
                 //alert(product); 
                   $.ajax({
                        async: false, 
                        'type': "POST",                                          
                        'url': "orderajax.php",
                        'data': { product:product,counter:counter },
                        'success': function (data) {  
                                                       $('#item_table').append(data);
                                                  
                                                        calculateTotal (); 
                                                         
                                                           $("input[name='orderitems']").val(counter);                                                                                                                                          
                                                  }     
                        });  
                       
                       
            
                // DELETE A ROW 
                $(document).on('click', '.remove', function()
                    {  

                      var button_id = $(this).attr("id");  
                       if( button_id > 1)
                           {
                              $('#row'+button_id+'').remove(); 
                            }else{}

                            calculateTotal ();  
                                            
                    }); 
            
                //  INITIALIZE SELECT 2 ON DYNAMIC TABLE 
            
                    $('.js-example-basic-single').select2({
                        placeholder:'',
                        width: 'resolve'        
                    });

                // UNIT CHANGE
                 $("#item_table").on('change', '.unit', function(){

                        //   var oldprice =     $(this).closest('tr').find('.oldprice').val();
                          var price =        $(this).closest('tr').find('.price').val();

                          var unit =         $(this).closest('tr').find('.unit').val();
                           //   var vatrate =      $(this).closest('tr').find('.vatrate').val();                                     
                           //   var rate       =     vatrate / 100 ;
                           //   var nvatamount =    ( price * unit ) * rate ;
                           //   var ntotal     =       unit * price  ;
                           // var npayable     =    nvatamount + ( unit * price ) ;
                          var ntotal     =       unit * price  ;
                                             
                        //   $(this).parents("tr").find(".vatamount").val(nvatamount);  
                        //   $(this).parents("tr").find(".total").val(ntotal) ; 
                          $(this).parents("tr").find(".payable").val(ntotal) ; 
                                    
                          calculateTotal (); 
                 });


                    
          
                                      
        });
         
                                                          
                function calculateTotal () { $(".item_table").each(function () {  
                                    var sum = 0; 
                                    $("input[name='payable[]']").each(function () {
                                    if (!isNaN(this.value) && this.value.length != 0) 
                                            { sum += parseFloat(this.value);  }
                                                    $("input[name='ordertotal']").val(sum);
                                                                                });  
                                                                    });
                                            }


                $("select[name='deliveryoption']") .change(function()
                {  
                    var  deliveryoption = $("select[name='deliveryoption']").val(); 
                     //  alert(deliveryoption);

                    if(deliveryoption==1){
                          $(".deliveryplace").hide();
                      }
                    else if( deliveryoption==2 )
                        {
                            $(".deliveryplace").show();
                        } else{}
                 });                                                     
          


</script>