Files
Backend-Api/CompanyManagment.Application/CustomizeWorkshopSettingsApplication.cs

717 lines
32 KiB
C#

using _0_Framework.Application;
using _0_Framework.Domain.CustomizeCheckoutValueObjects;
using AccountManagement.Application.Contracts.Account;
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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using Version = _0_Framework.Application.Version;
namespace CompanyManagment.Application;
public class CustomizeWorkshopSettingsApplication(ICustomizeWorkshopSettingsRepository customizeWorkshopSettingsRepository,
IAuthHelper authHelper, IPasswordHasher passwordHasher, ICameraAccountApplication cameraAccountApplication,
ICustomizeWorkshopGroupSettingsRepository customizeWorkshopGroupSettingsRepository, IEmployeeRepository employeeRepository,
ICustomizeWorkshopEmployeeSettingsRepository customizeWorkshopEmployeeSettingsRepository)
: 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;
//Create workshop settings
public OperationResult CreateWorkshopSettings(CreateCustomizeWorkshopSettings command)
{
OperationResult op = new();
var workshopId = _passwordHasher.SlugDecrypt(_authHelper.GetWorkshopSlug());
if (command.ShiftsList.Any(x => string.IsNullOrWhiteSpace(x.StartTime) || string.IsNullOrWhiteSpace(x.EndTime)))
return op.Failed("ساعات کاری شروع و پایان نمیتواند خالی باشد");
//تبدیل شیفت های ویو مدل به انتیتی
List<CustomizeWorkshopSettingsShift> shiftCollection = new List<CustomizeWorkshopSettingsShift>();
#region Validation
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
var record = new CustomizeWorkshopSettings(workshopId, shiftCollection, command.LeavePermittedDays);
_customizeWorkshopSettingsRepository.Create(record);
_customizeWorkshopSettingsRepository.SaveChanges();
return op.Succcedded(record.id);
}
//create group settings with workshopSettingsId.
public OperationResult CreateGroupSettingsByRollCallWorkshopSettingId(CreateCustomizeWorkshopGroupSettings command)
{
OperationResult op = new();
#region validation
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("چنین ساعت کاری برای کارگاهی وجود ندارد");
#endregion
CustomizeWorkshopSettings workshopSettings =
_customizeWorkshopSettingsRepository.Get(command.CustomizeWorkshopSettingId);
double salary = command.Salary.MoneyToDouble();
var collection =
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();
var entity = new CustomizeWorkshopGroupSettings(command.Name, salary, command.CustomizeWorkshopSettingId, collection, 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, workshopSettings.FridayWork, workshopSettings.HolidayWork);
_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 CreateRollCallEmployeeSettings(CreateCustomizeEmployeeSettings command)
{
OperationResult op = new();
#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);
var shifts = groupData.CustomizeWorkshopGroupSettingsShifts.Select(x =>
new CustomizeWorkshopEmployeeSettingsShift(x.StartTime, x.EndTime, x.Placement)).ToList();
foreach (var id in command.EmployeeIds)
{
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());
_customizeWorkshopEmployeeSettingsRepository.Create(entity);
}
_customizeWorkshopEmployeeSettingsRepository.SaveChanges();
return op.Succcedded();
}
//Edit the Workshop Settings Data
public OperationResult EditWorkshopSetting(EditCustomizeWorkshopSettings command)
{
OperationResult op = new();
#region Validation
if (!_customizeWorkshopSettingsRepository.Exists(x => x.id == command.Id))
return op.Failed("خطای سیستمی");
#endregion
var entity = _customizeWorkshopSettingsRepository.Get(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(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 shifts =
command.ShiftsList.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 CustomizeWorkshopSettingsShift(start, end, x.Placement);
}).ToList();
entity.Edit(fridayPay, overTimePay, baseYearsPay, bonusesPay, nightWorkPay, marriedAllowance, shiftPay, familyAllowance, leavePay, insuranceDeduction,
fineAbsenceDeduction, lateToWork, earlyExit, shifts, command.FridayWork, command.HolidayWork, command.BonusesPaysInEndOfMonth, command.LeavePermittedDays, command.BaseYearsPayInEndOfYear,command.OverTimeThresholdMinute);
_customizeWorkshopSettingsRepository.SaveChanges();
return op.Succcedded();
}
//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();
var employeesShifts =
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();
entity.EditAndOverwriteOnEmployees(command.Name, salary, command.EmployeeIds, fridayPay, overTimePay,
baseYearsPay, bonusesPay, shiftPay, nightWorkPay, marriedAllowance, familyAllowance,
leavePay, insuranceDeduction, fineAbsenceDeduction, lateToWork, earlyExit, employeesShifts, command.FridayWork, command.HolidayWork);
_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();
var employeesShifts =
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 CustomizeWorkshopEmployeeSettingsShift(start, end, x.Placement);
}).ToList();
//change employee data manually. It changes the 'IsChanged' property to true.
entity.EditEmployeesAndMakeIsChangeTrue(employeesShifts, salary, fridayPay, overTimePay, baseYearsPay,
bonusesPay, nightWorkPay, marriedAllowance, shiftPay, familyAllowance, leavePay,
insuranceDeduction, fineAbsenceDeduction, lateToWork, earlyExit, command.FridayWork, command.HolidayWork);
_customizeWorkshopEmployeeSettingsRepository.SaveChanges();
return op.Succcedded();
}
public List<CustomizeWorkshopEmployeeSettingsViewModel> GetChangedEmployeeSettingsByGroupSettingsId(long groupSettingsId)
{
return _customizeWorkshopGroupSettingsRepository.GetChangedEmployeeSettingsByGroupSettingsId(
groupSettingsId);
}
//Remove the Employee From the Group Settings
public OperationResult RemoveEmployeeFromRollCallWorkshopGroup(long employeeId, long groupId)
{
OperationResult op = new();
var groupEntity = _customizeWorkshopGroupSettingsRepository.GetWithEmployees(groupId);
#region Validation
if (groupEntity == null)
{
return op.Failed("چنین گروهی وجود ندارد");
}
#endregion
groupEntity.RemoveEmployeeFromGroup(employeeId);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult EditWorkshopSettingShifts(List<CustomizeWorkshopShiftViewModel> shiftViewModels, long customizeWorkshopSettingsId)
{
OperationResult op = new OperationResult();
var entity = _customizeWorkshopSettingsRepository.Get(customizeWorkshopSettingsId);
if (entity == null)
return op.Failed("چنین آیتمی وجود ندارد");
//تبدیل شیفت های ویو مدل به انتیتی
List<CustomizeWorkshopSettingsShift> shiftCollection = new List<CustomizeWorkshopSettingsShift>();
#region Validation
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
entity.ChangeWorkshopShifts(shiftCollection);
_customizeWorkshopSettingsRepository.SaveChanges();
return op.Succcedded();
}
// It will Get the Workshop Settings with its groups and the employees of groups.
public CustomizeWorkshopSettingsViewModel GetWorkshopSettingsByWorkshopId(long workshopId)
{
#region Validation
if (workshopId is < 1)
return new();
#endregion
var record = _customizeWorkshopSettingsRepository.GetWorkshopSettingsByWorkshopId(workshopId);
return record;
}
public CustomizeWorkshopEmployeeSettingsViewModel GetEmployeeSettingsByEmployeeIdGroupSettingsId(long workshopId, long employeeId)
{
var entity =
_customizeWorkshopEmployeeSettingsRepository.GetByEmployeeIdGroupSettingsId(workshopId, employeeId);
if (entity == null)
return new();
string employeeFullName = _employeeRepository.Get(entity.EmployeeId).FullName;
return new CustomizeWorkshopEmployeeSettingsViewModel()
{
EmployeeId = entity.EmployeeId,
Id = entity.id,
IsChanged = entity.IsChanged,
Name = entity.CustomizeWorkshopGroupSettings.GroupName,
EmployeeFullName = employeeFullName,
Salary = entity.Salary,
RollCallWorkshopShifts = entity.CustomizeWorkshopEmployeeSettingsShifts.Select(x =>
new CustomizeWorkshopShiftViewModel()
{
EndTime = x.EndTime.ToString("HH:mm"),
Placement = x.Placement,
StartTime = x.StartTime.ToString("HH:mm")
}).ToList()
};
}
public List<CustomizeWorkshopEmployeeSettingsViewModel> GetEmployeeSettingsByGroupSettingsId(long groupSettingsId)
{
List<CustomizeWorkshopEmployeeSettingsViewModel> result = _customizeWorkshopGroupSettingsRepository.GetEmployeeSettingsByGroupSettingsId(groupSettingsId);
if (result == null)
return new();
return result;
}
public List<EmployeeViewModel> GetEmployeesWithoutGroup(long rollCallWorkshopSettingId)
{
return _customizeWorkshopGroupSettingsRepository.GetEmployeesWithoutGroup(rollCallWorkshopSettingId);
}
public CustomizeWorkshopEmployeeSettingsViewModel GetEmployeeSettingsByEmployeeIdWorkshopId(long workshopId, long employeeId)
{
return _customizeWorkshopSettingsRepository.GetEmployeeSettingsByWorkshopIdGroupSettingsId(workshopId, employeeId);
}
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 OperationResult EditSimpleRollCallGroupSetting(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);
var employeeIds = command.ChangeSettingEmployeeIsChange ? entity.CustomizeWorkshopEmployeeSettingsCollection.Select(x => x.id).ToList() : entity.CustomizeWorkshopEmployeeSettingsCollection.Where(x => !x.IsChanged).Select(x => x.id).ToList();
double salary = command.Salary.MoneyToDouble();
var employeesShifts =
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();
entity.EditSimpleAndOverwriteOnEmployee(command.Name, salary, employeeIds, employeesShifts);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
#region Vafa
public OperationResult EditSimpleRollCallEmployeeGroupSetting(EditCustomizeEmployeeSettings command)
{
OperationResult op = new();
var entity = _customizeWorkshopEmployeeSettingsRepository.Get(command.Id);
#region Validation
if (entity == null)
return op.Failed("چنین پرسنلی وجود ندارد");
if (string.IsNullOrWhiteSpace(command.Salary))
return op.Failed("لطفا حقوق مورد نظر خود را وارد کنید");
#endregion
double salary = command.Salary.MoneyToDouble();
var employeesShifts =
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 CustomizeWorkshopEmployeeSettingsShift(start, end, x.Placement);
}).ToList();
entity.SimpleEditEmployeesAndMakeIsChangeTrue(employeesShifts, salary);
_customizeWorkshopGroupSettingsRepository.SaveChanges();
return op.Succcedded();
}
#endregion
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();
}
}