package hrm_package;

import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.EmpInfo;
import main_package.NumberToWord;

@ManagedBean(name="offerLetter")
@ViewScoped

public class OfferLetterBean implements Serializable
{
	int noticeDays;
	
	int currentSalary, newSalary, rating, increment;
	
	int totalDays,workingDays;
	
	
	String startDateStr, endDateStr,salaryInWord, salaryInWordGross,branchName;
	
	String dsaCode, companyName, designation;
	
	
	Date startDate, endDate;
	
	
	double salaryAmount, salary, basicYear, basicMonth, houseRentAllowance, specialAllowance, otherAllowance, GrossSalary, 
		   employeeEsicContribution, employeePfContribution, totalFixedPay, totalPfDeduction, TotalEsicDeduction, netCashInHand,
		   basicMonthYear, houseRentAllowanceYear, specialAllowanceYear, otherAllowanceYear, GrossSalaryYear, 
		   employeeEsicContributionYear, employeePfContributionYear, totalFixedPayYear, totalPfDeductionYear, 
		   TotalEsicDeductionYear, incrementAmountGross, netCashInHandYear;
	
	double salary1, netPy, halfPy;

	
	EmpInfo selected;
	
	DataBaseMethods obj = new DataBaseMethods();
	
	
	NumberToWord nn  = new NumberToWord();
	
	SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
	
	
	
	public OfferLetterBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		selected=(EmpInfo) ss.getAttribute("selectedRow");
		startDate= (Date) ss.getAttribute("startDateHrm");
		endDate= (Date) ss.getAttribute("endDateHrm");
		noticeDays=(int) ss.getAttribute("noticeDaysHrm");
		rating=(int) ss.getAttribute("ratingHrm");
		increment=(int) ss.getAttribute("incrementHrm");
		companyName=(String) ss.getAttribute("companyName");
		System.out.println("companpy name is"+companyName);
        
		salaryCreation();
		currentSalary=(int) (selected.getSalary()*12);
		int totalfixedPay =(int) totalFixedPayYear;
		salaryInWord = nn.convertNumberToWords((totalfixedPay));
		
		branchName = obj.branchNameByBranchId(selected.getBranchId(), conn);
		
		 designation  = selected.getDesignation();
		
		newSalary=currentSalary+(currentSalary*increment)/100;
		startDateStr=sf.format(startDate);
		endDateStr=sf.format(endDate);
		DecimalFormat df = new DecimalFormat("#.#");
		netPy= Double.parseDouble( df.format(newSalary/100000.0));
		halfPy=netPy/2;
		
