 <link href="<?php echo base_url(); ?>assets/vendors/select2/dist/css/select2.min.css" rel="stylesheet">
 <style>
     input[type="checkbox"][readonly] {
  pointer-events: none;
}
 </style>
 <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
                <div class="title_left">
                    <h3></h3>
                </div>
            </div>
            <div class="clearfix"></div>
<?php if ($this->session->flashdata('msg')): ?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('msg'); ?>
</div>
<?php endif ?>
<?php if ($this->session->flashdata('errmsg')): ?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('errmsg'); ?>
</div>
<?php endif ?>
            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Opening Balance</h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
          
                    <form id="form" class="form-horizontal form-label-left" action="<?php echo base_url()?>Account/save_opning_balance" method="post">
                    
                       <div class="item form-group">
                        <label class="control-label col-md-4 col-sm-3 col-xs-12" for="name">Start<span class="required">*</span>
                        </label>
                        <div class="col-md-4 col-sm-6 col-xs-6">
                         <input name="st_date" type="month" class="form-control" required>                    
                        </div>
                     
                      </div> 
                      
       <table id="myDataTable" class="table table-striped table-bordered" style="width:100%;" >
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Head</th>
                          <th>Dr</th>
                          <th>Cr</th>
                        </tr>
                      </thead>
          <tbody class="center"> 
          
                         <?php
                         $dr_tt=0;$cr_tt=0;
                         if(!empty($heads))
                         {
                         foreach($heads as $key=>$val)
                             {
                                $branch=$this->user_model->getbranch(); 
                                  $filter=$this->db->select('*')
                                                   ->from('heads_permission')
                                                   ->where('branch',$branch)
                                                   ->where('head',$val['id'])
                                                   ->get()->result_array();
                                 if(!empty($filter)) 
                                  {
                                  $branch=$this->user_model->getbranch(); 
                                  $flt_secnd=$this->db->select('*')
                                                   ->from('opening_balance')
                                                   ->where('branch',$branch)
                                                   ->where('head',$val['id'])
                                                   ->get()->result_array();
                                     
                                 if(empty($flt_secnd)) 
                                  {      
                                  ?>
                <tr>   
              <td><input type="checkbox" class="type_checkbox" name="checkme[]" value="<?php echo $val['id'];?>" readonly></td>        
                     <?php 
                         $dr_total=0;
                         $cr_total=0;
                         $fetch_parents_lev1=0;
                         $fetch_parents_lev2=0;
                         $fetch_parents_lev3=0;
                         $fetch_parents_lev4=0;
                         $fetch_parents_lev5=0;
                         $fetch_parents_lev6=0;
                         $fetch_parents_lev7=0;
                         if($val['parent'] !=0)
                         {
                             
                         $fetch_parents_lev1=$this->db->select('*')
                                                 ->from('account_heads')
                                                 ->where('id',$val['parent'])
                                                 ->get()->row();
                             
                              $names[]=$fetch_parents_lev1->name;
                              $fetch_parents_lev1=$fetch_parents_lev1->parent;
                             // var_dump('<pre>',$fetch_parents_lev1);
                              //die();
                              if($fetch_parents_lev1!= 0)
                              {
                           $fetch_parents_lev2=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev1)
                                                 ->get()->row();
                               
                                 $names[]=$fetch_parents_lev2->name;
                                 $fetch_parents_lev2=$fetch_parents_lev2->parent;
                              }
                              if($fetch_parents_lev2!= 0)
                              {
                           $fetch_parents_lev3=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev2)
                                                 ->get()->row();
                                               
                                 $names[]=$fetch_parents_lev3->name;       
                                 $fetch_parents_lev3=$fetch_parents_lev3->parent;     
                              }
                              
                            if($fetch_parents_lev3 != 0)
                              {
                           $fetch_parents_lev4=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev3)
                                                 ->get()->row();
                                                  
                                 $names[]=$fetch_parents_lev4->name;      
                                 $fetch_parents_lev4=$fetch_parents_lev4->parent;     
                              }
                              
                              if($fetch_parents_lev4 != 0)
                              {
                           $fetch_parents_lev5=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev4)
                                                 ->get()->row();
                                                
                                 $names[]=$fetch_parents_lev5->name;        
                                 $fetch_parents_lev5=$fetch_parents_lev5->parent;     
                              }
                              
                              if($fetch_parents_lev5 != 0)
                              {
                           $fetch_parents_lev6=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev5)
                                                 ->get()->row();
                                 
                                 $names[]=$fetch_parents_lev6->name;  
                                 $fetch_parents_lev6=$fetch_parents_lev6->parent;     
                              }
                              
                               if($fetch_parents_lev6 != 0)
                              {
                           $fetch_parents_lev7=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev6)
                                                 ->get()->row();
                                                
                                 $names[]=$fetch_parents_lev7->name;                 
                              }
                            
                             $names= array_reverse($names);
                        
                             if(!empty($names)){
                                 $i=1;
                                 echo "<td>";
                                  $cod=$this->db->select('code')
                                                   ->from('account_heads')
                                                   ->where('id',$val['id'])
                                                   ->get()->row_array()['code'];
                                     echo "<b>".$cod."</b>"."   "; 
                                 foreach($names as $key=>$valtm)
                                 {
                                     
                                                 
                                     echo " "."<b>".$valtm."</b>";
                                    
                                     $i++;
                                 }
                                 unset($names); 
                             }
                         }
                         ?>
                          
                          <?php echo "<span class='glyphicon glyphicon-share-alt'></span>";?>
                           
                        <?php echo $val['name'];?></td>  
              
                <td> <input type="number" class="form-control dr_amt" id="dr<?php echo $val['id'];?>" onkeyup="dr_up(<?php echo $val['id'];?>,event)"
                          onclick="dr_act(<?php echo $val['id'];?>,event)" placeholder="Dr Amount" name="dr_amt[<?php echo $val['id'];?>][]"> </td>
                        
                       
                           <td> <input type="number" class="form-control cr_amt" id="cr<?php echo $val['id'];?>" onkeyup="cr_up(<?php echo $val['id'];?>,event)"
                       onclick="cr_act(<?php echo $val['id'];?>,event)" placeholder="Cr Amount" name="cr_amt[<?php echo $val['id'];?>][]"></td>
                <?php
                          }//end here inner
                          ?>
                        </tr>  
                      <?php    
                      } 
                           }
                         }
                          ?>
                          <tr>
                            
                            <td></td>
                            <td><b>Total</b></td>
                            <td><input type="text" id="dr_tt" class="form-control" readonly></td>
                            <td><input type="text" id="cr_tt" class="form-control" readonly></td>
                        </tr>
                      </tbody>
                    </table>
                     
                      
                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-4 col-md-offset-2">
                          <button id="send" type="submit" class="btn btn-success pull-right">Save</button>
                        </div>
                      </div>
                    </form>
                  </div>
                  
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- /page content -->

        <script src="<?php echo base_url(); ?>assets/vendors/jquery/dist/jquery.min.js"></script>
        <script src="<?php echo base_url(); ?>assets/vendors/select2/dist/js/select2.full.min.js"></script>
       

    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-buttons-bs/js/buttons.bootstrap.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-buttons/js/buttons.flash.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-buttons/js/buttons.html5.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-buttons/js/buttons.print.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/datatables.net-scroller/js/datatables.scroller.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/jszip/dist/jszip.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/pdfmake/build/pdfmake.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/pdfmake/build/vfs_fonts.js"></script>
        <script>
        var jq=jQuery.noConflict();
        
        jq(document).ready(function() {
       
        jq('#myDataTable').DataTable(
            {
            "bPaginate":false,
            "bFilter":false,
            "bInfo":false,
            "ordering":false,
            "scrollX":true,
            dom: 'Bfrtip',
            buttons: [
            'csv', 'excel', 'pdf',{extend:'print',text:'Print',exportOptions:{columns:[0,1,2,3,4,5,6,7]}}            ]
            }
        );

       
        });
        </script>
        <script>
           var jq=jQuery.noConflict();
         jq(function(){
         jq('form').submit(function(){
            jq('button[type=submit], input[type=submit]').prop('disabled',true);
        });
         });
