Change CreateAsync For SalaryAid

This commit is contained in:
MahanCh
2025-08-09 14:45:04 +03:30
parent 3650b46b8e
commit a9008beb24
6 changed files with 71 additions and 42 deletions

View File

@@ -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<TKey, T> where T:class
bool Exists(Expression<Func<T, bool>> expression);
void SaveChanges();
Task SaveChangesAsync();
Task<IDbContextTransaction> BeginTransactionAsync();
}

View File

@@ -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<IDbContextTransaction> BeginTransactionAsync()
{
return await _context.Database.BeginTransactionAsync();
}
}
}

View File

@@ -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<long,SalaryAid>
SalaryAidsGroupedViewModel GetSearchListAsGrouped(SalaryAidSearchViewModel searchModel);
#endregion
}

View File

@@ -22,5 +22,5 @@ public interface ISalaryAidApplication
SalaryAidsGroupedViewModel GetSearchListAsGrouped(SalaryAidSearchViewModel searchModel);
#endregion
OperationResult CreateRange(List<CreateSalaryAidViewModel> commands);
Task<OperationResult> CreateRangeAsync(List<CreateSalaryAidViewModel> commands);
}

View File

@@ -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<CreateSalaryAidViewModel> commands)
public async Task<OperationResult> CreateRangeAsync(List<CreateSalaryAidViewModel> 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
/// <summary>
/// گروهبندی بر اساس ماه هنگام جستجو با انتخاب کارمند
/// </summary>
@@ -404,6 +423,5 @@ public class SalaryAidApplication : ISalaryAidApplication
return _salaryAidRepository.GetSearchListAsGrouped(searchModel);
}
#endregion
}

View File

@@ -175,7 +175,7 @@ namespace ServiceHost.Areas.Client.Pages.Company.SalaryAid
});
}
public IActionResult OnPostCreateFromExcelData(List<SalaryAidImportData> data)
public async Task<IActionResult> OnPostCreateFromExcelData(List<SalaryAidImportData> 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
{