			dsaCode=selected.getDsaCode();

		
	}

	
	public void salaryCreation()
	{	
		// Round before converting to int to avoid truncation
		salaryAmount = (int) selected.getSalary();

		// Monthly salary breakdown
		
		if ("Vibhu Micro Benefit Foundation".equalsIgnoreCase(companyName)) 
		{
			basicMonth = Math.round(salaryAmount * 50 / 100.0);
			houseRentAllowance = Math.round(basicMonth * 40 / 100.0);
			specialAllowance = Math.round(basicMonth * 60 / 100.0);
		}
		else
		{
			basicMonth = Math.round(salaryAmount / 30.0 * 30 * 47 / 100.0);
			houseRentAllowance = Math.round(basicMonth * 50 / 100.0);
			specialAllowance = Math.round(basicMonth * 50 / 100.0);
		}
		
		
		otherAllowance = Math.round(salaryAmount - basicMonth - houseRentAllowance - specialAllowance);

		// Yearly values
		basicMonthYear = basicMonth*12;
		houseRentAllowanceYear = houseRentAllowance * 12;
		specialAllowanceYear = specialAllowance * 12;
		otherAllowanceYear = otherAllowance * 12;

		GrossSalaryYear = basicMonthYear + houseRentAllowanceYear + specialAllowanceYear + otherAllowanceYear;

		// Monthly gross  
		GrossSalary = GrossSalaryYear / 12;
		
		// ✅ ESIC and PF calculation
		
	    if ("SUDERSHANA FINANCE".equalsIgnoreCase(companyName)|| companyName.equalsIgnoreCase("ROYAL MICROFINANCE")|| companyName.equalsIgnoreCase("DELIGHTS MICRO CREDIT INDIA FOUNDATION")) 
	    {
	        // For Sudershana Finance, set all PF/ESIC values to zero
	        employeeEsicContribution = 0;
	        employeePfContribution = 0;
	        employeeEsicContributionYear = 0;
	        employeePfContributionYear = 0;
	        
	        totalPfDeductionYear = 0;
	        TotalEsicDeductionYear = 0;
	    } 
	    else 
	    {
	        // Normal calculation for other companies
	        employeeEsicContribution = (int) Math.round(GrossSalary * 3.25 / 100.0);
	        employeePfContribution = Math.round(basicMonth * 12 / 100.0);

	        // Yearly ESIC and PF
	        employeeEsicContributionYear = employeeEsicContribution * 12;
	        employeePfContributionYear = employeePfContribution * 12;
	        
	        // Deductions
	        totalPfDeductionYear = Math.round(basicMonthYear * 12 / 100.0);
	        TotalEsicDeductionYear = (int) Math.round(GrossSalaryYear * 0.75 / 100.0);
	  
	    }
		totalFixedPayYear = GrossSalaryYear + employeeEsicContributionYear + employeePfContributionYear;
		totalFixedPay = totalFixedPayYear / 12;

//		// Deductions
//		totalPfDeductionYear = Math.round(basicMonthYear * 12 / 100.0);
//		TotalEsicDeductionYear = (int) Math.round(GrossSalaryYear * 0.75 / 100.0);

		// Net cash yearly
		netCashInHandYear = GrossSalaryYear - totalPfDeductionYear - TotalEsicDeductionYear;

		// Monthly deductions
		totalPfDeduction = Math.round(totalPfDeductionYear / 12.0);
		TotalEsicDeduction = Math.round(TotalEsicDeductionYear / 12.0);

		// Conditional ESIC deduction removal
		if (salaryAmount > 21000)
		{
		    TotalEsicDeduction = 0;
		    employeeEsicContribution = 0;
		}

		// Monthly net cash in hand
		netCashInHand = GrossSalary - totalPfDeduction - TotalEsicDeduction;
		
		
		
		salaryInWordGross = nn.convertNumberToWords((int) GrossSalaryYear);
	}
	
	public EmpInfo getSelected() {
		return selected;
	}

	public void setSelected(EmpInfo selected) {
		this.selected = selected;
	}

	public String getStartDateStr() {
		return startDateStr;
	}

	public void setStartDateStr(String startDateStr) {
		this.startDateStr = startDateStr;
	}

	public String getEndDateStr() {
		return endDateStr;
	}

	public void setEndDateStr(String endDateStr) {
		this.endDateStr = endDateStr;
	}

	public int getNoticeDays() {
		return noticeDays;
	}

	public void setNoticeDays(int noticeDays) {
		this.noticeDays = noticeDays;
	}

	public int getCurrentSalary() {
		return currentSalary;
	}

	public void setCurrentSalary(int currentSalary) {
		this.currentSalary = currentSalary;
	}

	public int getNewSalary() {
		return newSalary;
	}

	public void setNewSalary(int newSalary) {
		this.newSalary = newSalary;
	}

	public int getRating() {
		return rating;
	}

	public void setRating(int rating) {
		this.rating = rating;
	}

	public int getIncrement() {
		return increment;
	}

	public void setIncrement(int increment) {
		this.increment = increment;
	}

	public String getSalaryInWord() {
		return salaryInWord;
	}

	public void setSalaryInWord(String salaryInWord) {
		this.salaryInWord = salaryInWord;
	}


	public Date getStartDate() {
		return startDate;
	}


	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}


	public Date getEndDate() {
		return endDate;
	}


	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}


	public SimpleDateFormat getSf() {
		return sf;
	}


	public void setSf(SimpleDateFormat sf) {
		this.sf = sf;
	}


	public double getSalaryAmount() {
		return salaryAmount;
	}


	public void setSalaryAmount(double salaryAmount) {
		this.salaryAmount = salaryAmount;
	}


	public double getSalary() {
		return salary;
	}


	public void setSalary(double salary) {
		this.salary = salary;
	}


	public double getBasicYear() {
		return basicYear;
	}


	public void setBasicYear(double basicYear) {
		this.basicYear = basicYear;
	}


	public double getBasicMonth() {
		return basicMonth;
	}


	public void setBasicMonth(double basicMonth) {
		this.basicMonth = basicMonth;
	}


	public double getHouseRentAllowance() {
		return houseRentAllowance;
	}


	public void setHouseRentAllowance(double houseRentAllowance) {
		this.houseRentAllowance = houseRentAllowance;
	}


	public double getSpecialAllowance() {
		return specialAllowance;
	}


	public void setSpecialAllowance(double specialAllowance) {
		this.specialAllowance = specialAllowance;
	}


	public double getOtherAllowance() {
		return otherAllowance;
	}


	public void setOtherAllowance(double otherAllowance) {
		this.otherAllowance = otherAllowance;
	}


	public double getGrossSalary() {
		return GrossSalary;
	}


	public void setGrossSalary(double grossSalary) {
		GrossSalary = grossSalary;
	}


	public double getEmployeeEsicContribution() {
		return employeeEsicContribution;
	}


	public void setEmployeeEsicContribution(double employeeEsicContribution) {
		this.employeeEsicContribution = employeeEsicContribution;
	}


	public double getEmployeePfContribution() {
		return employeePfContribution;
	}


	public void setEmployeePfContribution(double employeePfContribution) {
		this.employeePfContribution = employeePfContribution;
	}


	public double getTotalFixedPay() {
		return totalFixedPay;
	}


	public void setTotalFixedPay(double totalFixedPay) {
		this.totalFixedPay = totalFixedPay;
	}


	public double getTotalPfDeduction() {
		return totalPfDeduction;
	}


	public void setTotalPfDeduction(double totalPfDeduction) {
		this.totalPfDeduction = totalPfDeduction;
	}


	public double getTotalEsicDeduction() {
		return TotalEsicDeduction;
	}


	public void setTotalEsicDeduction(double totalEsicDeduction) {
		TotalEsicDeduction = totalEsicDeduction;
	}


	public double getNetCashInHand() {
		return netCashInHand;
	}


	public void setNetCashInHand(double netCashInHand) {
		this.netCashInHand = netCashInHand;
	}


	public double getBasicMonthYear() {
		return basicMonthYear;
	}


	public void setBasicMonthYear(double basicMonthYear) {
		this.basicMonthYear = basicMonthYear;
	}


	public double getHouseRentAllowanceYear() {
		return houseRentAllowanceYear;
	}


	public void setHouseRentAllowanceYear(double houseRentAllowanceYear) {
		this.houseRentAllowanceYear = houseRentAllowanceYear;
	}


	public double getSpecialAllowanceYear() {
		return specialAllowanceYear;
	}


	public void setSpecialAllowanceYear(double specialAllowanceYear) {
		this.specialAllowanceYear = specialAllowanceYear;
	}


	public double getOtherAllowanceYear() {
		return otherAllowanceYear;
	}


	public void setOtherAllowanceYear(double otherAllowanceYear) {
		this.otherAllowanceYear = otherAllowanceYear;
	}


	public double getGrossSalaryYear() {
		return GrossSalaryYear;
	}


	public void setGrossSalaryYear(double grossSalaryYear) {
		GrossSalaryYear = grossSalaryYear;
	}


	public double getEmployeeEsicContributionYear() {
		return employeeEsicContributionYear;
	}


	public void setEmployeeEsicContributionYear(double employeeEsicContributionYear) {
		this.employeeEsicContributionYear = employeeEsicContributionYear;
	}


	public double getEmployeePfContributionYear() {
		return employeePfContributionYear;
	}


	public void setEmployeePfContributionYear(double employeePfContributionYear) {
		this.employeePfContributionYear = employeePfContributionYear;
	}


	public double getTotalFixedPayYear() {
		return totalFixedPayYear;
	}


	public void setTotalFixedPayYear(double totalFixedPayYear) {
		this.totalFixedPayYear = totalFixedPayYear;
	}


	public double getTotalPfDeductionYear() {
		return totalPfDeductionYear;
	}


	public void setTotalPfDeductionYear(double totalPfDeductionYear) {
		this.totalPfDeductionYear = totalPfDeductionYear;
	}


	public double getTotalEsicDeductionYear() {
		return TotalEsicDeductionYear;
	}


	public void setTotalEsicDeductionYear(double totalEsicDeductionYear) {
		TotalEsicDeductionYear = totalEsicDeductionYear;
	}


	public double getIncrementAmountGross() {
		return incrementAmountGross;
	}


	public void setIncrementAmountGross(double incrementAmountGross) {
		this.incrementAmountGross = incrementAmountGross;
	}


	public double getNetCashInHandYear() {
		return netCashInHandYear;
	}


	public void setNetCashInHandYear(double netCashInHandYear) {
		this.netCashInHandYear = netCashInHandYear;
	}


	public double getSalary1() {
		return salary1;
	}


	public void setSalary1(double salary1) {
		this.salary1 = salary1;
	}


	public int getTotalDays() {
		return totalDays;
	}


	public void setTotalDays(int totalDays) {
		this.totalDays = totalDays;
	}


	public int getWorkingDays() {
		return workingDays;
	}


	public void setWorkingDays(int workingDays) {
		this.workingDays = workingDays;
	}


	public NumberToWord getNn() {
		return nn;
	}


	public void setNn(NumberToWord nn) {
		this.nn = nn;
	}


	public String getBranchName() {
		return branchName;
	}


	public void setBranchName(String branchName) {
		this.branchName = branchName;
	}



	public String getCompanyName() {
		return companyName;
	}


	public void setCompanyName(String companyName) {
		this.companyName = companyName;
		
	}

	public double getNetPy() {
		return netPy;
	}


	public void setNetPy(double netPy) {
		this.netPy = netPy;
	}


	public String getDesignation() {
		return designation;
	}


	public void setDesignation(String designation) {
		this.designation = designation;
	}


	public double getHalfPy() {
		return halfPy;
	}


	public void setHalfPy(double halfPy) {
		this.halfPy = halfPy;


	}


	public String getDsaCode() {
		return dsaCode;
	}


	public void setDsaCode(String dsaCode) {
		this.dsaCode = dsaCode;

	}


	public String getSalaryInWordGross() {
		return salaryInWordGross;
	}


	public void setSalaryInWordGross(String salaryInWordGross) {
		this.salaryInWordGross = salaryInWordGross;
	}
	
	
	
}