window.setTimeout(function() {
jq(".alert").fadeTo(500, 0).slideUp(500, function(){
    jq(this).remove(); 
});
}, 3000);
        </script>
       

 <script>
function dr_act(cid,event){
            var jq=jQuery.noConflict();
var dr='#dr'+cid;
var cr='#cr'+cid;
jq(cr).val('');
var dm=jq(dr).val();
//jqdr_tt
var cm=jq(cr).val();
if(dm=='' && cm=='')
{
     jq("input[name='checkme[]']").each(function () {
            var slt_vl=jq(this).val();
            if(slt_vl ==cid)
            {
             jq(this).prop('checked',false);
            }
        });
}
jq(cr).prop('readonly',true);
jq(dr).prop('readonly',false);
}
 </script>
 
 <script>
function cr_act(cid,event){
            var jq=jQuery.noConflict();
var dr='#dr'+cid;
var cr='#cr'+cid;
jq(dr).val('');
var dm=jq(dr).val();
var cm=jq(cr).val();
if(dm=='' && cm=='')
{
     jq("input[name='checkme[]']").each(function () {
            var slt_vl=jq(this).val();
            if(slt_vl ==cid)
            {
             jq(this).prop('checked',false);
            }
        });
}
jq(dr).prop('readonly',true);
jq(cr).prop('readonly',false);
}
</script>

 <script>
