<link href="<?php echo base_url(); ?>assets/vendors/datatables.net-bs/css/dataTables.bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/vendors/datatables.net-buttons-bs/css/buttons.bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/vendors/datatables.net-fixedheader-bs/css/fixedHeader.bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/vendors/datatables.net-responsive-bs/css/responsive.bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url(); ?>assets/vendors/datatables.net-scroller-bs/css/scroller.bootstrap.min.css" rel="stylesheet">

<style>
.right-border{
 border-right:white 1px;
}
</style>

<?php $grosgrand=0; $incgrand=0; $adgrand=0;$daygrand=0;$provdgrand=0; $net=0;$rem=0;$provdgrand_ret=0; ?>
        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
                <div class="title_left">
                    <h3>Salary</h3>
                </div>
            </div>
            <div class="clearfix"></div>
     <?php if ($this->session->flashdata('suc_msg')): ?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('suc_msg'); ?>
</div>
<?php endif ?>
 <?php if ($this->session->flashdata('err_msg')): ?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('err_msg'); ?>
</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>View Salary</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 method="post" action="<?php echo base_url();?>salary/paid_emp">
                    <div class="form-horizontal form-label-left"  >
                       <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="month">Type <span class="required">*</span>
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input type="text" id='type' name="type" readonly value="<?php echo $type; ?>" class="form-control">
                        </div>
                      </div>
                       <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="month">Month <span class="required">*</span>
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input type="month" id='month' name="month"  readonly value="<?php echo $month; ?>" class="form-control">
                           </div>
                      </div>  
                      <div class="table-responsive">
<?php 
// $branch=$this->user_model->getbranch();
if($this->user_model->is_super()){
    $branch = $this->user_model->getAllBranches();
}
else{ 
    $branch = $this->user_model->getBranch();
}

 $d=$this->db->query("select b_logo,logo1,logo2 from branch where id IN ($branch) ")->result()[0];

 ?>
