From b11141a57c0bed68b43b583b8ace6f973e275600 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 11:59:10 +0600 Subject: [PATCH 1/8] Discontinue Employee Reports --- HRM.BO/Employee/Employee.cs | 1 + HRM.DA/DA/Employee/EmployeeDA.cs | 12 ++ HRM.DA/Service/Employee/EmployeeService.cs | 31 ++++ .../_services/employee/employee.service.ts | 6 + ...iscontinue-employee-details.component.html | 44 +++++ ...iscontinue-employee-details.component.scss | 60 +++++++ .../discontinue-employee-details.component.ts | 168 ++++++++++++++++++ .../src/app/reports/reports-routing.module.ts | 2 + .../src/app/reports/reports.module.ts | 4 +- .../Employee/EmployeeController.cs | 69 +++++++ 10 files changed, 396 insertions(+), 1 deletion(-) create mode 100644 HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html create mode 100644 HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss create mode 100644 HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts diff --git a/HRM.BO/Employee/Employee.cs b/HRM.BO/Employee/Employee.cs index 4ddf416..25e8573 100644 --- a/HRM.BO/Employee/Employee.cs +++ b/HRM.BO/Employee/Employee.cs @@ -522,6 +522,7 @@ namespace HRM.BO List GetAllEmps(); void UpdateLM2(List emps); List GetWithDiscontinue(int payrollTypeID); + List GetDiscontinuedInRange(DateTime fromDate, DateTime toDate, int payrollTypeID); List GetSubordinates(int employeeID); //List GetAllSubordinates(int nodeID); List GetByDeptShiftGFType(string deptIDs, int shiftID, string GFType, int payrollTypeID); diff --git a/HRM.DA/DA/Employee/EmployeeDA.cs b/HRM.DA/DA/Employee/EmployeeDA.cs index 6adbd52..27d205e 100644 --- a/HRM.DA/DA/Employee/EmployeeDA.cs +++ b/HRM.DA/DA/Employee/EmployeeDA.cs @@ -663,6 +663,18 @@ namespace HRM.DA return tc.ExecuteReader(sql); } + // Discontinue Employee Details report: employees discontinued (Status = Discontinued) + // whose discontinue date (ENDOFCONTRACTDATE) falls inside the date range. + internal static IDataReader GetDiscontinuedInRange(TransactionContext tc, DateTime fromDate, DateTime toDate, + int payrollTypeID) + { + string sql = SQLParser.MakeSQL( + "SELECT * FROM Employee WHERE STATUS = %n AND ENDOFCONTRACTDATE IS NOT NULL " + + "AND CAST(ENDOFCONTRACTDATE AS DATE) BETWEEN %d AND %d AND PAYROLLTYPEID = %n order by EmployeeNo", + (int)EnumEmployeeStatus.Discontinued, fromDate, toDate, payrollTypeID); + return tc.ExecuteReader(sql); + } + internal static IDataReader GetAllSubordinatesNew(TransactionContext tc, int employeeID) { //For Oracle diff --git a/HRM.DA/Service/Employee/EmployeeService.cs b/HRM.DA/Service/Employee/EmployeeService.cs index f0d72a9..5b368ad 100644 --- a/HRM.DA/Service/Employee/EmployeeService.cs +++ b/HRM.DA/Service/Employee/EmployeeService.cs @@ -1482,6 +1482,37 @@ namespace HRM.DA return employees; } + public List GetDiscontinuedInRange(DateTime fromDate, DateTime toDate, int payrollTypeID) + { + List employees = new List(); + + TransactionContext tc = null; + try + { + tc = TransactionContext.Begin(); + + DataReader dr = new DataReader(EmployeeDA.GetDiscontinuedInRange(tc, fromDate, toDate, payrollTypeID)); + employees = this.CreateObjects(dr); + dr.Close(); + + tc.End(); + } + catch (Exception e) + { + #region Handle Exception + + if (tc != null) + tc.HandleError(); + ExceptionLog.Write(e); + + throw new ServiceException(e.Message, e); + + #endregion + } + + return employees; + } + public List GetWithDiscontinue(int payrollTypeID) { List employees = new List(); diff --git a/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts b/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts index 87e80c1..6eb58a8 100644 --- a/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts +++ b/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts @@ -326,6 +326,12 @@ export class EmployeeServices { return this.apiService.httpPost('/Employee/getEmpDetailInfo', emps); } + // Discontinue Employee Details report: same rows as getEmpDetailInfo plus discontinueDate, + // for employees discontinued between fromDate and toDate (both 'yyyy-MM-dd'). + getDiscontinueEmpDetailInfo(fromDate: string, toDate: string) { + return this.apiService.httpPost('/Employee/getDiscontinueEmpDetailInfo', { fromDate: fromDate, toDate: toDate }); + } + getCurrentHeadCount(departmentId: number, designationId: number, gradeId: number) { return this.apiService.httpGet('/Employee/getCurrentHeadCount' + '/' + departmentId + '/' + designationId + '/' + gradeId); } diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html new file mode 100644 index 0000000..fe546dc --- /dev/null +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html @@ -0,0 +1,44 @@ + +
+Search Criteria +
+
+ + +
+
+ + +
+
+ +
+
+
+ +@if (showGrid) { +
+ + + + + + + + + @for (column of columns; track column.field) { + + + } + + +
+} diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss new file mode 100644 index 0000000..778804b --- /dev/null +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss @@ -0,0 +1,60 @@ +// Bordered box around the search criteria. +.filter-box { + border: 1px solid #d0d5dd; + border-radius: 6px; + padding: 4px 16px 16px; + margin: 0 0.5rem 14px; + + legend { + padding: 0 6px; + font-weight: 600; + width: auto; + } +} + +// Labels sit above their date pickers; the Preview button lines up with the pickers' bottom edge. +.filter-bar { + display: flex; + flex-wrap: wrap; + align-items: flex-end; + gap: 12px 24px; +} + +.filter-field { + display: flex; + flex-direction: column; + gap: 4px; + width: 280px; + + label { + font-weight: 600; + } + + kendo-datepicker { + width: 100%; + } +} + +.filter-action { + margin-left: auto; +} + +.grid-search { + width: 280px; +} + +.grid-count { + margin-left: 12px; + white-space: nowrap; +} + +@media (max-width: 600px) { + .filter-field, + .grid-search { + width: 100%; + } + + .filter-action { + margin-left: 0; + } +} diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts new file mode 100644 index 0000000..8472e09 --- /dev/null +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts @@ -0,0 +1,168 @@ +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; + } +} diff --git a/HRM.UI/ClientApp/src/app/reports/reports-routing.module.ts b/HRM.UI/ClientApp/src/app/reports/reports-routing.module.ts index b4244e2..5b884a3 100644 --- a/HRM.UI/ClientApp/src/app/reports/reports-routing.module.ts +++ b/HRM.UI/ClientApp/src/app/reports/reports-routing.module.ts @@ -33,6 +33,7 @@ import { CcwiseNewPfMemberWithAmountComponent } from './ccwise-new-pf-member-wit import { TrainingReportsComponent } from './training-reports/training-reports.component'; import { EssTaxCertificateComponent } from './ess-tax-certificate/ess-tax-certificate.component'; import { JvReportComponent } from './jv-report/jv-report.component'; +import { DiscontinueEmployeeDetailsComponent } from './discontinue-employee-details/discontinue-employee-details.component'; export const routes: Routes = [ { path: 'report-viewer/36', component: ReportViewerComponent, canActivate: [AuthGuard] }, @@ -82,6 +83,7 @@ export const routes: Routes = [ { path: 'date-wise-job-card-new', component: DateWiseJobCardNewComponent, canActivate: [AuthGuard] }, { path: 'date-wise-attn-records', component: DateWiseAttnRecordsComponent, canActivate: [AuthGuard] }, { path: 'monthly-exception-report', component: MonthlyExceptionReportComponent, canActivate: [AuthGuard] }, + { path: 'discontinue-employee-details', component: DiscontinueEmployeeDetailsComponent, canActivate: [AuthGuard] }, { path: 'report-viewer/34', component: ReportViewerComponent, canActivate: [AuthGuard] }, { path: 'report-viewer/170', component: ReportViewerComponent, canActivate: [AuthGuard] }, { path: 'report-viewer/169', component: ReportViewerComponent, canActivate: [AuthGuard] }, diff --git a/HRM.UI/ClientApp/src/app/reports/reports.module.ts b/HRM.UI/ClientApp/src/app/reports/reports.module.ts index d593a5b..5101ad7 100644 --- a/HRM.UI/ClientApp/src/app/reports/reports.module.ts +++ b/HRM.UI/ClientApp/src/app/reports/reports.module.ts @@ -56,6 +56,7 @@ import { TabViewCompatModule } from 'src/app/custom-components/tab-view-compat/t import { EssTaxCertificateComponent } from './ess-tax-certificate/ess-tax-certificate.component'; import { EmpCommonInformationModule } from '../ess/emp-common-information/EmpCommonInformationModule'; import { JvReportComponent } from './jv-report/jv-report.component'; +import { DiscontinueEmployeeDetailsComponent } from './discontinue-employee-details/discontinue-employee-details.component'; @NgModule({ declarations: [ @@ -86,7 +87,8 @@ import { JvReportComponent } from './jv-report/jv-report.component'; TrainingReportsComponent, CareerHistoryReportComponent, EssTaxCertificateComponent, - JvReportComponent + JvReportComponent, + DiscontinueEmployeeDetailsComponent ], imports: [ TextBoxModule, diff --git a/HRM.UI/Controllers/Employee/EmployeeController.cs b/HRM.UI/Controllers/Employee/EmployeeController.cs index 9dd96d8..fe3b1a4 100644 --- a/HRM.UI/Controllers/Employee/EmployeeController.cs +++ b/HRM.UI/Controllers/Employee/EmployeeController.cs @@ -2213,6 +2213,75 @@ namespace HRM.UI.Controllers return Ok(items); } + public class DiscontinueEmpDetailRequest + { + public string FromDate { get; set; } // yyyy-MM-dd + public string ToDate { get; set; } // yyyy-MM-dd + } + + /// + /// Discontinue Employee Details report: the same columns as Employee Details (getEmpDetailInfo), + /// but only for employees discontinued between FromDate and ToDate (by discontinue date), + /// in the current payroll type, plus their Discontinue Date. Reuses GetEmpDetailInfo unchanged. + /// + [HttpPost("getDiscontinueEmpDetailInfo")] + public ActionResult GetDiscontinueEmpDetailInfo(DiscontinueEmpDetailRequest request) + { + DateTime fromDate, toDate; + if (request == null + || !DateTime.TryParseExact(request.FromDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, + System.Globalization.DateTimeStyles.None, out fromDate) + || !DateTime.TryParseExact(request.ToDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, + System.Globalization.DateTimeStyles.None, out toDate)) + return BadRequest("Please select a valid From Date and To Date."); + if (fromDate > toDate) + return BadRequest("From Date cannot be after To Date."); + + List discontinued; + try + { + CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + discontinued = _EmployeeService.GetDiscontinuedInRange(fromDate, toDate, (int)currentUser.PayrollTypeID); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + } + + if (discontinued.Count == 0) + return Ok(new List()); + + // Built here rather than with Employee.getSearchEmp, which casts DepartmentID for + // LocationID/GradeID and throws when an employee has no department. + List selected = discontinued + .Select(x => new SearchEmployee { ID = x.ID, EmployeeID = x.ID, EmployeeNo = x.EmployeeNo, Name = x.Name }) + .ToList(); + ActionResult detailResult = GetEmpDetailInfo(selected); + if (!(detailResult is OkObjectResult ok) || !(ok.Value is List details)) + return detailResult; + + var serializer = JsonSerializer.Create(new JsonSerializerSettings + { + ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() + }); + var discontinueDates = discontinued + .GroupBy(x => x.EmployeeNo) + .ToDictionary(g => g.Key, g => g.First().EndOfContractDate); + + var rows = new List(); + foreach (EmployeeDetailInfo item in details) + { + var row = Newtonsoft.Json.Linq.JObject.FromObject(item, serializer); + DateTime? discontinueDate = null; + if (item.EmployeeNo != null && discontinueDates.TryGetValue(item.EmployeeNo, out DateTime? d)) + discontinueDate = d; + row["discontinueDate"] = discontinueDate; + rows.Add(row); + } + + return Ok(rows); + } + //[HttpPost("getEmpApiResponse")] //[AllowAnonymous] From 78747c06746afc30a7e6aa6463a951778fd59380 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 12:39:38 +0600 Subject: [PATCH 2/8] Previous --- HRM.BO/Employee/Employee.cs | 1 - HRM.DA/DA/Employee/EmployeeDA.cs | 12 ---- HRM.DA/Service/Employee/EmployeeService.cs | 31 --------- .../Employee/EmployeeController.cs | 69 ------------------- 4 files changed, 113 deletions(-) diff --git a/HRM.BO/Employee/Employee.cs b/HRM.BO/Employee/Employee.cs index 25e8573..4ddf416 100644 --- a/HRM.BO/Employee/Employee.cs +++ b/HRM.BO/Employee/Employee.cs @@ -522,7 +522,6 @@ namespace HRM.BO List GetAllEmps(); void UpdateLM2(List emps); List GetWithDiscontinue(int payrollTypeID); - List GetDiscontinuedInRange(DateTime fromDate, DateTime toDate, int payrollTypeID); List GetSubordinates(int employeeID); //List GetAllSubordinates(int nodeID); List GetByDeptShiftGFType(string deptIDs, int shiftID, string GFType, int payrollTypeID); diff --git a/HRM.DA/DA/Employee/EmployeeDA.cs b/HRM.DA/DA/Employee/EmployeeDA.cs index 27d205e..6adbd52 100644 --- a/HRM.DA/DA/Employee/EmployeeDA.cs +++ b/HRM.DA/DA/Employee/EmployeeDA.cs @@ -663,18 +663,6 @@ namespace HRM.DA return tc.ExecuteReader(sql); } - // Discontinue Employee Details report: employees discontinued (Status = Discontinued) - // whose discontinue date (ENDOFCONTRACTDATE) falls inside the date range. - internal static IDataReader GetDiscontinuedInRange(TransactionContext tc, DateTime fromDate, DateTime toDate, - int payrollTypeID) - { - string sql = SQLParser.MakeSQL( - "SELECT * FROM Employee WHERE STATUS = %n AND ENDOFCONTRACTDATE IS NOT NULL " - + "AND CAST(ENDOFCONTRACTDATE AS DATE) BETWEEN %d AND %d AND PAYROLLTYPEID = %n order by EmployeeNo", - (int)EnumEmployeeStatus.Discontinued, fromDate, toDate, payrollTypeID); - return tc.ExecuteReader(sql); - } - internal static IDataReader GetAllSubordinatesNew(TransactionContext tc, int employeeID) { //For Oracle diff --git a/HRM.DA/Service/Employee/EmployeeService.cs b/HRM.DA/Service/Employee/EmployeeService.cs index 5b368ad..f0d72a9 100644 --- a/HRM.DA/Service/Employee/EmployeeService.cs +++ b/HRM.DA/Service/Employee/EmployeeService.cs @@ -1482,37 +1482,6 @@ namespace HRM.DA return employees; } - public List GetDiscontinuedInRange(DateTime fromDate, DateTime toDate, int payrollTypeID) - { - List employees = new List(); - - TransactionContext tc = null; - try - { - tc = TransactionContext.Begin(); - - DataReader dr = new DataReader(EmployeeDA.GetDiscontinuedInRange(tc, fromDate, toDate, payrollTypeID)); - employees = this.CreateObjects(dr); - dr.Close(); - - tc.End(); - } - catch (Exception e) - { - #region Handle Exception - - if (tc != null) - tc.HandleError(); - ExceptionLog.Write(e); - - throw new ServiceException(e.Message, e); - - #endregion - } - - return employees; - } - public List GetWithDiscontinue(int payrollTypeID) { List employees = new List(); diff --git a/HRM.UI/Controllers/Employee/EmployeeController.cs b/HRM.UI/Controllers/Employee/EmployeeController.cs index fe3b1a4..9dd96d8 100644 --- a/HRM.UI/Controllers/Employee/EmployeeController.cs +++ b/HRM.UI/Controllers/Employee/EmployeeController.cs @@ -2213,75 +2213,6 @@ namespace HRM.UI.Controllers return Ok(items); } - public class DiscontinueEmpDetailRequest - { - public string FromDate { get; set; } // yyyy-MM-dd - public string ToDate { get; set; } // yyyy-MM-dd - } - - /// - /// Discontinue Employee Details report: the same columns as Employee Details (getEmpDetailInfo), - /// but only for employees discontinued between FromDate and ToDate (by discontinue date), - /// in the current payroll type, plus their Discontinue Date. Reuses GetEmpDetailInfo unchanged. - /// - [HttpPost("getDiscontinueEmpDetailInfo")] - public ActionResult GetDiscontinueEmpDetailInfo(DiscontinueEmpDetailRequest request) - { - DateTime fromDate, toDate; - if (request == null - || !DateTime.TryParseExact(request.FromDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, - System.Globalization.DateTimeStyles.None, out fromDate) - || !DateTime.TryParseExact(request.ToDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, - System.Globalization.DateTimeStyles.None, out toDate)) - return BadRequest("Please select a valid From Date and To Date."); - if (fromDate > toDate) - return BadRequest("From Date cannot be after To Date."); - - List discontinued; - try - { - CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); - discontinued = _EmployeeService.GetDiscontinuedInRange(fromDate, toDate, (int)currentUser.PayrollTypeID); - } - catch (Exception e) - { - return StatusCode(StatusCodes.Status500InternalServerError, e.Message); - } - - if (discontinued.Count == 0) - return Ok(new List()); - - // Built here rather than with Employee.getSearchEmp, which casts DepartmentID for - // LocationID/GradeID and throws when an employee has no department. - List selected = discontinued - .Select(x => new SearchEmployee { ID = x.ID, EmployeeID = x.ID, EmployeeNo = x.EmployeeNo, Name = x.Name }) - .ToList(); - ActionResult detailResult = GetEmpDetailInfo(selected); - if (!(detailResult is OkObjectResult ok) || !(ok.Value is List details)) - return detailResult; - - var serializer = JsonSerializer.Create(new JsonSerializerSettings - { - ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() - }); - var discontinueDates = discontinued - .GroupBy(x => x.EmployeeNo) - .ToDictionary(g => g.Key, g => g.First().EndOfContractDate); - - var rows = new List(); - foreach (EmployeeDetailInfo item in details) - { - var row = Newtonsoft.Json.Linq.JObject.FromObject(item, serializer); - DateTime? discontinueDate = null; - if (item.EmployeeNo != null && discontinueDates.TryGetValue(item.EmployeeNo, out DateTime? d)) - discontinueDate = d; - row["discontinueDate"] = discontinueDate; - rows.Add(row); - } - - return Ok(rows); - } - //[HttpPost("getEmpApiResponse")] //[AllowAnonymous] From 7b43017c5270beee61e32ad0aa74455221bb28ba Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 14:41:04 +0600 Subject: [PATCH 3/8] Virtual Scroll Grid --- DiscontinueEmployeeReport_DB_Changes.sql | 27 + HRM.BO/Employee/Employee.cs | 3 + .../Report/DiscontinueEmployeeDetailInfo.cs | 14 + HRM.BO/VirtualGrid/VirtualGrid.cs | 110 ++++ HRM.DA/DA/Employee/EmployeeDA.cs | 16 + HRM.DA/DA/VirtualGrid/VirtualGridDA.cs | 110 ++++ HRM.DA/Service/Employee/EmployeeService.cs | 154 +++++ .../DiscontinueEmployeeVirtualGrid.cs | 103 +++ HRM.DA/Service/VirtualGrid/README.md | 88 +++ .../VirtualGrid/VirtualGridDefinition.cs | 271 ++++++++ .../VirtualGrid/VirtualGridRegistry.cs | 27 + .../Service/VirtualGrid/VirtualGridService.cs | 591 ++++++++++++++++++ .../src/app/_models/common/virtual-grid.ts | 63 ++ .../_services/common/virtual-grid.service.ts | 30 + .../_services/employee/employee.service.ts | 6 - .../virtual-grid/virtual-grid.component.html | 46 ++ .../virtual-grid/virtual-grid.component.scss | 48 ++ .../virtual-grid/virtual-grid.component.ts | 290 +++++++++ .../virtual-grid/virtual-grid.module.ts | 15 + ...iscontinue-employee-details.component.html | 33 +- ...iscontinue-employee-details.component.scss | 12 +- .../discontinue-employee-details.component.ts | 149 +---- .../src/app/reports/reports.module.ts | 4 +- .../DI/ServiceCollectionExtensions.cs | 1 + .../CommonInterface/VirtualGridController.cs | 97 +++ 25 files changed, 2134 insertions(+), 174 deletions(-) create mode 100644 DiscontinueEmployeeReport_DB_Changes.sql create mode 100644 HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs create mode 100644 HRM.BO/VirtualGrid/VirtualGrid.cs create mode 100644 HRM.DA/DA/VirtualGrid/VirtualGridDA.cs create mode 100644 HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs create mode 100644 HRM.DA/Service/VirtualGrid/README.md create mode 100644 HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs create mode 100644 HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs create mode 100644 HRM.DA/Service/VirtualGrid/VirtualGridService.cs create mode 100644 HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts create mode 100644 HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts create mode 100644 HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html create mode 100644 HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.scss create mode 100644 HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts create mode 100644 HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts create mode 100644 HRM.UI/Controllers/CommonInterface/VirtualGridController.cs diff --git a/DiscontinueEmployeeReport_DB_Changes.sql b/DiscontinueEmployeeReport_DB_Changes.sql new file mode 100644 index 0000000..6034db6 --- /dev/null +++ b/DiscontinueEmployeeReport_DB_Changes.sql @@ -0,0 +1,27 @@ +/* ============================================================================ + Discontinue Employee Details report - supporting index (OPTIONAL) + ---------------------------------------------------------------------------- + The report (Manage Employee > Report > Discontinue Employee Details, + DiscontinueEmployeeReportDA) filters dbo.EMPLOYEE by + + STATUS = 2 (Discontinued) AND PAYROLLTYPEID = @x + AND ENDOFCONTRACTDATE >= @FromDate AND ENDOFCONTRACTDATE < @ToDate+1 + + The report works without this index (no code depends on it). The index + lets SQL Server seek straight to the discontinued employees of one payroll + type in the date range instead of scanning every employee, which keeps the + count and each 100-row page fast as the EMPLOYEE table grows. + + Run order: any time, before or after the code deploys. Guarded, safe to + re-run. It only adds an index: no table, column or data is changed. + ============================================================================ */ + +IF NOT EXISTS (SELECT 1 FROM sys.indexes + WHERE object_id = OBJECT_ID('dbo.EMPLOYEE') + AND name = 'IX_Employee_Status_PayrollType_EndOfContract') +BEGIN + CREATE NONCLUSTERED INDEX IX_Employee_Status_PayrollType_EndOfContract + ON dbo.EMPLOYEE (STATUS, PAYROLLTYPEID, ENDOFCONTRACTDATE) + INCLUDE (DEPARTMENTID); +END; +GO diff --git a/HRM.BO/Employee/Employee.cs b/HRM.BO/Employee/Employee.cs index 4ddf416..1bd8cd2 100644 --- a/HRM.BO/Employee/Employee.cs +++ b/HRM.BO/Employee/Employee.cs @@ -606,6 +606,9 @@ namespace HRM.BO DataTable GetDFSL(); DataSet GetEmpDetailsView(string sEmpID); DataSet GetEmpDetailsView(); + // Discontinue Employee Details report: employees discontinued (Status = Discontinued) whose + // discontinue date (ENDOFCONTRACTDATE) falls inside the range, in one payroll type. + List GetDiscontinuedEmployeeDetails(DateTime fromDate, DateTime toDate, int payrollTypeID); int GetCurrentHeadCount(int departmentId, int designationId, int gradeId); List GetbyDepartment(int deptId); List NotYetAssigned(int payrollTypeID); diff --git a/HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs b/HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs new file mode 100644 index 0000000..ce6c286 --- /dev/null +++ b/HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs @@ -0,0 +1,14 @@ +using System; + +namespace HRM.BO.Report +{ + /// + /// One row of the Discontinue Employee Details report (Manage Employee › Report): the same fields as + /// Employee Details () plus the date the employee was discontinued + /// (Employee.ENDOFCONTRACTDATE). Loaded by IEmployeeService.GetDiscontinuedEmployeeDetails. + /// + public class DiscontinueEmployeeDetailInfo : EmployeeDetailInfo + { + public DateTime? DiscontinueDate { get; set; } + } +} diff --git a/HRM.BO/VirtualGrid/VirtualGrid.cs b/HRM.BO/VirtualGrid/VirtualGrid.cs new file mode 100644 index 0000000..d423bc5 --- /dev/null +++ b/HRM.BO/VirtualGrid/VirtualGrid.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; + +namespace HRM.BO +{ + /// + /// Common virtual-scrolling grid ("virtual grid") contracts. + /// + /// Every large list screen shares ONE BO / DA / Service / Controller: a screen is just a registered + /// grid definition (HRM.DA/Service/VirtualGrid/Definitions) identified by . + /// The server pages, sorts, searches and exports; the browser only ever holds one window of rows. + /// See HRM.DA/Service/VirtualGrid/README.md for how to add a grid. + /// + public enum EnumVirtualGridColumnType + { + Text = 1, + Number = 2, + Money = 3, + Date = 4, + DateTime = 5 + } + + /// A column as the screen sees it (from the layout endpoint). + public class VirtualGridColumnInfo + { + public string Field { get; set; } + public string Title { get; set; } + public int Width { get; set; } + public EnumVirtualGridColumnType Type { get; set; } + public bool Sortable { get; set; } + } + + /// Everything the screen needs to draw an empty grid before any data is requested. + public class VirtualGridLayout + { + public string GridKey { get; set; } + public string Title { get; set; } + public string DefaultSortField { get; set; } + public string DefaultSortDir { get; set; } + public int MaxPageSize { get; set; } + public List Columns { get; set; } = new List(); + } + + /// One window request: which grid, its filters, and the grid's skip/take, sort and search. + public class VirtualGridRequest + { + public string GridKey { get; set; } + + /// + /// Screen-specific filters (e.g. fromDate / toDate as "yyyy-MM-dd"). Each grid definition reads and + /// validates the ones it needs; values are never spliced into SQL. + /// + public Dictionary Filters { get; set; } = new Dictionary(); + + public int Skip { get; set; } + public int Take { get; set; } = 100; + public string Search { get; set; } + public string SortField { get; set; } + public string SortDir { get; set; } + + /// + /// True on Preview: reload the data instead of using the copy kept in server memory (list grids). + /// Scrolling, sorting and searching send false and are answered from that copy. + /// + public bool Refresh { get; set; } + } + + /// Excel export: the same request plus the grid's visible columns, in order. + public class VirtualGridExportRequest : VirtualGridRequest + { + /// Fields of the columns to export, in grid order. Empty = every column of the grid. + public List Fields { get; set; } = new List(); + } + + public class VirtualGridPage + { + /// Rows matching the filters and the search. + public int Total { get; set; } + + /// Rows matching the filters alone (equals Total when there is no search). + public int TotalWithoutSearch { get; set; } + + /// One row per entry, keyed by column field. Values are raw (dates, numbers); the screen formats them. + public List> Data { get; set; } = new List>(); + } + + /// A generated file (Excel export). + public class VirtualGridFile + { + public string FileName { get; set; } + public string ContentType { get; set; } + public byte[] Content { get; set; } + } + + /// The signed-in user, as the grid definitions need it for scoping (payroll type) and access. + public class VirtualGridContext + { + public int UserID { get; set; } + public int? EmployeeID { get; set; } + public int? PayrollTypeID { get; set; } + public EnumUserType UserType { get; set; } + } + + public interface IVirtualGridService + { + VirtualGridLayout GetLayout(string gridKey, VirtualGridContext context); + VirtualGridPage GetPage(VirtualGridRequest request, VirtualGridContext context); + VirtualGridFile ExportExcel(VirtualGridExportRequest request, VirtualGridContext context); + } +} diff --git a/HRM.DA/DA/Employee/EmployeeDA.cs b/HRM.DA/DA/Employee/EmployeeDA.cs index 6adbd52..4f290b5 100644 --- a/HRM.DA/DA/Employee/EmployeeDA.cs +++ b/HRM.DA/DA/Employee/EmployeeDA.cs @@ -1597,6 +1597,22 @@ namespace HRM.DA return oEmpDetails; } + // Discontinue Employee Details report: the Employee Details view (VW_EMPLOYEEBASIC) for employees + // discontinued between fromDate and toDate (inclusive), plus their department and discontinue date. + // The end is exclusive (toDate + 1 day) so a time part on ENDOFCONTRACTDATE never drops the last day. + internal static IDataReader GetDiscontinuedEmployeeDetails(TransactionContext tc, DateTime fromDate, + DateTime toDate, int payrollTypeID) + { + string sql = SQLParser.MakeSQL(@"SELECT v.*, e.DEPARTMENTID AS EmpDepartmentID, e.ENDOFCONTRACTDATE AS DiscontinueDate + FROM VW_EMPLOYEEBASIC v + INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = v.EmployeeId + WHERE e.STATUS = %n AND e.PAYROLLTYPEID = %n + AND e.ENDOFCONTRACTDATE >= %d AND e.ENDOFCONTRACTDATE < %d + ORDER BY v.EMPLOYEENO", + (int)EnumEmployeeStatus.Discontinued, payrollTypeID, fromDate.Date, toDate.Date.AddDays(1)); + return tc.ExecuteReader(sql); + } + internal static DataSet GetEmpDetailsView(TransactionContext tc) { DataSet oEmpDetails = new DataSet(); diff --git a/HRM.DA/DA/VirtualGrid/VirtualGridDA.cs b/HRM.DA/DA/VirtualGrid/VirtualGridDA.cs new file mode 100644 index 0000000..24fd9fb --- /dev/null +++ b/HRM.DA/DA/VirtualGrid/VirtualGridDA.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using Ease.Core.DataAccess; +using Ease.Core.DataAccess.SQL; +using HRM.BO; +using Microsoft.Data.SqlClient; + +namespace HRM.DA +{ + /// + /// Everything VirtualGridDA needs to build a grid's SQL. Built by VirtualGridService from a grid + /// definition; all SQL fragments in it are code-produced (definition expressions, whitelisted sort, + /// integer lists), and all user values are parameters. + /// + internal sealed class VirtualGridQuery + { + /// "sqlExpression AS [alias]" entries. + public List Select { get; } = new List(); + + public VirtualGridSource Source { get; set; } + + /// Columns searched with LIKE, with the type that decides how each is rendered as text. + public List> SearchColumns { get; } = + new List>(); + + /// Extra OR-branches of the search (code-produced, e.g. "e.DEPARTMENTID IN (1,2)"). + public List SearchPredicates { get; } = new List(); + + /// The escaped LIKE pattern ("%text%"), or null for no search. + public string SearchPattern { get; set; } + + /// Whitelisted sort expression including ASC/DESC. + public string OrderBy { get; set; } + + public string TieBreaker { get; set; } + } + + /// + /// The one data-access class behind every virtual grid: a COUNT and an OFFSET/FETCH window (or every + /// row, for Excel) over the grid's source, with search and sort done by SQL Server. + /// + internal static class VirtualGridDA + { + internal static int Count(TransactionContext tc, VirtualGridQuery query, bool withSearch) + { + string sql = "SELECT COUNT(*) " + query.Source.FromWhere + SearchClause(query, withSearch); + object value = tc.ExecuteScalar(CommandType.Text, sql, Parameters(query, withSearch, null, null)); + return value == null || value == DBNull.Value ? 0 : Convert.ToInt32(value); + } + + /// One window of rows; with null, every row (Excel export). + internal static IDataReader GetRows(TransactionContext tc, VirtualGridQuery query, int skip, int? take) + { + string sql = "SELECT " + string.Join(", ", query.Select) + " " + + query.Source.FromWhere + SearchClause(query, true) + + " ORDER BY " + query.OrderBy + ", " + query.TieBreaker + " ASC"; + if (take.HasValue) + sql += " OFFSET @__Skip ROWS FETCH NEXT @__Take ROWS ONLY"; + return tc.ExecuteReader(CommandType.Text, sql, Parameters(query, true, skip, take)); + } + + /// A column as the screen displays it, so the user can search what they see. + private static string AsDisplayText(string sql, EnumVirtualGridColumnType type) + { + switch (type) + { + case EnumVirtualGridColumnType.Date: + return "CONVERT(varchar(11), " + sql + ", 106)"; // 13 Feb 2025 + case EnumVirtualGridColumnType.DateTime: + return "(CONVERT(varchar(11), " + sql + ", 106) + ' ' + CONVERT(varchar(5), " + sql + ", 108))"; + case EnumVirtualGridColumnType.Money: + return "CONVERT(varchar(30), CAST(" + sql + " AS money), 1)"; // 1,234.56 + case EnumVirtualGridColumnType.Number: + return "CONVERT(varchar(40), " + sql + ")"; + default: + return sql; + } + } + + private static string SearchClause(VirtualGridQuery query, bool withSearch) + { + if (!withSearch || string.IsNullOrEmpty(query.SearchPattern)) + return string.Empty; + + List parts = query.SearchColumns + .Select(c => AsDisplayText(c.Key, c.Value) + @" LIKE @__Search ESCAPE '\'") + .Concat(query.SearchPredicates) + .ToList(); + return parts.Count == 0 ? " AND 1 = 0" : " AND (" + string.Join(" OR ", parts) + ")"; + } + + private static IDataParameter[] Parameters(VirtualGridQuery query, bool withSearch, int? skip, int? take) + { + // Clones: a SqlParameter cannot belong to two commands, and one query runs several. + var list = query.Source.Parameters + .Select(p => (SqlParameter)((ICloneable)p).Clone()) + .ToList(); + if (withSearch && !string.IsNullOrEmpty(query.SearchPattern)) + list.Add(SqlHelperExtension.CreateInParam("@__Search", SqlDbType.NVarChar, query.SearchPattern, 400)); + if (take.HasValue) + { + list.Add(SqlHelperExtension.CreateInParam("@__Skip", SqlDbType.Int, skip ?? 0)); + list.Add(SqlHelperExtension.CreateInParam("@__Take", SqlDbType.Int, take.Value)); + } + return list.ToArray(); + } + } +} diff --git a/HRM.DA/Service/Employee/EmployeeService.cs b/HRM.DA/Service/Employee/EmployeeService.cs index f0d72a9..cb05a7e 100644 --- a/HRM.DA/Service/Employee/EmployeeService.cs +++ b/HRM.DA/Service/Employee/EmployeeService.cs @@ -2730,6 +2730,160 @@ namespace HRM.DA return oEmpDetails; } + #region Discontinue Employee Details + + /// + /// Employees discontinued between fromDate and toDate (by discontinue date) in one payroll type, with + /// the same fields, null handling and department tiers as the Employee Details report. + /// One query for all rows; departments are loaded once and looked up by ID. + /// + public List GetDiscontinuedEmployeeDetails(DateTime fromDate, + DateTime toDate, int payrollTypeID) + { + var items = new List(); + Dictionary departments = new DepartmentService().GetAll() + .GroupBy(d => d.ID) + .ToDictionary(g => g.Key, g => g.First()); + + TransactionContext tc = null; + try + { + tc = TransactionContext.Begin(); + DataReader dr = new DataReader(EmployeeDA.GetDiscontinuedEmployeeDetails(tc, fromDate, toDate, payrollTypeID)); + while (dr.Read()) + items.Add(MapDiscontinuedEmployee(dr, departments)); + dr.Close(); + tc.End(); + } + catch (Exception e) + { + #region Handle Exception + if (tc != null) + tc.HandleError(); + ExceptionLog.Write(e); + throw new ServiceException(e.Message, e); + #endregion + } + + return items; + } + + private static HRM.BO.Report.DiscontinueEmployeeDetailInfo MapDiscontinuedEmployee(DataReader dr, + Dictionary departments) + { + var item = new HRM.BO.Report.DiscontinueEmployeeDetailInfo(); + item.EmployeeId = dr.GetInt32("EmployeeId") ?? 0; + item.EmployeeNo = dr.GetString("EMPLOYEENO"); + item.Name = dr.GetString("NAME"); + item.BasicSalary = dr.GetDecimal("BASICSALARY") ?? 0; + item.GrossSalary = dr.GetDecimal("GrossSalary") ?? 0; + item.TaxAmount = dr.GetDecimal("TAXAMOUNT") ?? 0; + item.FatherName = dr.GetString("Fathername"); + item.FatherOccupation = dr.GetString("FatherOccupation"); + item.MotherOccupation = dr.GetString("MotherOccupation"); + item.MotherName = dr.GetString("MotherName"); + item.Gender = dr.GetString("Gender"); + item.BirthDate = dr.GetDateTime("BIRTHDATE"); + item.BirthPlace = dr.GetString("BIRTHPlace"); + item.TinNo = dr.GetString("TINNO"); + item.JoiningDate = dr.GetDateTime("JOININGDATE"); + item.EmailAddress = dr.GetString("EMAILADDRESS"); + item.Category = dr.GetString("Category"); + item.Status = dr.GetString("Status"); + item.Location = dr.GetString("Location"); + item.Division = dr.GetString("Division"); + item.Company = dr.GetString("Company"); + item.Department = dr.GetString("Department"); + item.Section = dr.GetString("Section"); + item.Designation = dr.GetString("DESIGNATION"); + item.Grade = dr.GetString("GRADE"); + item.Religion = dr.GetString("RELIGION"); + item.MaritalStatus = dr.GetString("MaritalStatus"); + item.MobileNo = dr.GetString("MOBILENO"); + item.DateOfConfirmation = dr.GetDateTime("DATEOFCONFIRMATION"); + item.NationalId = dr.GetString("NATIONALID"); + item.PassportNo = dr.GetString("PassPortNo"); + item.PhotoPath = dr.GetString("PhotoPath"); + int? bloodGroup = dr.GetInt32("bloodgroup"); + item.BloodGroup = ((EnumBloodGroup)(bloodGroup ?? (int)EnumBloodGroup.None)).BloodGroupToFriendlyName(); + item.LineManagerNo = dr.GetString("LineManagerNo"); + item.LineManagerName = dr.GetString("LineManagerName"); + item.LineManagerEmail = dr.GetString("LineManagerEmail"); + item.LineManagerDes = dr.GetString("LineManagerDes"); + item.LmDivision = dr.GetString("lmDivision"); + item.LmDepartment = dr.GetString("lmDepartment"); + item.EmpStatus = dr.GetString("EmpStatus"); + item.Nationality = dr.GetString("NATIONALITY"); + item.OutPayBank = dr.GetString("OUTPayBank"); + item.OutPayBranch = dr.GetString("OUTPayBranch"); + item.OutPayAccountNo = dr.GetString("OutPayAccountNo"); + // Bank / Branch / Account No fall back to the out-pay values when empty, as Employee Details shows them. + string accountNo = dr.GetString("ACCOUNTNO"); + string bank = dr.GetString("Bank"); + string branch = dr.GetString("Branch"); + item.AccountNo = string.IsNullOrEmpty(accountNo) ? item.OutPayAccountNo : accountNo; + item.Bank = string.IsNullOrEmpty(bank) ? item.OutPayBank : bank; + item.Branch = string.IsNullOrEmpty(branch) ? item.OutPayBranch : branch; + item.DiscontinueDate = dr.GetDateTime("DiscontinueDate"); + + int? departmentID = dr.GetInt32("EmpDepartmentID"); + Department department = null; + if (departmentID.HasValue) + departments.TryGetValue(departmentID.Value, out department); + string[] tiers = GetDepartmentTierNames(department, departments); + if (tiers != null) + { + item.deptireOne = tiers[0]; + item.deptireTwo = tiers[1]; + item.deptireThree = tiers[2]; + item.deptireFour = tiers[3]; + item.deptireFive = tiers[4]; + } + return item; + } + + /// + /// The five department tier names (tier 1 = top), worked out exactly as the Employee Details report + /// (EmployeeController.GetEmpDetailInfo) does. Null when there is no department. + /// + private static string[] GetDepartmentTierNames(Department dept, Dictionary departments) + { + if (dept == null) + return null; + + Func find = id => + { + Department d = null; + if (id.HasValue) + departments.TryGetValue(id.Value, out d); + return d; + }; + + Department tire5 = null, tire4 = null, tire3 = null, tire2 = null, tire1 = null; + if (dept.ParentID != null) + { + for (int i = dept.Tier; i > 0; i--) + { + if (i == 5) tire5 = find(dept.ID); + if (i == 4) tire4 = tire5 != null ? find(tire5.ParentID) : find(dept.ID); + if (i == 3) tire3 = tire4 != null ? find(tire4.ParentID) : find(dept.ID); + if (i == 2) tire2 = tire3 != null ? find(tire3.ParentID) : find(dept.ID); + if (i == 1) tire1 = tire2 != null ? find(tire2.ParentID) : find(dept.ID); + } + } + + return new[] + { + tire1 == null ? "" : tire1.Name, + tire2 == null ? "" : tire2.Name, + tire3 == null ? "" : tire3.Name, + tire4 == null ? "" : tire4.Name, + tire5 == null ? "" : tire5.Name, + }; + } + + #endregion + public DataSet GetEmpDetailsView() { DataSet oEmpDetails = new DataSet(); diff --git a/HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs b/HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs new file mode 100644 index 0000000..c65bc77 --- /dev/null +++ b/HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using HRM.BO; +using HRM.BO.Report; + +namespace HRM.DA +{ + /// + /// Discontinue Employee Details (Manage Employee › Report, /reports/discontinue-employee-details): + /// employees discontinued inside a date range, with the same columns as Employee Details plus the + /// Discontinue Date. + /// + /// The data comes the usual way — EmployeeService.GetDiscontinuedEmployeeDetails returns a + /// List<DiscontinueEmployeeDetailInfo> (query in EmployeeDA) — and the common virtual grid pages it. + /// Filters: fromDate, toDate ("yyyy-MM-dd", both required). Scoped to the user's payroll type. + /// + internal sealed class DiscontinueEmployeeVirtualGrid : VirtualGridListDefinition + { + public const string GridKey = "discontinue-employee"; + + public override string Key => GridKey; + public override string Title => "Discontinue Employee Details"; + public override string DefaultSortField => "employeeNo"; + public override string ExcelFileName => "DiscontinueEmployeeDetails.xlsx"; + + /// Each Field is a property of DiscontinueEmployeeDetailInfo. + public override List GetColumns(VirtualGridContext context) + { + var columns = new List + { + new VirtualGridColumn { Field = "employeeNo", Title = "Employee No", Width = 110 }, + new VirtualGridColumn { Field = "name", Title = "Employee Name", Width = 220 }, + new VirtualGridColumn { Field = "gender", Title = "Gender", Width = 100 }, + new VirtualGridColumn { Field = "birthDate", Title = "Birth Date", Width = 120, Type = EnumVirtualGridColumnType.Date }, + new VirtualGridColumn { Field = "joiningDate", Title = "Joining Date", Width = 120, Type = EnumVirtualGridColumnType.Date }, + new VirtualGridColumn { Field = "discontinueDate", Title = "Discontinue Date", Width = 140, Type = EnumVirtualGridColumnType.Date }, + new VirtualGridColumn { Field = "mobileNo", Title = "Mobile No", Width = 130 }, + new VirtualGridColumn { Field = "emailAddress", Title = "Email Address", Width = 200 }, + new VirtualGridColumn { Field = "designation", Title = "Designation", Width = 160 }, + new VirtualGridColumn { Field = "grade", Title = "Grade", Width = 120 }, + new VirtualGridColumn { Field = "category", Title = "Category", Width = 120 }, + new VirtualGridColumn { Field = "status", Title = "Status", Width = 120 }, + }; + + // Department tier headings come from the Logic configuration, as in Employee Details. + string[] tierFields = { "deptireOne", "deptireTwo", "deptireThree", "deptireFour", "deptireFive" }; + List configs = new SystemConfigarationService() + .GetAllChildrenByParentNode(EnumConfigurationType.Logic, "department") ?? new List(); + int tier = 0; + foreach (SystemConfigaration c in configs) + { + if (!string.Equals(c.Node, "text", StringComparison.OrdinalIgnoreCase)) + continue; + if (tier < tierFields.Length) + columns.Add(new VirtualGridColumn { Field = tierFields[tier], Title = c.Value, Width = 150 }); + tier++; + } + + columns.AddRange(new[] + { + new VirtualGridColumn { Field = "location", Title = "Location", Width = 130 }, + new VirtualGridColumn { Field = "religion", Title = "Religion", Width = 110 }, + new VirtualGridColumn { Field = "maritalStatus", Title = "Marital Status", Width = 120 }, + new VirtualGridColumn { Field = "dateOfConfirmation", Title = "Confirmation", Width = 120, Type = EnumVirtualGridColumnType.Date }, + new VirtualGridColumn { Field = "basicSalary", Title = "Basic Salary", Width = 120, Type = EnumVirtualGridColumnType.Money }, + new VirtualGridColumn { Field = "grossSalary", Title = "Gross Salary", Width = 120, Type = EnumVirtualGridColumnType.Money }, + new VirtualGridColumn { Field = "passportNo", Title = "Passport No", Width = 120 }, + new VirtualGridColumn { Field = "bloodGroup", Title = "Blood Group", Width = 110 }, + new VirtualGridColumn { Field = "lineManagerNo", Title = "LineManager No", Width = 130 }, + new VirtualGridColumn { Field = "lineManagerName", Title = "LineManager Name", Width = 180 }, + new VirtualGridColumn { Field = "lineManagerEmail", Title = "LineManager Email", Width = 200 }, + new VirtualGridColumn { Field = "empStatus", Title = "Employee Status", Width = 130 }, + new VirtualGridColumn { Field = "bank", Title = "Bank", Width = 150 }, + new VirtualGridColumn { Field = "branch", Title = "Branch", Width = 150 }, + new VirtualGridColumn { Field = "accountNo", Title = "Account No", Width = 150 }, + new VirtualGridColumn { Field = "nationalId", Title = "National ID/NID", Width = 150 }, + new VirtualGridColumn { Field = "tinNo", Title = "TIN", Width = 120 }, + }); + return columns; + } + + protected override List LoadData(VirtualGridFilters filters, VirtualGridContext context) + { + DateTime fromDate = filters.GetDate("fromDate", "From Date", true).Value; + DateTime toDate = filters.GetDate("toDate", "To Date", true).Value; + if (fromDate > toDate) + throw new ArgumentException("From Date cannot be after To Date."); + if (!context.PayrollTypeID.HasValue) + throw new ArgumentException("No payroll type is selected for your login."); + + return new EmployeeService().GetDiscontinuedEmployeeDetails(fromDate, toDate, context.PayrollTypeID.Value); + } + + public override string DescribeFilters(VirtualGridFilters filters) + { + DateTime? fromDate = filters.GetDate("fromDate", "From Date", false); + DateTime? toDate = filters.GetDate("toDate", "To Date", false); + return "Discontinued from " + fromDate?.ToString("dd MMM yyyy", CultureInfo.InvariantCulture) + + " to " + toDate?.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); + } + } +} diff --git a/HRM.DA/Service/VirtualGrid/README.md b/HRM.DA/Service/VirtualGrid/README.md new file mode 100644 index 0000000..0ad94e6 --- /dev/null +++ b/HRM.DA/Service/VirtualGrid/README.md @@ -0,0 +1,88 @@ +# Virtual grid — common virtual-scrolling grid for large lists + +One common controller, service and Angular component serve every virtual-scrolling list. The data +still comes **the usual way**: a service method returns a `List` (query in the module's DA). A grid +only says *which* service method to call and *which* columns to show. + +| Layer | File | Role | +|---|---|---| +| BO | `HRM.BO/VirtualGrid/VirtualGrid.cs` | Request/page/layout DTOs, `EnumVirtualGridColumnType`, `IVirtualGridService` | +| Service | `HRM.DA/Service/VirtualGrid/VirtualGridService.cs` | Access check, keeps the loaded list in memory, search/sort/page, Excel | +| Grids | `HRM.DA/Service/VirtualGrid/Definitions/*.cs` | **One small class per grid**: columns + which service to call | +| Registry | `HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs` | Key → grid. Unlisted keys cannot be queried | +| Controller | `HRM.UI/Controllers/CommonInterface/VirtualGridController.cs` | `GET api/virtualGrid/layout/{key}`, `POST page`, `POST export-excel` | +| Angular | `custom-components/virtual-grid/` (`VirtualGridModule`, ``) | Virtual scrolling, search box, sort, total, Excel button | + +## How it works + +1. Screen opens → the component asks for the grid's columns (`layout`). +2. **Preview** → `grid.load(filters)` → `page` with `refresh: true` → the grid's `LoadData` calls the + service (e.g. `EmployeeService.GetDiscontinuedEmployeeDetails`) → the whole `List` is kept in + server memory (per user + filters, 10 min sliding / 30 min max) → the first 100 rows go back. +3. **Scroll / sort / search** → `page` with `refresh: false` → answered from the kept list: search, + sort, `Skip/Take` 100. The service is not called again. +4. **Export to Excel** → every matching row of the kept list, same order, as `.xlsx`. + +The browser only ever holds ~100 rows, however long the list is. + +## Adding a grid (existing screen with a service that already returns a list) + +1. **Grid class** — `Definitions/VirtualGrid.cs`: + ```csharp + internal sealed class LeaveHistoryVirtualGrid : VirtualGridListDefinition + { + public const string GridKey = "leave-history"; + public override string Key => GridKey; + public override string Title => "Leave History"; + + public override List GetColumns(VirtualGridContext context) => new List + { + // Field = a property of LeaveEntry (camelCase) + new VirtualGridColumn { Field = "employeeNo", Title = "Employee No", Width = 110 }, + new VirtualGridColumn { Field = "fromDate", Title = "From", Width = 120, Type = EnumVirtualGridColumnType.Date }, + new VirtualGridColumn { Field = "amount", Title = "Amount", Width = 120, Type = EnumVirtualGridColumnType.Money }, + }; + + protected override List LoadData(VirtualGridFilters filters, VirtualGridContext context) + { + DateTime from = filters.GetDate("fromDate", "From Date", true).Value; // user-facing errors + DateTime to = filters.GetDate("toDate", "To Date", true).Value; + return new LeaveEntryService().Get(from, to, context.PayrollTypeID.Value); // the existing service + } + } + ``` +2. **Registry** — `{ LeaveHistoryVirtualGrid.GridKey, () => new LeaveHistoryVirtualGrid() }`. +3. **Screen** — import `VirtualGridModule`, then: + ```html + + ``` + ```ts + this.grid.load({ fromDate: '2025-01-01', toDate: '2025-12-31' }); // filters as strings + ``` + +No new controller, Angular service or model. For a brand-new screen, write its query in the module's +DA and a service method returning `List` (as always), then steps 1–3. + +Worked example: `Definitions/DiscontinueEmployeeVirtualGrid.cs` → +`EmployeeService.GetDiscontinuedEmployeeDetails` → `EmployeeDA.GetDiscontinuedEmployeeDetails`. + +## Column types + +`Text` (default), `Number`, `Money` (`1,234.56`), `Date` (`dd MMM yyyy`), `DateTime` (`dd MMM yyyy HH:mm`). +The screen shows values in that format and the search box searches that same text, so users find what +they see. Set `Sortable = false` / `Searchable = false` on a column to turn either off. + +## Very large lists: `VirtualGridSqlDefinition` (advanced) + +A list grid loads the whole list once per Preview — fine up to tens of thousands of rows. When even +that is too slow, derive from `VirtualGridSqlDefinition` instead: give each column a `Sql` expression +and a `BuildSource` returning `FROM … WHERE …` with parameters, and SQL Server pages each request +(`OFFSET/FETCH`, `HRM.DA/DA/VirtualGrid/VirtualGridDA.cs`). User values reach SQL only as parameters; +spliced text is code-produced only. + +## Rules + +- A service a grid calls must not run one query per row; load lookups once (see `GetDiscontinuedEmployeeDetails`). +- Self-service (ESS) logins are refused unless the grid sets `AllowSelfServiceUsers`. Authorization in + this app is menu-driven, so add any further check a grid needs in `LoadData`. +- The kept list is per user, payroll type, grid and filter values; Preview always reloads it. diff --git a/HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs b/HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs new file mode 100644 index 0000000..02f8de1 --- /dev/null +++ b/HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs @@ -0,0 +1,271 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Text.RegularExpressions; +using HRM.BO; +using Microsoft.Data.SqlClient; + +namespace HRM.DA +{ + /// + /// One column of a virtual grid. Field is the name the screen uses. In a list grid it is a property + /// of the business object; in a SQL grid it is the alias of Sql, the expression the column selects, + /// sorts and searches by (null = filled in code, not sorted or searched by SQL). + /// + internal sealed class VirtualGridColumn + { + public string Field { get; set; } + public string Title { get; set; } + public int Width { get; set; } = 130; + public EnumVirtualGridColumnType Type { get; set; } = EnumVirtualGridColumnType.Text; + public string Sql { get; set; } + public bool Sortable { get; set; } = true; + public bool Searchable { get; set; } = true; + + } + + /// + /// The rows a grid is built on: a "FROM ... WHERE ..." clause plus its parameters. + /// Filter values must only ever reach SQL through . + /// + internal sealed class VirtualGridSource + { + public string FromWhere { get; set; } + public List Parameters { get; } = new List(); + + /// Human-readable filter summary, printed at the top of the Excel file. + public string FilterDescription { get; set; } + } + + /// + /// Extra search conditions a definition adds for values that are worked out in code (e.g. department + /// tier names): each becomes one more OR branch of the search. Only code-produced SQL goes here. + /// + internal sealed class VirtualGridSearchExtras + { + internal List Predicates { get; } = new List(); + + /// Adds "sqlExpression IN (v1, v2, …)" — the values are integers, so they are safe to splice. + public void AddIntIn(string sqlExpression, IEnumerable values) + { + List list = (values ?? Enumerable.Empty()).Distinct().ToList(); + if (list.Count > 0) + Predicates.Add(sqlExpression + " IN (" + string.Join(",", list.Select(v => v.ToString(CultureInfo.InvariantCulture))) + ")"); + } + } + + /// + /// Typed, validated access to . Every problem is an + /// ArgumentException with a message fit to show the user (the controller returns it as 400). + /// + internal sealed class VirtualGridFilters + { + private readonly Dictionary _values; + + public VirtualGridFilters(Dictionary values) + { + _values = new Dictionary(values ?? new Dictionary(), StringComparer.OrdinalIgnoreCase); + } + + public string GetString(string name) + { + string value; + return _values.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null; + } + + /// A "yyyy-MM-dd" date. is the name the user sees ("From Date"). + public DateTime? GetDate(string name, string label, bool required) + { + string text = GetString(name); + if (text == null) + { + if (required) + throw new ArgumentException("Please select " + label + "."); + return null; + } + DateTime value; + if (!DateTime.TryParseExact(text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out value)) + throw new ArgumentException("Please select a valid " + label + "."); + return value; + } + + public int? GetInt(string name, string label, bool required) + { + string text = GetString(name); + if (text == null) + { + if (required) + throw new ArgumentException("Please select " + label + "."); + return null; + } + int value; + if (!int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) + throw new ArgumentException("Please select a valid " + label + "."); + return value; + } + } + + /// + /// Base class of every virtual grid: what the grid is called, its columns and its limits. + /// A grid derives from one of the two kinds below, never from this class directly: + /// + /// - VirtualGridListDefinition<T> (the usual choice): LoadData calls an ordinary service method + /// that returns a List of business objects, exactly as screens always get their data. The common + /// service keeps that list in memory and pages, sorts, searches and exports it on the server. + /// - VirtualGridSqlDefinition (for very large lists): the grid gives a FROM/WHERE and SQL Server + /// does the paging, so only one window of rows is ever read. + /// + /// A new instance is created for every call, so a definition may keep per-call state. + /// + internal abstract class VirtualGridDefinition + { + private static readonly Regex Identifier = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); + + /// The key the screen sends (e.g. "discontinue-employee"). + public abstract string Key { get; } + + public abstract string Title { get; } + + public virtual string DefaultSortField => null; // null = first sortable column + public virtual string DefaultSortDir => "asc"; + + /// Self-service (ESS) logins are refused unless a grid opts in. + public virtual bool AllowSelfServiceUsers => false; + + public virtual int MaxPageSize => 500; + + /// Largest Excel export allowed; past it the user is asked to narrow the filters. + public virtual int ExportRowLimit => 200000; + + public virtual string ExcelFileName => Regex.Replace(Title, "[^A-Za-z0-9]+", "") + ".xlsx"; + + /// The grid's columns, in display order. May depend on configuration (e.g. department tier names). + public abstract List GetColumns(VirtualGridContext context); + + /// Whether the user can sort by this column. + internal virtual bool CanSort(VirtualGridColumn column) => column.Sortable; + + /// Whether the search box looks in this column. + internal virtual bool CanSearch(VirtualGridColumn column) => column.Searchable; + + internal static bool IsIdentifier(string name) => name != null && Identifier.IsMatch(name); + } + + /// + /// A grid filled from an ordinary service method that returns a list of business objects, the way + /// screens have always loaded their data: + /// + /// protected override List<LeaveEntry> LoadData(VirtualGridFilters filters, VirtualGridContext context) + /// => new LeaveEntryService().Get(...); + /// + /// Each column's Field is the name of a property of T (camelCase, matched case-insensitively); + /// Sql is not used. + /// + /// The list is loaded on Preview and kept in server memory for a few minutes, so scrolling, sorting + /// and searching do not call the service again. Suits lists up to tens of thousands of rows; for + /// larger ones use VirtualGridSqlDefinition. + /// + internal abstract class VirtualGridListDefinition : VirtualGridDefinition, IVirtualGridListSource where T : class + { + /// Reads/validates the filters and returns the rows, usually with one call to an existing service. + protected abstract List LoadData(VirtualGridFilters filters, VirtualGridContext context); + + /// Human-readable filter summary, printed at the top of the Excel file. Optional. + public virtual string DescribeFilters(VirtualGridFilters filters) => null; + + /// Calls LoadData and turns each object into a row holding just the grid's columns. + List> IVirtualGridListSource.LoadRows( + VirtualGridFilters filters, VirtualGridContext context, List columns) + { + var properties = new List>(); + foreach (VirtualGridColumn c in columns) + { + PropertyInfo property = FindProperty(c.Field); + if (property == null || !property.CanRead) + throw new InvalidOperationException("Virtual grid '" + Key + "': " + typeof(T).Name + + " has no readable property for column '" + c.Field + "'."); + properties.Add(new KeyValuePair(c.Field, property)); + } + + List items = LoadData(filters, context) ?? new List(); + var rows = new List>(items.Count); + foreach (T item in items) + { + if (item == null) + continue; + var row = new Dictionary(properties.Count, StringComparer.OrdinalIgnoreCase); + foreach (KeyValuePair p in properties) + row[p.Key] = p.Value.GetValue(item); + rows.Add(row); + } + return rows; + } + + /// + /// The public property named (case-insensitive). When a class hides a base + /// property with "new" (e.g. EmployeeDetailInfo.Status over the base Status), the most derived one — + /// the one the business object itself shows — wins. + /// + private static PropertyInfo FindProperty(string field) + { + PropertyInfo best = null; + int bestDepth = int.MaxValue; + foreach (PropertyInfo p in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)) + { + if (!string.Equals(p.Name, field, StringComparison.OrdinalIgnoreCase) || p.GetIndexParameters().Length > 0) + continue; + int depth = 0; + for (Type t = typeof(T); t != null && t != p.DeclaringType; t = t.BaseType) + depth++; + if (depth < bestDepth) + { + best = p; + bestDepth = depth; + } + } + return best; + } + } + + /// Non-generic view of a list grid, used by VirtualGridService. + internal interface IVirtualGridListSource + { + List> LoadRows(VirtualGridFilters filters, VirtualGridContext context, List columns); + string DescribeFilters(VirtualGridFilters filters); + } + + /// + /// A grid whose paging, sorting and searching run in SQL Server: for very large lists, where loading + /// every row (even once) is too slow. Each column's Sql is the expression it selects, sorts and searches + /// by; a column with Sql = null is filled in OnRowsLoaded. + /// + internal abstract class VirtualGridSqlDefinition : VirtualGridDefinition + { + /// A unique, stable SQL expression (usually the primary key) used as the last sort key so pages never overlap. + public abstract string TieBreakerSql { get; } + + /// + /// Extra values selected for OnRowsLoaded only, as "sqlExpression AS _alias". + /// Aliases must start with '_'; they are removed from the rows before they leave the server. + /// + public virtual IEnumerable HiddenSelect => Enumerable.Empty(); + + /// Reads/validates the filters and returns the FROM/WHERE the grid is built on. + public abstract VirtualGridSource BuildSource(VirtualGridFilters filters, VirtualGridContext context); + + /// Adds OR-conditions for code-computed values matching the search text. Optional. + public virtual void AddSearchExtras(string term, VirtualGridSearchExtras extras, VirtualGridContext context) + { + } + + /// Fills code-computed columns (and reformats values) on one loaded window of rows. Optional. + public virtual void OnRowsLoaded(List> rows, VirtualGridContext context) + { + } + + internal override bool CanSort(VirtualGridColumn column) => column.Sortable && !string.IsNullOrEmpty(column.Sql); + internal override bool CanSearch(VirtualGridColumn column) => column.Searchable && !string.IsNullOrEmpty(column.Sql); + } +} diff --git a/HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs b/HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs new file mode 100644 index 0000000..e1357d4 --- /dev/null +++ b/HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; + +namespace HRM.DA +{ + /// + /// Every virtual grid the application serves, by key. Adding a grid = one definition class in + /// Definitions/ and one line here. A key that is not listed here cannot be queried. + /// + internal static class VirtualGridRegistry + { + private static readonly Dictionary> Grids = + new Dictionary>(StringComparer.OrdinalIgnoreCase) + { + { DiscontinueEmployeeVirtualGrid.GridKey, () => new DiscontinueEmployeeVirtualGrid() }, + }; + + /// A fresh definition instance for one call. Throws ArgumentException for an unknown key. + internal static VirtualGridDefinition Create(string gridKey) + { + Func factory; + if (string.IsNullOrWhiteSpace(gridKey) || !Grids.TryGetValue(gridKey.Trim(), out factory)) + throw new ArgumentException("Unknown grid: " + gridKey); + return factory(); + } + } +} diff --git a/HRM.DA/Service/VirtualGrid/VirtualGridService.cs b/HRM.DA/Service/VirtualGrid/VirtualGridService.cs new file mode 100644 index 0000000..a5a3863 --- /dev/null +++ b/HRM.DA/Service/VirtualGrid/VirtualGridService.cs @@ -0,0 +1,591 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using ClosedXML.Excel; +using Ease.Core.DataAccess; +using Ease.Core.Model; +using Ease.Core.Utility; +using HRM.BO; +using Microsoft.Extensions.Caching.Memory; + +namespace HRM.DA +{ + /// + /// The one service behind every virtual-scrolling grid (api/virtualGrid). It looks the grid up in + /// and serves it in one of two ways: + /// + /// - List grid (): the grid's existing service returns a + /// List of business objects; that list is kept in server memory for a few minutes and every scroll, + /// sort, search and Excel export is answered from it. + /// - SQL grid (): SQL Server counts, sorts, searches and returns + /// one OFFSET/FETCH window per request (VirtualGridDA). + /// + /// Shared work for both: access check, column validation, paging limits, sort whitelist, the Excel file. + /// + public class VirtualGridService : ServiceTemplate, IVirtualGridService + { + private const int ExportCommandTimeoutSeconds = 600; + private const int MaxSearchLength = 200; + private const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + + // List grids: loaded lists, per user + grid + filters. Size = rows, so at most ~1,000,000 rows are + // held in total; the least recently used lists are dropped first. + private static readonly MemoryCache ListCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000000 }); + private static readonly TimeSpan ListCacheSliding = TimeSpan.FromMinutes(10); + private static readonly TimeSpan ListCacheAbsolute = TimeSpan.FromMinutes(30); + + #region Public API + + public VirtualGridLayout GetLayout(string gridKey, VirtualGridContext context) + { + VirtualGridDefinition grid = Open(gridKey, context); + List columns = GetValidatedColumns(grid, context); + VirtualGridColumn defaultSort = DefaultSortColumn(grid, columns); + + var layout = new VirtualGridLayout + { + GridKey = grid.Key, + Title = grid.Title, + DefaultSortField = defaultSort?.Field, + DefaultSortDir = NormalizeDir(grid.DefaultSortDir), + MaxPageSize = grid.MaxPageSize, + }; + layout.Columns.AddRange(columns.Select(c => new VirtualGridColumnInfo + { + Field = c.Field, + Title = c.Title, + Width = c.Width, + Type = c.Type, + Sortable = grid.CanSort(c), + })); + return layout; + } + + public VirtualGridPage GetPage(VirtualGridRequest request, VirtualGridContext context) + { + if (request == null) + throw new ArgumentException("Invalid request."); + VirtualGridDefinition grid = Open(request.GridKey, context); + List columns = GetValidatedColumns(grid, context); + int skip = Math.Max(0, request.Skip); + int take = Math.Min(Math.Max(1, request.Take), grid.MaxPageSize); + + var listSource = grid as IVirtualGridListSource; + if (listSource != null) + { + CachedList list = GetList(grid, listSource, columns, request, context); + List matches = Search(list, request.Search); + List ordered = Sort(grid, columns, list, matches, request); + return new VirtualGridPage + { + Total = ordered.Count, + TotalWithoutSearch = list.Rows.Count, + Data = ordered.Skip(skip).Take(take).Select(i => list.Rows[i]).ToList(), + }; + } + + var sqlGrid = (VirtualGridSqlDefinition)grid; + VirtualGridQuery query = BuildQuery(sqlGrid, columns, request, context); + var page = new VirtualGridPage(); + TransactionContext tc = null; + try + { + tc = TransactionContext.Begin(); + page.Total = VirtualGridDA.Count(tc, query, true); + page.TotalWithoutSearch = query.SearchPattern == null ? page.Total : VirtualGridDA.Count(tc, query, false); + if (page.Total > skip) + page.Data = ReadRows(VirtualGridDA.GetRows(tc, query, skip, take)); + tc.End(); + } + catch (Exception e) + { + #region Handle Exception + if (tc != null) + tc.HandleError(); + ExceptionLog.Write(e); + throw new ServiceException(e.Message, e); + #endregion + } + + // After the transaction: definitions may call other services that open their own. + FinishRows(sqlGrid, page.Data, context); + return page; + } + + public VirtualGridFile ExportExcel(VirtualGridExportRequest request, VirtualGridContext context) + { + if (request == null) + throw new ArgumentException("Invalid request."); + VirtualGridDefinition grid = Open(request.GridKey, context); + List columns = GetValidatedColumns(grid, context); + List exportColumns = ResolveExportColumns(columns, request.Fields); + + List> rows; + string filterDescription; + + var listSource = grid as IVirtualGridListSource; + if (listSource != null) + { + CachedList list = GetList(grid, listSource, columns, request, context); + List ordered = Sort(grid, columns, list, Search(list, request.Search), request); + CheckExportLimit(grid, ordered.Count); + rows = ordered.Select(i => list.Rows[i]).ToList(); + filterDescription = listSource.DescribeFilters(new VirtualGridFilters(request.Filters)); + } + else + { + var sqlGrid = (VirtualGridSqlDefinition)grid; + VirtualGridQuery query = BuildQuery(sqlGrid, columns, request, context); + TransactionContext tc = null; + try + { + tc = TransactionContext.Begin(); + tc.CommandTimeOut = ExportCommandTimeoutSeconds; + CheckExportLimit(grid, VirtualGridDA.Count(tc, query, true)); + rows = ReadRows(VirtualGridDA.GetRows(tc, query, 0, null)); + tc.End(); + } + catch (ArgumentException) + { + if (tc != null) + tc.HandleError(); + throw; + } + catch (Exception e) + { + #region Handle Exception + if (tc != null) + tc.HandleError(); + ExceptionLog.Write(e); + throw new ServiceException(e.Message, e); + #endregion + } + FinishRows(sqlGrid, rows, context); + filterDescription = query.Source.FilterDescription; + } + + return new VirtualGridFile + { + FileName = grid.ExcelFileName, + ContentType = ExcelContentType, + Content = BuildWorkbook(grid, exportColumns, rows, filterDescription, request.Search), + }; + } + + #endregion + + #region Shared helpers + + /// Finds the grid and checks the user may use it. Unknown key → ArgumentException. + private static VirtualGridDefinition Open(string gridKey, VirtualGridContext context) + { + if (context == null) + throw new UnauthorizedAccessException("Not signed in."); + VirtualGridDefinition grid = VirtualGridRegistry.Create(gridKey); + if (!(grid is IVirtualGridListSource) && !(grid is VirtualGridSqlDefinition)) + throw new InvalidOperationException("Virtual grid '" + grid.Key + + "' must derive from VirtualGridListDefinition or VirtualGridSqlDefinition."); + if (context.UserType == EnumUserType.Employee && !grid.AllowSelfServiceUsers) + throw new UnauthorizedAccessException("You do not have access to " + grid.Title + "."); + return grid; + } + + /// The definition's columns, checked so a bad definition fails loudly. + private static List GetValidatedColumns(VirtualGridDefinition grid, VirtualGridContext context) + { + List columns = grid.GetColumns(context) ?? new List(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (VirtualGridColumn c in columns) + { + if (!VirtualGridDefinition.IsIdentifier(c.Field) || c.Field.StartsWith("_") || !seen.Add(c.Field)) + throw new InvalidOperationException("Virtual grid '" + grid.Key + "' has an invalid or duplicate column field: " + c.Field); + } + if (columns.Count == 0) + throw new InvalidOperationException("Virtual grid '" + grid.Key + "' has no columns."); + return columns; + } + + private static VirtualGridColumn DefaultSortColumn(VirtualGridDefinition grid, List columns) + { + VirtualGridColumn byName = columns.FirstOrDefault(c => grid.CanSort(c) + && string.Equals(c.Field, grid.DefaultSortField, StringComparison.OrdinalIgnoreCase)); + return byName ?? columns.FirstOrDefault(c => grid.CanSort(c)); + } + + /// The column to sort by and its direction: a sortable column the user picked, else the grid's default. + private static VirtualGridColumn ResolveSort(VirtualGridDefinition grid, List columns, + VirtualGridRequest request, out bool descending) + { + VirtualGridColumn picked = columns.FirstOrDefault(c => grid.CanSort(c) + && string.Equals(c.Field, request.SortField, StringComparison.OrdinalIgnoreCase)); + descending = NormalizeDir(picked != null ? request.SortDir : grid.DefaultSortDir) == "desc"; + return picked ?? DefaultSortColumn(grid, columns); + } + + private static string NormalizeDir(string dir) => + string.Equals(dir, "desc", StringComparison.OrdinalIgnoreCase) ? "desc" : "asc"; + + private static string CleanSearch(string search) + { + string term = (search ?? string.Empty).Trim(); + return term.Length > MaxSearchLength ? term.Substring(0, MaxSearchLength) : term; + } + + private static void CheckExportLimit(VirtualGridDefinition grid, int total) + { + if (total > grid.ExportRowLimit) + throw new ArgumentException("There are " + total.ToString("N0", CultureInfo.InvariantCulture) + + " rows; Excel export is limited to " + grid.ExportRowLimit.ToString("N0", CultureInfo.InvariantCulture) + + ". Please narrow the filters or the search."); + } + + /// + /// A value as the screen shows it (dates "dd MMM yyyy", money "1,234.56"), so the search box finds + /// what the user sees. Mirrors the formatting in <app-virtual-grid>. + /// + private static string DisplayText(object value, EnumVirtualGridColumnType type) + { + if (value == null) + return string.Empty; + switch (type) + { + case EnumVirtualGridColumnType.Date: + if (value is DateTime d) return d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); + break; + case EnumVirtualGridColumnType.DateTime: + if (value is DateTime dt) return dt.ToString("dd MMM yyyy HH:mm", CultureInfo.InvariantCulture); + break; + case EnumVirtualGridColumnType.Money: + if (value is IConvertible && !(value is string)) + return Convert.ToDecimal(value, CultureInfo.InvariantCulture).ToString("N2", CultureInfo.InvariantCulture); + break; + } + return Convert.ToString(value, CultureInfo.InvariantCulture); + } + + #endregion + + #region List grids (existing service + in-memory paging) + + /// A loaded list plus, per row, the text the search box looks in. + private sealed class CachedList + { + public List> Rows; + public List SearchText; + } + + /// + /// The grid's list for this user and these filters: from the cache, or — on Preview (Refresh) or + /// when the cache has expired — freshly from the grid's service. + /// + private static CachedList GetList(VirtualGridDefinition grid, IVirtualGridListSource source, + List columns, VirtualGridRequest request, VirtualGridContext context) + { + string key = CacheKey(grid, columns, request, context); + CachedList list; + if (!request.Refresh && ListCache.TryGetValue(key, out list)) + return list; + + List> rows = source.LoadRows(new VirtualGridFilters(request.Filters), context, columns); + + List searchable = columns.Where(c => grid.CanSearch(c)).ToList(); + var searchText = new List(rows.Count); + var sb = new StringBuilder(); + foreach (Dictionary row in rows) + { + sb.Clear(); + foreach (VirtualGridColumn c in searchable) + { + object value; + row.TryGetValue(c.Field, out value); + sb.Append(DisplayText(value, c.Type)).Append('\u0001'); // separator: no match across two columns + } + searchText.Add(sb.ToString()); + } + + list = new CachedList { Rows = rows, SearchText = searchText }; + ListCache.Set(key, list, new MemoryCacheEntryOptions + { + Size = Math.Max(1, rows.Count), + SlidingExpiration = ListCacheSliding, + AbsoluteExpirationRelativeToNow = ListCacheAbsolute, + }); + return list; + } + + /// One list per user, payroll type, grid and filter values (and column set, in case configuration changes). + private static string CacheKey(VirtualGridDefinition grid, List columns, + VirtualGridRequest request, VirtualGridContext context) + { + var sb = new StringBuilder(); + sb.Append(grid.Key.ToLowerInvariant()) + .Append('|').Append(context.UserID) + .Append('|').Append(context.PayrollTypeID) + .Append('|').Append((int)context.UserType) + .Append('|').Append(string.Join(",", columns.Select(c => c.Field))); + foreach (KeyValuePair f in (request.Filters ?? new Dictionary()) + .OrderBy(f => f.Key, StringComparer.OrdinalIgnoreCase)) + sb.Append('|').Append(f.Key.ToLowerInvariant()).Append('=').Append(f.Value); + return sb.ToString(); + } + + /// Indexes of the rows whose shown text contains the search (case-insensitive); all rows when there is none. + private static List Search(CachedList list, string search) + { + string term = CleanSearch(search); + var result = new List(term.Length == 0 ? list.Rows.Count : 64); + for (int i = 0; i < list.Rows.Count; i++) + { + if (term.Length == 0 || list.SearchText[i].IndexOf(term, StringComparison.OrdinalIgnoreCase) >= 0) + result.Add(i); + } + return result; + } + + /// Sorts row indexes by the chosen column (stable, so equal values keep the service's order). + private static List Sort(VirtualGridDefinition grid, List columns, CachedList list, + List indexes, VirtualGridRequest request) + { + bool descending; + VirtualGridColumn sortColumn = ResolveSort(grid, columns, request, out descending); + if (sortColumn == null) + return indexes; + + string field = sortColumn.Field; + Func keyOf = i => + { + object value; + list.Rows[i].TryGetValue(field, out value); + return value; + }; + return (descending + ? indexes.OrderByDescending(keyOf, ValueComparer.Instance) + : indexes.OrderBy(keyOf, ValueComparer.Instance)) + .ToList(); + } + + /// Compares cell values: empty first, numbers as numbers, dates as dates, text case-insensitively. + private sealed class ValueComparer : IComparer + { + public static readonly ValueComparer Instance = new ValueComparer(); + + public int Compare(object x, object y) + { + bool xEmpty = x == null || (x is string xs && xs.Length == 0); + bool yEmpty = y == null || (y is string ys && ys.Length == 0); + if (xEmpty || yEmpty) + return xEmpty == yEmpty ? 0 : (xEmpty ? -1 : 1); + + if (x is string a && y is string b) + return StringComparer.InvariantCultureIgnoreCase.Compare(a, b); + if (IsNumber(x) && IsNumber(y)) + return Convert.ToDecimal(x, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(y, CultureInfo.InvariantCulture)); + if (x.GetType() == y.GetType() && x is IComparable comparable) + return comparable.CompareTo(y); + return StringComparer.InvariantCultureIgnoreCase.Compare( + Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture)); + } + + private static bool IsNumber(object v) => + v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; + } + + #endregion + + #region SQL grids (VirtualGridDA) + + private static VirtualGridQuery BuildQuery(VirtualGridSqlDefinition grid, List columns, + VirtualGridRequest request, VirtualGridContext context) + { + VirtualGridSource source = grid.BuildSource(new VirtualGridFilters(request.Filters), context); + if (source == null || string.IsNullOrWhiteSpace(source.FromWhere)) + throw new InvalidOperationException("Virtual grid '" + grid.Key + "' returned no source."); + + var query = new VirtualGridQuery { Source = source, TieBreaker = grid.TieBreakerSql }; + + foreach (VirtualGridColumn c in columns.Where(c => !string.IsNullOrEmpty(c.Sql))) + query.Select.Add(c.Sql + " AS [" + c.Field + "]"); + foreach (string hidden in grid.HiddenSelect) + query.Select.Add(hidden); + + // Sort: only a sortable column of this grid; anything else falls back to the default. + bool descending; + VirtualGridColumn sortColumn = ResolveSort(grid, columns, request, out descending); + query.OrderBy = (sortColumn != null ? sortColumn.Sql : grid.TieBreakerSql) + (descending ? " DESC" : " ASC"); + + // Search: the user's text is a parameter; only LIKE wildcards are escaped. + string term = CleanSearch(request.Search); + if (term.Length > 0) + { + query.SearchPattern = "%" + term.Replace(@"\", @"\\").Replace("%", @"\%").Replace("_", @"\_").Replace("[", @"\[") + "%"; + foreach (VirtualGridColumn c in columns.Where(c => grid.CanSearch(c))) + query.SearchColumns.Add(new KeyValuePair(c.Sql, c.Type)); + var extras = new VirtualGridSearchExtras(); + grid.AddSearchExtras(term, extras, context); + query.SearchPredicates.AddRange(extras.Predicates); + } + + return query; + } + + private static List> ReadRows(IDataReader reader) + { + var rows = new List>(); + try + { + int count = reader.FieldCount; + var names = new string[count]; + for (int i = 0; i < count; i++) + names[i] = reader.GetName(i); + while (reader.Read()) + { + var row = new Dictionary(count, StringComparer.OrdinalIgnoreCase); + for (int i = 0; i < count; i++) + row[names[i]] = reader.IsDBNull(i) ? null : reader.GetValue(i); + rows.Add(row); + } + } + finally + { + reader.Close(); + } + return rows; + } + + /// Lets the definition fill computed columns, then drops the hidden "_" helper values. + private static void FinishRows(VirtualGridSqlDefinition grid, List> rows, VirtualGridContext context) + { + if (rows.Count == 0) + return; + grid.OnRowsLoaded(rows, context); + foreach (Dictionary row in rows) + { + foreach (string key in row.Keys.Where(k => k.StartsWith("_")).ToList()) + row.Remove(key); + } + } + + #endregion + + #region Excel + + /// The requested fields that are columns of this grid, in the requested order (all columns when none are given). + private static List ResolveExportColumns(List columns, List fields) + { + if (fields == null || fields.Count == 0) + return columns; + var byField = columns.ToDictionary(c => c.Field, StringComparer.OrdinalIgnoreCase); + var result = new List(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (string field in fields) + { + VirtualGridColumn c; + if (field != null && byField.TryGetValue(field.Trim(), out c) && seen.Add(c.Field)) + result.Add(c); + } + if (result.Count == 0) + throw new ArgumentException("No columns to export."); + return result; + } + + private static byte[] BuildWorkbook(VirtualGridDefinition grid, List columns, + List> rows, string filterDescription, string searchText) + { + const int headerRow = 5; + using (var workbook = new XLWorkbook()) + { + string sheetName = grid.Title.Length > 31 ? grid.Title.Substring(0, 31) : grid.Title; + foreach (char ch in new[] { ':', '\\', '/', '?', '*', '[', ']' }) + sheetName = sheetName.Replace(ch, ' '); + IXLWorksheet ws = workbook.Worksheets.Add(sheetName); + + ws.Cell(1, 1).Value = grid.Title; + ws.Cell(1, 1).Style.Font.Bold = true; + ws.Cell(1, 1).Style.Font.FontSize = 14; + string line2 = filterDescription ?? string.Empty; + if (!string.IsNullOrWhiteSpace(searchText)) + line2 += (line2.Length > 0 ? " | " : "") + "Search: " + searchText.Trim(); + ws.Cell(2, 1).Value = line2; + ws.Cell(3, 1).Value = "Total: " + rows.Count.ToString("N0", CultureInfo.InvariantCulture); + + for (int c = 0; c < columns.Count; c++) + { + ws.Cell(headerRow, c + 1).Value = columns[c].Title; + switch (columns[c].Type) + { + case EnumVirtualGridColumnType.Date: + ws.Column(c + 1).Style.DateFormat.Format = "dd MMM yyyy"; + break; + case EnumVirtualGridColumnType.DateTime: + ws.Column(c + 1).Style.DateFormat.Format = "dd MMM yyyy HH:mm"; + break; + case EnumVirtualGridColumnType.Money: + ws.Column(c + 1).Style.NumberFormat.Format = "#,##0.00"; + break; + } + } + IXLRange header = ws.Range(headerRow, 1, headerRow, columns.Count); + header.Style.Font.Bold = true; + header.Style.Fill.BackgroundColor = XLColor.FromHtml("#E7ECF3"); + header.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; + header.Style.Border.InsideBorder = XLBorderStyleValues.Thin; + + int r = headerRow + 1; + foreach (Dictionary row in rows) + { + for (int c = 0; c < columns.Count; c++) + { + object value; + if (!row.TryGetValue(columns[c].Field, out value) || value == null) + continue; + SetCell(ws.Cell(r, c + 1), value, columns[c].Type); + } + r++; + } + + int lastRow = Math.Max(headerRow, r - 1); + ws.Range(headerRow, 1, lastRow, columns.Count).SetAutoFilter(); + ws.SheetView.FreezeRows(headerRow); + // Measure the header and the first rows only: measuring every row is slow on large files. + ws.Columns(1, columns.Count).AdjustToContents(headerRow, Math.Min(lastRow, headerRow + 300), 8, 60); + + using (var stream = new MemoryStream()) + { + workbook.SaveAs(stream); + return stream.ToArray(); + } + } + } + + private static void SetCell(IXLCell cell, object value, EnumVirtualGridColumnType type) + { + switch (type) + { + case EnumVirtualGridColumnType.Date: + case EnumVirtualGridColumnType.DateTime: + if (value is DateTime dt) { cell.Value = dt; return; } + break; + case EnumVirtualGridColumnType.Money: + case EnumVirtualGridColumnType.Number: + if (value is IConvertible && !(value is string) && !(value is DateTime)) + { + cell.Value = Convert.ToDecimal(value, CultureInfo.InvariantCulture); + return; + } + break; + } + cell.Value = Convert.ToString(value, CultureInfo.InvariantCulture); // text: no formulas, keeps leading zeros + } + + #endregion + + protected override T CreateObject(DataReader dr) + { + throw new NotSupportedException("VirtualGridService reads rows generically."); + } + } +} diff --git a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts new file mode 100644 index 0000000..36e6f86 --- /dev/null +++ b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts @@ -0,0 +1,63 @@ +/** + * Common virtual-scrolling grid (api/virtualGrid) — matches HRM.BO/VirtualGrid/VirtualGrid.cs. + * Screens use (custom-components/virtual-grid) rather than these directly. + */ + +/** EnumVirtualGridColumnType on the server. */ +export enum EnumVirtualGridColumnType { + Text = 1, + Number = 2, + Money = 3, + Date = 4, + DateTime = 5 +} + +export interface VirtualGridColumnInfo { + field: string; + title: string; + width: number; + type: EnumVirtualGridColumnType; + sortable: boolean; +} + +export interface VirtualGridLayout { + gridKey: string; + title: string; + defaultSortField: string; + defaultSortDir: 'asc' | 'desc'; + maxPageSize: number; + columns: VirtualGridColumnInfo[]; +} + +/** Screen-specific filters, e.g. { fromDate: '2025-01-01', toDate: '2025-12-31' }. Dates as 'yyyy-MM-dd'. */ +export type VirtualGridFilters = { [name: string]: string }; + +export interface VirtualGridRequest { + gridKey: string; + filters: VirtualGridFilters; + skip: number; + take: number; + search?: string; + sortField?: string; + sortDir?: 'asc' | 'desc'; + /** True on Preview: reload the data instead of using the copy the server keeps for scrolling. */ + refresh?: boolean; +} + +export interface VirtualGridExportRequest extends VirtualGridRequest { + fields: string[]; +} + +export interface VirtualGridPage { + total: number; + totalWithoutSearch: number; + data: { [field: string]: any }[]; +} + +/** Emitted by after every window it loads. */ +export interface VirtualGridLoadedEvent { + total: number; + totalWithoutSearch: number; + /** True for the first window after load(filters). */ + firstLoad: boolean; +} diff --git a/HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts b/HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts new file mode 100644 index 0000000..844a1b9 --- /dev/null +++ b/HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; +import { ApiService } from 'src/app/app.api.service'; +import { + VirtualGridExportRequest, + VirtualGridLayout, + VirtualGridPage, + VirtualGridRequest +} from 'src/app/_models/common/virtual-grid'; + +/** The one API for every virtual-scrolling grid (api/virtualGrid). */ +@Injectable({ + providedIn: 'root' +}) +export class VirtualGridService { + constructor(private apiService: ApiService) { + } + + getLayout(gridKey: string): Observable { + return this.apiService.httpGet('/virtualGrid/layout/' + encodeURIComponent(gridKey)); + } + + getPage(request: VirtualGridRequest): Observable { + return this.apiService.httpPost('/virtualGrid/page', request); + } + + exportExcel(request: VirtualGridExportRequest): Observable { + return this.apiService.httpDownloadFile('/virtualGrid/export-excel', request); + } +} diff --git a/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts b/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts index 6eb58a8..87e80c1 100644 --- a/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts +++ b/HRM.UI/ClientApp/src/app/_services/employee/employee.service.ts @@ -326,12 +326,6 @@ export class EmployeeServices { return this.apiService.httpPost('/Employee/getEmpDetailInfo', emps); } - // Discontinue Employee Details report: same rows as getEmpDetailInfo plus discontinueDate, - // for employees discontinued between fromDate and toDate (both 'yyyy-MM-dd'). - getDiscontinueEmpDetailInfo(fromDate: string, toDate: string) { - return this.apiService.httpPost('/Employee/getDiscontinueEmpDetailInfo', { fromDate: fromDate, toDate: toDate }); - } - getCurrentHeadCount(departmentId: number, designationId: number, gradeId: number) { return this.apiService.httpGet('/Employee/getCurrentHeadCount' + '/' + departmentId + '/' + designationId + '/' + gradeId); } diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html new file mode 100644 index 0000000..eed4e8e --- /dev/null +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html @@ -0,0 +1,46 @@ +@if (layoutError) { +
{{ layoutError }}
+} +@if (started && layout) { + + + @if (showSearch) { + + + } + + + @if (showExport) { + + } + + @for (column of layout.columns; track column.field) { + + + } + + {{ noRecordsText }} + + +} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.scss b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.scss new file mode 100644 index 0000000..cd5b7ee --- /dev/null +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.scss @@ -0,0 +1,48 @@ +:host { + display: block; +} + +.virtual-grid-search { + width: 280px; +} + +.virtual-grid-count { + margin-left: 12px; + white-space: nowrap; +} + +.virtual-grid-message { + padding: 12px; + color: #b42318; +} + +// Text that does not fit its column wraps onto more lines, so the full value is always readable +// (no "…"). [rowHeight] is the minimum row height; Kendo measures each rendered row's real height +// for virtual scrolling, so taller wrapped rows still scroll correctly. Long unbroken values +// (e-mail addresses, account numbers) break inside the word rather than overflow the cell. +:host ::ng-deep .virtual-grid .k-grid-content td { + white-space: normal; + overflow-wrap: anywhere; + vertical-align: top; + padding-top: 8px; + padding-bottom: 8px; + line-height: 1.35; +} + +// Header titles on one line (the app-wide _header.scss lets .k-column-title wrap). Column widths are +// widened in the component so every title fits; ellipsis is only a fallback after a manual resize. +:host ::ng-deep .virtual-grid .k-grid-header .k-column-title { + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +:host ::ng-deep .virtual-grid .virtual-grid-number { + text-align: right; +} + +@media (max-width: 600px) { + .virtual-grid-search { + width: 100%; + } +} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts new file mode 100644 index 0000000..29a6536 --- /dev/null +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts @@ -0,0 +1,290 @@ +import { + ChangeDetectionStrategy, Component, EventEmitter, Inject, Input, LOCALE_ID, OnDestroy, OnInit, Optional, Output +} from '@angular/core'; +import { formatDate } from '@angular/common'; +import { of, Subject, Subscription } from 'rxjs'; +import { catchError, debounceTime, distinctUntilChanged, finalize, switchMap } from 'rxjs/operators'; +import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; +import { SortDescriptor } from '@progress/kendo-data-query'; +import { saveAs } from 'file-saver'; +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 { VirtualGridService } from 'src/app/_services/common/virtual-grid.service'; +import { + EnumVirtualGridColumnType, + VirtualGridFilters, + VirtualGridLayout, + VirtualGridLoadedEvent, + VirtualGridPage, + VirtualGridRequest +} from 'src/app/_models/common/virtual-grid'; + +@Component({ + selector: 'app-virtual-grid', + templateUrl: './virtual-grid.component.html', + styleUrls: ['./virtual-grid.component.scss'], + changeDetection: ChangeDetectionStrategy.Eager, + standalone: false, +}) +export class VirtualGridComponent implements OnInit, OnDestroy { + /** Server grid key (VirtualGridRegistry). Required. */ + @Input() gridKey: string; + /** Grid height in px. */ + @Input() height = 600; + /** Rows fetched per window. */ + @Input() pageSize = 100; + /** Minimum (estimated) row height in px. Rows with wrapped text grow; Kendo measures the real heights while scrolling. */ + @Input() rowHeight = 36; + @Input() searchPlaceholder = 'Search in all columns...'; + @Input() noRecordsText = 'No records found.'; + /** Excel file name; defaults to the grid title. */ + @Input() exportFileName: string; + @Input() showSearch = true; + @Input() showExport = true; + + /** After every loaded window. */ + @Output() loaded = new EventEmitter(); + + layout: VirtualGridLayout; + layoutError = ''; + started = false; + gridView: GridDataResult = { data: [], total: 0 }; + totalWithoutSearch = 0; + gridLoading = false; + skip = 0; + sort: SortDescriptor[] = []; + searchText = ''; + appliedSearch = ''; + exporting = false; + + readonly columnType = EnumVirtualGridColumnType; + + private filters: VirtualGridFilters = {}; + private pendingFilters: VirtualGridFilters = null; + private firstLoad = false; + private readonly load$ = new Subject(); + private readonly search$ = new Subject(); + private readonly subscriptions = new Subscription(); + private readonly money = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); + + constructor(public apiService: ApiService, + private virtualGridService: VirtualGridService, + @Inject(LOCALE_ID) private locale: string, + @Optional() private notificationService: HRMNotificationService, + @Optional() private loadingPanel: loadingPanelService) { + } + + ngOnInit(): void { + this.subscriptions.add( + this.virtualGridService.getLayout(this.gridKey).subscribe( + (layout) => { + layout.columns = (layout.columns || []).map(c => ({ ...c, width: this.fitWidth(c) })); + this.layout = layout; + this.resetSort(); + if (this.pendingFilters) { + const filters = this.pendingFilters; + this.pendingFilters = null; + this.load(filters); + } + }, + (err) => { + this.layoutError = this.errorText(err, 'Failed to load the grid.'); + this.showError(this.layoutError); + } + ) + ); + + // A newer request (scroll, sort, search) cancels an older one still in flight, + // so a slow response can never overwrite newer rows. + this.subscriptions.add( + this.load$.pipe( + switchMap(request => { + this.gridLoading = true; + return this.virtualGridService.getPage(request).pipe( + catchError(err => { + this.showError(this.errorText(err, 'Failed to load data.')); + return of(null as VirtualGridPage); + }), + finalize(() => this.gridLoading = false) + ); + }) + ).subscribe(page => this.onPageLoaded(page)) + ); + + // Ask the server only once the user stops typing. + this.subscriptions.add( + this.search$.pipe(debounceTime(400), distinctUntilChanged()).subscribe(text => { + this.appliedSearch = text; + this.skip = 0; + this.requestPage(); + }) + ); + } + + ngOnDestroy(): void { + this.subscriptions.unsubscribe(); + } + + /** Shows the grid for these filters, from the first row, with the search cleared and the default sort. */ + load(filters: VirtualGridFilters): void { + if (!this.layout) { + this.pendingFilters = filters || {}; // runs as soon as the layout arrives + return; + } + this.filters = { ...(filters || {}) }; + this.searchText = ''; + this.appliedSearch = ''; + this.search$.next(''); // keeps distinctUntilChanged in step with the cleared box + this.skip = 0; + this.resetSort(); + this.gridView = { data: [], total: 0 }; + this.totalWithoutSearch = 0; + this.started = true; + this.firstLoad = true; + this.requestPage(true); // Preview: the server reloads the data instead of using its kept copy + } + + /** Reloads the current window, keeping filters, search and sort. */ + reload(): void { + this.requestPage(); + } + + onPageChange(event: PageChangeEvent): void { + this.skip = event.skip; + this.requestPage(); + } + + onSortChange(sort: SortDescriptor[]): void { + this.sort = sort; + this.skip = 0; + this.requestPage(); + } + + onSearchChange(text: string): void { + this.search$.next((text || '').trim()); + } + + exportExcel(): void { + if (!this.started || this.gridView.total === 0 || this.exporting) { + return; + } + this.exporting = true; + if (this.loadingPanel) { + this.loadingPanel.ShowLoadingPanel = true; + } + const request = { ...this.buildRequest(0, 0), fields: this.layout.columns.map(c => c.field) }; + const fileName = this.exportFileName || (this.layout.title || this.gridKey).replace(/[^A-Za-z0-9]+/g, '') + '.xlsx'; + + this.virtualGridService.exportExcel(request).pipe( + finalize(() => { + this.exporting = false; + if (this.loadingPanel) { + this.loadingPanel.ShowLoadingPanel = false; + } + }) + ).subscribe( + (blob: Blob) => saveAs(blob, fileName), + (err) => this.showBlobError(err, 'Failed to export to Excel.') + ); + } + + /** + * At least wide enough for the header title on one line plus the sort icon and cell padding, + * so no grid ever needs a wrapped or cut-off header (~8px per character + 48px). + */ + private fitWidth(column: { title: string; width: number }): number { + const titleWidth = (column.title || '').length * 8 + 48; + return Math.max(column.width || 0, titleWidth); + } + + private resetSort(): void { + this.sort = this.layout && this.layout.defaultSortField + ? [{ field: this.layout.defaultSortField, dir: this.layout.defaultSortDir || 'asc' }] + : []; + } + + private requestPage(refresh = false): void { + if (!this.started || !this.layout) { + return; + } + this.load$.next({ ...this.buildRequest(this.skip, this.pageSize), refresh: refresh }); + } + + private buildRequest(skip: number, take: number): VirtualGridRequest { + const sort = this.sort && this.sort.length > 0 && this.sort[0].dir ? this.sort[0] : null; + return { + gridKey: this.gridKey, + filters: this.filters, + skip: skip, + take: take, + search: this.appliedSearch || undefined, + sortField: sort ? sort.field : undefined, + sortDir: sort ? (sort.dir as 'asc' | 'desc') : undefined, + }; + } + + private onPageLoaded(page: VirtualGridPage): void { + if (!page) { + return; + } + this.gridView = { data: (page.data || []).map(row => this.formatRow(row)), total: page.total }; + this.totalWithoutSearch = page.totalWithoutSearch; + this.loaded.emit({ total: page.total, totalWithoutSearch: page.totalWithoutSearch, firstLoad: this.firstLoad }); + this.firstLoad = false; + } + + /** Display formatting by column type: dates 'dd MMM yyyy', money with 2 decimals — the same text the server searches. */ + private formatRow(row: { [field: string]: any }): { [field: string]: any } { + const result: { [field: string]: any } = { ...row }; + for (const column of this.layout.columns) { + const value = row[column.field]; + if (value === null || value === undefined || value === '') { + result[column.field] = ''; + continue; + } + switch (column.type) { + case EnumVirtualGridColumnType.Date: + result[column.field] = formatDate(value, 'dd MMM yyyy', this.locale); + break; + case EnumVirtualGridColumnType.DateTime: + result[column.field] = formatDate(value, 'dd MMM yyyy HH:mm', this.locale); + break; + case EnumVirtualGridColumnType.Money: + result[column.field] = this.money.format(Number(value)); + break; + default: + result[column.field] = String(value); + } + } + return result; + } + + private errorText(err: any, fallback: string): string { + if (typeof err === 'string' && err) { + return err; + } + if (err && typeof err.error === 'string' && err.error) { + return err.error; + } + return fallback; + } + + private showError(message: string): void { + if (this.notificationService) { + this.notificationService.showError(message); + } else { + console.error(message); + } + } + + /** A failed file download returns its error message as a Blob; read it before showing it. */ + private showBlobError(err: any, fallback: string): void { + const blob: Blob = err instanceof Blob ? err : (err && err.error instanceof Blob ? err.error : null); + if (blob) { + blob.text().then(text => this.showError(text || fallback), () => this.showError(fallback)); + } else { + this.showError(this.errorText(err, fallback)); + } + } +} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts new file mode 100644 index 0000000..9b59add --- /dev/null +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts @@ -0,0 +1,15 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { GridModule } from '@progress/kendo-angular-grid'; +import { ButtonsModule } from '@progress/kendo-angular-buttons'; +import { TextBoxModule } from '@progress/kendo-angular-inputs'; +import { VirtualGridComponent } from './virtual-grid.component'; + +/** Import this module to use (common server-paged virtual-scrolling grid). */ +@NgModule({ + declarations: [VirtualGridComponent], + imports: [CommonModule, FormsModule, GridModule, ButtonsModule, TextBoxModule], + exports: [VirtualGridComponent] +}) +export class VirtualGridModule {} diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html index fe546dc..4a62400 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html @@ -17,28 +17,11 @@ -@if (showGrid) { -
- - - - - - - - - @for (column of columns; track column.field) { - - - } - - -
-} + +
+ + +
diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss index 778804b..7779367 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.scss @@ -39,18 +39,8 @@ margin-left: auto; } -.grid-search { - width: 280px; -} - -.grid-count { - margin-left: 12px; - white-space: nowrap; -} - @media (max-width: 600px) { - .filter-field, - .grid-search { + .filter-field { width: 100%; } diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts index 8472e09..9cea443 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts @@ -1,23 +1,16 @@ -import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core'; +import { Component, ChangeDetectionStrategy, ViewChild } 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; -} +import { VirtualGridComponent } from 'src/app/custom-components/virtual-grid/virtual-grid.component'; +import { VirtualGridLoadedEvent } from 'src/app/_models/common/virtual-grid'; /** - * 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. + * Discontinue Employee Details: employees discontinued inside a date range, with the same columns as + * Employee Details (/reports/report-viewer/empDetailInfo) plus the Discontinue Date. + * + * The grid is the common (server grid "discontinue-employee", + * HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs); this screen only owns its filters. */ @Component({ selector: 'app-discontinue-employee-details', @@ -26,86 +19,19 @@ interface GridColumn { changeDetection: ChangeDetectionStrategy.Eager, standalone: false, }) -export class DiscontinueEmployeeDetailsComponent implements OnInit { +export class DiscontinueEmployeeDetailsComponent { + @ViewChild('grid') grid: VirtualGridComponent; + 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'); @@ -115,54 +41,15 @@ export class DiscontinueEmployeeDetailsComponent implements OnInit { 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'); - } - } - ); + this.grid.load({ + fromDate: this.datePipe.transform(this.fromDate, 'yyyy-MM-dd'), + toDate: this.datePipe.transform(this.toDate, 'yyyy-MM-dd'), + }); } - /** 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; + onLoaded(event: VirtualGridLoadedEvent): void { + if (event.firstLoad && event.total === 0) { + this.notificationService.showWarning('No employee was discontinued in the selected date range.', 'Warning'); } - 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; } } diff --git a/HRM.UI/ClientApp/src/app/reports/reports.module.ts b/HRM.UI/ClientApp/src/app/reports/reports.module.ts index 5101ad7..cfa6876 100644 --- a/HRM.UI/ClientApp/src/app/reports/reports.module.ts +++ b/HRM.UI/ClientApp/src/app/reports/reports.module.ts @@ -57,6 +57,7 @@ import { EssTaxCertificateComponent } from './ess-tax-certificate/ess-tax-certif import { EmpCommonInformationModule } from '../ess/emp-common-information/EmpCommonInformationModule'; import { JvReportComponent } from './jv-report/jv-report.component'; import { DiscontinueEmployeeDetailsComponent } from './discontinue-employee-details/discontinue-employee-details.component'; +import { VirtualGridModule } from '../custom-components/virtual-grid/virtual-grid.module'; @NgModule({ declarations: [ @@ -114,7 +115,8 @@ import { DiscontinueEmployeeDetailsComponent } from './discontinue-employee-deta CardModule, BackgroundCanvasModule, TabViewCompatModule, - EmpCommonInformationModule + EmpCommonInformationModule, + VirtualGridModule ], providers: [ EmployeeServices, diff --git a/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs b/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs index 3904c74..3838dab 100644 --- a/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs +++ b/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs @@ -48,6 +48,7 @@ namespace HRM.UI.Configuration.DI services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/HRM.UI/Controllers/CommonInterface/VirtualGridController.cs b/HRM.UI/Controllers/CommonInterface/VirtualGridController.cs new file mode 100644 index 0000000..a8a216c --- /dev/null +++ b/HRM.UI/Controllers/CommonInterface/VirtualGridController.cs @@ -0,0 +1,97 @@ +using System; +using HRM.BO; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace HRM.UI.Controllers.CommonInterface +{ + [ApiController] + [Route("api/virtualGrid")] + [Authorize] + public class VirtualGridController : ControllerBase + { + private readonly IVirtualGridService _virtualGridService; + + public VirtualGridController(IVirtualGridService virtualGridService) + { + _virtualGridService = virtualGridService; + } + + [HttpGet("layout/{gridKey}")] + public ActionResult GetLayout(string gridKey) + { + try + { + return Ok(_virtualGridService.GetLayout(gridKey, GetContext())); + } + catch (ArgumentException e) + { + return BadRequest(e.Message); + } + catch (UnauthorizedAccessException e) + { + return StatusCode(StatusCodes.Status403Forbidden, e.Message); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + } + } + + [HttpPost("page")] + public ActionResult GetPage(VirtualGridRequest request) + { + try + { + return Ok(_virtualGridService.GetPage(request, GetContext())); + } + catch (ArgumentException e) + { + return BadRequest(e.Message); + } + catch (UnauthorizedAccessException e) + { + return StatusCode(StatusCodes.Status403Forbidden, e.Message); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + } + } + + [HttpPost("export-excel")] + public ActionResult ExportExcel(VirtualGridExportRequest request) + { + try + { + VirtualGridFile file = _virtualGridService.ExportExcel(request, GetContext()); + return File(file.Content, file.ContentType, file.FileName); + } + catch (ArgumentException e) + { + return BadRequest(e.Message); + } + catch (UnauthorizedAccessException e) + { + return StatusCode(StatusCodes.Status403Forbidden, e.Message); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, e.Message); + } + } + + private VirtualGridContext GetContext() + { + CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + return new VirtualGridContext + { + UserID = currentUser.UserID, + EmployeeID = currentUser.EmployeeID, + PayrollTypeID = currentUser.PayrollTypeID, + UserType = currentUser.UserType, + }; + } + } +} From ae428ff45228bf77f2e9cfc20a76ec22b9489ad3 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 14:54:13 +0600 Subject: [PATCH 4/8] Virtual Pagination --- HRM.BO/VirtualGrid/VirtualGrid.cs | 110 ---- HRM.BO/VirtualPaging/VirtualPaging.cs | 74 +++ HRM.DA/DA/VirtualGrid/VirtualGridDA.cs | 110 ---- .../DiscontinueEmployeeVirtualGrid.cs | 103 --- HRM.DA/Service/VirtualGrid/README.md | 88 --- .../VirtualGrid/VirtualGridDefinition.cs | 271 -------- .../VirtualGrid/VirtualGridRegistry.cs | 27 - .../Service/VirtualGrid/VirtualGridService.cs | 591 ------------------ HRM.DA/Service/VirtualPaging/README.md | 77 +++ HRM.DA/Service/VirtualPaging/VirtualPaging.cs | 200 ++++++ .../src/app/_models/common/virtual-grid.ts | 54 +- .../_services/common/virtual-grid.service.ts | 30 - .../virtual-grid/virtual-grid.component.html | 29 +- .../virtual-grid/virtual-grid.component.ts | 259 ++++---- .../virtual-grid/virtual-grid.module.ts | 7 +- ...iscontinue-employee-details.component.html | 4 +- .../discontinue-employee-details.component.ts | 73 ++- .../DI/ServiceCollectionExtensions.cs | 1 - .../CommonInterface/VirtualGridController.cs | 97 --- .../Employee/EmployeeController.cs | 21 + 20 files changed, 593 insertions(+), 1633 deletions(-) delete mode 100644 HRM.BO/VirtualGrid/VirtualGrid.cs create mode 100644 HRM.BO/VirtualPaging/VirtualPaging.cs delete mode 100644 HRM.DA/DA/VirtualGrid/VirtualGridDA.cs delete mode 100644 HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs delete mode 100644 HRM.DA/Service/VirtualGrid/README.md delete mode 100644 HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs delete mode 100644 HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs delete mode 100644 HRM.DA/Service/VirtualGrid/VirtualGridService.cs create mode 100644 HRM.DA/Service/VirtualPaging/README.md create mode 100644 HRM.DA/Service/VirtualPaging/VirtualPaging.cs delete mode 100644 HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts delete mode 100644 HRM.UI/Controllers/CommonInterface/VirtualGridController.cs diff --git a/HRM.BO/VirtualGrid/VirtualGrid.cs b/HRM.BO/VirtualGrid/VirtualGrid.cs deleted file mode 100644 index d423bc5..0000000 --- a/HRM.BO/VirtualGrid/VirtualGrid.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace HRM.BO -{ - /// - /// Common virtual-scrolling grid ("virtual grid") contracts. - /// - /// Every large list screen shares ONE BO / DA / Service / Controller: a screen is just a registered - /// grid definition (HRM.DA/Service/VirtualGrid/Definitions) identified by . - /// The server pages, sorts, searches and exports; the browser only ever holds one window of rows. - /// See HRM.DA/Service/VirtualGrid/README.md for how to add a grid. - /// - public enum EnumVirtualGridColumnType - { - Text = 1, - Number = 2, - Money = 3, - Date = 4, - DateTime = 5 - } - - /// A column as the screen sees it (from the layout endpoint). - public class VirtualGridColumnInfo - { - public string Field { get; set; } - public string Title { get; set; } - public int Width { get; set; } - public EnumVirtualGridColumnType Type { get; set; } - public bool Sortable { get; set; } - } - - /// Everything the screen needs to draw an empty grid before any data is requested. - public class VirtualGridLayout - { - public string GridKey { get; set; } - public string Title { get; set; } - public string DefaultSortField { get; set; } - public string DefaultSortDir { get; set; } - public int MaxPageSize { get; set; } - public List Columns { get; set; } = new List(); - } - - /// One window request: which grid, its filters, and the grid's skip/take, sort and search. - public class VirtualGridRequest - { - public string GridKey { get; set; } - - /// - /// Screen-specific filters (e.g. fromDate / toDate as "yyyy-MM-dd"). Each grid definition reads and - /// validates the ones it needs; values are never spliced into SQL. - /// - public Dictionary Filters { get; set; } = new Dictionary(); - - public int Skip { get; set; } - public int Take { get; set; } = 100; - public string Search { get; set; } - public string SortField { get; set; } - public string SortDir { get; set; } - - /// - /// True on Preview: reload the data instead of using the copy kept in server memory (list grids). - /// Scrolling, sorting and searching send false and are answered from that copy. - /// - public bool Refresh { get; set; } - } - - /// Excel export: the same request plus the grid's visible columns, in order. - public class VirtualGridExportRequest : VirtualGridRequest - { - /// Fields of the columns to export, in grid order. Empty = every column of the grid. - public List Fields { get; set; } = new List(); - } - - public class VirtualGridPage - { - /// Rows matching the filters and the search. - public int Total { get; set; } - - /// Rows matching the filters alone (equals Total when there is no search). - public int TotalWithoutSearch { get; set; } - - /// One row per entry, keyed by column field. Values are raw (dates, numbers); the screen formats them. - public List> Data { get; set; } = new List>(); - } - - /// A generated file (Excel export). - public class VirtualGridFile - { - public string FileName { get; set; } - public string ContentType { get; set; } - public byte[] Content { get; set; } - } - - /// The signed-in user, as the grid definitions need it for scoping (payroll type) and access. - public class VirtualGridContext - { - public int UserID { get; set; } - public int? EmployeeID { get; set; } - public int? PayrollTypeID { get; set; } - public EnumUserType UserType { get; set; } - } - - public interface IVirtualGridService - { - VirtualGridLayout GetLayout(string gridKey, VirtualGridContext context); - VirtualGridPage GetPage(VirtualGridRequest request, VirtualGridContext context); - VirtualGridFile ExportExcel(VirtualGridExportRequest request, VirtualGridContext context); - } -} diff --git a/HRM.BO/VirtualPaging/VirtualPaging.cs b/HRM.BO/VirtualPaging/VirtualPaging.cs new file mode 100644 index 0000000..85e9cbf --- /dev/null +++ b/HRM.BO/VirtualPaging/VirtualPaging.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace HRM.BO +{ + /// + /// What <app-virtual-grid> sends to a page endpoint: the page's own filters plus the grid's + /// scroll position, search text and sort. The same class is used by every page, so no page needs a + /// request class of its own. See HRM.DA/Service/VirtualPaging/README.md. + /// + public class VirtualPageRequest + { + /// The page's filters, e.g. { fromDate: "2025-01-01", toDate: "2025-12-31" }. Read them with GetDate / GetInt / GetString. + public Dictionary Filters { get; set; } = new Dictionary(); + + /// The grid's columns (property names). Only these are sent back and searched. + public List Fields { get; set; } = new List(); + + public int Skip { get; set; } + + /// Rows wanted. 0 = all rows (used by Export to Excel). + public int Take { get; set; } = 100; + + public string Search { get; set; } + public string SortField { get; set; } + + /// "asc" or "desc". + public string SortDir { get; set; } + + /// True on Preview: load the data again. False on scroll/sort/search/export: use the kept copy. + public bool Refresh { get; set; } + + /// A filter as text, or null when it was not sent. + public string GetString(string name) + { + string value; + if (Filters != null && Filters.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value)) + return value.Trim(); + return null; + } + + /// A "yyyy-MM-dd" filter. Throws a message fit for the user when it is missing or invalid. + public DateTime GetDate(string name, string label = null) + { + DateTime value; + if (!DateTime.TryParseExact(GetString(name), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out value)) + throw new ArgumentException("Please select a valid " + (label ?? name) + "."); + return value; + } + + /// A whole-number filter. Throws a message fit for the user when it is missing or invalid. + public int GetInt(string name, string label = null) + { + int value; + if (!int.TryParse(GetString(name), NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) + throw new ArgumentException("Please select a valid " + (label ?? name) + "."); + return value; + } + } + + /// One window of rows for the grid. + public class VirtualPageResult + { + /// Rows matching the search (all rows when there is no search). + public int Total { get; set; } + + /// All rows, ignoring the search. + public int TotalWithoutSearch { get; set; } + + /// The rows of this window, holding only the requested fields. + public List> Data { get; set; } = new List>(); + } +} diff --git a/HRM.DA/DA/VirtualGrid/VirtualGridDA.cs b/HRM.DA/DA/VirtualGrid/VirtualGridDA.cs deleted file mode 100644 index 24fd9fb..0000000 --- a/HRM.DA/DA/VirtualGrid/VirtualGridDA.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using Ease.Core.DataAccess; -using Ease.Core.DataAccess.SQL; -using HRM.BO; -using Microsoft.Data.SqlClient; - -namespace HRM.DA -{ - /// - /// Everything VirtualGridDA needs to build a grid's SQL. Built by VirtualGridService from a grid - /// definition; all SQL fragments in it are code-produced (definition expressions, whitelisted sort, - /// integer lists), and all user values are parameters. - /// - internal sealed class VirtualGridQuery - { - /// "sqlExpression AS [alias]" entries. - public List Select { get; } = new List(); - - public VirtualGridSource Source { get; set; } - - /// Columns searched with LIKE, with the type that decides how each is rendered as text. - public List> SearchColumns { get; } = - new List>(); - - /// Extra OR-branches of the search (code-produced, e.g. "e.DEPARTMENTID IN (1,2)"). - public List SearchPredicates { get; } = new List(); - - /// The escaped LIKE pattern ("%text%"), or null for no search. - public string SearchPattern { get; set; } - - /// Whitelisted sort expression including ASC/DESC. - public string OrderBy { get; set; } - - public string TieBreaker { get; set; } - } - - /// - /// The one data-access class behind every virtual grid: a COUNT and an OFFSET/FETCH window (or every - /// row, for Excel) over the grid's source, with search and sort done by SQL Server. - /// - internal static class VirtualGridDA - { - internal static int Count(TransactionContext tc, VirtualGridQuery query, bool withSearch) - { - string sql = "SELECT COUNT(*) " + query.Source.FromWhere + SearchClause(query, withSearch); - object value = tc.ExecuteScalar(CommandType.Text, sql, Parameters(query, withSearch, null, null)); - return value == null || value == DBNull.Value ? 0 : Convert.ToInt32(value); - } - - /// One window of rows; with null, every row (Excel export). - internal static IDataReader GetRows(TransactionContext tc, VirtualGridQuery query, int skip, int? take) - { - string sql = "SELECT " + string.Join(", ", query.Select) + " " - + query.Source.FromWhere + SearchClause(query, true) - + " ORDER BY " + query.OrderBy + ", " + query.TieBreaker + " ASC"; - if (take.HasValue) - sql += " OFFSET @__Skip ROWS FETCH NEXT @__Take ROWS ONLY"; - return tc.ExecuteReader(CommandType.Text, sql, Parameters(query, true, skip, take)); - } - - /// A column as the screen displays it, so the user can search what they see. - private static string AsDisplayText(string sql, EnumVirtualGridColumnType type) - { - switch (type) - { - case EnumVirtualGridColumnType.Date: - return "CONVERT(varchar(11), " + sql + ", 106)"; // 13 Feb 2025 - case EnumVirtualGridColumnType.DateTime: - return "(CONVERT(varchar(11), " + sql + ", 106) + ' ' + CONVERT(varchar(5), " + sql + ", 108))"; - case EnumVirtualGridColumnType.Money: - return "CONVERT(varchar(30), CAST(" + sql + " AS money), 1)"; // 1,234.56 - case EnumVirtualGridColumnType.Number: - return "CONVERT(varchar(40), " + sql + ")"; - default: - return sql; - } - } - - private static string SearchClause(VirtualGridQuery query, bool withSearch) - { - if (!withSearch || string.IsNullOrEmpty(query.SearchPattern)) - return string.Empty; - - List parts = query.SearchColumns - .Select(c => AsDisplayText(c.Key, c.Value) + @" LIKE @__Search ESCAPE '\'") - .Concat(query.SearchPredicates) - .ToList(); - return parts.Count == 0 ? " AND 1 = 0" : " AND (" + string.Join(" OR ", parts) + ")"; - } - - private static IDataParameter[] Parameters(VirtualGridQuery query, bool withSearch, int? skip, int? take) - { - // Clones: a SqlParameter cannot belong to two commands, and one query runs several. - var list = query.Source.Parameters - .Select(p => (SqlParameter)((ICloneable)p).Clone()) - .ToList(); - if (withSearch && !string.IsNullOrEmpty(query.SearchPattern)) - list.Add(SqlHelperExtension.CreateInParam("@__Search", SqlDbType.NVarChar, query.SearchPattern, 400)); - if (take.HasValue) - { - list.Add(SqlHelperExtension.CreateInParam("@__Skip", SqlDbType.Int, skip ?? 0)); - list.Add(SqlHelperExtension.CreateInParam("@__Take", SqlDbType.Int, take.Value)); - } - return list.ToArray(); - } - } -} diff --git a/HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs b/HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs deleted file mode 100644 index c65bc77..0000000 --- a/HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using HRM.BO; -using HRM.BO.Report; - -namespace HRM.DA -{ - /// - /// Discontinue Employee Details (Manage Employee › Report, /reports/discontinue-employee-details): - /// employees discontinued inside a date range, with the same columns as Employee Details plus the - /// Discontinue Date. - /// - /// The data comes the usual way — EmployeeService.GetDiscontinuedEmployeeDetails returns a - /// List<DiscontinueEmployeeDetailInfo> (query in EmployeeDA) — and the common virtual grid pages it. - /// Filters: fromDate, toDate ("yyyy-MM-dd", both required). Scoped to the user's payroll type. - /// - internal sealed class DiscontinueEmployeeVirtualGrid : VirtualGridListDefinition - { - public const string GridKey = "discontinue-employee"; - - public override string Key => GridKey; - public override string Title => "Discontinue Employee Details"; - public override string DefaultSortField => "employeeNo"; - public override string ExcelFileName => "DiscontinueEmployeeDetails.xlsx"; - - /// Each Field is a property of DiscontinueEmployeeDetailInfo. - public override List GetColumns(VirtualGridContext context) - { - var columns = new List - { - new VirtualGridColumn { Field = "employeeNo", Title = "Employee No", Width = 110 }, - new VirtualGridColumn { Field = "name", Title = "Employee Name", Width = 220 }, - new VirtualGridColumn { Field = "gender", Title = "Gender", Width = 100 }, - new VirtualGridColumn { Field = "birthDate", Title = "Birth Date", Width = 120, Type = EnumVirtualGridColumnType.Date }, - new VirtualGridColumn { Field = "joiningDate", Title = "Joining Date", Width = 120, Type = EnumVirtualGridColumnType.Date }, - new VirtualGridColumn { Field = "discontinueDate", Title = "Discontinue Date", Width = 140, Type = EnumVirtualGridColumnType.Date }, - new VirtualGridColumn { Field = "mobileNo", Title = "Mobile No", Width = 130 }, - new VirtualGridColumn { Field = "emailAddress", Title = "Email Address", Width = 200 }, - new VirtualGridColumn { Field = "designation", Title = "Designation", Width = 160 }, - new VirtualGridColumn { Field = "grade", Title = "Grade", Width = 120 }, - new VirtualGridColumn { Field = "category", Title = "Category", Width = 120 }, - new VirtualGridColumn { Field = "status", Title = "Status", Width = 120 }, - }; - - // Department tier headings come from the Logic configuration, as in Employee Details. - string[] tierFields = { "deptireOne", "deptireTwo", "deptireThree", "deptireFour", "deptireFive" }; - List configs = new SystemConfigarationService() - .GetAllChildrenByParentNode(EnumConfigurationType.Logic, "department") ?? new List(); - int tier = 0; - foreach (SystemConfigaration c in configs) - { - if (!string.Equals(c.Node, "text", StringComparison.OrdinalIgnoreCase)) - continue; - if (tier < tierFields.Length) - columns.Add(new VirtualGridColumn { Field = tierFields[tier], Title = c.Value, Width = 150 }); - tier++; - } - - columns.AddRange(new[] - { - new VirtualGridColumn { Field = "location", Title = "Location", Width = 130 }, - new VirtualGridColumn { Field = "religion", Title = "Religion", Width = 110 }, - new VirtualGridColumn { Field = "maritalStatus", Title = "Marital Status", Width = 120 }, - new VirtualGridColumn { Field = "dateOfConfirmation", Title = "Confirmation", Width = 120, Type = EnumVirtualGridColumnType.Date }, - new VirtualGridColumn { Field = "basicSalary", Title = "Basic Salary", Width = 120, Type = EnumVirtualGridColumnType.Money }, - new VirtualGridColumn { Field = "grossSalary", Title = "Gross Salary", Width = 120, Type = EnumVirtualGridColumnType.Money }, - new VirtualGridColumn { Field = "passportNo", Title = "Passport No", Width = 120 }, - new VirtualGridColumn { Field = "bloodGroup", Title = "Blood Group", Width = 110 }, - new VirtualGridColumn { Field = "lineManagerNo", Title = "LineManager No", Width = 130 }, - new VirtualGridColumn { Field = "lineManagerName", Title = "LineManager Name", Width = 180 }, - new VirtualGridColumn { Field = "lineManagerEmail", Title = "LineManager Email", Width = 200 }, - new VirtualGridColumn { Field = "empStatus", Title = "Employee Status", Width = 130 }, - new VirtualGridColumn { Field = "bank", Title = "Bank", Width = 150 }, - new VirtualGridColumn { Field = "branch", Title = "Branch", Width = 150 }, - new VirtualGridColumn { Field = "accountNo", Title = "Account No", Width = 150 }, - new VirtualGridColumn { Field = "nationalId", Title = "National ID/NID", Width = 150 }, - new VirtualGridColumn { Field = "tinNo", Title = "TIN", Width = 120 }, - }); - return columns; - } - - protected override List LoadData(VirtualGridFilters filters, VirtualGridContext context) - { - DateTime fromDate = filters.GetDate("fromDate", "From Date", true).Value; - DateTime toDate = filters.GetDate("toDate", "To Date", true).Value; - if (fromDate > toDate) - throw new ArgumentException("From Date cannot be after To Date."); - if (!context.PayrollTypeID.HasValue) - throw new ArgumentException("No payroll type is selected for your login."); - - return new EmployeeService().GetDiscontinuedEmployeeDetails(fromDate, toDate, context.PayrollTypeID.Value); - } - - public override string DescribeFilters(VirtualGridFilters filters) - { - DateTime? fromDate = filters.GetDate("fromDate", "From Date", false); - DateTime? toDate = filters.GetDate("toDate", "To Date", false); - return "Discontinued from " + fromDate?.ToString("dd MMM yyyy", CultureInfo.InvariantCulture) - + " to " + toDate?.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); - } - } -} diff --git a/HRM.DA/Service/VirtualGrid/README.md b/HRM.DA/Service/VirtualGrid/README.md deleted file mode 100644 index 0ad94e6..0000000 --- a/HRM.DA/Service/VirtualGrid/README.md +++ /dev/null @@ -1,88 +0,0 @@ -# Virtual grid — common virtual-scrolling grid for large lists - -One common controller, service and Angular component serve every virtual-scrolling list. The data -still comes **the usual way**: a service method returns a `List` (query in the module's DA). A grid -only says *which* service method to call and *which* columns to show. - -| Layer | File | Role | -|---|---|---| -| BO | `HRM.BO/VirtualGrid/VirtualGrid.cs` | Request/page/layout DTOs, `EnumVirtualGridColumnType`, `IVirtualGridService` | -| Service | `HRM.DA/Service/VirtualGrid/VirtualGridService.cs` | Access check, keeps the loaded list in memory, search/sort/page, Excel | -| Grids | `HRM.DA/Service/VirtualGrid/Definitions/*.cs` | **One small class per grid**: columns + which service to call | -| Registry | `HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs` | Key → grid. Unlisted keys cannot be queried | -| Controller | `HRM.UI/Controllers/CommonInterface/VirtualGridController.cs` | `GET api/virtualGrid/layout/{key}`, `POST page`, `POST export-excel` | -| Angular | `custom-components/virtual-grid/` (`VirtualGridModule`, ``) | Virtual scrolling, search box, sort, total, Excel button | - -## How it works - -1. Screen opens → the component asks for the grid's columns (`layout`). -2. **Preview** → `grid.load(filters)` → `page` with `refresh: true` → the grid's `LoadData` calls the - service (e.g. `EmployeeService.GetDiscontinuedEmployeeDetails`) → the whole `List` is kept in - server memory (per user + filters, 10 min sliding / 30 min max) → the first 100 rows go back. -3. **Scroll / sort / search** → `page` with `refresh: false` → answered from the kept list: search, - sort, `Skip/Take` 100. The service is not called again. -4. **Export to Excel** → every matching row of the kept list, same order, as `.xlsx`. - -The browser only ever holds ~100 rows, however long the list is. - -## Adding a grid (existing screen with a service that already returns a list) - -1. **Grid class** — `Definitions/VirtualGrid.cs`: - ```csharp - internal sealed class LeaveHistoryVirtualGrid : VirtualGridListDefinition - { - public const string GridKey = "leave-history"; - public override string Key => GridKey; - public override string Title => "Leave History"; - - public override List GetColumns(VirtualGridContext context) => new List - { - // Field = a property of LeaveEntry (camelCase) - new VirtualGridColumn { Field = "employeeNo", Title = "Employee No", Width = 110 }, - new VirtualGridColumn { Field = "fromDate", Title = "From", Width = 120, Type = EnumVirtualGridColumnType.Date }, - new VirtualGridColumn { Field = "amount", Title = "Amount", Width = 120, Type = EnumVirtualGridColumnType.Money }, - }; - - protected override List LoadData(VirtualGridFilters filters, VirtualGridContext context) - { - DateTime from = filters.GetDate("fromDate", "From Date", true).Value; // user-facing errors - DateTime to = filters.GetDate("toDate", "To Date", true).Value; - return new LeaveEntryService().Get(from, to, context.PayrollTypeID.Value); // the existing service - } - } - ``` -2. **Registry** — `{ LeaveHistoryVirtualGrid.GridKey, () => new LeaveHistoryVirtualGrid() }`. -3. **Screen** — import `VirtualGridModule`, then: - ```html - - ``` - ```ts - this.grid.load({ fromDate: '2025-01-01', toDate: '2025-12-31' }); // filters as strings - ``` - -No new controller, Angular service or model. For a brand-new screen, write its query in the module's -DA and a service method returning `List` (as always), then steps 1–3. - -Worked example: `Definitions/DiscontinueEmployeeVirtualGrid.cs` → -`EmployeeService.GetDiscontinuedEmployeeDetails` → `EmployeeDA.GetDiscontinuedEmployeeDetails`. - -## Column types - -`Text` (default), `Number`, `Money` (`1,234.56`), `Date` (`dd MMM yyyy`), `DateTime` (`dd MMM yyyy HH:mm`). -The screen shows values in that format and the search box searches that same text, so users find what -they see. Set `Sortable = false` / `Searchable = false` on a column to turn either off. - -## Very large lists: `VirtualGridSqlDefinition` (advanced) - -A list grid loads the whole list once per Preview — fine up to tens of thousands of rows. When even -that is too slow, derive from `VirtualGridSqlDefinition` instead: give each column a `Sql` expression -and a `BuildSource` returning `FROM … WHERE …` with parameters, and SQL Server pages each request -(`OFFSET/FETCH`, `HRM.DA/DA/VirtualGrid/VirtualGridDA.cs`). User values reach SQL only as parameters; -spliced text is code-produced only. - -## Rules - -- A service a grid calls must not run one query per row; load lookups once (see `GetDiscontinuedEmployeeDetails`). -- Self-service (ESS) logins are refused unless the grid sets `AllowSelfServiceUsers`. Authorization in - this app is menu-driven, so add any further check a grid needs in `LoadData`. -- The kept list is per user, payroll type, grid and filter values; Preview always reloads it. diff --git a/HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs b/HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs deleted file mode 100644 index 02f8de1..0000000 --- a/HRM.DA/Service/VirtualGrid/VirtualGridDefinition.cs +++ /dev/null @@ -1,271 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Reflection; -using System.Text.RegularExpressions; -using HRM.BO; -using Microsoft.Data.SqlClient; - -namespace HRM.DA -{ - /// - /// One column of a virtual grid. Field is the name the screen uses. In a list grid it is a property - /// of the business object; in a SQL grid it is the alias of Sql, the expression the column selects, - /// sorts and searches by (null = filled in code, not sorted or searched by SQL). - /// - internal sealed class VirtualGridColumn - { - public string Field { get; set; } - public string Title { get; set; } - public int Width { get; set; } = 130; - public EnumVirtualGridColumnType Type { get; set; } = EnumVirtualGridColumnType.Text; - public string Sql { get; set; } - public bool Sortable { get; set; } = true; - public bool Searchable { get; set; } = true; - - } - - /// - /// The rows a grid is built on: a "FROM ... WHERE ..." clause plus its parameters. - /// Filter values must only ever reach SQL through . - /// - internal sealed class VirtualGridSource - { - public string FromWhere { get; set; } - public List Parameters { get; } = new List(); - - /// Human-readable filter summary, printed at the top of the Excel file. - public string FilterDescription { get; set; } - } - - /// - /// Extra search conditions a definition adds for values that are worked out in code (e.g. department - /// tier names): each becomes one more OR branch of the search. Only code-produced SQL goes here. - /// - internal sealed class VirtualGridSearchExtras - { - internal List Predicates { get; } = new List(); - - /// Adds "sqlExpression IN (v1, v2, …)" — the values are integers, so they are safe to splice. - public void AddIntIn(string sqlExpression, IEnumerable values) - { - List list = (values ?? Enumerable.Empty()).Distinct().ToList(); - if (list.Count > 0) - Predicates.Add(sqlExpression + " IN (" + string.Join(",", list.Select(v => v.ToString(CultureInfo.InvariantCulture))) + ")"); - } - } - - /// - /// Typed, validated access to . Every problem is an - /// ArgumentException with a message fit to show the user (the controller returns it as 400). - /// - internal sealed class VirtualGridFilters - { - private readonly Dictionary _values; - - public VirtualGridFilters(Dictionary values) - { - _values = new Dictionary(values ?? new Dictionary(), StringComparer.OrdinalIgnoreCase); - } - - public string GetString(string name) - { - string value; - return _values.TryGetValue(name, out value) && !string.IsNullOrWhiteSpace(value) ? value.Trim() : null; - } - - /// A "yyyy-MM-dd" date. is the name the user sees ("From Date"). - public DateTime? GetDate(string name, string label, bool required) - { - string text = GetString(name); - if (text == null) - { - if (required) - throw new ArgumentException("Please select " + label + "."); - return null; - } - DateTime value; - if (!DateTime.TryParseExact(text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out value)) - throw new ArgumentException("Please select a valid " + label + "."); - return value; - } - - public int? GetInt(string name, string label, bool required) - { - string text = GetString(name); - if (text == null) - { - if (required) - throw new ArgumentException("Please select " + label + "."); - return null; - } - int value; - if (!int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) - throw new ArgumentException("Please select a valid " + label + "."); - return value; - } - } - - /// - /// Base class of every virtual grid: what the grid is called, its columns and its limits. - /// A grid derives from one of the two kinds below, never from this class directly: - /// - /// - VirtualGridListDefinition<T> (the usual choice): LoadData calls an ordinary service method - /// that returns a List of business objects, exactly as screens always get their data. The common - /// service keeps that list in memory and pages, sorts, searches and exports it on the server. - /// - VirtualGridSqlDefinition (for very large lists): the grid gives a FROM/WHERE and SQL Server - /// does the paging, so only one window of rows is ever read. - /// - /// A new instance is created for every call, so a definition may keep per-call state. - /// - internal abstract class VirtualGridDefinition - { - private static readonly Regex Identifier = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); - - /// The key the screen sends (e.g. "discontinue-employee"). - public abstract string Key { get; } - - public abstract string Title { get; } - - public virtual string DefaultSortField => null; // null = first sortable column - public virtual string DefaultSortDir => "asc"; - - /// Self-service (ESS) logins are refused unless a grid opts in. - public virtual bool AllowSelfServiceUsers => false; - - public virtual int MaxPageSize => 500; - - /// Largest Excel export allowed; past it the user is asked to narrow the filters. - public virtual int ExportRowLimit => 200000; - - public virtual string ExcelFileName => Regex.Replace(Title, "[^A-Za-z0-9]+", "") + ".xlsx"; - - /// The grid's columns, in display order. May depend on configuration (e.g. department tier names). - public abstract List GetColumns(VirtualGridContext context); - - /// Whether the user can sort by this column. - internal virtual bool CanSort(VirtualGridColumn column) => column.Sortable; - - /// Whether the search box looks in this column. - internal virtual bool CanSearch(VirtualGridColumn column) => column.Searchable; - - internal static bool IsIdentifier(string name) => name != null && Identifier.IsMatch(name); - } - - /// - /// A grid filled from an ordinary service method that returns a list of business objects, the way - /// screens have always loaded their data: - /// - /// protected override List<LeaveEntry> LoadData(VirtualGridFilters filters, VirtualGridContext context) - /// => new LeaveEntryService().Get(...); - /// - /// Each column's Field is the name of a property of T (camelCase, matched case-insensitively); - /// Sql is not used. - /// - /// The list is loaded on Preview and kept in server memory for a few minutes, so scrolling, sorting - /// and searching do not call the service again. Suits lists up to tens of thousands of rows; for - /// larger ones use VirtualGridSqlDefinition. - /// - internal abstract class VirtualGridListDefinition : VirtualGridDefinition, IVirtualGridListSource where T : class - { - /// Reads/validates the filters and returns the rows, usually with one call to an existing service. - protected abstract List LoadData(VirtualGridFilters filters, VirtualGridContext context); - - /// Human-readable filter summary, printed at the top of the Excel file. Optional. - public virtual string DescribeFilters(VirtualGridFilters filters) => null; - - /// Calls LoadData and turns each object into a row holding just the grid's columns. - List> IVirtualGridListSource.LoadRows( - VirtualGridFilters filters, VirtualGridContext context, List columns) - { - var properties = new List>(); - foreach (VirtualGridColumn c in columns) - { - PropertyInfo property = FindProperty(c.Field); - if (property == null || !property.CanRead) - throw new InvalidOperationException("Virtual grid '" + Key + "': " + typeof(T).Name - + " has no readable property for column '" + c.Field + "'."); - properties.Add(new KeyValuePair(c.Field, property)); - } - - List items = LoadData(filters, context) ?? new List(); - var rows = new List>(items.Count); - foreach (T item in items) - { - if (item == null) - continue; - var row = new Dictionary(properties.Count, StringComparer.OrdinalIgnoreCase); - foreach (KeyValuePair p in properties) - row[p.Key] = p.Value.GetValue(item); - rows.Add(row); - } - return rows; - } - - /// - /// The public property named (case-insensitive). When a class hides a base - /// property with "new" (e.g. EmployeeDetailInfo.Status over the base Status), the most derived one — - /// the one the business object itself shows — wins. - /// - private static PropertyInfo FindProperty(string field) - { - PropertyInfo best = null; - int bestDepth = int.MaxValue; - foreach (PropertyInfo p in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)) - { - if (!string.Equals(p.Name, field, StringComparison.OrdinalIgnoreCase) || p.GetIndexParameters().Length > 0) - continue; - int depth = 0; - for (Type t = typeof(T); t != null && t != p.DeclaringType; t = t.BaseType) - depth++; - if (depth < bestDepth) - { - best = p; - bestDepth = depth; - } - } - return best; - } - } - - /// Non-generic view of a list grid, used by VirtualGridService. - internal interface IVirtualGridListSource - { - List> LoadRows(VirtualGridFilters filters, VirtualGridContext context, List columns); - string DescribeFilters(VirtualGridFilters filters); - } - - /// - /// A grid whose paging, sorting and searching run in SQL Server: for very large lists, where loading - /// every row (even once) is too slow. Each column's Sql is the expression it selects, sorts and searches - /// by; a column with Sql = null is filled in OnRowsLoaded. - /// - internal abstract class VirtualGridSqlDefinition : VirtualGridDefinition - { - /// A unique, stable SQL expression (usually the primary key) used as the last sort key so pages never overlap. - public abstract string TieBreakerSql { get; } - - /// - /// Extra values selected for OnRowsLoaded only, as "sqlExpression AS _alias". - /// Aliases must start with '_'; they are removed from the rows before they leave the server. - /// - public virtual IEnumerable HiddenSelect => Enumerable.Empty(); - - /// Reads/validates the filters and returns the FROM/WHERE the grid is built on. - public abstract VirtualGridSource BuildSource(VirtualGridFilters filters, VirtualGridContext context); - - /// Adds OR-conditions for code-computed values matching the search text. Optional. - public virtual void AddSearchExtras(string term, VirtualGridSearchExtras extras, VirtualGridContext context) - { - } - - /// Fills code-computed columns (and reformats values) on one loaded window of rows. Optional. - public virtual void OnRowsLoaded(List> rows, VirtualGridContext context) - { - } - - internal override bool CanSort(VirtualGridColumn column) => column.Sortable && !string.IsNullOrEmpty(column.Sql); - internal override bool CanSearch(VirtualGridColumn column) => column.Searchable && !string.IsNullOrEmpty(column.Sql); - } -} diff --git a/HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs b/HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs deleted file mode 100644 index e1357d4..0000000 --- a/HRM.DA/Service/VirtualGrid/VirtualGridRegistry.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace HRM.DA -{ - /// - /// Every virtual grid the application serves, by key. Adding a grid = one definition class in - /// Definitions/ and one line here. A key that is not listed here cannot be queried. - /// - internal static class VirtualGridRegistry - { - private static readonly Dictionary> Grids = - new Dictionary>(StringComparer.OrdinalIgnoreCase) - { - { DiscontinueEmployeeVirtualGrid.GridKey, () => new DiscontinueEmployeeVirtualGrid() }, - }; - - /// A fresh definition instance for one call. Throws ArgumentException for an unknown key. - internal static VirtualGridDefinition Create(string gridKey) - { - Func factory; - if (string.IsNullOrWhiteSpace(gridKey) || !Grids.TryGetValue(gridKey.Trim(), out factory)) - throw new ArgumentException("Unknown grid: " + gridKey); - return factory(); - } - } -} diff --git a/HRM.DA/Service/VirtualGrid/VirtualGridService.cs b/HRM.DA/Service/VirtualGrid/VirtualGridService.cs deleted file mode 100644 index a5a3863..0000000 --- a/HRM.DA/Service/VirtualGrid/VirtualGridService.cs +++ /dev/null @@ -1,591 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Text; -using ClosedXML.Excel; -using Ease.Core.DataAccess; -using Ease.Core.Model; -using Ease.Core.Utility; -using HRM.BO; -using Microsoft.Extensions.Caching.Memory; - -namespace HRM.DA -{ - /// - /// The one service behind every virtual-scrolling grid (api/virtualGrid). It looks the grid up in - /// and serves it in one of two ways: - /// - /// - List grid (): the grid's existing service returns a - /// List of business objects; that list is kept in server memory for a few minutes and every scroll, - /// sort, search and Excel export is answered from it. - /// - SQL grid (): SQL Server counts, sorts, searches and returns - /// one OFFSET/FETCH window per request (VirtualGridDA). - /// - /// Shared work for both: access check, column validation, paging limits, sort whitelist, the Excel file. - /// - public class VirtualGridService : ServiceTemplate, IVirtualGridService - { - private const int ExportCommandTimeoutSeconds = 600; - private const int MaxSearchLength = 200; - private const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; - - // List grids: loaded lists, per user + grid + filters. Size = rows, so at most ~1,000,000 rows are - // held in total; the least recently used lists are dropped first. - private static readonly MemoryCache ListCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000000 }); - private static readonly TimeSpan ListCacheSliding = TimeSpan.FromMinutes(10); - private static readonly TimeSpan ListCacheAbsolute = TimeSpan.FromMinutes(30); - - #region Public API - - public VirtualGridLayout GetLayout(string gridKey, VirtualGridContext context) - { - VirtualGridDefinition grid = Open(gridKey, context); - List columns = GetValidatedColumns(grid, context); - VirtualGridColumn defaultSort = DefaultSortColumn(grid, columns); - - var layout = new VirtualGridLayout - { - GridKey = grid.Key, - Title = grid.Title, - DefaultSortField = defaultSort?.Field, - DefaultSortDir = NormalizeDir(grid.DefaultSortDir), - MaxPageSize = grid.MaxPageSize, - }; - layout.Columns.AddRange(columns.Select(c => new VirtualGridColumnInfo - { - Field = c.Field, - Title = c.Title, - Width = c.Width, - Type = c.Type, - Sortable = grid.CanSort(c), - })); - return layout; - } - - public VirtualGridPage GetPage(VirtualGridRequest request, VirtualGridContext context) - { - if (request == null) - throw new ArgumentException("Invalid request."); - VirtualGridDefinition grid = Open(request.GridKey, context); - List columns = GetValidatedColumns(grid, context); - int skip = Math.Max(0, request.Skip); - int take = Math.Min(Math.Max(1, request.Take), grid.MaxPageSize); - - var listSource = grid as IVirtualGridListSource; - if (listSource != null) - { - CachedList list = GetList(grid, listSource, columns, request, context); - List matches = Search(list, request.Search); - List ordered = Sort(grid, columns, list, matches, request); - return new VirtualGridPage - { - Total = ordered.Count, - TotalWithoutSearch = list.Rows.Count, - Data = ordered.Skip(skip).Take(take).Select(i => list.Rows[i]).ToList(), - }; - } - - var sqlGrid = (VirtualGridSqlDefinition)grid; - VirtualGridQuery query = BuildQuery(sqlGrid, columns, request, context); - var page = new VirtualGridPage(); - TransactionContext tc = null; - try - { - tc = TransactionContext.Begin(); - page.Total = VirtualGridDA.Count(tc, query, true); - page.TotalWithoutSearch = query.SearchPattern == null ? page.Total : VirtualGridDA.Count(tc, query, false); - if (page.Total > skip) - page.Data = ReadRows(VirtualGridDA.GetRows(tc, query, skip, take)); - tc.End(); - } - catch (Exception e) - { - #region Handle Exception - if (tc != null) - tc.HandleError(); - ExceptionLog.Write(e); - throw new ServiceException(e.Message, e); - #endregion - } - - // After the transaction: definitions may call other services that open their own. - FinishRows(sqlGrid, page.Data, context); - return page; - } - - public VirtualGridFile ExportExcel(VirtualGridExportRequest request, VirtualGridContext context) - { - if (request == null) - throw new ArgumentException("Invalid request."); - VirtualGridDefinition grid = Open(request.GridKey, context); - List columns = GetValidatedColumns(grid, context); - List exportColumns = ResolveExportColumns(columns, request.Fields); - - List> rows; - string filterDescription; - - var listSource = grid as IVirtualGridListSource; - if (listSource != null) - { - CachedList list = GetList(grid, listSource, columns, request, context); - List ordered = Sort(grid, columns, list, Search(list, request.Search), request); - CheckExportLimit(grid, ordered.Count); - rows = ordered.Select(i => list.Rows[i]).ToList(); - filterDescription = listSource.DescribeFilters(new VirtualGridFilters(request.Filters)); - } - else - { - var sqlGrid = (VirtualGridSqlDefinition)grid; - VirtualGridQuery query = BuildQuery(sqlGrid, columns, request, context); - TransactionContext tc = null; - try - { - tc = TransactionContext.Begin(); - tc.CommandTimeOut = ExportCommandTimeoutSeconds; - CheckExportLimit(grid, VirtualGridDA.Count(tc, query, true)); - rows = ReadRows(VirtualGridDA.GetRows(tc, query, 0, null)); - tc.End(); - } - catch (ArgumentException) - { - if (tc != null) - tc.HandleError(); - throw; - } - catch (Exception e) - { - #region Handle Exception - if (tc != null) - tc.HandleError(); - ExceptionLog.Write(e); - throw new ServiceException(e.Message, e); - #endregion - } - FinishRows(sqlGrid, rows, context); - filterDescription = query.Source.FilterDescription; - } - - return new VirtualGridFile - { - FileName = grid.ExcelFileName, - ContentType = ExcelContentType, - Content = BuildWorkbook(grid, exportColumns, rows, filterDescription, request.Search), - }; - } - - #endregion - - #region Shared helpers - - /// Finds the grid and checks the user may use it. Unknown key → ArgumentException. - private static VirtualGridDefinition Open(string gridKey, VirtualGridContext context) - { - if (context == null) - throw new UnauthorizedAccessException("Not signed in."); - VirtualGridDefinition grid = VirtualGridRegistry.Create(gridKey); - if (!(grid is IVirtualGridListSource) && !(grid is VirtualGridSqlDefinition)) - throw new InvalidOperationException("Virtual grid '" + grid.Key - + "' must derive from VirtualGridListDefinition or VirtualGridSqlDefinition."); - if (context.UserType == EnumUserType.Employee && !grid.AllowSelfServiceUsers) - throw new UnauthorizedAccessException("You do not have access to " + grid.Title + "."); - return grid; - } - - /// The definition's columns, checked so a bad definition fails loudly. - private static List GetValidatedColumns(VirtualGridDefinition grid, VirtualGridContext context) - { - List columns = grid.GetColumns(context) ?? new List(); - var seen = new HashSet(StringComparer.OrdinalIgnoreCase); - foreach (VirtualGridColumn c in columns) - { - if (!VirtualGridDefinition.IsIdentifier(c.Field) || c.Field.StartsWith("_") || !seen.Add(c.Field)) - throw new InvalidOperationException("Virtual grid '" + grid.Key + "' has an invalid or duplicate column field: " + c.Field); - } - if (columns.Count == 0) - throw new InvalidOperationException("Virtual grid '" + grid.Key + "' has no columns."); - return columns; - } - - private static VirtualGridColumn DefaultSortColumn(VirtualGridDefinition grid, List columns) - { - VirtualGridColumn byName = columns.FirstOrDefault(c => grid.CanSort(c) - && string.Equals(c.Field, grid.DefaultSortField, StringComparison.OrdinalIgnoreCase)); - return byName ?? columns.FirstOrDefault(c => grid.CanSort(c)); - } - - /// The column to sort by and its direction: a sortable column the user picked, else the grid's default. - private static VirtualGridColumn ResolveSort(VirtualGridDefinition grid, List columns, - VirtualGridRequest request, out bool descending) - { - VirtualGridColumn picked = columns.FirstOrDefault(c => grid.CanSort(c) - && string.Equals(c.Field, request.SortField, StringComparison.OrdinalIgnoreCase)); - descending = NormalizeDir(picked != null ? request.SortDir : grid.DefaultSortDir) == "desc"; - return picked ?? DefaultSortColumn(grid, columns); - } - - private static string NormalizeDir(string dir) => - string.Equals(dir, "desc", StringComparison.OrdinalIgnoreCase) ? "desc" : "asc"; - - private static string CleanSearch(string search) - { - string term = (search ?? string.Empty).Trim(); - return term.Length > MaxSearchLength ? term.Substring(0, MaxSearchLength) : term; - } - - private static void CheckExportLimit(VirtualGridDefinition grid, int total) - { - if (total > grid.ExportRowLimit) - throw new ArgumentException("There are " + total.ToString("N0", CultureInfo.InvariantCulture) - + " rows; Excel export is limited to " + grid.ExportRowLimit.ToString("N0", CultureInfo.InvariantCulture) - + ". Please narrow the filters or the search."); - } - - /// - /// A value as the screen shows it (dates "dd MMM yyyy", money "1,234.56"), so the search box finds - /// what the user sees. Mirrors the formatting in <app-virtual-grid>. - /// - private static string DisplayText(object value, EnumVirtualGridColumnType type) - { - if (value == null) - return string.Empty; - switch (type) - { - case EnumVirtualGridColumnType.Date: - if (value is DateTime d) return d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); - break; - case EnumVirtualGridColumnType.DateTime: - if (value is DateTime dt) return dt.ToString("dd MMM yyyy HH:mm", CultureInfo.InvariantCulture); - break; - case EnumVirtualGridColumnType.Money: - if (value is IConvertible && !(value is string)) - return Convert.ToDecimal(value, CultureInfo.InvariantCulture).ToString("N2", CultureInfo.InvariantCulture); - break; - } - return Convert.ToString(value, CultureInfo.InvariantCulture); - } - - #endregion - - #region List grids (existing service + in-memory paging) - - /// A loaded list plus, per row, the text the search box looks in. - private sealed class CachedList - { - public List> Rows; - public List SearchText; - } - - /// - /// The grid's list for this user and these filters: from the cache, or — on Preview (Refresh) or - /// when the cache has expired — freshly from the grid's service. - /// - private static CachedList GetList(VirtualGridDefinition grid, IVirtualGridListSource source, - List columns, VirtualGridRequest request, VirtualGridContext context) - { - string key = CacheKey(grid, columns, request, context); - CachedList list; - if (!request.Refresh && ListCache.TryGetValue(key, out list)) - return list; - - List> rows = source.LoadRows(new VirtualGridFilters(request.Filters), context, columns); - - List searchable = columns.Where(c => grid.CanSearch(c)).ToList(); - var searchText = new List(rows.Count); - var sb = new StringBuilder(); - foreach (Dictionary row in rows) - { - sb.Clear(); - foreach (VirtualGridColumn c in searchable) - { - object value; - row.TryGetValue(c.Field, out value); - sb.Append(DisplayText(value, c.Type)).Append('\u0001'); // separator: no match across two columns - } - searchText.Add(sb.ToString()); - } - - list = new CachedList { Rows = rows, SearchText = searchText }; - ListCache.Set(key, list, new MemoryCacheEntryOptions - { - Size = Math.Max(1, rows.Count), - SlidingExpiration = ListCacheSliding, - AbsoluteExpirationRelativeToNow = ListCacheAbsolute, - }); - return list; - } - - /// One list per user, payroll type, grid and filter values (and column set, in case configuration changes). - private static string CacheKey(VirtualGridDefinition grid, List columns, - VirtualGridRequest request, VirtualGridContext context) - { - var sb = new StringBuilder(); - sb.Append(grid.Key.ToLowerInvariant()) - .Append('|').Append(context.UserID) - .Append('|').Append(context.PayrollTypeID) - .Append('|').Append((int)context.UserType) - .Append('|').Append(string.Join(",", columns.Select(c => c.Field))); - foreach (KeyValuePair f in (request.Filters ?? new Dictionary()) - .OrderBy(f => f.Key, StringComparer.OrdinalIgnoreCase)) - sb.Append('|').Append(f.Key.ToLowerInvariant()).Append('=').Append(f.Value); - return sb.ToString(); - } - - /// Indexes of the rows whose shown text contains the search (case-insensitive); all rows when there is none. - private static List Search(CachedList list, string search) - { - string term = CleanSearch(search); - var result = new List(term.Length == 0 ? list.Rows.Count : 64); - for (int i = 0; i < list.Rows.Count; i++) - { - if (term.Length == 0 || list.SearchText[i].IndexOf(term, StringComparison.OrdinalIgnoreCase) >= 0) - result.Add(i); - } - return result; - } - - /// Sorts row indexes by the chosen column (stable, so equal values keep the service's order). - private static List Sort(VirtualGridDefinition grid, List columns, CachedList list, - List indexes, VirtualGridRequest request) - { - bool descending; - VirtualGridColumn sortColumn = ResolveSort(grid, columns, request, out descending); - if (sortColumn == null) - return indexes; - - string field = sortColumn.Field; - Func keyOf = i => - { - object value; - list.Rows[i].TryGetValue(field, out value); - return value; - }; - return (descending - ? indexes.OrderByDescending(keyOf, ValueComparer.Instance) - : indexes.OrderBy(keyOf, ValueComparer.Instance)) - .ToList(); - } - - /// Compares cell values: empty first, numbers as numbers, dates as dates, text case-insensitively. - private sealed class ValueComparer : IComparer - { - public static readonly ValueComparer Instance = new ValueComparer(); - - public int Compare(object x, object y) - { - bool xEmpty = x == null || (x is string xs && xs.Length == 0); - bool yEmpty = y == null || (y is string ys && ys.Length == 0); - if (xEmpty || yEmpty) - return xEmpty == yEmpty ? 0 : (xEmpty ? -1 : 1); - - if (x is string a && y is string b) - return StringComparer.InvariantCultureIgnoreCase.Compare(a, b); - if (IsNumber(x) && IsNumber(y)) - return Convert.ToDecimal(x, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(y, CultureInfo.InvariantCulture)); - if (x.GetType() == y.GetType() && x is IComparable comparable) - return comparable.CompareTo(y); - return StringComparer.InvariantCultureIgnoreCase.Compare( - Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture)); - } - - private static bool IsNumber(object v) => - v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; - } - - #endregion - - #region SQL grids (VirtualGridDA) - - private static VirtualGridQuery BuildQuery(VirtualGridSqlDefinition grid, List columns, - VirtualGridRequest request, VirtualGridContext context) - { - VirtualGridSource source = grid.BuildSource(new VirtualGridFilters(request.Filters), context); - if (source == null || string.IsNullOrWhiteSpace(source.FromWhere)) - throw new InvalidOperationException("Virtual grid '" + grid.Key + "' returned no source."); - - var query = new VirtualGridQuery { Source = source, TieBreaker = grid.TieBreakerSql }; - - foreach (VirtualGridColumn c in columns.Where(c => !string.IsNullOrEmpty(c.Sql))) - query.Select.Add(c.Sql + " AS [" + c.Field + "]"); - foreach (string hidden in grid.HiddenSelect) - query.Select.Add(hidden); - - // Sort: only a sortable column of this grid; anything else falls back to the default. - bool descending; - VirtualGridColumn sortColumn = ResolveSort(grid, columns, request, out descending); - query.OrderBy = (sortColumn != null ? sortColumn.Sql : grid.TieBreakerSql) + (descending ? " DESC" : " ASC"); - - // Search: the user's text is a parameter; only LIKE wildcards are escaped. - string term = CleanSearch(request.Search); - if (term.Length > 0) - { - query.SearchPattern = "%" + term.Replace(@"\", @"\\").Replace("%", @"\%").Replace("_", @"\_").Replace("[", @"\[") + "%"; - foreach (VirtualGridColumn c in columns.Where(c => grid.CanSearch(c))) - query.SearchColumns.Add(new KeyValuePair(c.Sql, c.Type)); - var extras = new VirtualGridSearchExtras(); - grid.AddSearchExtras(term, extras, context); - query.SearchPredicates.AddRange(extras.Predicates); - } - - return query; - } - - private static List> ReadRows(IDataReader reader) - { - var rows = new List>(); - try - { - int count = reader.FieldCount; - var names = new string[count]; - for (int i = 0; i < count; i++) - names[i] = reader.GetName(i); - while (reader.Read()) - { - var row = new Dictionary(count, StringComparer.OrdinalIgnoreCase); - for (int i = 0; i < count; i++) - row[names[i]] = reader.IsDBNull(i) ? null : reader.GetValue(i); - rows.Add(row); - } - } - finally - { - reader.Close(); - } - return rows; - } - - /// Lets the definition fill computed columns, then drops the hidden "_" helper values. - private static void FinishRows(VirtualGridSqlDefinition grid, List> rows, VirtualGridContext context) - { - if (rows.Count == 0) - return; - grid.OnRowsLoaded(rows, context); - foreach (Dictionary row in rows) - { - foreach (string key in row.Keys.Where(k => k.StartsWith("_")).ToList()) - row.Remove(key); - } - } - - #endregion - - #region Excel - - /// The requested fields that are columns of this grid, in the requested order (all columns when none are given). - private static List ResolveExportColumns(List columns, List fields) - { - if (fields == null || fields.Count == 0) - return columns; - var byField = columns.ToDictionary(c => c.Field, StringComparer.OrdinalIgnoreCase); - var result = new List(); - var seen = new HashSet(StringComparer.OrdinalIgnoreCase); - foreach (string field in fields) - { - VirtualGridColumn c; - if (field != null && byField.TryGetValue(field.Trim(), out c) && seen.Add(c.Field)) - result.Add(c); - } - if (result.Count == 0) - throw new ArgumentException("No columns to export."); - return result; - } - - private static byte[] BuildWorkbook(VirtualGridDefinition grid, List columns, - List> rows, string filterDescription, string searchText) - { - const int headerRow = 5; - using (var workbook = new XLWorkbook()) - { - string sheetName = grid.Title.Length > 31 ? grid.Title.Substring(0, 31) : grid.Title; - foreach (char ch in new[] { ':', '\\', '/', '?', '*', '[', ']' }) - sheetName = sheetName.Replace(ch, ' '); - IXLWorksheet ws = workbook.Worksheets.Add(sheetName); - - ws.Cell(1, 1).Value = grid.Title; - ws.Cell(1, 1).Style.Font.Bold = true; - ws.Cell(1, 1).Style.Font.FontSize = 14; - string line2 = filterDescription ?? string.Empty; - if (!string.IsNullOrWhiteSpace(searchText)) - line2 += (line2.Length > 0 ? " | " : "") + "Search: " + searchText.Trim(); - ws.Cell(2, 1).Value = line2; - ws.Cell(3, 1).Value = "Total: " + rows.Count.ToString("N0", CultureInfo.InvariantCulture); - - for (int c = 0; c < columns.Count; c++) - { - ws.Cell(headerRow, c + 1).Value = columns[c].Title; - switch (columns[c].Type) - { - case EnumVirtualGridColumnType.Date: - ws.Column(c + 1).Style.DateFormat.Format = "dd MMM yyyy"; - break; - case EnumVirtualGridColumnType.DateTime: - ws.Column(c + 1).Style.DateFormat.Format = "dd MMM yyyy HH:mm"; - break; - case EnumVirtualGridColumnType.Money: - ws.Column(c + 1).Style.NumberFormat.Format = "#,##0.00"; - break; - } - } - IXLRange header = ws.Range(headerRow, 1, headerRow, columns.Count); - header.Style.Font.Bold = true; - header.Style.Fill.BackgroundColor = XLColor.FromHtml("#E7ECF3"); - header.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; - header.Style.Border.InsideBorder = XLBorderStyleValues.Thin; - - int r = headerRow + 1; - foreach (Dictionary row in rows) - { - for (int c = 0; c < columns.Count; c++) - { - object value; - if (!row.TryGetValue(columns[c].Field, out value) || value == null) - continue; - SetCell(ws.Cell(r, c + 1), value, columns[c].Type); - } - r++; - } - - int lastRow = Math.Max(headerRow, r - 1); - ws.Range(headerRow, 1, lastRow, columns.Count).SetAutoFilter(); - ws.SheetView.FreezeRows(headerRow); - // Measure the header and the first rows only: measuring every row is slow on large files. - ws.Columns(1, columns.Count).AdjustToContents(headerRow, Math.Min(lastRow, headerRow + 300), 8, 60); - - using (var stream = new MemoryStream()) - { - workbook.SaveAs(stream); - return stream.ToArray(); - } - } - } - - private static void SetCell(IXLCell cell, object value, EnumVirtualGridColumnType type) - { - switch (type) - { - case EnumVirtualGridColumnType.Date: - case EnumVirtualGridColumnType.DateTime: - if (value is DateTime dt) { cell.Value = dt; return; } - break; - case EnumVirtualGridColumnType.Money: - case EnumVirtualGridColumnType.Number: - if (value is IConvertible && !(value is string) && !(value is DateTime)) - { - cell.Value = Convert.ToDecimal(value, CultureInfo.InvariantCulture); - return; - } - break; - } - cell.Value = Convert.ToString(value, CultureInfo.InvariantCulture); // text: no formulas, keeps leading zeros - } - - #endregion - - protected override T CreateObject(DataReader dr) - { - throw new NotSupportedException("VirtualGridService reads rows generically."); - } - } -} diff --git a/HRM.DA/Service/VirtualPaging/README.md b/HRM.DA/Service/VirtualPaging/README.md new file mode 100644 index 0000000..bde5940 --- /dev/null +++ b/HRM.DA/Service/VirtualPaging/README.md @@ -0,0 +1,77 @@ +# Virtual paging: large lists with virtual scrolling + +Data loads **the usual way**: DA query → service returns `List` → controller endpoint. +Only two common pieces are added: + +| Piece | File | What it does | +|---|---|---| +| `VirtualPaging.Page(...)` | `HRM.DA/Service/VirtualPaging/VirtualPaging.cs` | Keeps the list in memory for a few minutes, then search, sort, 100 rows at a time and the total count | +| `` | `ClientApp/src/app/custom-components/virtual-grid/` (`VirtualGridModule`) | Virtual scrolling, search box, total, Export to Excel | + +`VirtualPageRequest` / `VirtualPageResult` (`HRM.BO/VirtualPaging/VirtualPaging.cs`) are shared by every +page, so a page never needs its own request class. + +## A new page: 2 steps + +**1. Endpoint** in the module's own controller, calling the existing service: + +```csharp +[HttpPost("getDiscontinueEmployeePage")] +public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) +{ + try + { + CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + DateTime fromDate = request.GetDate("fromDate", "From Date"); // the page's own filters + DateTime toDate = request.GetDate("toDate", "To Date"); + + return Ok(VirtualPaging.Page(request, currentUser, + () => _EmployeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, currentUser.PayrollTypeID ?? 0))); + } + catch (Exception ex) + { + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); + } +} +``` + +Read filters with `request.GetDate(name)`, `request.GetInt(name)` or `request.GetString(name)` (null when not sent). + +**2. Screen**: import `VirtualGridModule`, then: + +```html + +``` +```ts +columns: VirtualGridColumn[] = [ + { field: 'employeeNo', title: 'Employee No', width: 110 }, + { field: 'joiningDate', title: 'Joining Date', type: 'date' }, + { field: 'grossSalary', title: 'Gross Salary', type: 'money' }, +]; + +preview() { + this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); // any filters; Dates are sent as yyyy-MM-dd +} +``` + +`field` is the property name of the BO (camelCase). Column `type`: `text` (default), `number`, +`money` (1,234.56), `date` (dd MMM yyyy), `datetime`. `sortable: false` turns sorting off for a column. + +That is all. No registry, no extra service, no extra Angular service. + +## How it runs + +1. **Preview** → `grid.load(filters)` → the endpoint with `refresh: true` → the service is called once, + the list is kept in server memory (per user + endpoint + filters, 10 min since last use, 30 min at + most) → the first 100 rows and the total go back. +2. **Scroll / sort / search** → same endpoint, `refresh: false` → answered from the kept list. +3. **Export to Excel** → same endpoint with `take: 0` → every matching row → Kendo builds the `.xlsx`. + +The browser shows ~100 rows at a time, however long the list is. + +## Rules + +- The service must not run one query per row; load lookups once (see `GetDiscontinuedEmployeeDetails`). +- Search looks in the text the grid shows (dates `dd MMM yyyy`, money `1,234.56`). +- Authorization is menu-driven in this app: put any extra check the page needs in its endpoint. diff --git a/HRM.DA/Service/VirtualPaging/VirtualPaging.cs b/HRM.DA/Service/VirtualPaging/VirtualPaging.cs new file mode 100644 index 0000000..e9f7f1e --- /dev/null +++ b/HRM.DA/Service/VirtualPaging/VirtualPaging.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using HRM.BO; +using Microsoft.Extensions.Caching.Memory; + +namespace HRM.DA +{ + /// + /// Server side of <app-virtual-grid>: turns any List<T> into one window of rows (search, sort, + /// skip/take, total count). A page endpoint calls it with its existing service method: + /// + /// [HttpPost("getDiscontinueEmployeePage")] + /// public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) + /// { + /// CurrentUser user = CurrentUser.GetCurrentUser(HttpContext.User); + /// DateTime fromDate = request.GetDate("fromDate", "From Date"); + /// DateTime toDate = request.GetDate("toDate", "To Date"); + /// return Ok(VirtualPaging.Page(request, user, + /// () => _employeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, user.PayrollTypeID.Value))); + /// } + /// + /// The list is loaded on Preview (request.Refresh) and kept in memory for a few minutes, per user, + /// endpoint and filters, so scrolling, sorting, searching and Export to Excel do not call the + /// service again. See README.md next to this file. + /// + public static class VirtualPaging + { + private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000000 }); // max rows kept, all users together + private static readonly TimeSpan KeepFor = TimeSpan.FromMinutes(10); // since last use + private static readonly TimeSpan KeepAtMost = TimeSpan.FromMinutes(30); // since loaded + private const int MaxRowsPerPage = 1000; + + public static VirtualPageResult Page(VirtualPageRequest request, CurrentUser user, Func> loadData, + [CallerFilePath] string callerFile = "", [CallerMemberName] string callerName = "") where T : class + { + if (request == null) + throw new ArgumentException("Invalid request."); + + // 1. The list: from memory, or from the service on Preview / when it has expired. + string key = CacheKey(request, user, callerFile + "|" + callerName + "|" + typeof(T).FullName); + KeptList kept; + if (request.Refresh || !Cache.TryGetValue(key, out kept)) + { + kept = new KeptList(loadData() ?? new List()); + Cache.Set(key, kept, new MemoryCacheEntryOptions + { + Size = Math.Max(1, kept.Items.Count), + SlidingExpiration = KeepFor, + AbsoluteExpirationRelativeToNow = KeepAtMost, + }); + } + + List fields = GetFields(request.Fields); + IEnumerable rows = Enumerable.Range(0, kept.Items.Count); + + // 2. Search: the text the grid shows, in any of the grid's columns. + string search = (request.Search ?? "").Trim(); + if (search.Length > 0) + { + List texts = kept.SearchTexts(fields); + rows = rows.Where(i => texts[i].IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0); + } + List matched = rows.ToList(); + + // 3. Sort: only by one of the grid's columns. + PropertyInfo sortBy = fields.FirstOrDefault(p => string.Equals(p.Name, request.SortField, StringComparison.OrdinalIgnoreCase)); + if (sortBy != null) + { + bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); + matched = desc + ? matched.OrderByDescending(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList() + : matched.OrderBy(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList(); + } + + // 4. The window: Take 0 = every row (Export to Excel). + int skip = Math.Max(0, request.Skip); + int take = request.Take <= 0 ? matched.Count : Math.Min(request.Take, MaxRowsPerPage); + + var result = new VirtualPageResult { Total = matched.Count, TotalWithoutSearch = kept.Items.Count }; + foreach (int i in matched.Skip(skip).Take(take)) + { + var row = new Dictionary(fields.Count); + foreach (PropertyInfo p in fields) + row[CamelCase(p.Name)] = p.GetValue(kept.Items[i]); + result.Data.Add(row); + } + return result; + } + + #region Helpers + + /// A loaded list, plus the search text of each row (worked out once, on the first search). + private sealed class KeptList + { + private readonly ConcurrentDictionary> _searchTexts = new ConcurrentDictionary>(); + + public KeptList(List items) + { + Items = items.Where(x => x != null).ToList(); + } + + public List Items { get; } + + public List SearchTexts(List fields) + { + string key = string.Join(",", fields.Select(p => p.Name)); + return _searchTexts.GetOrAdd(key, _ => Items + .Select(item => string.Join("\u0001", fields.Select(p => DisplayText(p.GetValue(item))))) + .ToList()); + } + } + + /// The requested fields that are readable properties of T (unknown names are ignored). None requested = all. + private static List GetFields(List names) + { + // When a class hides a base property with "new", keep the most derived one. + var properties = new Dictionary(StringComparer.OrdinalIgnoreCase); + for (Type t = typeof(T); t != null && t != typeof(object); t = t.BaseType) + { + foreach (PropertyInfo p in t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) + { + if (p.CanRead && p.GetIndexParameters().Length == 0 && !properties.ContainsKey(p.Name)) + properties[p.Name] = p; + } + } + + if (names == null || names.Count == 0) + return properties.Values.ToList(); + + var result = new List(); + foreach (string name in names.Distinct(StringComparer.OrdinalIgnoreCase)) + { + PropertyInfo p; + if (name != null && properties.TryGetValue(name, out p)) + result.Add(p); + } + return result; + } + + /// The text the grid shows for a value: dates "dd MMM yyyy", decimals "1,234.56". + private static string DisplayText(object value) + { + if (value == null) + return string.Empty; + if (value is DateTime d) + return d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); + if (value is decimal || value is double || value is float) + { + decimal number = Convert.ToDecimal(value, CultureInfo.InvariantCulture); + return number.ToString("N2", CultureInfo.InvariantCulture) + "\u0001" + number.ToString(CultureInfo.InvariantCulture); + } + if (value is Enum) + return value + "\u0001" + Convert.ToInt32(value, CultureInfo.InvariantCulture); + return Convert.ToString(value, CultureInfo.InvariantCulture); + } + + private static string CacheKey(VirtualPageRequest request, CurrentUser user, string endpoint) + { + string filters = string.Join("&", (request.Filters ?? new Dictionary()) + .OrderBy(f => f.Key, StringComparer.OrdinalIgnoreCase) + .Select(f => f.Key.ToLowerInvariant() + "=" + f.Value)); + return endpoint + "|" + user?.UserID + "|" + user?.PayrollTypeID + "|" + filters; + } + + private static string CamelCase(string name) => + string.IsNullOrEmpty(name) ? name : char.ToLowerInvariant(name[0]) + name.Substring(1); + + /// Empty values first; numbers, dates and text compared as such (text ignoring case). + private sealed class ValueComparer : IComparer + { + public static readonly ValueComparer Instance = new ValueComparer(); + + public int Compare(object x, object y) + { + bool xEmpty = x == null || (x is string xs && xs.Length == 0); + bool yEmpty = y == null || (y is string ys && ys.Length == 0); + if (xEmpty || yEmpty) + return xEmpty == yEmpty ? 0 : (xEmpty ? -1 : 1); + if (x is string a && y is string b) + return StringComparer.InvariantCultureIgnoreCase.Compare(a, b); + if (IsNumber(x) && IsNumber(y)) + return Convert.ToDecimal(x, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(y, CultureInfo.InvariantCulture)); + if (x.GetType() == y.GetType() && x is IComparable comparable) + return comparable.CompareTo(y); + return StringComparer.InvariantCultureIgnoreCase.Compare( + Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture)); + } + + private static bool IsNumber(object v) => + v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; + } + + #endregion + } +} diff --git a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts index 36e6f86..9b53a8f 100644 --- a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts +++ b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts @@ -1,54 +1,34 @@ /** - * Common virtual-scrolling grid (api/virtualGrid) — matches HRM.BO/VirtualGrid/VirtualGrid.cs. - * Screens use (custom-components/virtual-grid) rather than these directly. + * (custom-components/virtual-grid) and its server side + * (HRM.BO/VirtualPaging/VirtualPaging.cs, HRM.DA/Service/VirtualPaging/VirtualPaging.cs). */ -/** EnumVirtualGridColumnType on the server. */ -export enum EnumVirtualGridColumnType { - Text = 1, - Number = 2, - Money = 3, - Date = 4, - DateTime = 5 -} +/** How a column's values are shown (and exported to Excel). */ +export type VirtualGridColumnType = 'text' | 'number' | 'money' | 'date' | 'datetime'; -export interface VirtualGridColumnInfo { +/** One grid column. field = the property name of the server object, camelCase. */ +export interface VirtualGridColumn { field: string; title: string; - width: number; - type: EnumVirtualGridColumnType; - sortable: boolean; + width?: number; + type?: VirtualGridColumnType; // default 'text' + sortable?: boolean; // default true } -export interface VirtualGridLayout { - gridKey: string; - title: string; - defaultSortField: string; - defaultSortDir: 'asc' | 'desc'; - maxPageSize: number; - columns: VirtualGridColumnInfo[]; -} - -/** Screen-specific filters, e.g. { fromDate: '2025-01-01', toDate: '2025-12-31' }. Dates as 'yyyy-MM-dd'. */ -export type VirtualGridFilters = { [name: string]: string }; - -export interface VirtualGridRequest { - gridKey: string; - filters: VirtualGridFilters; +/** What the grid posts to the page endpoint (VirtualPageRequest on the server). */ +export interface VirtualPageRequest { + filters: { [name: string]: string }; + fields: string[]; skip: number; - take: number; + take: number; // 0 = all rows (Export to Excel) search?: string; sortField?: string; sortDir?: 'asc' | 'desc'; - /** True on Preview: reload the data instead of using the copy the server keeps for scrolling. */ - refresh?: boolean; + refresh: boolean; // true on Preview } -export interface VirtualGridExportRequest extends VirtualGridRequest { - fields: string[]; -} - -export interface VirtualGridPage { +/** What the page endpoint returns (VirtualPageResult on the server). */ +export interface VirtualPageResult { total: number; totalWithoutSearch: number; data: { [field: string]: any }[]; diff --git a/HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts b/HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts deleted file mode 100644 index 844a1b9..0000000 --- a/HRM.UI/ClientApp/src/app/_services/common/virtual-grid.service.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs'; -import { ApiService } from 'src/app/app.api.service'; -import { - VirtualGridExportRequest, - VirtualGridLayout, - VirtualGridPage, - VirtualGridRequest -} from 'src/app/_models/common/virtual-grid'; - -/** The one API for every virtual-scrolling grid (api/virtualGrid). */ -@Injectable({ - providedIn: 'root' -}) -export class VirtualGridService { - constructor(private apiService: ApiService) { - } - - getLayout(gridKey: string): Observable { - return this.apiService.httpGet('/virtualGrid/layout/' + encodeURIComponent(gridKey)); - } - - getPage(request: VirtualGridRequest): Observable { - return this.apiService.httpPost('/virtualGrid/page', request); - } - - exportExcel(request: VirtualGridExportRequest): Observable { - return this.apiService.httpDownloadFile('/virtualGrid/export-excel', request); - } -} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html index eed4e8e..5609f05 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html @@ -1,7 +1,4 @@ -@if (layoutError) { -
{{ layoutError }}
-} -@if (started && layout) { +@if (started) { } - @for (column of layout.columns; track column.field) { - + + @for (column of columns; track column.field) { + } + + + + @for (column of columns; track column.field) { + + + } + + {{ noRecordsText }} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts index 29a6536..3ded9a0 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts @@ -1,25 +1,31 @@ import { - ChangeDetectionStrategy, Component, EventEmitter, Inject, Input, LOCALE_ID, OnDestroy, OnInit, Optional, Output + ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, OnInit, Optional, Output, ViewChild } from '@angular/core'; import { formatDate } from '@angular/common'; import { of, Subject, Subscription } from 'rxjs'; import { catchError, debounceTime, distinctUntilChanged, finalize, switchMap } from 'rxjs/operators'; -import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; +import { ExcelExportData } from '@progress/kendo-angular-excel-export'; +import { GridComponent, GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; import { SortDescriptor } from '@progress/kendo-data-query'; -import { saveAs } from 'file-saver'; 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 { VirtualGridService } from 'src/app/_services/common/virtual-grid.service'; import { - EnumVirtualGridColumnType, - VirtualGridFilters, - VirtualGridLayout, + VirtualGridColumn, VirtualGridLoadedEvent, - VirtualGridPage, - VirtualGridRequest + VirtualPageRequest, + VirtualPageResult } from 'src/app/_models/common/virtual-grid'; +/** + * Common virtual-scrolling grid. The page gives it an endpoint and the columns: + * + * + * this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); + * + * The grid loads 100 rows at a time while the user scrolls; search, sort and the total count are done + * on the server (VirtualPaging.Page). Export to Excel downloads every matching row. + */ @Component({ selector: 'app-virtual-grid', templateUrl: './virtual-grid.component.html', @@ -28,26 +34,25 @@ import { standalone: false, }) export class VirtualGridComponent implements OnInit, OnDestroy { - /** Server grid key (VirtualGridRegistry). Required. */ - @Input() gridKey: string; - /** Grid height in px. */ + /** The page endpoint, e.g. '/Employee/getDiscontinueEmployeePage'. Required. */ + @Input() url: string; + /** The columns. field = property name of the server object (camelCase). Required. */ + @Input() columns: VirtualGridColumn[] = []; @Input() height = 600; - /** Rows fetched per window. */ @Input() pageSize = 100; - /** Minimum (estimated) row height in px. Rows with wrapped text grow; Kendo measures the real heights while scrolling. */ + /** Minimum row height; rows with wrapped text grow. */ @Input() rowHeight = 36; @Input() searchPlaceholder = 'Search in all columns...'; @Input() noRecordsText = 'No records found.'; - /** Excel file name; defaults to the grid title. */ - @Input() exportFileName: string; + @Input() exportFileName = 'Export.xlsx'; @Input() showSearch = true; @Input() showExport = true; /** After every loaded window. */ @Output() loaded = new EventEmitter(); - layout: VirtualGridLayout; - layoutError = ''; + @ViewChild(GridComponent) kendoGrid: GridComponent; + started = false; gridView: GridDataResult = { data: [], total: 0 }; totalWithoutSearch = 0; @@ -57,67 +62,42 @@ export class VirtualGridComponent implements OnInit, OnDestroy { searchText = ''; appliedSearch = ''; exporting = false; + exportRows: any[] = []; - readonly columnType = EnumVirtualGridColumnType; - - private filters: VirtualGridFilters = {}; - private pendingFilters: VirtualGridFilters = null; + private filters: { [name: string]: string } = {}; private firstLoad = false; - private readonly load$ = new Subject(); + private readonly load$ = new Subject(); private readonly search$ = new Subject(); private readonly subscriptions = new Subscription(); - private readonly money = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); constructor(public apiService: ApiService, - private virtualGridService: VirtualGridService, - @Inject(LOCALE_ID) private locale: string, @Optional() private notificationService: HRMNotificationService, @Optional() private loadingPanel: loadingPanelService) { } ngOnInit(): void { - this.subscriptions.add( - this.virtualGridService.getLayout(this.gridKey).subscribe( - (layout) => { - layout.columns = (layout.columns || []).map(c => ({ ...c, width: this.fitWidth(c) })); - this.layout = layout; - this.resetSort(); - if (this.pendingFilters) { - const filters = this.pendingFilters; - this.pendingFilters = null; - this.load(filters); - } - }, - (err) => { - this.layoutError = this.errorText(err, 'Failed to load the grid.'); - this.showError(this.layoutError); - } - ) - ); - - // A newer request (scroll, sort, search) cancels an older one still in flight, - // so a slow response can never overwrite newer rows. + // A newer request (scroll, sort, search) cancels an older one, so old rows never overwrite new ones. this.subscriptions.add( this.load$.pipe( switchMap(request => { this.gridLoading = true; - return this.virtualGridService.getPage(request).pipe( + return this.apiService.httpPost(this.url, request).pipe( catchError(err => { this.showError(this.errorText(err, 'Failed to load data.')); - return of(null as VirtualGridPage); + return of(null as VirtualPageResult); }), finalize(() => this.gridLoading = false) ); }) - ).subscribe(page => this.onPageLoaded(page)) + ).subscribe(result => this.onPageLoaded(result)) ); - // Ask the server only once the user stops typing. + // Search only once the user stops typing. this.subscriptions.add( this.search$.pipe(debounceTime(400), distinctUntilChanged()).subscribe(text => { this.appliedSearch = text; this.skip = 0; - this.requestPage(); + this.requestPage(false); }) ); } @@ -126,138 +106,143 @@ export class VirtualGridComponent implements OnInit, OnDestroy { this.subscriptions.unsubscribe(); } - /** Shows the grid for these filters, from the first row, with the search cleared and the default sort. */ - load(filters: VirtualGridFilters): void { - if (!this.layout) { - this.pendingFilters = filters || {}; // runs as soon as the layout arrives - return; - } - this.filters = { ...(filters || {}) }; + /** Preview: shows the grid for these filters (Date values are sent as 'yyyy-MM-dd'). */ + load(filters: { [name: string]: any }): void { + this.filters = {}; + Object.keys(filters || {}).forEach(name => { + const value = filters[name]; + if (value !== null && value !== undefined && value !== '') { + this.filters[name] = value instanceof Date ? formatDate(value, 'yyyy-MM-dd', 'en-US') : String(value); + } + }); this.searchText = ''; this.appliedSearch = ''; - this.search$.next(''); // keeps distinctUntilChanged in step with the cleared box + this.search$.next(''); this.skip = 0; - this.resetSort(); + this.sort = []; this.gridView = { data: [], total: 0 }; this.totalWithoutSearch = 0; this.started = true; this.firstLoad = true; - this.requestPage(true); // Preview: the server reloads the data instead of using its kept copy - } - - /** Reloads the current window, keeping filters, search and sort. */ - reload(): void { - this.requestPage(); + this.requestPage(true); } onPageChange(event: PageChangeEvent): void { this.skip = event.skip; - this.requestPage(); + this.requestPage(false); } onSortChange(sort: SortDescriptor[]): void { this.sort = sort; this.skip = 0; - this.requestPage(); + this.requestPage(false); } onSearchChange(text: string): void { this.search$.next((text || '').trim()); } + /** Loads every matching row (same search and sort), then lets Kendo build the Excel file. */ exportExcel(): void { if (!this.started || this.gridView.total === 0 || this.exporting) { return; } - this.exporting = true; - if (this.loadingPanel) { - this.loadingPanel.ShowLoadingPanel = true; - } - const request = { ...this.buildRequest(0, 0), fields: this.layout.columns.map(c => c.field) }; - const fileName = this.exportFileName || (this.layout.title || this.gridKey).replace(/[^A-Za-z0-9]+/g, '') + '.xlsx'; - - this.virtualGridService.exportExcel(request).pipe( - finalize(() => { - this.exporting = false; - if (this.loadingPanel) { - this.loadingPanel.ShowLoadingPanel = false; - } - }) - ).subscribe( - (blob: Blob) => saveAs(blob, fileName), - (err) => this.showBlobError(err, 'Failed to export to Excel.') + this.setExporting(true); + this.apiService.httpPost(this.url, this.buildRequest(0, 0, false)).subscribe( + (result) => { + this.exportRows = (result.data || []).map(row => this.prepareRow(row)); + setTimeout(() => this.kendoGrid.saveAsExcel()); + }, + (err) => { + this.setExporting(false); + this.showError(this.errorText(err, 'Failed to export to Excel.')); + } ); } - /** - * At least wide enough for the header title on one line plus the sort icon and cell padding, - * so no grid ever needs a wrapped or cut-off header (~8px per character + 48px). - */ - private fitWidth(column: { title: string; width: number }): number { - const titleWidth = (column.title || '').length * 8 + 48; - return Math.max(column.width || 0, titleWidth); + /** Kendo asks for the rows to put in the Excel file. */ + excelData = (): ExcelExportData => ({ data: this.exportRows }); + + onExcelCreated(): void { + this.exportRows = []; + this.setExporting(false); } - private resetSort(): void { - this.sort = this.layout && this.layout.defaultSortField - ? [{ field: this.layout.defaultSortField, dir: this.layout.defaultSortDir || 'asc' }] - : []; - } - - private requestPage(refresh = false): void { - if (!this.started || !this.layout) { - return; + /** Grid display format by column type. */ + displayFormat(column: VirtualGridColumn): string { + switch (column.type) { + case 'date': return '{0:dd MMM yyyy}'; + case 'datetime': return '{0:dd MMM yyyy HH:mm}'; + case 'money': return '{0:n2}'; + default: return undefined; } - this.load$.next({ ...this.buildRequest(this.skip, this.pageSize), refresh: refresh }); } - private buildRequest(skip: number, take: number): VirtualGridRequest { + /** Excel cell format by column type. */ + excelOptions(column: VirtualGridColumn): any { + switch (column.type) { + case 'date': return { format: 'dd MMM yyyy' }; + case 'datetime': return { format: 'dd MMM yyyy HH:mm' }; + case 'money': return { format: '#,##0.00', textAlign: 'right' }; + case 'number': return { textAlign: 'right' }; + default: return {}; + } + } + + isNumber(column: VirtualGridColumn): boolean { + return column.type === 'money' || column.type === 'number'; + } + + /** At least wide enough for the header title on one line (~8px per character + sort icon/padding). */ + columnWidth(column: VirtualGridColumn): number { + return Math.max(column.width || 130, (column.title || '').length * 8 + 48); + } + + private requestPage(refresh: boolean): void { + if (this.started && this.url) { + this.load$.next(this.buildRequest(this.skip, this.pageSize, refresh)); + } + } + + private buildRequest(skip: number, take: number, refresh: boolean): VirtualPageRequest { const sort = this.sort && this.sort.length > 0 && this.sort[0].dir ? this.sort[0] : null; return { - gridKey: this.gridKey, filters: this.filters, + fields: this.columns.map(c => c.field), skip: skip, take: take, search: this.appliedSearch || undefined, sortField: sort ? sort.field : undefined, sortDir: sort ? (sort.dir as 'asc' | 'desc') : undefined, + refresh: refresh, }; } - private onPageLoaded(page: VirtualGridPage): void { - if (!page) { + private onPageLoaded(result: VirtualPageResult): void { + if (!result) { return; } - this.gridView = { data: (page.data || []).map(row => this.formatRow(row)), total: page.total }; - this.totalWithoutSearch = page.totalWithoutSearch; - this.loaded.emit({ total: page.total, totalWithoutSearch: page.totalWithoutSearch, firstLoad: this.firstLoad }); + this.gridView = { data: (result.data || []).map(row => this.prepareRow(row)), total: result.total }; + this.totalWithoutSearch = result.totalWithoutSearch; + this.loaded.emit({ total: result.total, totalWithoutSearch: result.totalWithoutSearch, firstLoad: this.firstLoad }); this.firstLoad = false; } - /** Display formatting by column type: dates 'dd MMM yyyy', money with 2 decimals — the same text the server searches. */ - private formatRow(row: { [field: string]: any }): { [field: string]: any } { - const result: { [field: string]: any } = { ...row }; - for (const column of this.layout.columns) { - const value = row[column.field]; - if (value === null || value === undefined || value === '') { - result[column.field] = ''; - continue; - } - switch (column.type) { - case EnumVirtualGridColumnType.Date: - result[column.field] = formatDate(value, 'dd MMM yyyy', this.locale); - break; - case EnumVirtualGridColumnType.DateTime: - result[column.field] = formatDate(value, 'dd MMM yyyy HH:mm', this.locale); - break; - case EnumVirtualGridColumnType.Money: - result[column.field] = this.money.format(Number(value)); - break; - default: - result[column.field] = String(value); + /** Dates arrive as text; turn them into Date so the grid and Excel format them. */ + private prepareRow(row: { [field: string]: any }): { [field: string]: any } { + for (const column of this.columns) { + if ((column.type === 'date' || column.type === 'datetime') && row[column.field]) { + row[column.field] = new Date(row[column.field]); } } - return result; + return row; + } + + private setExporting(value: boolean): void { + this.exporting = value; + if (this.loadingPanel) { + this.loadingPanel.ShowLoadingPanel = value; + } } private errorText(err: any, fallback: string): string { @@ -277,14 +262,4 @@ export class VirtualGridComponent implements OnInit, OnDestroy { console.error(message); } } - - /** A failed file download returns its error message as a Blob; read it before showing it. */ - private showBlobError(err: any, fallback: string): void { - const blob: Blob = err instanceof Blob ? err : (err && err.error instanceof Blob ? err.error : null); - if (blob) { - blob.text().then(text => this.showError(text || fallback), () => this.showError(fallback)); - } else { - this.showError(this.errorText(err, fallback)); - } - } } diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts index 9b59add..8ba6e01 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts @@ -1,15 +1,16 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; -import { GridModule } from '@progress/kendo-angular-grid'; +import { ExcelModule, GridModule } from '@progress/kendo-angular-grid'; +import { ExcelExportModule } from '@progress/kendo-angular-excel-export'; import { ButtonsModule } from '@progress/kendo-angular-buttons'; import { TextBoxModule } from '@progress/kendo-angular-inputs'; import { VirtualGridComponent } from './virtual-grid.component'; -/** Import this module to use (common server-paged virtual-scrolling grid). */ +/** Import this module to use (common virtual-scrolling grid). */ @NgModule({ declarations: [VirtualGridComponent], - imports: [CommonModule, FormsModule, GridModule, ButtonsModule, TextBoxModule], + imports: [CommonModule, FormsModule, GridModule, ExcelModule, ExcelExportModule, ButtonsModule, TextBoxModule], exports: [VirtualGridComponent] }) export class VirtualGridModule {} diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html index 4a62400..6da365f 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.html @@ -17,9 +17,9 @@ - +
- diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts index 9cea443..b12e45a 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts @@ -1,16 +1,18 @@ -import { Component, ChangeDetectionStrategy, ViewChild } from '@angular/core'; -import { DatePipe } from '@angular/common'; +import { Component, ChangeDetectionStrategy, OnInit, ViewChild } from '@angular/core'; import { ApiService } from 'src/app/app.api.service'; import { HRMNotificationService } from 'src/app/app.notification.service'; import { VirtualGridComponent } from 'src/app/custom-components/virtual-grid/virtual-grid.component'; -import { VirtualGridLoadedEvent } from 'src/app/_models/common/virtual-grid'; +import { VirtualGridColumn, VirtualGridLoadedEvent } from 'src/app/_models/common/virtual-grid'; +import { SystemConfigurationService } from 'src/app/_services/common/systemconfiguration.service'; +import { SystemConfigaration } from 'src/app/_models/common/SystemConfigaration'; +import { EnumConfigurationType } from 'src/app/_models/enums'; /** * Discontinue Employee Details: employees discontinued inside a date range, with the same columns as * Employee Details (/reports/report-viewer/empDetailInfo) plus the Discontinue Date. * - * The grid is the common (server grid "discontinue-employee", - * HRM.DA/Service/VirtualGrid/Definitions/DiscontinueEmployeeVirtualGrid.cs); this screen only owns its filters. + * Data: EmployeeController.GetDiscontinueEmployeePage → EmployeeService.GetDiscontinuedEmployeeDetails. + * The common does virtual scrolling, search, sort, total and Excel. */ @Component({ selector: 'app-discontinue-employee-details', @@ -19,19 +21,30 @@ import { VirtualGridLoadedEvent } from 'src/app/_models/common/virtual-grid'; changeDetection: ChangeDetectionStrategy.Eager, standalone: false, }) -export class DiscontinueEmployeeDetailsComponent { +export class DiscontinueEmployeeDetailsComponent implements OnInit { @ViewChild('grid') grid: VirtualGridComponent; fromDate: Date = new Date(new Date().getFullYear(), new Date().getMonth(), 1); toDate: Date = new Date(); dateFormat = 'dd-MMM-yyyy'; + /** Grid columns. field = property of DiscontinueEmployeeDetailInfo (camelCase). */ + columns: VirtualGridColumn[] = []; + constructor(public apiService: ApiService, public notificationService: HRMNotificationService, - private datePipe: DatePipe) { + private systemConfigService: SystemConfigurationService) { this.apiService.selectedMenuName = 'Discontinue Employee Details'; } + ngOnInit(): void { + // Department tier headings come from the Logic configuration, as in Employee Details. + this.systemConfigService.GetAllChildrenByParentNode(EnumConfigurationType.Logic, 'department').subscribe( + (configs: SystemConfigaration[]) => this.columns = this.buildColumns(configs || []), + () => this.columns = this.buildColumns([]) + ); + } + preview(): void { if (!this.fromDate || !this.toDate) { this.notificationService.showWarning('Please select From Date and To Date.', 'Warning'); @@ -41,10 +54,7 @@ export class DiscontinueEmployeeDetailsComponent { this.notificationService.showWarning('From Date cannot be after To Date.', 'Warning'); return; } - this.grid.load({ - fromDate: this.datePipe.transform(this.fromDate, 'yyyy-MM-dd'), - toDate: this.datePipe.transform(this.toDate, 'yyyy-MM-dd'), - }); + this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); } onLoaded(event: VirtualGridLoadedEvent): void { @@ -52,4 +62,45 @@ export class DiscontinueEmployeeDetailsComponent { this.notificationService.showWarning('No employee was discontinued in the selected date range.', 'Warning'); } } + + private buildColumns(configs: SystemConfigaration[]): VirtualGridColumn[] { + const tierFields = ['deptireOne', 'deptireTwo', 'deptireThree', 'deptireFour', 'deptireFive']; + const tierColumns: VirtualGridColumn[] = configs + .filter(c => (c.node || '').toLowerCase() === 'text') + .slice(0, tierFields.length) + .map((c, i) => ({ field: tierFields[i], title: c.value, width: 150 })); + + return [ + { 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, type: 'date' }, + { field: 'joiningDate', title: 'Joining Date', width: 120, type: 'date' }, + { field: 'discontinueDate', title: 'Discontinue Date', width: 140, type: 'date' }, + { 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 }, + ...tierColumns, + { 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, type: 'date' }, + { field: 'basicSalary', title: 'Basic Salary', width: 120, type: 'money' }, + { field: 'grossSalary', title: 'Gross Salary', width: 120, type: 'money' }, + { 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 }, + ]; + } } diff --git a/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs b/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs index 3838dab..3904c74 100644 --- a/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs +++ b/HRM.UI/Configurations/DI/ServiceCollectionExtensions.cs @@ -48,7 +48,6 @@ namespace HRM.UI.Configuration.DI services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/HRM.UI/Controllers/CommonInterface/VirtualGridController.cs b/HRM.UI/Controllers/CommonInterface/VirtualGridController.cs deleted file mode 100644 index a8a216c..0000000 --- a/HRM.UI/Controllers/CommonInterface/VirtualGridController.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using HRM.BO; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace HRM.UI.Controllers.CommonInterface -{ - [ApiController] - [Route("api/virtualGrid")] - [Authorize] - public class VirtualGridController : ControllerBase - { - private readonly IVirtualGridService _virtualGridService; - - public VirtualGridController(IVirtualGridService virtualGridService) - { - _virtualGridService = virtualGridService; - } - - [HttpGet("layout/{gridKey}")] - public ActionResult GetLayout(string gridKey) - { - try - { - return Ok(_virtualGridService.GetLayout(gridKey, GetContext())); - } - catch (ArgumentException e) - { - return BadRequest(e.Message); - } - catch (UnauthorizedAccessException e) - { - return StatusCode(StatusCodes.Status403Forbidden, e.Message); - } - catch (Exception e) - { - return StatusCode(StatusCodes.Status500InternalServerError, e.Message); - } - } - - [HttpPost("page")] - public ActionResult GetPage(VirtualGridRequest request) - { - try - { - return Ok(_virtualGridService.GetPage(request, GetContext())); - } - catch (ArgumentException e) - { - return BadRequest(e.Message); - } - catch (UnauthorizedAccessException e) - { - return StatusCode(StatusCodes.Status403Forbidden, e.Message); - } - catch (Exception e) - { - return StatusCode(StatusCodes.Status500InternalServerError, e.Message); - } - } - - [HttpPost("export-excel")] - public ActionResult ExportExcel(VirtualGridExportRequest request) - { - try - { - VirtualGridFile file = _virtualGridService.ExportExcel(request, GetContext()); - return File(file.Content, file.ContentType, file.FileName); - } - catch (ArgumentException e) - { - return BadRequest(e.Message); - } - catch (UnauthorizedAccessException e) - { - return StatusCode(StatusCodes.Status403Forbidden, e.Message); - } - catch (Exception e) - { - return StatusCode(StatusCodes.Status500InternalServerError, e.Message); - } - } - - private VirtualGridContext GetContext() - { - CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); - return new VirtualGridContext - { - UserID = currentUser.UserID, - EmployeeID = currentUser.EmployeeID, - PayrollTypeID = currentUser.PayrollTypeID, - UserType = currentUser.UserType, - }; - } - } -} diff --git a/HRM.UI/Controllers/Employee/EmployeeController.cs b/HRM.UI/Controllers/Employee/EmployeeController.cs index 9dd96d8..174fea6 100644 --- a/HRM.UI/Controllers/Employee/EmployeeController.cs +++ b/HRM.UI/Controllers/Employee/EmployeeController.cs @@ -3410,5 +3410,26 @@ namespace HRM.UI.Controllers + /// + /// Discontinue Employee Details (Manage Employee › Report): one window of rows for <app-virtual-grid>. + /// Filters: fromDate, toDate ("yyyy-MM-dd"). VirtualPaging does the search, sort and paging. + /// + [HttpPost("getDiscontinueEmployeePage")] + public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) + { + try + { + CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + DateTime fromDate = request.GetDate("fromDate", "From Date"); + DateTime toDate = request.GetDate("toDate", "To Date"); + + return Ok(VirtualPaging.Page(request, currentUser, + () => _EmployeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, currentUser.PayrollTypeID ?? 0))); + } + catch (Exception ex) + { + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); + } + } } } \ No newline at end of file From 1895b2faa7d2f046cb78e92de6829b72e9ce2b27 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 15:57:13 +0600 Subject: [PATCH 5/8] Done --- .../src/app/app.topbar.component.html | 6 +++ .../src/app/app.topbar.component.scss | 24 +++++++++ .../ClientApp/src/app/app.topbar.component.ts | 50 +++++++++++++++++++ .../discontinue-employee-details.component.ts | 1 + 4 files changed, 81 insertions(+) diff --git a/HRM.UI/ClientApp/src/app/app.topbar.component.html b/HRM.UI/ClientApp/src/app/app.topbar.component.html index 2833eb5..4e79543 100644 --- a/HRM.UI/ClientApp/src/app/app.topbar.component.html +++ b/HRM.UI/ClientApp/src/app/app.topbar.component.html @@ -98,6 +98,12 @@ } + + @if (payrollTypeName) { +
  • + {{payrollTypeName}} +
  • + } @if (apiService.menuItems !== null) { diff --git a/HRM.UI/ClientApp/src/app/app.topbar.component.scss b/HRM.UI/ClientApp/src/app/app.topbar.component.scss index 6760b0f..ff2701c 100644 --- a/HRM.UI/ClientApp/src/app/app.topbar.component.scss +++ b/HRM.UI/ClientApp/src/app/app.topbar.component.scss @@ -409,3 +409,27 @@ border-left: 1px solid $topbar-accent-purple-light; margin-left: 12px; } + +// Current payroll type label (admin logins), left of the Pending Jobs icon. +// Sits on the dark admin topbar, so it uses the same white as .topbar-icon. +.topbar-payroll-type-item { + display: flex; + align-items: center; + height: 36px; +} +.topbar-payroll-type { + display: inline-block; + max-width: 260px; + padding: 4px 12px; + border: 1px solid rgba($color-white, 0.35); + border-radius: 999px; + background: rgba($color-white, 0.12); + color: $color-white; + font-size: 13px; + font-weight: 600; + line-height: 18px; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + cursor: default; +} diff --git a/HRM.UI/ClientApp/src/app/app.topbar.component.ts b/HRM.UI/ClientApp/src/app/app.topbar.component.ts index bec2e2c..3bac07b 100644 --- a/HRM.UI/ClientApp/src/app/app.topbar.component.ts +++ b/HRM.UI/ClientApp/src/app/app.topbar.component.ts @@ -54,6 +54,13 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy { private notifPanelWasOpen = false; private subscriptions: Subscription[] = []; + // Current payroll type's description, shown left of the Pending Jobs icon for admin + // (EnumUserType.User) logins only. Re-read whenever the stored token changes + // (login, user switch, payroll-type switch, logout). + payrollTypeName: string = ''; + private payrollTypeToken: string | null | undefined = undefined; + private payrollTypeSub: Subscription | null = null; + isScrolled: boolean = false; @HostListener('window:scroll', []) onWindowScroll() { @@ -159,6 +166,46 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy { this.onNotificationPanelClosed(); } this.notifPanelWasOpen = isOpen; + + this.syncPayrollTypeName(); + } + + /** + * Shows the current payroll type's description for admin logins. Cheap on every + * change-detection pass: it only does work when the token in localStorage changed. + * The name comes from the server (GetPayrollTypeByLoginID), so it is the payroll + * type the API actually scopes data by. + */ + private syncPayrollTypeName(): void { + const token = localStorage.getItem('token'); + if (token === this.payrollTypeToken) { + return; + } + this.payrollTypeToken = token; + if (this.payrollTypeSub) { + this.payrollTypeSub.unsubscribe(); + this.payrollTypeSub = null; + } + + let user: any = null; + try { + user = token ? new JwtHelperService().decodeToken(token) : null; + } catch { + user = null; + } + if (!user || user.UserType != EnumUserType.User || !user.PayrollTypeID) { + this.payrollTypeName = ''; + return; + } + + this.payrollTypeSub = this.authService.GetPayrollTypeByLoginID().subscribe({ + next: (payrollType) => { + this.payrollTypeName = payrollType?.description ?? ''; + }, + error: () => { + this.payrollTypeName = ''; + }, + }); } /** @@ -176,6 +223,9 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy { ngOnDestroy(): void { this.subscriptions.forEach(s => s.unsubscribe()); + if (this.payrollTypeSub) { + this.payrollTypeSub.unsubscribe(); + } } loadBookmark() { diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts index b12e45a..9e8623d 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts @@ -54,6 +54,7 @@ export class DiscontinueEmployeeDetailsComponent implements OnInit { this.notificationService.showWarning('From Date cannot be after To Date.', 'Warning'); return; } + debugger; this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); } From 0ce82ae791e7989c61a93671e7d5844011e30c2d Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 16:15:14 +0600 Subject: [PATCH 6/8] Discontinue Virtual pagination --- HRM.BO/Employee/Employee.cs | 2 +- .../Report/DiscontinueEmployeeDetailInfo.cs | 14 -- HRM.BO/VirtualPaging/VirtualPaging.cs | 34 ++- HRM.DA/DA/Employee/EmployeeDA.cs | 72 +++++-- HRM.DA/DA/VirtualPaging/README.md | 119 +++++++++++ HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs | 169 +++++++++++++++ HRM.DA/Service/Employee/EmployeeService.cs | 135 +----------- HRM.DA/Service/VirtualPaging/README.md | 77 ------- HRM.DA/Service/VirtualPaging/VirtualPaging.cs | 200 ------------------ .../src/app/_models/common/virtual-grid.ts | 10 +- .../virtual-grid/virtual-grid.component.ts | 36 ++-- .../discontinue-employee-details.component.ts | 5 +- .../Employee/EmployeeController.cs | 14 +- 13 files changed, 422 insertions(+), 465 deletions(-) delete mode 100644 HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs create mode 100644 HRM.DA/DA/VirtualPaging/README.md create mode 100644 HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs delete mode 100644 HRM.DA/Service/VirtualPaging/README.md delete mode 100644 HRM.DA/Service/VirtualPaging/VirtualPaging.cs diff --git a/HRM.BO/Employee/Employee.cs b/HRM.BO/Employee/Employee.cs index 1bd8cd2..14a1a81 100644 --- a/HRM.BO/Employee/Employee.cs +++ b/HRM.BO/Employee/Employee.cs @@ -608,7 +608,7 @@ namespace HRM.BO DataSet GetEmpDetailsView(); // Discontinue Employee Details report: employees discontinued (Status = Discontinued) whose // discontinue date (ENDOFCONTRACTDATE) falls inside the range, in one payroll type. - List GetDiscontinuedEmployeeDetails(DateTime fromDate, DateTime toDate, int payrollTypeID); + VirtualPageResult GetDiscontinuedEmployeePage(VirtualPageRequest request, DateTime fromDate, DateTime toDate, int payrollTypeID); int GetCurrentHeadCount(int departmentId, int designationId, int gradeId); List GetbyDepartment(int deptId); List NotYetAssigned(int payrollTypeID); diff --git a/HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs b/HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs deleted file mode 100644 index ce6c286..0000000 --- a/HRM.BO/Report/DiscontinueEmployeeDetailInfo.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace HRM.BO.Report -{ - /// - /// One row of the Discontinue Employee Details report (Manage Employee › Report): the same fields as - /// Employee Details () plus the date the employee was discontinued - /// (Employee.ENDOFCONTRACTDATE). Loaded by IEmployeeService.GetDiscontinuedEmployeeDetails. - /// - public class DiscontinueEmployeeDetailInfo : EmployeeDetailInfo - { - public DateTime? DiscontinueDate { get; set; } - } -} diff --git a/HRM.BO/VirtualPaging/VirtualPaging.cs b/HRM.BO/VirtualPaging/VirtualPaging.cs index 85e9cbf..d4cd09b 100644 --- a/HRM.BO/VirtualPaging/VirtualPaging.cs +++ b/HRM.BO/VirtualPaging/VirtualPaging.cs @@ -5,17 +5,17 @@ using System.Globalization; namespace HRM.BO { /// - /// What <app-virtual-grid> sends to a page endpoint: the page's own filters plus the grid's + /// What <app-virtual-grid> sends to a page endpoint: the page's own filters plus the grid's columns, /// scroll position, search text and sort. The same class is used by every page, so no page needs a - /// request class of its own. See HRM.DA/Service/VirtualPaging/README.md. + /// request class of its own. See HRM.DA/DA/VirtualPaging/README.md. /// public class VirtualPageRequest { /// The page's filters, e.g. { fromDate: "2025-01-01", toDate: "2025-12-31" }. Read them with GetDate / GetInt / GetString. public Dictionary Filters { get; set; } = new Dictionary(); - /// The grid's columns (property names). Only these are sent back and searched. - public List Fields { get; set; } = new List(); + /// The grid's columns. Only these are selected, searched and sortable. + public List Columns { get; set; } = new List(); public int Skip { get; set; } @@ -28,8 +28,11 @@ namespace HRM.BO /// "asc" or "desc". public string SortDir { get; set; } - /// True on Preview: load the data again. False on scroll/sort/search/export: use the kept copy. - public bool Refresh { get; set; } + /// + /// True when the totals must be counted (Preview, new search). Scrolling and sorting do not change + /// the totals, so they send false and the COUNT is skipped. + /// + public bool CountTotal { get; set; } /// A filter as text, or null when it was not sent. public string GetString(string name) @@ -59,16 +62,25 @@ namespace HRM.BO } } + /// One grid column: Field = a column alias of the page's query; Type = how it is shown. + public class VirtualPageColumn + { + public string Field { get; set; } + + /// "text" (default), "number", "money", "date" or "datetime". + public string Type { get; set; } + } + /// One window of rows for the grid. public class VirtualPageResult { - /// Rows matching the search (all rows when there is no search). - public int Total { get; set; } + /// Rows matching the search (all rows when there is no search). Null when not counted (CountTotal = false). + public int? Total { get; set; } - /// All rows, ignoring the search. - public int TotalWithoutSearch { get; set; } + /// All rows, ignoring the search. Null when not counted. + public int? TotalWithoutSearch { get; set; } - /// The rows of this window, holding only the requested fields. + /// The rows of this window, holding only the requested columns. public List> Data { get; set; } = new List>(); } } diff --git a/HRM.DA/DA/Employee/EmployeeDA.cs b/HRM.DA/DA/Employee/EmployeeDA.cs index 4f290b5..5efc8aa 100644 --- a/HRM.DA/DA/Employee/EmployeeDA.cs +++ b/HRM.DA/DA/Employee/EmployeeDA.cs @@ -1597,20 +1597,68 @@ namespace HRM.DA return oEmpDetails; } - // Discontinue Employee Details report: the Employee Details view (VW_EMPLOYEEBASIC) for employees - // discontinued between fromDate and toDate (inclusive), plus their department and discontinue date. - // The end is exclusive (toDate + 1 day) so a time part on ENDOFCONTRACTDATE never drops the last day. - internal static IDataReader GetDiscontinuedEmployeeDetails(TransactionContext tc, DateTime fromDate, - DateTime toDate, int payrollTypeID) + /// + /// Discontinue Employee Details (Manage Employee › Report): one row per employee discontinued + /// (STATUS = Discontinued) between fromDate and toDate in one payroll type, with the same columns as + /// the Employee Details report. Column aliases are the grid's field names; VirtualPagingDA adds the + /// search, sort and paging, so there is no ORDER BY here. + /// + /// Department tiers: the employee's department sits at its own tier (TIRE) and its parents fill the + /// tiers above it, as EmployeeController.GetEmpDetailInfo works them out (no tiers for a department + /// without a parent). The end date is exclusive (toDate + 1 day) so a time part never drops the last day. + /// + internal static string GetDiscontinuedEmployeeQuery(DateTime fromDate, DateTime toDate, int payrollTypeID) { - string sql = SQLParser.MakeSQL(@"SELECT v.*, e.DEPARTMENTID AS EmpDepartmentID, e.ENDOFCONTRACTDATE AS DiscontinueDate - FROM VW_EMPLOYEEBASIC v - INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = v.EmployeeId - WHERE e.STATUS = %n AND e.PAYROLLTYPEID = %n - AND e.ENDOFCONTRACTDATE >= %d AND e.ENDOFCONTRACTDATE < %d - ORDER BY v.EMPLOYEENO", + return SQLParser.MakeSQL(@" +SELECT + v.EMPLOYEENO AS employeeNo, + v.NAME AS name, + v.Gender AS gender, + v.BIRTHDATE AS birthDate, + v.JOININGDATE AS joiningDate, + e.ENDOFCONTRACTDATE AS discontinueDate, + v.MOBILENO AS mobileNo, + v.EMAILADDRESS AS emailAddress, + v.DESIGNATION AS designation, + v.GRADE AS grade, + v.Category AS category, + v.Status AS status, + ISNULL(CASE t.Tier WHEN 1 THEN d0.DESCRIPTION WHEN 2 THEN d1.DESCRIPTION WHEN 3 THEN d2.DESCRIPTION + WHEN 4 THEN d3.DESCRIPTION WHEN 5 THEN d4.DESCRIPTION END, '') AS deptireOne, + ISNULL(CASE t.Tier WHEN 2 THEN d0.DESCRIPTION WHEN 3 THEN d1.DESCRIPTION WHEN 4 THEN d2.DESCRIPTION + WHEN 5 THEN d3.DESCRIPTION END, '') AS deptireTwo, + ISNULL(CASE t.Tier WHEN 3 THEN d0.DESCRIPTION WHEN 4 THEN d1.DESCRIPTION WHEN 5 THEN d2.DESCRIPTION END, '') AS deptireThree, + ISNULL(CASE t.Tier WHEN 4 THEN d0.DESCRIPTION WHEN 5 THEN d1.DESCRIPTION END, '') AS deptireFour, + ISNULL(CASE t.Tier WHEN 5 THEN d0.DESCRIPTION END, '') AS deptireFive, + v.Location AS location, + v.RELIGION AS religion, + v.MaritalStatus AS maritalStatus, + v.DATEOFCONFIRMATION AS dateOfConfirmation, + ISNULL(v.BASICSALARY, 0) AS basicSalary, + ISNULL(v.GrossSalary, 0) AS grossSalary, + v.PassPortNo AS passportNo, + CASE ISNULL(v.bloodgroup, 0) WHEN 0 THEN 'NA' WHEN 1 THEN 'A+' WHEN 2 THEN 'A-' WHEN 3 THEN 'B+' + WHEN 4 THEN 'B-' WHEN 5 THEN 'O+' WHEN 6 THEN 'O-' WHEN 7 THEN 'AB+' WHEN 8 THEN 'AB-' ELSE '' END AS bloodGroup, + v.LineManagerNo AS lineManagerNo, + v.LineManagerName AS lineManagerName, + v.LineManagerEmail AS lineManagerEmail, + v.EmpStatus AS empStatus, + ISNULL(NULLIF(v.Bank, ''), v.OUTPayBank) AS bank, + ISNULL(NULLIF(v.Branch, ''), v.OUTPayBranch) AS branch, + ISNULL(NULLIF(v.ACCOUNTNO, ''), v.OutPayAccountNo) AS accountNo, + v.NATIONALID AS nationalId, + v.TINNO AS tinNo +FROM VW_EMPLOYEEBASIC v +INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = v.EmployeeId +LEFT JOIN DEPARTMENT d0 ON d0.DEPARTMENTID = e.DEPARTMENTID +LEFT JOIN DEPARTMENT d1 ON d1.DEPARTMENTID = d0.PARENTID +LEFT JOIN DEPARTMENT d2 ON d2.DEPARTMENTID = d1.PARENTID +LEFT JOIN DEPARTMENT d3 ON d3.DEPARTMENTID = d2.PARENTID +LEFT JOIN DEPARTMENT d4 ON d4.DEPARTMENTID = d3.PARENTID +CROSS APPLY (SELECT CASE WHEN d0.PARENTID IS NULL THEN 0 WHEN d0.TIRE > 5 THEN 5 ELSE d0.TIRE END AS Tier) t +WHERE e.STATUS = %n AND e.PAYROLLTYPEID = %n + AND e.ENDOFCONTRACTDATE >= %d AND e.ENDOFCONTRACTDATE < %d", (int)EnumEmployeeStatus.Discontinued, payrollTypeID, fromDate.Date, toDate.Date.AddDays(1)); - return tc.ExecuteReader(sql); } internal static DataSet GetEmpDetailsView(TransactionContext tc) diff --git a/HRM.DA/DA/VirtualPaging/README.md b/HRM.DA/DA/VirtualPaging/README.md new file mode 100644 index 0000000..439cd70 --- /dev/null +++ b/HRM.DA/DA/VirtualPaging/README.md @@ -0,0 +1,119 @@ +# Virtual paging: large lists with virtual scrolling + +SQL Server does the paging. Each request reads only the rows the grid needs (100 at a time) with +`OFFSET … FETCH`, and the search, sort and total count run in the same SQL. Nothing is kept in server +memory, so it works the same for 1,000 or 1,000,000 rows and always shows the latest data. + +| Piece | File | What it does | +|---|---|---| +| Request / result | `HRM.BO/VirtualPaging/VirtualPaging.cs` | `VirtualPageRequest` (filters, columns, skip/take, search, sort) and `VirtualPageResult`, shared by every page | +| Paging SQL | `HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs` | Wraps a page's query with search, sort, `OFFSET/FETCH` and the totals | +| Grid | `ClientApp/src/app/custom-components/virtual-grid/` (`VirtualGridModule`) | ``: virtual scrolling, search box, total, Export to Excel | + +## A new page: 4 small steps (worked example: Discontinue Employee Details) + +**1. DA: the query.** An ordinary `SELECT` built with `MakeSQL`. Each column alias is a grid field name. +No `ORDER BY` and no `WITH` (the query is used as a derived table). + +```csharp +internal static string GetDiscontinuedEmployeeQuery(DateTime fromDate, DateTime toDate, int payrollTypeID) +{ + return SQLParser.MakeSQL(@" +SELECT v.EMPLOYEENO AS employeeNo, + v.NAME AS name, + v.JOININGDATE AS joiningDate, + ISNULL(v.BASICSALARY, 0) AS basicSalary +FROM VW_EMPLOYEEBASIC v +INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = v.EmployeeId +WHERE e.STATUS = %n AND e.PAYROLLTYPEID = %n + AND e.ENDOFCONTRACTDATE >= %d AND e.ENDOFCONTRACTDATE < %d", + (int)EnumEmployeeStatus.Discontinued, payrollTypeID, fromDate.Date, toDate.Date.AddDays(1)); +} +``` + +**2. Service: one method** (canonical try/catch) + its line in the module's `I…Service` interface: + +```csharp +public VirtualPageResult GetDiscontinuedEmployeePage(VirtualPageRequest request, DateTime fromDate, DateTime toDate, int payrollTypeID) +{ + VirtualPageResult result = null; + TransactionContext tc = null; + try + { + tc = TransactionContext.Begin(); + string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); + result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); // "employeeNo" = a unique field + tc.End(); + } + catch (Exception e) + { + if (tc != null) tc.HandleError(); + ExceptionLog.Write(e); + throw new ServiceException(e.Message, e); + } + return result; +} +``` + +**3. Controller: one endpoint** in the module's own controller. Read the page's filters from the request: + +```csharp +[HttpPost("getDiscontinueEmployeePage")] +public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) +{ + try + { + CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + if (currentUser.UserType == EnumUserType.Employee) + return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to see this report."); + + DateTime fromDate = request.GetDate("fromDate", "From Date"); // also GetInt / GetString + DateTime toDate = request.GetDate("toDate", "To Date"); + return Ok(_EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0)); + } + catch (ArgumentException ex) { return BadRequest(ex.Message); } + catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } +} +``` + +**4. Screen**: import `VirtualGridModule`, then: + +```html + +``` +```ts +columns: VirtualGridColumn[] = [ + { field: 'employeeNo', title: 'Employee No', width: 110 }, + { field: 'joiningDate', title: 'Joining Date', type: 'date' }, + { field: 'basicSalary', title: 'Basic Salary', type: 'money' }, +]; + +preview() { + this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); // any filters; Dates go as yyyy-MM-dd +} +``` + +`field` = the column alias in the query. Column `type`: `text` (default), `number`, `money` (1,234.56), +`date` (dd MMM yyyy), `datetime`. `sortable: false` turns sorting off for a column. + +## How one request runs + +| User action | Request | SQL | +|---|---|---| +| Preview | `skip 0, take 100, countTotal true` | `COUNT` + first 100 rows, one round trip | +| Scroll | `skip 100, 200, …` | next 100 rows only (no `COUNT`) | +| Sort | `sortField`, `sortDir` | `ORDER BY` that column, then the unique field | +| Search (after 400 ms) | `search`, `countTotal true` | `WHERE` every column `LIKE @vpSearch`, new `COUNT` | +| Export to Excel | `take 0` | every matching row, same search and sort; Kendo builds the `.xlsx` | + +## Rules + +- **Speed**: the page query's `WHERE` must use an index (see `DiscontinueEmployeeReport_DB_Changes.sql`). + Values computed in C# cannot be searched or sorted, so compute them in the query (see the department + tiers and blood group in `GetDiscontinuedEmployeeQuery`). +- **Safety**: the query is built by code with `MakeSQL` from typed values (dates, ints). The search text, + skip and take are SQL parameters; field names must be plain identifiers and are bracketed. +- **Access**: authorization in this app is menu-driven, so every page endpoint checks who may call it + (self-service logins are refused above). +- The key field passed to `GetPage` must be unique, so pages never overlap or skip rows. diff --git a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs new file mode 100644 index 0000000..0b56d90 --- /dev/null +++ b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using Ease.Core.DataAccess; +using Ease.Core.DataAccess.SQL; +using HRM.BO; +using Microsoft.Data.SqlClient; + +namespace HRM.DA +{ + /// + /// Server side of <app-virtual-grid>: SQL Server does the paging. Each call reads only one window + /// of rows (OFFSET/FETCH), with the search and sort in the same SQL and the totals counted only when + /// they can change. A page's service calls it with its own query: + /// + /// string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); + /// return VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); + /// + /// The query is an ordinary SELECT whose column aliases are the grid's field names, with no ORDER BY + /// and no WITH (it is used as a derived table). See README.md next to this file. + /// + /// Safety: the page query is built by code with MakeSQL; user input (search text, skip, take) reaches + /// SQL only as parameters; field names are checked to be plain identifiers and bracketed. + /// + internal static class VirtualPagingDA + { + private static readonly Regex Identifier = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); + private const int MaxRowsPerPage = 1000; + private const int MaxSearchLength = 200; + + /// The page's SELECT (column aliases = grid fields, no ORDER BY). + /// A unique field (e.g. employeeNo): the default sort and the tie-breaker that keeps pages from overlapping. + internal static VirtualPageResult GetPage(TransactionContext tc, string query, VirtualPageRequest request, string keyField) + { + if (request == null) + throw new ArgumentException("Invalid request."); + if (!IsIdentifier(keyField)) + throw new ArgumentException("Invalid key field."); + + List columns = GetColumns(request.Columns); + string search = (request.Search ?? "").Trim(); + if (search.Length > MaxSearchLength) + search = search.Substring(0, MaxSearchLength); + + string source = " FROM (" + query + ") AS vp"; + string searchCondition = search.Length > 0 ? SearchCondition(columns) : null; + bool paged = request.Take > 0; + + var sql = new StringBuilder(); + + // 1. Totals, only on Preview / new search (scroll and sort do not change them). One scan for both. + if (request.CountTotal) + { + sql.Append("SELECT COUNT(*) AS TotalWithoutSearch, ") + .Append(searchCondition == null ? "COUNT(*)" : "ISNULL(SUM(CASE WHEN " + searchCondition + " THEN 1 ELSE 0 END), 0)") + .Append(" AS Total").Append(source).Append(";\n"); + } + + // 2. The window of rows. + sql.Append("SELECT ") + .Append(columns.Count == 0 ? "*" : string.Join(", ", columns.Select(c => "[" + c.Field + "]"))) + .Append(source) + .Append(searchCondition == null ? "" : " WHERE " + searchCondition) + .Append(" ORDER BY ").Append(OrderBy(columns, request, keyField)); + if (paged) + sql.Append(" OFFSET @vpSkip ROWS FETCH NEXT @vpTake ROWS ONLY"); + + var parameters = new List(); // SqlHelper expects a SqlParameter[] + if (searchCondition != null) + parameters.Add(SqlHelperExtension.CreateInParam("@vpSearch", SqlDbType.NVarChar, "%" + EscapeLike(search) + "%", 400)); + if (paged) + { + parameters.Add(SqlHelperExtension.CreateInParam("@vpSkip", SqlDbType.Int, Math.Max(0, request.Skip))); + parameters.Add(SqlHelperExtension.CreateInParam("@vpTake", SqlDbType.Int, Math.Min(request.Take, MaxRowsPerPage))); + } + + var result = new VirtualPageResult(); + using (IDataReader reader = tc.ExecuteReader(CommandType.Text, sql.ToString(), parameters.ToArray())) + { + if (request.CountTotal) + { + if (reader.Read()) + { + result.TotalWithoutSearch = Convert.ToInt32(reader["TotalWithoutSearch"]); + result.Total = Convert.ToInt32(reader["Total"]); + } + reader.NextResult(); + } + + while (reader.Read()) + { + var row = new Dictionary(reader.FieldCount); + for (int i = 0; i < reader.FieldCount; i++) + row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i); + result.Data.Add(row); + } + } + return result; + } + + /// The requested columns; every field must be a plain identifier (it is put in the SQL in brackets). + private static List GetColumns(List requested) + { + var columns = new List(); + var seen = new HashSet(StringComparer.OrdinalIgnoreCase); + foreach (VirtualPageColumn c in requested ?? new List()) + { + if (c == null || !IsIdentifier(c.Field)) + throw new ArgumentException("Invalid column: " + c?.Field); + if (seen.Add(c.Field)) + columns.Add(c); + } + return columns; + } + + /// + /// "column LIKE @vpSearch" for every column, OR-ed, on the text the grid shows: + /// dates "13 Feb 2025", money "1,234.56" (or "1234.56"). + /// + private static string SearchCondition(List columns) + { + const string like = " LIKE @vpSearch ESCAPE '\\'"; + var parts = new List(); + foreach (VirtualPageColumn c in columns) + { + string f = "[" + c.Field + "]"; + switch ((c.Type ?? "").ToLowerInvariant()) + { + case "date": + parts.Add("CONVERT(varchar(11), " + f + ", 106)" + like); + break; + case "datetime": + parts.Add("(CONVERT(varchar(11), " + f + ", 106) + ' ' + CONVERT(varchar(5), " + f + ", 108))" + like); + break; + case "money": + parts.Add("CONVERT(varchar(30), CAST(" + f + " AS money), 1)" + like); + parts.Add("CONVERT(varchar(30), CAST(" + f + " AS money), 0)" + like); + break; + default: + parts.Add("CONVERT(nvarchar(4000), " + f + ")" + like); + break; + } + } + return parts.Count == 0 ? "1 = 0" : "(" + string.Join(" OR ", parts) + ")"; + } + + /// The grid's sort (only by one of its columns), then the key field so the order is always the same. + private static string OrderBy(List columns, VirtualPageRequest request, string keyField) + { + VirtualPageColumn sort = columns.FirstOrDefault(c => string.Equals(c.Field, request.SortField, StringComparison.OrdinalIgnoreCase)); + if (sort == null || string.Equals(sort.Field, keyField, StringComparison.OrdinalIgnoreCase)) + { + bool keyDesc = sort != null && string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); + return "[" + keyField + "]" + (keyDesc ? " DESC" : " ASC"); + } + bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); + return "[" + sort.Field + "]" + (desc ? " DESC" : " ASC") + ", [" + keyField + "] ASC"; + } + + /// Makes %, _, [ and \ in the search text match themselves. + private static string EscapeLike(string text) => + text.Replace("\\", "\\\\").Replace("%", "\\%").Replace("_", "\\_").Replace("[", "\\["); + + private static bool IsIdentifier(string name) => name != null && Identifier.IsMatch(name); + } +} diff --git a/HRM.DA/Service/Employee/EmployeeService.cs b/HRM.DA/Service/Employee/EmployeeService.cs index cb05a7e..a05ad4a 100644 --- a/HRM.DA/Service/Employee/EmployeeService.cs +++ b/HRM.DA/Service/Employee/EmployeeService.cs @@ -2733,26 +2733,19 @@ namespace HRM.DA #region Discontinue Employee Details /// - /// Employees discontinued between fromDate and toDate (by discontinue date) in one payroll type, with - /// the same fields, null handling and department tiers as the Employee Details report. - /// One query for all rows; departments are loaded once and looked up by ID. + /// Discontinue Employee Details: one window of rows for the grid. SQL Server pages, searches and + /// sorts (VirtualPagingDA); the query is EmployeeDA.GetDiscontinuedEmployeeQuery. /// - public List GetDiscontinuedEmployeeDetails(DateTime fromDate, + public VirtualPageResult GetDiscontinuedEmployeePage(VirtualPageRequest request, DateTime fromDate, DateTime toDate, int payrollTypeID) { - var items = new List(); - Dictionary departments = new DepartmentService().GetAll() - .GroupBy(d => d.ID) - .ToDictionary(g => g.Key, g => g.First()); - + VirtualPageResult result = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); - DataReader dr = new DataReader(EmployeeDA.GetDiscontinuedEmployeeDetails(tc, fromDate, toDate, payrollTypeID)); - while (dr.Read()) - items.Add(MapDiscontinuedEmployee(dr, departments)); - dr.Close(); + string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); + result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); tc.End(); } catch (Exception e) @@ -2765,121 +2758,7 @@ namespace HRM.DA #endregion } - return items; - } - - private static HRM.BO.Report.DiscontinueEmployeeDetailInfo MapDiscontinuedEmployee(DataReader dr, - Dictionary departments) - { - var item = new HRM.BO.Report.DiscontinueEmployeeDetailInfo(); - item.EmployeeId = dr.GetInt32("EmployeeId") ?? 0; - item.EmployeeNo = dr.GetString("EMPLOYEENO"); - item.Name = dr.GetString("NAME"); - item.BasicSalary = dr.GetDecimal("BASICSALARY") ?? 0; - item.GrossSalary = dr.GetDecimal("GrossSalary") ?? 0; - item.TaxAmount = dr.GetDecimal("TAXAMOUNT") ?? 0; - item.FatherName = dr.GetString("Fathername"); - item.FatherOccupation = dr.GetString("FatherOccupation"); - item.MotherOccupation = dr.GetString("MotherOccupation"); - item.MotherName = dr.GetString("MotherName"); - item.Gender = dr.GetString("Gender"); - item.BirthDate = dr.GetDateTime("BIRTHDATE"); - item.BirthPlace = dr.GetString("BIRTHPlace"); - item.TinNo = dr.GetString("TINNO"); - item.JoiningDate = dr.GetDateTime("JOININGDATE"); - item.EmailAddress = dr.GetString("EMAILADDRESS"); - item.Category = dr.GetString("Category"); - item.Status = dr.GetString("Status"); - item.Location = dr.GetString("Location"); - item.Division = dr.GetString("Division"); - item.Company = dr.GetString("Company"); - item.Department = dr.GetString("Department"); - item.Section = dr.GetString("Section"); - item.Designation = dr.GetString("DESIGNATION"); - item.Grade = dr.GetString("GRADE"); - item.Religion = dr.GetString("RELIGION"); - item.MaritalStatus = dr.GetString("MaritalStatus"); - item.MobileNo = dr.GetString("MOBILENO"); - item.DateOfConfirmation = dr.GetDateTime("DATEOFCONFIRMATION"); - item.NationalId = dr.GetString("NATIONALID"); - item.PassportNo = dr.GetString("PassPortNo"); - item.PhotoPath = dr.GetString("PhotoPath"); - int? bloodGroup = dr.GetInt32("bloodgroup"); - item.BloodGroup = ((EnumBloodGroup)(bloodGroup ?? (int)EnumBloodGroup.None)).BloodGroupToFriendlyName(); - item.LineManagerNo = dr.GetString("LineManagerNo"); - item.LineManagerName = dr.GetString("LineManagerName"); - item.LineManagerEmail = dr.GetString("LineManagerEmail"); - item.LineManagerDes = dr.GetString("LineManagerDes"); - item.LmDivision = dr.GetString("lmDivision"); - item.LmDepartment = dr.GetString("lmDepartment"); - item.EmpStatus = dr.GetString("EmpStatus"); - item.Nationality = dr.GetString("NATIONALITY"); - item.OutPayBank = dr.GetString("OUTPayBank"); - item.OutPayBranch = dr.GetString("OUTPayBranch"); - item.OutPayAccountNo = dr.GetString("OutPayAccountNo"); - // Bank / Branch / Account No fall back to the out-pay values when empty, as Employee Details shows them. - string accountNo = dr.GetString("ACCOUNTNO"); - string bank = dr.GetString("Bank"); - string branch = dr.GetString("Branch"); - item.AccountNo = string.IsNullOrEmpty(accountNo) ? item.OutPayAccountNo : accountNo; - item.Bank = string.IsNullOrEmpty(bank) ? item.OutPayBank : bank; - item.Branch = string.IsNullOrEmpty(branch) ? item.OutPayBranch : branch; - item.DiscontinueDate = dr.GetDateTime("DiscontinueDate"); - - int? departmentID = dr.GetInt32("EmpDepartmentID"); - Department department = null; - if (departmentID.HasValue) - departments.TryGetValue(departmentID.Value, out department); - string[] tiers = GetDepartmentTierNames(department, departments); - if (tiers != null) - { - item.deptireOne = tiers[0]; - item.deptireTwo = tiers[1]; - item.deptireThree = tiers[2]; - item.deptireFour = tiers[3]; - item.deptireFive = tiers[4]; - } - return item; - } - - /// - /// The five department tier names (tier 1 = top), worked out exactly as the Employee Details report - /// (EmployeeController.GetEmpDetailInfo) does. Null when there is no department. - /// - private static string[] GetDepartmentTierNames(Department dept, Dictionary departments) - { - if (dept == null) - return null; - - Func find = id => - { - Department d = null; - if (id.HasValue) - departments.TryGetValue(id.Value, out d); - return d; - }; - - Department tire5 = null, tire4 = null, tire3 = null, tire2 = null, tire1 = null; - if (dept.ParentID != null) - { - for (int i = dept.Tier; i > 0; i--) - { - if (i == 5) tire5 = find(dept.ID); - if (i == 4) tire4 = tire5 != null ? find(tire5.ParentID) : find(dept.ID); - if (i == 3) tire3 = tire4 != null ? find(tire4.ParentID) : find(dept.ID); - if (i == 2) tire2 = tire3 != null ? find(tire3.ParentID) : find(dept.ID); - if (i == 1) tire1 = tire2 != null ? find(tire2.ParentID) : find(dept.ID); - } - } - - return new[] - { - tire1 == null ? "" : tire1.Name, - tire2 == null ? "" : tire2.Name, - tire3 == null ? "" : tire3.Name, - tire4 == null ? "" : tire4.Name, - tire5 == null ? "" : tire5.Name, - }; + return result; } #endregion diff --git a/HRM.DA/Service/VirtualPaging/README.md b/HRM.DA/Service/VirtualPaging/README.md deleted file mode 100644 index bde5940..0000000 --- a/HRM.DA/Service/VirtualPaging/README.md +++ /dev/null @@ -1,77 +0,0 @@ -# Virtual paging: large lists with virtual scrolling - -Data loads **the usual way**: DA query → service returns `List` → controller endpoint. -Only two common pieces are added: - -| Piece | File | What it does | -|---|---|---| -| `VirtualPaging.Page(...)` | `HRM.DA/Service/VirtualPaging/VirtualPaging.cs` | Keeps the list in memory for a few minutes, then search, sort, 100 rows at a time and the total count | -| `` | `ClientApp/src/app/custom-components/virtual-grid/` (`VirtualGridModule`) | Virtual scrolling, search box, total, Export to Excel | - -`VirtualPageRequest` / `VirtualPageResult` (`HRM.BO/VirtualPaging/VirtualPaging.cs`) are shared by every -page, so a page never needs its own request class. - -## A new page: 2 steps - -**1. Endpoint** in the module's own controller, calling the existing service: - -```csharp -[HttpPost("getDiscontinueEmployeePage")] -public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) -{ - try - { - CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); - DateTime fromDate = request.GetDate("fromDate", "From Date"); // the page's own filters - DateTime toDate = request.GetDate("toDate", "To Date"); - - return Ok(VirtualPaging.Page(request, currentUser, - () => _EmployeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, currentUser.PayrollTypeID ?? 0))); - } - catch (Exception ex) - { - return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); - } -} -``` - -Read filters with `request.GetDate(name)`, `request.GetInt(name)` or `request.GetString(name)` (null when not sent). - -**2. Screen**: import `VirtualGridModule`, then: - -```html - -``` -```ts -columns: VirtualGridColumn[] = [ - { field: 'employeeNo', title: 'Employee No', width: 110 }, - { field: 'joiningDate', title: 'Joining Date', type: 'date' }, - { field: 'grossSalary', title: 'Gross Salary', type: 'money' }, -]; - -preview() { - this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); // any filters; Dates are sent as yyyy-MM-dd -} -``` - -`field` is the property name of the BO (camelCase). Column `type`: `text` (default), `number`, -`money` (1,234.56), `date` (dd MMM yyyy), `datetime`. `sortable: false` turns sorting off for a column. - -That is all. No registry, no extra service, no extra Angular service. - -## How it runs - -1. **Preview** → `grid.load(filters)` → the endpoint with `refresh: true` → the service is called once, - the list is kept in server memory (per user + endpoint + filters, 10 min since last use, 30 min at - most) → the first 100 rows and the total go back. -2. **Scroll / sort / search** → same endpoint, `refresh: false` → answered from the kept list. -3. **Export to Excel** → same endpoint with `take: 0` → every matching row → Kendo builds the `.xlsx`. - -The browser shows ~100 rows at a time, however long the list is. - -## Rules - -- The service must not run one query per row; load lookups once (see `GetDiscontinuedEmployeeDetails`). -- Search looks in the text the grid shows (dates `dd MMM yyyy`, money `1,234.56`). -- Authorization is menu-driven in this app: put any extra check the page needs in its endpoint. diff --git a/HRM.DA/Service/VirtualPaging/VirtualPaging.cs b/HRM.DA/Service/VirtualPaging/VirtualPaging.cs deleted file mode 100644 index e9f7f1e..0000000 --- a/HRM.DA/Service/VirtualPaging/VirtualPaging.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; -using HRM.BO; -using Microsoft.Extensions.Caching.Memory; - -namespace HRM.DA -{ - /// - /// Server side of <app-virtual-grid>: turns any List<T> into one window of rows (search, sort, - /// skip/take, total count). A page endpoint calls it with its existing service method: - /// - /// [HttpPost("getDiscontinueEmployeePage")] - /// public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) - /// { - /// CurrentUser user = CurrentUser.GetCurrentUser(HttpContext.User); - /// DateTime fromDate = request.GetDate("fromDate", "From Date"); - /// DateTime toDate = request.GetDate("toDate", "To Date"); - /// return Ok(VirtualPaging.Page(request, user, - /// () => _employeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, user.PayrollTypeID.Value))); - /// } - /// - /// The list is loaded on Preview (request.Refresh) and kept in memory for a few minutes, per user, - /// endpoint and filters, so scrolling, sorting, searching and Export to Excel do not call the - /// service again. See README.md next to this file. - /// - public static class VirtualPaging - { - private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000000 }); // max rows kept, all users together - private static readonly TimeSpan KeepFor = TimeSpan.FromMinutes(10); // since last use - private static readonly TimeSpan KeepAtMost = TimeSpan.FromMinutes(30); // since loaded - private const int MaxRowsPerPage = 1000; - - public static VirtualPageResult Page(VirtualPageRequest request, CurrentUser user, Func> loadData, - [CallerFilePath] string callerFile = "", [CallerMemberName] string callerName = "") where T : class - { - if (request == null) - throw new ArgumentException("Invalid request."); - - // 1. The list: from memory, or from the service on Preview / when it has expired. - string key = CacheKey(request, user, callerFile + "|" + callerName + "|" + typeof(T).FullName); - KeptList kept; - if (request.Refresh || !Cache.TryGetValue(key, out kept)) - { - kept = new KeptList(loadData() ?? new List()); - Cache.Set(key, kept, new MemoryCacheEntryOptions - { - Size = Math.Max(1, kept.Items.Count), - SlidingExpiration = KeepFor, - AbsoluteExpirationRelativeToNow = KeepAtMost, - }); - } - - List fields = GetFields(request.Fields); - IEnumerable rows = Enumerable.Range(0, kept.Items.Count); - - // 2. Search: the text the grid shows, in any of the grid's columns. - string search = (request.Search ?? "").Trim(); - if (search.Length > 0) - { - List texts = kept.SearchTexts(fields); - rows = rows.Where(i => texts[i].IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0); - } - List matched = rows.ToList(); - - // 3. Sort: only by one of the grid's columns. - PropertyInfo sortBy = fields.FirstOrDefault(p => string.Equals(p.Name, request.SortField, StringComparison.OrdinalIgnoreCase)); - if (sortBy != null) - { - bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); - matched = desc - ? matched.OrderByDescending(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList() - : matched.OrderBy(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList(); - } - - // 4. The window: Take 0 = every row (Export to Excel). - int skip = Math.Max(0, request.Skip); - int take = request.Take <= 0 ? matched.Count : Math.Min(request.Take, MaxRowsPerPage); - - var result = new VirtualPageResult { Total = matched.Count, TotalWithoutSearch = kept.Items.Count }; - foreach (int i in matched.Skip(skip).Take(take)) - { - var row = new Dictionary(fields.Count); - foreach (PropertyInfo p in fields) - row[CamelCase(p.Name)] = p.GetValue(kept.Items[i]); - result.Data.Add(row); - } - return result; - } - - #region Helpers - - /// A loaded list, plus the search text of each row (worked out once, on the first search). - private sealed class KeptList - { - private readonly ConcurrentDictionary> _searchTexts = new ConcurrentDictionary>(); - - public KeptList(List items) - { - Items = items.Where(x => x != null).ToList(); - } - - public List Items { get; } - - public List SearchTexts(List fields) - { - string key = string.Join(",", fields.Select(p => p.Name)); - return _searchTexts.GetOrAdd(key, _ => Items - .Select(item => string.Join("\u0001", fields.Select(p => DisplayText(p.GetValue(item))))) - .ToList()); - } - } - - /// The requested fields that are readable properties of T (unknown names are ignored). None requested = all. - private static List GetFields(List names) - { - // When a class hides a base property with "new", keep the most derived one. - var properties = new Dictionary(StringComparer.OrdinalIgnoreCase); - for (Type t = typeof(T); t != null && t != typeof(object); t = t.BaseType) - { - foreach (PropertyInfo p in t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) - { - if (p.CanRead && p.GetIndexParameters().Length == 0 && !properties.ContainsKey(p.Name)) - properties[p.Name] = p; - } - } - - if (names == null || names.Count == 0) - return properties.Values.ToList(); - - var result = new List(); - foreach (string name in names.Distinct(StringComparer.OrdinalIgnoreCase)) - { - PropertyInfo p; - if (name != null && properties.TryGetValue(name, out p)) - result.Add(p); - } - return result; - } - - /// The text the grid shows for a value: dates "dd MMM yyyy", decimals "1,234.56". - private static string DisplayText(object value) - { - if (value == null) - return string.Empty; - if (value is DateTime d) - return d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); - if (value is decimal || value is double || value is float) - { - decimal number = Convert.ToDecimal(value, CultureInfo.InvariantCulture); - return number.ToString("N2", CultureInfo.InvariantCulture) + "\u0001" + number.ToString(CultureInfo.InvariantCulture); - } - if (value is Enum) - return value + "\u0001" + Convert.ToInt32(value, CultureInfo.InvariantCulture); - return Convert.ToString(value, CultureInfo.InvariantCulture); - } - - private static string CacheKey(VirtualPageRequest request, CurrentUser user, string endpoint) - { - string filters = string.Join("&", (request.Filters ?? new Dictionary()) - .OrderBy(f => f.Key, StringComparer.OrdinalIgnoreCase) - .Select(f => f.Key.ToLowerInvariant() + "=" + f.Value)); - return endpoint + "|" + user?.UserID + "|" + user?.PayrollTypeID + "|" + filters; - } - - private static string CamelCase(string name) => - string.IsNullOrEmpty(name) ? name : char.ToLowerInvariant(name[0]) + name.Substring(1); - - /// Empty values first; numbers, dates and text compared as such (text ignoring case). - private sealed class ValueComparer : IComparer - { - public static readonly ValueComparer Instance = new ValueComparer(); - - public int Compare(object x, object y) - { - bool xEmpty = x == null || (x is string xs && xs.Length == 0); - bool yEmpty = y == null || (y is string ys && ys.Length == 0); - if (xEmpty || yEmpty) - return xEmpty == yEmpty ? 0 : (xEmpty ? -1 : 1); - if (x is string a && y is string b) - return StringComparer.InvariantCultureIgnoreCase.Compare(a, b); - if (IsNumber(x) && IsNumber(y)) - return Convert.ToDecimal(x, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(y, CultureInfo.InvariantCulture)); - if (x.GetType() == y.GetType() && x is IComparable comparable) - return comparable.CompareTo(y); - return StringComparer.InvariantCultureIgnoreCase.Compare( - Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture)); - } - - private static bool IsNumber(object v) => - v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; - } - - #endregion - } -} diff --git a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts index 9b53a8f..e4528f0 100644 --- a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts +++ b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts @@ -1,6 +1,6 @@ /** * (custom-components/virtual-grid) and its server side - * (HRM.BO/VirtualPaging/VirtualPaging.cs, HRM.DA/Service/VirtualPaging/VirtualPaging.cs). + * (HRM.BO/VirtualPaging/VirtualPaging.cs, HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs). */ /** How a column's values are shown (and exported to Excel). */ @@ -18,19 +18,19 @@ export interface VirtualGridColumn { /** What the grid posts to the page endpoint (VirtualPageRequest on the server). */ export interface VirtualPageRequest { filters: { [name: string]: string }; - fields: string[]; + columns: { field: string; type: VirtualGridColumnType }[]; skip: number; take: number; // 0 = all rows (Export to Excel) search?: string; sortField?: string; sortDir?: 'asc' | 'desc'; - refresh: boolean; // true on Preview + countTotal: boolean; // true on Preview and on a new search; scroll/sort keep the totals } /** What the page endpoint returns (VirtualPageResult on the server). */ export interface VirtualPageResult { - total: number; - totalWithoutSearch: number; + total: number | null; // null when not counted (countTotal = false) + totalWithoutSearch: number | null; data: { [field: string]: any }[]; } diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts index 3ded9a0..781d4e8 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts @@ -23,8 +23,8 @@ import { * * this.grid.load({ fromDate: this.fromDate, toDate: this.toDate }); * - * The grid loads 100 rows at a time while the user scrolls; search, sort and the total count are done - * on the server (VirtualPaging.Page). Export to Excel downloads every matching row. + * The grid asks for 100 rows at a time while the user scrolls; SQL Server reads just those rows and does + * the search, sort and total count (VirtualPagingDA). Export to Excel downloads every matching row. */ @Component({ selector: 'app-virtual-grid', @@ -36,7 +36,7 @@ import { export class VirtualGridComponent implements OnInit, OnDestroy { /** The page endpoint, e.g. '/Employee/getDiscontinueEmployeePage'. Required. */ @Input() url: string; - /** The columns. field = property name of the server object (camelCase). Required. */ + /** The columns. field = a column alias of the page's SQL query (camelCase). Required. */ @Input() columns: VirtualGridColumn[] = []; @Input() height = 600; @Input() pageSize = 100; @@ -66,6 +66,7 @@ export class VirtualGridComponent implements OnInit, OnDestroy { private filters: { [name: string]: string } = {}; private firstLoad = false; + private totalsPending = false; private readonly load$ = new Subject(); private readonly search$ = new Subject(); private readonly subscriptions = new Subscription(); @@ -97,7 +98,7 @@ export class VirtualGridComponent implements OnInit, OnDestroy { this.search$.pipe(debounceTime(400), distinctUntilChanged()).subscribe(text => { this.appliedSearch = text; this.skip = 0; - this.requestPage(false); + this.requestPage(true); // a new search changes the total }) ); } @@ -198,23 +199,26 @@ export class VirtualGridComponent implements OnInit, OnDestroy { return Math.max(column.width || 130, (column.title || '').length * 8 + 48); } - private requestPage(refresh: boolean): void { + /** countTotal: true on Preview and a new search (the total can change); scroll and sort keep the totals. */ + private requestPage(countTotal: boolean): void { if (this.started && this.url) { - this.load$.next(this.buildRequest(this.skip, this.pageSize, refresh)); + // Keep asking for the totals until a response brings them (a newer request cancels an older one). + this.totalsPending = this.totalsPending || countTotal; + this.load$.next(this.buildRequest(this.skip, this.pageSize, this.totalsPending)); } } - private buildRequest(skip: number, take: number, refresh: boolean): VirtualPageRequest { + private buildRequest(skip: number, take: number, countTotal: boolean): VirtualPageRequest { const sort = this.sort && this.sort.length > 0 && this.sort[0].dir ? this.sort[0] : null; return { filters: this.filters, - fields: this.columns.map(c => c.field), + columns: this.columns.map(c => ({ field: c.field, type: c.type || 'text' })), skip: skip, take: take, search: this.appliedSearch || undefined, sortField: sort ? sort.field : undefined, sortDir: sort ? (sort.dir as 'asc' | 'desc') : undefined, - refresh: refresh, + countTotal: countTotal, }; } @@ -222,9 +226,17 @@ export class VirtualGridComponent implements OnInit, OnDestroy { if (!result) { return; } - this.gridView = { data: (result.data || []).map(row => this.prepareRow(row)), total: result.total }; - this.totalWithoutSearch = result.totalWithoutSearch; - this.loaded.emit({ total: result.total, totalWithoutSearch: result.totalWithoutSearch, firstLoad: this.firstLoad }); + // Totals come only when they were counted; otherwise keep the ones we have. + const counted = result.total !== null && result.total !== undefined; + if (counted) { + this.totalsPending = false; + } + const total = counted ? result.total : this.gridView.total; + if (result.totalWithoutSearch !== null && result.totalWithoutSearch !== undefined) { + this.totalWithoutSearch = result.totalWithoutSearch; + } + this.gridView = { data: (result.data || []).map(row => this.prepareRow(row)), total: total }; + this.loaded.emit({ total: total, totalWithoutSearch: this.totalWithoutSearch, firstLoad: this.firstLoad }); this.firstLoad = false; } diff --git a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts index 9e8623d..22da6e7 100644 --- a/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts +++ b/HRM.UI/ClientApp/src/app/reports/discontinue-employee-details/discontinue-employee-details.component.ts @@ -11,7 +11,8 @@ import { EnumConfigurationType } from 'src/app/_models/enums'; * Discontinue Employee Details: employees discontinued inside a date range, with the same columns as * Employee Details (/reports/report-viewer/empDetailInfo) plus the Discontinue Date. * - * Data: EmployeeController.GetDiscontinueEmployeePage → EmployeeService.GetDiscontinuedEmployeeDetails. + * Data: EmployeeController.GetDiscontinueEmployeePage → EmployeeService.GetDiscontinuedEmployeePage + * → EmployeeDA.GetDiscontinuedEmployeeQuery (SQL) + VirtualPagingDA (paging in SQL Server). * The common does virtual scrolling, search, sort, total and Excel. */ @Component({ @@ -28,7 +29,7 @@ export class DiscontinueEmployeeDetailsComponent implements OnInit { toDate: Date = new Date(); dateFormat = 'dd-MMM-yyyy'; - /** Grid columns. field = property of DiscontinueEmployeeDetailInfo (camelCase). */ + /** Grid columns. field = a column alias in EmployeeDA.GetDiscontinuedEmployeeQuery. */ columns: VirtualGridColumn[] = []; constructor(public apiService: ApiService, diff --git a/HRM.UI/Controllers/Employee/EmployeeController.cs b/HRM.UI/Controllers/Employee/EmployeeController.cs index 174fea6..34e9e6b 100644 --- a/HRM.UI/Controllers/Employee/EmployeeController.cs +++ b/HRM.UI/Controllers/Employee/EmployeeController.cs @@ -3412,7 +3412,7 @@ namespace HRM.UI.Controllers /// /// Discontinue Employee Details (Manage Employee › Report): one window of rows for <app-virtual-grid>. - /// Filters: fromDate, toDate ("yyyy-MM-dd"). VirtualPaging does the search, sort and paging. + /// Filters: fromDate, toDate ("yyyy-MM-dd"). SQL Server does the paging, search and sort (VirtualPagingDA). /// [HttpPost("getDiscontinueEmployeePage")] public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) @@ -3420,11 +3420,19 @@ namespace HRM.UI.Controllers try { CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User); + if (currentUser.UserType == EnumUserType.Employee) // self-service logins never see this report + return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to see this report."); + DateTime fromDate = request.GetDate("fromDate", "From Date"); DateTime toDate = request.GetDate("toDate", "To Date"); + if (fromDate > toDate) + return BadRequest("From Date cannot be after To Date."); - return Ok(VirtualPaging.Page(request, currentUser, - () => _EmployeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, currentUser.PayrollTypeID ?? 0))); + return Ok(_EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0)); + } + catch (ArgumentException ex) + { + return BadRequest(ex.Message); } catch (Exception ex) { From 186a5b60a8b0d1b88ae38f5e665a1eefa80ed734 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 16:39:51 +0600 Subject: [PATCH 7/8] Excel Export Discontinue --- HRM.BO/VirtualPaging/VirtualPaging.cs | 17 ++ HRM.DA/DA/VirtualPaging/README.md | 8 +- HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs | 15 +- HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs | 211 ++++++++++++++++++ .../src/app/_models/common/virtual-grid.ts | 3 +- .../virtual-grid/virtual-grid.component.html | 9 - .../virtual-grid/virtual-grid.component.ts | 60 ++--- .../virtual-grid/virtual-grid.module.ts | 5 +- .../Employee/EmployeeController.cs | 5 +- 9 files changed, 278 insertions(+), 55 deletions(-) create mode 100644 HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs diff --git a/HRM.BO/VirtualPaging/VirtualPaging.cs b/HRM.BO/VirtualPaging/VirtualPaging.cs index d4cd09b..4aef283 100644 --- a/HRM.BO/VirtualPaging/VirtualPaging.cs +++ b/HRM.BO/VirtualPaging/VirtualPaging.cs @@ -34,6 +34,12 @@ namespace HRM.BO /// public bool CountTotal { get; set; } + /// + /// True for Export to Excel: every row matching the search, in the grid's sort, comes back as an .xlsx + /// file (VirtualPageResult.ExcelFile) instead of JSON. + /// + public bool ExportExcel { get; set; } + /// A filter as text, or null when it was not sent. public string GetString(string name) { @@ -69,6 +75,12 @@ namespace HRM.BO /// "text" (default), "number", "money", "date" or "datetime". public string Type { get; set; } + + /// Header text in the Excel file. + public string Title { get; set; } + + /// Column width in the grid (px); the Excel column gets a matching width. + public int Width { get; set; } } /// One window of rows for the grid. @@ -82,5 +94,10 @@ namespace HRM.BO /// The rows of this window, holding only the requested columns. public List> Data { get; set; } = new List>(); + + /// The .xlsx file, set only for ExportExcel requests. The controller returns it as a file. + public byte[] ExcelFile { get; set; } + + public const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } } diff --git a/HRM.DA/DA/VirtualPaging/README.md b/HRM.DA/DA/VirtualPaging/README.md index 439cd70..bb54fd8 100644 --- a/HRM.DA/DA/VirtualPaging/README.md +++ b/HRM.DA/DA/VirtualPaging/README.md @@ -8,6 +8,7 @@ memory, so it works the same for 1,000 or 1,000,000 rows and always shows the la |---|---|---| | Request / result | `HRM.BO/VirtualPaging/VirtualPaging.cs` | `VirtualPageRequest` (filters, columns, skip/take, search, sort) and `VirtualPageResult`, shared by every page | | Paging SQL | `HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs` | Wraps a page's query with search, sort, `OFFSET/FETCH` and the totals | +| Excel | `HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs` | Streams the rows from SQL Server straight into an `.xlsx` (Open XML SAX writer) | | Grid | `ClientApp/src/app/custom-components/virtual-grid/` (`VirtualGridModule`) | ``: virtual scrolling, search box, total, Export to Excel | ## A new page: 4 small steps (worked example: Discontinue Employee Details) @@ -69,7 +70,10 @@ public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) DateTime fromDate = request.GetDate("fromDate", "From Date"); // also GetInt / GetString DateTime toDate = request.GetDate("toDate", "To Date"); - return Ok(_EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0)); + VirtualPageResult result = _EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0); + if (result.ExcelFile != null) // Export to Excel + return File(result.ExcelFile, VirtualPageResult.ExcelContentType, "DiscontinueEmployeeDetails.xlsx"); + return Ok(result); } catch (ArgumentException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } @@ -105,7 +109,7 @@ preview() { | Scroll | `skip 100, 200, …` | next 100 rows only (no `COUNT`) | | Sort | `sortField`, `sortDir` | `ORDER BY` that column, then the unique field | | Search (after 400 ms) | `search`, `countTotal true` | `WHERE` every column `LIKE @vpSearch`, new `COUNT` | -| Export to Excel | `take 0` | every matching row, same search and sort; Kendo builds the `.xlsx` | +| Export to Excel | `exportExcel true` | every matching row, same search and sort, written row by row into an `.xlsx` on the server; the browser only downloads the file | ## Rules diff --git a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs index 0b56d90..decd485 100644 --- a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs +++ b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs @@ -47,12 +47,14 @@ namespace HRM.DA string source = " FROM (" + query + ") AS vp"; string searchCondition = search.Length > 0 ? SearchCondition(columns) : null; - bool paged = request.Take > 0; + bool export = request.ExportExcel; // every matching row, written straight into an .xlsx + bool paged = !export && request.Take > 0; + bool countTotal = !export && request.CountTotal; var sql = new StringBuilder(); // 1. Totals, only on Preview / new search (scroll and sort do not change them). One scan for both. - if (request.CountTotal) + if (countTotal) { sql.Append("SELECT COUNT(*) AS TotalWithoutSearch, ") .Append(searchCondition == null ? "COUNT(*)" : "ISNULL(SUM(CASE WHEN " + searchCondition + " THEN 1 ELSE 0 END), 0)") @@ -80,7 +82,14 @@ namespace HRM.DA var result = new VirtualPageResult(); using (IDataReader reader = tc.ExecuteReader(CommandType.Text, sql.ToString(), parameters.ToArray())) { - if (request.CountTotal) + // Export to Excel: rows go from SQL Server into the file one by one, never all held in memory. + if (export) + { + result.ExcelFile = VirtualPagingExcel.Create(reader, columns); + return result; + } + + if (countTotal) { if (reader.Read()) { diff --git a/HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs b/HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs new file mode 100644 index 0000000..0191b18 --- /dev/null +++ b/HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Globalization; +using System.IO; +using System.Text; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using HRM.BO; + +namespace HRM.DA +{ + /// + /// Export to Excel for <app-virtual-grid>: writes the rows of a data reader straight into an .xlsx + /// with the Open XML SAX writer (OpenXmlWriter), one row at a time. Fast and light on memory even for + /// very long lists, unlike building the workbook in the browser or as a full object model. + /// + /// The file: a bold, frozen header row with filters, the grid's column titles and widths, dates as real + /// Excel dates ("dd mmm yyyy") and money as numbers ("#,##0.00"). Text is written as text, never as a formula. + /// + internal static class VirtualPagingExcel + { + private const int MaxRows = 1000000; // Excel's limit is 1,048,576 + + // Style indexes in the stylesheet below. + private const uint StyleHeader = 1, StyleDate = 2, StyleDateTime = 3, StyleMoney = 4; + + internal static byte[] Create(IDataReader reader, List columns) + { + // Columns to write: the grid's, or every field of the query when none were sent. + var fields = new List(); + if (columns.Count == 0) + { + for (int i = 0; i < reader.FieldCount; i++) + fields.Add(new ExcelColumn { Ordinal = i, Title = reader.GetName(i), Type = "text", Width = 15 }); + } + else + { + foreach (VirtualPageColumn c in columns) + { + fields.Add(new ExcelColumn + { + Ordinal = reader.GetOrdinal(c.Field), + Title = string.IsNullOrWhiteSpace(c.Title) ? c.Field : c.Title, + Type = (c.Type ?? "text").ToLowerInvariant(), + Width = Math.Max(10, Math.Min(60, (c.Width > 0 ? c.Width : 130) / 7.0)), + }); + } + } + + using (var stream = new MemoryStream()) + { + using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook)) + { + WorkbookPart workbookPart = document.AddWorkbookPart(); + workbookPart.Workbook = new Workbook(); + + WorkbookStylesPart stylesPart = workbookPart.AddNewPart(); + stylesPart.Stylesheet = CreateStylesheet(); + stylesPart.Stylesheet.Save(); + + WorksheetPart sheetPart = workbookPart.AddNewPart(); + int lastRow = WriteSheet(sheetPart, reader, fields); + + string range = "$A$1:$" + ColumnName(fields.Count) + "$" + lastRow; + workbookPart.Workbook.AppendChild(new Sheets( + new Sheet { Id = workbookPart.GetIdOfPart(sheetPart), SheetId = 1U, Name = "Sheet1" })); + workbookPart.Workbook.AppendChild(new DefinedNames( + new DefinedName("'Sheet1'!" + range) { Name = "_xlnm._FilterDatabase", LocalSheetId = 0U, Hidden = true })); + workbookPart.Workbook.Save(); + } + return stream.ToArray(); + } + } + + /// Header + every row, streamed. Returns the last row number. + private static int WriteSheet(WorksheetPart sheetPart, IDataReader reader, List fields) + { + int rowIndex = 1; + using (OpenXmlWriter writer = OpenXmlWriter.Create(sheetPart)) + { + writer.WriteStartElement(new Worksheet()); + + // Header row stays visible while scrolling. + writer.WriteElement(new SheetViews(new SheetView( + new Pane { VerticalSplit = 1D, TopLeftCell = "A2", ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen }) + { WorkbookViewId = 0U })); + + writer.WriteStartElement(new Columns()); + for (int i = 0; i < fields.Count; i++) + writer.WriteElement(new Column { Min = (uint)(i + 1), Max = (uint)(i + 1), Width = fields[i].Width, CustomWidth = true }); + writer.WriteEndElement(); + + writer.WriteStartElement(new SheetData()); + + writer.WriteStartElement(new Row { RowIndex = 1U }); + foreach (ExcelColumn f in fields) + writer.WriteElement(TextCell(f.Title, StyleHeader)); + writer.WriteEndElement(); + + while (rowIndex <= MaxRows && reader.Read()) + { + rowIndex++; + writer.WriteStartElement(new Row { RowIndex = (uint)rowIndex }); + foreach (ExcelColumn f in fields) + writer.WriteElement(ValueCell(reader.IsDBNull(f.Ordinal) ? null : reader.GetValue(f.Ordinal), f.Type)); + writer.WriteEndElement(); + } + + writer.WriteEndElement(); // SheetData + writer.WriteElement(new AutoFilter { Reference = "A1:" + ColumnName(fields.Count) + rowIndex }); + writer.WriteEndElement(); // Worksheet + } + return rowIndex; + } + + private static Cell ValueCell(object value, string type) + { + if (value == null) + return new Cell(); + + if (value is DateTime date && (type == "date" || type == "datetime")) + return new Cell { CellValue = new CellValue(date.ToOADate()), StyleIndex = type == "date" ? StyleDate : StyleDateTime }; + + if (IsNumber(value) && (type == "money" || type == "number")) + { + var cell = new Cell { CellValue = new CellValue(Convert.ToDecimal(value, CultureInfo.InvariantCulture)) }; + if (type == "money") + cell.StyleIndex = StyleMoney; + return cell; + } + + string text = value is DateTime d ? d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture) + : Convert.ToString(value, CultureInfo.InvariantCulture); + return TextCell(text, 0); + } + + /// An inline text cell (never a formula). Characters XML cannot hold are dropped. + private static Cell TextCell(string text, uint style) + { + var cell = new Cell + { + DataType = CellValues.InlineString, + InlineString = new InlineString(new Text(CleanText(text)) { Space = SpaceProcessingModeValues.Preserve }), + }; + if (style != 0) + cell.StyleIndex = style; + return cell; + } + + private static Stylesheet CreateStylesheet() + { + return new Stylesheet( + new NumberingFormats( + new NumberingFormat { NumberFormatId = 164U, FormatCode = "dd mmm yyyy" }, + new NumberingFormat { NumberFormatId = 165U, FormatCode = "dd mmm yyyy hh:mm" }) { Count = 2U }, + new Fonts( + new Font(new FontSize { Val = 11D }, new FontName { Val = "Calibri" }), + new Font(new Bold(), new FontSize { Val = 11D }, new FontName { Val = "Calibri" })) { Count = 2U }, + new Fills( + new Fill(new PatternFill { PatternType = PatternValues.None }), + new Fill(new PatternFill { PatternType = PatternValues.Gray125 }), + new Fill(new PatternFill(new ForegroundColor { Rgb = "FFE8ECF2" }) { PatternType = PatternValues.Solid })) { Count = 3U }, + new Borders(new Border()) { Count = 1U }, + new CellFormats( + new CellFormat(), // 0 default + new CellFormat { FontId = 1U, FillId = 2U, ApplyFont = true, ApplyFill = true }, // 1 header + new CellFormat { NumberFormatId = 164U, ApplyNumberFormat = true }, // 2 date + new CellFormat { NumberFormatId = 165U, ApplyNumberFormat = true }, // 3 date + time + new CellFormat { NumberFormatId = 4U, ApplyNumberFormat = true }) { Count = 5U }); // 4 money #,##0.00 + } + + private static bool IsNumber(object v) => + v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; + + private static string CleanText(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + var sb = new StringBuilder(text.Length); + foreach (char ch in text) + { + if (ch == '\t' || ch == '\n' || ch == '\r' || ch >= 0x20 && ch != 0xFFFE && ch != 0xFFFF) + sb.Append(ch); + } + return sb.ToString(); + } + + /// 1 → A, 27 → AA. + private static string ColumnName(int number) + { + string name = string.Empty; + while (number > 0) + { + int m = (number - 1) % 26; + name = (char)('A' + m) + name; + number = (number - 1) / 26; + } + return name; + } + + private sealed class ExcelColumn + { + public int Ordinal; + public string Title; + public string Type; + public double Width; + } + } +} diff --git a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts index e4528f0..3b5c1f2 100644 --- a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts +++ b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts @@ -18,13 +18,14 @@ export interface VirtualGridColumn { /** What the grid posts to the page endpoint (VirtualPageRequest on the server). */ export interface VirtualPageRequest { filters: { [name: string]: string }; - columns: { field: string; type: VirtualGridColumnType }[]; + columns: { field: string; type: VirtualGridColumnType; title: string; width: number }[]; skip: number; take: number; // 0 = all rows (Export to Excel) search?: string; sortField?: string; sortDir?: 'asc' | 'desc'; countTotal: boolean; // true on Preview and on a new search; scroll/sort keep the totals + exportExcel?: boolean; // true: the server returns every matching row as an .xlsx file } /** What the page endpoint returns (VirtualPageResult on the server). */ diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html index 5609f05..ee2de8e 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html @@ -39,15 +39,6 @@ } - - - @for (column of columns; track column.field) { - - - } - - {{ noRecordsText }} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts index 781d4e8..24d80e1 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts @@ -1,12 +1,12 @@ import { - ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, OnInit, Optional, Output, ViewChild + ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, OnInit, Optional, Output } from '@angular/core'; import { formatDate } from '@angular/common'; import { of, Subject, Subscription } from 'rxjs'; import { catchError, debounceTime, distinctUntilChanged, finalize, switchMap } from 'rxjs/operators'; -import { ExcelExportData } from '@progress/kendo-angular-excel-export'; -import { GridComponent, GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; +import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; import { SortDescriptor } from '@progress/kendo-data-query'; +import { saveAs } from 'file-saver'; 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'; @@ -51,8 +51,6 @@ export class VirtualGridComponent implements OnInit, OnDestroy { /** After every loaded window. */ @Output() loaded = new EventEmitter(); - @ViewChild(GridComponent) kendoGrid: GridComponent; - started = false; gridView: GridDataResult = { data: [], total: 0 }; totalWithoutSearch = 0; @@ -62,7 +60,6 @@ export class VirtualGridComponent implements OnInit, OnDestroy { searchText = ''; appliedSearch = ''; exporting = false; - exportRows: any[] = []; private filters: { [name: string]: string } = {}; private firstLoad = false; @@ -143,32 +140,24 @@ export class VirtualGridComponent implements OnInit, OnDestroy { this.search$.next((text || '').trim()); } - /** Loads every matching row (same search and sort), then lets Kendo build the Excel file. */ + /** + * The server builds the .xlsx (every row matching the search, in the grid's sort) and the browser + * only downloads it, so a long list never has to be loaded or processed in the page. + */ exportExcel(): void { if (!this.started || this.gridView.total === 0 || this.exporting) { return; } this.setExporting(true); - this.apiService.httpPost(this.url, this.buildRequest(0, 0, false)).subscribe( - (result) => { - this.exportRows = (result.data || []).map(row => this.prepareRow(row)); - setTimeout(() => this.kendoGrid.saveAsExcel()); - }, - (err) => { - this.setExporting(false); - this.showError(this.errorText(err, 'Failed to export to Excel.')); - } + const request: VirtualPageRequest = { ...this.buildRequest(0, 0, false), exportExcel: true }; + this.apiService.httpDownloadFile(this.url, request).pipe( + finalize(() => this.setExporting(false)) + ).subscribe( + (file: Blob) => saveAs(file, this.exportFileName), + (err) => this.showDownloadError(err, 'Failed to export to Excel.') ); } - /** Kendo asks for the rows to put in the Excel file. */ - excelData = (): ExcelExportData => ({ data: this.exportRows }); - - onExcelCreated(): void { - this.exportRows = []; - this.setExporting(false); - } - /** Grid display format by column type. */ displayFormat(column: VirtualGridColumn): string { switch (column.type) { @@ -179,17 +168,6 @@ export class VirtualGridComponent implements OnInit, OnDestroy { } } - /** Excel cell format by column type. */ - excelOptions(column: VirtualGridColumn): any { - switch (column.type) { - case 'date': return { format: 'dd MMM yyyy' }; - case 'datetime': return { format: 'dd MMM yyyy HH:mm' }; - case 'money': return { format: '#,##0.00', textAlign: 'right' }; - case 'number': return { textAlign: 'right' }; - default: return {}; - } - } - isNumber(column: VirtualGridColumn): boolean { return column.type === 'money' || column.type === 'number'; } @@ -212,7 +190,7 @@ export class VirtualGridComponent implements OnInit, OnDestroy { const sort = this.sort && this.sort.length > 0 && this.sort[0].dir ? this.sort[0] : null; return { filters: this.filters, - columns: this.columns.map(c => ({ field: c.field, type: c.type || 'text' })), + columns: this.columns.map(c => ({ field: c.field, type: c.type || 'text', title: c.title, width: this.columnWidth(c) })), skip: skip, take: take, search: this.appliedSearch || undefined, @@ -267,6 +245,16 @@ export class VirtualGridComponent implements OnInit, OnDestroy { return fallback; } + /** A failed download returns its error message as a Blob; read it before showing it. */ + private showDownloadError(err: any, fallback: string): void { + const blob: Blob = err instanceof Blob ? err : (err && err.error instanceof Blob ? err.error : null); + if (blob) { + blob.text().then(text => this.showError(text || fallback), () => this.showError(fallback)); + } else { + this.showError(this.errorText(err, fallback)); + } + } + private showError(message: string): void { if (this.notificationService) { this.notificationService.showError(message); diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts index 8ba6e01..3479021 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts @@ -1,8 +1,7 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; -import { ExcelModule, GridModule } from '@progress/kendo-angular-grid'; -import { ExcelExportModule } from '@progress/kendo-angular-excel-export'; +import { GridModule } from '@progress/kendo-angular-grid'; import { ButtonsModule } from '@progress/kendo-angular-buttons'; import { TextBoxModule } from '@progress/kendo-angular-inputs'; import { VirtualGridComponent } from './virtual-grid.component'; @@ -10,7 +9,7 @@ import { VirtualGridComponent } from './virtual-grid.component'; /** Import this module to use (common virtual-scrolling grid). */ @NgModule({ declarations: [VirtualGridComponent], - imports: [CommonModule, FormsModule, GridModule, ExcelModule, ExcelExportModule, ButtonsModule, TextBoxModule], + imports: [CommonModule, FormsModule, GridModule, ButtonsModule, TextBoxModule], exports: [VirtualGridComponent] }) export class VirtualGridModule {} diff --git a/HRM.UI/Controllers/Employee/EmployeeController.cs b/HRM.UI/Controllers/Employee/EmployeeController.cs index 34e9e6b..6bc47af 100644 --- a/HRM.UI/Controllers/Employee/EmployeeController.cs +++ b/HRM.UI/Controllers/Employee/EmployeeController.cs @@ -3428,7 +3428,10 @@ namespace HRM.UI.Controllers if (fromDate > toDate) return BadRequest("From Date cannot be after To Date."); - return Ok(_EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0)); + VirtualPageResult result = _EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0); + if (result.ExcelFile != null) // Export to Excel + return File(result.ExcelFile, VirtualPageResult.ExcelContentType, "DiscontinueEmployeeDetails.xlsx"); + return Ok(result); } catch (ArgumentException ex) { From 1e8b51b706c56a8d4e06dd763a53ad4cdfede878 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Mon, 9 Nov 2026 17:22:45 +0600 Subject: [PATCH 8/8] Employee Detail Information --- HRM.BO/Employee/Employee.cs | 1 + HRM.BO/VirtualPaging/VirtualPaging.cs | 21 +++++ HRM.DA/DA/Employee/EmployeeDA.cs | 59 ++++++++---- HRM.DA/DA/VirtualPaging/README.md | 21 ++++- HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs | 93 +++++++++++++++---- HRM.DA/Service/Employee/EmployeeService.cs | 36 ++++++- .../report-viewer.component.html | 10 ++ .../report-viewer/report-viewer.component.ts | 82 ++++++++-------- .../Employee/EmployeeController.cs | 31 +++++++ 9 files changed, 278 insertions(+), 76 deletions(-) diff --git a/HRM.BO/Employee/Employee.cs b/HRM.BO/Employee/Employee.cs index 14a1a81..10219f7 100644 --- a/HRM.BO/Employee/Employee.cs +++ b/HRM.BO/Employee/Employee.cs @@ -608,6 +608,7 @@ namespace HRM.BO DataSet GetEmpDetailsView(); // Discontinue Employee Details report: employees discontinued (Status = Discontinued) whose // discontinue date (ENDOFCONTRACTDATE) falls inside the range, in one payroll type. + VirtualPageResult GetEmpDetailInfoPage(VirtualPageRequest request, List employeeIds, int payrollTypeID); VirtualPageResult GetDiscontinuedEmployeePage(VirtualPageRequest request, DateTime fromDate, DateTime toDate, int payrollTypeID); int GetCurrentHeadCount(int departmentId, int designationId, int gradeId); List GetbyDepartment(int deptId); diff --git a/HRM.BO/VirtualPaging/VirtualPaging.cs b/HRM.BO/VirtualPaging/VirtualPaging.cs index 4aef283..3d673b1 100644 --- a/HRM.BO/VirtualPaging/VirtualPaging.cs +++ b/HRM.BO/VirtualPaging/VirtualPaging.cs @@ -58,6 +58,27 @@ namespace HRM.BO return value; } + /// + /// A comma-separated list of whole numbers (e.g. selected employee IDs "12,15,40"), without duplicates. + /// Throws a message fit for the user when it is missing, empty or holds anything but numbers. + /// + public List GetIntList(string name, string label = null) + { + var values = new List(); + var seen = new HashSet(); + foreach (string part in (GetString(name) ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) + { + int value; + if (!int.TryParse(part.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) + throw new ArgumentException("Please select a valid " + (label ?? name) + "."); + if (seen.Add(value)) + values.Add(value); + } + if (values.Count == 0) + throw new ArgumentException("Please select " + (label ?? name) + "."); + return values; + } + /// A whole-number filter. Throws a message fit for the user when it is missing or invalid. public int GetInt(string name, string label = null) { diff --git a/HRM.DA/DA/Employee/EmployeeDA.cs b/HRM.DA/DA/Employee/EmployeeDA.cs index 5efc8aa..dab0422 100644 --- a/HRM.DA/DA/Employee/EmployeeDA.cs +++ b/HRM.DA/DA/Employee/EmployeeDA.cs @@ -1597,26 +1597,22 @@ namespace HRM.DA return oEmpDetails; } - /// - /// Discontinue Employee Details (Manage Employee › Report): one row per employee discontinued - /// (STATUS = Discontinued) between fromDate and toDate in one payroll type, with the same columns as - /// the Employee Details report. Column aliases are the grid's field names; VirtualPagingDA adds the - /// search, sort and paging, so there is no ORDER BY here. - /// - /// Department tiers: the employee's department sits at its own tier (TIRE) and its parents fill the - /// tiers above it, as EmployeeController.GetEmpDetailInfo works them out (no tiers for a department - /// without a parent). The end date is exclusive (toDate + 1 day) so a time part never drops the last day. - /// - internal static string GetDiscontinuedEmployeeQuery(DateTime fromDate, DateTime toDate, int payrollTypeID) - { - return SQLParser.MakeSQL(@" -SELECT + #region Employee Details grids (virtual paging) + + // The columns of the Employee Details report, for the virtual-paging grids (Employee Detail + // Information, Discontinue Employee Details). Each alias is a grid field name. Values the old + // reports worked out in C# are worked out here, so the grid can search and sort by them: + // - department tiers: the employee's department sits at its own tier (TIRE) and its parents fill + // the tiers above it (no tiers for a department without a parent), as GetEmpDetailInfo did; + // - blood group name; bank / branch / account no falling back to the out-pay values. + // Used with EmployeeDetailFromSql, which defines the aliases v, e, d0..d4 and t. + private const string EmployeeDetailColumnsSql = @" + e.EMPLOYEEID AS employeeId, v.EMPLOYEENO AS employeeNo, v.NAME AS name, v.Gender AS gender, v.BIRTHDATE AS birthDate, v.JOININGDATE AS joiningDate, - e.ENDOFCONTRACTDATE AS discontinueDate, v.MOBILENO AS mobileNo, v.EMAILADDRESS AS emailAddress, v.DESIGNATION AS designation, @@ -1647,7 +1643,9 @@ SELECT ISNULL(NULLIF(v.Branch, ''), v.OUTPayBranch) AS branch, ISNULL(NULLIF(v.ACCOUNTNO, ''), v.OutPayAccountNo) AS accountNo, v.NATIONALID AS nationalId, - v.TINNO AS tinNo + v.TINNO AS tinNo"; + + private const string EmployeeDetailFromSql = @" FROM VW_EMPLOYEEBASIC v INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = v.EmployeeId LEFT JOIN DEPARTMENT d0 ON d0.DEPARTMENTID = e.DEPARTMENTID @@ -1655,12 +1653,39 @@ LEFT JOIN DEPARTMENT d1 ON d1.DEPARTMENTID = d0.PARENTID LEFT JOIN DEPARTMENT d2 ON d2.DEPARTMENTID = d1.PARENTID LEFT JOIN DEPARTMENT d3 ON d3.DEPARTMENTID = d2.PARENTID LEFT JOIN DEPARTMENT d4 ON d4.DEPARTMENTID = d3.PARENTID -CROSS APPLY (SELECT CASE WHEN d0.PARENTID IS NULL THEN 0 WHEN d0.TIRE > 5 THEN 5 ELSE d0.TIRE END AS Tier) t +CROSS APPLY (SELECT CASE WHEN d0.PARENTID IS NULL THEN 0 WHEN d0.TIRE > 5 THEN 5 ELSE d0.TIRE END AS Tier) t"; + + /// + /// Employee Detail Information (/reports/report-viewer/empDetailInfo): the employees selected on the + /// screen, in one payroll type. The selected IDs are in the temp table #empIds, filled by + /// VirtualPagingDA.IdList, so any number of employees is one short SQL text and a good plan. + /// VirtualPagingDA adds the search, sort and paging, so there is no ORDER BY here. + /// + internal static string GetEmpDetailInfoQuery(int payrollTypeID) + { + return SQLParser.MakeSQL("SELECT" + EmployeeDetailColumnsSql + EmployeeDetailFromSql + @" +WHERE e.PAYROLLTYPEID = %n + AND e.EMPLOYEEID IN (SELECT ID FROM #empIds)", + payrollTypeID); + } + + /// + /// Discontinue Employee Details (Manage Employee › Report): employees discontinued (STATUS = + /// Discontinued) between fromDate and toDate in one payroll type: the Employee Details columns plus + /// the discontinue date. The end date is exclusive (toDate + 1 day) so a time part never drops the + /// last day. VirtualPagingDA adds the search, sort and paging, so there is no ORDER BY here. + /// + internal static string GetDiscontinuedEmployeeQuery(DateTime fromDate, DateTime toDate, int payrollTypeID) + { + return SQLParser.MakeSQL("SELECT" + EmployeeDetailColumnsSql + @", + e.ENDOFCONTRACTDATE AS discontinueDate" + EmployeeDetailFromSql + @" WHERE e.STATUS = %n AND e.PAYROLLTYPEID = %n AND e.ENDOFCONTRACTDATE >= %d AND e.ENDOFCONTRACTDATE < %d", (int)EnumEmployeeStatus.Discontinued, payrollTypeID, fromDate.Date, toDate.Date.AddDays(1)); } + #endregion + internal static DataSet GetEmpDetailsView(TransactionContext tc) { DataSet oEmpDetails = new DataSet(); diff --git a/HRM.DA/DA/VirtualPaging/README.md b/HRM.DA/DA/VirtualPaging/README.md index bb54fd8..be6cfe6 100644 --- a/HRM.DA/DA/VirtualPaging/README.md +++ b/HRM.DA/DA/VirtualPaging/README.md @@ -43,7 +43,7 @@ public VirtualPageResult GetDiscontinuedEmployeePage(VirtualPageRequest request, { tc = TransactionContext.Begin(); string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); - result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); // "employeeNo" = a unique field + result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo,employeeId"); // fields that together are unique tc.End(); } catch (Exception e) @@ -120,4 +120,21 @@ preview() { skip and take are SQL parameters; field names must be plain identifiers and are bracketed. - **Access**: authorization in this app is menu-driven, so every page endpoint checks who may call it (self-service logins are refused above). -- The key field passed to `GetPage` must be unique, so pages never overlap or skip rows. +- The key fields passed to `GetPage` must together be unique (`"employeeNo,employeeId"`: two employees can + share an Employee No), so pages never overlap or skip rows. The first one is the default sort. + +## Selected IDs (e.g. "employees picked on the screen") + +Send them as one filter (`employeeIds: "12,15,40"`), read them with `request.GetIntList("employeeIds")`, and +pass them as an ID list. `GetPage` loads them into a temp table with a primary key (one parameter, any +number of IDs, and a good plan); the query reads the temp table: + +```csharp +// DA +WHERE e.PAYROLLTYPEID = %n AND e.EMPLOYEEID IN (SELECT ID FROM #empIds) +// Service +result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo,employeeId", VirtualPagingDA.IdList("#empIds", employeeIds)); +``` + +Worked example: Employee Detail Information (`EmployeeController.GetEmpDetailInfoPage`, +`EmployeeDA.GetEmpDetailInfoQuery`). Needs SQL Server 2016+ (`STRING_SPLIT`). diff --git a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs index decd485..636c0ab 100644 --- a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs +++ b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Globalization; using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -17,28 +18,36 @@ namespace HRM.DA /// they can change. A page's service calls it with its own query: /// /// string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); - /// return VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); + /// return VirtualPagingDA.GetPage(tc, query, request, "employeeNo,employeeId"); /// /// The query is an ordinary SELECT whose column aliases are the grid's field names, with no ORDER BY /// and no WITH (it is used as a derived table). See README.md next to this file. /// - /// Safety: the page query is built by code with MakeSQL; user input (search text, skip, take) reaches - /// SQL only as parameters; field names are checked to be plain identifiers and bracketed. + /// Safety: the page query is built by code with MakeSQL; user input (search text, skip, take, ID lists) + /// reaches SQL only as parameters; field names are checked to be plain identifiers and bracketed. /// internal static class VirtualPagingDA { private static readonly Regex Identifier = new Regex("^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); + private static readonly Regex TempTableName = new Regex("^#[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.Compiled); private const int MaxRowsPerPage = 1000; private const int MaxSearchLength = 200; /// The page's SELECT (column aliases = grid fields, no ORDER BY). - /// A unique field (e.g. employeeNo): the default sort and the tie-breaker that keeps pages from overlapping. - internal static VirtualPageResult GetPage(TransactionContext tc, string query, VirtualPageRequest request, string keyField) + /// + /// Comma-separated fields that together are unique, e.g. "employeeNo,employeeId". The first is the + /// default sort; all of them end every ORDER BY, so pages never overlap or skip a row. + /// + /// ID lists the query reads as temp tables, e.g. IdList("#empIds", ids). Optional. + internal static VirtualPageResult GetPage(TransactionContext tc, string query, VirtualPageRequest request, string keyFields, + params VirtualPagingIdList[] idLists) { if (request == null) throw new ArgumentException("Invalid request."); - if (!IsIdentifier(keyField)) - throw new ArgumentException("Invalid key field."); + List keys = (keyFields ?? "").Split(',').Select(k => k.Trim()).ToList(); + if (keys.Count == 0 || !keys.All(IsIdentifier)) + throw new ArgumentException("Invalid key fields."); + idLists = idLists ?? new VirtualPagingIdList[0]; List columns = GetColumns(request.Columns); string search = (request.Search ?? "").Trim(); @@ -51,7 +60,20 @@ namespace HRM.DA bool paged = !export && request.Take > 0; bool countTotal = !export && request.CountTotal; - var sql = new StringBuilder(); + var sql = new StringBuilder("SET NOCOUNT ON;\n"); + var parameters = new List(); // SqlHelper expects a SqlParameter[] + + // 0. ID lists → temp tables with a primary key: SQL Server then knows how many rows there are and + // picks a good plan (a STRING_SPLIT inside the query is guessed at 50 rows and can be 10x slower). + for (int i = 0; i < idLists.Length; i++) + { + string table = idLists[i].TableName, name = "@vpIds" + i; + sql.Append("IF OBJECT_ID('tempdb..").Append(table).Append("') IS NOT NULL DROP TABLE ").Append(table).Append(";\n") + .Append("CREATE TABLE ").Append(table).Append(" (ID int NOT NULL PRIMARY KEY);\n") + .Append("INSERT INTO ").Append(table).Append(" (ID) SELECT DISTINCT CONVERT(int, value) FROM STRING_SPLIT(") + .Append(name).Append(", ',');\n"); + parameters.Add(SqlHelperExtension.CreateInParam(name, SqlDbType.VarChar, idLists[i].ToText(), -1)); // varchar(max) + } // 1. Totals, only on Preview / new search (scroll and sort do not change them). One scan for both. if (countTotal) @@ -66,11 +88,14 @@ namespace HRM.DA .Append(columns.Count == 0 ? "*" : string.Join(", ", columns.Select(c => "[" + c.Field + "]"))) .Append(source) .Append(searchCondition == null ? "" : " WHERE " + searchCondition) - .Append(" ORDER BY ").Append(OrderBy(columns, request, keyField)); + .Append(" ORDER BY ").Append(OrderBy(columns, request, keys)); if (paged) sql.Append(" OFFSET @vpSkip ROWS FETCH NEXT @vpTake ROWS ONLY"); + sql.Append(";\n"); + + foreach (VirtualPagingIdList list in idLists) + sql.Append("DROP TABLE ").Append(list.TableName).Append(";\n"); - var parameters = new List(); // SqlHelper expects a SqlParameter[] if (searchCondition != null) parameters.Add(SqlHelperExtension.CreateInParam("@vpSearch", SqlDbType.NVarChar, "%" + EscapeLike(search) + "%", 400)); if (paged) @@ -110,6 +135,18 @@ namespace HRM.DA return result; } + /// + /// A list of IDs (e.g. the employees selected on the screen), however many there are. GetPage loads it + /// into the temp table (one parameter, then a primary key) and the query + /// reads it with: e.EMPLOYEEID IN (SELECT ID FROM #empIds). Needs SQL Server 2016+ (STRING_SPLIT). + /// + internal static VirtualPagingIdList IdList(string tableName, IEnumerable ids) + { + if (tableName == null || !TempTableName.IsMatch(tableName)) + throw new ArgumentException("Invalid temp table name."); + return new VirtualPagingIdList(tableName, (ids ?? Enumerable.Empty()).ToList()); + } + /// The requested columns; every field must be a plain identifier (it is put in the SQL in brackets). private static List GetColumns(List requested) { @@ -156,17 +193,24 @@ namespace HRM.DA return parts.Count == 0 ? "1 = 0" : "(" + string.Join(" OR ", parts) + ")"; } - /// The grid's sort (only by one of its columns), then the key field so the order is always the same. - private static string OrderBy(List columns, VirtualPageRequest request, string keyField) + /// + /// The grid's sort (only by one of its columns), then the key fields so the order is always the same. + /// No sort = the key fields (the first one is the default sort). + /// + private static string OrderBy(List columns, VirtualPageRequest request, List keys) { VirtualPageColumn sort = columns.FirstOrDefault(c => string.Equals(c.Field, request.SortField, StringComparison.OrdinalIgnoreCase)); - if (sort == null || string.Equals(sort.Field, keyField, StringComparison.OrdinalIgnoreCase)) + string dir = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase) ? " DESC" : " ASC"; + + var parts = new List(); + if (sort != null) + parts.Add("[" + sort.Field + "]" + dir); + foreach (string key in keys) { - bool keyDesc = sort != null && string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); - return "[" + keyField + "]" + (keyDesc ? " DESC" : " ASC"); + if (sort == null || !string.Equals(key, sort.Field, StringComparison.OrdinalIgnoreCase)) + parts.Add("[" + key + "] ASC"); } - bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); - return "[" + sort.Field + "]" + (desc ? " DESC" : " ASC") + ", [" + keyField + "] ASC"; + return string.Join(", ", parts); } /// Makes %, _, [ and \ in the search text match themselves. @@ -175,4 +219,19 @@ namespace HRM.DA private static bool IsIdentifier(string name) => name != null && Identifier.IsMatch(name); } + + /// An ID list for a virtual-paging query (made by VirtualPagingDA.IdList). + internal sealed class VirtualPagingIdList + { + internal VirtualPagingIdList(string tableName, List ids) + { + TableName = tableName; + Ids = ids; + } + + internal string TableName { get; } + internal List Ids { get; } + + internal string ToText() => string.Join(",", Ids.Select(id => id.ToString(CultureInfo.InvariantCulture))); + } } diff --git a/HRM.DA/Service/Employee/EmployeeService.cs b/HRM.DA/Service/Employee/EmployeeService.cs index a05ad4a..295b305 100644 --- a/HRM.DA/Service/Employee/EmployeeService.cs +++ b/HRM.DA/Service/Employee/EmployeeService.cs @@ -2730,6 +2730,40 @@ namespace HRM.DA return oEmpDetails; } + #region Employee Detail Information + + /// + /// Employee Detail Information (/reports/report-viewer/empDetailInfo): one window of rows for the grid, + /// for the employees selected on the screen. SQL Server pages, searches and sorts (VirtualPagingDA); + /// the query is EmployeeDA.GetEmpDetailInfoQuery and the selected IDs go in as one ID list (#empIds). + /// + public VirtualPageResult GetEmpDetailInfoPage(VirtualPageRequest request, List employeeIds, int payrollTypeID) + { + VirtualPageResult result = null; + TransactionContext tc = null; + try + { + tc = TransactionContext.Begin(); + string query = EmployeeDA.GetEmpDetailInfoQuery(payrollTypeID); + result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo,employeeId", + VirtualPagingDA.IdList("#empIds", employeeIds)); + tc.End(); + } + catch (Exception e) + { + #region Handle Exception + if (tc != null) + tc.HandleError(); + ExceptionLog.Write(e); + throw new ServiceException(e.Message, e); + #endregion + } + + return result; + } + + #endregion + #region Discontinue Employee Details /// @@ -2745,7 +2779,7 @@ namespace HRM.DA { tc = TransactionContext.Begin(); string query = EmployeeDA.GetDiscontinuedEmployeeQuery(fromDate, toDate, payrollTypeID); - result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo"); + result = VirtualPagingDA.GetPage(tc, query, request, "employeeNo,employeeId"); tc.End(); } catch (Exception e) diff --git a/HRM.UI/ClientApp/src/app/reports/report-viewer/report-viewer.component.html b/HRM.UI/ClientApp/src/app/reports/report-viewer/report-viewer.component.html index be01999..2c54ef9 100644 --- a/HRM.UI/ClientApp/src/app/reports/report-viewer/report-viewer.component.html +++ b/HRM.UI/ClientApp/src/app/reports/report-viewer/report-viewer.component.html @@ -357,6 +357,16 @@ --> + @if (reportType === 'empDetailInfo') { + +
    + + +
    + } @if (showAny) {