              <?php if($this->session->flashdata('add_dc_success')){ ?>
                            <div class="alert alert-success solid" style='text-align:center;'><?php echo $this->session->flashdata('add_dc_success'); ?></div>
                        <?php } ?>
                                    <div id='add_dc_success' class='alert alert-success solid' style="display: none;">
                                   
                                    </div>    

                                    <div id='no_stock' class='alert alert-danger solid' style="display: none;">
                                    
                                    </div> 
            <div class="welcome-text">
                            <h4>DC Challan Receving</h4>
                        </div>
                <div class="col-lg-12">
                        <div class="card">
                           
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-hover table-responsive-lg">
                                        <thead>
                                            <tr>
                                               <th>S.O No</th>
                                                <th>Items</th>
                                                <th>In Stock</th>
                                                <th>Total Qty</th>
                                                <th>Qty</th>
                                               <th>Unit</th>
                                               <th>Date</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if(!empty($get_invoice_data)) {?>
                                                <?php //foreach ($get_invoice_data as $all_get_po) {
                                                   
                                                 ?>
                                            <tr> 
                                                <td><strong><?php echo so_number( $get_invoice_data[0]['so_id']); ?></strong></td>
                                              

                                                    <td style="font-size: 11px;"><strong><?php echo product_name($get_invoice_data[0]['product_id']); ?></strong></td>

                                                    <td><?php 

                                                    $total = get_in_balance($get_invoice_data[0]['product_id'])-get_out_balance($get_invoice_data[0]['product_id']); 
                                                   echo  max($total, 0);

                                                    ?></td>

                                              <td><?php echo qty_so( $get_invoice_data[0]['so_id']); ?></td>

                                                <?php  
                                                
                                                    $dc_qty = $get_dc_qty[0]['qty'];
                                                    $mat_qty = $get_invoice_data[0]['total_qty'];
                                                    $total_condition =  $mat_qty-$dc_qty;                                        
                                                if($dc_qty ==""){ ?>                                                 
                                                 <td><strong><?php  echo $get_invoice_data[0]['total_qty']; ?></strong></td>
                                                 <td><strong>Pcs</strong></td>
                                                <?php }else if($total_condition == 0){?>

                                                <td><strong><?php echo $get_invoice_data[0]['total_qty'];?></strong></td>
                                                <td><strong>Pcs</strong></td>
                                                
                                               <?php }else{ ?>
                                                    <td><strong><?php  echo $mat_qty-$dc_qty; ?></strong></td>

                                                    <td><strong>Pcs</strong></td>
                                                <?php } ?>

                                                <td><?php  echo date("d-m-Y", strtotime($get_invoice_data[0]['so_date']) ); ?></td>
                                            </tr>
                                        <?php }
                                              // }
                                         ?>
                                          
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <?php 


                        $total = $mat_qty-$dc_qty;
                        if($total == 0){

                          echo "";
                        }//else

                        if($get_dc_qty[0]['qc_status']!=1){
                         ?>
                        

                  <input type="hidden" name="tqty" id="tqty"  value="<?php echo qty_so( $get_invoice_data[0]['so_id']); ?>"/> 
                                                  <?php 
                                                    $dc_qty = $get_dc_qty[0]['qty'];
                                                    $mat_qty = $get_invoice_data[0]['total_qty'];
                                                    $total_condition =  $mat_qty-$dc_qty;  
                                                     ?>
                    <input type="hidden" name="total" id="total"  value="<?php echo  $total_condition; ?>"/> 


                    
                <?php echo form_open(base_url(''),array('class'=>"form-horizontal add_dc", 'id'=>"add_dc")); ?>
                    <div class="row">
                        <?php if(!empty($get_sets)) {?>
                 
                            <?php foreach ($get_sets as $get_setss) { ?> 

                            <div class="col-lg-5 col-md-4 col-sm-8">
                                <div class="form-group">
                                    <label class="form-label">Products</label>
                                    <input type="text" readonly name="set_product_id[]" readonly="readonly" value="<?php echo product_name($get_setss['product_id']); ?>" id="product_id"  class="form-control required">
                                </div>
                            </div>

                            <div class="col-lg-4 col-md-3 col-sm-8">
                                <div class="form-group">
                                    <label class="form-label">In Stock</label>
                                    <input type="text" readonly name="set_qty[]" readonly="readonly" value="<?php  $total = get_in_balance($get_setss['product_id'])-get_out_balance($get_setss['product_id']); 
                                                   echo  max($total, 0); ?>" id="stock_in"  class="form-control required">
                                </div>
                            </div>

                            <div class="col-lg-3 col-md-4 col-sm-8">
                        <div class="form-group">
                            <label class="form-label">Qty</label>
                            <input type="text" name="qty_semi[]" id="qty_semi[]" required="required" placeholder="Quantity" class="form-control ">
                        </div>
                    </div>

                        <?php } } ?>
                           
                        <input type="hidden" name="gst"  id="gst" value="<?php echo $get_invoice_data[0]['gst']; ?>">
                           
                            <div class="col-lg-4 col-md-4 col-sm-8">
                                <div class="form-group">
                                    <label class="form-label">Challan No</label>
                                    <input type="text" placeholder="Challan No" name="dc_no" readonly="readonly" value="<?php echo $dc_no; ?>" id="dc_no"  class="form-control required">
                                </div>
                            </div>


                             <div class="col-lg-4 col-md-4 col-sm-8">
                        <div class="form-group">
                            <label class="form-label">Ref Code</label>
                            <input type="text" name="ref_code" id="ref_code" readonly="readonly" value="<?php echo $get_invoice_data[0]['ref_code'] ?>" required="required" placeholder="Ref No" class="form-control ref_code">
                        </div>
                    </div>


                             <div class="col-lg-4 col-md-4 col-sm-8">
                        <div class="form-group">
                            <label class="form-label">Batch No</label>
                            <input type="text" name="batch" id="batch" required="required" placeholder="Batch No" class="form-control batch">
                        </div>
                    </div>

                      <div class="col-lg-4 col-md-4 col-sm-8">
                        <div class="form-group">
                            <label class="form-label">FG Qty</label>
                            <input type="text" name="fgqty" id="fgqty" required="required" placeholder="Quantity" class="form-control ">
                        </div>
                    </div>


                     <div class="col-lg-4 col-md-4 col-sm-8">
                        <div class="form-group">
                            <label class="form-label">Remarks</label>
                            <textarea name="remarks" id="remarks"  placeholder="Remarks" class="form-control "></textarea>
                        </div>
                    </div>
                  
                    
                     <input type="hidden" name="created" id="created" value="<?php echo date('Y-m-d H:i:s'); ?>" class="form-control required">
                      <input type="hidden" value="<?php echo date('Y-m-d H:i:s'); ?>" name="modified" id="modified" class="form-control required">
                       <input type="hidden" name="user_id" id="user_id" value="<?php echo $this->session->userdata('id');?>">

                         <input type="hidden" name="product_id" id="product_id" value="<?php echo $get_invoice_data[0]['product_id']; ?>">

                       <input type="hidden" name="so_id" id="so_id" value="<?php echo $get_invoice_data[0]['so_id']; ?>">

                       <input type="hidden" id="in_stock" value="<?php  echo get_in_balance($get_invoice_data[0]['product_id'])-get_out_balance($get_invoice_data[0]['product_id']); ?>">
                </div>

                  <?php form_close();?>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" id="dc_dis" class="btn btn-primary">Submit <i class="icon-arrow-right14 position-right"></i></button>
                    </div>
                  <?php } ?>

                  
             </div>


            
             <script type="text/javascript">
                 
                /*else if(in_stock < 1){
                     alert("Stock Quantity Not Available..!");

                }*/

                           var dc = $('#add_dc');   
    
                                dc.on('submit',function(e){
                                 
       
                                    e.preventDefault();
                                    
                                    var dcdata = dc.serialize();
                                    //var tqty = parseInt($("#tqty").val());
                                     var total = parseInt($("#total").val());
                                        var qty = parseInt($("#fgqty").val());
                                      var in_stock = parseInt($("#in_stock").val());



                                        if (total < qty) {
                                            alert("Quantity Greater Then PO Quantity..!");
                                           
                                        }else{
                                        $("#dc_dis").attr('disabled', true);
                                        $.ajax({
                                            type:'POST',
                                            url:'<?php echo base_url('order_qc/pending_orders'); ?>',
                                            data:dcdata,
                                            dataType:'json',
                                            success:function(data){
                                                console.log('first'+data.id);
                                               // alert(data);
                                                if(data.status !=''){
                                                    console.log('second'+data.id);
                                                    $('#add_dc_success').show().html('Dc added Successfully');
                                                   setTimeout(function(){location.href="<?php echo base_url('Sales_order/so_delivery_challan'); ?>"} , 3000);
                                                }else{
                                                    console.log('third'+data);   
                                                    $('#no_stock').show().html('Stock Not Available...!');       
                                                }
                                            },
                                            error:function(data){console.log(data.responseText);}
                                        });
                                    }
                                });

                       /* }

                      });
                    });*/


              
             </script>