From a9008beb2438d2a3a9347e680e77b88586ffc45d Mon Sep 17 00:00:00 2001 From: MahanCh Date: Sat, 9 Aug 2025 14:45:04 +0330 Subject: [PATCH] Change CreateAsync For SalaryAid --- 0_Framework/Domain/IRepository.cs | 3 + 0_Framework/InfraStructure/RepositoryBase.cs | 6 ++ .../SalaryAidAgg/ISalaryAidRepository.cs | 4 +- .../SalaryAid/ISalaryAidApplication.cs | 2 +- .../SalaryAidApplication.cs | 94 +++++++++++-------- .../Pages/Company/SalaryAid/Index.cshtml.cs | 4 +- 6 files changed, 71 insertions(+), 42 deletions(-) diff --git a/0_Framework/Domain/IRepository.cs b/0_Framework/Domain/IRepository.cs index 8c4b008a..209a5f29 100644 --- a/0_Framework/Domain/IRepository.cs +++ b/0_Framework/Domain/IRepository.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Storage; namespace _0_Framework.Domain; @@ -17,4 +18,6 @@ public interface IRepository where T:class bool Exists(Expression> expression); void SaveChanges(); Task SaveChangesAsync(); + Task BeginTransactionAsync(); + } \ No newline at end of file diff --git a/0_Framework/InfraStructure/RepositoryBase.cs b/0_Framework/InfraStructure/RepositoryBase.cs index 3c573b18..8f8a9fac 100644 --- a/0_Framework/InfraStructure/RepositoryBase.cs +++ b/0_Framework/InfraStructure/RepositoryBase.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using _0_Framework.Domain; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage; namespace _0_Framework.InfraStructure { @@ -70,5 +71,10 @@ namespace _0_Framework.InfraStructure { await _context.SaveChangesAsync(); } + + public async Task BeginTransactionAsync() + { + return await _context.Database.BeginTransactionAsync(); + } } } diff --git a/Company.Domain/SalaryAidAgg/ISalaryAidRepository.cs b/Company.Domain/SalaryAidAgg/ISalaryAidRepository.cs index edc2d7c5..4acc1cec 100644 --- a/Company.Domain/SalaryAidAgg/ISalaryAidRepository.cs +++ b/Company.Domain/SalaryAidAgg/ISalaryAidRepository.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using _0_Framework.Domain; using CompanyManagment.App.Contracts.Reward; using CompanyManagment.App.Contracts.SalaryAid; @@ -21,4 +22,5 @@ public interface ISalaryAidRepository:IRepository SalaryAidsGroupedViewModel GetSearchListAsGrouped(SalaryAidSearchViewModel searchModel); #endregion + } \ No newline at end of file diff --git a/CompanyManagment.App.Contracts/SalaryAid/ISalaryAidApplication.cs b/CompanyManagment.App.Contracts/SalaryAid/ISalaryAidApplication.cs index ebaccead..ff068485 100644 --- a/CompanyManagment.App.Contracts/SalaryAid/ISalaryAidApplication.cs +++ b/CompanyManagment.App.Contracts/SalaryAid/ISalaryAidApplication.cs @@ -22,5 +22,5 @@ public interface ISalaryAidApplication SalaryAidsGroupedViewModel GetSearchListAsGrouped(SalaryAidSearchViewModel searchModel); #endregion - OperationResult CreateRange(List commands); + Task CreateRangeAsync(List commands); } \ No newline at end of file diff --git a/CompanyManagment.Application/SalaryAidApplication.cs b/CompanyManagment.Application/SalaryAidApplication.cs index 5746a240..958de53c 100644 --- a/CompanyManagment.Application/SalaryAidApplication.cs +++ b/CompanyManagment.Application/SalaryAidApplication.cs @@ -30,8 +30,11 @@ public class SalaryAidApplication : ISalaryAidApplication private readonly ICheckoutRepository _checkoutRepository; - public SalaryAidApplication(ISalaryAidRepository salaryAidRepository, ICustomizeCheckoutRepository customizeCheckoutRepository, - IEmployeeRepository employeeRepository, ICustomizeCheckoutTempRepository customizeCheckoutTempRepository, IAuthHelper authHelper, ICustomizeCheckoutApplication customizeCheckoutApplication, ICustomizeCheckoutTempApplication customizeCheckoutTempApplication, ICheckoutRepository checkoutRepository) + public SalaryAidApplication(ISalaryAidRepository salaryAidRepository, + ICustomizeCheckoutRepository customizeCheckoutRepository, + IEmployeeRepository employeeRepository, ICustomizeCheckoutTempRepository customizeCheckoutTempRepository, + IAuthHelper authHelper, ICustomizeCheckoutApplication customizeCheckoutApplication, + ICustomizeCheckoutTempApplication customizeCheckoutTempApplication, ICheckoutRepository checkoutRepository) { _salaryAidRepository = salaryAidRepository; _customizeCheckoutRepository = customizeCheckoutRepository; @@ -62,6 +65,7 @@ public class SalaryAidApplication : ISalaryAidApplication { return op.Failed("تاریخ وارد شده نامعتبر است"); } + var calculationDate = $"{command.CalculationYear:0000}/{command.CalculationMonth:00}/01"; if (!calculationDate.TryToGeorgianDateTime(out var calculationDateGr)) { @@ -71,7 +75,6 @@ public class SalaryAidApplication : ISalaryAidApplication if (startDate > DateTime.Now) { return op.Failed("تاریخ پرداخت مساعده می بایست تاریخ امروز یا قبل تر باشد"); - } if (command.Amount.Length > 15) @@ -100,15 +103,15 @@ public class SalaryAidApplication : ISalaryAidApplication calculationDateGr, command.WorkshopId, command.EmployeeIds); //if (existsCheckouts.Checkout) - // return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است مساعده ای دهید"); + // return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است مساعده ای دهید"); var (userId, userType, _) = _authHelper.GetUserTypeWithId(); using var transaction = new TransactionScope(); foreach (var employeeId in command.EmployeeIds) { - - var entity = new SalaryAid(employeeId, command.WorkshopId, command.Amount.MoneyToDouble(), startDate, userId, userType, command.CalculationMonth, command.CalculationYear); + var entity = new SalaryAid(employeeId, command.WorkshopId, command.Amount.MoneyToDouble(), startDate, + userId, userType, command.CalculationMonth, command.CalculationYear); _salaryAidRepository.Create(entity); _salaryAidRepository.SaveChanges(); if (existsCheckouts.CustomizeCheckout) @@ -119,13 +122,13 @@ public class SalaryAidApplication : ISalaryAidApplication if (customizeCheckouts != null) { - var salaryAids = customizeCheckouts.CustomizeCheckoutSalaryAids.ToList(); - salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, entity.CalculationDate.ToFarsi(), entity.id)); + salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, + entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, entity.CalculationDate.ToFarsi(), + entity.id)); customizeCheckouts.SetSalaryAids(salaryAids); } - } if (existsCheckouts.CustomizeCheckoutTemp) @@ -137,11 +140,12 @@ public class SalaryAidApplication : ISalaryAidApplication { var salaryAids = customizeCheckoutTemp.CustomizeCheckoutSalaryAids.ToList(); - salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, entity.CalculationDate.ToFarsi(), entity.id)); + salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, + entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, entity.CalculationDate.ToFarsi(), + entity.id)); customizeCheckoutTemp.SetSalaryAids(salaryAids); } } - } _customizeCheckoutRepository.SaveChanges(); @@ -167,12 +171,13 @@ public class SalaryAidApplication : ISalaryAidApplication if (startDate > DateTime.Now) { return op.Failed("تاریخ پرداخت مساعده می بایست تاریخ امروز یا قبل تر باشد"); - } + if (command.Amount.Length > 15) { return op.Failed("مبلغ وارد شده معتبر نیست"); } + var entity = _salaryAidRepository.Get(command.Id); if (entity == null) return op.Failed("چنین مساعده ای وجود ندارد"); @@ -198,14 +203,15 @@ public class SalaryAidApplication : ISalaryAidApplication var existsCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(calculationDateGr, calculationDateGr, entity.WorkshopId, [entity.EmployeeId]); - // if (existsCheckouts.Checkout) - // return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است مساعده ای دهید"); + // if (existsCheckouts.Checkout) + // return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است مساعده ای دهید"); using var transaction = new TransactionScope(); var (userId, userType, _) = _authHelper.GetUserTypeWithId(); - entity.Edit(Tools.MoneyToDouble(command.Amount), startDate, userId, userType, command.CalculationMonth, command.CalculationYear); + entity.Edit(Tools.MoneyToDouble(command.Amount), startDate, userId, userType, command.CalculationMonth, + command.CalculationYear); _salaryAidRepository.SaveChanges(); if (existsCheckouts.CustomizeCheckout) @@ -226,8 +232,8 @@ public class SalaryAidApplication : ISalaryAidApplication entity.CalculationDate, entity.CalculationDate.ToFarsi(), entity.id)); customizeCheckouts.SetSalaryAids(salaryAids); - } + if (existsCheckouts.CustomizeCheckoutTemp) { var customizeCheckoutTemp = _customizeCheckoutTempRepository.GetByWorkshopIdEmployeeIdInDate( @@ -245,8 +251,8 @@ public class SalaryAidApplication : ISalaryAidApplication entity.CalculationDate, entity.CalculationDate.ToFarsi(), entity.id)); customizeCheckoutTemp.SetSalaryAids(salaryAids); - } + _customizeCheckoutRepository.SaveChanges(); transaction.Complete(); return op.Succcedded(); @@ -262,18 +268,23 @@ public class SalaryAidApplication : ISalaryAidApplication var month = Convert.ToInt32(entity.CalculationDate.ToFarsi().Substring(5, 2)); var year = Convert.ToInt32(entity.CalculationDate.ToFarsi().Substring(0, 4)); - if (_customizeCheckoutRepository.Exists(x => x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && x.YearInt == year && x.MonthInt == month)) + if (_customizeCheckoutRepository.Exists(x => + x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && x.YearInt == year && + x.MonthInt == month)) return op.Failed("این پرسنل در این تاریخ دارای فیش حقوقی است"); - if (_customizeCheckoutTempRepository.Exists(x => x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && - x.YearInt == year && x.MonthInt == month && x.ContractStart <= entity.CalculationDate && x.ContractEnd >= entity.CalculationDate)) + if (_customizeCheckoutTempRepository.Exists(x => + x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && + x.YearInt == year && x.MonthInt == month && x.ContractStart <= entity.CalculationDate && + x.ContractEnd >= entity.CalculationDate)) { return op.Failed("این پرسنل در این تاریخ دارای فیش حقوقی موقت است"); } if (_checkoutRepository.Exists(x => x.WorkshopId == entity.WorkshopId && entity.EmployeeId == x.EmployeeId && - x.ContractStart <= entity.CalculationDate && x.ContractEnd >= entity.CalculationDate)) + x.ContractStart <= entity.CalculationDate && + x.ContractEnd >= entity.CalculationDate)) { return op.Failed("این پرسنل در این تاریخ دارای فیش حقوقی رسمی است"); } @@ -293,17 +304,18 @@ public class SalaryAidApplication : ISalaryAidApplication } - public OperationResult CreateRange(List commands) + public async Task CreateRangeAsync(List commands) { var op = new OperationResult(); - using var transaction = new TransactionScope(); + await using var transaction = await _salaryAidRepository.BeginTransactionAsync(); + foreach (var command in commands) { - if (!command.SalaryDateTime.TryToGeorgianDateTime(out var startDate)) { return op.Failed("تاریخ وارد شده نامعتبر است"); } + var calculationDate = $"{command.CalculationYear:0000}/{command.CalculationMonth:00}/01"; if (!calculationDate.TryToGeorgianDateTime(out var calculationDateGr)) { @@ -313,12 +325,13 @@ public class SalaryAidApplication : ISalaryAidApplication if (startDate > DateTime.Now) { return op.Failed("تاریخ پرداخت مساعده می بایست تاریخ امروز یا قبل تر باشد"); - } + if (command.Amount.Length > 15) { return op.Failed("مبلغ وارد شده معتبر نیست"); } + _ = DateTime.Now.Date.AddMonthsFa(-1, out var oneMonthAgoGr); if (oneMonthAgoGr > calculationDateGr) @@ -336,14 +349,13 @@ public class SalaryAidApplication : ISalaryAidApplication calculationDateGr, command.WorkshopId, command.EmployeeIds); - // if (existsCheckouts.Checkout) - // return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است مساعده ای دهید"); + // if (existsCheckouts.Checkout) + // return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است مساعده ای دهید"); var month = Convert.ToInt32(command.CalculationMonth); var year = Convert.ToInt32(command.CalculationYear); - var (userId, userType, _) = _authHelper.GetUserTypeWithId(); foreach (var employeeId in command.EmployeeIds) @@ -355,9 +367,10 @@ public class SalaryAidApplication : ISalaryAidApplication id = employee.id; } - var entity = new SalaryAid(id, command.WorkshopId, command.Amount.MoneyToDouble(), startDate, userId, userType, command.CalculationMonth, command.CalculationYear); - _salaryAidRepository.Create(entity); - _salaryAidRepository.SaveChanges(); + var entity = new SalaryAid(id, command.WorkshopId, command.Amount.MoneyToDouble(), startDate, userId, + userType, command.CalculationMonth, command.CalculationYear); + await _salaryAidRepository.CreateAsync(entity); + await _salaryAidRepository.SaveChangesAsync(); if (existsCheckouts.CustomizeCheckout) { @@ -367,13 +380,13 @@ public class SalaryAidApplication : ISalaryAidApplication if (customizeCheckouts != null) { - var salaryAids = customizeCheckouts.CustomizeCheckoutSalaryAids.ToList(); - salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, entity.CalculationDate.ToFarsi(), entity.id)); + salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, + entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, + entity.CalculationDate.ToFarsi(), entity.id)); customizeCheckouts.SetSalaryAids(salaryAids); } - } if (existsCheckouts.CustomizeCheckoutTemp) @@ -385,17 +398,23 @@ public class SalaryAidApplication : ISalaryAidApplication { var salaryAids = customizeCheckoutTemp.CustomizeCheckoutSalaryAids.ToList(); - salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, entity.CalculationDate.ToFarsi(), entity.id)); + salaryAids.Add(new(entity.Amount.ToMoney(), entity.SalaryAidDateTime, + entity.SalaryAidDateTime.ToFarsi(), entity.CalculationDate, + entity.CalculationDate.ToFarsi(), entity.id)); customizeCheckoutTemp.SetSalaryAids(salaryAids); } } } - _customizeCheckoutRepository.SaveChanges(); + + await _customizeCheckoutRepository.SaveChangesAsync(); } - transaction.Complete(); + + await transaction.CommitAsync(); return op.Succcedded(); } + #region Pooya + /// /// گروهبندی بر اساس ماه هنگام جستجو با انتخاب کارمند /// @@ -404,6 +423,5 @@ public class SalaryAidApplication : ISalaryAidApplication return _salaryAidRepository.GetSearchListAsGrouped(searchModel); } - #endregion } \ No newline at end of file diff --git a/ServiceHost/Areas/Client/Pages/Company/SalaryAid/Index.cshtml.cs b/ServiceHost/Areas/Client/Pages/Company/SalaryAid/Index.cshtml.cs index 3f32b709..6c4ecf7c 100644 --- a/ServiceHost/Areas/Client/Pages/Company/SalaryAid/Index.cshtml.cs +++ b/ServiceHost/Areas/Client/Pages/Company/SalaryAid/Index.cshtml.cs @@ -175,7 +175,7 @@ namespace ServiceHost.Areas.Client.Pages.Company.SalaryAid }); } - public IActionResult OnPostCreateFromExcelData(List data) + public async Task OnPostCreateFromExcelData(List data) { var commands = data.Select(x => new CreateSalaryAidViewModel() { @@ -187,7 +187,7 @@ namespace ServiceHost.Areas.Client.Pages.Company.SalaryAid CalculationMonth = x.calculationMonth, CalculationYear = x.calculationYear }).ToList(); - OperationResult result = _salaryAidApplication.CreateRange(commands); + OperationResult result = await _salaryAidApplication.CreateRangeAsync(commands); return new JsonResult(new {