 <?php
 $iduser=$_SESSION['user_idd'];
$query=mysqli_query($con,"select * from Staffs where id='$iduser'");
while($row=mysqli_fetch_array($query))
{
$po=htmlentities($row['Position']);
}
 ?>

<script>
function myFunction() {
  var pno=document.getElementById('pholder').value;
  if (confirm("Are you sure you want to remove this payroll?") == true) {
   
    $.ajax({
           url:"deletepayroll.php",
           method:"POST",
           data:{pno:pno},
           success:function(data)
           {
            if(data=='yes')
            {
                $('#savefeedback').html("Payroll deleted successfully").css('color', 'red');
              
                setInterval(function ()
                {
                 window.location.href='Expenditure.php?page=Payroll';
                
                }, 2000);   
            }else
            {
             $('#savefeedback').html(data).css('color', 'red');
             
                setInterval(function ()
                {
                $('#savefeedback').html("").css('color', 'red');
                }, 2000);    
            }
           }
          });
   
  } else {
   
  }
  //document.getElementById("demo").innerHTML = text;
}
</script>

<script>
function functionapprove() {
  var pno=document.getElementById('pholder').value;
  if (confirm("Approve this Payroll?  NB: Approved Payroll cannot be cancelled!!") == true) {
   
    $.ajax({
           url:"approve.php",
           method:"POST",
           data:{pno:pno},
           success:function(data)
           {
            if(data=='yes')
            {
                $('#savefeedback').html("Payroll has been Approved").css('color', 'green');
              
                setInterval(function ()
                {
                 window.location.href='Expenditure.php?page=Payroll';
                
                }, 2000);   
            }else
            {
             $('#savefeedback').html(data).css('color', 'red');
             
                setInterval(function ()
                {
                $('#savefeedback').html("").css('color', 'red');
                }, 2000);    
            }
           }
          });
   
  } else {
  }
  //document.getElementById("demo").innerHTML = text;
}
</script>


<div class="content-wrapper" >
    <div class="container" style="border:1px solid #008000">
<?php
$yr="";  
$trm="";
$grad="";
$months='';
    
