using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using BeWo.Data.Entities; using BS.Shared.Extensions; using NHibernate; using NHibernate.Criterion; using NHibernate.Engine; using NHibernate.Impl; using NHibernate.Loader.Criteria; using NHibernate.Persister.Entity; using NHibernate.SqlCommand; using BS.Shared.Core; using BS.Shared; using BS.Shared.DataContracts; using static System.String; using Login = BeWo.Data.Entities.Login; namespace BeWo.Data.Access { public class SearchDAO : AbstractBaseDAO { private static Regex RecurrenceIdRegex = new Regex("(Id=\\\"[a-z0-9-]+\\\")"); public IEnumerable FindValueListEntry(ValueListEntryType pType) { return CreateCriteria().Add(Restrictions.Eq(ValueListEntry.PropertyName_Type, pType)).AddOrder(Order.Asc(ValueListEntry.PropertyName_Value)).List(); } public IEnumerable FindValueListEntries(List pTypes) { return CreateCriteria().Add(Restrictions.In(ValueListEntry.PropertyName_Type, pTypes)).AddOrder(Order.Asc(ValueListEntry.PropertyName_Value)).List(); } public virtual IEnumerable FindEmployee(string pFirstName, string pLastName, string pPersonnelNumber) { return CreateCriteria() .Add(Restrictions.Like(Employee.PropertyName_PersonnelNumber, pPersonnelNumber, MatchMode.Anywhere)) .CreateCriteria(Employee.PropertyName_Person, JoinType.InnerJoin) .Add(Restrictions.Like(Person.PropertyName_FirstName, pFirstName, MatchMode.Anywhere)) .Add(Restrictions.Like(Person.PropertyName_LastName, pLastName, MatchMode.Anywhere)) .List(); } public virtual Employee FindEmployeeByFullname(string pFullname) { var q = Session.CreateSQLQuery(Format("SELECT Oid FROM Person WHERE CONCAT_WS(' ', FirstName, LastName) LIKE '%{0}%'", pFullname)); var x = q.List(); return x.Count > 0 ? DAOFactory.GenericDAO.GetByID(x.First()) : null; } public virtual IEnumerable FindWohnheim(string pwohnheimName, string pWohnheimStrasse, string pWohnheimPlz) { return CreateCriteria() .Add(Restrictions.Like(Wohnheim.PropertyName_WohnheimName, pwohnheimName, MatchMode.Anywhere)) .CreateCriteria(Wohnheim.PropertyName_Wohnheim, JoinType.InnerJoin) .Add(Restrictions.Like(Wohnheim.PropertyName_Strasse, pWohnheimStrasse, MatchMode.Anywhere)) .Add(Restrictions.Like(Wohnheim.PropertyName_PlZ, pWohnheimPlz, MatchMode.Anywhere)) .List(); } public virtual Wohnheim FindWohnheimByFullname(string pWohnheimName) { var q = Session.CreateSQLQuery(Format("SELECT Oid FROM Wohnheim WHERE CONCAT_WS(' ', WohnheimName) LIKE '%{0}%'", pWohnheimName)); var x = q.List(); return x.Count > 0 ? DAOFactory.GenericDAO.GetByID(x.First()) : null; } public virtual IEnumerable FindCustomer(string pFirstName, string pLastName, string pReferenceNumber) { return CreateCriteria() .Add(Restrictions.Like(Customer.PropertyName_ReferenceNumber, pReferenceNumber, MatchMode.Anywhere)) .CreateCriteria(Customer.PropertyName_Person, JoinType.InnerJoin) .Add(Restrictions.Like(Person.PropertyName_FirstName, pFirstName, MatchMode.Anywhere)) .Add(Restrictions.Like(Person.PropertyName_LastName, pLastName, MatchMode.Anywhere)) .List(); } public virtual IList FindCustomer(string pFirstName, string pLastName, DateTime pBirthDate) { return CreateCriteriaIsActiveOrArchived() .CreateCriteria(Customer.PropertyName_Person, JoinType.InnerJoin) .Add(Restrictions.Eq(Person.PropertyName_FirstName, pFirstName)) .Add(Restrictions.Eq(Person.PropertyName_LastName, pLastName)) .Add(Restrictions.Eq(Person.PropertyName_DateOfBirth, pBirthDate)) .List(); } public virtual IEnumerable FindPerson(string pFirstName, string pLastName, DateTime? pDateOfBirth, PersonType? pPersonType) { var lCriteria = CreateCriteria() .Add(Restrictions.Like(Person.PropertyName_FirstName, pFirstName, MatchMode.Anywhere)) .Add(Restrictions.Like(Person.PropertyName_LastName, pLastName, MatchMode.Anywhere)); if (pDateOfBirth != null) lCriteria.Add(Restrictions.Eq(Person.PropertyName_DateOfBirth, pDateOfBirth)); if (pPersonType != null) lCriteria.Add(Restrictions.Eq(Person.PropertyName_Type, pPersonType)); return lCriteria.List(); } public virtual IEnumerable FindOrganisation(string pName, bool pOnlyCostBearer) { var lCriteria = CreateCriteria() .Add(Restrictions.Like(Organisation.PropertyName_Name, pName, MatchMode.Anywhere)); if (pOnlyCostBearer) lCriteria.Add(Restrictions.IsNotNull(Organisation.PropertyName_CostBearer)); return lCriteria.List(); } public virtual IEnumerable FindTeams(string pTeamName, string pLeaderFirstName, string pLeaderLastName) { return CreateCriteria() .Add(Restrictions.Like(Team.PropertyName_Name, pTeamName, MatchMode.Anywhere)) .CreateCriteria(Team.PropertyName_Leader, JoinType.InnerJoin) .CreateCriteria(Employee.PropertyName_Person) .Add(Restrictions.Like(Person.PropertyName_FirstName, pLeaderFirstName, MatchMode.Anywhere)) .Add(Restrictions.Like(Person.PropertyName_LastName, pLeaderLastName, MatchMode.Anywhere)) .List(); } public virtual IList FindTeamsOfEmployee(long employeeOid) { return CreateCriteriaIsActiveOrArchived() .CreateCriteria(Team.PropertyName_MemberList, JoinType.InnerJoin) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, employeeOid)) .List(); } public virtual IEnumerable FindLeadingTeams(long employeeOid) { return CreateCriteriaIsActiveOrArchived() .CreateCriteria(Team.PropertyName_Leader, JoinType.InnerJoin) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, employeeOid)) .List(); } public virtual IList FindAllActiveTeamsOfEmployee(long employeeOid) { return CreateCriteriaIsActive() .CreateAlias(Team.PropertyName_MemberList, "m", JoinType.InnerJoin) .CreateAlias(Team.PropertyName_Leader, "l", JoinType.InnerJoin) .Add(Restrictions.Disjunction() .Add(Restrictions.Eq("m." + BeWoEntityBase.PropertyName_Oid, employeeOid)) .Add(Restrictions.Eq("l." + BeWoEntityBase.PropertyName_Oid, employeeOid))) .List().Distinct().ToList(); } public virtual IList FindCustomerOfTeam(long teamOid) { return CreateCriteriaIsActiveOrArchived() .CreateCriteria(Customer.PropertyName_Team2CustomerList, JoinType.InnerJoin) .Add(Restrictions.Eq(Team2Customer.PropertyName_TeamOid, teamOid)) .List(); } public IEnumerable FindBookings(long pResourceOid, DateTime pSpanStart, DateTime pSpanEnd) { var lResult = CreateCriteria() .CreateAlias(ResourceBookingSequence.PropertyName_Resource, "r") .CreateAlias(ResourceBookingSequence.PropertyName_Details, "d") .Add(Restrictions.Eq("r." + BeWoEntityBase.PropertyName_Oid, pResourceOid)) .Add(Restrictions.Eq("d." + ResourceBooking.PropertyName_SequencePosition, 0)) .Add(Restrictions.Disjunction() .Add(Restrictions.Between("d." + ResourceBooking.PropertyName_Start, pSpanStart, pSpanEnd)) .Add(Restrictions.Between(ResourceBookingSequence.PropertyName_SequenceEnd, pSpanStart, pSpanEnd)) .Add(Restrictions.Conjunction() .Add(Restrictions.Le("d." + ResourceBooking.PropertyName_Start, pSpanStart)) .Add(Restrictions.Ge(ResourceBookingSequence.PropertyName_SequenceEnd, pSpanEnd)) ) ) .List(); return lResult; } public IEnumerable FindBookings(DateTime pSpanStart, DateTime pSpanEnd) { var lResult = CreateCriteria() .CreateAlias(ResourceBookingSequence.PropertyName_Details, "d") .Add(Restrictions.Eq("d." + ResourceBooking.PropertyName_SequencePosition, 0)) .Add(Restrictions.Disjunction() .Add(Restrictions.Between("d." + ResourceBooking.PropertyName_Start, pSpanStart, pSpanEnd)) .Add(Restrictions.Between(ResourceBookingSequence.PropertyName_SequenceEnd, pSpanStart, pSpanEnd)) .Add(Restrictions.Conjunction() .Add(Restrictions.Le("d." + ResourceBooking.PropertyName_Start, pSpanStart)) .Add(Restrictions.Ge(ResourceBookingSequence.PropertyName_SequenceEnd, pSpanEnd)) ) ) .List(); return lResult; } public List FindServiceRecords(long? pEmployeeOid, long? pCostBearer2SupportConceptOid) { var c = CreateCriteria() .CreateAlias(ServiceRecord.PropertyName_Employee, "e", JoinType.InnerJoin); if (pCostBearer2SupportConceptOid.HasValue) { c = c.CreateAlias(ServiceRecord.PropertyName_SupportConcept, "sc", JoinType.InnerJoin) .CreateAlias("sc." + SupportConcept.PropertyName_CostBearer2SupportConceptList, "c2s", JoinType.InnerJoin); } if (pEmployeeOid.HasValue) c = c.Add(Restrictions.Eq("e." + BeWoEntityBase.PropertyName_Oid, pEmployeeOid)); if (pCostBearer2SupportConceptOid.HasValue) c = c.Add(Restrictions.Eq("c2s." + BeWoEntityBase.PropertyName_Oid, pCostBearer2SupportConceptOid)); return c.List().ToList(); } public List FindServiceRecordsForSupportConcept(long pSupportConceptOid) { var c = CreateCriteria() .CreateAlias(ServiceRecord.PropertyName_SupportConcept, "sc", JoinType.InnerJoin); c = c.Add(Restrictions.Eq("sc." + BeWoEntityBase.PropertyName_Oid, pSupportConceptOid)); return c.List().ToList(); } public IList FindSupportConceptApprovalPeriod2Employees(Employee emp) { var c = CreateCriteria() .Add(Restrictions.Eq(SupportConceptApprovalPeriod2Employee.PropertyName_Employee, emp)); return c.List().ToList(); } public IList FindSupportConceptApprovalPeriod2Employees(SupportConceptApprovalPeriod scap) { var c = CreateCriteria() .Add(Restrictions.Eq(SupportConceptApprovalPeriod2Employee.PropertyName_SupportConceptApprovalPeriod, scap)); return c.List().ToList(); } public IEnumerable FindServiceRecordsInSpan(long pCostBearer2SupportConceptOid, DateTimeSpan period) { var criteria = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_CostBearer2SupportConceptOid, pCostBearer2SupportConceptOid)); if (period != null) criteria = criteria.Add( Restrictions.Or( Restrictions.Between(ServiceRecord.PropertyName_Start, period.StartDateTime, period.EndDateTime), Restrictions.Between(ServiceRecord.PropertyName_End, period.StartDateTime, period.EndDateTime))); return criteria.List().ToList(); } public IEnumerable FindServiceRecordHistory(long serviceRecordOid) { var criteria = CreateCriteria() .Add(Restrictions.Eq(ServiceRecordHistory.PropertyName_ServiceRecordOid, serviceRecordOid)); return criteria.List().ToList(); } public IEnumerable FindEmployeeServiceRecordsWithoutCustomer(long pEmployeeOid, long? days) { var c = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_EmployeeOid, pEmployeeOid)) .Add(Restrictions.IsNull(ServiceRecord.PropertyName_CostBearer2SupportConceptOid)); if (days.HasValue) { var minDate = DateTime.Now.Date.AddDays(-1 * days.Value); c.Add(Restrictions.Ge(ServiceRecord.PropertyName_Start, minDate)); } return c.List().ToList(); } public IEnumerable FindEmployeeServiceRecordsWithoutCustomerInSpan(long pEmployeeOid, DateTimeSpan period) { var criteria = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_EmployeeOid, pEmployeeOid)) .Add(Restrictions.IsNull(ServiceRecord.PropertyName_CostBearer2SupportConceptOid)); if (period != null) { criteria = criteria.Add( Restrictions.Or( Restrictions.Between(ServiceRecord.PropertyName_Start, period.StartDateTime, period.EndDateTime), Restrictions.Between(ServiceRecord.PropertyName_End, period.StartDateTime, period.EndDateTime))); } return criteria.List().ToList(); } public IEnumerable FindEmployeeServiceRecordsWithoutCustomerWithStartEndDate(long pEmployeeOid, DateTime start, DateTime ende) { var c = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_EmployeeOid, pEmployeeOid)) .Add(Restrictions.IsNull(ServiceRecord.PropertyName_CostBearer2SupportConceptOid)); var max = ende.Date.AddDays(1); c.Add(Restrictions.Ge(ServiceRecord.PropertyName_Start, start.Date)) .Add(Restrictions.Lt(ServiceRecord.PropertyName_End, max.Date)); return c.List().ToList(); } public IList FindEmployeeServiceRecords(long pEmployeeOid, DateTimeSpan pSpan, long? customerOid, ServiceRecordTypeId? srTypeFilter) { var lCriteria = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_EmployeeOid, pEmployeeOid)); if (pSpan != null) lCriteria.Add(Restrictions.Between(ServiceRecord.PropertyName_Start, pSpan.StartDateTime, pSpan.EndDateTime)); if (customerOid != null) lCriteria.Add(Restrictions.Eq(ServiceRecord.PropertyName_CustomerOid, customerOid.Value)); if (srTypeFilter.HasValue) { lCriteria.Add(Restrictions.Eq(ServiceRecord.PropertyName_ServiceRecordType, srTypeFilter.Value)); } var result = lCriteria.List().ToList(); return result; } public IList FindCustomerServiceRecords(long customerOid) { var lCriteria = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_CustomerOid, customerOid)); return lCriteria.List(); } public IList FindCustomerServiceRecords(long customerOid, DateTimeSpan pSpan, long? employeeOid, ServiceRecordTypeId? srTypeFilter, bool includeEndDateInSearch) { var lCriteria = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_CustomerOid, customerOid)); if (includeEndDateInSearch) { var orCriteria = Restrictions.Or( Restrictions.Between(ServiceRecord.PropertyName_Start, pSpan.StartDateTime, pSpan.EndDateTime), Restrictions.Between(ServiceRecord.PropertyName_End, pSpan.StartDateTime, pSpan.EndDateTime)); orCriteria = Restrictions.Or(orCriteria, Restrictions.And( Restrictions.Le(ServiceRecord.PropertyName_Start,pSpan.EndDateTime), Restrictions.Ge(ServiceRecord.PropertyName_End, pSpan.StartDateTime))); lCriteria.Add(orCriteria); } else { lCriteria.Add(Restrictions.Between(ServiceRecord.PropertyName_Start, pSpan.StartDateTime, pSpan.EndDateTime)); } if (employeeOid != null) lCriteria.Add(Restrictions.Eq(ServiceRecord.PropertyName_EmployeeOid, employeeOid.Value)); if (srTypeFilter.HasValue) { lCriteria.Add(Restrictions.Eq(ServiceRecord.PropertyName_ServiceRecordType, srTypeFilter.Value)); } return lCriteria.List(); } public IEnumerable FindServiceRecordsInSpan(DateTimeSpan pSpan, ServiceRecordTypeId? srTypeFilter) { var lCriteria = CreateCriteria() .Add(Restrictions.Between(ServiceRecord.PropertyName_Start, pSpan.StartDateTime, pSpan.EndDateTime)); if (srTypeFilter.HasValue) { lCriteria.Add(Restrictions.Eq(ServiceRecord.PropertyName_ServiceRecordType, srTypeFilter.Value)); } return lCriteria.List().ToList(); } public IEnumerable FindUnassignedAccountingTransactions() { return CreateCriteria() .Add(Restrictions.IsNull(AccountingTransaction.PropertyName_CostBearer2SupportConcept)) .List().ToList(); } public IEnumerable FindSupportConcept( string pCustomerFirstName, string pCustomerLastName, string pCustomerReferenceNumber, DateTime? pFrom, DateTime? pTill) { var lCriteria = CreateCriteria(); //if (pFrom != null) // lCriteria.Add(Expression.Ge(SupportConcept.PropertyName_Start, pFrom)); //if (pTill != null) // lCriteria.Add(Expression.Le(SupportConcept.PropertyName_End, pTill)); lCriteria = lCriteria.CreateCriteria(SupportConcept.PropertyName_Customer, JoinType.InnerJoin); if (!IsNullOrEmpty(pCustomerReferenceNumber)) lCriteria.Add(Restrictions.Eq(Customer.PropertyName_ReferenceNumber, pCustomerReferenceNumber)); if (!Utils.AreAllNullOrEmpty(pCustomerFirstName, pCustomerLastName)) lCriteria.CreateCriteria(Customer.PropertyName_Person) .Add(Restrictions.Like(Person.PropertyName_FirstName, pCustomerFirstName, MatchMode.Anywhere)) .Add(Restrictions.Like(Person.PropertyName_LastName, pCustomerLastName, MatchMode.Anywhere)); return lCriteria.List(); } public IEnumerable FindCurrentSupportConcepts() { return CreateCriteriaIsActive() //.Add(Expression.Ge(SupportConcept.PropertyName_End, DateTime.Now)) .List(); } public IEnumerable FindSupportConceptsInSpan(DateTimeSpan pSpan) { return CreateCriteriaIsActive() //.Add(Expression.Disjunction() // .Add(Expression.Between(SupportConcept.PropertyName_Start, pSpan.StartDateTime, pSpan.EndDateTime)) // .Add(Expression.Between(SupportConcept.PropertyName_End, pSpan.StartDateTime, pSpan.EndDateTime)) // .Add(Expression.Conjunction() // .Add(Expression.Le(SupportConcept.PropertyName_Start, pSpan.StartDateTime)) // .Add(Expression.Ge(SupportConcept.PropertyName_End, pSpan.EndDateTime)) // ) // ) .List(); } public IEnumerable FindExpiringSupportConcepts(long? employeeOid, DateTime minDate, DateTime expiredUntil) { var c = CreateCriteriaIsActive(); if (employeeOid == null) { return c .CreateCriteria(SupportConcept.PropertyName_CostBearer2SupportConceptList, JoinType.InnerJoin) .Add(Restrictions.Between(CostBearer2SupportConcept.PropertyName_ApprovedEndDate, minDate, expiredUntil)) .List(); } return c .CreateAlias(SupportConcept.PropertyName_CostBearer2SupportConceptList, "cb2sc", JoinType.InnerJoin) .CreateAlias(SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .CreateAlias("c.Employee2CustomerList", "e2c", JoinType.InnerJoin) .Add(Restrictions.Eq("e2c." + Employee2Customer.PropertyName_EmployeeOid, employeeOid.Value)) .Add(Restrictions.Or( Restrictions.Between("cb2sc." + CostBearer2SupportConcept.PropertyName_ApprovedEndDate, minDate, expiredUntil), Restrictions.And(Restrictions.IsNotNull("c.TerminationDate"), Restrictions.Between("c.TerminationDate", minDate, expiredUntil)))) .List().Distinct().ToList(); } public IEnumerable FindSupportConceptsWithConferenceDate(long? employeeOid, DateTime conferenceDateUntil) { var c = CreateCriteriaIsActive(); if (employeeOid == null) { return c .Add(Restrictions.Between(SupportConcept.PropertyName_ConferenceDate, DateTime.Now, conferenceDateUntil)) .List(); } return c .CreateAlias(SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .CreateAlias("c.Employee2CustomerList", "e2c", JoinType.InnerJoin) .Add(Restrictions.Between(SupportConcept.PropertyName_ConferenceDate, DateTime.Now.Date, conferenceDateUntil)) .Add(Restrictions.Eq("e2c." + Employee2Customer.PropertyName_EmployeeOid, employeeOid.Value)) .List().Distinct().ToList(); } public ApplicationUser FindUserForEmployee(Employee employee) { return CreateCriteria() .Add(Restrictions.Eq(ApplicationUser.PropertyName_Employee, employee)) .List().FirstOrDefault(); } public IEnumerable FindPersonsHavingBirthday(DateTime birthdayUntil) { var ts = birthdayUntil.Subtract(DateTime.Now); var days = ts.Days + 1; return CreateCriteriaIsActive() .Add(Expression.Sql(new SqlString("dayofyear(CAST(CONCAT(Year(CURDATE()), '-', Month(" + Person.PropertyName_DateOfBirth + "), '-', Day(" + Person.PropertyName_DateOfBirth + ")) AS DATE )) between dayofyear(CURDATE()) -4 and dayofyear(CURDATE()) + " + days))) .List(); } public IEnumerable FindLastLogins(string loginName) { return CreateCriteria() .Add(Restrictions.Eq(Login.PropertyName_LoginName, loginName)) .AddOrder(Order.Desc("Oid")) .SetMaxResults(10) .List(); } public IEnumerable FindQuery(QueryType pType) { return CreateCriteria().Add(Restrictions.Eq(Query.PropertyName_Type, pType)) .List().ToList(); } public IList FindAccoutingTransactions(DateTimeSpan pSpan) { var lCriteria = CreateCriteriaIsActive(); if (pSpan != null) lCriteria.Add(Restrictions.Between(AccountingTransaction.PropertyName_BookingDate, pSpan.StartDateTime, pSpan.EndDateTime)); var test = lCriteria.List(); return test; } public IEnumerable FindAccoutingTransactions(DateTimeSpan pSpan, long? pSupportConceptOid, long? pSupportConceptCostBearerRelOid) { var lCriteria = CreateCriteriaIsActive() .CreateAlias(AccountingTransaction.PropertyName_CostBearer2SupportConcept, "cb2sc", JoinType.LeftOuterJoin); if (pSpan != null) lCriteria.Add(Restrictions.Between(AccountingTransaction.PropertyName_BookingDate, pSpan.StartDateTime, pSpan.EndDateTime)); if (pSupportConceptCostBearerRelOid != null) lCriteria.Add(Restrictions.Eq("cb2sc." + BeWoEntityBase.PropertyName_Oid, pSupportConceptCostBearerRelOid)); if (pSupportConceptOid != null) { lCriteria.CreateCriteria("cb2sc." + CostBearer2SupportConcept.PropertyName_SupportConcept) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pSupportConceptOid)); } var test = lCriteria.List(); return test; } public IList FindSettingsByValuePart(SettingsType pType, string pValuePart) { return CreateCriteria() .Add(Restrictions.Eq(Settings.PropertyName_Type, pType)) .Add(Restrictions.Like(Settings.PropertyName_Value, pValuePart, MatchMode.Anywhere)) .List(); } public IList FindFileAttachments(TableID pObjTid, long pObjOid) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(FileAttachment.PropertyName_ObjectOid, pObjOid)) .Add(Restrictions.Eq(FileAttachment.PropertyName_ObjectTid, pObjTid)) .List(); } public IEnumerable FindFileAttachmentInfos(TableID pObjTid, long pObjOid) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(FileAttachmentInfo.PropertyName_ObjectOid, pObjOid)) .Add(Restrictions.Eq(FileAttachmentInfo.PropertyName_ObjectTid, pObjTid)) .List(); } public IList FindBeWoFolders(TableID pObjTid, long pObjOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(BeWoFolder.PropertyName_ObjectTid, pObjTid)); if (pObjOid > 0) { c.Add(Restrictions.Eq(BeWoFolder.PropertyName_ObjectOid, pObjOid)); } return c.List(); } //public IList FindAllActiveApprovedLVRSupportConcepts() //{ // return CreateCriteriaIsActive() // .CreateCriteria(SupportConcept.PropertyName_CostBearer2SupportConceptList, JoinType.InnerJoin) // .Add(Expression.Eq(CostBearer2SupportConcept.PropertyName_Status, CostBearer2SupportConceptStatus.Approved)) // .CreateCriteria(CostBearer2SupportConcept.PropertyName_CostBearer, JoinType.InnerJoin) // .Add(Expression.Eq(CostBearer.PropertyName_SystemEntryID, SystemEntryID.CostBearerLVR)) // .List(); //} public IList FindAllActiveApprovedSupportConcepts() { return CreateCriteriaIsActive() .CreateCriteria(SupportConcept.PropertyName_CostBearer2SupportConceptList, JoinType.InnerJoin) .Add(Restrictions.Eq(CostBearer2SupportConcept.PropertyName_Status, CostBearer2SupportConceptStatus.Approved)) .List().Distinct().ToList(); } public Employee FindEmployeeWithPersonOid(long pPersonOid) { var person = DAOFactory.GenericDAO.LoadByID(pPersonOid); if (person != null) { return CreateCriteria() .Add(Restrictions.Eq(Employee.PropertyName_Person, person)).UniqueResult(); } return null; } public Customer FindCustomerWithPersonOid(long pPersonOid) { var person = DAOFactory.GenericDAO.LoadByID(pPersonOid); if (person != null) { return CreateCriteria() .Add(Restrictions.Eq(Customer.PropertyName_Person, person)).UniqueResult(); } return null; } public IList FindCustomersWithPersonOids(IEnumerable pPersonOid) { return CreateCriteriaIsActive() .CreateAlias(Customer.PropertyName_Person, "p", JoinType.InnerJoin) .Add(Restrictions.In("p." + BeWoEntityBase.PropertyName_Oid, pPersonOid.ToArray())) .List(); } public IList GetActivePersonsWithType(PersonType type) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(Person.PropertyName_Type, type)) .List(); } public IEnumerable GetAllActiveCustomersWithSupportConceptData() { return CreateCriteriaIsActiveOrArchived() .CreateAlias(Customer.PropertyName_SupportConcepts, "sc", JoinType.LeftOuterJoin) .CreateCriteria("sc." + SupportConcept.PropertyName_CostBearer2SupportConceptList, JoinType.LeftOuterJoin) .Add(Restrictions.Eq("sc." + SupportConcept.PropertyName_IsActive, ActivationTypeId.Active)) .List(); } public IEnumerable GetAllActiveCustomersWithAddress() { return CreateCriteriaIsActive() .CreateCriteria(Customer.PropertyName_Person, JoinType.LeftOuterJoin) .CreateCriteria(Person.PropertyName_Address, JoinType.LeftOuterJoin) .List(); } public IEnumerable GetAllCustomersWithRelatedEmployee() { return CreateCriteria() .CreateAlias("Person", "cp", JoinType.InnerJoin) .CreateAlias("cp." + Person.PropertyName_Address, "a", JoinType.LeftOuterJoin) .CreateAlias("Employee2CustomerList", "e2c", JoinType.LeftOuterJoin) .CreateAlias("e2c." + Employee2Customer.PropertyName_Employee, "e", JoinType.LeftOuterJoin) .CreateCriteria("e2c." + Employee2Customer.PropertyName_ValueList, JoinType.LeftOuterJoin) .CreateCriteria("e." + Employee.PropertyName_Person, JoinType.LeftOuterJoin) .List(); } public IList FindServiceRecordsDetailsForLastDays(long costbearer2SupportConcept, long? days) { var c = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_CostBearer2SupportConceptOid, costbearer2SupportConcept)); if (days.HasValue) { var minDate = DateTime.Now.Date.AddDays(-1 * days.Value); c.Add(Restrictions.Ge(ServiceRecord.PropertyName_Start, minDate)); } return c.CreateAlias(ServiceRecord.PropertyName_ValueList, "vl", JoinType.LeftOuterJoin) .CreateAlias(ServiceRecord.PropertyName_ServiceDescription, "sd", JoinType.InnerJoin) .CreateAlias("sd." + ServiceDescription.PropertyName_ServiceCategory, "sc", JoinType.InnerJoin) .CreateAlias(ServiceRecord.PropertyName_Employee, "e", JoinType.InnerJoin) .CreateCriteria("e." + Employee.PropertyName_Person, JoinType.InnerJoin) .List(); } public IList FindServiceRecordsDetailsForLastDaysWithStartEndDate(long costbearer2SupportConcept, DateTime start, DateTime ende) { var c = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_CostBearer2SupportConceptOid, costbearer2SupportConcept)); var max = ende.AddDays(1); c.Add(Restrictions.Ge(ServiceRecord.PropertyName_Start, start.Date)) .Add(Restrictions.Lt(ServiceRecord.PropertyName_End, max.Date)); return c.CreateAlias(ServiceRecord.PropertyName_ValueList, "vl", JoinType.LeftOuterJoin) .CreateAlias(ServiceRecord.PropertyName_ServiceDescription, "sd", JoinType.InnerJoin) .CreateAlias("sd." + ServiceDescription.PropertyName_ServiceCategory, "sc", JoinType.InnerJoin) .CreateAlias(ServiceRecord.PropertyName_Employee, "e", JoinType.InnerJoin) .CreateCriteria("e." + Employee.PropertyName_Person, JoinType.InnerJoin) .List(); } public IList FindServiceRecordsDetailWithServiceInfo(long costbearer2SupportConcept) { var c = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_CostBearer2SupportConceptOid, costbearer2SupportConcept)); return c.CreateAlias(ServiceRecord.PropertyName_ServiceDescription, "sd", JoinType.InnerJoin) .CreateAlias("sd." + ServiceDescription.PropertyName_ServiceCategory, "sc", JoinType.InnerJoin) .List(); } public Customer FindCustomerWithServiceRecordsWithDetail(long pCustomerOid) { return CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pCustomerOid)) .CreateAlias(Customer.PropertyName_ServiceRecordList, "sr", JoinType.InnerJoin) .CreateCriteria("sr." + ServiceRecord.PropertyName_ValueList, JoinType.LeftOuterJoin) .CreateAlias("sr." + ServiceRecord.PropertyName_ServiceDescription, "sd", JoinType.InnerJoin) .CreateCriteria("sd." + ServiceDescription.PropertyName_ServiceCategory, JoinType.InnerJoin) .CreateAlias("sr." + ServiceRecord.PropertyName_CostBearer2SupportConcept, "c2s", JoinType.InnerJoin) .CreateCriteria("c2s." + CostBearer2SupportConcept.PropertyName_SupportConcept, JoinType.InnerJoin) .CreateAlias("c2s." + CostBearer2SupportConcept.PropertyName_CostBearer, "cb", JoinType.InnerJoin) .CreateAlias("cb." + CostBearer.PropertyName_Organisation, "org", JoinType.InnerJoin) .UniqueResult(); } public ServiceRecordGroup FindServiceRecordGroup(long pGroupOid) { return CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pGroupOid)) .CreateCriteria(ServiceRecordGroup.PropertyName_ServiceRecordList, JoinType.InnerJoin) .UniqueResult(); } public IEnumerable GetAllActiveAndArchivedSupportConceptsCompact() { return CreateCriteria() .Add(Restrictions.Or(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active), Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Archived))) .CreateAlias(SupportConcept.PropertyName_CostBearer2SupportConceptList, "cb2sc", JoinType.LeftOuterJoin) .CreateAlias(SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .CreateAlias("c." + Customer.PropertyName_Person, "p", JoinType.InnerJoin) .CreateAlias("p." + Person.PropertyName_Address, "a", JoinType.LeftOuterJoin) .CreateAlias("c." + Customer.PropertyName_Employee2CustomerList, "e2c", JoinType.LeftOuterJoin) .List(); } public IEnumerable GetAllSupportConceptsCompact() { return CreateCriteria() .CreateAlias(SupportConcept.PropertyName_CostBearer2SupportConceptList, "cb2sc", JoinType.LeftOuterJoin) .CreateAlias(SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .CreateAlias("c." + Customer.PropertyName_Person, "p", JoinType.InnerJoin) .CreateAlias("p." + Person.PropertyName_Address, "a", JoinType.LeftOuterJoin) .CreateAlias("c." + Customer.PropertyName_Employee2CustomerList, "e2c", JoinType.LeftOuterJoin) .List(); } public IList GetAllActiveAndArchivedListedSupportConceptsCompact() { return Session.CreateCriteria() .List(); } public IList GetAllActiveAndArchivedListedSupportConceptsCompact(List oids) { return Session.CreateCriteria() .Add(Restrictions.In("SupportConceptOid", oids)) .List(); } public IList GetAllCustomerWithServiceRecordsInSpan(DateTimeSpan pSpan) { return CreateCriteria() .CreateCriteria(Customer.PropertyName_ServiceRecordList, JoinType.InnerJoin) .Add(Restrictions.Between(ServiceRecord.PropertyName_Start, pSpan.StartDateTime, pSpan.EndDateTime)) .List(); } public IEnumerable GetAllAccountingTransactionsWithDetails() { return CreateCriteria() .AddOrder(Order.Desc(AccountingTransaction.PropertyName_BookingDate)) .CreateAlias(AccountingTransaction.PropertyName_CostBearer2SupportConcept, "cb2sc", JoinType.LeftOuterJoin) .CreateCriteria("cb2sc." + CostBearer2SupportConcept.PropertyName_CostBearer, JoinType.LeftOuterJoin) .CreateCriteria(CostBearer.PropertyName_Organisation, JoinType.LeftOuterJoin) .CreateCriteria("cb2sc." + CostBearer2SupportConcept.PropertyName_SupportConcept, JoinType.LeftOuterJoin) .CreateCriteria(SupportConcept.PropertyName_Customer, JoinType.LeftOuterJoin) .CreateCriteria(Customer.PropertyName_Person, JoinType.LeftOuterJoin) .List(); } public IEnumerable FindAccountingTransactionsWithImportNotice(String notice) { return CreateCriteria() .Add(Restrictions.Eq(AccountingTransaction.PropertyName_ImportNotice, notice)).List(); } public IEnumerable GetAllActiveAndArchivedSupportConcepts() { return CreateCriteria() .Add(Restrictions.Or(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active), Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Archived))) .CreateAlias(SupportConcept.PropertyName_CostBearer2SupportConceptList, "cb2sc", JoinType.InnerJoin) .CreateAlias("cb2sc." + CostBearer2SupportConcept.PropertyName_CostBearer, "cb", JoinType.InnerJoin) .CreateAlias("cb." + CostBearer.PropertyName_Organisation, "org", JoinType.InnerJoin) .CreateAlias(SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .CreateAlias("c." + Customer.PropertyName_Person, "p", JoinType.InnerJoin) .List(); } public IEnumerable GetAllActiveAndArchivedCustomersWithRelatedEmployee() { return CreateCriteria() .Add(Restrictions.Or(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active), Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Archived))) .CreateAlias(Customer.PropertyName_Person, "cp", JoinType.InnerJoin) .CreateAlias("cp." + Person.PropertyName_Address, "a", JoinType.LeftOuterJoin) .CreateAlias(Customer.PropertyName_Employee2CustomerList, "e2c", JoinType.LeftOuterJoin) .CreateAlias("e2c." + Employee2Customer.PropertyName_Employee, "e", JoinType.LeftOuterJoin) .CreateCriteria("e2c." + Employee2Customer.PropertyName_ValueList, JoinType.LeftOuterJoin) .CreateCriteria("e." + Employee.PropertyName_Person, JoinType.LeftOuterJoin) .List(); } public IEnumerable GetSettlementInvoiceBySupportConceptOid(long supportConceptOid) { return CreateCriteriaIsActive() .CreateAlias(SettlementInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .Add(Restrictions.Eq("ib." + InvoiceBase.PropertyName_SupportConceptOid, supportConceptOid)) .List(); } public IList GetSettlementInvoiceByCostBearer2SupportConceptOid(long costBearer2SupportConceptOid) { return CreateCriteriaIsActive() .CreateAlias(SettlementInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .Add(Restrictions.Eq("ib." + InvoiceBase.PropertyName_CostBearer2SupportConceptOid, costBearer2SupportConceptOid)) .List(); } public IEnumerable GetSettlementInvoicesForCostBearerAndPeriod(long costBearerOid, DateTime periodStart, DateTime periodEnd) { return CreateCriteriaIsActive() .CreateAlias(SettlementInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .CreateAlias("ib." + InvoiceBase.PropertyName_CostBearer2SupportConcept, "cb2sc", JoinType.InnerJoin) .CreateAlias("cb2sc." + CostBearer2SupportConcept.PropertyName_CostBearer, "cb", JoinType.InnerJoin) .Add(Restrictions.Eq("cb." + BeWoEntityBase.PropertyName_Oid, costBearerOid)) .Add(Restrictions.Or( Restrictions.Between("ib." + InvoiceBase.PropertyName_AccountingPeriodStart, periodStart, periodEnd), Restrictions.Between("ib." + InvoiceBase.PropertyName_AccountingPeriodEnd, periodStart, periodEnd))) .List(); } public IList GetInvoiceBaseByTypeAndSupportConceptOid(InvoiceType type, long supportConceptOid) { return SearchInvoiceBase(type, InvoiceBase.PropertyName_SupportConceptOid, supportConceptOid); } public IEnumerable GetInvoiceBaseByOrganisationOid(InvoiceType type, long organisationOid) { return SearchInvoiceBase(type, InvoiceBase.PropertyName_RecipientOrganisationOid, organisationOid); } public IEnumerable GetInvoiceBaseByCustomerOid(InvoiceType type, long customerOid) { return SearchInvoiceBase(type, InvoiceBase.PropertyName_RecipientCustomerOid, customerOid); } public IEnumerable GetInvoiceBaseByPersonOid(InvoiceType type, long personOid) { return SearchInvoiceBase(type, InvoiceBase.PropertyName_RecipientPersonOid, personOid); } private IList SearchInvoiceBase(InvoiceType type, string propertyname, long fkOid) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(InvoiceBase.PropertyName_Type, type)) .Add(Restrictions.Eq(propertyname, fkOid)) .List(); } public SettlementInvoice GetSettlementInvoiceByInvoiceBaseOid(long invoiceBaseOid) { return CreateCriteriaIsActive() .CreateAlias(SettlementInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .Add(Restrictions.Eq("ib." + BeWoEntityBase.PropertyName_Oid, invoiceBaseOid)) .UniqueResult(); } public ServiceInvoice GetServiceInvoiceByInvoiceBaseOid(long invoiceBaseOid) { var res = CreateCriteriaIsActive() .CreateAlias(ServiceInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .Add(Restrictions.Eq("ib." + BeWoEntityBase.PropertyName_Oid, invoiceBaseOid)) .UniqueResult(); return res; } public IEnumerable GetAssessmentSheetEntriesForCustomer(long customerOid, DateTime startDt, DateTime endDt) { return CreateCriteriaIsActive() .CreateAlias(AssessmentSheetEntry.PropertyName_Customer, "c", JoinType.InnerJoin) .Add(Restrictions.Eq("c." + BeWoEntityBase.PropertyName_Oid, customerOid)) .Add(Restrictions.Ge(AssessmentSheetEntry.PropertyName_Day, startDt)) .Add(Restrictions.Lt(AssessmentSheetEntry.PropertyName_Day, endDt)) .List(); } public IList GetVarFieldDefs(TableID tid) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(VarFieldDef.PropertyName_ObjectTid, tid)) .List(); } public IEnumerable FindTasksForEmployee(long employeeoid) { return CreateCriteriaIsActive() .CreateAlias(Task.PropertyName_SupportConcept, "sc", JoinType.InnerJoin) .CreateAlias("sc." + SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .CreateCriteria(Task.PropertyName_EmployeeList, JoinType.InnerJoin) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, employeeoid)) .Add(Restrictions.Eq("sc." + BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .Add(Restrictions.Eq("c." + BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .List(); } public List GetServiceInvoices(long costBearerOid, long? supportConceptOid, DateTimeSpan period) { var criteria = CreateCriteriaIsActive() .CreateAlias(ServiceInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .Add(Restrictions.Eq("ib." + InvoiceBase.PropertyName_RecipientCostBearerOid, costBearerOid)) .Add(Restrictions.Eq("ib." + BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)); if (supportConceptOid.HasValue) criteria = criteria .Add(Restrictions.Eq("ib." + InvoiceBase.PropertyName_SupportConceptOid, supportConceptOid.Value)); if (period != null) criteria = criteria .Add(Restrictions.Or( Restrictions.Or( Restrictions.Between("ib." + InvoiceBase.PropertyName_AccountingPeriodStart, period.StartDateTime, period.EndDateTime), Restrictions.Between("ib." + InvoiceBase.PropertyName_AccountingPeriodEnd, period.StartDateTime, period.EndDateTime)), Restrictions.And( Restrictions.Le("ib." + InvoiceBase.PropertyName_AccountingPeriodStart, period.StartDateTime), Restrictions.Ge("ib." + InvoiceBase.PropertyName_AccountingPeriodEnd, period.EndDateTime)))); return criteria.List().ToList(); } public List GetServiceInvoices(long? costbearer2SupportConceptOid, DateTimeSpan period) { var criteria = CreateCriteriaIsActive() .CreateAlias(ServiceInvoice.PropertyName_InvoiceBase, "ib", JoinType.InnerJoin) .Add(Restrictions.Eq("ib." + BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)); if (costbearer2SupportConceptOid.HasValue) criteria = criteria .Add(Restrictions.Eq("ib." + InvoiceBase.PropertyName_CostBearer2SupportConceptOid, costbearer2SupportConceptOid.Value)); if (period != null) criteria = criteria .Add(Restrictions.Or( Restrictions.Or( Restrictions.Between("ib." + InvoiceBase.PropertyName_AccountingPeriodStart, period.StartDateTime, period.EndDateTime), Restrictions.Between("ib." + InvoiceBase.PropertyName_AccountingPeriodEnd, period.StartDateTime, period.EndDateTime)), Restrictions.And( Restrictions.Le("ib." + InvoiceBase.PropertyName_AccountingPeriodStart, period.StartDateTime), Restrictions.Ge("ib." + InvoiceBase.PropertyName_AccountingPeriodEnd, period.EndDateTime)))); return criteria.List().ToList(); } public IEnumerable GetAssessmentSheetCategoryListForCustomer(long customerOid) { return CreateCriteriaIsActive() .CreateAlias(AssessmentSheetCategory.PropertyName_Customers, "c", JoinType.InnerJoin) .Add(Restrictions.Eq("c." + BeWoEntityBase.PropertyName_Oid, customerOid)) .List(); } public IEnumerable FindAbsenceTimes(bool fetchEmployees, bool fetchCustomers) { var criteria = CreateCriteriaIsActive(); if (!fetchEmployees) criteria = criteria .Add(Restrictions.IsNull(AbsenceTime.PropertyName_EmployeeOid)); else if (!fetchCustomers) criteria = criteria .Add(Restrictions.IsNull(AbsenceTime.PropertyName_CustomerOid)); return criteria.List().ToList(); } public IEnumerable FindAbsenceTimes(DateTime startDate, DateTime endDate) { var criteria = CreateCriteriaIsActive() .Add( Restrictions.Or( Restrictions.And( Restrictions.Ge(AbsenceTime.PropertyName_End, startDate), Restrictions.Le(AbsenceTime.PropertyName_Start, endDate) ), Restrictions.And( Restrictions.Le(AbsenceTime.PropertyName_Start, endDate), Restrictions.IsNull(AbsenceTime.PropertyName_End) ) ) ); return criteria.List().ToList(); } public IEnumerable FindVertretungen(DateTime startDate, DateTime endDate) { var criteria = CreateCriteriaIsActive() .Add( Restrictions.Or( Restrictions.And( Restrictions.Ge(Vertretung.PropertyName_VertretungsZeitraumBis, startDate), Restrictions.Le(Vertretung.PropertyName_VertretungsZeitraumVon, endDate) ), Restrictions.And( Restrictions.Le(Vertretung.PropertyName_VertretungsZeitraumVon, endDate), Restrictions.IsNull(Vertretung.PropertyName_VertretungsZeitraumBis) ) ) ); return criteria.List().ToList(); } public IEnumerable FindAppointments(long? customerOid, long? employeeOid) { var criteria = CreateCriteriaIsActive(); if(employeeOid.HasValue) { criteria = criteria.Add(Restrictions.Eq(AbsenceTime.PropertyName_EmployeeOid, employeeOid)); } if(customerOid.HasValue) { criteria = criteria.Add(Restrictions.Eq(AbsenceTime.PropertyName_CustomerOid, customerOid)); } return criteria.List().ToList(); } public ServiceCategory FindDefaultIndividualServiceCategory() { return CreateCriteriaIsActive() .Add(Restrictions.Eq(ServiceCategory.PropertyName_ScopeType, ScopeTypeId.Individual)) .List().FirstOrDefault(); } public ServiceCategory FindServiceCategoryByName(String name) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(ServiceCategory.PropertyName_Name, name)) .List().FirstOrDefault(); } public IEnumerable FindFileAttachmentInfoByType(FileAttachmentType type) { var criteria = CreateCriteria() .Add(Restrictions.Eq("Type", type)); return criteria.List().ToList(); } public IEnumerable GetAdditionalAssessmentSheetEntries(DateTime dateTime) { return CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_SystemEntryID, SystemEntryID.AdditionalServiceAssessmentSheet)) .Add(Restrictions.Eq(AssessmentSheetEntry.PropertyName_Day, dateTime.Date)) .List(); } public IEnumerable GetResourceAppointmentExceptionsWithIndexAndId(string info) { return CreateCriteria() .Add(Restrictions.Like(ResourceAppointment.PropertyName_RecurrenceInfo, info)) .List(); } public IEnumerable GetAdditionalServiceBookings(long? regionOid, DateTime start, DateTime end) { var criteria = CreateCriteriaIsActive() .Add(Restrictions.Between("Datum", start, end)); if (regionOid.HasValue) criteria = criteria.CreateAlias(AdditionalServiceBooking.PropertyName_AdditionalServiceRegion, "asr", JoinType.InnerJoin) .Add(Restrictions.Eq("asr." + BeWoEntityBase.PropertyName_Oid, regionOid)); return criteria.List(); } public IEnumerable GetAdditionalServiceBookingsForCustomer(long customerOid, DateTime start, DateTime end) { return CreateCriteriaIsActive() .CreateAlias(AdditionalServiceBooking.PropertyName_Customer2AddServiceBookings, "c2s", JoinType.InnerJoin) .Add(Restrictions.Between("Datum", start, end)) .Add(Restrictions.Eq("c2s.CustomerOid", customerOid)) .List(); } public AssessmentSheetCategory GetAddServiceAssessmentSheetCategoryWithName(string name) { return CreateCriteriaIsActive() .Add(Restrictions.Eq(AssessmentSheetCategory.PropertyName_Description, name)) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_SystemEntryID, SystemEntryID.AdditionalServiceAssessmentSheet)) .List().FirstOrDefault(); } public IList GetBillableServiceRecords() { var c = CreateCriteria() .CreateAlias(ServiceRecord.PropertyName_ServiceDescription, "sd", JoinType.InnerJoin) .CreateAlias("sd." + ServiceDescription.PropertyName_ServiceCategory, "sc", JoinType.InnerJoin) .Add(Restrictions.Eq("sc." + ServiceCategory.PropertyName_IsBillable, true)); return c.List(); } public IList GetBillableServiceRecords(DateTime start, DateTime end) { var c = CreateCriteria() .CreateAlias(ServiceRecord.PropertyName_ServiceDescription, "sd", JoinType.InnerJoin) .CreateAlias("sd." + ServiceDescription.PropertyName_ServiceCategory, "sc", JoinType.InnerJoin) .Add(Restrictions.Eq("sc." + ServiceCategory.PropertyName_IsBillable, true)) .Add(Restrictions.Between(ServiceRecord.PropertyName_Start, start, end)); return c.List(); } public IEnumerable FindServiceDescriptionForCategory(long categoryOid) { var c = CreateCriteriaIsActive() .CreateAlias(ServiceDescription.PropertyName_ServiceCategory, "sc", JoinType.InnerJoin) .Add(Restrictions.Eq("sc."+ BeWoEntityBase.PropertyName_Oid, categoryOid)); return c.List(); } public IEnumerable FindServiceAccountingsForServiceDescriptions(IList descriptionOids) { var c = CreateCriteriaIsActive() .CreateAlias("ServiceDescription", "sd", JoinType.InnerJoin) .Add(Restrictions.In("sd." + BeWoEntityBase.PropertyName_Oid, descriptionOids.ToArray())); return c.List(); } public IList GetAllMedikamentenverordnungslistenButNewestByCustomerOid(long customerOid, long newestOid, bool isBedarfsListe) { var c = CreateCriteria() .CreateAlias(Medikamentenverordnungsliste.PropertyName_Customer, "c") .Add(Restrictions.Eq("c." + BeWoEntityBase.PropertyName_Oid, customerOid)) .Add(Restrictions.Not(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, newestOid))) .Add(Restrictions.Eq(Medikamentenverordnungsliste.PropertyName_MedListType, isBedarfsListe)); return c.List(); } public IList GetAllOpenAppointmentsForEmployee(long employeeOid) { var c = CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .Add(Restrictions.Not(Restrictions.Eq(SchedulerAppointment.PropertyName_Originator + ".Oid", employeeOid))) .CreateCriteria(SchedulerAppointment.PropertyName_EmployeeList) .Add(Restrictions.And(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", employeeOid), Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_ParticipationAnswer, ParticipationAnswer.Offen))); return c.List(); } public IEnumerable GetAllParticipationNotificationsForEmployee(long employeeOid) { var c = CreateCriteria() .Add(Restrictions.And(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active), Restrictions.Eq(SchedulerAppointment.PropertyName_Originator + ".Oid", employeeOid))) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .CreateCriteria(SchedulerAppointment.PropertyName_EmployeeList) .Add(Restrictions.And(Restrictions.And(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_IsPChanged, true), Restrictions.Not(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", employeeOid))), Restrictions.Not(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_ParticipationAnswer, ParticipationAnswer.Offen)))) .Add(Restrictions.Not(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_ParticipationAnswer, ParticipationAnswer.Verstrichen))) .Add(Restrictions.IsNull(Employee2SchedulerAppointment.PropertyName_IsPC_CheckedTs)); var result = c.List(); return result; } public IEnumerable GetRemovedEmp2AppObjectsBySchAppOid(long schAppOid) { var c = CreateCriteria() .Add(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment, schAppOid)); return c.List(); } public IEnumerable GetAllActiveAndNotDeclinedForEmployeeAppointments(long employeeOid) { var detachedCriteria = DetachedCriteria.For() .Add(Restrictions.And(Restrictions.Not(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", employeeOid)), Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_ParticipationAnswer, ParticipationAnswer.Absage))); detachedCriteria.SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment)); var crit = CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .Add(Restrictions.Or(Restrictions.Eq(SchedulerAppointment.PropertyName_Originator + ".Oid", employeeOid), Subqueries.PropertyIn(BeWoEntityBase.PropertyName_Oid, detachedCriteria))); return crit.List(); } public bool ResetPasswordCodeExists(string code, long userOid) { var c = CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, userOid)) .CreateCriteria(ApplicationUser.PropertyName_ResetPasswordInfos) .Add(Restrictions.Eq(ResetPasswordInfo.PropertyName_Code, code)); return c.List().Count > 0; } public bool OverlappingAppointmentsExist(DateTime start, DateTime end, IEnumerable employees, IEnumerable customers, IEnumerable resources, long originator, long? appointmentOid, string recurrenceId = "", int occurrenceIndex = 0) { var criteria = CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .Add(Restrictions.Or( Restrictions.Or( Restrictions.And( Restrictions.Gt(SchedulerAppointment.PropertyName_EndDate, start), Restrictions.Lt(SchedulerAppointment.PropertyName_EndDate, end) ), Restrictions.And( Restrictions.Gt(SchedulerAppointment.PropertyName_StartDate, start), Restrictions.Lt(SchedulerAppointment.PropertyName_StartDate, end) ) ), Restrictions.Or( Restrictions.And( Restrictions.Lt(SchedulerAppointment.PropertyName_StartDate, start), Restrictions.Gt(SchedulerAppointment.PropertyName_EndDate, start) ), Restrictions.And( Restrictions.Eq(SchedulerAppointment.PropertyName_StartDate, start), Restrictions.Eq(SchedulerAppointment.PropertyName_EndDate, end) ) ) ) ); if (appointmentOid != null) { criteria.Add(Restrictions.Not(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, appointmentOid))); } var appTmp = criteria.List(); var appointments = appTmp.Where(a => { if (a.RecurrenceInfo == null) { return true; } var recId = GetSpecificValueFromRecurrenceInfo(a.RecurrenceInfo, a.RecurrenceInfo.IndexOf("Id=\"") + 4); var index = GetSpecificValueFromRecurrenceInfo(a.RecurrenceInfo, a.RecurrenceInfo.IndexOf("Index=\"") + 7); int.TryParse(index, out var intIndex); return !(recurrenceId.Equals(recId) && occurrenceIndex == intIndex); }); var schedulerAppointments = appointments as IList ?? appointments.ToList(); var oidList = new List(); foreach (var appointment in schedulerAppointments) { oidList.AddRange(appointment.EmployeeList.Select(rel => rel.ParticipationAnswer != ParticipationAnswer.Absage && rel.Employee.Oid != null ? rel.Employee.Oid.Value : 0)); } var emp = oidList.Intersect(employees).Any(); var cus = schedulerAppointments.Any(app => app.CustomerList.Select(c => c.Oid ?? 0).Intersect(customers).Any()); var res = schedulerAppointments.Any(app => app.ResourceList.Select(r => r.Oid ?? 0).Intersect(resources).Any()); var ori = schedulerAppointments.Any(app => app.Originator.Oid.HasValue && app.Originator.Oid.Value == originator && (app.EmployeeList.Count == 0 || app.EmployeeList.Any(a => a.Employee.Oid.HasValue && a.Employee.Oid.Value == originator))); return emp || cus || res || ori; } public IEnumerable GetAllActiveAppointmentsForEmployeeInInterval(DateTime start, DateTime end, long pEmployeeOid) { var hasResources = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM resource2newschapp)"; var criteria = CreateRecurrenceCriteria(start, end) .Add(Restrictions.Or( Expression.Sql(new SqlString(hasResources)), CreateOwnAppointmentsCriteria(pEmployeeOid))); return criteria.List(); } public IEnumerable GetAllActiveAppointmentsInInterval(DateTime start, DateTime end) { return CreateRecurrenceCriteria(start, end).List(); } private static string GetSpecificValueFromRecurrenceInfo(string str, int startIndex) { var valueBuilder = new StringBuilder(); for (; startIndex < str.Length; startIndex++) { var cr = str[startIndex]; if (cr.Equals('"')) break; valueBuilder.Append(cr); } return valueBuilder.ToString(); } public ResourceBooking GetFirstResourceBookingFromSequence(long sequenceOid) { return CreateCriteria() .Add(Restrictions.Eq(ResourceBooking.PropertyName_SequencePosition, 0)) .CreateAlias(ResourceBooking.PropertyName_Sequence, "s") .Add(Restrictions.Eq("s." + BeWoEntityBase.PropertyName_Oid, sequenceOid)).UniqueResult(); } public List GetExcludedBookingSequencePositions(long sequenceOid) { var c = CreateCriteria() .Add(Restrictions.Gt(ResourceBooking.PropertyName_SequencePosition, 0)) .CreateAlias(ResourceBooking.PropertyName_Sequence, "s") .Add(Restrictions.Eq("s." + BeWoEntityBase.PropertyName_Oid, sequenceOid)).List(); return c.Select(s => s.SequencePosition).ToList(); } public IEnumerable GetInvoiceBases(DateTime? startDate, DateTime? endDate, bool useInvoiceDate = true) { var lCriteria = CreateCriteriaIsActive(); if (useInvoiceDate) { if (startDate.HasValue) lCriteria.Add(Restrictions.Ge(InvoiceBase.PropertyName_InvoiceDate, startDate)); if (endDate.HasValue) lCriteria.Add(Restrictions.Le(InvoiceBase.PropertyName_InvoiceDate, endDate)); } else { if (startDate.HasValue) lCriteria.Add(Restrictions.Ge(InvoiceBase.PropertyName_AccountingPeriodEnd, startDate)); if (endDate.HasValue) lCriteria.Add(Restrictions.Le(InvoiceBase.PropertyName_AccountingPeriodStart, endDate)); } return lCriteria.List(); } public IEnumerable GetAllActiveSupportConceptsByCustomers(List customerOids, bool expiredOnesToo = false) { var c = CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .CreateAlias(SupportConcept.PropertyName_Customer, "c") .Add(Restrictions.In("c." + BeWoEntityBase.PropertyName_Oid, customerOids)); if (expiredOnesToo) { } return c.List(); } public IEnumerable FindServiceRecordsForDays(long? costBearer2SupportConceptOid, int dayCount, long? employeeOid) { var minStart = DateTime.Now.GetShortDateTime().AddDays(-dayCount); var c = CreateCriteriaIsActive(); if (costBearer2SupportConceptOid.HasValue) { c.Add(Restrictions.Eq(ServiceRecord.PropertyName_CostBearer2SupportConceptOid, costBearer2SupportConceptOid)); } else { c.Add(Restrictions.Eq(ServiceRecord.PropertyName_EmployeeOid, employeeOid)) .Add(Restrictions.IsNull(ServiceRecord.PropertyName_CostBearer2SupportConceptOid)); } if (dayCount > 0) { c.Add(Restrictions.Ge(ServiceRecord.PropertyName_Start, minStart)); } return c.List(); } public IEnumerable GetActiveTextModuleByServiceCategory(long pServiceCategoryOid) { var c = CreateCriteriaIsActive(); c.Add(Restrictions.Or(Restrictions.Eq(TextModule.PropertyName_ServiceCategory + ".Oid", pServiceCategoryOid), Restrictions.IsNull(TextModule.PropertyName_ServiceCategory))); return c.List(); } public IEnumerable GetAllActiveAppointmentsForEmployeeInInterval2(DateTime start, DateTime end, List pEmployeeOids) { var mainCriteria = CreateRecurrenceCriteria(start, end); var detachedCriteria1 = DetachedCriteria.For() .Add(Restrictions.In(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", pEmployeeOids)) .SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment)); var employee2SchedCrit = Subqueries.PropertyIn(BeWoEntityBase.PropertyName_Oid, detachedCriteria1); var originatorCrit = Restrictions.In(SchedulerAppointment.PropertyName_Originator, pEmployeeOids); var detachedCriteria2 = DetachedCriteria.For("e2s2") .SetProjection(Projections.Property(BeWoEntityBase.PropertyName_Oid)) .Add(Restrictions.EqProperty("e2s2." + Employee2SchedulerAppointment.PropertyName_SchedulerAppointment, "sa.Oid")); var employee2SchedCrit2 = Subqueries.NotExists(detachedCriteria2); var and = Restrictions.And(originatorCrit, employee2SchedCrit2); ICriterion employeeCriterion = Restrictions.Or(employee2SchedCrit, and); mainCriteria.Add(employeeCriterion); var appointments = mainCriteria.List(); var exceptionalRecurrenceInfos = appointments.Where(w => w.RecurrenceInfo != null && w.Type == 3).ToList(); var exceptionIds = new List(); var regex = new Regex("(Id=\\\"[a-z0-9-]+\\\")"); foreach(var appointment in exceptionalRecurrenceInfos) { var match = regex.Match(appointment.RecurrenceInfo); if(match.Success) { var value = match.Value; var actualId = value.Split("\""); if(actualId.Count > 1) { var id = actualId[1]; if(!appointments.Any(w => w.RecurrenceInfo != null && w.RecurrenceInfo.Contains(id) && w.Type == 1)) { exceptionIds.Add(appointment.Oid.Value); } } } } var exceptionsToModify = appointments.Where(w => w.Oid.HasValue && exceptionIds.Contains(w.Oid.Value)).ToList(); foreach(var exception in exceptionsToModify) { var replacingAppointment = new SchedulerAppointment { Oid = exception.Oid.Value * -1, Version = 1, Type = 4, RecurrenceInfo = exception.RecurrenceInfo, Originator = exception.Originator, EmployeeList = exception.EmployeeList, CustomerList = exception.CustomerList, ResourceList = exception.ResourceList }; exception.RecurrenceInfo = null; exception.Type = 0; appointments.Add(replacingAppointment); } var recurrenceIds = new List(); var allExceptionalRecurrenceInfos = appointments.Where(w => w.RecurrenceInfo != null); foreach(var appointment in allExceptionalRecurrenceInfos) { var match = regex.Match(appointment.RecurrenceInfo); if(match.Success) { var value = match.Value; var actualId = value.Split("\""); if(actualId.Count > 1) { recurrenceIds.AddIfNotIn(actualId[1]); } } } var changedOrDeletedOccurences = FindAppointmentsByRecurrenceId(recurrenceIds, true); appointments.AddRangeIfElementsNotIn(changedOrDeletedOccurences); return appointments; } public IEnumerable FilterEmployeesWithAppointments(List pEmployeeOids, DateTime pStartTime, DateTime pEndTime) { var result = new List(); var appointments = GetAllActiveAppointmentsForEmployeeInInterval2(pStartTime, pEndTime, pEmployeeOids); var gefilterteTermine = appointments.Where(w => w.EmployeeList.Select(s => s.Employee.Oid.Value).Intersect(pEmployeeOids).Any() || !w.EmployeeList.Select(s => s.Employee.Oid.Value).Intersect(pEmployeeOids).Any() && pEmployeeOids.Contains(w.Originator.Oid.Value)); var employee2SchedulerAppointmentsList = gefilterteTermine.Select(s => s.EmployeeList).ToList(); foreach(var employee2SchedulerAppointments in employee2SchedulerAppointmentsList) { foreach(var employee2SchedulerAppointment in employee2SchedulerAppointments) { if(employee2SchedulerAppointment.Employee.Oid.HasValue && !result.Contains(employee2SchedulerAppointment.Employee.Oid.Value)) { result.Add(employee2SchedulerAppointment.Employee.Oid.Value); } } } foreach(var originator in gefilterteTermine.Select(s => s.Originator)) { if(originator.Oid.HasValue && !result.Contains(originator.Oid.Value)) { result.Add(originator.Oid.Value); } } var c = CreateCriteria() .Add(Restrictions.In(AbsenceTime.PropertyName_EmployeeOid, pEmployeeOids)) .Add(Restrictions.Or( Restrictions.Or( Restrictions.Or( Restrictions.And( Restrictions.Eq(AbsenceTime.PropertyName_Start, pStartTime), Restrictions.Eq(AbsenceTime.PropertyName_End, pEndTime)), Restrictions.And( Restrictions.And( Restrictions.Lt(AbsenceTime.PropertyName_Start, pStartTime), Restrictions.Lt(AbsenceTime.PropertyName_End, pStartTime)), Restrictions.Gt(AbsenceTime.PropertyName_End, pEndTime))), Restrictions.And( Restrictions.Lt(AbsenceTime.PropertyName_Start, pStartTime), Restrictions.Gt(AbsenceTime.PropertyName_End, pStartTime))), Restrictions.And( Restrictions.Gt(AbsenceTime.PropertyName_Start, pStartTime), Restrictions.Lt(AbsenceTime.PropertyName_End, pEndTime)))); var abwesenheiten = c.List(); foreach(var abwesenheit in abwesenheiten) { result.AddIfNotIn(abwesenheit.EmployeeOid.Value); } return result; } public IEnumerable GetTasksForEmployeeByDate(long employeeOid, DateTime date){ var c = CreateCriteria().Add(Restrictions.Between(Task.PropertyName_DueDate, date, date.AddDays(1))). CreateCriteria(Task.PropertyName_EmployeeList, JoinType.InnerJoin) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, employeeOid)).List(); return c; } public Wohnheimbuchung GetWohnheimbuchungByWohnheimAndBuchungsdatum(long pWohnheimOid, DateTime pBuchungsdatum) { var c = CreateCriteriaIsActive(); c.Add(Restrictions.Eq(Wohnheimbuchung.PropertyName_Buchungsdatum, pBuchungsdatum)) .Add(Restrictions.Eq(Format("{0}.Oid", Wohnheimbuchung.PropertyName_Wohnheim), pWohnheimOid)); return c.UniqueResult(); } public List FindWohnheimbuchungsServiceRecords(long pWohnheimbuchungsOid) { var c = CreateCriteria() .Add(Restrictions.Eq(ServiceRecord.PropertyName_WohnheimbuchungsOid, pWohnheimbuchungsOid)); return c.List().ToList(); } public bool CheckIfUserGroupIsLastWithUserGroupEditingRights(List pUserGroupOids) { var dc = DetachedCriteria.For() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .SetProjection(Projections.Property(BeWoEntityBase.PropertyName_Oid)); var c = CreateCriteriaIsActive() .Add(Restrictions.Not(Restrictions.In(RightRelation.PropertyName_UserGroupOid, pUserGroupOids))) .Add(Restrictions.In(RightRelation.PropertyName_RightType, new[] { UserRightType.UserGroupView_View, UserRightType.UserGroupView_Edit, UserRightType.ViewAll, UserRightType.EditAll })) .Add(Subqueries.PropertyIn(RightRelation.PropertyName_UserGroupOid, dc)); var result = c.List(); var nachUserGroupOidSortiert = new Dictionary>(); foreach (var relation in result) { nachUserGroupOidSortiert.AddOrUpdateValueInDictionary(relation.UserGroupOid.Value, relation.RightType); } var minRights2 = nachUserGroupOidSortiert.Any(intersection => intersection.Value.Contains(UserRightType.UserGroupView_Edit) && intersection.Value.Contains(UserRightType.ViewAll) || intersection.Value.Contains(UserRightType.UserGroupView_Edit) && intersection.Value.Contains(UserRightType.UserGroupView_View) || intersection.Value.Contains(UserRightType.EditAll) && intersection.Value.Contains(UserRightType.ViewAll) || intersection.Value.Contains(UserRightType.EditAll) && intersection.Value.Contains(UserRightType.UserGroupView_View)); return minRights2 == false; } public ServiceDescription FindServiceDescriptionForWohnheimbuchungsServiceRecord(long pWohnheimbuchungsOid) { var dc = DetachedCriteria.For() .Add(Restrictions.Eq(ServiceRecord.PropertyName_WohnheimbuchungsOid, pWohnheimbuchungsOid)) .SetProjection(Projections.Property(ServiceRecord.PropertyName_ServiceDescription + ".Oid")); var c = CreateCriteriaIsActive() .Add(Subqueries.PropertyIn(BeWoEntityBase.PropertyName_Oid, dc)).SetMaxResults(1); return c.List().FirstOrDefault(); } public IEnumerable FindBargeldkasse(TableID objectTid, long objectOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(Bargeldkasse.PropertyName_ObjectTid, objectTid)) .Add(Restrictions.Eq(Bargeldkasse.PropertyName_ObjectOid, objectOid)); return c.List().ToList(); } public IList FindNotizenKategorien(TableID objectTid, long objectOid) { var c = CreateCriteriaIsActiveOrArchived() .Add(Restrictions.Eq(NotizenKategorie.PropertyName_ObjectTid, objectTid)) .Add(Restrictions.Eq(NotizenKategorie.PropertyName_ObjectOid, objectOid)); return c.List().ToList(); } public IEnumerable FindMedRecordsForCustomer(long pCustomerOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(MedRecord.PropertyName_Customer + ".Oid", pCustomerOid)); return c.List().ToList(); } public IEnumerable FindAllChatActiveCustomers() { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(CustomerAPPCode.PropertyName_IsChatAktiv, 1)); var codes = c.List().Select(s => s.CustomerOid.Value).ToArray(); var c2 = CreateCriteriaIsActive() .Add(Restrictions.In(BeWoEntityBase.PropertyName_Oid, codes)); return c2.List(); } public IEnumerable FindAllChatpartnerEmployee2Customer(long pRecipientOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(Employee2Customer.PropertyName_EmployeeOid, pRecipientOid)) .Add(Restrictions.Eq(Employee2Customer.PropertyName_Chatpartner,true)); return c.List(); } public IEnumerable FindAllChatMessages(long senderOid, long empfaengerOid,int maxNachrichten) { var c = CreateCriteriaIsActive() .Add( Restrictions.Or( Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, senderOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, empfaengerOid)), Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, empfaengerOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, senderOid)))) .Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)) .AddOrder(Order.Desc(ChatMessage.PropertyName_Uhrzeit)) .SetMaxResults(maxNachrichten); return c.List(); } public IEnumerable FindNextChatMessages(long senderOid, long empfaengerOid, int messlateZahl, List list,bool isteam ) { var c = CreateCriteriaIsActive(); if (!isteam) { c.Add( Restrictions.Or( Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, senderOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, empfaengerOid)), Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, empfaengerOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, senderOid)))).Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)); } else { c.Add(Restrictions.Eq(ChatMessage.PropertyName_TeamOid, empfaengerOid)); } c.Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, list))); c.AddOrder(Order.Desc(ChatMessage.PropertyName_Uhrzeit)); c.SetMaxResults(messlateZahl); return c.List(); } public int CountChatMessages(int aktuelleZahl, long senderOid, long empfaengerOid,bool isTeam) { //Zähle hier alle ChatMessages int xc; if (!isTeam) { xc = Session.QueryOver() .Where( message => message.SenderPersonOid == senderOid && message.EmpfaengerPersonOid == empfaengerOid || message.SenderPersonOid == empfaengerOid && message.EmpfaengerPersonOid == senderOid) .RowCount(); } else { xc = Session.QueryOver() .Where( message => message.TeamOid == empfaengerOid) .RowCount(); } int ergebnis = xc - aktuelleZahl; if (ergebnis < 0) ergebnis = 0; return ergebnis; } public IEnumerable FindAllChatMessagesFromTeam(long senderOid, long teamOid, int messlateZahl) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatMessage.PropertyName_TeamOid, teamOid)); c.AddOrder(Order.Desc(ChatMessage.PropertyName_Uhrzeit)); c.SetMaxResults(messlateZahl); return c.List(); } public IEnumerable FindAllEmpfängerChatMessages(long senderOid, long empfaengerOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, senderOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, empfaengerOid))); return c.List(); } public IEnumerable FindEmpfängerChatMessages(long senderOid, long empfaengerOid,string messageOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, senderOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, empfaengerOid))); c.Add(Restrictions.Eq(ChatMessage.PropertyName_MessageId, messageOid)); return c.List(); } public IEnumerable FindAllChatActiveEmployees() { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(EmployeeAPPCode.PropertyName_IsChatAktiv, 1)); var codes = c.List().Select(s => s.EmployeeOid.Value).ToArray(); var c2 = CreateCriteriaIsActive() .Add(Restrictions.In(BeWoEntityBase.PropertyName_Oid, codes)); return c2.List(); } public bool CheckIfEmployeeIsAllowedToChat(long pPersonOid) { var employee = FindEmployeeWithPersonOid(pPersonOid); var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(EmployeeAPPCode.PropertyName_IsChatAktiv, 1)) .Add(Restrictions.Eq(EmployeeAPPCode.PropertyName_EmployeeOid, employee.Oid)); var codes = c.List().ToArray(); return codes.Length > 0; } public IEnumerable FindAllEmployeeAppCodes(long employeeoid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(EmployeeAPPCode.PropertyName_EmployeeOid, employeeoid)); return c.List(); } public IEnumerable FindAllCustomerAppCodes(long customeroid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(CustomerAPPCode.PropertyName_CustomerOid, customeroid)); return c.List(); } public IEnumerable FindAllCustomerAppCodeBenutzer(string benutzername) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(CustomerAPPCode.PropertyName_Benutzername, benutzername)); return c.List(); } public IEnumerable FindAllChatMessagesForAndroid(long pSenderOid, long pRecipientOid, List pExceptions, bool pForTeam) { var c = CreateCriteriaIsActive() .Add( Restrictions.Or( Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pSenderOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pRecipientOid)), Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pRecipientOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pSenderOid)))) .Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))) .Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)); if(pForTeam) { c = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatMessage.PropertyName_TeamOid, pRecipientOid)) .Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))); } return c.List(); } //public IEnumerable FindAllUnreadChatMessages(List pExceptions) //{ // var c = CreateCriteriaIsActive() // .Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))); // return c.List(); //} public Dictionary FindAllChatAuthorizedPersonsForEmployee(long pEmployeeOid) { var result = new Dictionary(); var employee = CreateCriteriaIsActive().Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pEmployeeOid)).UniqueResult(); var relatedCustomerOids = employee.Employee2CustomerList.Select(s => s.CustomerOid.Value); var c1 = CreateCriteriaIsActive() .Add(Restrictions.Eq(CustomerAPPCode.PropertyName_IsChatAktiv, 1)); var codes = c1.List().Select(s => s.CustomerOid.Value).ToArray(); var c2 = CreateCriteriaIsActive() .Add(Restrictions.In(BeWoEntityBase.PropertyName_Oid, codes)) .Add(Restrictions.In(BeWoEntityBase.PropertyName_Oid, relatedCustomerOids.ToList())); var customers = c2.List(); var employees = FindAllChatActiveEmployees(); employees.DoForEach(d => result.Add(d.Person, true)); customers.DoForEach(d => result.Add(d.Person, false)); return result; } public Dictionary FindImagesForChatAuthorizedPersonsForEmployee(long pEmployeeOid) { var result = new Dictionary(); var employee = CreateCriteriaIsActive().Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pEmployeeOid)).UniqueResult(); var relatedCustomerOids = employee.Employee2CustomerList.Select(s => s.CustomerOid.Value); var c1 = CreateCriteriaIsActive() .Add(Restrictions.Eq(CustomerAPPCode.PropertyName_IsChatAktiv, 1)); var codes = c1.List().Select(s => s.CustomerOid.Value).ToArray(); var c2 = CreateCriteriaIsActive() .Add(Restrictions.In(BeWoEntityBase.PropertyName_Oid, codes)) .Add(Restrictions.In(BeWoEntityBase.PropertyName_Oid, relatedCustomerOids.ToList())); var customers = c2.List(); var employees = FindAllChatActiveEmployees(); //foreach (var customer in customers.Where(w => w.CustomerImage != null)) //{ // result.Add(customer.Person.Oid.Value, customer.CustomerImage); //} //foreach (var emp in employees.Where(w => w.EmployeeImage != null)) //{ // result.Add(emp.Person.Oid.Value, emp.EmployeeImage); //} return result; } public IEnumerable FindUnreadChatMessagesForRecipient(long pRecipientOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pRecipientOid)) .Add(Restrictions.Eq(ChatMessage.PropertyName_IstGelesen, 0)); return c.List(); } public IEnumerable FindNewestChatMessages(long pRecipientPersonOid) { var blubb = FindTeamsOfEmployee(pRecipientPersonOid); var c = CreateCriteria(); c.Add(Subqueries.PropertyIn("Oid", DetachedCriteria.For() .Add( Restrictions.Or( Restrictions.And( Restrictions.Or( Restrictions.Eq(NewestChatMessage.PropertyName_RecipientPersonOid, pRecipientPersonOid), Restrictions.Eq(NewestChatMessage.PropertyName_SenderPersonOid, pRecipientPersonOid)), Restrictions.Eq(NewestChatMessage.PropertyName_IsTeam, false)), Restrictions.And(Restrictions.Eq(NewestChatMessage.PropertyName_IsTeam, true), Restrictions.In(NewestChatMessage.PropertyName_RecipientPersonOid, blubb.Select(s => s.Oid.Value).ToArray()))) ) .SetProjection(Projections.Property("ChatMessageOid")))); var query = ToSql(c); return c.List(); } public static string ToSql(ICriteria criteria) { var c = (CriteriaImpl)criteria; var s = (SessionImpl)c.Session; var factory = (ISessionFactoryImplementor)s.SessionFactory; var implementors = factory.GetImplementors(c.EntityOrClassName); var loader = new CriteriaLoader( (IOuterJoinLoadable)factory.GetEntityPersister(implementors[0]), factory, c, implementors[0], s.EnabledFilters); return loader.SqlString.ToString(); } public IEnumerable FindChatMessageIds(List messageIds) { var c = CreateCriteria() .Add(Restrictions.In(ChatMessage.PropertyName_MessageId, messageIds)); var liste = c.List(); return liste.Select(s => s.MessageId); } public IEnumerable LoadChatMessagesChunkwise(string pMessageId, long pRecipientOid, long pSenderOid, bool pForTeam, List pExceptions, bool pIsInitialCall) { var c = CreateCriteriaIsActive() .Add(Restrictions.Or( Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pSenderOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pRecipientOid)), Restrictions.And( Restrictions.Eq(ChatMessage.PropertyName_SenderPersonOid, pRecipientOid), Restrictions.Eq(ChatMessage.PropertyName_EmpfaengerPersonOid, pSenderOid)))); c.Add(Restrictions.IsNull(ChatMessage.PropertyName_TeamOid)); if (pForTeam) { c = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatMessage.PropertyName_TeamOid, pRecipientOid)); } if (!IsNullOrEmpty(pMessageId)) { var c1 = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatMessage.PropertyName_MessageId, pMessageId)) .AddOrder(Order.Desc(BeWoEntityBase.PropertyName_InsTs)); var list = c1.List(); if (list.Count > 0) { var lastLoadedChatMessage = c1.List().First(); c.Add(Restrictions.Not(Restrictions.In(ChatMessage.PropertyName_MessageId, pExceptions))); if (pIsInitialCall) { c.Add(Restrictions.Gt(BeWoEntityBase.PropertyName_InsTs, lastLoadedChatMessage.InsTs)); } } } c.AddOrder(Order.Desc(BeWoEntityBase.PropertyName_InsTs)); c.SetMaxResults(50); var sqlQeury = ToSql(c); var result = c.List(); return result; } public IList GetChatMediaMessagesForChatMessage(long chatMessageOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatMediaMessage.PropertyName_ChatMessageOid, chatMessageOid)); return c.List(); } public NewestChatMessage GetNewestChatMessageForConversation(long pRecipientPersonOid, long pSenderPersonOid, bool pIsTeam) { var teams = new List(); if (pIsTeam) { var employee = FindEmployeeWithPersonOid(pSenderPersonOid); if (employee != null) { teams.AddRange(FindTeamsOfEmployee(employee.Oid.Value).Select(s => s.Oid.Value)); } } var c = CreateCriteria(); if (pIsTeam) { c.Add(Restrictions.And(Restrictions.Eq(NewestChatMessage.PropertyName_IsTeam, true), Restrictions.Eq(NewestChatMessage.PropertyName_RecipientPersonOid, pRecipientPersonOid))); } else { c.Add( Restrictions.And( Restrictions.Or( Restrictions.And( Restrictions.Eq(NewestChatMessage.PropertyName_RecipientPersonOid, pRecipientPersonOid), Restrictions.Eq(NewestChatMessage.PropertyName_SenderPersonOid, pSenderPersonOid)), Restrictions.And( Restrictions.Eq(NewestChatMessage.PropertyName_RecipientPersonOid, pSenderPersonOid), Restrictions.Eq(NewestChatMessage.PropertyName_SenderPersonOid, pRecipientPersonOid))), Restrictions.Eq(NewestChatMessage.PropertyName_IsTeam, false))); } return c.UniqueResult(); } public IList GetAllOrganisation2PersonRelations(long personOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid,personOid)).UniqueResult().Organisation2Persons.Select(s => s.Organisation).ToList(); return c; } public IList GetAllPerson2OrganisationRelations(long organisationOid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, organisationOid)).UniqueResult().Organisation2PersonList.Select(s => s.Person).ToList(); return c; } public ServiceRecord FindServiceRecordforHistory(long? pServiceRecordOid) { var c = CreateCriteria().Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pServiceRecordOid)); return c.UniqueResult(); } public ArbeitszeitListe FindEmployeeArbeitszeitListe(long employeeOid) { var c = CreateCriteria().Add(Restrictions.Eq(Arbeitszeit.PropertyName_EmployeeOid,employeeOid)); List nAr = new List(); foreach (var item in c.List()) { nAr.Add(item.Oid.Value); } ArbeitszeitListe a = new ArbeitszeitListe(); a.Arbeitszeit = c.List().ToList(); if(nAr.Count != 0){ var cEintrag = CreateCriteria() .Add(Restrictions.In(ArbeitszeitEintrag.PropertyName_ArbeitszeitOid, nAr)); a.ArbeitszeitEintrag = cEintrag.List().ToList(); } return a; } public IList FindCostBearer2SupportConceptForDate(DateTime datum) { var c = CreateCriteriaIsActive(); return c .CreateAlias(SupportConcept.PropertyName_CostBearer2SupportConceptList, "cb2sc", JoinType.InnerJoin) .CreateAlias(SupportConcept.PropertyName_Customer, "c", JoinType.InnerJoin) .Add(Restrictions.Or( Restrictions.And(Restrictions.Le(CostBearer2SupportConcept.PropertyName_ApprovedStartDate, datum), Restrictions.Ge(CostBearer2SupportConcept.PropertyName_ApprovedEndDate, datum)), Restrictions.And(Restrictions.Le(CostBearer2SupportConcept.PropertyName_ApprovedStartDate, datum), Restrictions.Ge(CostBearer2SupportConcept.PropertyName_ApprovedEndDate, datum)))) .List().Distinct().ToList(); } public IList FindQuittierungsCheckWithCustomerOids(List oids) { var c = CreateCriteria() .Add(Restrictions.In(QuittierungsCheck.PropertyName_CustomerOid, oids)) .List(); return c; } public IList FindQuittierungsCheckWithCustomerOid(long oid) { var c = CreateCriteria() .Add(Restrictions.Eq(QuittierungsCheck.PropertyName_CustomerOid, oid)); //.UniqueResult(); return c.List(); } public IEnumerable GetAllAbsenceTimesInIntervalByEmployee(DateTime start, DateTime end, long pEmployeeOid, bool pHasRightToSeeAllEmployeeAppointments) { // Wenn das Enddatum null ist, wird das Ende des Intervalls als Enddatum gesetzt (Ist ja eh read-only). var criteria = CreateCriteriaIsActive() .Add(Restrictions.IsNotNull(AbsenceTime.PropertyName_EmployeeOid)) .Add(Restrictions.Or (Restrictions.Or(Restrictions.Or( Restrictions.And(Restrictions.Ge(AbsenceTime.PropertyName_Start, start), Restrictions.Le(AbsenceTime.PropertyName_Start, end)), Restrictions.Eq(AbsenceTime.PropertyName_Start, start) ), Restrictions.And(Restrictions.Le(AbsenceTime.PropertyName_Start, start), Restrictions.IsNull(AbsenceTime.PropertyName_End)) ), Restrictions.And(Restrictions.Lt(AbsenceTime.PropertyName_Start, start), Restrictions.Gt(AbsenceTime.PropertyName_End, start))) ); if (!pHasRightToSeeAllEmployeeAppointments) { criteria.Add(Restrictions.Eq(AbsenceTime.PropertyName_EmployeeOid, pEmployeeOid)); } return criteria.List(); } public IEnumerable GetActiveTextModules(bool pShouldOnlyLoadOwnTextModules, bool pHasRightToSeeAll, bool pIsInAdministrationView, long pEmployeeOid) { var criteria = CreateCriteriaIsActive(); if(pIsInAdministrationView) { criteria.Add(Restrictions.Eq(nameof(TextModule.IsOnlyForEmployee), false)); } else if(!pHasRightToSeeAll) { criteria.Add(Restrictions.Eq(nameof(TextModule.IsOnlyForEmployee), true)) .Add(Restrictions.Eq(nameof(TextModule.Employee) + "." + nameof(BeWoEntityBase.Oid), pEmployeeOid)); } else { criteria.Add( Restrictions.Or( Restrictions.And( Restrictions.Eq(nameof(TextModule.IsOnlyForEmployee), true), Restrictions.Eq(nameof(TextModule.Employee) + "." + nameof(BeWoEntityBase.Oid), pEmployeeOid)), Restrictions.Eq(nameof(TextModule.IsOnlyForEmployee), false))); } return criteria.List(); } public IEnumerable GetChildTextModules(TextModule textModule) { var criteria = CreateCriteriaIsActive(); criteria.Add(Restrictions.Eq(nameof(TextModule.Parent), textModule)); return criteria.List(); } public IEnumerable FindEmployeeChatBewoMessageSync(long oid) { var c = CreateCriteriaIsActive() .Add(Restrictions.Eq(ChatBewoMessageSync.PropertyName_EmployeeOid, oid)); return c.List(); } public IList FindCustomerPersonRelationsForPerson(long personOid) { var c = CreateCriteriaIsActive() .CreateCriteria(Customer2Person.PropertyName_Person, JoinType.InnerJoin) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, personOid)); return c.List(); } public IList LoadFilteredAppointments(bool pHasRightToSeeAllEmployeeAppointments, long pEmployeeOid, DateTime pIntervalStart, DateTime pIntervalEnd, List pSelectedEmployees, List pSelectedCustomers, List pSelectedResources, bool pEmployeesOnly, bool pCustomersOnly, bool pResourcesOnly, bool pPrivateAppointmentsOnly, bool pOnlyMyAppointments, bool pIncludeInactiveOnes) { var recurrenceBetween = Format("'{0:yyyy-MM-dd} 00:00:00' BETWEEN STR_TO_DATE(SUBSTRING({1}, 24, 19), '%m/%d/%Y %H:%i:%s') AND STR_TO_DATE(SUBSTRING({1}, 50, 19), '%m/%d/%Y %H:%i:%s')", pIntervalStart, SchedulerAppointment.PropertyName_RecurrenceInfo); var criteria = CreateCriteria(); if(!pIncludeInactiveOnes) { criteria.Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)); } criteria.Add(Restrictions.Or( Restrictions.And(Restrictions.Not(Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, "Range", MatchMode.Anywhere)), Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, "OccurrenceCount=\"10\"", MatchMode.Anywhere)), Restrictions.Or( Restrictions.Or( Restrictions.And(Restrictions.Lt(SchedulerAppointment.PropertyName_StartDate, pIntervalEnd), Restrictions.Ge(SchedulerAppointment.PropertyName_EndDate, pIntervalStart)), Restrictions.And(Restrictions.IsNotNull(SchedulerAppointment.PropertyName_RecurrenceInfo), Expression.Sql(new SqlString(recurrenceBetween)))), Restrictions.And(Restrictions.Eq(SchedulerAppointment.PropertyName_Type, 3), Expression.Sql(new SqlString(recurrenceBetween)))))); if(pPrivateAppointmentsOnly) { criteria.Add(Restrictions.Eq(SchedulerAppointment.PropertyName_IsPrivate, true)); } else { if(pEmployeesOnly) { var hasEmployees = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM employee2newschapp)"; criteria.Add(Expression.Sql(hasEmployees)); } if(pCustomersOnly) { var hasCustomers = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM customer2newschapp)"; criteria.Add(Expression.Sql(hasCustomers)); } if(pResourcesOnly) { var hasResources = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM resource2newschapp)"; criteria.Add(Expression.Sql(hasResources)); } if(pSelectedEmployees.Any()) { var detachedCriteria1 = DetachedCriteria.For() .Add(Restrictions.In(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", pSelectedEmployees)) .SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment)); criteria.Add(Subqueries.PropertyIn(BeWoEntityBase.PropertyName_Oid, detachedCriteria1)); } if(pSelectedCustomers.Any()) { var list = ""; for(var i = 0; i < pSelectedCustomers.Count; i++) { if(i != pSelectedCustomers.Count - 1) { list += "" + pSelectedCustomers[i] + ","; } else { list += "" + pSelectedCustomers[i]; } } var blah = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM customer2newschapp WHERE customeroid IN ({list}))"; criteria.Add(Expression.Sql(blah)); } if(pSelectedResources.Any()) { var list = ""; for(var i = 0; i < pSelectedResources.Count; i++) { if(i != pSelectedResources.Count - 1) { list += "" + pSelectedResources[i] + ","; } else { list += "" + pSelectedResources[i]; } } var blah = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM resource2newschapp WHERE resourceoid IN ({list}))"; criteria.Add(Expression.Sql(blah)); } } return criteria.List(); } public IEnumerable LoadFilteredAppointmentsForEmployee(bool pHasRightToSeeAllEmployeeAppointments, long? pEmployeeOid, DateTime pIntervalStart, DateTime pIntervalEnd, List pSelectedEmployees, List pSelectedCustomers, List pSelectedResources, bool pEmployeesOnly, bool pCustomersOnly, bool pResourcesOnly, bool pPrivateAppointmentsOnly, bool pOnlyMyAppointments, bool pIncludeInactiveOnes) { var mainCriteria = CreateRecurrenceCriteria(pIntervalStart, pIntervalEnd, pIncludeInactiveOnes); ICriterion ownAppointmentCriterion = null; ICriterion employeeCriterion = null; ICriterion customerCriterion = null; ICriterion resourceCriterion = null; if(pEmployeeOid.HasValue) { ownAppointmentCriterion = CreateOwnAppointmentsCriteria(pEmployeeOid.Value); } if(pPrivateAppointmentsOnly) { mainCriteria.Add(Restrictions.Eq(SchedulerAppointment.PropertyName_IsPrivate, true)); } if(pEmployeesOnly) { var hasEmployees = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM employee2newschapp)"; mainCriteria.Add(Expression.Sql(hasEmployees)); } if(pCustomersOnly) { var hasCustomers = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM customer2newschapp)"; mainCriteria.Add(Expression.Sql(hasCustomers)); } if(pResourcesOnly) { var hasResources = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM resource2newschapp)"; mainCriteria.Add(Expression.Sql(hasResources)); } if(pSelectedEmployees.Any()) { var detachedCriteria1 = DetachedCriteria.For() .Add(Restrictions.In(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", pSelectedEmployees)) .SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment)); var employee2SchedCrit = Subqueries.PropertyIn(BeWoEntityBase.PropertyName_Oid, detachedCriteria1); var originatorCrit = Restrictions.In(SchedulerAppointment.PropertyName_Originator, pSelectedEmployees); //var notEmployee2SchedCrit = Subqueries.PropertyNotex(BeWoEntityBase.PropertyName_Oid, detachedCriteria1); //and not exists( // SELECT //this_0_.Oid as y0_ //FROM // Employee2NewSchApp this_0_ // WHERE //this_0_.NewSchAppOid = this_.Oid // ) var detachedCriteria2 = DetachedCriteria.For("e2s2") .SetProjection(Projections.Property(BeWoEntityBase.PropertyName_Oid)) .Add(Restrictions.EqProperty("e2s2." + Employee2SchedulerAppointment.PropertyName_SchedulerAppointment, "sa.Oid")); //.Add(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment + ".Oid", mainCriteria.SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_Oid)))); var employee2SchedCrit2 = Subqueries.NotExists(detachedCriteria2); var and = Restrictions.And(originatorCrit, employee2SchedCrit2); employeeCriterion = Restrictions.Or(employee2SchedCrit, and); } if(pSelectedCustomers.Any()) { var list = ""; for(var i = 0; i < pSelectedCustomers.Count; i++) { if(i != pSelectedCustomers.Count - 1) { list += "" + pSelectedCustomers[i] + ","; } else { list += "" + pSelectedCustomers[i]; } } var blah = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM customer2newschapp WHERE customeroid IN ({list}))"; customerCriterion = Expression.Sql(blah); } if(pSelectedResources.Any()) { var list = ""; for(var i = 0; i < pSelectedResources.Count; i++) { if(i != pSelectedResources.Count - 1) { list += "" + pSelectedResources[i] + ","; } else { list += "" + pSelectedResources[i]; } } var blah = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM resource2newschapp WHERE resourceoid IN ({list}))"; resourceCriterion = Expression.Sql(blah); } // AND {SchedulerAppointment.PropertyName_RecurrenceInfo} IS NOT NULL AND {SchedulerAppointment.PropertyName_RecurrenceInfo} NOT LIKE '%Index%' var listOfCriterias = new List { employeeCriterion, customerCriterion, resourceCriterion }; if(!pHasRightToSeeAllEmployeeAppointments && pSelectedEmployees.Count == 0) { mainCriteria.Add(ownAppointmentCriterion); } else { listOfCriterias.Add(ownAppointmentCriterion); } var orCriteria = CreateOrCriteria(listOfCriterias); if(orCriteria != null) { mainCriteria.Add(orCriteria); } var appointments = mainCriteria.List(); var exceptionalRecurrenceInfos = appointments.Where(w => w.RecurrenceInfo != null && w.Type == 3).ToList(); var exceptionIds = new List(); foreach(var appointment in exceptionalRecurrenceInfos) { var match = RecurrenceIdRegex.Match(appointment.RecurrenceInfo); if(match.Success) { var value = match.Value; var actualId = value.Split("\""); if(actualId.Count > 1) { var id = actualId[1]; if(!appointments.Any(w => w.RecurrenceInfo != null && w.RecurrenceInfo.Contains(id) && w.Type == 1)) { exceptionIds.Add(appointment.Oid.Value); } } } } // Serienausnahmen werden als gelöscht markiert und es wird die RecurrenceInfo entfernt. // Der Type wird auf "normal" gesetzt, damit nicht die ganze Serie angezeigt werden muss, die unter Umständen nichts mit den Filterkriterien zu tun hat. var exceptionsToModify = appointments.Where(w => w.Oid.HasValue && exceptionIds.Contains(w.Oid.Value)).ToList(); foreach(var exception in exceptionsToModify) { var replacingAppointment = new SchedulerAppointment { Oid = exception.Oid.Value * -1, Version = 1, Type = 4, RecurrenceInfo = exception.RecurrenceInfo, Originator = exception.Originator, EmployeeList = exception.EmployeeList, CustomerList = exception.CustomerList, ResourceList = exception.ResourceList }; exception.RecurrenceInfo = null; exception.Type = 0; appointments.Add(replacingAppointment); } // Wenn ein Serientermin bearbeitet wird, sodass er außerhalb des Fetch-Zeitraumes liegt, wird er nicht mehr korrekt angezeigt. // Deshalb werden hier alle Ausnahmen von den in der appointments-Collection enthaltenen Terminen mitgeladen. var allExceptionalRecurrenceInfos = appointments.Where(w => w.RecurrenceInfo != null); var changedOrDeletedOccurences = FindAppointmentsByRecurrenceId(ExtractRecurrenceIdFromRecurrenceInfo(allExceptionalRecurrenceInfos.Select(s => s.RecurrenceInfo).ToList()), true); appointments.AddRangeIfElementsNotIn(changedOrDeletedOccurences); return appointments; } private static ICriterion CreateOrCriteria(IReadOnlyCollection criterionList) { ICriterion orCriterion = null; if (criterionList != null && criterionList.Count > 0) { foreach (var c in criterionList) { if (c != null) { orCriterion = orCriterion == null ? c : Restrictions.Or(orCriterion, c); } } } return orCriterion; } private static ICriterion CreateOwnAppointmentsCriteria(long pEmployeeOid) { var detachedCriteria = DetachedCriteria.For() .Add(Restrictions.And( Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_Employee + ".Oid", pEmployeeOid), Restrictions.Not(Restrictions.Eq(Employee2SchedulerAppointment.PropertyName_ParticipationAnswer, ParticipationAnswer.Absage)))); detachedCriteria.SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment)); var detachedCriteria2 = DetachedCriteria.For() .SetProjection(Projections.Property(Employee2SchedulerAppointment.PropertyName_SchedulerAppointment)); ICriterion ownAppointmentCriterion = Restrictions.Or( //Restrictions.And(Restrictions.Eq(SchedulerAppointment.PropertyName_Originator + ".Oid", pEmployeeOid), Subqueries.PropertyNotIn(BeWoEntityBase.PropertyName_Oid, detachedCriteria2)), Restrictions.Eq(SchedulerAppointment.PropertyName_Originator + ".Oid", pEmployeeOid), Subqueries.PropertyIn(BeWoEntityBase.PropertyName_Oid, detachedCriteria)); return ownAppointmentCriterion; } private ICriteria CreateRecurrenceCriteria(DateTime start, DateTime end, bool pIncludeInactiveOnes = false) { var recurrenceBetween = Format("'{0:yyyy-MM-dd} 00:00:00' BETWEEN STR_TO_DATE(SUBSTRING({1}, 24, 19), '%m/%d/%Y %H:%i:%s') AND STR_TO_DATE(SUBSTRING({1}, 50, 19), '%m/%d/%Y %H:%i:%s')", start, SchedulerAppointment.PropertyName_RecurrenceInfo); var recurrenceAfter = $"'{start:yyyy-MM-dd} 00:00:00' > STR_TO_DATE(SUBSTRING({SchedulerAppointment.PropertyName_RecurrenceInfo}, 24, 19), '%m/%d/%Y %H:%i:%s')"; var criteria = CreateCriteria("sa"); if(!pIncludeInactiveOnes) { criteria.Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)); } criteria.Add(Restrictions.Or( Restrictions.And( Restrictions.Not(Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, "Range", MatchMode.Anywhere)), Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, "OccurrenceCount=\"10\"", MatchMode.Anywhere)), Restrictions.Or( Restrictions.Or( Restrictions.And(Restrictions.Lt(SchedulerAppointment.PropertyName_StartDate, end), Restrictions.Ge(SchedulerAppointment.PropertyName_EndDate, start)), Restrictions.Or(Restrictions.And( Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, "End", MatchMode.Anywhere), Restrictions.And(Restrictions.IsNotNull(SchedulerAppointment.PropertyName_RecurrenceInfo), Expression.Sql(new SqlString(recurrenceBetween))) ), Restrictions.And( Restrictions.And( Restrictions.IsNotNull(SchedulerAppointment.PropertyName_RecurrenceInfo), Restrictions.Not(Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, "End", MatchMode.Anywhere))), Expression.Sql(recurrenceAfter))) ), Restrictions.And(Restrictions.Eq(SchedulerAppointment.PropertyName_Type, 3), Expression.Sql(new SqlString(recurrenceBetween)))))); return criteria; } public IList FindDeletedRecurrencesByRecurrenceId(string pRecurrenceId) { var c = CreateCriteria() .Add(Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, pRecurrenceId, MatchMode.Anywhere)) .Add(Restrictions.Eq(SchedulerAppointment.PropertyName_Type, 4)); return c.List(); } public IList FindAppointmentsByRecurrenecInfo(List pRecurrenceInfos, bool pExcludeRootAppointments = false) { if(pRecurrenceInfos == null || pRecurrenceInfos.Count == 0) { return new List(); } return FindAppointmentsByRecurrenceId(ExtractRecurrenceIdFromRecurrenceInfo(pRecurrenceInfos), pExcludeRootAppointments); } public IList FindAppointmentsByRecurrenceId(List pRecurrenceIds, bool pExcludeRootAppointments = false) { if (pRecurrenceIds == null || pRecurrenceIds.Count == 0) { return new List(); } var criterionList = new List(); pRecurrenceIds.DoForEach(id => { criterionList.AddIfNotIn(Restrictions.Like(SchedulerAppointment.PropertyName_RecurrenceInfo, id, MatchMode.Anywhere)); }); var recurrenceIdOr = CreateOrCriteria(criterionList); if (recurrenceIdOr == null) { return new List(); ; } var c = CreateCriteria() .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_IsActive, ActivationTypeId.Active)) .Add(Restrictions.IsNotNull(SchedulerAppointment.PropertyName_RecurrenceInfo)) .Add(recurrenceIdOr); if(pExcludeRootAppointments) { c.Add(Restrictions.In(nameof(Appointment.Type), new []{ 2, 3, 4})); } return c.List(); } public IList FindAppointmentsForCustomer(long pCustomerOid) { var blah = $"{BeWoEntityBase.PropertyName_Oid} IN (SELECT newschappoid FROM customer2newschapp WHERE customeroid IN ({pCustomerOid}))"; var customerCriterion = Expression.Sql(blah); var c = CreateCriteria().Add(customerCriterion); return c.List(); } public IList GroupOfPeopleForSupportConcept(long pCostBearer2SupportConceptOid) { var c = CreateCriteria(); c.CreateCriteria(GroupOfPeople.PropertyName_CostBearer2SupportConceptList, JoinType.InnerJoin) .Add(Restrictions.Eq(BeWoEntityBase.PropertyName_Oid, pCostBearer2SupportConceptOid)); return c.List(); } public IList FindDefaultHourlyRateCostRatePeriods() { var c = CreateCriteria(); c.Add(Restrictions.Eq(CostRatePeriod.PropertyName_CostRateType, CostRatePeriodType.HourlyRate)) .Add(Restrictions.IsNull(CostRatePeriod.PropertyName_ObjectOid)); return c.List(); } public virtual IList GetSqlResult(string sql) { var q = Session.CreateSQLQuery(sql); return q.List(); } public SchedulerAppointment FindRootAppointmentForException(SchedulerAppointmentDC pAppointment) { if(pAppointment?.RecurrenceInfo == null) { return null; } var match = RecurrenceIdRegex.Match(pAppointment.RecurrenceInfo); if(match.Success) { var value = match.Value; var actualId = value.Split("\""); if(actualId.Count > 1) { var id = actualId[1]; var criteria = CreateCriteria() .Add(Restrictions.IsNotNull(nameof(SchedulerAppointment.RecurrenceInfo))) .Add(Restrictions.Like(nameof(SchedulerAppointment.RecurrenceInfo), id, MatchMode.Anywhere)) .Add(Restrictions.Eq(nameof(Appointment.Type), 1)); var resultList = criteria.List(); if(resultList == null || resultList.Count == 0) { return null; } return resultList.First(); } } return null; } public SchedulerAppointment FindRootAppointmentForException(SchedulerAppointment pAppointment) { if(pAppointment?.RecurrenceInfo == null) { return null; } var match = RecurrenceIdRegex.Match(pAppointment.RecurrenceInfo); if(match.Success) { var value = match.Value; var actualId = value.Split("\""); if(actualId.Count > 1) { var id = actualId[1]; var criteria = CreateCriteria() .Add(Restrictions.IsNotNull(nameof(SchedulerAppointment.RecurrenceInfo))) .Add(Restrictions.Like(nameof(SchedulerAppointment.RecurrenceInfo), id, MatchMode.Anywhere)) .Add(Restrictions.Eq(nameof(Appointment.Type), 1)); var resultList = criteria.List(); if(resultList == null || resultList.Count == 0) { return null; } return resultList.First(); } } return null; } private static List ExtractRecurrenceIdFromRecurrenceInfo(List pRecurrenceInfos) { var recurrenceIds = new List(); foreach(var info in pRecurrenceInfos) { var match = RecurrenceIdRegex.Match(info); if(match.Success) { var value = match.Value; var actualId = value.Split("\""); if(actualId.Count > 1) { recurrenceIds.AddIfNotIn(actualId[1]); } } } return recurrenceIds; } } }