/// <summary> /// Safely load entities that have not yet been assigned a non-null value based on the arguments. /// </summary> private void LoadEntities() { if (_automatedPaymentArgs.ScheduledTransactionId.HasValue && _financialScheduledTransaction == null) { _financialScheduledTransaction = _financialScheduledTransactionService.Queryable() .AsNoTracking() .Include(s => s.TransactionFrequencyValue) .FirstOrDefault(s => s.Id == _automatedPaymentArgs.ScheduledTransactionId.Value); } if (_authorizedPerson == null) { _authorizedPerson = _personAliasService.GetPersonNoTracking(_automatedPaymentArgs.AuthorizedPersonAliasId); } if (_financialGateway == null) { _financialGateway = _financialGatewayService.GetNoTracking(_automatedPaymentArgs.AutomatedGatewayId); } if (_financialGateway != null && _automatedGatewayComponent == null) { _automatedGatewayComponent = _financialGateway.GetGatewayComponent(); } if (_financialAccounts == null) { var accountIds = _automatedPaymentArgs.AutomatedPaymentDetails.Select(d => d.AccountId).ToList(); _financialAccounts = _financialAccountService.GetByIds(accountIds).AsNoTracking().ToDictionary(fa => fa.Id, fa => fa); } if (_authorizedPerson != null && _financialPersonSavedAccount == null && _financialGateway != null) { // Pick the correct saved account based on args or default for the user var financialGatewayId = _financialGateway.Id; var savedAccounts = _financialPersonSavedAccountService .GetByPersonId(_authorizedPerson.Id) .AsNoTracking() .Where(sa => sa.FinancialGatewayId == financialGatewayId) .Include(sa => sa.FinancialPaymentDetail) .OrderByDescending(sa => sa.CreatedDateTime ?? DateTime.MinValue) .ToList(); if (_automatedPaymentArgs.FinancialPersonSavedAccountId.HasValue) { // If there is an indicated saved account to use, don't assume any other saved account even with a schedule var savedAccountId = _automatedPaymentArgs.FinancialPersonSavedAccountId.Value; _financialPersonSavedAccount = savedAccounts.FirstOrDefault(sa => sa.Id == savedAccountId); } else { // If there is a schedule and no indicated saved account to use, try to use payment info associated with the schedule if (_financialScheduledTransaction != null) { _financialPersonSavedAccount = // sa.ReferenceNumber == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.ReferenceNumber) && sa.ReferenceNumber == _financialScheduledTransaction.TransactionCode) ?? // sa.GatewayPersonIdentifier == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.GatewayPersonIdentifier) && sa.GatewayPersonIdentifier == _financialScheduledTransaction.TransactionCode) ?? // sa.FinancialPaymentDetailId == fst.FinancialPaymentDetailId savedAccounts.FirstOrDefault(sa => sa.FinancialPaymentDetailId.HasValue && sa.FinancialPaymentDetailId == _financialScheduledTransaction.FinancialPaymentDetailId) ?? // sa.TransactionCode == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.TransactionCode) && sa.TransactionCode == _financialScheduledTransaction.TransactionCode); } if (_financialPersonSavedAccount == null) { // Use the default or first if no default _financialPersonSavedAccount = savedAccounts.FirstOrDefault(sa => sa.IsDefault) ?? savedAccounts.FirstOrDefault(); } } } if (_financialPersonSavedAccount != null && _referencePaymentInfo == null) { _referencePaymentInfo = _financialPersonSavedAccount.GetReferencePayment(); } if (_transactionType == null) { _transactionType = DefinedValueCache.Get(_automatedPaymentArgs.TransactionTypeGuid ?? SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid()); } if (_financialSource == null) { _financialSource = DefinedValueCache.Get(_automatedPaymentArgs.FinancialSourceGuid ?? SystemGuid.DefinedValue.FINANCIAL_SOURCE_TYPE_WEBSITE.AsGuid()); } }
private static AttendanceCache LoadByAttendance(Attendance attendance) { //We don't want to cache attendances created before today. if (!attendance.CreatedDateTime.HasValue || attendance.CreatedDateTime < Rock.RockDateTime.Today) { Remove(attendance.Id); return(null); } Person person; if (attendance.PersonAliasId != null && attendance.PersonAlias?.Person == null) { PersonAliasService personAliasService = new PersonAliasService(new RockContext()); person = personAliasService.GetPersonNoTracking(attendance.PersonAliasId ?? 0); } else { person = attendance.PersonAlias.Person; } var attendanceCache = new AttendanceCache { Id = attendance.Id, OccurrenceAccessKey = OccurrenceCache.GetByOccurrence(attendance.Occurrence)?.AccessKey ?? "None", Code = attendance.AttendanceCode?.Code, CreatedDateTime = attendance.CreatedDateTime, StartDateTime = attendance.StartDateTime, EndDateTime = attendance.EndDateTime, PersonId = person?.Id ?? 0, PersonName = person?.FullName ?? "Unnamed Person", GroupId = attendance.Occurrence.GroupId, LocationId = attendance.Occurrence.LocationId, ScheduleId = attendance.Occurrence.ScheduleId, IsVolunteer = OccurrenceCache.GetVolunteerOccurrences().Select(o => o.GroupId).Contains(attendance.Occurrence.GroupId ?? 0), IsChildren = OccurrenceCache.GetChildrenOccurrences().Select(o => o.GroupId).Contains(attendance.Occurrence.GroupId ?? 0), WithParent = false }; //Sometimes we need to look up the attendance code if (attendanceCache.Code.IsNullOrWhiteSpace() && attendance.AttendanceCodeId.HasValue) { RockContext rockContext = new RockContext(); AttendanceCodeService attendanceCodeService = new AttendanceCodeService(rockContext); var code = attendanceCodeService.GetNoTracking(attendance.AttendanceCodeId.Value); if (code != null) { attendanceCache.Code = code.Code; } } if (attendance.EndDateTime.HasValue) //End date means checked out { attendanceCache.AttendanceState = AttendanceState.CheckedOut; } else //has not been checked out yet { if (attendance.DidAttend == false && attendance.QualifierValueId.HasValue) { attendanceCache.AttendanceState = AttendanceState.MobileReserve; } else if (attendance.DidAttend == true) { attendanceCache.AttendanceState = AttendanceState.InRoom; if (attendance.QualifierValueId == DefinedValueCache.GetId(Constants.DEFINED_VALUE_ATTENDANCE_STATUS_WITH_PARENT.AsGuid())) { attendanceCache.WithParent = true; } } else { attendanceCache.AttendanceState = AttendanceState.EnRoute; } } return(attendanceCache); }