if(isset($_POST['filter']))
{
$mo=addslashes($_POST['mon']); 
$ord=addslashes($_POST['ooder']); 

$monn=substr($mo, -2);
$mon=substr($mo, -2);
$year=substr($mo, -7,4);

if($mon=='01'){$months='January';}
else if($mon=='02'){$months='February';}
else if($mon=='03'){$months='March';}
else if($mon=='04'){$months='April';}
else if($mon=='05'){$months='May';}
else if($mon=='06'){$months='June';}
else if($mon=='07'){$months='July';}
else if($mon=='08'){$months='August';}
else if($mon=='09'){$months='September';}
else if($mon=='10'){$months='October';}
else if($mon=='11'){$months='November';}
else if($mon=='12'){$months='December';}


$query=mysqli_query($con,"select * from payment where Year='$year' and Month='$months'");
while($row=mysqli_fetch_array($query))
{
  $pno=$row['Pay_No'];  
}

$query=mysqli_query($con,"select * from Staffs where id='$iduser'");
while($row=mysqli_fetch_array($query))
{
  $pos=$row['Postion'];  
}
}
?>
    <!-- Content Header (Page header) -->
    
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h4 class="m-0">NSSF/NHIF Reports  </h4>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">Dashboard</li>
              <li class="breadcrumb-item ">Reports</li>
              <li class="breadcrumb-item active">NSSF/NHIF Reports</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
      <div class="container-fluid">
        <div class="row">
        <h6>Filter Data  <i class="fa fa-calendar"></i></h6>
          </div>
         </div>
    </section>
          
    
    <section class="content" style="text-decoration:none;font-weight:normal;font-family:Times;font-size:12px">
        <form action="" method="POST">
          <div class="row">
              
             <div class="col-md-2">
                    <div class="form-group">
                    <label for="inputStatus">Select Payment Month <span class="requireds">*</span></label>
                    <input type="month" class="form-control" name="mon" required />
                    </div>
               </div>     
             <div class="col-md-2">       
                    <div class="form-group">
                        <label for="inputStatus">Order By <span class="text-danger">*</span></label>
                        <select name="ooder" class="form-control custom-select" required>
                        <option selected disabled value="">---</option>
                        <option value="Priority">Superiority</option>
                        <option value="Asc">Names Ascending</option>
                        </select>
                     </div>
             </div>
             
              <div class="col-md-2">
                   <div class="form-group">
                       <label id="msgstatement">.</label>
                       <input class="form-control" type="hidden" value="............" />
                     <br />
                      <button class="btn btn-success btn-xs" name="filter"> <i class="fa fa-arrow-right"></i> Submit </button>
                   </div>
              </div>
             <hr />
          </div>
         </form>
         
           <div class="row">
            <div class="col-md-12 col-sm-12 col-lg-12">
                  <div class="card-body">
              <!--h5 class="card-title">Manage Salaries</h5 -->
              <hr />
              <!-- Table with stripped rows -->
              
              <?php
              if(isset($_POST['filter']))
              {
               $ord=addslashes($_POST['ooder']); 
               
               if($ord=='Priority')
               {
                  $pri="Order By Staff's Superiority";
                 $query=mysqli_query($con,"SELECT * FROM payment WHERE Month='$months' AND Year ='$year'");  
               }else if($ord=='Asc')
               {
                 $query=mysqli_query($con,"SELECT * FROM payment WHERE Month='$months' AND Year ='$year' ");  
                  $pri="Order by Staff's Names ascending";
               }
                $num=mysqli_fetch_array($query);
                if($num>0)
                {
                 ?>
                <h5 class="card-title">Report for:   <span id="spandata" class="badge bg-primary text-white"> <?php echo $months.' '.$year.' -- '.$pri;  ?> </span>
                </h5>

              <div class="table-responsive datatable-dashv1-list custom-datatable-overright">
              <div class="divpayment" id="divpayment">
              <table class="table datatable" id="tablepayment" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"                                 data-cookie-id-table="saveId"   data-toolbar="#toolbar" style="font-size:12px">
                <thead>
                  <tr class="bg-secondary">
                    <th  data-field="counting">#</th>
                    <th  data-field="Name" data-editable="true">Names</th>
                    <th  data-field="IDno" data-editable="true">ID No</th>
                     <th  data-field="nhif" data-editable="true">NHIF No.</th>
                     <th  data-field="nhif" data-editable="true">NHIF Amt</th>
                    <th  data-field="nssf" data-editable="true">NSSF No.</th>
                    <th  data-field="nssf" data-editable="true">NSSF Amt</th>
                  </tr>
                </thead>
                <tbody style="padding:10px">
                  <?php 
                  
              if($ord=='Priority')
               {
                $query=mysqli_query($con,"select Staffs.*, paydetails.Bank as bnk,paydetails.Account as act,payment.Gross as gross,payment.Basic as basic, payment.NSSF as nssf,payment.NHIF as nhif,payment.House as house,payment.Refund as ref,payment.Medical as med,payment.NITA as nita,payment.TD as totald,payment.Net as net,payment.count as pcount from Staffs left join paydetails ON Staffs.id=paydetails.staff_id left join payment ON payment.Staff_id=Staffs.id where Staffs.Status='Active' AND payment.Month='$months' AND payment.Year='$year' ORDER BY PRNo ASC");
               }else if($ord=='Asc')
               {
                 $query=mysqli_query($con,"select Staffs.*, paydetails.Bank as bnk,paydetails.Account as act,payment.Gross as gross,payment.Basic as basic, payment.NSSF as nssf,payment.NHIF as nhif,payment.House as house,payment.Refund as ref,payment.Medical as med,payment.NITA as nita,payment.TD as totald,payment.Net as net,payment.count as pcount from Staffs left join paydetails ON Staffs.id=paydetails.staff_id left join payment ON payment.Staff_id=Staffs.id where Staffs.Status='Active' AND payment.Month='$months' AND payment.Year='$year' ORDER BY Name ASC"); 
               }
                  
                     
                     
                     
                    $cnt=1;
                    while($row=mysqli_fetch_array($query))
                    {
                    ?> 
                  <tr>
                    <td><?php echo $cnt ?>  </td>
                    <td class="bg-secondary" style="padding-right:10px"><strong><?php echo htmlentities($row['Name']); ?></strong></td>
                    <td><strong><?php echo htmlentities($row['IDNo']); ?></strong></td>
                     <td style="" class="alert-success"><strong>  <?php echo htmlentities($row['NHIF']); ?> </strong>
                    </td>
                    <td class="alert-success"><?php echo htmlentities($row['nhif']); ?></td>
                    
                    <td style=""><strong>  <?php echo htmlentities($row['NSSF']); ?>  </strong>
                    </td>
                    <td><?php echo htmlentities($row['nssf']); ?></td>
                  </tr>
                  <?php
                   $cnt=$cnt+1;
                   }
                   ?>
                </tbody>
                <tfoot class="tfooot">
                    <tr class="tfooot alert-danger">
                        <?php
                        $query=mysqli_query($con,"select Staffs.*, (select SUM(NHIF) from payment where Month='$months' AND Year='$year') as nh,(select SUM(NSSF) from payment where Month='$months' AND Year='$year') as ns from Staffs left join payment ON payment.Staff_id=Staffs.id where Staffs.Status='Active'");
                        while($row=mysqli_fetch_array($query))
                        {
                            $ns=htmlentities($row['ns']);
                            $nh=htmlentities($row['nh']);
                            $nh=$nh-600;
                            $ns=$ns-1080;
                        }
                        ?>
                        
                    <td colspan="4">Total (KES)</td>
                      <td><strong style="font-size:16px"> <?php echo $nh; ?></strong></td>
                      <td></td>
                       <td colspan="2"><strong style="font-size:16px"> <?php echo $ns; ?></strong>
                       </td>
                    </tr>
                </tfoot>
                </table>
                <table>
                 <tr>
                    <td colspan="11"><button class="btn btn-success" id="paymentexporttoexcel"><i class="fa fa-cloud-download" ></i> Export to Excel</button>
                    </td>
                    </tr>
                </table>
              </div>
            </div>
                 <?php
                }else
                {
                echo "NO PAYMENT DETAILS FOUND";
                }
              ?>
            
            <?php
              }else
              {
              ?>
              <center><h5>No Payment Period Selected <i class="fas fa-radiation"></i> !!!</h5></center>
              <?php
              }
              ?>
            </div>
 
              </div>
              
            </div>
            <input type="hidden" value="<?php  echo $pno; ?>" id="pholder" />
             <input type="hidden" value="<?php  echo $months; ?>" id="mon" />
             <input type="hidden" value="<?php  echo $year; ?>" id="year" />
            <input type="hidden" id="tototod" />
            <input type="hidden" id="tototon" />
            
            <input type="hidden" id="tototonh" />
            <input type="hidden" id="tototons" />
    </section>
  
</div>
</div>

