import { DatePipe, formatDate } from '@angular/common'; import { Component, ChangeDetectionStrategy } from '@angular/core'; import { RowClassArgs } from '@progress/kendo-angular-grid'; import { LabelFn, LabelSettings } from '@progress/kendo-angular-progressbar'; import { GroupDescriptor, DataResult, process } from '@progress/kendo-data-query'; import { Employee } from 'src/app/_models/Employee/employee'; import { SearchEmployee } from 'src/app/_models/Employee/searchEmployee'; import { EnumObjectiveFlowStatus, EnumPMPStatus, EnumObjectiveStatus } from 'src/app/_models/enums'; import { DevelopmentPlan } from 'src/app/_models/PMS/developmentPlan'; import { Objective } from 'src/app/_models/PMS/objective'; import { ObjectiveCategoryGrades } from 'src/app/_models/PMS/objectiveCategoryGrades'; import { ObjectiveSet } from 'src/app/_models/PMS/objectiveSet'; import { ObjectiveType } from 'src/app/_models/PMS/objectiveType'; import { PMPProcess } from 'src/app/_models/PMS/pMPProcess'; import { PMPRating } from 'src/app/_models/PMS/pMPRating'; import { PMPValueBehaviorSetting } from 'src/app/_models/PMS/pmpValueBehaviorSetting'; import { BasicService } from 'src/app/_services/Basic/basic.service'; import { SystemConfigurationService } from 'src/app/_services/common/systemconfiguration.service'; import { EmployeeServices } from 'src/app/_services/employee/employee.service'; import { PmpService } from 'src/app/_services/pmp/pmp.service'; import { ApiService } from 'src/app/app.api.service'; import { HRMNotificationService } from 'src/app/app.notification.service'; import { DataTransferService } from 'src/app/data.transfer.service'; import { loadingPanelService } from 'src/app/hrm-loding panel/loding.panel.service'; import { AttendanceServices } from '../../_services/attendance/attendance.service'; import { DailyAttnProcess } from 'src/app/_models/Attendance/dailyAttnProcess'; import { FormGroup } from '@angular/forms'; import { ViewChild } from '@angular/core'; import { GridComponent } from '@progress/kendo-angular-grid'; import { LeaveEntry } from 'src/app/_models/Leave/leaveEntry'; import { LeaveService } from 'src/app/_services/leave/leave.service'; @Component({ selector: 'app-attendance-workhour-effect', standalone: false, templateUrl: './attendance-workhour-effect.component.html', changeDetection: ChangeDetectionStrategy.Eager, styleUrl: './attendance-workhour-effect.component.scss' }) export class AttendanceWorkhourEffectComponent { selectedWorkhourRow: any; attendanceWorkhourEffect: FormGroup; selectedEmployees: SearchEmployee[] = []; isVisibleEmpPicker: boolean = true; isVisibleCordinatorPicker: boolean = true; isVisiblelmPicker: boolean = true; selectedAttnDate: Date; selectedAttnMonth: Date; isLate: true; dailyAttns: DailyAttnProcess[]; dailyAttnsDT: any; monthlyAttnsDT: any; monthlyLateAttnSummaryDT: any; @ViewChild('grid', { static: false }) grid!: GridComponent; @ViewChild('gridDateWiseWorkhour', { static: false }) gridDateWiseWorkhour!: GridComponent; @ViewChild('processResultGrid', { static: false }) processResultGrid!: GridComponent; @ViewChild('gridMonthlyLateAttnSummary', { static: false }) gridMonthlyLateAttnSummary!: GridComponent; isWorkHourDialogOpen: boolean = false; workHourDetails: any[] = []; public leaveEntrys: LeaveEntry[] = []; isWorkHourMailLogOpen = false; workHourMailLogDetails: any[] = []; processResponse: any; isEffectResultDialogOpen = false; public showAttnDialog = false; lateAttendanceDetails: any[] = []; dailyAttenProcessList: DailyAttnProcess[]; mailLogTitle: string = ''; constructor( public leaveServices: LeaveService, public attendanceServices: AttendanceServices, public apiService: ApiService, public employeeService: EmployeeServices, public pmpService: PmpService, public basicService: BasicService, public notificationService: HRMNotificationService, public loadingPanel: loadingPanelService, public datePipe: DatePipe, public systemConfigService: SystemConfigurationService, public datatransferService: DataTransferService ) { this.apiService.selectedMenuName = "Work Hour Effect"; } ngOnInit() { this.selectedAttnDate = new Date(); const today = new Date(); this.selectedAttnMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1); this.isLate = true; this.loadAttendaceProcessData(new Date()); this.loadAllLeaveEntry(new Date()); } onDateChange(selectedDate: Date): void { this.selectedAttnDate = selectedDate; this.loadAttendaceProcessData(this.selectedAttnDate); } loadAllLeaveEntry(startDate: Date): void { ; const effectDate = startDate.toLocaleDateString('en-CA'); const leaveYear = new Date(effectDate).getFullYear(); this.leaveServices.getAllLeaveYear().subscribe((years: any[]) => { const selectedLeaveYear = years.find( (year: any) => new Date(year.startDate).getFullYear() === leaveYear ); console.log(selectedLeaveYear); if (selectedLeaveYear) { this.leaveServices.getAllLeaveEntrys().subscribe((entries: any[]) => { this.leaveEntrys = entries.filter( (entry: any) => entry.leaveYearId === selectedLeaveYear.id ); console.log(this.leaveEntrys); // ✅ Correct place }); } }); } isNextMonthLeaveProcessed(attnDate: Date, leaveEntrys: any[]): boolean { if (!leaveEntrys || leaveEntrys.length === 0) return false; const nextMonthDate = new Date(attnDate); nextMonthDate.setMonth(nextMonthDate.getMonth() + 1); return leaveEntrys.some((entry: any) => { const entryDate = new Date(entry.appliedFromDate); // make sure this is a valid date return entry.remarks?.toLowerCase() === 'workhour leave deduction (effect processed)' && entryDate.getMonth() === nextMonthDate.getMonth() && entryDate.getFullYear() === nextMonthDate.getFullYear(); }); } get totalWorkHour(): number { if (!this.workHourDetails || this.workHourDetails.length === 0) { return 0; } return this.workHourDetails.reduce((sum, item) => { const value = parseFloat(item.workHour); return sum + (isNaN(value) ? 0 : value); }, 0); } showWorkHourInfo(row: any): void { // Ensure attndate is a Date object const attnDate = new Date(row.attndate); if (this.isNextMonthLeaveProcessed(attnDate, this.leaveEntrys) == true) { this.notificationService.showWarning('Already Processed'); return; // stop execution } const formattedFromDate = attnDate.toLocaleDateString('en-CA'); const formattedToDate = attnDate.toLocaleDateString('en-CA'); const empIDs = row.employeeid; this.attendanceServices .getWorkHourViewRawDataDT(formattedFromDate, formattedToDate, empIDs) .subscribe(res => { this.workHourDetails = res || []; this.isWorkHourDialogOpen = true; this.loadingPanel.ShowLoadingPanel = false; }, error => { console.error('API Error:', error); this.workHourDetails = []; this.isWorkHourDialogOpen = true; this.loadingPanel.ShowLoadingPanel = false; }); } closeWorkHourDialog(): void { this.isWorkHourDialogOpen = false; } loadAttendaceProcessData(date: Date): void { // WeeklyOff = 6, // Compensation = 9, // ManualEntry = 10, // None = 0, // Present = 1, // Absent = 2, // Delay = 3, // Late = 11, // Early = 12, // Leave = 4, // Holiday = 5, // OutSideDuty = 7, // WeeklyHoliday = 8, // HalfDay = 18, // LOA = 22 if (this.isNextMonthLeaveProcessed(date, this.leaveEntrys) == true) { this.notificationService.showWarning('Already Processed'); return; // stop execution } const formattedDate = date.toLocaleDateString('en-CA'); this.loadingPanel.ShowLoadingPanel = true; console.log(this.isLate); this.attendanceServices .getDailyAttnProcessByAttnDateDT(formattedDate) .subscribe(res => { if (this.isLate == true) { this.dailyAttnsDT = res.filter(x => x.workhour <= 5 && x.attentype !== 2 && [1, 3, 11, 12].includes(x.attentype) ); } else { this.dailyAttnsDT = res; } this.loadingPanel.ShowLoadingPanel = false; }); } onLateChange(): void { if (this.selectedAttnDate) { this.loadAttendaceProcessData(this.selectedAttnDate); } } loadMonthlySummary() { if (this.isNextMonthLeaveProcessed(this.selectedAttnMonth, this.leaveEntrys)) { this.notificationService.showWarning('Already Processed'); return; // stop execution } const firstDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth(), 1); const lastDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 0); const formattedFromDate = firstDay.toLocaleDateString('en-CA'); const formattedToDate = lastDay.toLocaleDateString('en-CA'); this.attendanceServices .getWorkHourSummeryByDateRangeDT(formattedFromDate, formattedToDate) .subscribe(res => { this.monthlyAttnsDT = res.filter(x => x.leaveDeducts > 0); this.loadingPanel.ShowLoadingPanel = false; }); } loadDailyAttnByAttnTypeAndDateRange() { const firstDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth(), 1); const lastDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 0); const enumAttendanceType = 11; //Late const formattedFromDate = firstDay.toLocaleDateString('en-CA'); const formattedToDate = lastDay.toLocaleDateString('en-CA'); this.attendanceServices .loadDailyAttnByAttnTypeAndDateRange(formattedFromDate, formattedToDate, enumAttendanceType) .subscribe(res => { this.dailyAttenProcessList = res; console.log(this.dailyAttenProcessList); this.loadingPanel.ShowLoadingPanel = false; }); } export(): void { if (!this.grid) { console.error('Grid is not initialized yet.'); return; } if (!this.monthlyAttnsDT?.length) { this.notificationService.showWarning('No data to export'); return; } this.grid.saveAsExcel(); } exportMonthlyLateAttnSummary(): void { if (!this.gridMonthlyLateAttnSummary) { console.error('Grid is not initialized yet.'); return; } if (!this.monthlyLateAttnSummaryDT?.length) { this.notificationService.showWarning('No data to export'); return; } this.gridMonthlyLateAttnSummary.saveAsExcel(); } exportDateWiseWorkhour(): void { if (!this.gridDateWiseWorkhour) { console.error('Grid is not initialized yet.'); return; } if (!this.dailyAttnsDT?.length) { this.notificationService.showWarning('No data to export'); return; } this.gridDateWiseWorkhour.saveAsExcel(); } effectProcess() { if (!this.monthlyAttnsDT?.length) { this.notificationService.showWarning('No data to process.'); return; } if (this.isNextMonthLeaveProcessed(this.selectedAttnMonth, this.leaveEntrys)) { this.notificationService.showWarning('Already Processed'); return; // stop execution } const confirmation = window.confirm( "You are about to initiate the final processing. Based on the reviewed data, annual leave deductions will be applied, and mail notifications will be sent to all employees. This action is irreversible. Do you wish to proceed?"); if (confirmation) { const workhourEffect: any = { effectDatalist: this.monthlyAttnsDT, effectmonth: this.selectedAttnMonth }; this.sendEffectData(workhourEffect); } } sendEffectData(workhourEffect: any) { this.loadingPanel.ShowLoadingPanel = true; this.attendanceServices .workhourEffectProcess(workhourEffect) .subscribe({ next: (res: any) => { this.processResponse = res; this.isEffectResultDialogOpen = true; this.loadingPanel.ShowLoadingPanel = false; }, error: (err) => { this.notificationService.showWarning('An error occurred while processing the workhour effect. Please try again.'); this.loadingPanel.ShowLoadingPanel = false; } }); } exportToExcel() { if (this.processResultGrid) { this.processResultGrid.saveAsExcel(); } } closeEffectResultDialog() { this.isEffectResultDialogOpen = false; } sendMailNotification() { if (this.monthlyAttnsDT === undefined || this.monthlyAttnsDT.length === 0) { this.notificationService.showWarning('Data not found for process.'); return; } const data = { effectDatalist: this.monthlyAttnsDT, effectMonth: this.selectedAttnMonth }; this.loadingPanel.ShowLoadingPanel = true; //this.attendanceServices // .sendEffectProcessMailForMultiple(data) // .subscribe({ // next: (res: any) => { // this.notificationService.showWarning('Annual leave deduction alert mail has been send successfully.'); // this.loadingPanel.ShowLoadingPanel = false; // }, // error: (err) => { // this.notificationService.showWarning('An error occurred while sending mail. Please try again.'); // this.loadingPanel.ShowLoadingPanel = false; // } // }); this.notificationService.showWarning( 'Mail sending has been initiated. The process may take several minutes depending on the data volume. You may continue with other tasks in the meantime. You can check the mail status by clicking the Log button.' ); this.loadingPanel.ShowLoadingPanel = true; this.attendanceServices .sendEffectProcessMailForMultiple(data) .subscribe({ next: (res: any) => { this.loadingPanel.ShowLoadingPanel = false; }, error: (err) => { this.notificationService.showWarning( 'An error occurred while initiating the mail sending process. Please try again.' ); this.loadingPanel.ShowLoadingPanel = false; } }); } openWorkHourMailLog() { if (this.selectedAttnMonth === undefined) { this.notificationService.showWarning('Select a valid month.'); return; } const data = { effectMonth: this.selectedAttnMonth }; const nextMonthDate = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 1); const monthText = nextMonthDate.toLocaleString('en-US', { month: 'long', year: 'numeric' }); this.mailLogTitle = `Mail Log Status - ${monthText}`; this.attendanceServices.getWorkHourMailLog(data).subscribe({ next: (data) => { this.workHourMailLogDetails = data; this.isWorkHourMailLogOpen = true; }, error: (err) => console.error(err) }); } closeWorkHourMailLog() { this.isWorkHourMailLogOpen = false; } onMonthChange(newDate: Date): void { this.selectedAttnMonth = newDate; // Call your reload logic here this.loadMonthlyLateAttnSummary(); } loadMonthlyLateAttnSummary() { const firstDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth(), 1); const lastDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 0); const formattedFromDate = firstDay.toLocaleDateString('en-CA'); const formattedToDate = lastDay.toLocaleDateString('en-CA'); this.attendanceServices .getMonthlyLateAttnSummaryDT(formattedFromDate, formattedToDate) .subscribe(res => { this.monthlyLateAttnSummaryDT = res.filter(x => x.leaveDeducts > 0); console.log(this.monthlyLateAttnSummaryDT); this.loadingPanel.ShowLoadingPanel = false; }); } showAttendanceDetails(row: any) { const firstDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth(), 1); const lastDay = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 0); const formattedFromDate = firstDay.toLocaleDateString('en-CA'); const formattedToDate = lastDay.toLocaleDateString('en-CA'); this.attendanceServices .getMonthlyLateAttnDetailsDT(formattedFromDate, formattedToDate) .subscribe(res => { console.log('API Response:', res); console.log('Row Data:', row); this.lateAttendanceDetails = res.filter(x => x.employeeid == row.employeeid); //this.dailyAttenProcessList.forEach(x => { // x.attnDate = new Date(x.attnDate); // x.shiftName = x.shiftName // x.inTime = new Date(x.inTime); //}); console.log(this.lateAttendanceDetails); this.showAttnDialog = true; this.loadingPanel.ShowLoadingPanel = false; }); } onNotify(dataItem: any): void { const dayNo = this.getNextMonthDayNumber(new Date(), this.selectedAttnMonth); const nextMonthDate = new Date(this.selectedAttnMonth.getFullYear(),this.selectedAttnMonth.getMonth() + 1, 1); const monthText = nextMonthDate.toLocaleString('en-US', {month: 'long',year: 'numeric'}); //if (dayNo === null || dayNo < 1 || dayNo > 3) { // this.notificationService.showWarning( // `Notify is allowed only from the 1st to 3rd day of ${monthText}.` // ); // return; //} const data = { effectDatalist: [dataItem], effectMonth: this.selectedAttnMonth }; this.loadingPanel.ShowLoadingPanel = true; this.notificationService.showWarning( 'Mail sending has been initiated. The process may take minutes depending on the data volume and network. You may continue with other tasks in the meantime. You can check the mail status by clicking the Log button.' ); this.loadingPanel.ShowLoadingPanel = true; this.attendanceServices .sendMailMonthlyLateAttnSummaryForMultiple(data) .subscribe({ next: (res: any) => { this.loadingPanel.ShowLoadingPanel = false; }, error: (err) => { this.notificationService.showWarning( 'An error occurred while initiating the mail sending process. Please try again.' ); this.loadingPanel.ShowLoadingPanel = false; } }); } onNotifyAll(): void { const dayNo = this.getNextMonthDayNumber(new Date(), this.selectedAttnMonth); const nextMonthDate = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 1); const monthText = nextMonthDate.toLocaleString('en-US', { month: 'long', year: 'numeric' }); //if (dayNo === null || dayNo < 1 || dayNo > 3) { // this.notificationService.showWarning( // `Notify is allowed only from the 1st to 3rd day of ${monthText}.` // ); // return; //} if (this.monthlyLateAttnSummaryDT === undefined || (this.monthlyLateAttnSummaryDT.filter(x => x.isEffectApplied == 0)).length === 0) { this.notificationService.showWarning('Data not found for process.'); return; } const data = { effectDatalist: this.monthlyLateAttnSummaryDT.filter(x => x.isEffectApplied == 0), effectMonth: this.selectedAttnMonth }; this.loadingPanel.ShowLoadingPanel = true; this.notificationService.showWarning( 'Mail sending has been initiated. The process may take minutes depending on the data volume and network. You may continue with other tasks in the meantime. You can check the mail status by clicking the Log button.' ); this.loadingPanel.ShowLoadingPanel = true; this.attendanceServices .sendMailMonthlyLateAttnSummaryForMultiple(data) .subscribe({ next: (res: any) => { this.loadingPanel.ShowLoadingPanel = false; }, error: (err) => { this.notificationService.showWarning( 'An error occurred while initiating the mail sending process. Please try again.' ); this.loadingPanel.ShowLoadingPanel = false; } }); } onEffect(dataItem: any): void { const dayNo = this.getNextMonthDayNumber(new Date(), this.selectedAttnMonth); const nextMonthDate = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 1); const monthText = nextMonthDate.toLocaleString('en-US', { month: 'long', year: 'numeric' }); //if (dayNo === null || dayNo < 5 || dayNo > 5) { // this.notificationService.showWarning( // `Effect is allowed only on the 5th day of ${monthText}.` // ); // return; //} const confirmation = window.confirm( "You are about to initiate the final processing. Based on the reviewed data, annual leave deductions will be applied, and mail notifications will be sent to all employees. This action is irreversible. Do you wish to proceed?"); if (confirmation) { const lateAttnEffect: any = { effectDatalist: [dataItem], effectmonth: this.selectedAttnMonth }; this.sendMonthlyLateAttenEffectProcess(lateAttnEffect); } } onEffectAll(): void { const dayNo = this.getNextMonthDayNumber(new Date(), this.selectedAttnMonth); const nextMonthDate = new Date(this.selectedAttnMonth.getFullYear(), this.selectedAttnMonth.getMonth() + 1, 1); const monthText = nextMonthDate.toLocaleString('en-US', { month: 'long', year: 'numeric' }); //if (dayNo === null || dayNo < 5 || dayNo > 5) { // this.notificationService.showWarning( // `Effect is allowed only on the 5th day of ${monthText}.` // ); // return; //} if (this.monthlyLateAttnSummaryDT === undefined || (this.monthlyLateAttnSummaryDT.filter(x => x.isEffectApplied == 0)).length === 0) { this.notificationService.showWarning('Data not found for process.'); return; } const confirmation = window.confirm( "You are about to initiate the final processing. Based on the reviewed data, annual leave deductions will be applied, and mail notifications will be sent to all employees. This action is irreversible. Do you wish to proceed?"); if (confirmation) { const lateAttnEffect: any = { effectDatalist: this.monthlyLateAttnSummaryDT.filter(x => x.isEffectApplied == 0), effectmonth: this.selectedAttnMonth }; this.sendMonthlyLateAttenEffectProcess(lateAttnEffect); } } get totalEmployees(): number { return this.monthlyLateAttnSummaryDT?.length || 0; } get effectApplied(): number { return this.monthlyLateAttnSummaryDT?.filter(x => x.isEffectApplied === 1).length || 0; } get remaining(): number { return this.monthlyLateAttnSummaryDT?.filter(x => x.isEffectApplied === 0).length || 0; } sendMonthlyLateAttenEffectProcess(lateAttnEffect: any) { this.loadingPanel.ShowLoadingPanel = true; this.attendanceServices .monthlyLateAttenEffectProcess(lateAttnEffect) .subscribe({ next: (res: any) => { this.processResponse = res; this.isEffectResultDialogOpen = true; //Refresh list here this.loadMonthlyLateAttnSummary(); this.loadingPanel.ShowLoadingPanel = false; }, error: (err) => { this.notificationService.showWarning('An error occurred while processing the workhour effect. Please try again.'); this.loadingPanel.ShowLoadingPanel = false; } }); } getNextMonthDayNumber(today: Date, selectedMonth: Date): number | null { const nextMonth = new Date( selectedMonth.getFullYear(), selectedMonth.getMonth() + 1, 1 ); // Check if today is in the next month if ( today.getFullYear() === nextMonth.getFullYear() && today.getMonth() === nextMonth.getMonth() ) { return today.getDate(); // e.g. 1, 2, 3... } return null; // not in next month } }