SELECT es.company_id, er.company_name, es.department_id, er.department_name, es.employee_registration_id, er.employee_code, er.registration_code, er.employee_name
, es.taxable, es.basic_salary, es.gross_amount, es.taxable_amount AS monthly_taxable, er.total_payroll, (es.taxable_amount*er.total_payroll) AS annual_taxable
, ts.tax_percent, ts.additional, IF(taxable='No',0,ROUND((((es.taxable_amount*er.total_payroll)-ts.range_from)*ts.tax_percent/100)+ts.additional,2)) AS annual_tax_amount
FROM `vw_employee_salary` es
INNER JOIN (
SELECT employee_registration_id, MAX(wef_date) AS wef_date
FROM `vw_employee_salary`
GROUP BY employee_registration_id
) t ON t.employee_registration_id = es.employee_registration_id AND t.wef_date = es.wef_date
INNER JOIN vw_employee_registration er ON er.employee_registration_id = es.employee_registration_id AND er.status='Active'
INNER JOIN `tax_slab` ts ON fiscal_year_id = 5 AND range_from <= (es.taxable_amount*er.total_payroll) AND range_to >= (es.taxable_amount*er.total_payroll);
