import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core'; import { DatePipe } from '@angular/common'; import { ApiService } from 'src/app/app.api.service'; import { HRMNotificationService } from 'src/app/app.notification.service'; import { loadingPanelService } from 'src/app/hrm-loding panel/loding.panel.service'; import { EmployeeServices } from 'src/app/_services/employee/employee.service'; import { SystemConfigurationService } from 'src/app/_services/common/systemconfiguration.service'; import { SystemConfigaration } from 'src/app/_models/common/SystemConfigaration'; import { EnumConfigurationType } from 'src/app/_models/enums'; interface GridColumn { field: string; title: string; width: number; } /** * Discontinue Employee Details: the same columns as Employee Details * (/reports/report-viewer/empDetailInfo), but only for the employees discontinued * inside a date range, plus their Discontinue Date. Export to Excel from the grid. */ @Component({ selector: 'app-discontinue-employee-details', templateUrl: './discontinue-employee-details.component.html', styleUrls: ['./discontinue-employee-details.component.scss'], changeDetection: ChangeDetectionStrategy.Eager, standalone: false, }) export class DiscontinueEmployeeDetailsComponent implements OnInit { fromDate: Date = new Date(new Date().getFullYear(), new Date().getMonth(), 1); toDate: Date = new Date(); dateFormat = 'dd-MMM-yyyy'; data: any[] = []; filteredData: any[] = []; searchText = ''; showGrid = false; columns: GridColumn[] = []; excelFileName = 'DiscontinueEmployeeDetails.xlsx'; constructor(public apiService: ApiService, public notificationService: HRMNotificationService, public loadingPanel: loadingPanelService, private employeeService: EmployeeServices, private systemConfigService: SystemConfigurationService, private datePipe: DatePipe) { this.apiService.selectedMenuName = 'Discontinue Employee Details'; } ngOnInit(): void { this.buildColumns([]); this.systemConfigService.GetAllChildrenByParentNode(EnumConfigurationType.Logic, 'department').subscribe( (configs: SystemConfigaration[]) => this.buildColumns(configs || []), () => this.buildColumns([]) ); } /** Same columns, in the same order, as the Employee Details report, plus Discontinue Date. */ private buildColumns(departmentConfigs: SystemConfigaration[]): void { const cols: GridColumn[] = [ { field: 'employeeNo', title: 'Employee No', width: 110 }, { field: 'name', title: 'Employee Name', width: 220 }, { field: 'gender', title: 'Gender', width: 100 }, { field: 'birthDate', title: 'Birth Date', width: 120 }, { field: 'joiningDate', title: 'Joining Date', width: 120 }, { field: 'discontinueDate', title: 'Discontinue Date', width: 130 }, { field: 'mobileNo', title: 'Mobile No', width: 130 }, { field: 'emailAddress', title: 'Email Address', width: 200 }, { field: 'designation', title: 'Designation', width: 160 }, { field: 'grade', title: 'Grade', width: 120 }, { field: 'category', title: 'Category', width: 120 }, { field: 'status', title: 'Status', width: 120 }, ]; // Department tier headings come from the Logic configuration, exactly as in Employee Details. const tierFields = ['deptireOne', 'deptireTwo', 'deptireThree', 'deptireFour', 'deptireFive']; let ind = 0; departmentConfigs.forEach(c => { if (c.node && c.node.toLocaleLowerCase() === 'text') { if (ind < tierFields.length) { cols.push({ field: tierFields[ind], title: c.value, width: 150 }); } ind++; } }); cols.push( { field: 'location', title: 'Location', width: 130 }, { field: 'religion', title: 'Religion', width: 110 }, { field: 'maritalStatus', title: 'Marital Status', width: 120 }, { field: 'dateOfConfirmation', title: 'Confirmation', width: 120 }, { field: 'basicSalary', title: 'Basic Salary', width: 120 }, { field: 'grossSalary', title: 'Gross Salary', width: 120 }, { field: 'passportNo', title: 'Passport No', width: 120 }, { field: 'bloodGroup', title: 'Blood Group', width: 110 }, { field: 'lineManagerNo', title: 'LineManager No', width: 130 }, { field: 'lineManagerName', title: 'LineManager Name', width: 180 }, { field: 'lineManagerEmail', title: 'LineManager Email', width: 200 }, { field: 'empStatus', title: 'Employee Status', width: 130 }, { field: 'bank', title: 'Bank', width: 150 }, { field: 'branch', title: 'Branch', width: 150 }, { field: 'accountNo', title: 'Account No', width: 150 }, { field: 'nationalId', title: 'National ID/NID', width: 150 }, { field: 'tinNo', title: 'TIN', width: 120 }, ); this.columns = cols; } preview(): void { if (!this.fromDate || !this.toDate) { this.notificationService.showWarning('Please select From Date and To Date.', 'Warning'); return; } if (this.fromDate > this.toDate) { this.notificationService.showWarning('From Date cannot be after To Date.', 'Warning'); return; } const from = this.datePipe.transform(this.fromDate, 'yyyy-MM-dd'); const to = this.datePipe.transform(this.toDate, 'yyyy-MM-dd'); this.excelFileName = 'DiscontinueEmployeeDetails_' + from + '_to_' + to + '.xlsx'; this.loadingPanel.ShowLoadingPanel = true; this.showGrid = false; this.searchText = ''; this.employeeService.getDiscontinueEmpDetailInfo(from, to).subscribe( (resp: any[]) => { this.data = (resp || []).map(row => this.formatRow(row)); this.filteredData = this.data; }, (err) => { this.loadingPanel.ShowLoadingPanel = false; this.notificationService.showError(err && err.error ? err.error : 'Failed to load discontinued employees.'); }, () => { this.loadingPanel.ShowLoadingPanel = false; this.showGrid = true; if (this.data.length === 0) { this.notificationService.showWarning('No employee was discontinued in the selected date range.', 'Warning'); } } ); } /** Keeps the rows where any shown column contains the search text (case-insensitive). */ onSearch(value: string): void { const term = (value || '').trim().toLowerCase(); if (!term) { this.filteredData = this.data; return; } const fields = this.columns.map(c => c.field); this.filteredData = this.data.filter(row => fields.some(f => row[f] != null && String(row[f]).toLowerCase().includes(term))); } /** Same display formatting as Employee Details: dates as 'dd MMM yyyy', salaries with 2 decimals. */ private formatRow(row: any): any { ['birthDate', 'joiningDate', 'dateOfConfirmation', 'discontinueDate'].forEach(f => { row[f] = row[f] != null ? this.datePipe.transform(row[f], 'dd MMM yyyy') : ''; }); const money = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); ['basicSalary', 'grossSalary'].forEach(f => { row[f] = row[f] != null ? money.format(row[f]) : '0.00'; }); return row; } }