
<?php
require __DIR__ . '/vendor/autoload.php';

use setasign\Fpdi\Fpdi;

// Get input values from the form submission
//Hoursworked
$inputNumber = isset($_POST['inputNumber']) ? $_POST['inputNumber'] : '';
$Hoursworked = isset($_POST['Hoursworked']) ? $_POST['Hoursworked'] : '';
$result = isset($_POST['result']) ? $_POST['result'] : '';

// Path to the existing PDF
$pdfPath = __DIR__ . '/assets/Ombrella payslip-new.pdf';


// Check if the file exists
if (!file_exists($pdfPath)) {
    die('PDF file does not exist.');
}

// Create the 'public' directory if it doesn't exist
$outputDir = __DIR__ . '/public';
if (!is_dir($outputDir)) {
    mkdir($outputDir, 0777, true);
}

// Create a new FPDI instance
$pdf = new FPDI();

// Set font for the PDF
$pdf->SetFont('Arial', '', 10);

// Add pages from the existing PDF and modify the first page
$pageCount = $pdf->setSourceFile($pdfPath);
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($pageNo);
    $pdf->useTemplate($tplIdx, 0, 0);

    // Modify the first page
    if ($pageNo == 1) {
        //Hours Weekly
        $hoursworked = "37.5";
        $pdf->SetXY(140, 49);
        $pdf->Cell(40, 10, $hoursworked);
        
        //Hours Monthly
        $hoursmonthly = "150";
        $pdf->SetXY(188, 49);
        $pdf->Cell(40, 10, $hoursmonthly);
        
        //Rate_Weekly
        $rate = "$inputNumber";
        $pdf->SetXY(140, 57);
        $pdf->Cell(40, 10, $rate);
        
        //Rate_monthly
        $rate = "$inputNumber";
        $pdf->SetXY(188, 56);
        $pdf->Cell(40, 10, $rate);
        
        //Umbrella Income_Weekly
        $umbrellaincome = $inputNumber * 37.5;
        $pdf->SetXY(138, 84);
        $pdf->Cell(40, 10, $umbrellaincome);
        
        //Umbrella Income_monthly
        $umbrellaincome = $inputNumber * 150;
        $pdf->SetXY(188, 84);
        $pdf->Cell(40, 10, $umbrellaincome);
        
        //Employers Workplace Pension_Weekly
        $ewp = "0";
        $pdf->SetXY(138, 90);
        $pdf->Cell(40, 10, $ewp);
        
        //Employers Workplace Pension_Monthly
        $ewp = "0";
        $pdf->SetXY(188, 90);
        $pdf->Cell(40, 10, $ewp);
        
        //Employers NI (-) Weekly
        //Gross
        $earnings = $inputNumber * 37.5;
        $gross1 = $earnings - 11.25 - 0;
        $gross2 = 96 * 0.15;
        $gross3 = 1 + 0.15;
        $gross4 = $gross1 + $gross2;
        $grossfinalw = $gross4 / $gross3;
        $gross = $grossfinalw;
        if ($gross - 96 > 0) {
            $ersni = ($gross - 96) * 0.15;
            $result_ersni = number_format($ersni, 2);
        } else {
            $result_ersni = 0;
        }
        $employersni = "$result_ersni";
        $pdf->SetXY(138, 98);
        $pdf->Cell(40, 10, $employersni);
        
        //Employers NI (-) Monthly
        $earnings = $inputNumber * 150;
        $gross1 = $earnings - 45 - 0;
        $gross2 = 417 * 0.15;
        $gross3 = 1 + 0.15;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        
        if($grossfinal-417>0){
            $empnic = ($grossfinal-417)*0.15;
            $result_empnic = number_format($empnic, 2);
        }
        $employersni = "$result_empnic";
        $pdf->SetXY(188, 98);
        $pdf->Cell(40, 10, $employersni);
        
        
        //Apprenticeship Levy (-) Weekly
        $al = "0";
        $pdf->SetXY(138, 105);
        $pdf->Cell(40, 10, $al);
        
        //Apprenticeship Levy (-) Monthly
        $al = "0";
        $pdf->SetXY(188, 105);
        $pdf->Cell(40, 10, $al);
        
        //Umbrella Margin (-) weekly
        $um = "11.25";
        $pdf->SetXY(138, 113);
        $pdf->Cell(40, 10, $um);
        
        //Umbrella Margin (-) monthly
        $um = "45";
        $pdf->SetXY(188, 113);
        $pdf->Cell(40, 10, $um);
        
        //Gross Pay weekly
        $gpweek = number_format($gross, 2);
        $gp = "$gpweek";
        $pdf->SetTextColor(255, 255, 255);// Reset to white color
        $pdf->SetFont('Arial', '', 12);
        $pdf->SetXY(134, 121);
        $pdf->Cell(40, 10, $gp);
        
        //Gross Pay monthly
        $earnings = $inputNumber * 150;
        $gross1 = $earnings - 45 - 0;
        $gross2 = 417 * 0.15;
        $gross3 = 1 + 0.15;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        $grossmon = number_format($grossfinal, 2);
        $pdf->SetTextColor(255, 255, 255);// Reset to white color
        $pdf->SetFont('Arial', '', 12);
        $pdf->SetXY(184, 121);
        $pdf->Cell(40, 10, $grossmon);
        
        //Gross Pay weekly
        $gp1 = "$gpweek";
        $pdf->SetTextColor(0, 0, 0);  // Reset to black color
        $pdf->SetFont('Arial', '', 10);  // Set font size to 10 (or your desired size)
        $pdf->SetXY(137, 148);
        $pdf->Cell(40, 10, $gp1);
        
        //Gross Pay monthly
        $gp1 = "$grossmon";
        $pdf->SetTextColor(0, 0, 0);  // Reset to black color
        $pdf->SetFont('Arial', '', 10);  // Set font size to 10 (or your desired size)
        $pdf->SetXY(188, 148);
        $pdf->Cell(40, 10, $gp1);
        
        // NI weekly
        if ($gross < 242) {
            $national = 0;
        } elseif ($gross < 967) {
            $national = ($gross - 242) * 0.08;
        } else {
            $national = (967 - 242) * 0.08 + ($gross - 967) * 0.02;
        }
        $resultFieldValue = number_format($national, 2);
        $ni = "$resultFieldValue";
        $pdf->SetXY(137, 156);
        $pdf->Cell(40, 10, $ni);
        
        // NI monthly
        $earnings = $inputNumber * 150;
        $gross1 = $earnings - 45 - 0;
        $gross2 = 417 * 0.15;
        $gross3 = 1 + 0.15;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        if ($grossfinal < 1048) {
            $national = 0;
        } elseif ($grossfinal < 4189) {
            $national = ($grossfinal - 1048) * 0.08;
        } else {
            $national = (4189 - 1048) * 0.08 + ($grossfinal - 4189) * 0.02;
        }
        $resultFieldValuemonthly = number_format($national, 2);
        $nim = "$resultFieldValuemonthly";
        $pdf->SetXY(188, 156);
        $pdf->Cell(40, 10, $nim);
        
        //PAYE weekly
        $earnings = $inputNumber * 37.5;
        $gross1 = $earnings - 11.25 - 0;
        $gross2 = 96 * 0.15;
        $gross3 = 1 + 0.15;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        $gross12 = $grossfinal;
        $tax1 = $gross12 * 52;
        $tax2 = $tax1 - 12579;
        $tax3 = floor($tax2 / 52);
        $round = $tax3 * 52;
        $taxcalc = ($round < 0) ? 0 : (($round > 37700) ? 37700 * 0.2 : $round * 0.2);
        $hightax = ($round > 125140) ? 87440 * 0.4 : (($round > 37700) ? ($round - 37700) * 0.4 : 0);
        $addtax = ($round > 125140) ? ($round - 125140) * 0.45 : 0;
        $threetaxes = $taxcalc + $hightax + $addtax;
        $finaltax = $threetaxes / 52;
        $finaltax1 = number_format($finaltax, 2);
        $paye = "$finaltax1";
        $pdf->SetXY(137, 165);
        $pdf->Cell(40, 10, $paye);
        
        //PAYE monthly
        $earnings = $inputNumber * 150;
        $gross1 = $earnings - 45 - 0;
        $gross2 = 417 * 0.15;
        $gross3 = 1 + 0.15;
        $gross4 = $gross1 + $gross2;
        $grossfinalm = $gross4 / $gross3;
        $gross12 = $grossfinalm;
        $tax1 = $gross12 * 12;
        $tax2 = $tax1 - 12579;
        $tax3 = floor($tax2 / 12);
        $round = $tax3 * 12;
        $taxcalc = ($round < 0) ? 0 : (($round > 37700) ? 37700 * 0.2 : $round * 0.2);
        $hightax = ($round > 125140) ? 87440 * 0.4 : (($round > 37700) ? ($round - 37700) * 0.4 : 0);
        $addtax = ($round > 125140) ? ($round - 125140) * 0.45 : 0;
        $threetaxes = $taxcalc + $hightax + $addtax;
        $finaltax = $threetaxes / 12;
        $finaltax1 = number_format($finaltax, 2);
        $payem = "$finaltax1";
        $pdf->SetXY(188, 165);
        $pdf->Cell(40, 10, $payem);
        
        //Employees Workplace Pension (-) weekly
        $ewp = "0";
        $pdf->SetXY(137, 171);
        $pdf->Cell(40, 10, $ewp);
        
        //Employees Workplace Pension (-) monthly
        $ewp = "0";
        $pdf->SetXY(188, 171);
        $pdf->Cell(40, 10, $ewp);
        
        //weekly NETPAY
        $netpayweekly = $grossfinalw - $paye - $ni;
        
        //monthly netpay
        $netpaymonthly = $grossfinalm - $payem - $nim;
        
        //Take Home Pay weekly
        $takehomepay = "$netpayweekly";
        $finalnetpayw = number_format($takehomepay, 2);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFont('Arial', '', 12);
        $pdf->SetXY(136, 179);
        $pdf->Cell(40, 10, $finalnetpayw);
        
        //Take Home Pay weekly
        $takehomepaym = "$netpaymonthly";
        $finalnetpaym = number_format($takehomepaym, 2);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFont('Arial', '', 12);
        $pdf->SetXY(185, 179);
        $pdf->Cell(40, 10, $finalnetpaym);
        
    }
}

// Output the PDF directly to the browser
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="payslip.pdf"');
$pdf->Output('I'); // Display PDF in browser

exit; // Ensure no further output is sent

?>

