<?php
$this->load->view ( $left );
$this->load->view ( $header );
?>
<style type="text/css">
    .ibox-content {
        border-style : none;
    }

    .hr-line-dashed {
        margin: 5px 0;
    }
</style>
<?php  // echo "<pre>"; print_r($order_step_data); //echo $order_current_step_qty[0]->qty_receive; print_r($order_current_step_qty); print_r($order_data); //echo $order_data->item_id; // 0000-00-00 // print_r($order_data); ?>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2><?php echo ($order_id == 0) ? 'New Order' : 'Update Order';?></h2>
    </div>
</div>
<?php if(!empty($order_step_data)){ ?>
<div class="row wrapper border-bottom white-bg">
    <div class="col-lg-12">        
        <?php 
        echo "<h4>Order steps of ID - ".$order_id."</h4>";
        foreach($order_step_data as $order_da){  
            if($order_da->date_receive == "0000-00-00") {
                echo "<br>Incomplete -- Status - ".$order_da->status_name." || Worker - ".$order_da->name." || Item - ".$order_da->item_name." || Qty - ".$order_da->qty." || Qty Receive - ".$order_da->qty_receive." || Date Assign - ".$order_da->date_assign." || Weight Assign - ".$order_da->weight_assign." || Weight Receive - ".$order_da->weight_receive."<br>";
                foreach($order_data_detail_qty as $order_qty_da){ 
                        if($order_da->order_status_step_id == $order_qty_da->order_status_step_id){
                            echo "--------".date("j F, Y", strtotime($order_qty_da->date_added))." ||  Qty receive-".$order_qty_da->qty_receive." || Weight receive-".$order_qty_da->weight_receive."<br>";
                        }
                }
            }else{
                echo "<br>Complete -- Status - ".$order_da->status_name." || Worker - ".$order_da->name." || Item - ".$order_da->item_name." || Qty - ".$order_da->qty." || Qty Receive - ".$order_da->qty_receive." || Date Assign - ".$order_da->date_assign." || Date Receive - ".$order_da->date_receive." || Weight Assign - ".$order_da->weight_assign." || Weight Receive - ".$order_da->weight_receive."<br>";
                foreach($order_data_detail_qty as $order_qty_da){ 
                    if($order_da->order_status_step_id == $order_qty_da->order_status_step_id){
                        echo "--------".$order_qty_da->date_added." ||  Qty receive-".$order_qty_da->qty_receive." || Weight receive-".$order_qty_da->weight_receive."<br>";
                    }
                }
            }            
        } 
        echo "<br>";
        ?>    
    </div>
</div>
<?php } 
$status_n = !empty($order_data->status_name) ? $order_data->status_name : '';
if($status_n != "Complete"){
?>
<div class="wrapper wrapper-content">
    <div class="row text-center animated fadeInRightBig">
        <div class="col-lg-12">
            <form method="post" class="form-horizontal" id="bank_data">
                <input type="hidden" name="order_id" id="order_id" value="<?php echo $order_id; ?>" />
                <input type="hidden" name="parent_order_id" id="parent_order_id" value="<?php echo $parent_order_id; ?>" />
                <input type="hidden" name="order_status_step_id" id="order_status_step_id" value="<?php echo !empty($order_data->order_status_step_id) ? $order_data->order_status_step_id : '';?>" /> 
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <?php if($parent_order_id != 0){ ?>
                        <div class="form-group">
                            <label class="col-sm-6 control-label">Parent Order ID - <?php echo $parent_order_id; ?></label>                            
                        </div>
                        <?php } ?>
                        <?php if($parent_order_id != 0){ ?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Item</label>
                            <div class="col-sm-10">
                                
                                <select class="form-control" name="item_id" id="item_id" disabled>
                                    <option value="">Select Item</option>
                                    <?php 
                                    $items_autocomplete = json_decode($items_autocomplete);
                                    if (count($items_autocomplete) > 0) {
                                        foreach ($items_autocomplete as $key => $value) {?>
                                            <option value="<?php echo $value->id;?>" <?php  echo (!empty($order_data->item_id) && isset($order_data->item_id) && ($order_data->item_id == $value->id)) ? 'selected="selected"' : '';?>><?php echo $value->name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Worker</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="customer_id" id="customer_id">
                                    <option value="">Select Customer</option>
                                    <?php if (count($customer_list) > 0) {
                                        foreach ($customer_list as $key => $value) {?>
                                            <option value="<?php echo $value->id;?>" ><?php echo $value->name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Metal</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="metal" id="metal" disabled>
                                    <option value="">Select Metal</option>                                    
                                    <option value="Gold" <?php  echo (!empty($order_data->metal) && isset($order_data->metal) && ($order_data->metal == "Gold")) ? 'selected="selected"' : '';?>>Gold</option>
                                    <option value="Silver" <?php  echo (!empty($order_data->metal) && isset($order_data->metal) && ($order_data->metal == "Silver")) ? 'selected="selected"' : '';?>>Silver</option>
                                    <option value="Brass" <?php  echo (!empty($order_data->metal) && isset($order_data->metal) && ($order_data->metal == "Brass")) ? 'selected="selected"' : '';?>>Brass</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Qty</label>
                            <div class="col-sm-10">
                                <input type="text" name="qty" id="qty" class="form-control" placeholder="Qty" value="">                                
                            </div>
                        </div>
                        <?php if($order_id != 0 && $order_data->date_receive == "0000-00-00" ){ ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Assign</label>
                                <div class="col-sm-4">
                                    <input type="text" name="weight_assign" id="weight_assign" class="form-control" placeholder="Weight Assign" value="">
                                </div>
                                <label class="col-sm-2 control-label">Loss %</label>
                                <div class="col-sm-4">
                                    <input type="text" name="loss" id="loss" class="form-control" placeholder="Loss" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Receive</label>
                                <div class="col-sm-10">
                                    <input type="text" name="weight_receive" id="weight_receive" class="form-control" placeholder="Weight Receive" value="">
                                </div>
                            </div> 
                            <div class="form-group">
                            <label class="col-sm-2 control-label">Status</label>
                            <div class="col-sm-10">
                                <select name="status" id="status" class="form-control" >
                                    <?php if (count($order_step) > 0) {
                                        foreach ($order_step as $key => $value) {?>
                                            <option value="<?php echo $value->status_id;?>"><?php echo $value->status_name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                            </div>
                        <?php }else{ ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Assign</label>
                                <div class="col-sm-4">
                                    <input type="text" name="weight_assign" id="weight_assign" class="form-control" placeholder="Weight Assign" value="">
                                </div>
                                <label class="col-sm-2 control-label">Loss %</label>
                                <div class="col-sm-4">
                                    <input type="text" name="loss" id="loss" class="form-control" placeholder="Loss" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Receive</label>
                                <div class="col-sm-10">
                                    <input type="text" name="weight_receive" id="weight_receive" class="form-control" placeholder="Weight Receive" value="0">
                                </div>
                            </div> 
                            <div class="form-group">
                            <label class="col-sm-2 control-label">Status</label>
                            <div class="col-sm-10">
                                <select name="status" id="status" class="form-control">
                                    <?php if (count($order_step) > 0) {
                                        foreach ($order_step as $key => $value) {?>
                                            <option value="<?php echo $value->status_id;?>" <?php  echo (!empty($order_data->status_id) && isset($order_data->status_id) && ($order_data->status_id == $value->status_id)) ? 'selected="selected"' : '';?>><?php echo $value->status_name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                            </div>
                        <?php } ?>
                        <?php }else{
                            ?>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">Item</label>
                            <div class="col-sm-10">
                                
                                <select class="form-control" name="item_id" id="item_id" <?php echo (!empty($order_data->item_id) && isset($order_data->item_id)) ? 'disabled' : '';?>>
                                    <option value="">Select Item</option>
                                    <?php 
                                    $items_autocomplete = json_decode($items_autocomplete);
                                    if (count($items_autocomplete) > 0) {
                                        foreach ($items_autocomplete as $key => $value) {?>
                                            <option value="<?php echo $value->id;?>" <?php  echo (!empty($order_data->item_id) && isset($order_data->item_id) && ($order_data->item_id == $value->id)) ? 'selected="selected"' : '';?>><?php echo $value->name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                        </div>
                        <?php if($order_id != 0 && $order_data->date_receive == "0000-00-00" ){ ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Worker</label>
                                <div class="col-sm-10">
                                    <select class="form-control" name="customer_id" id="customer_id" disabled>
                                        <option value="">Select Customer</option>
                                        <?php if (count($customer_list) > 0) {
                                            foreach ($customer_list as $key => $value) {?>
                                                <option value="<?php echo $value->id;?>" <?php  echo (!empty($order_data->worker_id) && isset($order_data->worker_id) && ($order_data->worker_id == $value->id)) ? 'selected="selected"' : '';?>><?php echo $value->name;?></option>
                                            <?php }
                                        }?>
                                    </select>
                                </div>
                            </div>
                        <?php }else{
                            ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Worker</label>
                                <div class="col-sm-10">
                                    <select class="form-control" name="customer_id" id="customer_id">
                                        <option value="">Select Customer</option>
                                        <?php if (count($customer_list) > 0) {
                                            foreach ($customer_list as $key => $value) {?>
                                                <option value="<?php echo $value->id;?>" <?php  echo (!empty($order_data->worker_id) && isset($order_data->worker_id) && ($order_data->worker_id == $value->id)) ? 'selected="selected"' : '';?>><?php echo $value->name;?></option>
                                            <?php }
                                        }?>
                                    </select>
                                </div>
                            </div>
                            <?php
                        }?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Metal</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="metal" id="metal"  <?php echo (!empty($order_data->metal) && isset($order_data->metal)) ? 'disabled' : '';?>>
                                    <option value="">Select Metal</option>                                    
                                    <option value="Gold" <?php  echo (!empty($order_data->metal) && isset($order_data->metal) && ($order_data->metal == "Gold")) ? 'selected="selected"' : '';?>>Gold</option>
                                    <option value="Silver" <?php  echo (!empty($order_data->metal) && isset($order_data->metal) && ($order_data->metal == "Silver")) ? 'selected="selected"' : '';?>>Silver</option>
                                    <option value="Brass" <?php  echo (!empty($order_data->metal) && isset($order_data->metal) && ($order_data->metal == "Brass")) ? 'selected="selected"' : '';?>>Brass</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Qty</label>
                            <div class="col-sm-10">
                                <input type="text" name="qty" id="qty" class="form-control" placeholder="Qty" value="<?php $qty_total = !empty($order_data->qty) ? $order_data->qty : 0; $qty_receive = !empty($order_current_step_qty[0]->qty_receive) ? $order_current_step_qty[0]->qty_receive : 0; echo $qty_total - $qty_receive; ?>">
                                Total Qty - <?php echo $qty_total = !empty($order_data->qty) ? $order_data->qty : 0; ?> --- Qty Received - <?php $qty_total = !empty($order_data->qty) ? $order_data->qty : 0; $qty_receive = !empty($order_current_step_qty[0]->qty_receive) ? $order_current_step_qty[0]->qty_receive : 0; echo $qty_receive; ?> ---  Qty remaining - <?php $qty_total = !empty($order_data->qty) ? $order_data->qty : 0; $qty_receive = !empty($order_current_step_qty[0]->qty_receive) ? $order_current_step_qty[0]->qty_receive : 0; echo $qty_total - $qty_receive; ?>
                                <input type="hidden" name="qty_received" id="qty_received" class="form-control" placeholder="Qty" value="<?php $qty_total = !empty($order_data->qty) ? $order_data->qty : 0; $qty_receive = !empty($order_current_step_qty[0]->qty_receive) ? $order_current_step_qty[0]->qty_receive : 0; echo $qty_receive; ?>">
                                <input type="hidden" name="qty_left" id="qty_left" class="form-control" placeholder="Qty" value="<?php $qty_total = !empty($order_data->qty) ? $order_data->qty : 0; $qty_receive = !empty($order_current_step_qty[0]->qty_receive) ? $order_current_step_qty[0]->qty_receive : 0; echo $qty_total - $qty_receive; ?>">
                            </div>
                        </div>
                        <?php if($order_id != 0 && $order_data->date_receive == "0000-00-00" ){ ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Assign</label>
                                <div class="col-sm-4">
                                    <input type="text" name="weight_assign" id="weight_assign" class="form-control" placeholder="Weight Assign" value="<?php echo !empty($order_data->weight_assign) ? $order_data->weight_assign : '';?>" disabled>
                                </div>
                                <label class="col-sm-2 control-label">Loss %</label>
                                <div class="col-sm-4">
                                    <input type="text" name="loss" id="loss" class="form-control" placeholder="Loss" value="<?php echo !empty($order_data->loss) ? $order_data->loss : '';?>" disabled>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Receive</label>
                                <div class="col-sm-10">
                                
                                <?php
$total_w = !empty($order_data->weight_assign) ? (float)$order_data->weight_assign : 0;
$received_w = !empty($order_data->weight_receive) ? (float)$order_data->weight_receive : 0;
$balance = $total_w - $received_w;
?>

<input type="text" name="weight_receive" id="weight_receive" class="form-control"
    placeholder="Weight Receive" value="<?php echo $balance; ?>">

Total Weight - <?php echo $total_w; ?> --- 
Weight Received - <?php
$weight_receive = !empty($order_current_step_qty[0]->weight_receive) ? (float)$order_current_step_qty[0]->weight_receive : 0;
echo $weight_receive;
?>

<input type="hidden" name="weight_receive_till" id="weight_receive_till" class="form-control"
    value="<?php echo $weight_receive; ?>">

                                
                                </div>
                            </div> 
                            <div class="form-group">
                            <label class="col-sm-2 control-label">Status</label>
                            <div class="col-sm-10">
                                <select name="status" id="status" class="form-control" disabled>
                                    <?php if (count($order_step) > 0) {
                                        foreach ($order_step as $key => $value) {?>
                                            <option value="<?php echo $value->status_id;?>" <?php  echo (!empty($order_data->status_id) && isset($order_data->status_id) && ($order_data->status_id == $value->status_id)) ? 'selected="selected"' : '';?>><?php echo $value->status_name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                            </div>
                        <?php }else{ ?>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Assign</label>
                                <div class="col-sm-4">
                                    <input type="text" name="weight_assign" id="weight_assign" class="form-control" placeholder="Weight Assign" value="<?php echo !empty($order_data->weight_receive) ? $order_data->weight_receive : '';?>">
                                </div>
                                <label class="col-sm-2 control-label">Loss %</label>
                                <div class="col-sm-4">
                                    <input type="text" name="loss" id="loss" class="form-control" placeholder="Loss" value="<?php echo !empty($order_data->loss) ? $order_data->loss : '';?>">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Weight Receive</label>
                                <div class="col-sm-10">
                                    <input type="text" name="weight_receive" id="weight_receive" class="form-control" placeholder="Weight Receive" value="0">
                                </div>
                            </div> 
                            <div class="form-group">
                            <label class="col-sm-2 control-label">Status</label>
                            <div class="col-sm-10">
                                <select name="status" id="status" class="form-control">
                                    <?php if (count($order_step) > 0) {
                                        foreach ($order_step as $key => $value) {?>
                                            <option value="<?php echo $value->status_id;?>" <?php  echo (!empty($order_data->status_id) && isset($order_data->status_id) && ($order_data->status_id == $value->status_id)) ? 'selected="selected"' : '';?>><?php echo $value->status_name;?></option>
                                        <?php }
                                    }?>
                                </select>
                            </div>
                            </div>
                        <?php } 
                        } ?>
                    </div>
                </div>
                <div class="ibox">
                    <div class="ibox-content">
                        <div class="row">
                            <div class="col-lg-6 pull-right">
                                <div class="form-group">
                                    <div class="col-sm-12 col-sm-offset-2">
                                    <?php if($order_id != 0 && $order_data->date_receive == "0000-00-00" ){ ?> <button class="btn btn-primary receve_order_details" type="button" data-status="1"><span class="m-l-xs">Receive Order</span></button> <a href="<?php echo site_url("order/newsuborder/".$order_id);?>" class="btn btn-primary addNewUser"><i class="fa fa-plus-square-o"></i><span class="m-l-xs">Add Sub Order</span></a> <?php }else{ ?> <button class="btn btn-primary save_order_details" type="button" data-status="1"><span class="m-l-xs"> Assign Order </span></button> <?php if($order_id != 0){ ?> <a href="<?php echo site_url("order/newsuborder/".$order_id);?>" class="btn btn-primary addNewUser"><i class="fa fa-plus-square-o"></i><span class="m-l-xs">Add Sub Order</span></a> <?php } ?>  <button class="btn btn-primary receve_order_details" type="button" data-status="1"><span class="m-l-xs"> Complete Order </span></button><?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<?php 
}
$this->load->view($footer); ?>
<script type="text/javascript">
    $(document).ready(function(){

        var items = new Object();
        var items_tot = new Object();
        
        // var items_autocomplete = JSON.parse('<?php //echo $items_autocomplete;?>');
        // var $input = $("#item_name_0");
        // $input.typeahead({
        //   source: items_autocomplete
        //   //autoSelect: true
        // });

       /* $(document).on('change','.item_name',function(){
            var rowid = $(this).data('tr');
            // items[rowid].item_id = $(this).val();
            // items[rowid].item_name = $("option:selected", this).text();
            //var $input = $(this);
            var current = $(this).typeahead("getActive");
            if (current) {
                // Some item from your model is active!
                if (current.name == $(this).val()) {
                  // This means the exact match is found. Use toLowerCase() if you want case insensitive match.
                    alert(current.id);
                    items[rowid].item_id = current.id;
                    items[rowid].item_name = current.name;
                    
                } else {
                  // This means it is only a partial match, you can either add a new item
                  // or take the active if you don't want new items
                   items[rowid].item_name = $(this).val();
                }
            } else {
              items[rowid].item_name = $(this).val();
            // Nothing is active so it is a new value (or maybe empty value)
            }
            //invoice_calculation();
            //alert(items[rowid].item_id);
       }); */


        $('.save_order_details').on('click',function(){
            if ($('#item_id').val() == '') {
                $('#item_id').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#item_id').parent().parent().removeClass('has-error');
            }

            if ($('#customer_id').val() == '') {
                $('#customer_id').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#customer_id').parent().parent().removeClass('has-error');
            } 
            
            if ($('#metal').val() == '') {
                $('#metal').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#metal').parent().parent().removeClass('has-error');
            }

            if ($('#qty').val() == '') {
                $('#qty').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#qty').parent().parent().removeClass('has-error');
                // if($('#qty').val() > $('#qty_left').val())
                // {
                //     $('#qty').parent().parent().addClass('has-error');
                //     return false;
                // }
            }

            

            if ($('#weight_assign').val() == '') {
                $('#weight_assign').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#weight_assign').parent().parent().removeClass('has-error');
            }
            
            if ($('#loss').val() == '') {
                $('#loss').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#loss').parent().parent().removeClass('has-error');
            }
            
            if ($('#weight_receive').val() == '') {
                $('#weight_receive').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#weight_receive').parent().parent().removeClass('has-error');
            }

            if ($('#status').val() == '') {
                $('#status').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#status').parent().parent().removeClass('has-error');
            }
            
            var parent_order_id         = $('#parent_order_id').val();
            var order_id         = $('#order_id').val();
            var order_status_step_id         = $('#order_status_step_id').val();
            var item_id    = $('#item_id').val();
            var customer_id    = $('#customer_id').val();
            var metal    = $('#metal').val();
            var qty  = $('#qty').val();
            var qty_received  = $('#qty_received').val();
            var qty_left  = $('#qty_left').val();
            var weight_assign       = $('#weight_assign').val();
            var loss       = $('#loss').val();
            var weight_receive  = $('#weight_receive').val(); 
            var weight_receive_till  = $('#weight_receive_till').val(); 
            var status  = $('#status').val();
            var cur_date = '<?php echo date("Y-m-d"); ?>';

            //var htmlContent = '<html><head><title>Print Order</title></head><body><h1>Order ID - '+order_id+' Item no - '++' </h1><p>This is some content.</p></body></html>';

            $.ajax({
                url: '<?php echo site_url('order/save_order');?>',
                type: "POST",
                data:{    
                    'parent_order_id'  : parent_order_id,
                    'order_id'  : order_id,
                    'order_status_step_id'  : order_status_step_id,                
                    'status_id'  : status,
                    'worker_id'  : customer_id,
                    'metal' : metal,
                    'item_id': item_id,
                    'qty'       : qty,
                    'qty_received' : qty_received,
                    'qty_left' : qty_left,
                    'weight_assign'  : weight_assign,
                    'loss'  : loss,
                    'weight_receive'  : weight_receive,
                    'weight_receive_till'  : weight_receive_till,
                    'date_assign': cur_date,
                    'date_receive'     : cur_date
                },
                success: function(data){ 
                    alert("Oredr Assign Successfully.");
                    //var newWindow = window.open();
                    //$(newWindow.document.body).html(htmlContent);
                    window.open("<?php echo site_url('order/print_order/');?>"+data , '_blank');
                    window.location.href = '<?php echo site_url("order");?>';
                }
            });        

        }); 

        $('.receve_order_details').on('click',function(){
            if ($('#item_id').val() == '') {
                $('#item_id').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#item_id').parent().parent().removeClass('has-error');
            }

            if ($('#customer_id').val() == '') {
                $('#customer_id').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#customer_id').parent().parent().removeClass('has-error');
            }

            if ($('#metal').val() == '') {
                $('#metal').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#metal').parent().parent().removeClass('has-error');
            }

            if ($('#qty').val() == '') {
                $('#qty').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#qty').parent().parent().removeClass('has-error');
                if(parseInt($('#qty').val()) > parseInt($('#qty_left').val()))
                {  
                    $('#qty').parent().parent().addClass('has-error');
                    return false;
                }
            }

            if ($('#weight_assign').val() == '') {
                $('#weight_assign').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#weight_assign').parent().parent().removeClass('has-error');
            }
            
            if ($('#weight_receive').val() == '') {
                $('#weight_receive').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#weight_receive').parent().parent().removeClass('has-error');
            }

            if ($('#status').val() == '') {
                $('#status').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#status').parent().parent().removeClass('has-error');
            }

            var parent_order_id         = $('#parent_order_id').val();
            var order_id    = $('#order_id').val();
            var order_status_step_id    = $('#order_status_step_id').val();
            var item_id    = $('#item_id').val();
            var customer_id    = $('#customer_id').val();
            var metal    = $('#metal').val();
            var qty  = $('#qty').val();
            var qty_received  = $('#qty_received').val();
            var qty_left  = $('#qty_left').val();
            var weight_assign       = $('#weight_assign').val();
            var weight_receive  = $('#weight_receive').val(); 
            var weight_receive_till  = $('#weight_receive_till').val();
            var status  = $('#status').val();
            var cur_date = '<?php echo date("Y-m-d"); ?>';

            $.ajax({
                url: '<?php echo site_url('order/receve_order');?>',
                type: "POST",
                data:{   
                    'parent_order_id'  : parent_order_id,            
                    'order_id'  : order_id,  
                    'order_status_step_id'  : order_status_step_id,    
                    'status_id'  : status,
                    'worker_id'  : customer_id,
                    'metal' : metal,
                    'item_id': item_id,
                    'qty'       : qty,
                    'qty_received' : qty_received,
                    'qty_left' : qty_left,
                    'weight_assign'  : weight_assign,
                    'weight_receive'  : weight_receive,
                    'weight_receive_till'  : weight_receive_till,
                    'date_assign': cur_date,
                    'date_receive'     : cur_date
                },
                success: function(data){ 
                    alert("Oredr Received Successfully.");
                    location.reload();
                    //window.location.href = '<?php // echo site_url("bank");?>';
                }
            });        

        }); 

    });
</script>
