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 remainingDaysCount = tenant.CalculateRemainingDayCount();

            if (remainingDaysCount <= 0)
            {
                return(targetEdition.GetPaymentAmount(periodType));
            }

            Debug.Assert(tenant.EditionId != null, "tenant.EditionId != null");

            var currentEdition = (SubscribableEdition)await _editionManager.GetByIdAsync(tenant.EditionId.Value);

            return(TenantManager.GetUpgradePrice(currentEdition, targetEdition, remainingDaysCount));
        }
Пример #2
0
        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 remainingDaysCount = tenant.CalculateRemainingDayCount();

            if (remainingDaysCount <= 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 == null || !lastPayment.PaymentPeriodType.HasValue)
            {
                throw new ApplicationException("There is no completed payment record !");
            }

            return(TenantManager.GetUpgradePrice(currentEdition, targetEdition, remainingDaysCount, lastPayment.PaymentPeriodType.Value));
        }