Files
Backend-Api/CompanyManagment.Application/CustomizeWorkshopSettingsApplication.cs
2025-03-08 21:49:34 +03:30

1590 lines
61 KiB
C#

using _0_Framework.Application;
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
using _0_Framework.Domain.CustomizeCheckoutShared.ValueObjects;
using AccountManagement.Application.Contracts.CameraAccount;
using Company.Domain.CustomizeWorkshopEmployeeSettingsAgg;
using Company.Domain.CustomizeWorkshopEmployeeSettingsAgg.Entities;
using Company.Domain.CustomizeWorkshopGroupSettingsAgg;
using Company.Domain.CustomizeWorkshopGroupSettingsAgg.Entities;
using Company.Domain.CustomizeWorkshopSettingsAgg;
using Company.Domain.CustomizeWorkshopSettingsAgg.Entities;
using Company.Domain.EmployeeAgg;
using CompanyManagment.App.Contracts.CustomizeWorkshopSettings;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.RollCallEmployee;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Transactions;
namespace CompanyManagment.Application;
public class CustomizeWorkshopSettingsApplication(ICustomizeWorkshopSettingsRepository customizeWorkshopSettingsRepository,
IAuthHelper authHelper, IPasswordHasher passwordHasher, ICameraAccountApplication cameraAccountApplication,
ICustomizeWorkshopGroupSettingsRepository customizeWorkshopGroupSettingsRepository, IEmployeeRepository employeeRepository,
ICustomizeWorkshopEmployeeSettingsRepository customizeWorkshopEmployeeSettingsRepository,
IRollCallEmployeeApplication rollCallEmployeeApplication)
: ICustomizeWorkshopSettingsApplication
{
private readonly ICustomizeWorkshopSettingsRepository _customizeWorkshopSettingsRepository = customizeWorkshopSettingsRepository;
private readonly ICustomizeWorkshopGroupSettingsRepository _customizeWorkshopGroupSettingsRepository = customizeWorkshopGroupSettingsRepository;
private readonly ICustomizeWorkshopEmployeeSettingsRepository _customizeWorkshopEmployeeSettingsRepository = customizeWorkshopEmployeeSettingsRepository;
private readonly IAuthHelper _authHelper = authHelper;
private readonly IEmployeeRepository _employeeRepository = employeeRepository;
private readonly IPasswordHasher _passwordHasher = passwordHasher;
private readonly ICameraAccountApplication _cameraAccountApplication = cameraAccountApplication;
private readonly IRollCallEmployeeApplication _rollCallEmployeeApplication = rollCallEmployeeApplication;
#region RollCallShifts
//Create workshop settings
public OperationResult CreateWorkshopSettings(CreateCustomizeWorkshopSettings command)
{
OperationResult op = new();
var workshopId = _passwordHasher.SlugDecrypt(_authHelper.GetWorkshopSlug());
//تبدیل شیفت های ویو مدل به انتیتی
List<CustomizeWorkshopSettingsShift> shiftCollection = new List<CustomizeWorkshopSettingsShift>();
if (command.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
#region Validation
if (command.ShiftsList.Any(x => string.IsNullOrWhiteSpace(x.StartTime) || string.IsNullOrWhiteSpace(x.EndTime)))
return op.Failed("ساعات کاری شروع و پایان نمیتواند خالی باشد");
try
{
shiftCollection =
command.ShiftsList.Select(x =>
{
var placement = x.Placement switch
{
ShiftPlacement.First => "اول",
ShiftPlacement.Second => "دوم",
ShiftPlacement.Third => "سوم"
};
if (!TimeOnly.TryParseExact(x.StartTime, "HH:mm", out TimeOnly start))
throw new InvalidDataException($"فرمت شروع نوبت{placement}نادرست است");
if (!TimeOnly.TryParseExact(x.EndTime, "HH:mm", out TimeOnly end))
throw new InvalidDataException($"فرمت پایان نوبت{placement}نادرست است");
return new CustomizeWorkshopSettingsShift(start, end, x.Placement);
}).ToList();
}
catch (InvalidDataException e)
{
return op.Failed(e.Message);
}
if (workshopId < 1)
{
return op.Failed("خطای سیستمی");
}
DateTime day = DateTime.Now;
DateTime previousEnd = new DateTime();
var finalShiftList = new List<(DateTime start, DateTime end, ShiftPlacement placement)>();
shiftCollection = shiftCollection.OrderBy(x => x.Placement).ToList();
foreach (var shift in shiftCollection)
{
(DateTime start, DateTime end, ShiftPlacement placement) newShift =
new()
{
placement = shift.Placement,
start = new DateTime(DateOnly.FromDateTime(day), shift.StartTime),
end = new DateTime(DateOnly.FromDateTime(day), shift.EndTime)
};
if (previousEnd != new DateTime())
{
if (newShift.start <= previousEnd)
{
newShift.start = newShift.start.AddDays(1);
}
}
while (newShift.start >= newShift.end)
{
newShift.end = newShift.end.AddDays(1);
}
finalShiftList.Add(newShift);
previousEnd = newShift.end;
}
var firstShiftStart = finalShiftList.FirstOrDefault(x => x.placement == ShiftPlacement.First).start;
var lastShiftEnd = finalShiftList.OrderByDescending(x => x.placement).FirstOrDefault().end;
if (firstShiftStart.AddDays(1) < lastShiftEnd)
return op.Failed("بازه زمانی کارگاه نامعتبر است");
var total = finalShiftList.Sum(x => (x.end - x.start).TotalHours);
if (total >= 24)
{
return op.Failed("بازه زمانی کارگاه نمیتواند بیشتر از 24 ساعت باشد");
}
#endregion
}
else
{
command.ShiftsList = [];
shiftCollection = [];
}
var record = new CustomizeWorkshopSettings(workshopId, shiftCollection, command.LeavePermittedDays,
command.WorkshopShiftStatus, command.FridayWork, command.HolidayWork);
using (var transaction = new TransactionScope())
{
try
{
_customizeWorkshopSettingsRepository.Create(record);
_customizeWorkshopSettingsRepository.SaveChanges();
OperationResult result = new OperationResult();
result = command.WorkshopShiftStatus == WorkshopShiftStatus.Regular ? CreateGeneralGroup(record) : result.Succcedded();
if (result.IsSuccedded)
{
transaction.Complete();
}
else
{
op = result;
transaction.Dispose();
}
}
catch
{
transaction.Dispose();
// ignored
}
}
return string.IsNullOrWhiteSpace(op.Message) ? op.Succcedded() : op;
}
//create group settings with workshopSettingsId.
public OperationResult CreateGroupSettingsByRollCallWorkshopSettingId(CreateCustomizeWorkshopGroupSettings command)
{
OperationResult op = new();
CustomizeWorkshopSettings workshopSettings =
_customizeWorkshopSettingsRepository.Get(command.CustomizeWorkshopSettingId);
List<CustomizeWorkshopGroupSettingsShift> shiftCollection = new List<CustomizeWorkshopGroupSettingsShift>();
ICollection<CustomizeRotatingShift> customizeRotatingShifts = [];
#region validation
if (workshopSettings == null)
return op.Failed("خطای سیستمی");
if (string.IsNullOrWhiteSpace(command.Name))
return op.Failed("لطفا نام گروه را وارد کنید");
if (string.IsNullOrWhiteSpace(command.Salary))
return op.Failed("لطفا حقوق مورد نظر خود را وارد کنید");
if (!_customizeWorkshopSettingsRepository.Exists(x => x.id == command.CustomizeWorkshopSettingId))
return op.Failed("چنین ساعت کاری برای کارگاهی وجود ندارد");
if (command.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
if (command.ShiftViewModel.Any(x => string.IsNullOrWhiteSpace(x.StartTime) || string.IsNullOrWhiteSpace(x.EndTime)))
return op.Failed("ساعات کاری شروع و پایان نمیتواند خالی باشد");
try
{
shiftCollection =
command.ShiftViewModel.Select(x =>
{
var placement = x.Placement switch
{
ShiftPlacement.First => "اول",
ShiftPlacement.Second => "دوم",
ShiftPlacement.Third => "سوم"
};
if (!TimeOnly.TryParseExact(x.StartTime, "HH:mm", out TimeOnly start))
throw new InvalidDataException($"فرمت شروع نوبت{placement}نادرست است");
if (!TimeOnly.TryParseExact(x.EndTime, "HH:mm", out TimeOnly end))
throw new InvalidDataException($"فرمت پایان نوبت{placement}نادرست است");
return new CustomizeWorkshopGroupSettingsShift(start, end, x.Placement);
}).ToList();
}
catch (InvalidDataException e)
{
return op.Failed(e.Message);
}
DateTime previousEnd = new DateTime();
var finalShiftList = new List<(DateTime start, DateTime end, ShiftPlacement placement)>();
shiftCollection = shiftCollection.OrderBy(x => x.Placement).ToList();
foreach (var shift in shiftCollection)
{
(DateTime start, DateTime end, ShiftPlacement placement) newShift =
new()
{
placement = shift.Placement,
start = new DateTime(DateOnly.MinValue, shift.StartTime),
end = new DateTime(DateOnly.MinValue, shift.EndTime)
};
if (previousEnd != new DateTime())
{
if (newShift.start <= previousEnd)
{
newShift.start = newShift.start.AddDays(1);
}
}
while (newShift.start >= newShift.end)
{
newShift.end = newShift.end.AddDays(1);
}
finalShiftList.Add(newShift);
previousEnd = newShift.end;
}
//var firstWorkshopTimeShift = workshopSettings.CustomizeWorkshopSettingsShifts.MinBy(x => x.Placement).StartTime;
//var lastWorkshopTimeShift = workshopSettings.CustomizeWorkshopSettingsShifts.MaxBy(x => x.Placement).EndTime;
//var startDateTime = new DateTime(DateOnly.MinValue, firstWorkshopTimeShift);
//var lastDateTime = new DateTime(DateOnly.MinValue, lastWorkshopTimeShift);
//if (lastDateTime < startDateTime)
//{
// lastDateTime = lastDateTime.AddDays(1);
//}
//var lastGroupShift = finalShiftList.MaxBy(x => x.placement).end;
//var firstGroupShift = finalShiftList.MinBy(x => x.placement).start;
//if (lastDateTime < lastGroupShift || firstGroupShift < startDateTime)
//{
// return op.Failed("ساعت کاری گروه باید بین ساعت کاری کارگاه باشد");
//}
}
else if (command.WorkshopShiftStatus == WorkshopShiftStatus.Irregular)
{
var irregularShiftStartTime = new DateTime(DateOnly.MinValue, command.IrregularShift.StartTime);
var irregularShiftEndTime = new DateTime(DateOnly.MinValue, command.IrregularShift.EndTime);
if (irregularShiftEndTime < irregularShiftStartTime)
{
irregularShiftEndTime = irregularShiftEndTime.AddDays(1);
}
switch (command.IrregularShift.WorkshopIrregularShifts)
{
case WorkshopIrregularShifts.TwelveThirtySix:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
case WorkshopIrregularShifts.TwelveTwentyFour:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
}
}
else
{
customizeRotatingShifts = command.CustomizeRotatingShiftsViewModels.Select(x=> new CustomizeRotatingShift(TimeOnly.Parse( x.StartTime), TimeOnly.Parse(x.EndTime))).ToList();
}
#endregion
var breakTime = new BreakTime(command.BreakTime.HasBreakTimeValue, command.BreakTime.BreakTimeValue);
double salary = command.Salary.MoneyToDouble();
var entity = new CustomizeWorkshopGroupSettings(command.Name, salary, command.CustomizeWorkshopSettingId, shiftCollection, workshopSettings.FridayPay, workshopSettings.OverTimePay,
workshopSettings.BaseYearsPay, workshopSettings.BonusesPay, workshopSettings.NightWorkPay, workshopSettings.MarriedAllowance,
workshopSettings.ShiftPay, workshopSettings.FamilyAllowance, workshopSettings.LeavePay, workshopSettings.InsuranceDeduction, workshopSettings.FineAbsenceDeduction,
workshopSettings.LateToWork, workshopSettings.EarlyExit, command.FridayWork,
command.HolidayWork, breakTime, command.WorkshopShiftStatus, command.IrregularShift, command.LeavePermittedDays,customizeRotatingShifts);
_customizeWorkshopGroupSettingsRepository.Create(entity);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
//Create Employee Settings with Group data. It will Replace the Group data (such as salary, shifts and ...) on Employee settings On creation.
public OperationResult CreateEmployeesSettingsAndSetChanges(EditCustomizeEmployeeSettings command)
{
var op = new OperationResult();
var customizeWorkshopGroupSettings = _customizeWorkshopGroupSettingsRepository.Get(command.GroupId);
if (customizeWorkshopGroupSettings == null)
{
return op.Failed("گروه انتخاب شده نا معتبر است");
}
if (customizeWorkshopGroupSettings.MainGroup)
{
var createDefaultEmployee = CreateEmployeeSettings(command);
return createDefaultEmployee;
}
List<CustomizeWorkshopEmployeeSettingsShift> shiftCollection = new List<CustomizeWorkshopEmployeeSettingsShift>();
List<CustomizeRotatingShift> rotatingShift = [];
var isChanged = false;
#region validation
if (command.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
if (command.ShiftViewModel.Any(x => string.IsNullOrWhiteSpace(x.StartTime) || string.IsNullOrWhiteSpace(x.EndTime)))
return op.Failed("ساعات کاری شروع و پایان نمیتواند خالی باشد");
try
{
shiftCollection =
command.ShiftViewModel.Select(x =>
{
var placement = x.Placement switch
{
ShiftPlacement.First => "اول",
ShiftPlacement.Second => "دوم",
ShiftPlacement.Third => "سوم"
};
if (!TimeOnly.TryParseExact(x.StartTime, "HH:mm", out TimeOnly start))
throw new InvalidDataException($"فرمت شروع نوبت{placement}نادرست است");
if (!TimeOnly.TryParseExact(x.EndTime, "HH:mm", out TimeOnly end))
throw new InvalidDataException($"فرمت پایان نوبت{placement}نادرست است");
return new CustomizeWorkshopEmployeeSettingsShift(start, end, x.Placement);
}).ToList();
}
catch (InvalidDataException e)
{
return op.Failed(e.Message);
}
DateTime previousEnd = new DateTime();
var finalShiftList = new List<(DateTime start, DateTime end, ShiftPlacement placement)>();
shiftCollection = shiftCollection.OrderBy(x => x.Placement).ToList();
foreach (var shift in shiftCollection)
{
(DateTime start, DateTime end, ShiftPlacement placement) newShift =
new()
{
placement = shift.Placement,
start = new DateTime(DateOnly.MinValue, shift.StartTime),
end = new DateTime(DateOnly.MinValue, shift.EndTime)
};
if (previousEnd != new DateTime())
{
if (newShift.start == previousEnd)
{
return op.Failed("در شیفت منظم پایان شیفت نمیتواند با شروع شیفت بعدی برابر باشد");
}
if (newShift.start <= previousEnd)
{
newShift.start = newShift.start.AddDays(1);
}
}
while (newShift.start >= newShift.end)
{
newShift.end = newShift.end.AddDays(1);
}
finalShiftList.Add(newShift);
previousEnd = newShift.end;
}
//var firstWorkshopTimeShift = workshopSettings.CustomizeWorkshopSettingsShifts.MinBy(x => x.Placement).StartTime;
//var lastWorkshopTimeShift = workshopSettings.CustomizeWorkshopSettingsShifts.MaxBy(x => x.Placement).EndTime;
//var startDateTime = new DateTime(DateOnly.MinValue, firstWorkshopTimeShift);
//var lastDateTime = new DateTime(DateOnly.MinValue, lastWorkshopTimeShift);
//if (lastDateTime < startDateTime)
//{
// lastDateTime = lastDateTime.AddDays(1);
//}
//var lastGroupShift = finalShiftList.MaxBy(x => x.placement).end;
//var firstGroupShift = finalShiftList.MinBy(x => x.placement).start;
//if (lastDateTime < lastGroupShift || firstGroupShift < startDateTime)
//{
// return op.Failed("ساعت کاری گروه باید بین ساعت کاری کارگاه باشد");
//}
if (shiftCollection.All(x => customizeWorkshopGroupSettings.CustomizeWorkshopGroupSettingsShifts.Any(y => x.Equals(y)))
&& command.WorkshopShiftStatus == customizeWorkshopGroupSettings.WorkshopShiftStatus && command.FridayWork == customizeWorkshopGroupSettings.FridayWork &&
command.HolidayWork == customizeWorkshopGroupSettings.HolidayWork && command.BreakTime == customizeWorkshopGroupSettings.BreakTime)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
else if (command.WorkshopShiftStatus == WorkshopShiftStatus.Irregular)
{
var irregularShiftStartTime = new DateTime(DateOnly.MinValue, command.IrregularShift.StartTime);
var irregularShiftEndTime = new DateTime(DateOnly.MinValue, command.IrregularShift.EndTime);
if (irregularShiftEndTime < irregularShiftStartTime)
{
irregularShiftEndTime = irregularShiftEndTime.AddDays(1);
}
switch (command.IrregularShift.WorkshopIrregularShifts)
{
case WorkshopIrregularShifts.TwelveThirtySix:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
case WorkshopIrregularShifts.TwelveTwentyFour:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
}
if (command.WorkshopShiftStatus == customizeWorkshopGroupSettings.WorkshopShiftStatus && command.BreakTime == customizeWorkshopGroupSettings.BreakTime &&
command.IrregularShift == customizeWorkshopGroupSettings.IrregularShift && command.FridayWork == customizeWorkshopGroupSettings.FridayWork &&
command.HolidayWork == customizeWorkshopGroupSettings.HolidayWork)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
else
{
rotatingShift = command.CustomizeRotatingShifts
.Select(x => new CustomizeRotatingShift(TimeOnly.Parse(x.StartTime), TimeOnly.Parse(x.EndTime)))
.ToList();
if (rotatingShift.All(x => customizeWorkshopGroupSettings.CustomizeRotatingShifts.Any(y => x.Equals(y)))
&& command.WorkshopShiftStatus == customizeWorkshopGroupSettings.WorkshopShiftStatus &&
command.FridayWork == customizeWorkshopGroupSettings.FridayWork &&
command.HolidayWork == customizeWorkshopGroupSettings.HolidayWork && command.BreakTime == customizeWorkshopGroupSettings.BreakTime)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
#endregion
var breakTime = new BreakTime(command.BreakTime.HasBreakTimeValue, command.BreakTime.BreakTimeValue);
var entity = new CustomizeWorkshopEmployeeSettings(customizeWorkshopGroupSettings.FridayPay, customizeWorkshopGroupSettings.OverTimePay,
customizeWorkshopGroupSettings.BaseYearsPay, customizeWorkshopGroupSettings.BonusesPay, customizeWorkshopGroupSettings.NightWorkPay, customizeWorkshopGroupSettings.MarriedAllowance,
customizeWorkshopGroupSettings.ShiftPay, customizeWorkshopGroupSettings.FamilyAllowance, customizeWorkshopGroupSettings.LeavePay, customizeWorkshopGroupSettings.InsuranceDeduction, customizeWorkshopGroupSettings.FineAbsenceDeduction,
customizeWorkshopGroupSettings.LateToWork, customizeWorkshopGroupSettings.EarlyExit, command.EmployeeIds.First(), command.WorkshopId, customizeWorkshopGroupSettings.Salary, command.GroupId,
shiftCollection, command.FridayWork, command.HolidayWork, command.IrregularShift, command.WorkshopShiftStatus, breakTime, command.LeavePermittedDays, rotatingShift);
_customizeWorkshopEmployeeSettingsRepository.Create(entity);
entity.SimpleEdit(shiftCollection, command.IrregularShift, command.WorkshopShiftStatus, command.BreakTime, isChanged, command.FridayWork, command.HolidayWork, rotatingShift);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult EditWorkshopSettingShifts(List<CustomizeWorkshopShiftViewModel> shiftViewModels, long customizeWorkshopSettingsId,
WorkshopShiftStatus workshopShiftStatus, FridayWork fridayWork, HolidayWork holidayWork)
{
OperationResult op = new OperationResult();
var entity = _customizeWorkshopSettingsRepository.Get(customizeWorkshopSettingsId);
if (entity == null)
return op.Failed("چنین آیتمی وجود ندارد");
//تبدیل شیفت های ویو مدل به انتیتی
List<CustomizeWorkshopSettingsShift> shiftCollection = [];
#region Validation
if (workshopShiftStatus == WorkshopShiftStatus.Regular)
{
try
{
shiftCollection =
shiftViewModels.Select(x =>
{
var placement = x.Placement switch
{
ShiftPlacement.First => "اول",
ShiftPlacement.Second => "دوم",
ShiftPlacement.Third => "سوم",
_ => throw new ArgumentOutOfRangeException()
};
if (!TimeOnly.TryParseExact(x.StartTime, "HH:mm", out TimeOnly start))
throw new InvalidDataException($"فرمت شروع نوبت{placement}نادرست است");
if (!TimeOnly.TryParseExact(x.EndTime, "HH:mm", out TimeOnly end))
throw new InvalidDataException($"فرمت پایان نوبت{placement}نادرست است");
return new CustomizeWorkshopSettingsShift(start, end, x.Placement);
}).ToList();
}
catch (InvalidDataException e)
{
return op.Failed(e.Message);
}
DateTime day = DateTime.Now;
DateTime previousEnd = new DateTime();
var finalShiftList = new List<(DateTime start, DateTime end, ShiftPlacement placement)>();
shiftCollection = shiftCollection.OrderBy(x => x.Placement).ToList();
foreach (var shift in shiftCollection)
{
(DateTime start, DateTime end, ShiftPlacement placement) newShift =
new()
{
placement = shift.Placement,
start = new DateTime(DateOnly.FromDateTime(day), shift.StartTime),
end = new DateTime(DateOnly.FromDateTime(day), shift.EndTime)
};
if (previousEnd != new DateTime())
{
if (newShift.start <= previousEnd)
{
newShift.start = newShift.start.AddDays(1);
}
}
while (newShift.start >= newShift.end)
{
newShift.end = newShift.end.AddDays(1);
}
finalShiftList.Add(newShift);
previousEnd = newShift.end;
}
var firstShiftStart = finalShiftList.FirstOrDefault(x => x.placement == ShiftPlacement.First).start;
var lastShiftEnd = finalShiftList.OrderByDescending(x => x.placement).FirstOrDefault().end;
if (firstShiftStart.AddDays(1) < lastShiftEnd)
return op.Failed("بازه زمانی کارگاه نامعتبر است");
var total = finalShiftList.Sum(x => (x.end - x.start).TotalHours);
if (total >= 24)
{
return op.Failed("بازه زمانی کارگاه نمیتواند بیشتر از 24 ساعت باشد");
}
}
#endregion
using var transActionScope = new TransactionScope();
entity.ChangeWorkshopShifts(shiftCollection, workshopShiftStatus, fridayWork, holidayWork);
//op = ChangeAllGroupsShiftsWithEmployees(entity, replaceChangedGroups);
//if (!op.IsSuccedded)
//{
// return op;
//}
_customizeWorkshopSettingsRepository.SaveChanges();
if (!(_customizeWorkshopGroupSettingsRepository.Exists(x => x.CustomizeWorkshopSettingId == entity.id && x.MainGroup)) && entity.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
var operationResult = CreateGeneralGroup(entity);
if (!operationResult.IsSuccedded)
return operationResult;
}
transActionScope.Complete();
return op.Succcedded();
}
public OperationResult EditSimpleRollCallGroupSetting(EditCustomizeWorkshopGroupSettings command)
{
OperationResult op = new();
#region Validation
if (string.IsNullOrWhiteSpace(command.Name))
return op.Failed("لطفا نام گروه را وارد کنید");
if (!_customizeWorkshopGroupSettingsRepository.Exists(x => x.id == command.Id))
return op.Failed("چنین ساعت کاری برای گروه وجود ندارد");
#endregion
var entity = _customizeWorkshopGroupSettingsRepository.GetWithEmployees(command.Id);
var workshopSettings = _customizeWorkshopSettingsRepository.Get(entity.CustomizeWorkshopSettingId);
var employeeIds = command.IsShiftChanged ? entity.CustomizeWorkshopEmployeeSettingsCollection
.Select(x => x.id).ToList() : entity.CustomizeWorkshopEmployeeSettingsCollection.Where(x => !x.IsShiftChanged)
.Select(x => x.id).ToList();
var groupSettingsShifts = new List<CustomizeWorkshopGroupSettingsShift>();
ICollection<CustomizeRotatingShift> rotatingShift = [];
bool isChanged;
if (command.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
groupSettingsShifts = command.ShiftViewModel.Select(x =>
{
if (!TimeOnly.TryParseExact(x.StartTime, "HH:mm", out TimeOnly start))
throw new InvalidDataException();
if (!TimeOnly.TryParseExact(x.EndTime, "HH:mm", out TimeOnly end))
throw new InvalidDataException();
return new CustomizeWorkshopGroupSettingsShift(start, end, x.Placement);
}).ToList();
;
if (groupSettingsShifts.All(x => workshopSettings.CustomizeWorkshopSettingsShifts.Any(y => x.Equals(y)))
&& command.WorkshopShiftStatus == workshopSettings.WorkshopShiftStatus && command.FridayWork == workshopSettings.FridayWork &&
command.HolidayWork == workshopSettings.HolidayWork&&
command.BreakTime == workshopSettings.BreakTime)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
else if (command.WorkshopShiftStatus == WorkshopShiftStatus.Irregular)
{
var irregularShiftStartTime = new DateTime(DateOnly.MinValue, command.IrregularShift.StartTime);
var irregularShiftEndTime = new DateTime(DateOnly.MinValue, command.IrregularShift.EndTime);
if (irregularShiftEndTime < irregularShiftStartTime)
{
irregularShiftEndTime = irregularShiftEndTime.AddDays(1);
}
switch (command.IrregularShift.WorkshopIrregularShifts)
{
case WorkshopIrregularShifts.TwelveThirtySix:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
case WorkshopIrregularShifts.TwelveTwentyFour:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
}
if (command.WorkshopShiftStatus == workshopSettings.WorkshopShiftStatus && command.BreakTime == workshopSettings.BreakTime
&& command.FridayWork == workshopSettings.FridayWork &&
command.HolidayWork == workshopSettings.HolidayWork)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
else
{
rotatingShift = command.CustomizeRotatingShiftsViewModels
.Select(x => new CustomizeRotatingShift(TimeOnly.Parse(x.StartTime), TimeOnly.Parse(x.EndTime))).ToList();
if (command.WorkshopShiftStatus == workshopSettings.WorkshopShiftStatus && command.BreakTime == workshopSettings.BreakTime
&& command.FridayWork == workshopSettings.FridayWork &&
command.HolidayWork == workshopSettings.HolidayWork)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
var breakTime = new BreakTime(command.BreakTime.HasBreakTimeValue, command.BreakTime.BreakTimeValue);
entity.EditSimpleAndOverwriteOnEmployee(command.Name, employeeIds, groupSettingsShifts, command.WorkshopShiftStatus,
command.IrregularShift, breakTime, isChanged, command.FridayWork, command.HolidayWork,rotatingShift);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult EditSimpleRollCallEmployeeSetting(EditCustomizeEmployeeSettings command)
{
OperationResult op = new();
var entity = _customizeWorkshopEmployeeSettingsRepository.Get(command.Id);
#region Validation
if (entity == null)
return op.Failed("چنین پرسنلی وجود ندارد");
#endregion
var groupSettings =
_customizeWorkshopGroupSettingsRepository.GetIncludeWorkshopSettings(
entity.CustomizeWorkshopGroupSettingId);
List<CustomizeWorkshopEmployeeSettingsShift> employeesShifts = [];
ICollection<CustomizeRotatingShift> rotatingShift = [];
bool isChanged = false;
if (command.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
#region Validation
if (command.ShiftViewModel.Any(x => string.IsNullOrWhiteSpace(x.StartTime) || string.IsNullOrWhiteSpace(x.EndTime)))
return op.Failed("ساعات کاری شروع و پایان نمیتواند خالی باشد");
try
{
employeesShifts =
command.ShiftViewModel.Select(x =>
{
var placement = x.Placement switch
{
ShiftPlacement.First => "اول",
ShiftPlacement.Second => "دوم",
ShiftPlacement.Third => "سوم"
};
if (!TimeOnly.TryParseExact(x.StartTime, "HH:mm", out TimeOnly start))
throw new InvalidDataException($"فرمت شروع نوبت{placement}نادرست است");
if (!TimeOnly.TryParseExact(x.EndTime, "HH:mm", out TimeOnly end))
throw new InvalidDataException($"فرمت پایان نوبت{placement}نادرست است");
return new CustomizeWorkshopEmployeeSettingsShift(start, end, x.Placement);
}).ToList();
}
catch (InvalidDataException e)
{
return op.Failed(e.Message);
}
if (command.WorkshopId < 1)
{
return op.Failed("خطای سیستمی");
}
DateTime day = DateTime.Now;
DateTime previousEnd = new DateTime();
var finalShiftList = new List<(DateTime start, DateTime end, ShiftPlacement placement)>();
employeesShifts = employeesShifts.OrderBy(x => x.Placement).ToList();
foreach (var shift in employeesShifts)
{
(DateTime start, DateTime end, ShiftPlacement placement) newShift =
new()
{
placement = shift.Placement,
start = new DateTime(DateOnly.FromDateTime(day), shift.StartTime),
end = new DateTime(DateOnly.FromDateTime(day), shift.EndTime)
};
if (previousEnd != new DateTime())
{
if (newShift.start <= previousEnd)
{
newShift.start = newShift.start.AddDays(1);
}
}
while (newShift.start >= newShift.end)
{
newShift.end = newShift.end.AddDays(1);
}
finalShiftList.Add(newShift);
previousEnd = newShift.end;
}
var firstShiftStart = finalShiftList.FirstOrDefault(x => x.placement == ShiftPlacement.First).start;
var lastShiftEnd = finalShiftList.OrderByDescending(x => x.placement).FirstOrDefault().end;
if (firstShiftStart.AddDays(1) < lastShiftEnd)
return op.Failed("بازه زمانی کارگاه نامعتبر است");
var total = finalShiftList.Sum(x => (x.end - x.start).TotalHours);
if (total >= 24)
{
return op.Failed("بازه زمانی کارگاه نمیتواند بیشتر از 24 ساعت باشد");
}
#endregion
if (employeesShifts.All(x => groupSettings.CustomizeWorkshopGroupSettingsShifts.Any(y => x.Equals(y)))
&& command.WorkshopShiftStatus == groupSettings.WorkshopShiftStatus && command.FridayWork == groupSettings.FridayWork &&
command.HolidayWork == groupSettings.HolidayWork &&command.BreakTime == groupSettings.BreakTime)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
else if (command.WorkshopShiftStatus == WorkshopShiftStatus.Irregular)
{
var irregularShiftStartTime = new DateTime(DateOnly.MinValue, command.IrregularShift.StartTime);
var irregularShiftEndTime = new DateTime(DateOnly.MinValue, command.IrregularShift.EndTime);
if (irregularShiftEndTime < irregularShiftStartTime)
{
irregularShiftEndTime = irregularShiftEndTime.AddDays(1);
}
switch (command.IrregularShift.WorkshopIrregularShifts)
{
case WorkshopIrregularShifts.TwelveThirtySix:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
case WorkshopIrregularShifts.TwelveTwentyFour:
if ((irregularShiftEndTime - irregularShiftStartTime).TotalHours > 12)
{
return op.Failed("ساعت کاری شما نمیتواند بیشتر از 12 ساعت باشد");
}
break;
}
if (command.WorkshopShiftStatus == groupSettings.WorkshopShiftStatus && command.BreakTime == groupSettings.BreakTime &&
command.IrregularShift == groupSettings.IrregularShift && command.FridayWork == groupSettings.FridayWork &&
command.HolidayWork == groupSettings.HolidayWork)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
else
{
rotatingShift = command.CustomizeRotatingShifts
.Select(x => new CustomizeRotatingShift(TimeOnly.Parse( x.StartTime), TimeOnly.Parse(x.EndTime))).ToList();
if (rotatingShift.All(x => groupSettings.CustomizeRotatingShifts.Any(y => x.Equals(y)))
&& command.WorkshopShiftStatus == groupSettings.WorkshopShiftStatus && command.FridayWork == groupSettings.FridayWork &&
command.HolidayWork == groupSettings.HolidayWork && command.BreakTime == groupSettings.BreakTime)
{
isChanged = false;
}
else
{
isChanged = true;
}
}
entity.SimpleEdit(employeesShifts, command.IrregularShift, command.WorkshopShiftStatus, command.BreakTime,
isChanged, command.FridayWork, command.HolidayWork,rotatingShift);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
//Remove the Employee From the Group Settings
public OperationResult RemoveEmployeeFromRollCallWorkshopGroup(long employeeId, long groupId, long workshopId)
{
OperationResult op = new();
var groupEntity = _customizeWorkshopGroupSettingsRepository.GetWithEmployees(groupId);
#region Validation
if (groupEntity == null)
{
return op.Failed("چنین گروهی وجود ندارد");
}
#endregion
using var transaction = new TransactionScope();
groupEntity.RemoveEmployeeFromGroup(employeeId);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
var res = AddEmployeeToMainGroupSettings(workshopId, employeeId);
if (res.IsSuccedded)
{
transaction.Complete();
return op.Succcedded();
}
return res;
}
public OperationResult AddEmployeeToMainGroupSettings(long workshopId, long employeeId)
{
var op = new OperationResult();
var mainGroup = _customizeWorkshopGroupSettingsRepository.GetWorkshopMainGroup(workshopId);
if (mainGroup == null)
return op.Succcedded();
mainGroup.AddEmployeeSettingToGroupWithGroupData(employeeId, workshopId);
_customizeWorkshopEmployeeSettingsRepository.SaveChanges();
return op.Succcedded();
}
#endregion
#region CustomizeCheckoutSettings
public OperationResult CreateEmployeeSettings(CreateCustomizeEmployeeSettings command)
{
OperationResult op = new();
CustomizeWorkshopGroupSettings mainGroup = new CustomizeWorkshopGroupSettings();
#region Validation
//if (!_customizeWorkshopGroupSettingsRepository.Exists(x => x.id == command.GroupId))
// return op.Failed("چنین گروهی وجود ندارد");
if (!_employeeRepository.Exists(x => command.EmployeeIds.Any(y => x.id == y)))
return op.Failed("چنین پرسنلی وجود ندارد");
#endregion
var groupData = _customizeWorkshopGroupSettingsRepository.GetIncludeWorkshopSettings(command.GroupId);
if (groupData == null)
return op.Failed("خطای سیستمی");
var workshopSettings = _customizeWorkshopSettingsRepository.Get(groupData.CustomizeWorkshopSettingId);
var employeesInMainGroup = new List<CustomizeWorkshopEmployeeSettings>();
mainGroup = _customizeWorkshopGroupSettingsRepository.GetWorkshopMainGroup(command.WorkshopId);
if (workshopSettings.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
if (mainGroup != null)
{
employeesInMainGroup = _customizeWorkshopEmployeeSettingsRepository.GetBy(mainGroup.id);
if (_customizeWorkshopEmployeeSettingsRepository.Exists(x => x.WorkshopId == command.WorkshopId
&& command.EmployeeIds.Contains(x.EmployeeId) && x.CustomizeWorkshopGroupSettingId != mainGroup.id))
{
return op.Failed("این پرسنل در گروه دیگری وجود دارد");
}
}
else
{
if (_customizeWorkshopEmployeeSettingsRepository.Exists(x => x.WorkshopId == command.WorkshopId
&& command.EmployeeIds.Contains(x.EmployeeId)))
{
return op.Failed("این پرسنل در گروه دیگری وجود دارد");
}
}
}
else
{
if (mainGroup != null)
{
employeesInMainGroup = _customizeWorkshopEmployeeSettingsRepository.GetBy(mainGroup.id);
if (_customizeWorkshopEmployeeSettingsRepository.Exists(x => x.WorkshopId == command.WorkshopId
&& command.EmployeeIds.Contains(
x.EmployeeId) &&
x.CustomizeWorkshopGroupSettingId !=
mainGroup.id))
{
return op.Failed("این پرسنل در گروه دیگری وجود دارد");
}
}
else
{
if (_customizeWorkshopEmployeeSettingsRepository.Exists(x => x.WorkshopId == command.WorkshopId
&& command.EmployeeIds.Contains(x.EmployeeId)))
{
return op.Failed("این پرسنل در گروه دیگری وجود دارد");
}
}
}
var shifts = groupData.CustomizeWorkshopGroupSettingsShifts
.Select(x => new CustomizeWorkshopEmployeeSettingsShift(x.StartTime, x.EndTime, x.Placement)).ToList();
foreach (var id in command.EmployeeIds)
{
var employeeSettings = employeesInMainGroup.FirstOrDefault(x => x.EmployeeId == id);
if (employeeSettings != null)
{
_customizeWorkshopEmployeeSettingsRepository.Remove(employeeSettings.id);
}
var entity = new CustomizeWorkshopEmployeeSettings(
new(groupData.FridayPay.FridayPayType, groupData.FridayPay.Value),
new(groupData.OverTimePay.OverTimePayType, groupData.OverTimePay.Value),
new(groupData.BaseYearsPay.BaseYearsPayType, groupData.BaseYearsPay.Value, groupData.BaseYearsPay.PaymentType),
new(groupData.BonusesPay.BonusesPayType, groupData.BonusesPay.Value, groupData.BonusesPay.PaymentType),
new(groupData.NightWorkPay.NightWorkingType, groupData.NightWorkPay.Value),
new(groupData.MarriedAllowance.MarriedAllowanceType, groupData.MarriedAllowance.Value),
new(groupData.ShiftPay.ShiftType, groupData.ShiftPay.ShiftPayType, groupData.ShiftPay.Value),
new(groupData.FamilyAllowance.FamilyAllowanceType, groupData.FamilyAllowance.Value),
new(groupData.LeavePay.LeavePayType, groupData.LeavePay.Value),
new(groupData.InsuranceDeduction.InsuranceDeductionType, groupData.InsuranceDeduction.Value),
new(groupData.FineAbsenceDeduction.FineAbsenceDeductionType, groupData.FineAbsenceDeduction.Value,
groupData.FineAbsenceDeduction.FineAbsenceDayOfWeekCollection
.Select(x => new FineAbsenceDayOfWeek(x.DayOfWeek)).ToList()),
new(groupData.LateToWork.LateToWorkType,
groupData.LateToWork.LateToWorkTimeFines.Select(x => new LateToWorkTimeFine(x.Minute, x.FineMoney))
.ToList(), groupData.LateToWork.Value),
new(groupData.EarlyExit.EarlyExitType,
groupData.EarlyExit.EarlyExitTimeFines.Select(x => new EarlyExitTimeFine(x.Minute, x.FineMoney))
.ToList(), groupData.EarlyExit.Value),
id,
groupData.CustomizeWorkshopSettings.WorkshopId,
groupData.Salary,
groupData.id,
shifts.Select(x => new CustomizeWorkshopEmployeeSettingsShift(x.StartTime, x.EndTime, x.Placement))
.ToList(),
groupData.FridayWork,
groupData.HolidayWork,
groupData.IrregularShift,
groupData.WorkshopShiftStatus,
new(groupData.BreakTime.HasBreakTimeValue, groupData.BreakTime.BreakTimeValue),
command.LeavePermittedDays,
groupData.CustomizeRotatingShifts.Select(x=> new CustomizeRotatingShift(x.StartTime,x.EndTime)).ToList()
);
_customizeWorkshopEmployeeSettingsRepository.Create(entity);
}
_customizeWorkshopEmployeeSettingsRepository.SaveChanges();
return op.Succcedded();
}
//Edit the Workshop Settings Data
public OperationResult EditWorkshopSetting(EditCustomizeWorkshopSettings command, bool replaceInAllGroups)
{
OperationResult op = new();
#region Validation
if (!_customizeWorkshopSettingsRepository.Exists(x => x.id == command.Id))
return op.Failed("خطای سیستمی");
#endregion
var entity = _customizeWorkshopSettingsRepository.Get(command.Id);
if (entity == null)
return op.Failed("چنین آیتمی وجود ندارد");
FridayPay fridayPay = new(command.FridayPay.FridayPayType, command.FridayPay.Value);
OverTimePay overTimePay = new(command.OverTimePay.OverTimePayType, command.OverTimePay.Value);
BaseYearsPay baseYearsPay = new(command.BaseYearsPay.BaseYearsPayType, command.BaseYearsPay.Value, command.BaseYearsPay.PaymentType);
BonusesPay bonusesPay = new(command.BonusesPay.BonusesPayType, command.BonusesPay.Value, command.BonusesPay.PaymentType);
NightWorkPay nightWorkPay = new(command.NightWorkPay.NightWorkingType, command.NightWorkPay.Value);
MarriedAllowance marriedAllowance = new(command.MarriedAllowance.MarriedAllowanceType, command.MarriedAllowance.Value);
ShiftPay shiftPay = new(ShiftType.None, ShiftPayType.None, 0);
FamilyAllowance familyAllowance = new(command.FamilyAllowance.FamilyAllowanceType, command.FamilyAllowance.Value);
LeavePay leavePay = new(command.LeavePay.LeavePayType, command.LeavePay.Value);
InsuranceDeduction insuranceDeduction = new(command.InsuranceDeduction.InsuranceDeductionType, command.InsuranceDeduction.Value);
FineAbsenceDeduction fineAbsenceDeduction = new(
command.FineAbsenceDeduction.FineAbsenceDeductionType,
command.FineAbsenceDeduction.Value,
command.FineAbsenceDeduction.FineAbsenceDayOfWeekViewModels?.Select(x => new FineAbsenceDayOfWeek(x.DayOfWeek)).ToList() ?? new List<FineAbsenceDayOfWeek>()
);
LateToWork lateToWork = new(
command.LateToWork.LateToWorkType,
command.LateToWork.LateToWorkTimeFinesVewModels?.Select(x => new LateToWorkTimeFine(x.Minute, x.FineMoney))
.ToList() ?? new List<LateToWorkTimeFine>()
, command.LateToWork.Value
);
EarlyExit earlyExit = new(command.EarlyExit.EarlyExitType,
command.EarlyExit.EarlyExitTimeFinesViewModels?.Select(x => new EarlyExitTimeFine(x.Minute, x.FineMoney)).ToList() ?? new List<EarlyExitTimeFine>()
, command.EarlyExit.Value);
var groups = _customizeWorkshopGroupSettingsRepository.GetAllGroupsIncludeEmployeeSettingsByWorkshopSettingsId(entity.id);
using (var transaction = new TransactionScope())
{
try
{
entity.Edit(fridayPay, overTimePay, baseYearsPay, bonusesPay, nightWorkPay, marriedAllowance, shiftPay,
familyAllowance, leavePay, insuranceDeduction,
fineAbsenceDeduction, lateToWork, earlyExit, command.FridayWork, command.HolidayWork,
command.BonusesPaysInEndOfMonth, command.LeavePermittedDays, command.BaseYearsPayInEndOfYear,
command.OverTimeThresholdMinute);
_customizeWorkshopSettingsRepository.SaveChanges();
var editViewModel = new EditCustomizeWorkshopGroupSettings()
{
};
OperationResult result = new OperationResult();
if (entity.WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
//foreach (var group in groups)
//{
// var employeeIds = group.CustomizeWorkshopEmployeeSettingsCollection.Select(x => x.EmployeeId)
// .ToList();
// group.EditAndOverwriteOnEmployees(group.GroupName,group.Salary,employeeIds,group.FridayPay,group.OverTimePay,group.BaseYearsPay,group.BonusesPay,
// group.ShiftPay,group.NightWorkPay,group.MarriedAllowance,group.FamilyAllowance,group.LeavePay,group.InsuranceDeduction,
// group.FineAbsenceDeduction,);
//}
}
else
{
result = result.Succcedded();
}
ChangeAllSettingsGroups(entity, replaceInAllGroups);
result.Succcedded();
transaction.Complete();
}
catch
{
// ignored
}
}
return string.IsNullOrWhiteSpace(op.Message) ? op.Succcedded() : op;
}
//Edit the Group Settings Data
public OperationResult EditRollCallGroupSetting(EditCustomizeWorkshopGroupSettings command)
{
OperationResult op = new();
#region Validation
if (string.IsNullOrWhiteSpace(command.Name))
return op.Failed("لطفا نام گروه را وارد کنید");
if (string.IsNullOrWhiteSpace(command.Salary))
return op.Failed("لطفا حقوق مورد نظر خود را وارد کنید");
if (!_customizeWorkshopGroupSettingsRepository.Exists(x => x.id == command.Id))
return op.Failed("چنین ساعت کاری برای گروه وجود ندارد");
#endregion
var entity = _customizeWorkshopGroupSettingsRepository.GetWithEmployees(command.Id);
FridayPay fridayPay = new(command.FridayPay.FridayPayType, command.FridayPay.Value);
OverTimePay overTimePay = new(command.OverTimePay.OverTimePayType, command.OverTimePay.Value);
BaseYearsPay baseYearsPay = new(command.BaseYearsPay.BaseYearsPayType, command.BaseYearsPay.Value, command.BaseYearsPay.PaymentType);
BonusesPay bonusesPay = new(command.BonusesPay.BonusesPayType, command.BonusesPay.Value, command.BonusesPay.PaymentType);
NightWorkPay nightWorkPay = new(command.NightWorkPay.NightWorkingType, command.NightWorkPay.Value);
MarriedAllowance marriedAllowance = new(command.MarriedAllowance.MarriedAllowanceType, command.MarriedAllowance.Value);
//ShiftPay shiftPay = new(command.ShiftPay.ShiftType, command.ShiftPay.ShiftPayType, command.ShiftPay.Value);
ShiftPay shiftPay = new(ShiftType.None, ShiftPayType.None, 0);
FamilyAllowance familyAllowance = new(command.FamilyAllowance.FamilyAllowanceType, command.FamilyAllowance.Value);
LeavePay leavePay = new(command.LeavePay.LeavePayType, command.LeavePay.Value);
InsuranceDeduction insuranceDeduction = new(command.InsuranceDeduction.InsuranceDeductionType, command.InsuranceDeduction.Value);
FineAbsenceDeduction fineAbsenceDeduction = new(
command.FineAbsenceDeduction.FineAbsenceDeductionType, command.FineAbsenceDeduction.Value,
command.FineAbsenceDeduction.FineAbsenceDayOfWeekViewModels
.Select(x => new FineAbsenceDayOfWeek(x.DayOfWeek)).ToList()
);
LateToWork lateToWork = new(
command.LateToWork.LateToWorkType,
command.LateToWork.LateToWorkTimeFinesVewModels.Select(x => new LateToWorkTimeFine(x.Minute, x.FineMoney))
.ToList(), command.LateToWork.Value
);
EarlyExit earlyExit = new(command.EarlyExit.EarlyExitType,
command.EarlyExit.EarlyExitTimeFinesViewModels.Select(x => new EarlyExitTimeFine(x.Minute, x.FineMoney))
.ToList(), command.EarlyExit.Value);
double salary = command.Salary.MoneyToDouble();
bool isChanged;
if (fridayPay == entity.FridayPay && overTimePay == entity.OverTimePay && baseYearsPay == entity.BaseYearsPay && bonusesPay == entity.BonusesPay
&& shiftPay == entity.ShiftPay && nightWorkPay == entity.NightWorkPay && marriedAllowance == entity.MarriedAllowance
&& familyAllowance == entity.FamilyAllowance && leavePay == entity.LeavePay && insuranceDeduction == entity.InsuranceDeduction
&& fineAbsenceDeduction == entity.FineAbsenceDeduction && lateToWork == entity.LateToWork && earlyExit == entity.EarlyExit && command.FridayWork == entity.FridayWork
&& command.HolidayWork == entity.HolidayWork && command.IrregularShift == entity.IrregularShift && command.WorkshopShiftStatus == entity.WorkshopShiftStatus)
{
isChanged = false;
}
else
{
isChanged = true;
}
entity.EditAndOverwriteOnEmployees(command.Name, salary, command.EmployeeIds, fridayPay, overTimePay,
baseYearsPay, bonusesPay, shiftPay, nightWorkPay, marriedAllowance, familyAllowance,
leavePay, insuranceDeduction, fineAbsenceDeduction, lateToWork, earlyExit,
command.FridayWork, command.HolidayWork, isChanged, command.LeavePermittedDays);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
//Edit the Employee settings and change the 'IsChanged' bool to true.
public OperationResult EditRollCallEmployeeSettings(EditCustomizeEmployeeSettings command)
{
OperationResult op = new();
var entity = _customizeWorkshopEmployeeSettingsRepository.Get(command.Id);
#region Validation
if (entity == null)
return op.Failed("چنین پرسنلی وجود ندارد");
#endregion
FridayPay fridayPay = new(command.FridayPay.FridayPayType, command.FridayPay.Value);
OverTimePay overTimePay = new(command.OverTimePay.OverTimePayType, command.OverTimePay.Value);
BaseYearsPay baseYearsPay = new(command.BaseYearsPay.BaseYearsPayType, command.BaseYearsPay.Value, command.BaseYearsPay.PaymentType);
BonusesPay bonusesPay = new(command.BonusesPay.BonusesPayType, command.BonusesPay.Value, command.BonusesPay.PaymentType);
NightWorkPay nightWorkPay = new(command.NightWorkPay.NightWorkingType, command.NightWorkPay.Value);
MarriedAllowance marriedAllowance = new(command.MarriedAllowance.MarriedAllowanceType, command.MarriedAllowance.Value);
// ShiftPay shiftPay = new(command.ShiftPay.ShiftType, command.ShiftPay.ShiftPayType, command.ShiftPay.Value);
ShiftPay shiftPay = new(ShiftType.None, ShiftPayType.None, 0);
FamilyAllowance familyAllowance = new(command.FamilyAllowance.FamilyAllowanceType, command.FamilyAllowance.Value);
LeavePay leavePay = new(command.LeavePay.LeavePayType, command.LeavePay.Value);
InsuranceDeduction insuranceDeduction = new(command.InsuranceDeduction.InsuranceDeductionType, command.InsuranceDeduction.Value);
FineAbsenceDeduction fineAbsenceDeduction = new(
command.FineAbsenceDeduction.FineAbsenceDeductionType, command.FineAbsenceDeduction.Value,
command.FineAbsenceDeduction.FineAbsenceDayOfWeekViewModels
.Select(x => new FineAbsenceDayOfWeek(x.DayOfWeek)).ToList()
);
LateToWork lateToWork = new(
command.LateToWork.LateToWorkType,
command.LateToWork.LateToWorkTimeFinesVewModels.Select(x => new LateToWorkTimeFine(x.Minute, x.FineMoney))
.ToList(), command.LateToWork.Value
);
EarlyExit earlyExit = new(command.EarlyExit.EarlyExitType,
command.EarlyExit.EarlyExitTimeFinesViewModels.Select(x => new EarlyExitTimeFine(x.Minute, x.FineMoney))
.ToList(), command.EarlyExit.Value);
double salary = command.Salary.MoneyToDouble();
bool isChanged;
if (fridayPay == entity.FridayPay && overTimePay == entity.OverTimePay && baseYearsPay == entity.BaseYearsPay && bonusesPay == entity.BonusesPay
&& shiftPay == entity.ShiftPay && nightWorkPay == entity.NightWorkPay && marriedAllowance == entity.MarriedAllowance
&& familyAllowance == entity.FamilyAllowance && leavePay == entity.LeavePay && insuranceDeduction == entity.InsuranceDeduction
&& fineAbsenceDeduction == entity.FineAbsenceDeduction && lateToWork == entity.LateToWork && earlyExit == entity.EarlyExit && command.FridayWork == entity.FridayWork
&& command.HolidayWork == entity.HolidayWork )
{
isChanged = false;
}
else
{
isChanged = true;
}
//change employee data manually. It changes the 'IsChanged' property to true.
entity.EditEmployees(salary, fridayPay, overTimePay, baseYearsPay,
bonusesPay, nightWorkPay, marriedAllowance, shiftPay, familyAllowance, leavePay,
insuranceDeduction, fineAbsenceDeduction, lateToWork, earlyExit, command.FridayWork, command.HolidayWork, command.IrregularShift, isChanged, command.LeavePermittedDays);
_customizeWorkshopEmployeeSettingsRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult RemoveGroupSettings(long groupSettingsId)
{
OperationResult op = new OperationResult();
var entity = _customizeWorkshopGroupSettingsRepository.GetWithEmployees(groupSettingsId);
if (entity.CustomizeWorkshopEmployeeSettingsCollection.Any())
return op.Failed("نمیتوانید گروهی که پرسنل در آن وجود دارد را حذف کنید");
_customizeWorkshopGroupSettingsRepository.Remove(entity.id);
return op.Succcedded();
}
#endregion
#region Queries
public List<CustomizeWorkshopEmployeeSettingsViewModel> GetChangedEmployeeSettingsByGroupSettingsId(long groupSettingsId)
{
return _customizeWorkshopGroupSettingsRepository.GetShiftChangedEmployeeSettingsByGroupSettingsId(
groupSettingsId);
}
// It will Get the Workshop Settings with its groups and the employees of groups.
public CustomizeWorkshopSettingsViewModel GetWorkshopSettingsByWorkshopId(long workshopId, AuthViewModel auth)
{
#region Validation
if (workshopId is < 1)
return new();
#endregion
var record = _customizeWorkshopSettingsRepository.GetWorkshopSettingsByWorkshopId(workshopId, auth);
return record;
}
public CustomizeWorkshopEmployeeSettingsViewModel GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(long workshopId, long employeeId)
{
var entity =
_customizeWorkshopEmployeeSettingsRepository.GetByEmployeeIdAndWorkshopIdIncludeGroupSettings(workshopId, employeeId);
if (entity == null)
return new();
string employeeFullName = _employeeRepository.Get(entity.EmployeeId).FullName;
return new CustomizeWorkshopEmployeeSettingsViewModel()
{
EmployeeId = entity.EmployeeId,
Id = entity.id,
IsSettingChanged = entity.IsSettingChanged,
IsShiftChanged = entity.IsShiftChanged,
Name = entity.CustomizeWorkshopGroupSettings.GroupName,
EmployeeFullName = employeeFullName,
Salary = entity.Salary,
BreakTime = entity.BreakTime,
WorkshopShiftStatus = entity.WorkshopShiftStatus,
IrregularShift = entity.IrregularShift,
RollCallWorkshopShifts = entity.CustomizeWorkshopEmployeeSettingsShifts.Select(x =>
new CustomizeWorkshopShiftViewModel()
{
EndTime = x.EndTime.ToString("HH:mm"),
Placement = x.Placement,
StartTime = x.StartTime.ToString("HH:mm")
}).ToList(),
FridayWork = entity.FridayWork,
HolidayWork = entity.HolidayWork,
CustomizeRotatingShiftsViewModels = entity.CustomizeRotatingShifts.Select(x=>new CustomizeRotatingShiftsViewModel()
{
StartTime = x.StartTime.ToString("HH:mm"),
EndTime = x.EndTime.ToString("HH:mm")
}).ToList()
};
}
public List<CustomizeWorkshopEmployeeSettingsViewModel> GetEmployeeSettingsByGroupSettingsId(long groupSettingsId)
{
List<CustomizeWorkshopEmployeeSettingsViewModel> result = _customizeWorkshopGroupSettingsRepository.GetEmployeeSettingsByGroupSettingsId(groupSettingsId);
if (result == null)
return new();
return result;
}
public CustomizeWorkshopSettingsViewModel GetWorkshopIncludeGroupsByWorkshopId(long workshopId)
{
return _customizeWorkshopSettingsRepository.GetWorkshopIncludeGroupsByWorkshopId(workshopId);
}
public List<EmployeeViewModel> GetEmployeesWithoutGroup(long rollCallWorkshopSettingId)
{
return _customizeWorkshopGroupSettingsRepository.GetEmployeesWithoutGroup(rollCallWorkshopSettingId);
}
public List<EmployeeViewModel> GetEmployeesWithoutGroupByWorkshopId(long workshopId)
{
return _customizeWorkshopGroupSettingsRepository.GetEmployeesWithoutGroupByWorkshopId(workshopId);
}
public CustomizeWorkshopSettingsViewModel GetWorkshopSettingsByWorkshopIdForAdmin(long workshopId)
{
return _customizeWorkshopSettingsRepository.GetWorkshopSettingsByWorkshopIdForAdmin(workshopId);
}
public EditCustomizeWorkshopSettings GetWorkshopSettingsDetails(long workshopId)
{
return _customizeWorkshopSettingsRepository.GetWorkshopSettingsDetails(workshopId);
}
public EditCustomizeWorkshopGroupSettings GetCustomizeWorkshopGroupSettingsDetails(long groupId)
{
return _customizeWorkshopGroupSettingsRepository.GetCustomizeWorkshopGroupSettingsDetails(groupId);
}
public EditCustomizeWorkshopSettings GetSimpleWorkshopSettings(long workshopId)
{
return _customizeWorkshopSettingsRepository.GetSimpleWorkshopSettings(workshopId);
}
public EditCustomizeEmployeeSettings GetCustomizeEmployeeSettingsDetails(long customizeEmployeeId)
{
return _customizeWorkshopEmployeeSettingsRepository.GetCustomizeEmployeeSettingsDetails(customizeEmployeeId);
}
public List<ChangedGroupedViewModel> GetShiftChangesGroupAndEmployees(long customizeWorkshopSettingsId)
{
return _customizeWorkshopSettingsRepository.GetShiftChangesGroupAndEmployees(customizeWorkshopSettingsId);
}
public List<CustomizeWorkshopEmployeeSettingsViewModel> GetEmployeeSettingsByWorkshopId(long workshopId)
{
return _customizeWorkshopEmployeeSettingsRepository.GetEmployeeSettingsByWorkshopId(workshopId);
}
public CustomizeWorkshopGroupSettingsViewModel GetEmployeesGroupSettingsByEmployeeId(long employeeId, long workshopId)
{
return _customizeWorkshopGroupSettingsRepository.GetEmployeesGroupSettingsByEmployeeId(employeeId, workshopId);
}
public bool HasAnyEmployeeWithoutGroup(long workshopId)
{
return _customizeWorkshopGroupSettingsRepository.HasAnyEmployeeWithoutGroup(workshopId);
}
private OperationResult CreateGeneralGroup(CustomizeWorkshopSettings entity)
{
var op = new OperationResult();
if (_customizeWorkshopGroupSettingsRepository.Exists(x => x.CustomizeWorkshopSettingId == entity.id && x.MainGroup))
{
return op.Succcedded();
}
try
{
var shifts =
entity.CustomizeWorkshopSettingsShifts.Select(x =>
new CustomizeWorkshopGroupSettingsShift(x.StartTime, x.EndTime, x.Placement)).ToList();
var irregularShift = new IrregularShift(new TimeOnly(), new TimeOnly(), WorkshopIrregularShifts.None);
var customizeWorkshopGroupSettings = new CustomizeWorkshopGroupSettings().CreateMainGroup(entity.FridayPay, entity.OverTimePay,
entity.BaseYearsPay, entity.BonusesPay, entity.ShiftPay, entity.NightWorkPay, entity.MarriedAllowance,
entity.FamilyAllowance, entity.LeavePay, entity.InsuranceDeduction, entity.FineAbsenceDeduction,
entity.LateToWork, entity.EarlyExit, shifts, entity.FridayWork, entity.HolidayWork,
irregularShift, [] , entity.WorkshopShiftStatus, entity.id, new BreakTime(false, new TimeOnly()), entity.LeavePermittedDays);
_customizeWorkshopGroupSettingsRepository.Create(customizeWorkshopGroupSettings);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
var employeesHasSettings = _customizeWorkshopEmployeeSettingsRepository
.GetEmployeeSettingNotInMainGroup(entity.WorkshopId).Select(x => x.EmployeeId);
var rollCallEmployeeViewModels = _rollCallEmployeeApplication.GetByWorkshopId(entity.WorkshopId).Where(x => !employeesHasSettings.Contains(x.EmployeeId));
foreach (var rollCallEmployeeViewModel in rollCallEmployeeViewModels)
{
customizeWorkshopGroupSettings.AddEmployeeSettingToGroupWithGroupData(
rollCallEmployeeViewModel.EmployeeId, entity.WorkshopId);
}
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
catch (Exception e)
{
return op.Failed(e.Message);
}
}
private OperationResult ChangeAllGroupsShiftsWithEmployees(CustomizeWorkshopSettings entity, bool replaceChangedGroups)
{
var op = new OperationResult();
var groupSettings = _customizeWorkshopGroupSettingsRepository.GetAllGroupsIncludeEmployeeSettingsByWorkshopSettingsId(entity.id);
if (!replaceChangedGroups)
{
groupSettings = groupSettings.Where(x => !x.IsShiftChange).ToList();
}
var groupShifts = entity.CustomizeWorkshopSettingsShifts
.Select(x => new CustomizeWorkshopGroupSettingsShift(x.StartTime, x.EndTime, x.Placement)).ToList();
var irregularShift = new IrregularShift(new TimeOnly(), new TimeOnly(), WorkshopIrregularShifts.None);
foreach (var customizeWorkshopGroupSettings in groupSettings)
{
customizeWorkshopGroupSettings.EditSimpleAndOverwriteOnAllEmployees(customizeWorkshopGroupSettings.GroupName,
groupShifts, entity.WorkshopShiftStatus, irregularShift, new BreakTime(false, new TimeOnly()),
false, entity.FridayWork, entity.HolidayWork, []);
}
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
private void ChangeAllSettingsGroups(CustomizeWorkshopSettings customizeWorkshopSettings, bool replaceInAllGroups)
{
var op = new OperationResult();
var groupSettings = _customizeWorkshopGroupSettingsRepository.GetAllGroupsIncludeEmployeeSettingsByWorkshopSettingsId(customizeWorkshopSettings.id);
if (!replaceInAllGroups)
{
groupSettings = groupSettings.Where(x => !x.IsSettingChange).ToList();
}
foreach (var groupSetting in groupSettings)
{
groupSetting.EditAndOverwriteOnAllEmployees(groupSetting.GroupName, groupSetting.Salary,
customizeWorkshopSettings.FridayPay,
customizeWorkshopSettings.OverTimePay, customizeWorkshopSettings.BaseYearsPay,
customizeWorkshopSettings.BonusesPay, customizeWorkshopSettings.ShiftPay,
customizeWorkshopSettings.NightWorkPay, customizeWorkshopSettings.MarriedAllowance,
customizeWorkshopSettings.FamilyAllowance, customizeWorkshopSettings.LeavePay,
customizeWorkshopSettings.InsuranceDeduction, customizeWorkshopSettings.FineAbsenceDeduction,
customizeWorkshopSettings.LateToWork, customizeWorkshopSettings.EarlyExit,
customizeWorkshopSettings.FridayWork, customizeWorkshopSettings.HolidayWork, replaceInAllGroups,
customizeWorkshopSettings.LeavePermittedDays);
}
_customizeWorkshopSettingsRepository.SaveChanges();
}
#endregion
}