<?php
include('../include/config.php');
error_reporting(0);
session_start();
$iduser=$_SESSION['user_idd'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$yr=$date->format('Y');
$month=$date->format('M');
$pano='';
$months='';
//$daa=$date->format('l jS \of F Y');

function createRandomPassword() {
	$chars = "003232303232023232023456789";
	srand((double)microtime()*1000000);
	$i = 0;
	$pass = '' ;
	while ($i <= 7) 
	{
		$num = rand() % 33;
		$tmp = substr($chars, $num, 1);
		$pass = $pass . $tmp;
		$i++;
	}
	return $pass;
}
$pano='P_'.createRandomPassword();

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

 $query=mysqli_query($con,"select * from companydata");
 while($row=mysqli_fetch_array($query))
 {
   $emma=htmlentities($row['cemail']);  
   $pasw=htmlentities($row['cpassword']);
 }
 
 $quer=mysqli_query($con,"SELECT * FROM Staffs where Position='School Director'");
 while($row=mysqli_fetch_array($quer))
{
//$em=$row['Email'];
$em='jamesmuriithiwanja@gmail.com';
}


if(isset($_POST['monn']))
{
$yrr=addslashes($_POST['year']);
$months=$_POST['monn'];

$mon=substr($pay, -2);
$year=substr($pay, -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';}

$query2=mysqli_query($con,"UPDATE paydetails SET PAYDATE='$timenow2',PAYMONTH='$months',Ent_Date='$timenow2',Year='$yrr',payment_user='$iduser',Pay_No='$pano'");
if($query2)
{
    
$query3=mysqli_query($con,"INSERT INTO payment(Staff_id,HLevy,Basic,Bank,Gross,Month,PayDate,Status,EntryDate,Year,User,Pay_No,Account,House,Medical,NITA,NSSF,NHIF,Net,Paye,Refund,TD) SELECT Staffs.id,paydetails.HLevy,paydetails.Basic, paydetails.Bank,paydetails.Gross,paydetails.PAYMONTH, paydetails.PAYDATE, paydetails.Statuss, paydetails.Ent_Date, paydetails.Year, paydetails.payment_user, paydetails.Pay_No, paydetails.Account, paydetails.House, paydetails.Medical, paydetails.NITA,paydetails.nssf, paydetails.nhif, paydetails.NetPay, paydetails.paye,paydetails.Refund,paydetails.TD FROM paydetails join Staffs ON Staffs.id=paydetails.staff_id WHERE paydetails.Basic !='' AND Staffs.Status='Active'");

$quer=mysqli_query($con,"SELECT * FROM paydetails where Refund !='0'");
 while($row=mysqli_fetch_array($quer))
{
$emp=$row['staff_id'];
$refu=$row['Refund'];
$vo=$row['AVN'];

$monn=$months.' '.$yrr;
}

$msggg="created a new payroll report for ". $months.' '.$yrr;
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msggg','$timenow','$timenow2','saved','$month')");
if($sql3)
{ 
echo "saved";

 $mail = new PHPMailer(true);
 $mail->IsSMTP();
 $mail->Host = 'smtp.gmail.com';
 $mail->SMTPAuth= true;
 $mail->Username =$emma;
 $mail->Password = $pasw;
 $mail->SMTPSecure = 'tls';
 $mail->Port = 587;
 
 $mail->setFrom($emma);
 $mail->addAddress($em);
 $mail->isHTML(true);
 $mail->Subject = "NEW PAYROLL - Approval Required";
 $mail->Body = "Payroll for the period: <strong>".$months." ".$yrr."</strong> has been successfuly created. "."<br /> Kindly login to the system to APPROVE or CANCEL it. <br />"."Click this link to visit the website: bishopkairoeducationcentre.sc.ke/QB-Beta/";

 $mail->send();
 
}
}else
{
    echo 'Sorry. The system encountered a problem while creating payroll !!';
}
}
?>