<?php
include "../config.php";
require_once "../fun/money_format.php";
$user_name = $_SESSION['user_name'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];

if (!isset($_SESSION['emp_id'])) {
    echo "<script>location.href='../index';</script>";
}

function AmountInWords(float $amount)
{
    $amount_after_decimal = round($amount - ($num = floor($amount)), 2) * 100;
    // Check if there is any number after decimal
    $amt_hundred = null;
    $count_length = strlen($num);
    $x = 0;
    $string = array();
    $change_words = array(
        0 => '',
        1 => 'One',
        2 => 'Two',
        3 => 'Three',
        4 => 'Four',
        5 => 'Five',
        6 => 'Six',
        7 => 'Seven',
        8 => 'Eight',
        9 => 'Nine',
        10 => 'Ten',
        11 => 'Eleven',
        12 => 'Twelve',
        13 => 'Thirteen',
        14 => 'Fourteen',
        15 => 'Fifteen',
        16 => 'Sixteen',
        17 => 'Seventeen',
        18 => 'Eighteen',
        19 => 'Nineteen',
        20 => 'Twenty',
        30 => 'Thirty',
        40 => 'Forty',
        50 => 'Fifty',
        60 => 'Sixty',
        70 => 'Seventy',
        80 => 'Eighty',
        90 => 'Ninety'
    );
    $here_digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');

    while ($x < $count_length) {
        $get_divider = ($x == 2) ? 10 : 100;
        $amount = floor($num % $get_divider);
        $num = floor($num / $get_divider);
        $x += $get_divider == 10 ? 1 : 2;
        if ($amount) {
            $add_plural = (($counter = count($string)) && $amount > 9) ? 's' : null;
            $amt_hundred = ($counter == 1 && $string[0]) ? ' and ' : null;
            $string[] = ($amount < 21) ? $change_words[$amount] . ' ' . $here_digits[$counter] . $add_plural . ' 
                ' . $amt_hundred : $change_words[floor($amount / 10) * 10] . ' ' . $change_words[$amount % 10] . ' 
                ' . $here_digits[$counter] . $add_plural . ' ' . $amt_hundred;
        } else {
            $string[] = null;
        }
    }
    $implode_to_Rupees = implode('', array_reverse($string));
    $get_paise = ($amount_after_decimal > 0) ? "And " . ($change_words[$amount_after_decimal / 10] . "
            " . $change_words[$amount_after_decimal % 10]) . ' Paise' : '';
    return ($implode_to_Rupees ? $implode_to_Rupees . 'Rupees ' : '') . $get_paise;
}


$id = base64_decode($_GET['slip_id']);
$vDate = date("Y-m-d");
$sql = "SELECT 
    t1.*, t2.name, t2.employee_id, t2.joining_date, t3.designation_name, t2.working_status_date, t2.uan_no
FROM fnf t1 
INNER JOIN (
    SELECT 
        p1.*,
        (CASE
            WHEN COALESCE(p2.promote_to,'')<>'' THEN p2.promote_to
            ELSE p1.designation
        END) AS new_designation
    FROM users p1
    LEFT JOIN (
        SELECT
            *
        FROM (
            SELECT 
                id,
                employee_id, 
                promote_to, 
                applicable_from,
                ROW_NUMBER() OVER(PARTITION BY employee_id ORDER BY id DESC) as rn
            FROM promotions
        ) final_promotions
        WHERE rn=1
    ) p2 ON p1.employee_id=p2.employee_id
) as t2 ON t2.employee_id = t1.employee_id 
LEFT JOIN designations AS t3 ON t3.id=t2.new_designation 
WHERE t1.id='$id'";
$fnf_data = mysqli_fetch_assoc(mysqli_query($db, $sql));
if (empty($fnf_data)) {
    echo "<script>alert('No Data Found');window.close();</script>";
    exit();
}

$fnf_details = json_decode($fnf_data['fnf_details'], true);


$for_month = [];
foreach ($fnf_details as $month_year => $month_data) {
    $for_month[] = $month_year;
}
?>
<!doctype html>
<html lang=" en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Favicon -->
    <link rel="shortcut icon" href="https://cdn.nishtyainfotech.com/content/jobaaj/assets/img/jobaaj-favicon.ico">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>FNF Slip - Jobaaj</title>
    <style>
        body {
            -webkit-print-color-adjust: exact;
        }

        .font-size-14 {
            font-size: 14px;
        }

        .font-size-12 {
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div>

        <div class="border border-1 p-0 m-0">

            <div class="row py-0 align-items-center">
                <div class="col-6 p-0 m-0" style="padding:0px 20px !important;">
                    <h4>Nishtya Infotech Pvt. Ltd.</h4>
                    <p class="font-size-12 m-0" style="line-height:0.875rem;margin-bottom:0;">
                        CIN No- U72900DL2020PTC369688<br>
                        379, Vardhman Grand Plaza, Plot No.7 Manglam Place,<br>
                        Sector-3, Rohini, New Delhi- 110085 <br>
                    </p>
                </div>
                <div class="col-6 p-0 m-0 text-end">
                    <img src="nishtya_transparent.png" alt="" style="width:160px;">
                </div>
            </div>
            <div class="border-top border-bottom p-0 m-0">
                <h4 class="text-center" style="padding: 10px 0px 10px 0px;font-weight: bold;">
                    FNF Slip for the month of
                    <?php echo implode(', ', $for_month); ?>
                </h4>
            </div>

            <div class="border-top border-bottom p-0 m-0">
                <div class="row py-0 pb-3">
                    <h5 class="mb-3">EMPLOYEE FNF SUMMARY</h5>
                    <div class="col-6 font-size-12 pe-0 mt-2">
                        <table width="100%" border="0" cellpadding="0" cellspacing="0">

                            <tr>
                                <td class="ps-1">Employee Name</td>
                                <td class="fw-bold">: <?php echo $fnf_data['name']; ?></td>
                            </tr>
                            <tr>
                                <td class="ps-1">Employee No.</td>
                                <td class="fw-bold">: <?php echo $fnf_data['employee_id']; ?> </td>
                            </tr>
                            <tr>
                                <td class="ps-1">Date of Joining</td>
                                <td class="fw-bold">: <?php echo (!empty($fnf_data['joining_date'])) ? date("d M Y", strtotime($fnf_data['joining_date'])) : ''; ?></td>
                            </tr>
                            <tr>
                                <td class="ps-1">Designation</td>
                                <td class="fw-bold">: <?php echo $fnf_data['designation_name'] ?></td>
                            </tr>
                            <tr>
                                <td class="ps-1">Last Working Day</td>
                                <td class="fw-bold">: <?php echo (!empty($fnf_data['working_status_date'])) ? date("d M Y", strtotime($fnf_data['working_status_date'])) : ''; ?></td>
                            </tr>
                            <?php if(!empty($fnf_data['uan_no'])) { ?>
                            <tr>
                                <td class="ps-1">UAN No.</td>
                                <td class="fw-bold">: <?php echo $fnf_data['uan_no']; ?></td>
                            </tr>
                            <?php }  ?>

                        </table>
                    </div>
                    <div class="col-6 font-size-12 ps-0">
                        <table width="100%" border="0" cellpadding="0" cellspacing="0">
                            <?php
                            foreach ($fnf_details as $month_year => $month_data) {
                                $fixed_data = isset($month_data['fixed']) ? $month_data['fixed'] : [];
                            ?>      
                                <tr>
                                    <td class="fw-bold ps-1"><?php echo $month_year; ?></td>
                                    <td class=""></td>
                                </tr>
                                <tr>
                                    <td class="ps-1"><?php echo $fixed_data[0]['title']; ?></td>
                                    <td class="fw-bold">: <?php echo $fixed_data[0]['value']; ?></td>
                                </tr>
                                <tr>
                                    <td class="ps-1"><?php echo $fixed_data[1]['title']; ?></td>
                                    <td class="fw-bold">: <?php echo $fixed_data[1]['value']; ?></td>
                                </tr>
                                <tr>
                                    <td class="ps-1"><?php echo $fixed_data[2]['title']; ?></td>
                                    <td class="fw-bold">: <?php echo $fixed_data[2]['value']; ?></td>
                                </tr>
                                <tr>
                                    <td class="ps-1"><?php echo $fixed_data[3]['title']; ?></td>
                                    <td class="fw-bold">: <?php echo $fixed_data[3]['value']; ?></td>
                                </tr>
                                <tr>
                                    <td class="ps-1"><?php echo $fixed_data[4]['title']; ?></td>
                                    <td class="fw-bold">: <?php echo $fixed_data[4]['value']; ?></td>
                                </tr>
                                <tr>
                                    <td class="ps-1"><?php echo $fixed_data[6]['title']; ?></td>
                                    <td class="fw-bold">: <?php echo $fixed_data[6]['value']; ?></td>
                                </tr>
                                <tr>
                                    <td class="pb-2"></td>
                                    <td class="pb-2"></td>
                                </tr>
                            <?php }  ?>

                        </table>
                    </div>
                </div>
            </div>

            <div class="border-top border-bottom p-0">
                <div class="row py-0">
                    <!-- salary detail -->
                    <div class="col-2 font-size-12 pe-0" style="position: relative;">
                        <div class="border-top border-bottom p-0 m-0" style="background-color:#f2f2f3; height:37px;">
                            <p class="m-0 py-1 ps-1 fw-bolder">Earnings</p>
                        </div>

                        <div class="pt-1 pb-5">
                            <p class="font-size-12 py-1 ps-1">
                            <?php
                            // echo "<pre>";
                            // print_r($fnf_details);
                            foreach ($fnf_details as $month_year => $month_data) {
                                $fixed_data = isset($month_data['fixed']) ? $month_data['fixed'] : [];
                                $add_on = isset($month_data['add_on']) ? $month_data['add_on'] : [];
                                echo "<strong>$month_year</strong><br>";
                                echo $fixed_data[5]['title'] . "<br>";
                                // foreach ($add_on as $ao) {
                                //     echo $ao['title'] . "<br>";
                                // }
                                echo "<br>";
                            }
                            ?>
                            </p>
                        </div>
                        <div class="border-top border-bottom p-0 mt-3" style="position: absolute;bottom: 0;width: 100%;">
                            <p class="font-size-12 py-1 ps-1 fw-bolder">Gross Earnings</p>
                        </div>
                    </div>
                    <div class="col-2 font-size-12 border-end px-0" style="position: relative;">
                        <div class="border-top border-bottom p-0 m-0" style="background-color:#f2f2f3; height:37px;">
                            <p class="m-0 py-1 ps-1 fw-bolder">Amount</p>
                        </div>

                        <div class="ps-1 pt-1 pb-5">
                            <p class="font-size-12 ps-1 py-1">
                                <?php
                                $gross_earnings = 0;
                                foreach ($fnf_details as $month_year => $month_data) {
                                    $fixed_data = isset($month_data['fixed']) ? $month_data['fixed'] : [];
                                    $add_on = isset($month_data['add_on']) ? $month_data['add_on'] : [];
                                    echo "<strong></strong><br>";
                                    echo "&#8377;&nbsp;" . IND_money_format($fixed_data[5]['value']) . "<br>";
                                    $gross_earnings += $fixed_data[5]['value'];
                                    // foreach ($add_on as $ao) {
                                    //     echo "&#8377;&nbsp;" . IND_money_format($ao['value']) . "<br>";
                                    //     $gross_earnings += $ao['value'];
                                    // }
                                    echo "<br>";
                                }
                                ?>
                            </p>
                        </div>
                        <div class="border-top border-bottom p-0 mt-3" style="position: absolute;bottom: 0;width: 100%;">
                            <p class="font-size-12 py-1 ps-1 fw-bolder">&#8377;&nbsp;<?php echo IND_money_format($gross_earnings) ?></p>
                        </div>
                    </div>

                    <!-- deduction -->
                    <div class="col-2 font-size-12 px-0" style="position: relative;">
                        <div class="border-top border-bottom p-0 m-0" style="background-color:#f2f2f3; height:37px;">
                            <p class="m-0 py-1 ps-1 fw-bolder">Deduction</p>
                        </div>

                        <div class="pt-1 pb-5">
                            <p class="font-size-12 py-1 ps-1">
                                <?php
                                foreach ($fnf_details as $month_year => $month_data) {
                                    $deduction = isset($month_data['deduction']) ? $month_data['deduction'] : [];
                                    echo "<strong>$month_year</strong><br>";
                                    foreach ($deduction as $ao) {
                                        echo $ao['title'] . "<br>";
                                    }
                                    echo "<br>";
                                }
                                ?>
                            </p>
                        </div>
                        <div class="border-top border-bottom p-0 mt-3" style="position: absolute;bottom: 0;width: 100%;">
                            <p class="font-size-12 py-1 ps-1" style="font-weight:bold;">Total Deductions</p>
                        </div>
                    </div>
                    <div class="col-2 font-size-12 border-end px-0" style="position: relative;">
                        <div class="border-top border-bottom p-0 m-0" style="background-color:#f2f2f3; height:37px;">
                            <p class="m-0 py-1 ps-1 fw-bolder">Amount</p>
                        </div>

                        <div class="pt-1 pb-5">
                            <p class="font-size-12 py-1 ps-1">
                                <?php
                                $total_deduction = 0;
                                foreach ($fnf_details as $month_year => $month_data) {
                                    $deduction = isset($month_data['deduction']) ? $month_data['deduction'] : [];
                                    echo "<strong></strong><br>";
                                    foreach ($deduction as $ao) {
                                        echo "&#8377;&nbsp;" . IND_money_format($ao['value']) . "<br>";
                                        $total_deduction += $ao['value'];
                                    }
                                    echo "<br>";
                                }
                                ?>
                            </p>
                        </div>
                        <div class="border-top border-bottom p-0 mt-3" style="position: absolute;bottom: 0;width: 100%;">
                            <p class="font-size-12 py-1 ps-1" style="font-weight:bold;">&#8377;&nbsp;<?php echo IND_money_format($total_deduction); ?></p>
                        </div>
                    </div>

                    <!-- adon -->
                    <div class="col-2 font-size-12 px-0" style="position: relative;">
                        <div class="border-top border-bottom p-0 m-0" style="background-color:#f2f2f3; height:37px;">
                            <p class="m-0 py-1 ps-1 fw-bolder">Addons</p>
                        </div>

                        <div class="pt-1 pb-5">
                            <p class="font-size-12 py-1 ps-1">
                                <?php
                                foreach ($fnf_details as $month_year => $month_data) {
                                    $add_on = isset($month_data['add_on']) ? $month_data['add_on'] : [];    
                                    echo "<strong>$month_year</strong><br>";
                                    foreach ($add_on as $ao) {
                                        if (stripos($ao['title'], 'incentive') !== false) {
                                            echo $ao['title'] . "<br>";
                                        }
                                    }
                                    echo "<br>";
                                }
                                ?>
                            </p>
                        </div>
                        <div class="border-top border-bottom p-0 mt-3" style="position: absolute;bottom: 0;width: 100%;">
                            <p class="font-size-12 py-1 ps-1" style="font-weight:bold;">Total Addons</p>
                        </div>
                    </div>
                    <div class="col-2 font-size-12 border-end px-0" style="position: relative;">
                        <div class="border-top border-bottom p-0 m-0" style="background-color:#f2f2f3; height:37px;">
                            <p class="m-0 py-1 ps-1 fw-bolder">Amount</p>
                        </div>

                        <div class="pt-1 pb-5">
                            <p class="font-size-12 py-1 ps-1">
                                <?php
                                $total_add_on = 0;
                                foreach ($fnf_details as $month_year => $month_data) {
                                    $add_on = isset($month_data['add_on']) ? $month_data['add_on'] : [];
                                    echo "<strong></strong><br>";
                                    foreach ($add_on as $ao) {
                                        if (stripos($ao['title'], 'incentive') !== false) {
                                            echo "&#8377;&nbsp;" . IND_money_format($ao['value']) . "<br>";
                                            $total_add_on += $ao['value'];
                                        }
                                    }
                                    echo "<br>";
                                }
                                ?>
                            </p>
                        </div>
                        <div class="border-top border-bottom p-0 mt-3" style="position: absolute;bottom: 0;width: 100%;">
                            <p class="font-size-12 py-1 ps-1" style="font-weight:bold;">&#8377;&nbsp;<?php echo IND_money_format($total_add_on); ?></p>
                        </div>
                    </div>

                </div>
            </div>

            <!-- <div class="border-top border-bottom p-0">
                <div class="row py-0">
                    <div class="col-12 font-size-12 pe-0" style="height:10rem;">
                        <div class="border-top border-start border-end border-bottom p-0 m-0" style="height:30%;">
                        </div>
                        <div class="border-start border-end border-bottom p-0 m-0" style="height:70%;">
                        </div>
                    </div>
                    <div class="col-12 border-start border-end font-size-12 pe-0" style="height:10rem;">
                    </div>
                </div>
            </div> -->

            <div class="border-top border-bottom p-0">
                <div class="row py-0">
                    <div class="col-12 font-size-12 pe-0" style="height:10rem;">
                    </div>
                </div>
            </div>
            

            <?php $adjusted_amount = $total_deduction - $total_add_on; ?>

            <div class="border-top border-bottom p-0" style="background-color:#f2f2f3">
                <p class="text-center fw-bolder fs-4"><span>Total Net Payable &#8377;&nbsp;<?php echo IND_money_format(round($gross_earnings-$adjusted_amount)) ?></span> <span>(<?php echo AmountInWords(round($gross_earnings-$adjusted_amount)); ?>)</span></p>
                <p class="text-center fw-bold" style="color:gray;">Total Net Payable = (Gross Earnings + Total Addons - Total Deductions)</p>
            </div>
            <p class="text-center font-size-14" style="color:gray;">-This is Computer Generated slip does not Required Any Signature.-</p>


        </div>

    </div>


    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <script>
        window.print();
    </script>
</body>

</html>