<?php
if (isset($_GET['salaryMonth'])) {
    $salaryMonth = $_GET['salaryMonth'];
    $userId = $_GET['userId'];
    $netAttendance = $_GET['netAttendance'];
    $earning1 = $_GET['er1'];
    $earning2 = $_GET['er2'];
    $earning3 = $_GET['er3'];
    $earning4 = $_GET['er4'];
    $grossSalary = $_GET['grS'];
    $deduction1 = $_GET['ded1'];
    $deduction2 = $_GET['ded2'];
    $deduction3 = $_GET['ded3'];
    $deduction4 = $_GET['ded4'];
    $deduction5 = $_GET['ded5'];
    $totalAmount = $_GET['total'];
    $totalEarnings = $_GET['totalE'];
    $totalDeductions = $_GET['totalDed'];
    $paySlipId = $_GET['paySlipId'];

}

require_once '../../common/connectToDB.php';
include_once('../../../fpdf/fpdf.php');
require('../../../fpdf/customPdf.php');

$sqlC = "SELECT * FROM companymaster LIMIT 1";
$resultC = mysqli_query($conn, $sqlC) or die("database error:" . mysqli_error($conn));
if ($resultC->num_rows > 0) {
    while ($row = $resultC->fetch_assoc()) {
        $companyName            = $row['companyname'];
        $companyAddress         = $row['address'];
        $companyEmail           = $row['companyEmail'];
        $companyContactNumber   = $row['contactnumber'];
    }
}
$sql = "SELECT * FROM logindetail WHERE userid = '$userId'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $firstRow = $row;
        $userName = $firstRow['firstname'] . ' ' . $firstRow['lastname'];
        $designation = $firstRow['user_type'];
        $location = $firstRow['city'];
        $dateOfJoining = $firstRow['dateOfJoining'];
        $dateOfJoining = date('d-m-Y', strtotime($dateOfJoining));
        $pancardNo = $firstRow['pancardNo'];
    }
}

$earning = [];
$deductions = [];
$sql3 = "SELECT * FROM seriesnumber where seriesName IN ('Earning','Deductions');";
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
    while ($row3 = $result3->fetch_assoc()) {
        if ($row3['seriesName'] == 'Earning') array_push($earning, $row3['seriesValue']);
        if ($row3['seriesName'] == 'Deductions') array_push($deductions, $row3['seriesValue']);
    }
}
$class_obj = new numbertowordconvertsconver();
$amountInWords = 'Rupees '.$class_obj->convert_number($totalAmount).' Only';

