private async Task <decimal> CalculateAmountForPaymentAsync(SubscribableEdition targetEdition, PaymentPeriodType?periodType, EditionPaymentType editionPaymentType, Tenant tenant) { if (editionPaymentType != EditionPaymentType.Upgrade) { return(targetEdition.GetPaymentAmount(periodType)); } if (tenant.EditionId == null) { throw new UserFriendlyException(L("CanNotUpgradeSubscriptionSinceTenantHasNoEditionAssigned")); } var remainingHoursCount = tenant.CalculateRemainingHoursCount(); if (remainingHoursCount <= 0) { return(targetEdition.GetPaymentAmount(periodType)); } Debug.Assert(tenant.EditionId != null, "tenant.EditionId != null"); var currentEdition = (SubscribableEdition)await _editionManager.GetByIdAsync(tenant.EditionId.Value); var lastPayment = await _subscriptionPaymentRepository.GetLastCompletedPaymentOrDefaultAsync(tenant.Id, null, null); if (lastPayment?.PaymentPeriodType == null) { throw new ApplicationException("There is no completed payment record !"); } return(TenantManager.GetUpgradePrice(currentEdition, targetEdition, remainingHoursCount, lastPayment.PaymentPeriodType.Value)); }