<?php
 $iduser=$_SESSION['user_idd'];
 $pno='';
$query=mysqli_query($con,"select * from Staffs where id='$iduser'");
while($row=mysqli_fetch_array($query))
{
$po=htmlentities($row['Position']);
}
 ?>
 <script>
function savepayroll() 
{
   var mon=document.getElementById('mon').value;
   var year=document.getElementById('year').value;
    
  if (confirm("All  data correct?... Submit") == true) {
    $.ajax({
           url:"savenewpayroll.php",
           method:"POST",
           data:{monn:mon,year:year},
           success:function(data)
           {
            if(data=='saved')
            {
                $('#savefeedback').html("Payroll for "+mon+' '+year+" created successfully").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');
                $('#savefeedbackmodal').modal('hide');
                }, 2000);    
            }
           }
          });
   
  } else {
  }
  //document.getElementById("demo").innerHTML = text;
}
</script>

<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">
    <?php
    $yr="";  
    $trm="";
    $grad="";
    
    $months='';
    
if(isset($_POST['filter']))
{
$mo=addslashes($_POST['mon']); 
$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 Month='$months' AND Year='$year'");
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'];  
}


$_SESSION['months']=$months;
$_SESSION['year']=$year;
}
?>


<?php
if($_SESSION['months']=='' || $_SESSION['months']==null)
{
    $_SESSION['months']='1';
}

if($_SESSION['year']=='' || $_SESSION['year']==null)
{
    $_SESSION['year']='1';
}

