HRM-10/HRM.UI/ClientApp/src/app/ess/leave/leave-encashment/leave-encashment.component.ts
2026-11-23 11:49:34 +06:00

242 lines
9.9 KiB
TypeScript

import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { ApiService } from '../../../app.api.service';
import { EmployeeServices } from '../../../_services/employee/employee.service';
import { HRMNotificationService } from '../../../app.notification.service';
import { loadingPanelService } from '../../../hrm-loding panel/loding.panel.service';
import { Employee } from '../../../_models/Employee/employee';
import { LeaveService } from '../../../_services/leave/leave.service';
import { LeaveYear } from '../../../_models/Leave/leaveYear';
import { LeaveEncashment } from '../../../_models/Leave/leave-encashment';
import { EmpLeaveStatus } from '../../../_models/Leave/empLeaveStatus';
import { forkJoin } from 'rxjs';
import { LeaveEntry } from '../../../_models/Leave/leaveEntry';
import { EnumLeaveStatus, EnumStatus } from '../../../_models/enums';
import { AuthService } from 'src/app/_services/auth/auth.service';
import { PayrollType } from 'src/app/_models/Authentication/payrollType';
import { Leave } from '../../../_models/Leave/leave';
import { CommonModule } from '@angular/common';
import { LoadingPanelComponent } from 'src/app/hrm-loding panel/loading-panel.component';
import { ReactiveFormsModule } from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { DatePickerModule } from '@progress/kendo-angular-dateinputs';
@Component({
selector: 'app-leave-encashment',
templateUrl: './leave-encashment.component.html',
styleUrls: ['./leave-encashment.component.scss']
,changeDetection: ChangeDetectionStrategy.Eager,
standalone: false,
})
export class LeaveEncashmentComponent implements OnInit {
employee: Employee;
leaveYear: LeaveYear;
leaveEncashment: LeaveEncashment;
empLeaveStatuses: EmpLeaveStatus[] = [];
leaveEntries: LeaveEntry[] = [];
closingBal: number;
leaveBal: number;
annualLeaveBalance = 0;
appliedLeave = 0;
encashableLeave = 0;
applicableForEncashment = 0;
buttonDisable = false;
buttonLabel = 'Submit';
operationDate: Date;
leaves: Leave[];
constructor(public apiService: ApiService,
public employeeService: EmployeeServices,
public leaveService: LeaveService,
public notificationService: HRMNotificationService,
public authService: AuthService,
public loadingPanel: loadingPanelService) {
this.apiService.selectedMenuName = 'Leave Encashment';
}
ngOnInit(): void {
this.loadingPanel.ShowLoadingPanel = true;
this.leaveService.getAllLeave(EnumStatus.Regardless).subscribe(
(resp) => {
this.leaves = resp;
},
(err) => {
this.loadingPanel.ShowLoadingPanel = false;
this.notificationService.showError('Failed to Load Operation Date');
}, () => {
this.loadingPanel.ShowLoadingPanel = false;
});
this.loadLoggedInEmployee();
this.loadOperationDate();
}
loadOperationDate() {
this.loadingPanel.ShowLoadingPanel = false;
this.authService.GetOperationDate().subscribe(
(resp) => {
this.operationDate = resp;
},
(err) => {
this.loadingPanel.ShowLoadingPanel = false;
this.notificationService.showError('Failed to Load Operation Date');
}, ()=>{
this.loadingPanel.ShowLoadingPanel = false;
});
}
loadCurrentLeaveYear() {
this.buttonDisable = false;
this.loadingPanel.ShowLoadingPanel = true;
this.leaveService.getLastLeaveYear().subscribe(
(resp) => {
this.leaveYear = resp;
if (this.leaveYear.encashmentStartDate != undefined
|| this.leaveYear.encashmentEndDate != undefined) {
if (
new Date(this.operationDate) >= new Date(this.leaveYear.encashmentStartDate) &&
new Date(this.operationDate) <= new Date(this.leaveYear.encashmentEndDate)
) {
this.buttonDisable = true;
}
}
if(!this.isOneYearOrMore(new Date(this.leaveYear.endDate), new Date(this.employee.joiningDate))){
this.buttonDisable = false;
}
},
(x) => {
this.loadingPanel.ShowLoadingPanel = false;
console.log(x);
this.notificationService.showError('Error loading current year');
},
() => {
this.loadingPanel.ShowLoadingPanel = false;
this.loadData();
}
);
}
isOneYearOrMore(endDate: Date, joiningDate: Date): boolean {
const oneYearInMilliseconds = 365 * 24 * 60 * 60 * 1000; // Approximation for 1 year
const x= endDate.getTime() - joiningDate.getTime() > oneYearInMilliseconds;
return x;
}
loadLoggedInEmployee() {
this.employeeService.getLoginEmployee().subscribe(
(resp) => {
this.employee = resp;
},
(x) => {
console.log(x);
this.notificationService.showError('Error getting logged in employee information');
},
() => {
this.loadCurrentLeaveYear();
}
);
}
loadData() {
const dataForCurrentStatus = {
empIds: this.employee.id.toString(),
leaveYearId: this.leaveYear.id,
};
this.loadingPanel.ShowLoadingPanel = true;
const leaveEncashment = this.leaveService.getLeaveEncashment(this.leaveYear.id, this.employee.id);
const currentYearStatus = this.leaveService.getEmpLeaveStatusByEmpIds(this.employee.id.toString(), this.leaveYear.id);
// const leaveEntries = this.leaveService.getLeaveEntriesByEmpId(this.employee.id, this.leaveYear.id);
forkJoin(leaveEncashment, currentYearStatus).subscribe(
([res1, res2]) => {
this.leaveEncashment = res1;
this.empLeaveStatuses = res2;
},
(x) => {
this.loadingPanel.ShowLoadingPanel = false;
console.log(x);
this.notificationService.showError(x.error);
},
() => {
this.loadingPanel.ShowLoadingPanel = false;
if (this.leaveEncashment !== null) {
console.log(this.leaveEncashment);
this.buttonLabel = 'Submitted';
}
console.log(this.empLeaveStatuses);
this.calculateLeaveEncashment();
}
);
}
private calculateLeaveEncashment() {
if (this.empLeaveStatuses != undefined) {
var alLeave: Leave = this.leaves.find(x => x.isEarnedLeave == true);
var al = this.empLeaveStatuses.find(x => x.leaveId == alLeave.id);
if (al != undefined) {
this.annualLeaveBalance = al.openingBalance;
this.closingBal = al.closingBalance;
this.appliedLeave = al.leaveAvailed;
this.encashableLeave = al.yearEndBalance;
this.applicableForEncashment = Math.floor( al.yearEndBalance / 2);
}
}
//this.leaveEntries.filter(x => x.leaveStatus === EnumLeaveStatus.OnApproval
//&& x.leaveID === 1) // 1 for Annual leave
//.reduce((sumVal, a) => sumVal + a.appliedTotalDays, 0);
// if (this.leaveEncashment !== null) {
// this.annualLeaveBalance = this.closingBal;
// this.appliedLeave = this.leaveBal;
// this.encashableLeave = (this.closingBal - this.leaveBal - this.leaveEncashment.encashmentDays);
// this.applicableForEncashment = 0;
// this.buttonDisable = true;
// } else {
// this.annualLeaveBalance = this.closingBal;
// this.appliedLeave = this.leaveBal;
// this.encashableLeave = (this.closingBal - this.leaveBal);
// this.applicableForEncashment = Math.floor(this.encashableLeave / 2);
// }
}
onSave() {
if(this.applicableForEncashment > Math.floor(this.encashableLeave / 2))
{
this.notificationService.showInfo("You can not encash more than half of the annual leave balance!");
return;
}
var alLeave: Leave = this.leaves.find(x => x.isEarnedLeave == true);
const leaveEncashment = new LeaveEncashment();
leaveEncashment.leaveYearID = this.leaveYear.id;
leaveEncashment.leaveID = alLeave.id;
leaveEncashment.employeeID = this.employee.id;
leaveEncashment.encashmentDays = this.applicableForEncashment;
leaveEncashment.essSubmittedDays = this.applicableForEncashment;
leaveEncashment.leaveBalance = this.annualLeaveBalance - this.appliedLeave;
leaveEncashment.grossSalary = this.employee.grossSalary;
leaveEncashment.amount = (this.employee.grossSalary * 12 / 365) * leaveEncashment.encashmentDays;
leaveEncashment.createdBy = this.employee.id;
leaveEncashment.isFromWeb = true;
leaveEncashment.basicSalary= this.employee.basicSalary;
this.loadingPanel.ShowLoadingPanel = true;
this.leaveService.saveLeaveEncashment(leaveEncashment).subscribe(
() => { },
(x) => {
this.loadingPanel.ShowLoadingPanel = false;
console.log(x);
this.notificationService.showError('Error saving data');
},
() => {
this.loadingPanel.ShowLoadingPanel = false;
this.notificationService.showSuccess('Data saved successfully');
this.applicableForEncashment = 0;
this.buttonDisable = true;
this.buttonLabel = 'Submitted';
this.loadData();
}
);
}
}