Files
BeWoPlaner/ReportImp/HausDuelken/Validation/ServiceRecordValidator.cs

193 lines
8.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Data.Security;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.Plugins;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using BS.Shared.Services;
namespace KetteReports.Validation
{
public class CustomServiceRecordValidator : ServiceRecordValidator
{
public override string[] GetIgnoreCategoriesForOverlappingCheck()
{
return new[] { "Arbeitszeit" };
}
public override string[] GetIgnoreServiceDescriptionsForOverlappingCheck()
{
return new[] { "Arbeitszeit", "Pause" };
}
public override List<ServiceRecordValidationResultDC> ValidateServiceRecord(ServiceRecordDC newServiceRecord, SupportConceptStatisticsDC statistics, int maxDaysEditServiceRecordsAllowed, IList<long> employeeOids, IList<long> cb2scOids)
{
var results = base.ValidateServiceRecord(newServiceRecord, statistics, maxDaysEditServiceRecordsAllowed, employeeOids, cb2scOids);
if (newServiceRecord.RoundedDuration == 0)
{
results.Add(new ServiceRecordValidationResultDC()
{
Allow = false,
CustomerName = newServiceRecord.Customer != null ? newServiceRecord.Customer.LastNameFirstName : "",
EmployeeName = newServiceRecord.Employee != null ? newServiceRecord.Employee.FirstNameLastName : "",
Message = "Einträge mit einer Dauer von 0 Minuten können nicht gespeichert werden.",
ResultType = ServiceRecordValidationResult.Custom,
});
}
return results;
}
public override ServiceRecordValidationResultDC CheckOverlappingCustomerServiceRecords(ServiceRecordDC newServiceRecord, DateTime start, IList<ServiceRecord> allCustomerRecords, bool isGroupRecord, decimal roundedDuration)
{
if (SollBeiDiesemEintragGeprueftWerdenObAndereCustomerEintraegeUeberschnittenWerden(newServiceRecord))
{
var customerRecordsWithTime = allCustomerRecords.Where(r => r.Start.HasValue && r.Start.Value.Second == 0).ToList();
if (isGroupRecord && newServiceRecord.GroupOid.HasValue)
{
customerRecordsWithTime =
customerRecordsWithTime.Where(r => !r.GroupOid.HasValue || r.GroupOid != newServiceRecord.GroupOid).ToList();
}
ServiceRecord overlappingRecord =
customerRecordsWithTime.FirstOrDefault(
record => TimeSpanOverlapsWithOtherTimeSpan(start, record.Start,
roundedDuration,
record.GroupRoundedDuration ?? record.RoundedDuration));
if (overlappingRecord != null)
{
var rdc = new ServiceRecordValidationResultDC
{
ResultType = ServiceRecordValidationResult.ServiceRecordOverlapping,
Message = String.Format("{0}, Dauer {1:0} Minuten", overlappingRecord.Start.Value.ToShortTimeString(),
overlappingRecord.GroupRoundedDuration ?? overlappingRecord.RoundedDuration)
};
Customer c = overlappingRecord.Customer;
if (c != null)
{
rdc.CustomerName = String.Format("{0}, {1}", c.Person.LastName, c.Person.FirstName);
}
Employee e = overlappingRecord.Employee;
if (e != null)
{
rdc.EmployeeName = String.Format("{0} {1}", e.Person.FirstName, e.Person.LastName);
}
return rdc;
}
}
return null;
}
public override ServiceRecordValidationResultDC CheckOverlappingEmployeeServiceRecords(ServiceRecordDC newServiceRecord, DateTime start, long employeeOid, decimal roundedDuration)
{
var empRecords = GetEmployeeServiceRecordsWithTimeForLastDay(newServiceRecord, start, employeeOid);
String[] ignoreNames = GetIgnoreCategoriesForOverlappingCheck();
if (ignoreNames != null)
{
bool ignore = ignoreNames.Contains(newServiceRecord.ServiceDescription.Category.Name);
if (ignore)
{
empRecords =
empRecords.Where(r => ignoreNames.Contains(r.ServiceDescription.ServiceCategory.Name)).ToList();
}
else
{
empRecords =
empRecords.Where(r => !ignoreNames.Contains(r.ServiceDescription.ServiceCategory.Name)).ToList();
}
}
ignoreNames = GetIgnoreServiceDescriptionsForOverlappingCheck();
if (ignoreNames != null)
{
bool ignore = ignoreNames.Contains(newServiceRecord.ServiceDescription.Name);
if (ignore)
{
empRecords =
empRecords.Where(r => ignoreNames.Contains(r.ServiceDescription.Name)).ToList();
}
else
{
empRecords =
empRecords.Where(r => !ignoreNames.Contains(r.ServiceDescription.Name)).ToList();
}
}
try
{
ServiceRecord overlappingRecord =
empRecords.FirstOrDefault(
record =>
TimeSpanOverlapsWithOtherTimeSpan(start, record.Start, roundedDuration, record.GroupRoundedDuration ?? record.RoundedDuration));
if (overlappingRecord != null && SollBeiDiesemEintragGeprueftWerdenObAndereCustomerEintraegeUeberschnittenWerden(newServiceRecord))
{
var rdc = new ServiceRecordValidationResultDC
{
ResultType = ServiceRecordValidationResult.EmployeeOverlapping,
Message = String.Format("{0}, Dauer {1:0} Minuten", overlappingRecord.Start.Value.ToShortTimeString(),
overlappingRecord.GroupRoundedDuration ?? overlappingRecord.RoundedDuration)
};
Customer c = overlappingRecord.Customer;
if (c != null)
{
rdc.Message += String.Format(" bei Klient/in {0}, {1}", c.Person.LastName, c.Person.FirstName);
}
Employee e = overlappingRecord.Employee;
if (e != null)
{
rdc.EmployeeName = String.Format("{0} {1}", e.Person.FirstName, e.Person.LastName);
}
return rdc;
}
}
catch (Exception)
{
return new ServiceRecordValidationResultDC { ResultType = ServiceRecordValidationResult.Error };
}
return null;
}
private bool SollBeiDiesemEintragGeprueftWerdenObAndereCustomerEintraegeUeberschnittenWerden(ServiceRecordDC editServiceRecord)
{
//Bei neuen Einträgen soll immer geprüft werden
if (!editServiceRecord.ServiceRecordOid.HasValue)
{
return true;
}
var original = DAOFactory.GenericDAO.LoadByID<ServiceRecord>(editServiceRecord.ServiceRecordOid.Value);
if (original.Start == editServiceRecord.Start && original.End == editServiceRecord.End || editServiceRecord.ServiceDescription.CategoryName.Trim().ToLower() == "lt24")
{
//Wenn sich das Datum nicht ändert, muss keine Überlappung geprüft werden (z.B. wenn sich nur der Dokutext ändert)
return false;
}
return true;
}
}
}