$months=$_SESSION['months'];
$year=$_SESSION['year'];

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



    <!-- 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">Manage Payroll</h4>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">Dashboard</li>
              <li class="breadcrumb-item ">Expenses</li>
              <li class="breadcrumb-item active">Employee Cost/Payroll</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
      <div class="container-fluid">
        <div class="row">
        <h6>Select Payment Period <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 id="msgstatement">.</label>
                       <input class="form-control" type="hidden" value="............" />
                     <br />
                      <button class="btn btn-primary btn-xs" name="filter"> <i class="fa fa-arrow-right"></i> Proceed </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($months !='1' || $year !='1')
              {
               ?>
                <h5 class="card-title">Payroll Report for:   <span id="spandata" class="badge bg-primary text-white"> <?php echo $months.' '.$year;  ?> </span></h5>
               <?php
             
                $query=mysqli_query($con,"SELECT * FROM payment WHERE Month='$months' AND Year ='$year'");
                $num=mysqli_fetch_array($query);
                if($num>0)
                {
                 ?>
                  <center><h5 class="alert-danger" >Pay Period already created</span></h5>
              <div class="table-responsive datatable-dashv1-list custom-datatable-overright">
               <?php
               $ap='';
                $query=mysqli_query($con,"select * from payment WHERE Month='$months' AND Year ='$year'");
                while($row=mysqli_fetch_array($query))
                {
                $ap=htmlentities($row['Approved']);
                }
                
                if($ap=='Yes')
                {
                ?>
                    <h6 class="text-success"><center>Payroll Approved</center></h6>
                    <?php
                }else
                {
                if($po=='System User' || $po=='School Director')  
                {
                    
               ?>
                <button class="btn btn-success" id="" onclick="functionapprove()"> <i class="fa fa-check" ></i> Approve Payroll</button>
                  || 
              <button class="btn btn-danger" id="cancelconfirm" onclick="myFunction()"> <i class="fa fa-close" ></i> Cancel Payroll</button>
              <?php
                }else{
                    
                }
                }
                ?>
              <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>
                        <th  data-field="counting">#</th>
                        <th  data-field="Name" data-editable="true">Staff Name</th>
                        <th  data-field="IDno" data-editable="true">ID No</th>
                        <th  data-field="basic" data-editable="true">Basic Salary</th>
                        <th  data-field="medical" data-editable="true">Medical Allowance</th>
                        <th  data-field="house" data-editable="true">House Allowance</th>
                        <th  data-field="gross" data-editable="true">Gross Salary</th>
                        <th  data-field="Hlevy" data-editable="true">House Levy</th>
                        <th  data-field="nhif" data-editable="true">SHA</th>
                        <th  data-field="nssf" data-editable="true">NSSF</th>
                        <th  data-field="advance" data-editable="true">Advance</th>
                        <th  data-field="deductions" data-editable="true">Total Deductions</th>
                        <th  data-field="Net Pay" data-editable="true">Net Pay</th>
                        <th  data-field="Nita" data-editable="true">NITA</th>
                        <th  data-field="Bank" data-editable="true">Bank</th>
                        <th  data-field="Act" data-editable="true">Account</th>
                        <th  data-field="nhif" data-editable="true"> SHA No.</th>
                        <th  data-field="nssf" data-editable="true">NSSF No</th>
                  </tr>
                </thead>
                <tbody style="padding:10px">
                  <?php 
                     $query=mysqli_query($con,"select Staffs.*, paydetails.Bank as bnk,paydetails.Account as act,payment.Gross as gross,payment.Basic as basic,payment.HLevy as hlev, 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 paydetails.Gross !='0' AND payment.Gross !='0' AND payment.Month='$months' AND payment.Year='$year' 
                     ORDER BY Staffs.PRNo 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><span  style="font-weight:bold;color:green"><?php echo htmlentities($row['basic']); ?></span></td>
                    <td><?php echo htmlentities($row['med']); ?></td>
                    <td><?php echo htmlentities($row['house']); ?></td>
                    <td class="alert-success"><?php echo htmlentities($row['gross']); ?></td>
                    <td class=""><?php echo htmlentities($row['hlev']); ?></td>
                    <td><?php echo htmlentities($row['nhif']); ?></td>
                    <td><?php echo htmlentities($row['nssf']); ?></td>
                    <td><?php echo htmlentities($row['ref']); ?></td>
                    <td class="alert-warning"><?php echo htmlentities($row['totald']); ?></td>
                    <td class="alert-success"><strong><?php echo htmlentities($row['net']); ?></strong></td>
                    <td><?php echo htmlentities($row['nita']); ?></td>
                    <td><?php echo htmlentities($row['bnk']); ?></td>
                    <td><?php echo htmlentities($row['act']); ?></td>
                    <td style=""><strong>  <?php echo htmlentities($row['NHIF']); ?> </strong></td>
                    <td style=""><strong>  <?php echo htmlentities($row['NSSF']); ?>  </strong></td>
                     <!--td>
                        <input type="hidden" value="<?php echo htmlentities($row['pcount']); ?>" class="form-control id" />
                        <i title="Delete Payroll" class="fa fa-trash text-red btn btn-delete hoverme" style=""></i> 
                    </td-->
                  </tr>
                  <?php
                   $cnt=$cnt+1;
                   }
                   ?>
                </tbody>
                <tfoot class="tfooot">
                    <tr class="tfooot">
                        <?php
                        $tg='';
                        $query=mysqli_query($con,"select SUM(Gross) as tg,SUM(NHIF) as nh,SUM(NSSF) as ns,SUM(TD) as td,SUM(Net) as net, SUM(NITA) as nita,SUM(HLevy) as hlevy from payment where Month='$months' AND Year='$year'");
                        while($row=mysqli_fetch_array($query))
                        {
                            $tg=htmlentities($row['tg']);
                            $ns=htmlentities($row['ns']);
                            $nh=htmlentities($row['nh']);
                            $td=htmlentities($row['td']);
                            $net=htmlentities($row['net']);
                            $nita=htmlentities($row['nita']);
                            $hle=htmlentities($row['hlevy']);
                        }
                        
                        ?>
                        
                    <td colspan="6">Total (KES)</td>
                     <td><strong style="font-size:18px"> <?php echo $tg; ?></strong></td>
                     <td><strong style="font-size:16px"> <?php echo $hle; ?></strong></td>
                     
                      <td><strong style="font-size:16px"> <?php echo $nh; ?></strong></td>
                       <td colspan="2"><strong style="font-size:16px"> <?php echo $ns; ?></strong></td>
                       <td><strong style="font-size:16px"> <?php echo $td; ?></strong></td>
                       <td><strong style="font-size:18px;color:red"> <?php echo $net; ?></strong></td>
                       <td><strong style="font-size:16px"> <?php echo $nita; ?></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
                {
                ?>
                  <!-- Add new Month payroll -->
                  
              <center><span class="badge bg-primary" >Create new payroll for  <?php echo $months.' '.$year;  ?> </span></center>
              <div class="table-responsive tableholder datatable-dashv1-list custom-datatable-overright">
                  <center><marquee behavior="alternate"><label class="text-red">Make sure you UPDATE each data changed before creating payroll for the new month </label></marquee></center>
                  <br />
                  <center><label id="savefeedback"></label></center>
             
              <table class="table datatable" id="tableee" 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:11px;font-family:Times">
                <thead>
                  <tr>
                    <th  data-field="counting">#</th>
                    <th  data-field="Name" data-editable="true">Staf Name</th>
                    <th  data-field="IDno" data-editable="true">ID No</th>
                    <th  data-field="basic" data-editable="true">Basic Salary</th>
                    <th  data-field="medical" data-editable="true">Medical Allowance</th>
                    <th  data-field="house" data-editable="true">House Allowance</th>
                    <th  data-field="gross" data-editable="true">Gross Salary</th>
                    <th  data-field="Hlevy" data-editable="true">House Lev</th>
                    <th  data-field="nhif" data-editable="true">SHA</th>
                    <th  data-field="nssf" data-editable="true">NSSF</th>
                    <th  data-field="advance" data-editable="true">Advance</th>
                    <th  data-field="deductions" data-editable="true">Total Deductions</th>
                    <th  data-field="Net Pay" data-editable="true">Net Pay</th>
                     <th  data-field="Nita" data-editable="true">NITA</th>
                     <th  data-field="Bank" data-editable="true">Bank</th>
                     <th  data-field="Act" data-editable="true">Account</th>
                     <th  data-field="Act" data-editable="true">SHA No.</th>
                     <th  data-field="Act" data-editable="true">NSSF No.</th>
                    <th  data-field="action">Action</th>
                  </tr>
                </thead>
                <tbody style="padding:10px;border:1px solid #000">
                  <?php 
                     $query=mysqli_query($con,"select Staffs.*,paydetails.HLevy as lev, paydetails.Bank as bnk,paydetails.Account as act,paydetails.Basic as basic, paydetails.Gross as gross,paydetails.nssf as nssf,
                     paydetails.nhif as nhif,paydetails.NITA as nita,paydetails.Refund as ref,paydetails.TD as totald,paydetails.NetPay as net,paydetails.Medical as medi,paydetails.House as house,paydetails.HLevy as
                     hlev from Staffs left join paydetails ON Staffs.id=paydetails.staff_id where Staffs.Status='Active' AND paydetails.Gross !='0' AND Staffs.Specia='0' AND Staffs.id !='82' ORDER BY Staffs.PRNo ASC");
                    $cnt=1;
                    while($row=mysqli_fetch_array($query))
                    {
                    ?> 
                  <tr>
                    <td><?php echo $cnt ?>  </td>
                    <td class="" style="padding-right:10px"><strong><?php echo htmlentities($row['Name']); ?></strong></td>
                    <td><strong><?php echo htmlentities($row['IDNo']); ?></strong></td>
                    <td class="alert-success"><input type="text" style='width:80px' onkeypress="return /[0-9]/i.test(event.key)" style="font-weight:bold;color:green" value="<?php echo htmlentities($row['basic']); ?>" class="form-control basic" /></td>
                    <td class="alert-success"><input type="text" onkeypress="return /[0-9]/i.test(event.key)" value="<?php echo htmlentities($row['medi']); ?>" class="form-control med" /></td>
                    <td class="alert-success"><input type="text" onkeypress="return /[0-9]/i.test(event.key)" value="<?php echo htmlentities($row['house']); ?>" class="form-control house" /> </td>
                    <td><strong style="font-weight:bold;color:green;font-size:16px"><?php echo $row['basic']+$row['house']+$row['medi']; ?></strong></td>
                    
                    <td>
                        <?php echo htmlentities($row['lev'])  ?> 
                        </td>
                    
                    <td>
                        <?php echo htmlentities($row['nhif']); ?> 
                        </td>
                    <td><?php echo htmlentities($row['nssf']); ?></td>
                    <td><input type="text" onkeypress="return /[0-9]/i.test(event.key)" value="<?php echo htmlentities($row['ref']); ?>" class="form-control ref" /></td>
                    <td class="alert-warning"><strong><?php echo $row['nssf']+$row['ref']+$row['nhif']+$row['lev'];  ?></strong></td>
                    <td class="alert-success"> 
                    <strong style="font-size:18px">
                    <?php echo ($row['basic']+$row['house']+$row['medi'])- ($row['nssf']+$row['nhif']+$row['ref']+$row['lev']); ?>
                    </strong>
                    </td>
                    <td><input type="text" onkeypress="return /[0-9]/i.test(event.key)" value="<?php echo htmlentities($row['nita']); ?>" class="form-control nita" />
                    </td>
            
                    <td style=""><strong>  <input type="text" value="<?php echo htmlentities($row['bnk']); ?>" class="form-control bank" />  </strong>
                    </td>
                    
                    <td style=""><strong>  <input type="text" value="<?php echo htmlentities($row['act']); ?>" class="form-control acc" />  </strong>
                    </td>
              
                    <td style=""><strong>  <?php echo htmlentities($row['NHIF']); ?> </strong>
                    </td>
                    
                    <td style=""><strong>  <?php echo htmlentities($row['NSSF']); ?>  </strong>
                    </td>
                  
                     <td>
                        <input type="hidden" value="<?php echo htmlentities($row['id']); ?>" class="form-control idd" />
                        <i title="Click to save" class="fa fa-save text-green btn btn-save hoverme" style=""></i> 
                    </td>
                  </tr>
                  <?php
                   $cnt=$cnt+1;
                   }
                   ?>
                </tbody>
                </table>
                 <button class="pull-right btn btn-info  btn-sm" id="addpayroll" onclick="savepayroll()"> <i class="fa fa-plus" ></i> Create Payroll
                 </button>
            </div>
                <?php
                }
              ?>
            
            <?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" />
            <input type="hidden" id="levy" />
    </section>
  
</div>