class PDF extends PDF_Rotate
{
    function Header()
    {
        global $x, $companyName, $companyEmail,$companyAddress, $userId, $userName, $salaryMonth, $designation, $location, $dateOfJoining, $pancardNo, $netAttendance, $companyContactNumber, $earning, $deductions, $grossSalary, $earning1, $earning2, $earning3, $earning4, $deduction1, $deduction2, $deduction3, $deduction4, $deduction5, $totalAmount,$totalEarnings,$totalDeductions,$amountInWords,$paySlipId;
        $image = "../../../dist/img/newBodheLogo.png";
        $callIcon = "../../../dist/img/callIcon.png";
        $websiteIcon = "../../../dist/img/websiteIcon.png";
        $emailIcon = "../../../dist/img/emailIcon.png";

        // watermark
        $this->SetFont('Arial', 'B', 50);
        $this->SetTextColor(254, 229, 229);
        $this->RotatedText(40, 65, 'B I E', 0);
        $this->RotatedText(25, 95, 'I N D I A', 0);
     

        $this->Ln(5);
        $x = $this->getX();
        $y = $this->getY();
        // $this->setXY($x-5, $y);
        // $this->Image($image,47, 134, 25, 20);
        // $this-> SetFont("Arial","",0);
        $this-> Image($image,43,-7,35,35);


        // heading
        $this->Ln(5);
        $this->SetFont('Arial', 'B', 10);
        $this->SetFontSize(8);
        $x = $this->getX();
        $y = $this->getY();
        $this->setXY($x - 5, $y-10);
        $this->SetTextColor(95, 97, 95);
        $this->Cell(110, 20, 'PAY SLIP', 0, 0, 'C');

        //new line
        $this->SetFont('Arial', 'B', 10);
        $this->Ln(13);
        $this->setXY($x - 5,$this->getY());
        $this->SetFontSize(8);
        $this->SetTextColor(255, 255, 255);
        $this->SetFillColor(248, 83, 20);
        $this->Cell(110, 5, $companyName, 0, 1, 'C', 1);
        $this->setX($x - 5);
        $this->SetFont('Arial', 'B', 10);
        $this->SetFontSize(5);
        $this->Cell(110, 5, '', 0, 0, 'C', 1);
        $this->setX($x - 5);
        $this->Cell(110, 5, $companyAddress, 0, 1, 'C', 1);

        $this->Ln(4);
        //  $x2 = $this->getX();
        $y2 = $this->getY();
        $this->setXY($x - 5, $y2);
        $this->SetFont('Arial', 'BU', 10);
        $this->SetFontSize(10);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(110, 5, $salaryMonth, 0, 0, 'C');

        $this->Ln(10);
        $this->SetFont('Arial', 'B', 10);
        $y2 = $this->getY();
        $this->setXY($x - 5, $y2);
        $this->SetFontSize(7);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(20, 5, 'Employee Name :', 0, 0, 'L');
        $this->SetTextColor(95, 97, 95);
        $this->Cell(27, 5, $userName, 0, 0, 'R');
        $this->setXY($x + 40, $y2);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(40, 5, 'Employee ID. :', 0, 0, 'R');
        $this->SetTextColor(95, 97, 95);
        $this->Cell(28, 5, $userId, 0, 0, 'L');


        $this->SetFont('Arial', 'B', 10);
        $this->Ln(5);
        $y2 = $this->getY();
        $this->setXY($x - 5, $y2);
        $this->SetFontSize(7);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(20, 5, 'Centre Location :', 0, 0, 'L'); //breakups of 96(45,25,25)
        $this->SetTextColor(96, 97, 96);
        $this->Cell(30, 5, 'THANE/KHANDIVALI', 0, 0, 'R'); //breakups of 96(45,25,25)
        // $this->setXY($x + 40, $y2);
        // $this->SetTextColor(0, 0, 0);
        // $this->Cell(40, 5, 'Date of Joining :', 0, 0, 'R'); //breakups of 96(45,25,25)
        // $this->SetTextColor(96, 97, 96);
        // $this->Cell(28, 5, $dateOfJoining, 0, 0, 'L'); //breakups of 96(45,25,25)
        


        

        


        // $this->Ln(5);
        // $y2 = $this->getY();
        // $this->setXY($x - 5, $y2);
        // $this->SetFontSize(7);
        // $this->SetTextColor(96, 97, 96);
        // $this->Cell(49, 5, $designation, 0, 0, 'R'); //breakups of 96(45,25,25)
        // $this->setXY($x + 40, $y2);
        // $this->SetTextColor(96, 97, 96);
        // $this->Cell(20, 5, '', 0, 0, 'L'); //breakups of 96(45,25,25)

        $this->Ln(5);
        $this->SetFont('Arial', 'B', 10);
        $y2 = $this->getY();
        $this->setXY($x - 5, $y2);
        $this->SetFontSize(7);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(20, 5, 'Payable Days :', 0, 0, 'L'); //breakups of 96(45,25,25)
        $this->SetTextColor(96, 97, 96);
        $this->Cell(4, 5, $netAttendance, 0, 0, 'R'); //breakups of 96(45,25,25)
        $this->setXY($x + 40, $y2);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(40, 5, 'PAN No. :', 0, 0, 'R'); //breakups of 96(45,25,25)
        $this->SetTextColor(96, 97, 96);
        $this->Cell(20, 5, $pancardNo, 0, 0, 'L'); //breakups of 96(45,25,25)

        
        $this->SetFont('Arial', 'B', 10);
        $this->Ln(5);
        $y2 = $this->getY();
        $this->setXY($x - 5, $y2);
        $this->SetFontSize(7);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(20, 5, 'Designation :', 0, 0, 'L');
        $this->SetTextColor(96, 97, 96);
        $this->Cell(50,5, $designation, 0, 0, 'R');  //breakups of 96(45,25,25)
        
        $this->setXY($x + 40, $y2);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(40, 5, 'Pay Slip No. :' , 0, 0, 'R');
        $this->SetTextColor(96, 97, 96);
        $this->Cell(20, 5, $paySlipId, 0, 0, 'L'); //breakups of 96(45,25,25)
      

        // table header
        $this->Ln(10);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', 'B', 10);
        $this->SetFontSize(9);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(30, 5, 'Salary Heads', 'LTB', 0, 'C');
        $this->Cell(25, 5, 'Amount', 'TBR', 0, 'C');
        $this->Cell(30, 5, 'Deductions', 'LTB', 0, 'C');
        $this->Cell(25, 5, 'Amount', 'TBR', 0, 'C');

        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', '', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        if ($grossSalary > 0) {
            $this->Cell(30, 5, $earning[0], 'L', 0, 'C');
            $this->Cell(25, 5, $grossSalary, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }
        if ($deduction1 > 0) {
            $this->Cell(30, 5, $deductions[0], 'L', 0, 'C');
            $this->Cell(25, 5, $deduction1, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }


        // GrossSalary
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', '', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        if ($earning1 > 0) {
            $this->Cell(30, 5, $earning[1], 'L', 0, 'C');
            $this->Cell(25, 5, $earning1, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }
        if ($deduction2 > 0) {
            $this->Cell(30, 5, $deductions[1], 'L', 0, 'C');
            $this->Cell(25, 5, $deduction2, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }


        //Earning1
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', '', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        if ($earning2 > 0) {
            $this->Cell(30, 5, $earning[2], 'L', 0, 'C');
            $this->Cell(25, 5, $earning2, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }
        if ($deduction3 > 0) {
            $this->Cell(30, 5, $deductions[2], 'L', 0, 'C');
            $this->Cell(25, 5, $deduction3, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }
        //Earning2
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', '', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        if ($earning3 > 0) {
            $this->Cell(30, 5, $earning[3], 'L', 0, 'C');
            $this->Cell(25, 5, $earning3, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }
        if ($deduction4 > 0) {
            $this->Cell(30, 5, $deductions[3], 'L', 0, 'C');
            $this->Cell(25, 5, $deduction4, 'R', 0, 'C');
        } else {
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');
        }

        //Earning3
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', '', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        if($earning4 > 0){
        $this->Cell(30, 5, $earning[4], 'L', 0, 'C');
        $this->Cell(25, 5, $earning4, 'R', 0, 'C');
        }else{
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');  
        }
        if($deduction5 > 0){
        $this->Cell(30, 5, $deductions[4], 'L', 0, 'C');
        $this->Cell(25, 5, $deduction5, 'R', 0, 'C');
        }else{
            $this->Cell(30, 5, '', 'L', 0, 'C');
            $this->Cell(25, 5, '', 'R', 0, 'C');  
        }
        //Earning4
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', 'B', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(30, 5, '', 'BL', 0, 'C');
        $this->Cell(25, 5, '', 'RB', 0, 'C');
        $this->Cell(30, 5, '', 'BL', 0, 'C');
        $this->Cell(25, 5, '', 'RB', 0, 'C');

        //Earning4
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', '', 10);
        $this->SetFontSize(8);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(30, 5,'NET SALARY', 'BL', 0, 'C');
        $this->Cell(25, 5, $totalEarnings, 'RB', 0, 'C');
        $this->Cell(30, 5, 'Total Deductions', 'BL', 0, 'C');
        $this->Cell(25, 5, $totalDeductions, 'RB', 0, 'C');


        //Earning4
        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', 'B', 10);
        $this->SetFontSize(9);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(30, 5, 'Total Amount', 'BL', 0, 'C');
        $this->Cell(25, 5, '', 'B', 0, 'C');
        $this->Cell(30, 5, '', 'B', 0, 'C');
        $this->Cell(25, 5, $totalAmount .'/-', 'RB', 0, 'C');

        $this->Ln(5);
        $x1 = $this->getX();
        $y1 = $this->getY();
        $this->setXY($x1 - 6, $y1);
        $this->SetFont('Arial', 'B', 10);
        $this->SetFontSize(9);
        $this->SetTextColor(0, 0, 0);
        $this->Cell(30, 5, ' Amount(In words):', 'BL', 0, 'C');
        $this->Cell(80, 5,$amountInWords , 'RB', 0, 'LR');


        

        //  // new line
        //  $this->Ln(10);
        //  $this->setXY($x-5,120.00125);
        //  $this->SetFont('Arial', 'B', 10);
        //  $this->SetFontSize(7);
        //  $this->SetTextColor(0, 0, 0);
        //  $this->Cell(80, 10, '#This is a computer generated pay slip & does not required signature or any company seal.', 0, 0, 'L');
 

        // new line
        $this->SetFont('Arial', 'B', 10);
        $this->Ln(10);
        $this->setXY($x - 5, 130.00125);
        $this->SetFontSize(8);
        $this->SetTextColor(255, 255, 255);
        $this->SetFillColor(250, 90, 30);


        $this->Cell(22, 5, $companyContactNumber, 0, 0, 'R', 1); //break up of 95 is 22 and 33 and 40
        $this->SetAlpha(0.5);

        if (file_exists($image)) $this->Image($callIcon, $x - 3, 130.60125, 3, 3);
        $this->SetAlpha(1);

        $this->Cell(38, 5, 'www.bieindia.com', 0, 0, 'R', 1);
        $this->SetAlpha(0.5);

        if (file_exists($image)) $this->Image($websiteIcon, $x + 25, 130.60125, 4, 4);
        $this->SetAlpha(1);

        $this->Cell(50, 5, 'accounts@edj-tcsc.com', 0, 0, 'R', 1);
        $this->SetAlpha(0.5);

        if (file_exists($image)) $this->Image($emailIcon, $x + 68, 131.00125, 3, 3);
        $this->SetAlpha(1);
    }
    // 
}


$pdf = new PDF('P', 'mm', array(120, 150));
$pdf->AddPage();
$pdf->AliasNbPages();
$pdf->SetFont('Arial', 'B', 13);
$col = 0;


// $pdf = new PDF('P','mm',array(105,148));
// $pdf->AddPage();
// $pdf->AliasNbPages();
// $pdf->SetFont('Arial', 'B', 13);
// $pdf->Ln(6);
// $pdf->SetFontSize(10);
// $dir = '../../../dist/documents/EmployeePaymentSlip/';

if (!file_exists('../../../dist/documents/EmployeePaymentSlip/' . $userId . '')) {
    mkdir('../../../dist/documents/EmployeePaymentSlip/' . $userId . '', 0777, true);
    $dir = '../../../dist/documents/EmployeePaymentSlip/' . $userId . '/';
} else {
    $dir = '../../../dist/documents/EmployeePaymentSlip/' . $userId . '/';
}
// $fileName = round(rand()) . '.pdf';
// $pdf->Output($dir . $fileName, 'F');
// $filepath = $dir . $fileName;


$fileName = $paySlipId.'.pdf';
$pdf->Output($dir.$fileName,'F');
$filepath=$dir.$fileName;



// $fileName = $userId.'.pdf';
// $pdf->Output($dir.$fileName,'F');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filepath . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile($filepath);

$conn->begin_transaction();


$sql="UPDATE user_payment set receiptNo='$paySlipId',paySlip='$fileName' where userid='$userId' AND salaryMonth='$salaryMonth';";
// $rr3 = mysqli_query($conn, $sql);


$sql1 = "UPDATE seriesnumber SET seriesValue= seriesValue + 1 WHERE seriesName ='paySlipId';";
        // $result2 = mysqli_query($conn, $sql1);
$conn->commit();

class numbertowordconvertsconver {
    function convert_number($number) 
    {
        if (($number < 0) || ($number > 999999999)) 
        {
            throw new Exception("Number is out of range");
        }
        $giga = floor($number / 1000000);
        // Millions (giga)
        $number -= $giga * 1000000;
        $kilo = floor($number / 1000);
        // Thousands (kilo)
        $number -= $kilo * 1000;
        $hecto = floor($number / 100);
        // Hundreds (hecto)
        $number -= $hecto * 100;
        $deca = floor($number / 10);
        // Tens (deca)
        $n = $number % 10;
        // Ones
        $result = "";
        if ($giga) 
        {
            $result .= $this->convert_number($giga) .  "Million";
        }
        if ($kilo) 
        {
            $result .= (empty($result) ? "" : " ") .$this->convert_number($kilo) . " Thousand";
        }
        if ($hecto) 
        {
            $result .= (empty($result) ? "" : " ") .$this->convert_number($hecto) . " Hundred";
        }
        $ones = array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
        $tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eigthy", "Ninety");
        if ($deca || $n) {
            if (!empty($result)) 
            {
                $result .= " and ";
            }
            if ($deca < 2) 
            {
                $result .= $ones[$deca * 10 + $n];
            } else {
                $result .= $tens[$deca];
                if ($n) 
                {
                    $result .= "-" . $ones[$n];
                }
            }
        }
        if (empty($result)) 
        {
            $result = "zero";
        }
        return $result;
    }
}
