@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Payroll.Deduction @using Indotalent.Features.Payroll.Deduction.Cqrs @using Microsoft.AspNetCore.Components.Web @using Microsoft.JSInterop @using MudBlazor @using Features.Root.Shared @using ClosedXML.Excel @using System.IO @inject DeductionService DeductionService @inject IDialogService DialogService @inject ISnackbar Snackbar @inject IJSRuntime JSRuntime
Deduction Components Manage statutory contributions, insurance, and internal company deductions.
/ Payroll / Deduction
Search
@if (_isExporting) { Processing... } else { Excel } @if (_isRefreshing) { Refreshing... } else { Refresh } @if (_selectedDeduction != null) { View Edit Remove } else { Add New Deduction }
Code Component Name Category Pre-Tax Calculation Method Status @context.Code @context.Name @context.Category @context.CalculationMethod @context.Status
Rows per page: Showing @(GetFilteredData().Count() == 0 ? 0 : _skip + 1)-@Math.Min(_skip + _top, GetFilteredData().Count()) of @GetFilteredData().Count()
Prev @{ var totalPages = _totalPage == 0 ? 1 : _totalPage; var maxVisible = 5; var startPage = Math.Max(1, _currentPage - maxVisible / 2); var endPage = Math.Min(totalPages, startPage + maxVisible - 1); if (endPage - startPage < maxVisible - 1) { startPage = Math.Max(1, endPage - maxVisible + 1); } } @for (int i = startPage; i <= endPage; i++) { var pageNum = i; var isActive = pageNum == _currentPage; @pageNum } Next
@code { [Parameter] public EventCallback OnAdd { get; set; } [Parameter] public EventCallback OnEdit { get; set; } [Parameter] public EventCallback OnView { get; set; } private List _deductions = new(); private GetDeductionListResponse? _selectedDeduction; private string _searchString = ""; private bool _isRefreshing = false; private bool _isExporting = false; private int _skip = 0; private int _top = 5; private int _totalPage => GetFilteredData().Count() == 0 ? 0 : (int)Math.Ceiling((double)GetFilteredData().Count() / (_top == 0 ? 1 : _top)); private int _currentPage => (_top >= GetFilteredData().Count() || _top == 0) ? 1 : (_skip / _top) + 1; protected override async Task OnInitializedAsync() => await LoadData(); private async Task LoadData() { _isRefreshing = true; _selectedDeduction = null; StateHasChanged(); try { var response = await DeductionService.GetDeductionListAsync(); await Task.Delay(500); if (response != null && response.IsSuccess) { _deductions = response.Value ?? new List(); } } finally { _isRefreshing = false; StateHasChanged(); } } private IEnumerable GetFilteredData() { if (string.IsNullOrWhiteSpace(_searchString)) return _deductions; return _deductions.Where(x => (x.Name?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) || (x.Code?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) || (x.Category?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) ); } private IEnumerable GetPagedData() => GetFilteredData().Skip(_skip).Take(_top); private void OnSearchClick() { _skip = 0; _selectedDeduction = null; StateHasChanged(); } private void HandleSearchKeyDown(KeyboardEventArgs e) { if (e.Key == "Enter") OnSearchClick(); } private async Task ExportToExcel() { _isExporting = true; StateHasChanged(); try { await Task.Delay(1000); using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Deductions"); var currentRow = 1; worksheet.Cell(currentRow, 1).Value = "Code"; worksheet.Cell(currentRow, 2).Value = "Component Name"; worksheet.Cell(currentRow, 3).Value = "Category"; worksheet.Cell(currentRow, 4).Value = "Pre-Tax"; worksheet.Cell(currentRow, 5).Value = "Calculation Method"; worksheet.Cell(currentRow, 6).Value = "Status"; var headerRange = worksheet.Range(1, 1, 1, 6); headerRange.Style.Font.Bold = true; headerRange.Style.Fill.BackgroundColor = XLColor.FromHtml("#C62828"); headerRange.Style.Font.FontColor = XLColor.White; foreach (var item in GetFilteredData()) { currentRow++; worksheet.Cell(currentRow, 1).Value = item.Code; worksheet.Cell(currentRow, 2).Value = item.Name; worksheet.Cell(currentRow, 3).Value = item.Category; worksheet.Cell(currentRow, 4).Value = item.PreTax ? "Yes" : "No"; worksheet.Cell(currentRow, 5).Value = item.CalculationMethod; worksheet.Cell(currentRow, 6).Value = item.Status; } worksheet.Columns().AdjustToContents(); using (var stream = new MemoryStream()) { workbook.SaveAs(stream); var content = Convert.ToBase64String(stream.ToArray()); await JSRuntime.InvokeVoidAsync("downloadFile", "Deductions_List.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", content); Snackbar.Add("Excel exported successfully", Severity.Success); } } } catch (Exception ex) { Snackbar.Add($"Export failed: {ex.Message}", Severity.Error); } finally { _isExporting = false; StateHasChanged(); } } private void OnPageChanged(int page) { if (page >= 1 && page <= _totalPage) { _skip = (page - 1) * _top; _selectedDeduction = null; StateHasChanged(); } } private void OnPageSizeChanged(int size) { _top = size; _skip = 0; _selectedDeduction = null; StateHasChanged(); } private async Task InvokeEdit() { if (_selectedDeduction == null) return; var res = await DeductionService.GetDeductionByIdAsync(_selectedDeduction.Id!); if (res != null && res.IsSuccess) await OnEdit.InvokeAsync(MapToRequest(res.Value!)); } private async Task InvokeView() { if (_selectedDeduction == null) return; var res = await DeductionService.GetDeductionByIdAsync(_selectedDeduction.Id!); if (res != null && res.IsSuccess) await OnView.InvokeAsync(MapToRequest(res.Value!)); } private UpdateDeductionRequest MapToRequest(GetDeductionByIdResponse data) => new() { Id = data.Id, Code = data.Code, Name = data.Name, Category = data.Category, PreTax = data.PreTax, CalculationMethod = data.CalculationMethod, Status = data.Status, Description = data.Description, CreatedAt = data.CreatedAt, CreatedBy = data.CreatedBy, UpdatedAt = data.UpdatedAt, UpdatedBy = data.UpdatedBy }; private async Task OnDelete() { if (_selectedDeduction == null) return; var parameters = new DialogParameters<_DeleteConfirmation> { { x => x.ContentText, _selectedDeduction.Name } }; var options = new DialogOptions { CloseButton = false, MaxWidth = MaxWidth.ExtraSmall, FullWidth = true }; var dialog = await DialogService.ShowAsync<_DeleteConfirmation>("", parameters, options); var result = await dialog.Result; if (result != null && !result.Canceled) { var isSuccess = await DeductionService.DeleteDeductionByIdAsync(_selectedDeduction.Id!); if (isSuccess) { _selectedDeduction = null; await LoadData(); Snackbar.Add("Deduction deleted successfully", Severity.Success); } } } }