function cr_up(cid,event){
    var jq=jQuery.noConflict();
     jq("input[name='checkme[]']").each(function () {
            var slt_vl=jq(this).val();
            if(slt_vl ==cid)
            {
             jq(this).prop('checked',true);
            }
        });
        
var dr='#dr'+cid;
var cr='#cr'+cid;
var dm=jq(dr).val();
var cm=jq(cr).val();
var old_value=jq("#cr_tt").val();
var st_nw=cm + old_value;
jq("#cr_tt").val(st_nw);

if(dm=='' && cm=='')
{
     jq("input[name='checkme[]']").each(function () {
            var slt_vl=jq(this).val();
            if(slt_vl ==cid)
            {
             jq(this).prop('checked',false);
            }
        });
}

             var sum_dr = 0;
             var sum_cr = 0;
                 jq('.dr_amt').each(function() {
                  sum_dr += Number(jq(this).val());
                  });
                  
                  jq('.cr_amt').each(function() {
                  sum_cr += Number(jq(this).val());
                  });
              jq("#dr_tt").val(sum_dr);
              jq("#cr_tt").val(sum_cr);
                  if(sum_cr!=sum_dr)
                  {
                      jq('#send').prop('disabled',true);
                  }else
                  {
                      jq('#send').prop('disabled',false);
                  }

}
</script>
 <script>
function dr_up(cid,event){
    var jq=jQuery.noConflict();
     jq("input[name='checkme[]']").each(function () {
            var slt_vl=jq(this).val();
            if(slt_vl ==cid)
            {
             jq(this).prop('checked',true);
            }
        });
        
var dr='#dr'+cid;
var cr='#cr'+cid;
var dm=jq(dr).val();
var cm=jq(cr).val();

if(dm=='' && cm=='')
{
     jq("input[name='checkme[]']").each(function () {
            var slt_vl=jq(this).val();
            if(slt_vl ==cid)
            {
             jq(this).prop('checked',false);
            }
        });
}

             var sum_dr = 0;
             var sum_cr = 0;
                 jq('.dr_amt').each(function() {
                  sum_dr += Number(jq(this).val());
                  });
                  
                  jq('.cr_amt').each(function() {
                  sum_cr += Number(jq(this).val());
                  });
                  jq("#dr_tt").val(sum_dr);
                  jq("#cr_tt").val(sum_cr);
                  if(sum_cr!=sum_dr)
                  {
                      jq('#send').prop('disabled',true);
                  }else
                  {
                      jq('#send').prop('disabled',false);
                  }

}
</script>
<footer>
          <div class="pull-right">
            EduSolutions
          </div>
          <div class="clearfix"></div>
        </footer>
        <div id='ajaxload' style='position:fixed;top:0;left:0;bottom:0; width:100%;height:100%;z-index:1000;background:rgba(2,132,130,0.5);display:none;'>
            <i class='fa fa-spinner fa-spin fa-4x' style='position: relative;top: 50%;left: 50%;margin-top: -50px;margin-left: -50px;'></i>
        </div>
        <!-- /footer content -->
      </div>
    </div>

    <!-- jQuery -->
    
    <!-- Custom JS -->
    <script src="<?php echo base_url(); ?>assets/js/main2.js"></script>
    <!-- Bootstrap -->
    <script src="<?php echo base_url(); ?>assets/vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="<?php echo base_url(); ?>assets/vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="<?php echo base_url(); ?>assets/vendors/nprogress/nprogress.js"></script>
    <!-- Chart.js -->
    <script src="<?php echo base_url(); ?>assets/vendors/Chart.js/dist/Chart.min.js"></script>
    <!-- gauge.js -->
    <script src="<?php echo base_url(); ?>assets/vendors/gauge.js/dist/gauge.min.js"></script>
    <!-- bootstrap-progressbar -->
    <script src="<?php echo base_url(); ?>assets/vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
    <!-- iCheck -->
    <script src="<?php echo base_url(); ?>assets/vendors/iCheck/icheck.min.js"></script>
    <!-- Skycons -->
    <script src="<?php echo base_url(); ?>assets/vendors/skycons/skycons.js"></script>
    <!-- Flot -->
    <script src="<?php echo base_url(); ?>assets/vendors/Flot/jquery.flot.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/Flot/jquery.flot.pie.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/Flot/jquery.flot.time.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/Flot/jquery.flot.stack.js"></script>
    <script src="<?php echo base_url(); ?>assets/vendors/Flot/jquery.flot.resize.js"></script>
    <!-- Flot plugins -->
    <script src="<?php echo base_url(); ?>assets/js/flot/jquery.flot.orderBars.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/flot/date.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/flot/jquery.flot.spline.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/flot/curvedLines.js"></script>
    <!-- jVectorMap -->
    <script src="<?php echo base_url(); ?>assets/js/maps/jquery-jvectormap-2.0.3.min.js"></script>
    <!-- bootstrap-daterangepicker -->
    <script src="<?php echo base_url(); ?>assets/js/moment/moment.min.js"></script>
    <script src="<?php echo base_url(); ?>assets/js/datepicker/daterangepicker.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="<?php echo base_url(); ?>assets/build/js/custom.min2.js"></script>
          <script src="<?php echo base_url(); ?>assets/js/bootstrap-datepicker.js"></script>
<script>
var jq=jQuery.noConflict();
 jq(document).ready(function() {
    jq('#datatable').DataTable();
} );
jq(document).ajaxStart(function() {
          jq('#ajaxload').show();
      });
      jq(document).ajaxStop(function() {
           jq('#ajaxload').hide();
      });
</script>
   
  </body>
</html>