<?php
require_once('../config.php');
$user_id = $_SESSION['user_id'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];


if (!isset($_SESSION['user_id']) || $_SESSION['user_role']!='0') {
    http_response_code(403);
    exit(json_encode(["error" => "Unauthorized access!!"]));
}


// SQL server connection information
// $sql_details = $search_db;
$sql_details = $search_db;

if(!isset($_GET['isset'])){
    exit();
}


$table = <<<EOT
(
    SELECT 
    ROW_NUMBER() OVER(ORDER BY t1.id DESC) row_num,
    t1.*, t2.name,
        COALESCE(
            (CASE
                WHEN (t1.revision_start_date='' OR t1.revision_start_date IS NULL)  AND (t1.revision_end_date='' OR t1.revision_end_date IS NULL) 
                 THEN (SELECT SUM(bonus) AS bonus FROM employee_bonusIncentive WHERE employee_id=t1.employee_id)
         
                 WHEN (t1.revision_start_date='' OR t1.revision_start_date IS NULL)  AND (t1.revision_end_date<>'' OR t1.revision_end_date IS NOT NULL) 
                 THEN (SELECT SUM(bonus) AS bonus FROM employee_bonusIncentive WHERE employee_id=t1.employee_id AND date<DATE_FORMAT(t1.revision_end_date, '%Y-%m'))
         
                 WHEN (t1.revision_start_date<>'' OR t1.revision_start_date IS NOT NULL)  AND (t1.revision_end_date='' OR t1.revision_end_date IS NULL) 
                 THEN (SELECT SUM(bonus) AS bonus FROM employee_bonusIncentive WHERE employee_id=t1.employee_id AND date<DATE_FORMAT(t1.revision_start_date, '%Y-%m'))
         
                 ELSE (SELECT SUM(bonus) AS bonus FROM employee_bonusIncentive WHERE employee_id=t1.employee_id AND date BETWEEN DATE_FORMAT(t1.revision_start_date, '%Y-%m') AND DATE_FORMAT(t1.revision_end_date, '%Y-%m'))
        
        END),0) as bonus,
        COALESCE(
            (CASE
                WHEN (t1.revision_start_date='' OR t1.revision_start_date IS NULL)  AND (t1.revision_end_date='' OR t1.revision_end_date IS NULL) 
                 THEN (SELECT SUM(incentive) AS incentive FROM employee_bonusIncentive WHERE employee_id=t1.employee_id)
         
                 WHEN (t1.revision_start_date='' OR t1.revision_start_date IS NULL)  AND (t1.revision_end_date<>'' OR t1.revision_end_date IS NOT NULL) 
                 THEN (SELECT SUM(incentive) AS incentive FROM employee_bonusIncentive WHERE employee_id=t1.employee_id AND date<DATE_FORMAT(t1.revision_end_date, '%Y-%m'))
         
                 WHEN (t1.revision_start_date<>'' OR t1.revision_start_date IS NOT NULL)  AND (t1.revision_end_date='' OR t1.revision_end_date IS NULL) 
                 THEN (SELECT SUM(incentive) AS incentive FROM employee_bonusIncentive WHERE employee_id=t1.employee_id AND date<DATE_FORMAT(t1.revision_start_date, '%Y-%m'))
         
                 ELSE (SELECT SUM(incentive) AS incentive FROM employee_bonusIncentive WHERE employee_id=t1.employee_id AND date BETWEEN DATE_FORMAT(t1.revision_start_date, '%Y-%m') AND DATE_FORMAT(t1.revision_end_date, '%Y-%m'))
        
        END),0) as incentive 
        FROM employee_salary t1
        INNER JOIN users t2 ON t2.employee_id = t1.employee_id
) join_table
EOT;


$whereResult = null;
$whereAll = null;
if (isset($_GET['where_condition']) && !empty($_GET['where_condition'])) {
    $whereAll = $_GET['where_condition'];
}

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array('db' => 'row_num', 'dt' => 'row_num'),
    array('db' => 'employee_id', 'dt' => 'employee_id'),
    array('db' => 'name', 'dt' => 'name'),
    array(
        'db' => 'basic_salary', 
        'dt' => 'basic_salary',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'hra', 
        'dt' => 'hra',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'books_per', 
        'dt' => 'books_per',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'special_allowance', 
        'dt' => 'special_allowance',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'company_epf', 
        'dt' => 'company_epf',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'ctc', 
        'dt' => 'ctc',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array('db' => 'epf_status', 'dt' => 'epf_status'),
    array('db' => 'esi_status', 'dt' => 'esi_status'),
    array(
        'db' => 'tds', 
        'dt' => 'tds',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'bonus', 
        'dt' => 'bonus',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array(
        'db' => 'incentive', 
        'dt' => 'incentive',
        'formatter' => function($cell, $row){
            $returnValue = '&#8377;&nbsp;'.round($cell/12);
            return $returnValue;
        }
    ),
    array('db' => 'id', 'dt' => 'id'),

);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
require('ssp.class.php');

echo json_encode(
    // SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns)
    SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, $whereResult, $whereAll)
);
