348 lines
13 KiB
C#
348 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Transactions;
|
|
using _0_Framework.Application;
|
|
using Company.Domain.CheckoutAgg;
|
|
using Company.Domain.CustomizeCheckoutAgg;
|
|
using Company.Domain.CustomizeCheckoutTempAgg;
|
|
using Company.Domain.EmployeeAgg;
|
|
using Company.Domain.File1;
|
|
using Company.Domain.FineAgg;
|
|
using Company.Domain.WorkshopAgg;
|
|
using CompanyManagment.App.Contracts.CustomizeCheckout;
|
|
using CompanyManagment.App.Contracts.Fine;
|
|
using CompanyManagment.EFCore.Repository;
|
|
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
namespace CompanyManagment.Application;
|
|
|
|
public class FineApplication : IFineApplication
|
|
{
|
|
private readonly IFineRepository _fineRepository;
|
|
private readonly IWorkshopRepository _workshopRepository;
|
|
private readonly IEmployeeRepository _employeeRepository;
|
|
private readonly ICustomizeCheckoutRepository _customizeCheckoutRepository;
|
|
public readonly ICustomizeCheckoutTempRepository CustomizeCheckoutTempRepository;
|
|
private readonly IAuthHelper _authHelper;
|
|
private readonly ICustomizeCheckoutApplication _customizeCheckoutApplication;
|
|
private readonly ICustomizeCheckoutTempRepository _customizeCheckoutTempRepository;
|
|
private readonly ICustomizeCheckoutTempApplication _customizeCheckoutTempApplication;
|
|
|
|
|
|
|
|
public FineApplication(IFineRepository fineRepository, IEmployeeRepository employeeRepository,
|
|
IWorkshopRepository workshopRepository, ICustomizeCheckoutRepository customizeCheckoutRepository,
|
|
IAuthHelper authHelper, ICustomizeCheckoutTempRepository customizeCheckoutTempRepository, ICustomizeCheckoutApplication customizeCheckoutApplication, ICustomizeCheckoutTempApplication customizeCheckoutTempApplication)
|
|
{
|
|
_fineRepository = fineRepository;
|
|
_employeeRepository = employeeRepository;
|
|
_workshopRepository = workshopRepository;
|
|
_customizeCheckoutRepository = customizeCheckoutRepository;
|
|
_authHelper = authHelper;
|
|
_customizeCheckoutTempRepository = customizeCheckoutTempRepository;
|
|
|
|
CustomizeCheckoutTempRepository = customizeCheckoutTempRepository;
|
|
_customizeCheckoutApplication = customizeCheckoutApplication;
|
|
_customizeCheckoutTempApplication = customizeCheckoutTempApplication;
|
|
}
|
|
|
|
public List<FineViewModel> GetSearchList(FineSearchViewModel searchModel)
|
|
{
|
|
return _fineRepository.GetSearchList(searchModel);
|
|
}
|
|
|
|
public EditFineViewModel GetDetails(long id)
|
|
{
|
|
return _fineRepository.GetDetails(id);
|
|
}
|
|
|
|
public OperationResult Remove(long id)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var entity = _fineRepository.Get(id);
|
|
if (entity == null)
|
|
return op.Failed("چنین آیتمی وجود ندارد");
|
|
|
|
var month = Convert.ToInt32(entity.FineDate.ToFarsi().Substring(5, 2));
|
|
var year = Convert.ToInt32(entity.FineDate.ToFarsi().Substring(0, 4));
|
|
|
|
|
|
|
|
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.FineDate && x.ContractEnd >= entity.FineDate))
|
|
{
|
|
return op.Failed("این پرسنل در این تاریخ دارای فیش حقوقی است");
|
|
}
|
|
|
|
_fineRepository.Remove(entity);
|
|
_fineRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult RemoveRange(List<long> ids)
|
|
{
|
|
OperationResult op = new OperationResult();
|
|
var fines = _fineRepository.GetBy(ids);
|
|
_fineRepository.RemoveRange(fines);
|
|
_fineRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
/// <summary>
|
|
/// گروهبندی بر اساس ماه هنگام جستجو با انتخاب کارمند
|
|
/// </summary>
|
|
public FinesGroupedViewModel GetSearchListAsGrouped(FineSearchViewModel searchModel)
|
|
{
|
|
return _fineRepository.GetSearchListAsGrouped(searchModel);
|
|
}
|
|
|
|
public OperationResult Create(CreateFineViewModel command)
|
|
{
|
|
OperationResult op = new();
|
|
|
|
#region Validation
|
|
|
|
if (!_workshopRepository.Exists(x => x.id == command.WorkshopId))
|
|
return op.Failed("خطای سیستمی");
|
|
if (!_employeeRepository.Exists(x => command.EmployeeIds.Any(a => a == x.id)))
|
|
return op.Failed("خطای سیستمی");
|
|
|
|
if (!command.FineDate.TryToGeorgianDateTime(out var fineDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (fineDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت جریمه می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
|
|
var month = Convert.ToInt32(command.FineDate.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.FineDate.Substring(0, 4));
|
|
|
|
|
|
|
|
//if (_customizeCheckoutRepository.Exists(x =>
|
|
// x.WorkshopId == command.WorkshopId && command.EmployeeIds.Contains(x.EmployeeId) && x.YearInt == year &&
|
|
// x.MonthInt == month))
|
|
//{
|
|
// return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی صادر شده است جریمه ای دهید");
|
|
|
|
//}
|
|
|
|
//if (CustomizeCheckoutTempRepository.Exists(x =>
|
|
// x.WorkshopId == command.WorkshopId && command.EmployeeIds.Contains(x.EmployeeId) && x.YearInt == year &&
|
|
// x.MonthInt == month &&
|
|
// x.ContractStart <= fineDate && x.ContractEnd >= fineDate))
|
|
//{
|
|
// return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی موقت صادر شده است جریمه ای دهید");
|
|
|
|
//}
|
|
|
|
_ = DateTime.Now.Date.AddMonthsFa(-1, out var oneMonthAgoGr);
|
|
|
|
if (oneMonthAgoGr > fineDate)
|
|
{
|
|
var prevCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(fineDate,
|
|
oneMonthAgoGr, command.WorkshopId, command.EmployeeIds);
|
|
|
|
if (prevCheckouts.CustomizeCheckout || prevCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
return op.Failed("شما نمیتوانید در تاریخ قبل از یک ماه گذشته که فیش صادر شده باشد جریمه ای دهید");
|
|
}
|
|
}
|
|
|
|
|
|
var existsCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(fineDate,
|
|
fineDate, command.WorkshopId, command.EmployeeIds);
|
|
|
|
//if (existsCheckouts.Checkout)
|
|
// return op.Failed("شما نمیتوانید برای پرسنلی در تاریخی که برای فیش حقوقی رسمی صادر شده است جریمه ای دهید");
|
|
|
|
#endregion
|
|
|
|
DateTime date = command.FineDate.ToGeorgianDateTime();
|
|
var (userId, userType,_) = _authHelper.GetUserTypeWithId();
|
|
|
|
using var transaction = new TransactionScope();
|
|
|
|
foreach (var employeeId in command.EmployeeIds)
|
|
{
|
|
Fine entity = new Fine(employeeId, command.WorkshopId, command.Title, command.Amount.MoneyToDouble(), date,
|
|
userId, userType);
|
|
_fineRepository.Create(entity);
|
|
_fineRepository.SaveChanges();
|
|
|
|
if (existsCheckouts.CustomizeCheckout)
|
|
{
|
|
var customizeCheckouts = _customizeCheckoutRepository.GetByWorkshopIdEmployeeIdMonthYear(
|
|
command.WorkshopId, employeeId,
|
|
year, month).GetAwaiter().GetResult();
|
|
|
|
if (customizeCheckouts != null)
|
|
{
|
|
|
|
var fines = customizeCheckouts.CheckoutFines.ToList();
|
|
|
|
fines.Add(new(entity.Title,entity.Amount.ToMoney(),
|
|
entity.FineDate.ToFarsi(),entity.FineDate,entity.IsActive,entity.CreationDate,entity.id));
|
|
customizeCheckouts.SetFines(fines);
|
|
}
|
|
}
|
|
|
|
if (existsCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
var customizeCheckoutTemp = _customizeCheckoutTempRepository.GetByWorkshopIdEmployeeIdInDate(
|
|
command.WorkshopId, employeeId, fineDate).GetAwaiter().GetResult();
|
|
|
|
if (customizeCheckoutTemp != null)
|
|
{
|
|
var fines = customizeCheckoutTemp.CheckoutFines.ToList();
|
|
|
|
|
|
fines.Add(new(entity.Title, entity.Amount.ToMoney(),
|
|
entity.FineDate.ToFarsi(), entity.FineDate, entity.IsActive, entity.CreationDate, entity.id));
|
|
customizeCheckoutTemp.SetFines(fines);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
_customizeCheckoutRepository.SaveChanges();
|
|
transaction.Complete();
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult Edit(EditFineViewModel command)
|
|
{
|
|
OperationResult op = new();
|
|
var entity = _fineRepository.Get(command.Id);
|
|
if (entity == null)
|
|
return op.Failed("چنین جریمه ای یافت نشد");
|
|
if (command.EmployeeId <= 0)
|
|
{
|
|
return op.Failed("خطای سیستمی!");
|
|
}
|
|
|
|
if (!command.FineDate.TryToGeorgianDateTime(out var fineDate))
|
|
{
|
|
return op.Failed("تاریخ وارد شده نامعتبر است");
|
|
}
|
|
|
|
if (fineDate > DateTime.Now)
|
|
{
|
|
return op.Failed("تاریخ پرداخت جریمه می بایست تاریخ امروز یا قبل تر باشد");
|
|
|
|
}
|
|
|
|
if (command.Amount.Length > 15)
|
|
{
|
|
return op.Failed("مبلغ وارد شده معتبر نیست");
|
|
}
|
|
|
|
var month = Convert.ToInt32(command.FineDate.Substring(5, 2));
|
|
var year = Convert.ToInt32(command.FineDate.Substring(0, 4));
|
|
|
|
if (!_employeeRepository.Exists(x => x.id == command.EmployeeId))
|
|
{
|
|
return op.Failed("شخص وارد شده معتبر نمیباشد");
|
|
}
|
|
|
|
|
|
_ = DateTime.Now.Date.AddMonthsFa(-1, out var oneMonthAgoGr);
|
|
|
|
if (oneMonthAgoGr > fineDate)
|
|
{
|
|
var prevCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(fineDate,
|
|
oneMonthAgoGr, entity.WorkshopId, [entity.EmployeeId]);
|
|
|
|
if (prevCheckouts.CustomizeCheckout || prevCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
return op.Failed("شما نمیتوانید در تاریخ قبل از یک ماه گذشته که فیش صادر شده باشد جریمه ای دهید");
|
|
}
|
|
}
|
|
|
|
if (!_employeeRepository.Exists(x => x.id == command.EmployeeId))
|
|
{
|
|
return op.Failed("شخص وارد شده معتبر نمیباشد");
|
|
}
|
|
|
|
|
|
var existsCheckouts = _customizeCheckoutRepository.ValidateExistsCheckouts(fineDate,
|
|
fineDate, entity.WorkshopId, [entity.EmployeeId]);
|
|
|
|
DateTime date = command.FineDate.ToGeorgianDateTime();
|
|
var (userId, userType, _) = _authHelper.GetUserTypeWithId();
|
|
|
|
using var transaction = new TransactionScope();
|
|
|
|
|
|
|
|
entity.Edit(command.EmployeeId, command.WorkshopId, command.Title, command.Amount.MoneyToDouble(), date,
|
|
userId, userType);
|
|
_fineRepository.SaveChanges();
|
|
|
|
if (existsCheckouts.CustomizeCheckout)
|
|
{
|
|
var customizeCheckouts = _customizeCheckoutRepository.GetByWorkshopIdEmployeeIdMonthYear(
|
|
entity.WorkshopId, entity.EmployeeId,
|
|
year, month).GetAwaiter().GetResult();
|
|
|
|
var fines = customizeCheckouts.CheckoutFines.ToList();
|
|
|
|
var existsSalaryAid = fines.FirstOrDefault(x => x.EntityId == entity.id);
|
|
if (existsSalaryAid != null)
|
|
{
|
|
fines.Remove(existsSalaryAid);
|
|
}
|
|
|
|
fines.Add(new(entity.Title, entity.Amount.ToMoney(), entity.FineDate.ToFarsi(), entity.FineDate,
|
|
entity.IsActive, entity.CreationDate, entity.id));
|
|
|
|
customizeCheckouts.SetFines(fines);
|
|
|
|
}
|
|
if (existsCheckouts.CustomizeCheckoutTemp)
|
|
{
|
|
var customizeCheckoutTemp = _customizeCheckoutTempRepository.GetByWorkshopIdEmployeeIdInDate(
|
|
entity.WorkshopId, entity.EmployeeId, fineDate).GetAwaiter().GetResult();
|
|
|
|
var fines = customizeCheckoutTemp.CheckoutFines.ToList();
|
|
|
|
var existsFines = fines.FirstOrDefault(x => x.EntityId == entity.id);
|
|
if (existsFines!= null)
|
|
{
|
|
fines.Remove(existsFines);
|
|
}
|
|
|
|
|
|
fines.Add(new(entity.Title, entity.Amount.ToMoney(), entity.FineDate.ToFarsi(), entity.FineDate,
|
|
entity.IsActive, entity.CreationDate, entity.id));
|
|
|
|
customizeCheckoutTemp.SetFines(fines);
|
|
|
|
}
|
|
_customizeCheckoutRepository.SaveChanges();
|
|
transaction.Complete();
|
|
return op.Succcedded(entity.id);
|
|
|
|
}
|
|
}
|