<a href="<?php echo base_url();?>salary/view_salary" class="btn btn-primary"><i class="fa fa-arrow-circle-left" style="font-size:15px">Back</i></a>
 <input style="float:right" class="btn btn-primary" type="submit" value="Submit Salary">
                      <table id="myDataTable" class="table table-striped table-bordered nowrap" style="width:100%;" >
                      <thead >
                        <tr>
                          
                          <th>#</th>
                          <th>Employee Name</th>
                          <th>Basic Salary</th>
                           <th >Increment</th>
                           <th>Attendence Bouns</th>
                           <th>Return Provident Fund</th>
                          <?php foreach ($allonce as $key => $value) { ?>
                            <th><?php echo $value['name']; ?></th>
                        <?php  } ?>
                        <!--<th>Increments</th>-->
                        <th>Gross Salary</th>
                        <!--Loan-->
                        <?php
                         if(!empty($loan_def))
                          {
                         foreach ($loan_def as $key => $value) {
                                                      
                              ?>

                           <th>
                           <?php
                            echo $value['name']; 
                           ?>
                           </th>
                        <?php 
                        }} 
                         ?>
                         <!--End Loan-->
                         
                           <?php
                            if(!empty($deduction_def))
                          {
                            foreach ($deduction_def as $key => $value) {
                                                      
                              ?>

                            <th><?php echo $value['name']; ?></th>
                        <?php  }} ?>
                        <!--Personal Expense header-->
                                <?php
                                 if(!empty($personal_exp))
                          {
                                 foreach ($personal_exp as $key => $value) {
                                                     
                              ?>

                            <th><?php echo $value['name']; ?></th>
                        <?php  }} ?>
                        <!--end personal expense header-->
                         <th>Provident Fund</th>
                         <th>Advance</th>
                         <th>Days Deduction</th>
                         <th>Total</th>
                         <th>
                             
                             <label><input type="checkbox" placeholder="abc" id="all_check" name="all_check">Check All</label>
                         </th>
                         
                        </tr>
                      </thead>
                      
                      <tbody class="center">
                     <?php  
                     $i=1;
                     $sal=0;
                     $allo=array();
                     $ded=array();
                     $per=array();
                     $ad=array();
                     $days_ded=array();
                     $incr=0;
                     $loans=array();
                     $addinc=0;$adddinc=0;
                     $tmp_bounes=0;
                     foreach ($employee as $key => $emp) { 
                      
                      
                       $gros=0;
                       ?>
                       <tr>
                        <td><?php echo $i++; ?></td>
                        <td><?php echo $emp['firstname'].' '.$emp['lastname']; ?></td>
                        <td>
                            <?php 
                             $prev_inc=$this->db->select('sum(amount) as amount')
                                             ->from('increments')
                                             ->where('bothid',$emp['empid'])
                                              ->where('month <',$month)
                                              ->where('refrence',$type)
                                              ->where('is_del',0)
                                              ->get()->row_array()['amount'];
                         if(!empty($prev_inc))
                         {
                             $tmp_sal=$emp['salery']+$prev_inc;
                          
                         }else
                         {
                            $tmp_sal=$emp['salery'];
                         }                  
                            echo $tmp_sal; 
                            $gros+=$tmp_sal;
                            $sal+=$tmp_sal; 
                            ?>
                            </td>
                       <td><?php $v=0;  foreach ($increment as $keyinc => $allinc) {
                                if($emp['empid']==$allinc['bothid']){
                                  $v = $allinc['amount'];
                                  $adddinc=$adddinc+$allinc['amount'];
                                   $addinc=$addinc+$allinc['amount'];
                                }
                            } 
                            echo $v;
                            $gros+=$v;
                            ?></td>   
                    
                    <td>
                        <?php 
                        
                        $att_bounes=0;
                          $att_bounes=$this->db->select('bounes')
                                             ->from('days_deduction')
                                             ->where('bothid',$emp['empid'])
                                              ->where('month',$month)
                                              ->where('type',$type)
                                              ->where('is_delete',0)
                                              ->get()->row_array()['bounes'];
                        
                         echo $att_bounes;
                         $gros+=$att_bounes;
                         $tmp_bounes+=$att_bounes;
                          ?>
                    </td>
                      
                         <!--Provident fund-->

 <?php 
                         $provd_ret=$this->sm->provident_ret($emp['empid'],$type,$month);  
                         ?>
                           <td>
                           <?php
                            if($provd_ret>0)
                            { 
                              echo $provd_ret; 
                              $gros+=$provd_ret; 
                              $provdgrand_ret+=$provd_ret;  
                             }
                             else
                             {
                             echo 0;
                             } 
                           
                              ?></td>
                        
<!--End providend fund-->

                        <?php 
                       
                        foreach ($allonce as $key => $all) 
                        { 
                        $am=$this->sm->getallonce_ammountmonthly($emp['salery'],$emp['empid'],$type,$all['id'],$month);
                         ?>
                      <td>
                      <?php 
                      if($am>0)
                      { 
                        echo $am; 
                        $gros+=$am;
                          }
                      else{
                      echo 0;
                      } 
                      $am!=0?(!empty($allo[$key])?$allo[$key]+=$am:$allo[$key]=$am):(!empty($allo[$key])?$allo[$key]+=0:$allo[$key]=0); ?>
                      </td>
                                
                        <?php } ?>
                      
                           <td><?php echo $gros; $grosgrand+=$gros; ?></td>
                           
                            <!--Loan-->
                            <?php 
                           if(!empty($loan_def))
                           {
                        foreach ($loan_def as $key => $all)
                         { 
                         $loon=$this->sm->getloan_ammount2($emp['salery'],$emp['empid'],$type,$all['id'],$month);  
                         ?>
                           <td>
                           <?php
                            if($loon>0)
                            { 
                              echo $loon; 
                              $gros-=$loon;  
                             }
                             else
                             {
                             echo 0;
                             } 
         $loon!=0?(!empty($loans[$key])?$loans[$key]+=$loon:$loans[$key]=$loon):(!empty($loans[$key])?$loans[$key]+=0:$loans[$key]=0); ?></td>
                                
                        <?php }} ?> 
                        
                        <!--End Loan-->
                  
<!--Add new deductions-->

 <?php 
                           if(!empty($deduction_def))
                           {
                        foreach ($deduction_def as $key => $all)
                         { 
                         $pai=$this->sm->getdeduct_ammount2($emp['salery'],$emp['empid'],$type,$all['id'],$month);  
                         ?>
                           <td>
                           <?php
                            if($pai>0)
                            { 
                              echo $pai; 
                              $gros-=$pai;  
                             }
                             else
                             {
                             echo 0;
                             } 
         $pai!=0?(!empty($ded[$key])?$ded[$key]+=$pai:$ded[$key]=$pai):(!empty($ded[$key])?$ded[$key]+=0:$ded[$key]=0); ?></td>
                                
                        <?php }} ?>

<!--End new deductions-->

<!--personal expense start-->
    <?php 
      if(!empty($personal_exp))
                           {
                        foreach ($personal_exp as $key => $all)
                         { 
                         $pai2=$this->sm->personal_exp($emp['empid'],$type,$all['id'],$month);  
                         ?>
                           <td>
                           <?php
                            if($pai2>0)
                            { 
                              echo $pai2; 
                              $gros-=$pai2;  
                             }
                             else
                             {
                             echo 0;
                             } 
         $pai2!=0?(!empty($per[$key])?$per[$key]+=$pai2:$per[$key]=$pai2):(!empty($per[$key])?$per[$key]+=0:$per[$key]=0); ?></td>
                                
                        <?php }} ?>                   

<!--End personal expense-->

<!--Provident fund-->

 <?php 
                         $provd=$this->sm->provident($emp['salery'],$emp['empid'],$type,$month);  
                         ?>
                           <td>
                           <?php
                            if($provd>0)
                            { 
                              echo $provd; 
                              $gros-=$provd; 
                              $provdgrand+=$provd;  
                             }
                             else
                             {
                             echo 0;
                             } 
                           
                              ?></td>
                        
<!--End providend fund-->

<!--Advance Ammount start-->
    <?php 
                     
                         $adv=$this->sm->advance($emp['empid'],$type,$month);  
                         ?>
                           <td>
                           <?php
                            if($adv>0)
                            { 
                              echo $adv; 
                              $gros-=$adv; 
                               $adgrand+=$adv;  
                             }
                             else
                             {
                             echo 0;
                             } 
    ?></td>
                                
                                        

<!--End dvance Ammount-->

<!--Days start-->
    <?php 
                       
                         $days=$this->sm->days_deduction($emp['empid'],$type,$month);  
                         ?>
                           <td>
                           <?php
                            if($days>0)
                            { 
                              echo $days; 
                              $gros-=$days;  
                               $daygrand+=$days;  
                             }
                             else
                             {
                             echo 0;
                             } 
    
      ?></td>
                                
                                      

<!--End Days Deduction Ammount-->
                        <td>
                            <?php echo $gros; $net+=$gros; ?>
                        </td>
                        
                        
                        <td class="actions">
                           
                            <?php
                           $prev_inc=$this->db->select('*')
                                             ->from('salary')
                                             ->where('bothid',$emp['empid'])
                                              ->where('month',$month)
                                              ->where('refrence',$type)
                                              ->where('is_delete',0)
                                              ->where('is_paid',0)
                                              ->get()->row_array();
                                if(!empty($prev_inc)){?>
                               <label><input class="check_me" type="checkbox" name="employees_pay[]" value="<?php echo $prev_inc['id'] ?>">Pay</label> &nbsp;    
                            <?php } ?>                  
                             <a style="float:right" target="_blank" href="<?php echo base_url(); ?>salary/Action_salary/<?php echo $emp['id'] ?>"><i class='fa fa-eye' style="font-size:18px"></i></a>
                            
                        </td>
                        
                        
                        </tr>
                     <?php  }  
                     ?>
    </form>                 
                     <tr>
                     <td>-</td>
                    <td>Total</td>
                     <td><?php echo $sal; ?></td>
                     <td><?php echo $adddinc; ?></td>
                      <td>
                        <?php echo $tmp_bounes;?> 
                     </td>
                      <td>
                     <?php echo $provdgrand_ret; ?>
                     </td>
                     <?php 
                     if(!empty($allo))
                     {
                    foreach ($allo as $key => $value){
                     
                     ?>
                       <td>
                           <?php 
                          echo $value; 
                          
                           ?>
                           </td>
                     <?php } }else
                     {
                         foreach ($allonce as $key => $value) {?>
                            <th><?php echo "0"; ?></th>
                        <?php  }  }?>
                     <td>
                     <?php 
                     echo $grosgrand; ?>
                     </td>
                      <?php 
                      if(!empty($loans))
                     {
                    foreach ($loans as $key => $value){?>
                       <td><?php echo $value; ?></td>
                     <?php }}else
                     {
                         foreach ($loan_def as $key => $value) {?>
                            <th><?php echo "0"; ?></th>
                        <?php  } ?>
                       
                   <?php  }?>
                      <?php
                       if(!empty($ded))
                     {
                      foreach ($ded as $key => $value) {?>
                       <td><?php echo $value; ?></td>
                       <?php }}else
                     {
                         foreach ($deduction_def as $key => $value) {?>
                            <th><?php echo "0"; ?></th>
                        <?php  } ?>
                    
                   <?php  }?>
                     <?php
                  if(!empty($per))
                     {
                     foreach ($per as $key => $value) {?>
                       <td><?php echo $value; ?></td>
                     <?php }}else
                     {
                         foreach ($personal_exp as $key => $value) {?>
                            <th><?php echo "0"; ?></th>
                        <?php  } ?>
                   <?php  }?>

                       <td>
                     <?php echo $provdgrand; ?>
                     </td>
                     
                     
                     <td>
                     <?php 
                     echo $adgrand; 
                     ?>
                     </td>

                      <td>
                     <?php 
                    echo $daygrand; 
                     ?>
                     </td>
                     <td><?php echo $net; ?></td>
                     <td>-</td>
                     </tr>
                      </tbody>
                      </table>
                      </div >
                     </div>
                    </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/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>
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
    $(this).remove(); 
});
}, 3000);
    </script>
        <script>
    $(document).ready(function() {
   
    $('#all_check').change(function() {
        if($(this).is(":checked")) {
           $(".check_me").each(function () {
            
            $(this).prop('checked',true);
           
        });
        }else
        {
         $(".check_me").each(function () {
           
               $(this).removeAttr('checked');  
             });
            
        }//else
    });
});
    </script>
        <script>
        $(document).ready(function() {
        var t=$('#type').val();
var month=$('#month').val();
        $('#myDataTable').DataTable(
            {
            "bPaginate":false,
            "bFilter":true,
            "bInfo":false,
            "ordering":false,
            "scrollX":true,
            dom: 'Bfrtip',
            buttons: [
            'csv', 'excel', 'pdf','print']
            }
        );

       /*{text:'Print',action:function() {var win = window.open('<?php //echo base_url() ?>salary/printview/'+t+'/'+month, '_blank');
  win.focus();}}  */
        });
        
        </script>
