diff --git a/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/GetExpendituresRequestDto.cs b/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/GetExpendituresRequestDto.cs new file mode 100644 index 00000000..0085d15d --- /dev/null +++ b/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/GetExpendituresRequestDto.cs @@ -0,0 +1,11 @@ +using DFApp.CommonDtos; + +namespace DFApp.Bookkeeping.Expenditure +{ + public class GetExpendituresRequestDto : FilterAndPagedAndSortedResultRequestDto + { + public long? CategoryId { get; set; } + + public bool? IsBelongToSelf { get; set; } + } +} diff --git a/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/IBookkeepingExpenditureService.cs b/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/IBookkeepingExpenditureService.cs index 757d94b4..fec31d42 100644 --- a/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/IBookkeepingExpenditureService.cs +++ b/src/DFApp.Application.Contracts/Bookkeeping/Expenditure/IBookkeepingExpenditureService.cs @@ -11,11 +11,13 @@ namespace DFApp.Bookkeeping.Expenditure public interface IBookkeepingExpenditureService : ICrudAppService< BookkeepingExpenditureDto , long - , FilterAndPagedAndSortedResultRequestDto + , GetExpendituresRequestDto , CreateUpdateBookkeepingExpenditureDto> { Task> GetCategoryLookupDto(); + Task GetTotalExpenditureAsync(string? filter = null, long? categoryId = null, bool? isBelongToSelf = null); + Task GetChartJSDto(DateTime start , DateTime end , CompareType compareType diff --git a/src/DFApp.Application/Bookkeeping/Expenditure/BookkeepingExpenditureService.cs b/src/DFApp.Application/Bookkeeping/Expenditure/BookkeepingExpenditureService.cs index c5c25962..6da549a9 100644 --- a/src/DFApp.Application/Bookkeeping/Expenditure/BookkeepingExpenditureService.cs +++ b/src/DFApp.Application/Bookkeeping/Expenditure/BookkeepingExpenditureService.cs @@ -18,7 +18,7 @@ public class BookkeepingExpenditureService : CrudAppService< BookkeepingExpenditure , BookkeepingExpenditureDto , long - , FilterAndPagedAndSortedResultRequestDto + , GetExpendituresRequestDto , CreateUpdateBookkeepingExpenditureDto>, IBookkeepingExpenditureService { private readonly IRepository _categoryRepository; @@ -34,18 +34,33 @@ public BookkeepingExpenditureService(IRepository cate DeletePolicyName = DFAppPermissions.BookkeepingExpenditure.Delete; } - protected override async Task> CreateFilteredQueryAsync(FilterAndPagedAndSortedResultRequestDto input) + protected override async Task> CreateFilteredQueryAsync(GetExpendituresRequestDto input) { - if(!string.IsNullOrWhiteSpace(input.Filter)){ - var query = await Repository.WithDetailsAsync(); - return query.Where(x => x.Category!.Category.Contains(input.Filter) - || x.Remark!.Contains(input.Filter) - || x.Expenditure.ToString().Contains(input.Filter)); + var query = await Repository.WithDetailsAsync(); + + if (!string.IsNullOrWhiteSpace(input.Filter)) + { + query = query.Where(x => x.Category!.Category.Contains(input.Filter) + || x.Remark!.Contains(input.Filter) + || x.Expenditure.ToString().Contains(input.Filter)); } - else{ - return await Repository.WithDetailsAsync(); + + if (input.CategoryId.HasValue) + { + query = query.Where(x => x.CategoryId == input.CategoryId.Value); } - + + if (input.IsBelongToSelf.HasValue) + { + query = query.Where(x => x.IsBelongToSelf == input.IsBelongToSelf.Value); + } + + if (string.IsNullOrWhiteSpace(input.Sorting)) + { + query = query.OrderByDescending(x => x.ExpenditureDate); + } + + return query; } public async Task> GetCategoryLookupDto() @@ -57,6 +72,31 @@ public async Task> GetCategoryLookupDto() return result; } + public async Task GetTotalExpenditureAsync(string? filter = null, long? categoryId = null, bool? isBelongToSelf = null) + { + var query = await ReadOnlyRepository.GetQueryableAsync(); + + if (!string.IsNullOrWhiteSpace(filter)) + { + query = query.Where(x => x.Category!.Category.Contains(filter) + || x.Remark!.Contains(filter) + || x.Expenditure.ToString().Contains(filter)); + } + + if (categoryId.HasValue) + { + query = query.Where(x => x.CategoryId == categoryId.Value); + } + + if (isBelongToSelf.HasValue) + { + query = query.Where(x => x.IsBelongToSelf == isBelongToSelf.Value); + } + + var total = await AsyncExecuter.SumAsync(query, x => x.Expenditure); + return total; + } + [Authorize(DFAppPermissions.BookkeepingExpenditure.Analysis)] public async Task GetChartJSDto(DateTime start, DateTime end , CompareType compareType, NumberType numberType, bool? isBelongToSelf)