Files
Backend-Api/CompanyManagment.EFCore/Repository/RollCallMandatoryRepository.cs
2024-08-12 21:30:25 +03:30

494 lines
18 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using _0_Framework.Application;
using _0_Framework.InfraStructure;
using Company.Domain.LeaveAgg;
using Company.Domain.LeftWorkAgg;
using Company.Domain.RollCallAgg;
using Company.Domain.YearlySalaryAgg;
using CompanyManagment.App.Contracts.Contract;
using CompanyManagment.App.Contracts.Leave;
using CompanyManagment.App.Contracts.LeftWork;
using CompanyManagment.App.Contracts.RollCall;
using CompanyManagment.App.Contracts.WorkingHoursTemp;
namespace CompanyManagment.EFCore.Repository;
public class RollCallMandatoryRepository : RepositoryBase<long, RollCall>, IRollCallMandatoryRepository
{
private readonly CompanyContext _context;
private readonly IYearlySalaryRepository _yearlySalaryRepository;
private readonly ILeftWorkRepository _leftWorkRepository;
private readonly ILeaveRepository _leaveRepository;
public RollCallMandatoryRepository(CompanyContext context, IYearlySalaryRepository yearlySalaryRepository,
ILeftWorkRepository leftWorkRepository, ILeaveRepository leaveRepository) : base(context)
{
_context = context;
_yearlySalaryRepository = yearlySalaryRepository;
_leftWorkRepository = leftWorkRepository;
_leaveRepository = leaveRepository;
}
public ComputingViewModel MandatoryCompute(long employeeId, long workshopId, DateTime contractStart,
DateTime contractEnd,
CreateWorkingHoursTemp command, long leavId)
{
#region Entities
string SumWorkeTime = string.Empty;
var weeklyTime = new TimeSpan();
string shift1Hourse = "0";
string shift1Minuts = "0";
string overMandatoryHours = "0";
string overMandatoryMinuts = "0";
#endregion
var rollCallResult = _context.RollCalls.Where(x =>
x.EmployeeId == employeeId && x.WorkshopId == workshopId && x.StartDate.Value.Date >= contractStart.Date &&
x.StartDate.Value.Date <= contractEnd.Date && x.EndDate != null).Select(x => new RollCallViewModel()
{
StartDate = x.StartDate,
EndDate = x.EndDate,
ShiftSpan = (x.EndDate.Value - x.StartDate.Value),
CreationDate = x.CreationDate,
}).ToList();
var groupedRollCall = rollCallResult.GroupBy(x=>x.CreationDate).Select(x => new GroupedRollCalls()
{
CreationDate = x.Key,
SumOneDaySpan = new TimeSpan(x.Sum(shift => shift.ShiftSpan.Ticks)),
}).ToList();
//*****کسر ساعاعت استراحت پرسنل از ساعت کار
var rollCallSubtractSpan = groupedRollCall.Select(x => new GroupedRollCalls()
{
CreationDate = x.CreationDate,
AfterSubtractRestSpan = AfterSubtract(command,x.SumOneDaySpan,x.CreationDate),
}).ToList();
TimeSpan sumSpans = new TimeSpan(rollCallSubtractSpan.Sum(x => x.AfterSubtractRestSpan.Ticks));
//****افزودن مرخصی پرسنل به مجموع ساعات کار***
#region AddEmployeeLeavs
LeaveSearchModel leaveSearch = new LeaveSearchModel()
{
EmployeeId = employeeId,
WorkshopId = workshopId,
LeaveType = "استحقاقی",
PaidLeaveType = "روزانه",
StartLeaveGr = contractStart,
EndLeaveGr = contractEnd,
IsAccepted = true,
};
var leaveSearchResult = _leaveRepository.search(leaveSearch);
if (leaveSearchResult.Count > 0)
{
int leavingDayCout = 0;
//مرخصی های مابین
List<LeaveViewModel> beatweenCheckout = leaveSearchResult.Where(x => x.StartLeaveGr >= contractStart && x.EndLeaveGr <= contractEnd).Select(x => new LeaveViewModel()
{
DayCounter = Convert.ToInt32(x.LeaveHourses),
}).ToList();
leavingDayCout += beatweenCheckout.Sum(x => x.DayCounter);
// مرخصی که شروعش قبل از شروع تصفیه حساب است
List<LeaveViewModel> beforeCheckout = leaveSearchResult.Where(x => x.StartLeaveGr < contractStart).Select(x => new LeaveViewModel()
{
DayCounter = (int)(contractStart - x.EndLeaveGr).TotalDays + 1,
}).ToList();
leavingDayCout+= beforeCheckout.Sum(x => x.DayCounter);
// مرخصی که پایانش بعد از پایان تصفیه حساب است
List<LeaveViewModel> afterCheckout = leaveSearchResult.Where(x => x.EndLeaveGr > contractEnd).Select(x => new LeaveViewModel()
{
DayCounter = (int)(x.StartLeaveGr - contractEnd).TotalDays + 1,
}).ToList();
leavingDayCout += afterCheckout.Sum(x => x.DayCounter);
Console.WriteLine(leavingDayCout);
TimeSpan workingPerDayAve = sumSpans / rollCallSubtractSpan.Count;//میانگین ساعت کار در روز
TimeSpan sumLeave = new TimeSpan();
if (workingPerDayAve <= new TimeSpan(7, 20, 0))
{
sumLeave = leavingDayCout * workingPerDayAve;
}
else
{
sumLeave = leavingDayCout * new TimeSpan(7, 20, 0);
}
sumSpans = sumSpans.Add(sumLeave);
}
Console.WriteLine(sumSpans);
#endregion
//***********************************//
//ToTalHourse Employe eWorked
var totalHourses = (sumSpans.TotalMinutes) / 60;
int totalHolidaysAndNotH = (int)sumSpans.TotalHours;
int totalHolidaysAndNotM = (int)(sumSpans.TotalMinutes % 60);
//***********************************//
#region ComputeMandatoryAtThisTime
int TotalContractDays = (int)(contractEnd - contractStart).TotalDays + 1;
int fridays = 0;
int holiday = _context.HolidayItems.Count(x => x.Holidaydate >= contractStart && x.Holidaydate <= contractEnd);
;
for (var gDate = contractStart; gDate <= contractEnd; gDate = gDate.AddDays(1))
{
if (gDate.DayOfWeek == DayOfWeek.Friday)
{
fridays += 1;
}
}
int TotalDaysNoFriday = TotalContractDays - fridays;
int mandatorDays = TotalContractDays - (fridays + holiday);
//***********************************//
//This Time Mandatory Hourse
double mandatoryHours = Math.Round((mandatorDays * 7.33), 2);
//***********************************//
var dailyFix = TimeSpan.Parse("07:20");
dailyFix = dailyFix.Multiply(mandatorDays);
TimeSpan Mandatory = sumSpans.Subtract(dailyFix);
#endregion
#region DailyFeeCompute
var searchModel = new LeftWorkSearchModel()
{
EmployeeId = command.EmployeeId,
WorkshopId = command.WorkshopId,
};
var leftworkList = _leftWorkRepository.search(searchModel);
var basic = "0";
double dayliFeeComplete = 0;
var GetWorkStartDate = command.GetWorkDateHide.ToEnglishNumber();
var styear = Convert.ToInt32(GetWorkStartDate.Substring(0, 4));
var startDate = command.GetWorkDateHide.ToGeorgianDateTime();
var dayliFee = "خطای تاریخ";
double dayliFeeDouble = 0;
if (styear >= 1370)
{
if (leftworkList == null)
leftworkList = new List<LeftWorkViewModel>();
var dayliFeeResult = _yearlySalaryRepository.DayliFeeComputing(startDate, contractStart, contractEnd,
command.EmployeeId, command.WorkshopId, leftworkList);
dayliFee = dayliFeeResult.DayliFee;
dayliFeeDouble = dayliFeeResult.DayliFeeDouble;
dayliFeeComplete = dayliFeeResult.DayliFee.MoneyToDouble();
basic = dayliFeeResult.Basic;
}
#endregion
#region ConsumableItemsAndHousingAndFamily
var ConsumableItems = _yearlySalaryRepository.ConsumableItems(contractEnd);
var HousingAllowance = _yearlySalaryRepository.HousingAllowance(contractEnd);
var familyAllowance = _yearlySalaryRepository.FamilyAllowance(command.EmployeeId, contractEnd);
var MarriedAllowance = _yearlySalaryRepository.MarriedAllowance(contractEnd, command.EmployeeId);
// حق تاهل
string MarriedAllowanceStr = MarriedAllowance > 0 ? MarriedAllowance.ToMoney() : "0";
#endregion
var totalWeek = (int)(TotalContractDays / 6);
#region Fix44Compute
if (totalHourses < mandatoryHours)
{
if (command.ShiftWork == "1" || command.ShiftWork == "2" || command.ShiftWork == "4")
{
var workedHoursePerDay = totalHourses / mandatorDays;
var result = (dayliFeeDouble / 7.33) * workedHoursePerDay;
dayliFee = result.ToMoney();
var HousingAllowonceNumberType = HousingAllowance.MoneyToDouble();
var HousingStep1 = HousingAllowonceNumberType / 30;
var HousingStep2 = HousingStep1 / 7.33;
var HousingStep3 = HousingStep2 * workedHoursePerDay;
var HousingStep4 = HousingStep3 * TotalContractDays;
HousingAllowance = HousingStep4.ToMoney();
var ConsumableItemsNumberType = ConsumableItems.MoneyToDouble();
var consumableItemsStep1 = ConsumableItemsNumberType / 30;
var consumableItemsStep2 = consumableItemsStep1 / 7.33;
var consumableItemsStep3 = consumableItemsStep2 * workedHoursePerDay;
var consumableItemsStep4 = consumableItemsStep3 * TotalContractDays;
ConsumableItems = consumableItemsStep4.ToMoney();
//حق تاهل
if (MarriedAllowance > 0)
{
var MarriedStep1 = MarriedAllowance / 30;
var MarriedStep2 = MarriedStep1 / 7.33;
var MarriedStep3 = MarriedStep2 * workedHoursePerDay;
var MarriedStep4 = MarriedStep3 * TotalContractDays;
MarriedAllowanceStr = MarriedStep4.ToMoney();
}
if (familyAllowance != "0")
{
var familyAllowanceNumberType = familyAllowance.MoneyToDouble();
var familyAllowanceStep1 = familyAllowanceNumberType / 30;
var familyAllowanceStep2 = familyAllowanceStep1 / 7.33;
var familyAllowanceStep3 = familyAllowanceStep2 * workedHoursePerDay;
var familyAllowanceStep4 = familyAllowanceStep3 * TotalContractDays;
familyAllowance = familyAllowanceStep4.ToMoney();
}
if (totalWeek > 1)
{
double weekAvrage = 0;
if (totalHourses < 44.00)
{
weekAvrage = (totalHourses * 6) / TotalContractDays;
}
else
{
weekAvrage = (totalHourses * 6) / TotalDaysNoFriday;
}
//var oneday = weekAvrage * 6;
var totalShiftRound = Math.Round(weekAvrage, 2);
SumWorkeTime = $"{totalShiftRound}";
}
else if (totalWeek <= 1 && TotalDaysNoFriday <= 6)
{
var totalShiftRound = Math.Round(totalHourses, 2);
SumWorkeTime = $"{totalShiftRound}";
}
else if (totalWeek <= 1 && TotalDaysNoFriday > 6)
{
var perDyeWorked = totalHourses / TotalDaysNoFriday;
var weekAvrage = perDyeWorked * 6;
var totalShiftRound = Math.Round(weekAvrage, 2);
SumWorkeTime = $"{totalShiftRound}";
}
weeklyTime = sumSpans;
}
}
else // اگر بیشتر از 44 بود
{
var HousingAllowonceNumberType = HousingAllowance.MoneyToDouble();
var ConsumableItemsNumberType = ConsumableItems.MoneyToDouble();
var familyAllowanceNumberType = familyAllowance.MoneyToDouble();
var HousingStep1 = HousingAllowonceNumberType / 30;
var HousingStep4 = HousingStep1 * TotalContractDays;
HousingAllowance = HousingStep4.ToMoney();
var consumableItemsStep1 = ConsumableItemsNumberType / 30;
var consumableItemsStep4 = consumableItemsStep1 * TotalContractDays;
ConsumableItems = consumableItemsStep4.ToMoney();
//حق تاهل
if (MarriedAllowance > 0)
{
var MarriedStep1 = MarriedAllowance / 30;
var MarriedStep4 = MarriedStep1 * TotalContractDays;
MarriedAllowanceStr = MarriedStep4.ToMoney();
}
if (familyAllowance != "0")
{
var familyAllowanceStep1 = familyAllowanceNumberType / 30;
var familyAllowanceStep4 = familyAllowanceStep1 * TotalContractDays;
familyAllowance = familyAllowanceStep4.ToMoney();
}
SumWorkeTime = $"{44}";
//اضافه کار
if (totalHourses > mandatoryHours)
{
var mandatoryH = (int)Mandatory.TotalHours;
int mandatoryM = (int)(Mandatory.Minutes % 60);
overMandatoryHours = mandatoryH.ToString();
overMandatoryMinuts = mandatoryM.ToString();
}
}
#endregion
#region Result
var res = new ComputingViewModel()
{
NumberOfWorkingDays = "0",
NumberOfFriday = "0",
TotalHoursesH = totalHolidaysAndNotH.ToString(),
TotalHoursesM = totalHolidaysAndNotM.ToString(),
OverTimeWorkH = overMandatoryHours,
OverTimeWorkM = overMandatoryMinuts,
//OverNightWorkH = shiftOver22Hours,
//OverNightWorkM = shiftOver22Minuts,
ComplexNumberOfWorkingDays = "0",
SalaryCompute = dayliFee,
SumTime44 = SumWorkeTime,
ConsumableItems = ConsumableItems,
HousingAllowance = HousingAllowance,
FamilyAllowance = familyAllowance,
OfficialHoliday = holiday,
weeklyTime = weeklyTime,
//RotatingResultList = rotatingResultList,
//RotatingStatus = rotatingFaResult,
//ShiftPay = ShiftPayResult,
Basic = basic,
FridayStartToEnd = fridays,
TotalHolidayAndNotH = totalHolidaysAndNotH.ToString(),
TotalHolidayAndNotM = totalHolidaysAndNotM.ToString(),
DayliFeeComplete = dayliFeeComplete,
MarriedAllowance = MarriedAllowanceStr,
};
#endregion
return new ComputingViewModel();
}
public TimeSpan AfterSubtract(CreateWorkingHoursTemp command,TimeSpan sumOneDaySpan,DateTime creationDate)
{
#region RestTimes
var rest0 = new TimeSpan();
var rest1 = new TimeSpan();
var rest2 = new TimeSpan();
var rest3 = new TimeSpan();
var rest4 = new TimeSpan();
var rest5 = new TimeSpan();
var rest6 = new TimeSpan();
switch (command.ShiftWork)
{
case "1":
case "2":
command.RestTime = command.RestTime == "0" ? "00" : command.RestTime;
command.RestTimeYekshanbeh = command.RestTimeYekshanbeh == "0" ? "00" : command.RestTimeYekshanbeh;
command.RestTimeDoshanbeh = command.RestTimeDoshanbeh == "0" ? "00" : command.RestTimeDoshanbeh;
command.RestTimeSeshanbeh = command.RestTimeSeshanbeh == "0" ? "00" : command.RestTimeSeshanbeh;
command.RestTimeCheharshanbeh =
command.RestTimeCheharshanbeh == "0" ? "00" : command.RestTimeCheharshanbeh;
command.RestTimePanjshanbeh = command.RestTimePanjshanbeh == "0" ? "00" : command.RestTimePanjshanbeh;
command.RestTimeJomeh = command.RestTimeJomeh == "0" ? "00" : command.RestTimeJomeh;
command.RestTimeMin = command.RestTimeMin == "0" ? "00" : command.RestTimeMin;
command.RestTimeYekshanbehMin =
command.RestTimeYekshanbehMin == "0" ? "00" : command.RestTimeYekshanbehMin;
command.RestTimeDoshanbehMin =
command.RestTimeDoshanbehMin == "0" ? "00" : command.RestTimeDoshanbehMin;
command.RestTimeSeshanbehMin =
command.RestTimeSeshanbehMin == "0" ? "00" : command.RestTimeSeshanbehMin;
command.RestTimeCheharshanbehMin =
command.RestTimeCheharshanbehMin == "0" ? "00" : command.RestTimeCheharshanbehMin;
command.RestTimePanjshanbehMin =
command.RestTimePanjshanbehMin == "0" ? "00" : command.RestTimePanjshanbehMin;
command.RestTimeJomehMin = command.RestTimeJomehMin == "0" ? "00" : command.RestTimeJomehMin;
rest0 = TimeSpan.Parse($"{command.RestTime}:{command.RestTimeMin}");
rest1 = TimeSpan.Parse($"{command.RestTimeYekshanbeh}:{command.RestTimeYekshanbehMin}");
rest2 = TimeSpan.Parse($"{command.RestTimeDoshanbeh}:{command.RestTimeDoshanbehMin}");
rest3 = TimeSpan.Parse($"{command.RestTimeSeshanbeh}:{command.RestTimeSeshanbehMin}");
rest4 = TimeSpan.Parse($"{command.RestTimeCheharshanbeh}:{command.RestTimeCheharshanbehMin}");
rest5 = TimeSpan.Parse($"{command.RestTimePanjshanbeh}:{command.RestTimePanjshanbehMin}");
rest6 = TimeSpan.Parse($"{command.RestTimeJomeh}:{command.RestTimeJomehMin}");
break;
case "4":
command.RestTimeShanbe1 = command.RestTimeShanbe1 == "0" ? "00" : command.RestTimeShanbe1;
command.RestTimeShanbe1Min = command.RestTimeShanbe1Min == "0" ? "00" : command.RestTimeShanbe1Min;
command.RestTimeYekShanbe1 = command.RestTimeYekShanbe1 == "0" ? "00" : command.RestTimeYekShanbe1;
command.RestTimeYekShanbe1Min =
command.RestTimeYekShanbe1Min == "0" ? "00" : command.RestTimeYekShanbe1Min;
command.RestTimeDoShanbe1 = command.RestTimeDoShanbe1 == "0" ? "00" : command.RestTimeDoShanbe1;
command.RestTimeDoShanbe1Min =
command.RestTimeDoShanbe1Min == "0" ? "00" : command.RestTimeDoShanbe1Min;
command.RestTimeSeShanbe1 = command.RestTimeSeShanbe1 == "0" ? "00" : command.RestTimeSeShanbe1;
command.RestTimeSeShanbe1Min =
command.RestTimeSeShanbe1Min == "0" ? "00" : command.RestTimeSeShanbe1Min;
command.RestTimeCheharShanbe1 =
command.RestTimeCheharShanbe1 == "0" ? "00" : command.RestTimeCheharShanbe1;
command.RestTimeCheharShanbe1Min =
command.RestTimeCheharShanbe1Min == "0" ? "00" : command.RestTimeCheharShanbe1Min;
command.RestTimePanjShanbe1 = command.RestTimePanjShanbe1 == "0" ? "00" : command.RestTimePanjShanbe1;
command.RestTimePanjShanbe1Min =
command.RestTimePanjShanbe1Min == "0" ? "00" : command.RestTimePanjShanbe1Min;
command.RestTimeJome1 = command.RestTimeJome1 == "0" ? "00" : command.RestTimeJome1;
command.RestTimeJome1Min = command.RestTimeJome1Min == "0" ? "00" : command.RestTimeJome1Min;
// sumrest week1
rest0 = TimeSpan.Parse($"{command.RestTimeShanbe1}:{command.RestTimeShanbe1Min}");
rest1 = TimeSpan.Parse($"{command.RestTimeYekShanbe1}:{command.RestTimeYekShanbe1Min}");
rest2 = TimeSpan.Parse($"{command.RestTimeDoShanbe1}:{command.RestTimeDoShanbe1Min}");
rest3 = TimeSpan.Parse($"{command.RestTimeSeShanbe1}:{command.RestTimeSeShanbe1Min}");
rest4 = TimeSpan.Parse($"{command.RestTimeCheharShanbe1}:{command.RestTimeCheharShanbe1Min}");
rest5 = TimeSpan.Parse($"{command.RestTimePanjShanbe1}:{command.RestTimePanjShanbe1Min}");
rest6 = TimeSpan.Parse($"{command.RestTimeJome1}:{command.RestTimeJome1Min}");
break;
}
//week1
#endregion
var result = new TimeSpan();
switch (creationDate.DayOfWeek)
{
case DayOfWeek.Saturday:
if (sumOneDaySpan >= rest0)
result = sumOneDaySpan.Subtract(rest0);
break;
case DayOfWeek.Sunday:
if (sumOneDaySpan >= rest1)
result = sumOneDaySpan.Subtract(rest1);
break;
case DayOfWeek.Monday:
if (sumOneDaySpan >= rest2)
result = sumOneDaySpan.Subtract(rest2);
break;
case DayOfWeek.Tuesday:
if (sumOneDaySpan >= rest3)
result = sumOneDaySpan.Subtract(rest3);
break;
case DayOfWeek.Wednesday:
if (sumOneDaySpan >= rest4)
result = sumOneDaySpan.Subtract(rest4);
break;
case DayOfWeek.Thursday:
if (sumOneDaySpan >= rest5)
result = sumOneDaySpan.Subtract(rest5);
break;
case DayOfWeek.Friday:
if (sumOneDaySpan >= rest6)
result = sumOneDaySpan.Subtract(rest6);
break;
}
return result;
}
}