Files
Backend-Api/Company.Domain/CustomizeWorkshopEmployeeSettingsAgg/Entities/CustomizeWorkshopEmployeeSettings.cs

272 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using _0_Framework.Domain;
using _0_Framework.Domain.CustomizeCheckoutShared.Base;
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
using _0_Framework.Domain.CustomizeCheckoutShared.ValueObjects;
using Company.Domain.CustomizeWorkshopGroupSettingsAgg.Entities;
using Company.Domain.EmployeeAgg;
using Company.Domain.WorkshopAgg;
using CompanyManagment.App.Contracts.CustomizeWorkshopSettings.ValueObjectsViewModel;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
namespace Company.Domain.CustomizeWorkshopEmployeeSettingsAgg.Entities;
public class CustomizeWorkshopEmployeeSettings : BaseCustomizeEntity
{
private CustomizeWorkshopEmployeeSettings()
{
}
public CustomizeWorkshopEmployeeSettings(FridayPay fridayPay, OverTimePay overTimePay, BaseYearsPay baseYearsPay,
BonusesPay bonusesPay, NightWorkPay nightWorkPay, MarriedAllowance marriedAllowance, ShiftPay shiftPay,
FamilyAllowance familyAllowance, LeavePay leavePay, InsuranceDeduction insuranceDeduction,
FineAbsenceDeduction fineAbsenceDeduction, LateToWork lateToWork, EarlyExit earlyExit, long employeeId,
long workshopId, double salary, long customizeWorkshopGroupSettingId,
ICollection<CustomizeWorkshopEmployeeSettingsShift> customizeWorkshopEmployeeSettingsShifts,
HolidayWork holidayWork, IrregularShift irregularShift, WorkshopShiftStatus workshopShiftStatus, BreakTime breakTime,
int leavePermittedDays, ICollection<CustomizeRotatingShift> rotatingShifts
, List<WeeklyOffDay> weeklyOffDays) :
base(fridayPay, overTimePay,
baseYearsPay, bonusesPay, nightWorkPay,
marriedAllowance, shiftPay, familyAllowance, leavePay, insuranceDeduction, fineAbsenceDeduction, lateToWork,
earlyExit, holidayWork, breakTime, leavePermittedDays,weeklyOffDays)
{
CustomizeWorkshopGroupSettingId = customizeWorkshopGroupSettingId;
IsSettingChanged = false;
IsShiftChanged = false;
EmployeeId = employeeId;
WorkshopId = workshopId;
Salary = salary;
WorkshopShiftStatus = workshopShiftStatus;
CustomizeWorkshopEmployeeSettingsShifts = workshopShiftStatus == WorkshopShiftStatus.Regular ? EmployeeSettingsShiftsConvertor(customizeWorkshopEmployeeSettingsShifts) : [];
CustomizeRotatingShifts = workshopShiftStatus == WorkshopShiftStatus.Rotating ? rotatingShifts : [];
IrregularShift = workshopShiftStatus == WorkshopShiftStatus.Regular ?
new IrregularShift(new TimeOnly(), new TimeOnly(), WorkshopIrregularShifts.None)
: new IrregularShift(irregularShift.StartTime, irregularShift.EndTime, irregularShift.WorkshopIrregularShifts);
}
public IrregularShift IrregularShift { get; set; }
public WorkshopShiftStatus WorkshopShiftStatus { get; private set; }
public long EmployeeId { get; private set; }
public long WorkshopId { get; private set; }
public double Salary { get; private set; }
public bool IsShiftChanged { get; private set; }
public bool IsSettingChanged { get; private set; }
public long CustomizeWorkshopGroupSettingId { get; set; }
public ICollection<CustomizeRotatingShift> CustomizeRotatingShifts { get; set; }
public ICollection<CustomizeWorkshopEmployeeSettingsShift> CustomizeWorkshopEmployeeSettingsShifts { get; set; }
public CustomizeWorkshopGroupSettings CustomizeWorkshopGroupSettings { get; set; }
/// <summary>
/// تغییر مقادیر تنظیمات پرسنل
/// </summary>
/// <param name="customizeWorkshopEmployeeSettingsShifts">شیفت های کاری پرسنل</param>
/// <param name="salary"> حقوق ماهیانه اختصاص داده شده</param>
/// <param name="fridayPay">جمعه کاری</param>
/// <param name="overTimePay">اضافه کاری</param>
/// <param name="baseYearsPay">سنوات</param>
/// <param name="bonusesPay">عیدی</param>
/// <param name="nightWorkPay">شب کاری</param>
/// <param name="marriedAllowance">حق تاهل</param>
/// <param name="shiftPay">نوبت کاری</param>
/// <param name="familyAllowance">حق اولاد</param>
/// <param name="leavePay">مزد مرخصی</param>
/// <param name="insuranceDeduction">حق بیمه</param>
/// <param name="fineAbsenceDeduction">جریمه غیبت</param>
/// <param name="lateToWork">تاخیر در ورود</param>
/// <param name="earlyExit">تعجیل درخروج</param>
/// <param name="holidayWork">آیا در تعطیلات رسمی موظف به کار است</param>
/// <param name="workshopIrregularShifts">نوع شیفت کاری </param>
/// <param name="workshopShiftStatus">آیا شیفت منظم است یا نا منظم</param>
/// <param name="isSettingChange"></param>
public void EditEmployees(double salary, FridayPay fridayPay, OverTimePay overTimePay, BaseYearsPay baseYearsPay, BonusesPay bonusesPay,
NightWorkPay nightWorkPay, MarriedAllowance marriedAllowance, ShiftPay shiftPay,
FamilyAllowance familyAllowance, LeavePay leavePay, InsuranceDeduction insuranceDeduction,
FineAbsenceDeduction fineAbsenceDeduction, LateToWork lateToWork, EarlyExit earlyExit,
HolidayWork holidayWork, IrregularShift irregularShift, bool isSettingChange, int leavePermittedDays)
{
SetValueObjects(fridayPay, overTimePay, baseYearsPay, bonusesPay
, nightWorkPay, marriedAllowance, shiftPay, familyAllowance, leavePay, insuranceDeduction, fineAbsenceDeduction,
lateToWork, earlyExit);
Salary = salary;
IsSettingChanged = isSettingChange;
HolidayWork = holidayWork;
LeavePermittedDays = leavePermittedDays;
}
public void SimpleEdit(
ICollection<CustomizeWorkshopEmployeeSettingsShift> employeeSettingsShift,
IrregularShift irregularShift,
WorkshopShiftStatus workshopShiftStatus, BreakTime breakTime, bool isShiftChange,HolidayWork holidayWork,
ICollection<CustomizeRotatingShift> rotatingShifts,List<WeeklyOffDay> weeklyOffDays)
{
BreakTime = new BreakTime(breakTime.HasBreakTimeValue, breakTime.BreakTimeValue);
IsShiftChanged = isShiftChange;
WorkshopShiftStatus = workshopShiftStatus;
CustomizeWorkshopEmployeeSettingsShifts = workshopShiftStatus == WorkshopShiftStatus.Regular ? EmployeeSettingsShiftsConvertor(employeeSettingsShift) : [];
IrregularShift = workshopShiftStatus == WorkshopShiftStatus.Regular ?
new IrregularShift(new TimeOnly(), new TimeOnly(), WorkshopIrregularShifts.None)
: new IrregularShift(irregularShift.StartTime, irregularShift.EndTime, irregularShift.WorkshopIrregularShifts);
CustomizeRotatingShifts = workshopShiftStatus == WorkshopShiftStatus.Rotating ? rotatingShifts : [];
HolidayWork = holidayWork;
WeeklyOffDays = weeklyOffDays;
}
private List<CustomizeWorkshopEmployeeSettingsShift> EmployeeSettingsShiftsConvertor(
ICollection<CustomizeWorkshopEmployeeSettingsShift> employeeSettingsShift)
{
var customizeWorkshopSettingShiftsList =
employeeSettingsShift.ToList();
// Handle the special case where there's only one shift
if (customizeWorkshopSettingShiftsList.Count == 1)
{
var singleShift = customizeWorkshopSettingShiftsList.First();
return new List<CustomizeWorkshopEmployeeSettingsShift>
{
new CustomizeWorkshopEmployeeSettingsShift(singleShift.StartTime, singleShift.EndTime,
singleShift.Placement, TimeOnly.MaxValue)
};
}
return customizeWorkshopSettingShiftsList.Select((x, index) =>
{
if (index == 0)
{
return new CustomizeWorkshopEmployeeSettingsShift(x.StartTime, x.EndTime, x.Placement);
}
// Ensure no more than three shifts
if (index >= 3)
{
throw new InvalidDataException("شیفت های پرسنل نمیتواند بیشتر از سه تا باشد");
}
var previousShift = customizeWorkshopSettingShiftsList[index - 1];
var nextStart = new DateTime(DateOnly.MinValue, x.StartTime);
var previousEnd = new DateTime(DateOnly.MinValue, previousShift.EndTime);
if (nextStart < previousEnd)
{
nextStart = nextStart.AddDays(1);
}
var differenceTimeSpan = nextStart - previousEnd;
var oneThirdOfDifference = (differenceTimeSpan / 3) * 2;
var previousTimeThreshold = TimeOnly.FromDateTime(previousEnd.AddTicks(oneThirdOfDifference.Ticks));
return new CustomizeWorkshopEmployeeSettingsShift(x.StartTime, x.EndTime, x.Placement,
previousTimeThreshold);
}).ToList();
}
private void SetValueObjects(FridayPay fridayPay, OverTimePay overTimePay, BaseYearsPay baseYearsPay,
BonusesPay bonusesPay
, NightWorkPay nightWorkPay, MarriedAllowance marriedAllowance, ShiftPay shiftPay,
FamilyAllowance familyAllowance, LeavePay leavePay, InsuranceDeduction insuranceDeduction,
FineAbsenceDeduction fineAbsenceDeduction,
LateToWork lateToWork, EarlyExit earlyExit)
{
FridayPay = new(fridayPay.FridayPayType, fridayPay.Value);
OverTimePay = new(overTimePay.OverTimePayType, overTimePay.Value);
BaseYearsPay = new(baseYearsPay.BaseYearsPayType, baseYearsPay.Value, baseYearsPay.PaymentType);
BonusesPay = new(bonusesPay.BonusesPayType, bonusesPay.Value, bonusesPay.PaymentType);
NightWorkPay = new(nightWorkPay.NightWorkingType, nightWorkPay.Value);
MarriedAllowance = new(marriedAllowance.MarriedAllowanceType, marriedAllowance.Value);
ShiftPay = new(shiftPay.ShiftType, shiftPay.ShiftPayType, shiftPay.Value);
FamilyAllowance = new(familyAllowance.FamilyAllowanceType, familyAllowance.Value);
LeavePay = new(leavePay.LeavePayType, leavePay.Value);
InsuranceDeduction = new(insuranceDeduction.InsuranceDeductionType, insuranceDeduction.Value);
FineAbsenceDeduction = new(
fineAbsenceDeduction.FineAbsenceDeductionType, fineAbsenceDeduction.Value,
fineAbsenceDeduction.FineAbsenceDayOfWeekCollection
.Select(x => new FineAbsenceDayOfWeek(x.DayOfWeek)).ToList()
);
LateToWork = new(
lateToWork.LateToWorkType,
lateToWork.LateToWorkTimeFines.Select(x => new LateToWorkTimeFine(x.Minute, x.FineMoney))
.ToList(), lateToWork.Value
);
EarlyExit = new(earlyExit.EarlyExitType,
earlyExit.EarlyExitTimeFines.Select(x => new EarlyExitTimeFine(x.Minute, x.FineMoney))
.ToList(), earlyExit.Value);
}
public void UpdateIsShiftChange()
{
var groupSetting = CustomizeWorkshopGroupSettings;
if (groupSetting == null)
return;
bool isShiftChange;
if (WorkshopShiftStatus == WorkshopShiftStatus.Regular)
{
if (CustomizeWorkshopEmployeeSettingsShifts.All(x => groupSetting.CustomizeWorkshopGroupSettingsShifts.Any(y => x.Equals(y)))
&& WorkshopShiftStatus == groupSetting.WorkshopShiftStatus && FridayWork == groupSetting.FridayWork &&
HolidayWork == groupSetting.HolidayWork && BreakTime == groupSetting.BreakTime)
{
isShiftChange = false;
}
else
{
isShiftChange = true;
}
}
else if (WorkshopShiftStatus == WorkshopShiftStatus.Irregular)
{
if (WorkshopShiftStatus == groupSetting.WorkshopShiftStatus && BreakTime == groupSetting.BreakTime &&
IrregularShift == groupSetting.IrregularShift && FridayWork == groupSetting.FridayWork &&
HolidayWork == groupSetting.HolidayWork)
{
isShiftChange = false;
}
else
{
isShiftChange = true;
}
}
else
{
if (CustomizeRotatingShifts.All(x => groupSetting.CustomizeRotatingShifts.Any(y => x.Equals(y)))
&& WorkshopShiftStatus == groupSetting.WorkshopShiftStatus && FridayWork == groupSetting.FridayWork &&
HolidayWork == groupSetting.HolidayWork && BreakTime == groupSetting.BreakTime)
{
isShiftChange = false;
}
else
{
isShiftChange = true;
}
}
IsShiftChanged = isShiftChange;
}
}