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

use setasign\Fpdi\Fpdi;

// Get input values from the form submission
$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-weekNewV.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', '', 12);

// 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) {
        //Gross
        $earnings = $inputNumber * $Hoursworked;
        $gross1 = $earnings - 11.25 - 0;
        $gross2 = 175 * 0.138;  
        $gross3 = 1 + 0.138;
        $gross4 = $gross1 + $gross2;
        $grossfinal = $gross4 / $gross3;
        $gross = $grossfinal;
        
        //Holidaypay
        $holidaypay = $gross*0.1077;
        
        //empni
        if ($gross - 175 > 0){
            $employeeni = ($gross-175)*0.138;
        } else {
            $employeeni = 0;
        }
        $employeesni = $employeeni;
        
        //Totalcompanycost
        $companycost = 11.25 + $employeesni + $holidaypay;
        
        //Final Gross
        $finalGross = $earnings - $companycost;
        
        
        //Hours
        $hours = "$Hoursworked";
        $pdf->SetXY(54, 102);
        $pdf->Cell(40, 10, $hours);
        
        // Basic pay
        $basicpay = $hours * 11.44;
        $pdf->SetXY(100, 102);
        $pdf->Cell(40, 10, $basicpay);
        
        //DBSP Bonus
        $dbspbonus = $finalGross-$basicpay;
        $dbsp = number_format($dbspbonus, 2);
        $pdf->SetXY(100, 107);
        $pdf->Cell(40, 10, $dbsp);
        
        //Holidaypay Advance
        $hppay = number_format($holidaypay, 2);
        $pdf->SetXY(100, 112);
        $pdf->Cell(40, 10, $hppay);
        
        //Total payments
        $totalpayments = $basicpay+$dbsp+$hppay;
        $pdf->SetXY(100, 140);
        $pdf->Cell(40, 10, $totalpayments);
        
        //Tax
        $tax1 = $gross * 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 =$finaltax;
        $paye = "$finaltax1";
        $pdf->SetXY(185, 102);
        $pdf->Cell(40, 10, $paye);
        
        //National Insurance
        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);
        $pdf->SetXY(185, 107);
        $pdf->Cell(40, 10, $resultFieldValue);
        
        //Total deductions
        $totaldeductions = $paye+$resultFieldValue;
        $pdf->SetXY(185, 140);
        $pdf->Cell(40, 10, $totaldeductions);
        
        //Netpay
        $netpay = $result;
        $pdf->SetXY(185, 153);
        $pdf->SetTextColor(255, 255, 255);// Reset to white color
        $pdf->Cell(40, 10, $netpay);
        
        //Taxable pay
        $taxablepayweek = number_format($gross, 2);
        $pdf->SetTextColor(0, 0, 0);  // Reset to black color
        $pdf->SetXY(100, 174);
        $pdf->Cell(40, 10, $taxablepayweek);
        
        //tax
        $paye = "$finaltax1";
        $pdf->SetXY(100, 179);
        $pdf->Cell(40, 10, $paye);
        
        //niable pay
        $taxablepayweek = number_format($gross, 2);
        $pdf->SetTextColor(0, 0, 0);  // Reset to black color
        $pdf->SetXY(100, 184);
        $pdf->Cell(40, 10, $taxablepayweek);
        
        //NI
        $resultFieldValue = number_format($national, 2);
        $pdf->SetXY(100, 189);
        $pdf->Cell(40, 10, $resultFieldValue);
        
        //$employeesni
        $employeesnics = number_format($employeesni, 2);
        $pdf->SetXY(100, 194);
        $pdf->Cell(40, 10, $employeesnics);
        
        //
        //
        //Year To Date
        // Taxable pay
        $taxablepayweek = number_format($gross, 2);
        $taxablepayyear = $taxablepayweek+9527.17;
        $pdf->SetXY(185, 174);
        $pdf->Cell(40, 10, $taxablepayyear);
        
        //tax
        $paye = "$finaltax1";
        $payeyear = $paye+1373.2;
        $pdf->SetXY(185, 179);
        $pdf->Cell(40, 10, $payeyear);
        
        //niable pay
        $taxablepayweek = number_format($gross, 2);
        $taxablepayyear = $taxablepayweek+9527.17;
        $pdf->SetXY(185, 184);
        $pdf->Cell(40, 10, $taxablepayyear);
        
        //NI
        $resultFieldValue = number_format($national, 2);
        $niyear = $resultFieldValue+543.95;
        $pdf->SetXY(185, 189);
        $pdf->Cell(40, 10, $niyear);
        
        //$employeesni
        $employeesnics = number_format($employeesni, 2);
        $employeeniyear = $employeesnics+1049.08;
        $pdf->SetXY(185, 194);
        $pdf->Cell(40, 10, $employeeniyear);
        
    }
  
    // Modify the second page
    if ($pageNo == 2) {
        $pdf->SetFont('Arial', '', 12);
        
        //Hours
        $hours = "$Hoursworked";
        $pdf->SetXY(62, 73);
        $pdf->Cell(40, 10, $hours);
        
        //RAte
        $inputNumber = "$inputNumber";
        $pdf->SetXY(99, 73);
        $pdf->Cell(40, 10, $inputNumber);
        
        //$earnings
        $earnings = "$earnings";
        $pdf->SetXY(130, 73);
        $pdf->Cell(40, 10, $earnings);
        
        //company margin
        $cm = "11.25";
        $pdf->SetXY(130, 97);
        $pdf->Cell(40, 10, $cm);
        
        //$employeesni
        $employeesnics = number_format($employeesni, 2);
        $pdf->SetXY(130, 111);
        $pdf->Cell(40, 10, $employeesnics);
        
        //Holidaypay Advance
        $hppay = number_format($holidaypay, 2);
        $pdf->SetXY(130, 132);
        $pdf->Cell(40, 10, $hppay);
        
        //Total company cost
        $tcc = $cm+$employeesnics+$hppay;
        $pdf->SetXY(130, 147);
        $pdf->Cell(40, 10, $tcc);
        
        //Holidaypay Advance
        $hppay = number_format($holidaypay, 2);
        $pdf->SetXY(130, 168);
        $pdf->Cell(40, 10, $hppay);
        
        //Gross for Tax (A) - (B) + C
        $totalofabc = $earnings-$tcc+$hppay;
        $pdf->SetXY(130, 178);
        $pdf->Cell(40, 10, $totalofabc);
        
        //paye tax
        $paye = "$finaltax1";
        $pdf->SetXY(130, 192);
        $pdf->Cell(40, 10, $paye);
        
        //NI
        $resultFieldValue = number_format($national, 2);
        $pdf->SetXY(130, 201);
        $pdf->Cell(40, 10, $resultFieldValue);
        
        //Total detection
        $td = $paye+$resultFieldValue;
        $pdf->SetXY(130, 223);
        $pdf->Cell(40, 10, $td);
        
        //Netpay
        $netpay = $result;
        $pdf->SetXY(130, 232);
        $pdf->Cell(40, 10, $netpay);
    }
    

}

// Output the new PDF
$outputPath = 'public/ombrella_payslip.pdf';
$pdf->Output('F', $outputPath);

// Redirect to the modified PDF to display it
header('Location: ' . $outputPath);
exit;
?>