Files
Backend-Api/ServiceHost/Areas/Client/Pages/Company/RollCall/CaseHistory.cshtml.cs

460 lines
19 KiB
C#

using _0_Framework.Application;
using _0_Framework.Domain.CustomizeCheckoutShared.Enums;
using CompanyManagment.App.Contracts.CustomizeWorkshopSettings;
using CompanyManagment.App.Contracts.Leave;
using CompanyManagment.App.Contracts.RollCall;
using CompanyManagment.App.Contracts.RollCallEmployee;
using CompanyManagment.App.Contracts.Workshop;
using CompanyManagment.App.Contracts.YearlySalary;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Globalization;
using System.Security.Claims;
using System.Xml.Linq;
using _0_Framework.Infrastructure;
using Microsoft.AspNetCore.Http.HttpResults;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Company.Domain.empolyerAgg;
using CompanyManagement.Infrastructure.Excel.RollCall;
namespace ServiceHost.Areas.Client.Pages.Company.RollCall
{
[Authorize]
[NeedsPermission(SubAccountPermissionHelper.ListRollCallHistoryPermissionCode)]
public class CaseHistoryModel : PageModel
{
private readonly IAuthHelper _authHelper;
private readonly IPasswordHasher _passwordHasher;
private readonly IWorkshopApplication _workshopApplication;
private readonly IRollCallEmployeeApplication _rollCallEmployeeApplication;
private readonly IRollCallApplication _rollCallApplication;
private readonly IYearlySalaryApplication _yearlySalaryApplication;
private readonly ILeaveApplication _leaveApplication;
private readonly ICustomizeWorkshopSettingsApplication _customizeWorkshopSettingsApplication;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly long _workshopId;
public RollCallsByDateViewModel RollCallViewModels { get; set; }
public EmployeeRollCallsByMonthViewModel RollCallEmployeeViewModels { get; set; }
public string WorkshopFullName { get; set; }
public List<RollCallEmployeeViewModel> RollCallEmployeeList;
public RollCallSearchModel SearchModel { get; set; } = new();
public List<string> YearlyList { get; set; }
public CaseHistoryModel(IAuthHelper authHelper, IPasswordHasher passwordHasher, IRollCallEmployeeApplication rollCallEmployeeApplication, IWorkshopApplication workshopApplication, IRollCallApplication rollCallApplication, IYearlySalaryApplication yearlySalaryApplication, ILeaveApplication leaveApplication, ICustomizeWorkshopSettingsApplication customizeWorkshopSettingsApplication, IHttpContextAccessor httpContextAccessor)
{
_authHelper = authHelper;
_passwordHasher = passwordHasher;
_rollCallEmployeeApplication = rollCallEmployeeApplication;
_workshopApplication = workshopApplication;
_rollCallApplication = rollCallApplication;
_yearlySalaryApplication = yearlySalaryApplication;
_leaveApplication = leaveApplication;
_customizeWorkshopSettingsApplication = customizeWorkshopSettingsApplication;
_httpContextAccessor = httpContextAccessor;
var workshopHash = _httpContextAccessor.HttpContext?.User.FindFirstValue("WorkshopSlug");
_workshopId = _passwordHasher.SlugDecrypt(workshopHash);
if (_workshopId < 1)
throw new InvalidDataException("اختلال در کارگاه");
}
public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
//if (context.HttpContext.Request.Query["handler"].ToString().ToLower().Trim() == "edit")
//{
//}
//else
if (IrregularWorkshopHaveGroupedAllPersonnelValidation(_workshopId) == false)
context.HttpContext.Response.Redirect("./grouping");
base.OnPageHandlerExecuting(context);
}
public bool IrregularWorkshopHaveGroupedAllPersonnelValidation(long workshopId)
{
//var isWorkshopIrregular = _customizeWorkshopSettingsApplication
// .GetWorkshopSettingsDetails(workshopId).WorkshopShiftStatus == WorkshopShiftStatus.Irregular;
//if (isWorkshopIrregular == false)
// return true;
var employeesWithoutGroup = _customizeWorkshopSettingsApplication.HasAnyEmployeeWithoutGroup(workshopId);
if (employeesWithoutGroup)
return false;
return true;
}
public IActionResult OnGet()
{
if (_rollCallEmployeeApplication.GetByWorkshopId(_workshopId).Any())
{
RollCallEmployeeList = _rollCallEmployeeApplication.GetEmployeeRollCalls(_workshopId);
WorkshopFullName = _workshopApplication.GetDetails(_workshopId).WorkshopFullName;
var searchModel = new RollCallSearchModel()
{
WorkshopId = _workshopId
};
RollCallViewModels = _rollCallApplication.GetWorkshopRollCallHistory(searchModel);
YearlyList = _yearlySalaryApplication.GetYears();
return Page();
}
else
{
return Redirect("/Client/Company/RollCall");
}
}
public IActionResult OnGetCaseHistoryRollCallAjax(int dateIndex, string exactDateFa, string startDate, string endDate, long employeeId, string dateEmployeeIndex)
{
if (employeeId > 0)
{
RollCallEmployeeViewModels = _rollCallApplication.GetEmployeeRollCallsHistory(employeeId, _workshopId, startDate, endDate, exactDateFa, dateEmployeeIndex);
if (RollCallEmployeeViewModels == null || RollCallEmployeeViewModels.DateGr == new DateTime())
return new JsonResult(new
{
IsSuccedded = false,
message = "داده ای یافت نشد!",
});
}
else
{
var searchModel = new RollCallSearchModel()
{
WorkshopId = _workshopId,
DateIndex = dateIndex,
ExactDateFa = exactDateFa,
StarDateFa = startDate,
EndDateFa = endDate,
EmployeeId = employeeId
};
RollCallViewModels = _rollCallApplication.GetWorkshopRollCallHistory(searchModel);
}
return new JsonResult(new
{
IsSuccedded = true,
data = RollCallViewModels,
dataEmployee = RollCallEmployeeViewModels,
});
}
public IActionResult OnGetEmployeeRollCallTimeSpan(long employeeId, string start, string end)
{
var resultData = _rollCallApplication.GetEmployeeRollCallTimeSpanForDuration(employeeId, _workshopId, start, end);
var hours = (int)resultData.TotalHours;
var minutes = resultData.Minutes;
var timeString = Tools.ToFarsiHoursAndMinutes(hours, minutes);
return new JsonResult(new
{
success = true,
data = timeString
});
}
public IActionResult OnGetEmployeeList()
{
var resultData = _rollCallEmployeeApplication.GetRollCallEmployeesByWorkshopId(_workshopId);
return new JsonResult(new
{
success = true,
data = resultData
});
}
public IActionResult OnGetCreate()
{
var command = new CreateOrEditEmployeeRollCall();
return Partial("ModalAddRollCall", command);
}
public IActionResult OnGetCheckEmployeeData(long employeeId, string date)
{
bool isSuccess = false;
//var dates = _rollCallApplication.GetEditableDatesForManualEdit(date.ToGeorgianDateTime());
List<RollCallViewModel> resultData = new List<RollCallViewModel>();
if (!string.IsNullOrWhiteSpace(date))
{
resultData = _rollCallApplication.GetWorkshopEmployeeRollCallsForDate(_workshopId, employeeId, date);
isSuccess = resultData.Any();
}
var result = _leaveApplication.HasDailyLeave(_workshopId, employeeId, date);
var isLeave = result.IsSuccedded ? result.Data : false;
var isNullEndDate = resultData.Any(x => x.EndDate == null);
return new JsonResult(new
{
success = isSuccess,
data = resultData,
hasLeave = isLeave,
//EditableDates = dates,
hasNullEndDate = isNullEndDate
});
}
public IActionResult OnGetEdit(long employeeId, string date)
{
var result = _rollCallApplication.GetWorkshopEmployeeRollCallsForDate(_workshopId, employeeId, date);
//var dates = _rollCallApplication.GetEditableDatesForManualEdit(date.ToGeorgianDateTime());
var name = _rollCallEmployeeApplication.GetByEmployeeIdAndWorkshopId(employeeId, _workshopId);
var total = new TimeSpan(result.Sum(x =>
(x.EndDate!.Value.Ticks - x.StartDate!.Value.Ticks)));
var command = new EmployeeRollCallsViewModel()
{
EmployeeFullName = name.EmployeeFullName,
EmployeeId = employeeId,
DateFa = date,
//EditableDates = dates,
RollCalls = result,
TotalRollCallsDuration = total.ToFarsiHoursAndMinutes("-")
};
return Partial("ModalEditRollCall", command);
}
public IActionResult OnGetTotalWorking(string startDate, string startTime, string endDate, string endTime)
{
try
{
DateTime start = startDate.ToGeorgianDateTime();
DateTime end = endDate.ToGeorgianDateTime();
TimeOnly startTimeOnly = TimeOnly.Parse(startTime);
TimeOnly endTimeOnly = TimeOnly.Parse(endTime);
start = start + startTimeOnly.ToTimeSpan();
end = end + endTimeOnly.ToTimeSpan();
if (start >= end)
return new JsonResult(new
{
message = "",
});
var span = end - start;
var hours = (int)span.TotalHours;
var minutes = span.Minutes;
if (hours > 0 && minutes > 0)
{
return new JsonResult(new
{
message = hours + " " + "ساعت و" + " " + minutes + " " + "دقیقه",
});
}
else if (hours > 0 && minutes == 0)
{
return new JsonResult(new
{
message = hours + " " + "ساعت ",
});
}
else if (hours == 0 && minutes > 0)
{
return new JsonResult(new
{
message = minutes + " " + "دقیقه",
});
}
else
{
return new JsonResult(new
{
message = "",
});
}
}
catch
{
return new JsonResult(new
{
message = "",
});
}
}
public IActionResult OnGetDayOfWeek(string dateFa)
{
if (dateFa.TryToGeorgianDateTime(out DateTime date) == false)
return new JsonResult(new
{
success = true,
message = "",
});
return new JsonResult(new
{
success = true,
message = date.DayOfWeek.DayOfWeeKToPersian(),
});
}
public IActionResult OnPostManualCreateOrEdit(CreateOrEditEmployeeRollCall command)
{
command.WorkshopId = _workshopId;
var result = _rollCallApplication.ManualEdit(command);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnPostRemoveEmployeeRollCallsInDate(long employeeId, string date)
{
var result = _rollCallApplication.RemoveEmployeeRollCallsInDate(_workshopId, employeeId, date);
return new JsonResult(new
{
success = result.IsSuccedded,
message = result.Message,
});
}
public IActionResult OnGetPrintAllWithoutPersonnel(string exactDateFa)
{
var searchModel = new RollCallSearchModel()
{
WorkshopId = _workshopId,
ExactDateFa = exactDateFa,
};
var command = _rollCallApplication.GetWorkshopRollCallHistory(searchModel);
return Partial("PrintAllCaseHistoryWithoutPersonnel", command);
}
public IActionResult OnGetPrintAllWithPersonnel(int yearFa, string monthFaName, long employeeId)
{
Console.WriteLine("12");
int monthFa = monthFaName.ToMonthByStringValue();
var pc = new PersianCalendar();
var startDate = $"{yearFa:0000}/{monthFa:00}/01";
var endDate = startDate.ToGeorgianDateTime().AddMonthsFa(1, out _).ToGeorgianDateTime().AddDays(-1).ToFarsi();
var command = _rollCallApplication.GetEmployeeRollCallsHistory(employeeId, _workshopId, startDate, endDate, "", "");
return Partial("PrintAllCaseHistoryWithPersonnel", command);
}
public IActionResult OnGetPrintAllWithPersonnelNew(long employeeId, string firstRecordToPrintStartDateFa, string lastRecordToPrintEndDateFa)
{
var command = _rollCallApplication.GetEmployeeRollCallsHistoryForPrint(employeeId, _workshopId, firstRecordToPrintStartDateFa, lastRecordToPrintEndDateFa);
return Partial("PrintAllCaseHistoryWithPersonnel", command);
}
public IActionResult OnGetDownloadCaseHistoryWithPersonnelExcel(string firstRecordToPrintStartDateFa, string lastRecordToPrintEndDateFa, long employeeId)
{
var command = _rollCallApplication.GetEmployeeRollCallsHistoryForPrint(employeeId, _workshopId, firstRecordToPrintStartDateFa, lastRecordToPrintEndDateFa);
//var command = _rollCallApplication.GetEmployeeRollCallsHistory(employeeId, _workshopId, startDate, endDate, "", "");
var caseHistoryRollCallExcelForEmployee = new CaseHistoryRollCallExcelForEmployeeViewModel()
{
EmployeeId = command.RollCalls.FirstOrDefault()?.EmployeeId ?? 0,
DateGr = command.DateGr,
PersonnelCode =command.RollCalls.FirstOrDefault()?.PersonnelCode,
EmployeeFullName = command.RollCalls.FirstOrDefault()?.EmployeeFullName,
PersianMonthName = command.PersianMonthName,
TotalWorkingHoursFa = command.TotalWorkingHours,
TotalWorkingTimeSpan = command.TotalWorkingHoursTimeSpan,
PersianYear = command.PersianYear,
RollCalls = command.RollCalls.Select(x=> new RollCallItemForEmployeeExcelViewModel()
{
DateGr = x.DateGr,
DateFa = x.DateFa,
DayOfWeekFa = x.DayOfWeekFa,
HasLeave = x.HasLeave,
IsHoliday = x.IsHoliday,
IsAbsent = x.IsAbsent,
TotalWorkingHours = x.TotalWorkingHours,
StartsItems = string.Join(Environment.NewLine, x.RollCallTimesList.Select(tr=>tr.StartDate)),
EndsItems= string.Join(Environment.NewLine, x.RollCallTimesList.Select(tr=>tr.EndDate)),
EnterTimeDifferences = string.Join(Environment.NewLine, x.RollCallTimesList.Select(tr=>tr.EntryTimeDifferences)),
ExitTimeDifferences = string.Join(Environment.NewLine, x.RollCallTimesList.Select(tr=>tr.ExitTimeDifferences))
}).ToList()
};
var excelBytes = RollCallExcelGenerator.CaseHistoryExcelForEmployee(caseHistoryRollCallExcelForEmployee);
return File(excelBytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"{caseHistoryRollCallExcelForEmployee.EmployeeFullName} - {caseHistoryRollCallExcelForEmployee.PersianYear}/{caseHistoryRollCallExcelForEmployee.PersianMonthName}.xlsx");
}
public IActionResult OnGetDownloadCaseHistoryWithoutPersonnelExcel(string exactDateFa)
{
var searchModel = new RollCallSearchModel()
{
WorkshopId = _workshopId,
ExactDateFa = exactDateFa,
};
var command = _rollCallApplication.GetWorkshopRollCallHistory(searchModel);
var workshopFullName = _workshopApplication.GetDetails(_workshopId)?.WorkshopFullName;
var caseHistoryRollCallExcelForOneDay = new CaseHistoryRollCallForOneDayViewModel()
{
DayOfWeekFa = command.DayOfWeekFa,
DateFa = command.DateFa,
DateGr = command.DateGr,
RollCalls = command.ActiveEmployees.Select(x => new RollCallItemForOneDayExcelViewModel()
{
EmployeeFullName = x.EmployeeFullName,
StartsItems = string.Join(Environment.NewLine, x.RollCallTimesList.Select(s => s.StartDate)),
EndsItems = string.Join(Environment.NewLine, x.RollCallTimesList.Select(e => e.EndDate)),
PersonnelCode = x.PersonnelCode,
TotalWorkingHours = x.TotalWorkingHours
}).ToList()
};
byte[] excelBytes = RollCallExcelGenerator.CaseHistoryExcelForOneDay(caseHistoryRollCallExcelForOneDay);
return File(excelBytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"{workshopFullName} - {caseHistoryRollCallExcelForOneDay.DayOfWeekFa}،{caseHistoryRollCallExcelForOneDay.DateFa}.xlsx");
}
public IActionResult OnPostCalculateRollCallsTotalDuration(List<CalculateRollCallsTotalDuration> rollCalls)
{
var withoutNull = rollCalls.Where(x => x.StartDateFa != null && x.StartTime !=null &&
x.EndDateFa != null && x.EndTime!=null);
var ticks = withoutNull.Sum(x =>
((x.EndDateFa.ToGeorgianDateTime().Ticks + TimeOnly.Parse(x.EndTime).Ticks) - (x.StartDateFa.ToGeorgianDateTime().Ticks + TimeOnly.Parse(x.StartTime).Ticks)));
var timeSpan = new TimeSpan(ticks);
return new JsonResult(new
{
result = timeSpan.ToFarsiHoursAndMinutes("-")
});
}
public IActionResult OnGetLeaveCreate(CreateLeave command)
{
return Partial("LeaveCreate", command);
}
}
public class CalculateRollCallsTotalDuration
{
public string StartDateFa { get; set; }
public string StartTime { get; set; }
public string EndDateFa { get; set; }
public string EndTime { get; set; }
}
}