2019-07-12 17:36:37 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2022-04-25 15:45:02 +02:00
using BeWo.Data ;
2019-07-12 17:36:37 +02:00
using BeWo.Data.Access ;
using BeWo.Data.Entities ;
using BeWo.Service.DCEntityMapper ;
2025-03-16 20:42:21 +01:00
using BeWo.ServiceUtils.History ;
2019-07-12 17:36:37 +02:00
using BS.Shared ;
using BS.Shared.Core ;
using BS.Shared.DataContracts ;
using BS.Shared.Extensions ;
namespace BeWo.Service.Plugins
{
public class OperationService : AbstractIDSpecificDefaultClass < OperationService >
{
2022-04-25 15:45:02 +02:00
protected String ReturnMessage { get ; set ; }
2024-09-17 21:10:47 +02:00
protected virtual bool CreateServiceRecordsIfAnyExistInDateRange { get { return false ; } }
2022-06-03 12:34:41 +02:00
public virtual String CreateDefaultCustomerServiceRecords ( DateTime start , DateTime end , IList < long > cb2scOids )
2022-04-25 15:45:02 +02:00
{
var vs = PluginLoader . FindClass < VacationService > ( ) ;
ReturnMessage = "" ;
DateTimeSpan span = new DateTimeSpan ( ) ;
span . StartDateTime = start ;
span . EndDateTime = end . AddDays ( 1 ) . AddTicks ( - 1 ) ;
var c2sList = DAOFactory . GenericDAO . LoadByIDs < CostBearer2SupportConcept > ( cb2scOids ) ;
foreach ( var c2s in c2sList )
{
var srList = DAOFactory . SearchDAO . FindServiceRecordsInSpan ( c2s . Oid . Value , span ) . ToList ( ) ;
2024-09-17 21:10:47 +02:00
if ( srList . Count = = 0 | | CreateServiceRecordsIfAnyExistInDateRange )
2022-04-25 15:45:02 +02:00
{
2024-09-17 21:10:47 +02:00
var newSrList = ErstelleStundenplanEintraege ( span . StartDateTime . Date , span . EndDate . Date , c2s , vs , srList ) ;
2022-04-25 15:45:02 +02:00
2024-09-17 21:10:47 +02:00
DAOFactory . GenericDAO . Insert ( newSrList ) ;
2025-03-16 20:42:21 +01:00
HistoryCreator . MakeServiceRecordHistoryEntry ( newSrList , newSrList . Select ( sr = > sr . Oid . Value ) . ToList ( ) , StatementType . Insert ) ;
2023-09-08 15:52:55 +02:00
if ( ReturnMessage . Length > 0 )
{
ReturnMessage + = "\n" ;
}
2024-09-17 21:10:47 +02:00
ReturnMessage + = String . Format ( "Es wurden {0} Einträge erstellt." , newSrList . Count ) ;
2022-04-25 15:45:02 +02:00
}
else
{
if ( srList . Count > 1 )
{
ReturnMessage = String . Format ( "In dem ausgewählten Zeitraum sind bereits {0} Einträge vorhanden, daher können keine weiteren Einträge automatisch erstellt werden." , srList . Count ) ;
}
else
{
ReturnMessage = String . Format ( "In dem ausgewählten Zeitraum ist bereits ein Eintrag vorhanden, daher können keine weiteren Einträge automatisch erstellt werden." ) ;
}
}
}
return ReturnMessage ;
}
2024-09-17 21:10:47 +02:00
public virtual List < ServiceRecord > ErstelleStundenplanEintraege ( DateTime start , DateTime ende , CostBearer2SupportConcept c2s , VacationService vs , List < ServiceRecord > existingList )
2022-04-25 15:45:02 +02:00
{
var customer = c2s . SupportConcept . Customer ;
List < ServiceRecord > serviceRecords = new List < ServiceRecord > ( ) ;
var defaultSd = GetDefaultServiceDescription ( c2s ) ;
if ( defaultSd = = null )
{
ReturnMessage = "Es konnte keine passende Leistung ermittelt werden." ;
return serviceRecords ;
}
while ( start < = ende )
{
//var ferien = vs.GetFerien(start, "nrw");
2024-01-16 18:42:02 +01:00
var stundenplan = GetStundenplan ( customer , start , start ) ;
2022-04-25 15:45:02 +02:00
var ft = vs . GetFeiertag ( start ) ;
//if (ferien == null && ft == null)
2022-06-03 12:34:41 +02:00
if ( ft = = null & & ! HatAbwesenheit ( start , customer ) )
2022-04-25 15:45:02 +02:00
{
2024-09-17 21:10:47 +02:00
var newServiceRecords = CreateServiceRecords ( stundenplan , start , c2s , defaultSd , existingList ) ;
2022-04-25 15:45:02 +02:00
serviceRecords . AddRange ( newServiceRecords ) ;
}
start = start . AddDays ( 1 ) ;
}
return serviceRecords ;
}
2025-04-23 00:26:10 +02:00
public virtual bool HatAbwesenheit ( DateTime start , Customer customer )
2022-06-03 12:34:41 +02:00
{
foreach ( var at in customer . AbsenceTimes )
{
if ( at . Start < = start & & ( ! at . End . HasValue | | at . End > = start ) )
{
return true ;
}
}
return false ;
}
2024-09-17 21:10:47 +02:00
protected virtual IList < ServiceRecord > CreateServiceRecords ( Arbeitszeit arbeitszeit , DateTime datum , CostBearer2SupportConcept c2s , ServiceDescription defaultSd , List < ServiceRecord > existingList )
2022-04-25 15:45:02 +02:00
{
var records = new List < ServiceRecord > ( ) ;
if ( arbeitszeit ! = null & & arbeitszeit . ArbeitszeitEintraege ! = null )
{
foreach ( var ae in arbeitszeit . ArbeitszeitEintraege )
{
if ( IsSameWeekday ( ae , datum . DayOfWeek ) )
{
2024-09-17 21:10:47 +02:00
if ( existingList = = null | | existingList . Count ( s = > s . Start . Value . Date = = datum . Date ) = = 0 )
{
2025-10-30 16:52:46 +01:00
if ( datum . Date > = c2s . StartDate . Value . Date & & datum . Date < = c2s . EndDate . Value . Date )
{
var sr = CreateServiceRecord ( ae , datum , c2s , defaultSd ) ;
2024-09-17 21:10:47 +02:00
2025-10-30 16:52:46 +01:00
records . Add ( sr ) ;
}
2024-09-17 21:10:47 +02:00
}
2022-04-25 15:45:02 +02:00
}
}
}
return records ;
}
2022-06-03 12:34:41 +02:00
public virtual ServiceRecord CreateServiceRecord ( ArbeitszeitEintrag ae , DateTime datum , CostBearer2SupportConcept c2s , ServiceDescription defaultSd )
2025-05-15 20:41:41 +02:00
{
//Wegen Abwärtskompatibilität muss diese Methode erhalten beleiben
Employee emp = GetHauptbetreuung ( c2s . SupportConcept . Customer , datum ) ; ;
return CreateServiceRecord ( ae , datum , emp , c2s . SupportConcept . Customer , c2s , c2s . SupportConcept , defaultSd ) ;
}
public virtual ServiceRecord CreateServiceRecord ( ArbeitszeitEintrag ae , DateTime datum , Employee emp , Customer c , CostBearer2SupportConcept c2s , SupportConcept sc , ServiceDescription defaultSd )
2022-06-03 12:34:41 +02:00
{
ServiceRecord sr = new ServiceRecord ( ) ;
sr . Start = GetDateTime ( ae . UhrzeitVon , datum ) ;
sr . End = GetDateTime ( ae . UhrzeitBis , datum ) ;
sr . RoundedDuration = ( decimal ) sr . End . Value . Subtract ( sr . Start . Value ) . TotalMinutes ;
if ( ae . Employee ! = null )
{
sr . Employee = ae . Employee ;
}
else
2024-09-17 21:10:47 +02:00
{
2025-05-15 20:41:41 +02:00
sr . Employee = emp ;
2024-09-17 21:10:47 +02:00
}
if ( sr . Employee = = null )
2022-06-03 12:34:41 +02:00
{
sr . Employee = LoggedInUserOperationContextExt . Current . User . Employee ;
}
2025-05-15 20:41:41 +02:00
sr . Customer = c ;
sr . SupportConcept = sc ;
2022-06-03 12:34:41 +02:00
sr . CostBearer2SupportConcept = c2s ;
sr . ServiceDescription = defaultSd ;
sr . ServiceRecordType = ServiceRecordTypeId . DefaultActivity ;
sr . DistanceInMeter = 0 ;
sr . DurationInStunden = ZeiterfassungsDauer . Minuten ;
sr . ServiceRecordFormat = ServiceRecordFormate . OriginaleZeiterfassung ;
sr . Relevance = 0 ;
return sr ;
2025-05-15 20:41:41 +02:00
2022-06-03 12:34:41 +02:00
}
2024-09-17 21:10:47 +02:00
public virtual Employee GetHauptbetreuung ( Customer customer , DateTime date )
{
return customer . Employee2CustomerList . FirstOrDefault ( e2c = > e2c . IsActive = = ActivationTypeId . Active & &
( ! e2c . StartDate . HasValue | | e2c . StartDate . Value . Date < = date ) & & ( ! e2c . EndDate . HasValue | | e2c . EndDate . Value . Date > = date ) & &
e2c . ValueList . Any ( b = > b . Entry . Type . Equals ( ValueListEntryType . StaffRoleType ) & &
b . Entry . SystemEntryID . HasValue & & b . Entry . SystemEntryID . Value = = SystemEntryID . EmployeeRoleMainAttendant ) ) ? . Employee ;
}
2022-04-25 15:45:02 +02:00
public virtual ServiceDescription GetDefaultServiceDescription ( CostBearer2SupportConcept c2s )
{
foreach ( var ap in c2s . ApprovalPeriodList )
{
if ( ap . ServiceCategory ! = null )
{
var desc = DAOFactory . SearchDAO . FindServiceDescriptionForCategory ( ap . ServiceCategory . Oid . Value ) ;
var sd = desc . OrderBy ( a = > a . Position ) . FirstOrDefault ( ) ;
if ( sd ! = null )
{
return sd ;
}
}
}
return GetDefaultServiceDescription ( ) ;
}
2024-11-05 14:26:49 +01:00
public virtual CheckObjectDeletionResultDC CheckObjectDeletionAllowed ( long objectoid , TableID tableId )
{
var result = new CheckObjectDeletionResultDC ( ) ;
if ( tableId = = TableID . ServiceCategory )
{
result . DeletionAllowed = true ;
var count = DAOFactory . SearchDAO . GetServiceRecordCountForServiceCategory ( objectoid ) ;
if ( count > 0 )
{
if ( count = = 1 )
{
result . Message = String . Format ( "Es existiert ein Eintrag für die gewählte Kategorie. Vorhandene Einträge werden nicht verändert. Möchten Sie die Kategorie wirklich löschen?" , count ) ;
}
else
{
result . Message = String . Format ( "Es existieren {0} Einträge für die gewählte Kategorie. Vorhandene Einträge werden nicht verändert. Möchten Sie die Kategorie wirklich löschen?" , count ) ;
}
}
}
else if ( tableId = = TableID . ServiceDescription )
{
result . DeletionAllowed = true ;
2024-11-05 14:36:22 +01:00
var count = DAOFactory . SearchDAO . GetServiceRecordCountForServiceDescription ( objectoid ) ;
2024-11-05 14:26:49 +01:00
if ( count > 0 )
{
if ( count = = 1 )
{
result . Message = String . Format ( "Es existiert ein Eintrag für die gewählte Leistung. Vorhandene Einträge werden nicht verändert. Möchten Sie die Leistung wirklich löschen?" , count ) ;
}
else
{
result . Message = String . Format ( "Es existieren {0} Einträge für die gewählte Leistung. Vorhandene Einträge werden nicht verändert. Möchten Sie die Leistung wirklich löschen?" , count ) ;
}
}
}
return result ;
}
2022-04-25 15:45:02 +02:00
public virtual ServiceDescription GetDefaultServiceDescription ( )
{
return null ;
}
private Arbeitszeit GetStundenplan ( Customer customer , DateTime start , DateTime ende )
{
if ( customer = = null )
{
return null ;
}
var stundenplan = SucheStundenplan ( customer . Arbeitszeiten , start , ende ) ;
return stundenplan ;
}
private static Arbeitszeit SucheStundenplan ( IList < Arbeitszeit > arbeitszeiten , DateTime start , DateTime ende )
{
if ( arbeitszeiten ! = null & & arbeitszeiten . Count > 0 )
{
foreach ( var az in arbeitszeiten )
{
if ( ( ! az . GueltigVon . HasValue | | az . GueltigVon < = ende ) & & ( ! az . GueltigBis . HasValue | | az . GueltigBis > = start ) )
{
return az ;
}
}
}
return null ;
}
public static bool IsSameWeekday ( ArbeitszeitEintrag ae , DayOfWeek day )
{
if ( day = = DayOfWeek . Monday & & ae . Tag = = AppointmentDayOfWeek . Montag )
{
return true ;
}
if ( day = = DayOfWeek . Tuesday & & ae . Tag = = AppointmentDayOfWeek . Dienstag )
{
return true ;
}
if ( day = = DayOfWeek . Wednesday & & ae . Tag = = AppointmentDayOfWeek . Mittwoch )
{
return true ;
}
if ( day = = DayOfWeek . Thursday & & ae . Tag = = AppointmentDayOfWeek . Donnerstag )
{
return true ;
}
if ( day = = DayOfWeek . Friday & & ae . Tag = = AppointmentDayOfWeek . Freitag )
{
return true ;
}
if ( day = = DayOfWeek . Saturday & & ae . Tag = = AppointmentDayOfWeek . Samstag )
{
return true ;
}
return false ;
}
public static DateTime ? GetDateTime ( String timeAsString , DateTime date )
{
String dateStr = String . Format ( "{0:dd.MM.yyyy} {1}" , date , timeAsString ) ;
DateTime dateOut ;
if ( DateTime . TryParse ( dateStr , out dateOut ) )
{
return dateOut ;
}
return null ;
}
2024-11-05 14:26:49 +01:00
2025-02-20 00:20:46 +01:00
public virtual IList < TextModuleDC > GetTextModulesForSelectedSupportConcept ( bool pShouldShowAllTextModules , long pEmployeeOid , bool pHasRightToSeeAllTextModules , bool pIsInAdministrationView , long? cb2scOid )
{
var list = GetTextModules ( pShouldShowAllTextModules , pEmployeeOid , pHasRightToSeeAllTextModules , pIsInAdministrationView ) ;
//if (cb2scOid.HasValue)
//{
// var cb2Sc = DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(cb2scOid.Value);
// var vlist = new List<ValueListEntryType>
// {
// ValueListEntryType.SupportConceptGoalCategoryType,
// ValueListEntryType.SupportConceptGoalType,
// ValueListEntryType.SupportConceptIndividualGoalType,
// ValueListEntryType.SupportConceptIndividualGoalCategoryType
// };
2025-02-26 00:24:17 +01:00
// var goals = cb2Sc.SupportConcept.ValueList.Where(v => vlist.Contains(v.Entry.Type)).Select(v => v.Entry).ToList();
2025-02-20 00:20:46 +01:00
// if (goals.Count > 0)
// {
2025-02-26 00:24:17 +01:00
// foreach (var tb in list)
// {
// if (tb.Parent != null)
// {
// tb.Parent.Name = String.Format(" {0}", tb.Parent.Name);
// }
// }
2025-02-20 00:20:46 +01:00
// var allMainGoals = DAOFactory.SearchDAO.FindValueListEntries(new List<ValueListEntryType>() { ValueListEntryType.SupportConceptGoalCategoryType, ValueListEntryType.SupportConceptGoalType });
// Dictionary<long, ValueListEntry> dict = new Dictionary<long, ValueListEntry>();
// foreach (var item in allMainGoals)
// {
// dict.Add(item.Oid.Value, item);
// }
2025-02-26 00:24:17 +01:00
// foreach (var item in goals)
// {
// if (!dict.ContainsKey(item.Oid.Value))
// {
// dict.Add(item.Oid.Value, item);
// }
// }
// var zielKategorie = new TextModuleDC();
// zielKategorie.Name = "Ziele";
// zielKategorie.TextModuleOid = 1000000;
// zielKategorie.IsParent = true;
// list.Add(zielKategorie);
2025-02-20 00:20:46 +01:00
2025-02-26 00:24:17 +01:00
// var entryOid2TextbausteinDict = new Dictionary<long, TextModuleDC>();
// long oid = 1000001;
2025-02-20 00:20:46 +01:00
// foreach (var item in goals)
// {
2025-02-26 00:24:17 +01:00
// var path = new List<ValueListEntry>();
// path.Add(item);
// var parentpath = GetAllParentGoalCategoies(dict, item);
// path.AddRange(parentpath);
// //Der Pfad enthält den Gesamten Pfad vom Kind zum Obersten Vater Knoten und muss rückwärts durchlaufen werden
// for (int i = path.Count - 1; i >= 0; i--)
// {
// var ziel = path[i];
// if (!entryOid2TextbausteinDict.ContainsKey(ziel.Oid.Value))
// {
// TextModuleDC tm = new TextModuleDC();
// tm.Text = ziel.Value;
// tm.Name = ziel.Value;
// tm.TextModuleOid = oid++;
// if (ziel.ParentOid.HasValue && entryOid2TextbausteinDict.ContainsKey(ziel.ParentOid.Value))
// {
// var parent = entryOid2TextbausteinDict[ziel.ParentOid.Value];
// parent.IsParent = true;
// tm.Parent = parent;
// }
// else
// {
// //an die oberste Kategorie hängen
// tm.Parent = zielKategorie;
// }
// entryOid2TextbausteinDict.Add(ziel.Oid.Value, tm);
// list.Add(tm);
// }
// }
2025-02-20 00:20:46 +01:00
// }
// }
//}
return list ;
}
2025-02-26 00:24:17 +01:00
private static List < ValueListEntry > GetAllParentGoalCategoies ( Dictionary < long , ValueListEntry > goalCategoryDict , ValueListEntry goal )
2025-02-20 00:20:46 +01:00
{
2025-02-26 00:24:17 +01:00
var path = new List < ValueListEntry > ( ) ;
2025-02-20 00:20:46 +01:00
if ( goal . ParentOid . HasValue & & goalCategoryDict . ContainsKey ( goal . ParentOid . Value ) )
{
2025-02-26 00:24:17 +01:00
var parentCat = goalCategoryDict [ goal . ParentOid . Value ] ;
2025-02-20 00:20:46 +01:00
while ( parentCat ! = null )
{
path . Add ( parentCat ) ;
if ( parentCat . ParentOid . HasValue & & goalCategoryDict . ContainsKey ( parentCat . ParentOid . Value ) )
{
parentCat = goalCategoryDict [ parentCat . ParentOid . Value ] ;
}
else
{
parentCat = null ;
}
}
}
return path ;
}
public virtual IList < TextModuleDC > GetTextModules ( bool pShouldShowAllTextModules , long pEmployeeOid , bool pHasRightToSeeAllTextModules , bool pIsInAdministrationView )
{
var textmodules = DAOFactory . SearchDAO . GetActiveTextModules ( pShouldShowAllTextModules , pHasRightToSeeAllTextModules , pIsInAdministrationView , pEmployeeOid ) ;
var abc = textmodules . FirstOrDefault ( f = > f . Oid = = 41 ) ;
var isActive = abc ? . IsActive ;
var list = MapperFactory . TextModuleDC_TextModule . CreateDcList ( textmodules ) ;
var abcDC = list . FirstOrDefault ( f = > f . TextModuleOid = = 41 ) ;
var activationType = abcDC ? . ActivationType ;
return list . OrderBy ( t = > t . Name ) . ToList ( ) ;
}
2026-03-06 18:17:57 +01:00
public bool CheckForOverlappingAbsenceTimes ( DateTime ? start , DateTime ? end , long? employeeOid , long? customerOid )
{
var startValue = start ? ? DateTime . MinValue ;
var endValue = end ? ? DateTime . MaxValue ;
2025-02-20 00:20:46 +01:00
2026-03-06 18:17:57 +01:00
return DAOFactory . SearchDAO . CheckForOverlappingAbsenceTimes ( startValue , endValue , employeeOid , customerOid ) ;
}
2019-07-12 17:36:37 +02:00
}
}