public void Can_create_disparate_trial_and_subscription_periods() { var start = DateTime.UtcNow; var end = start.AddYears(5); // Every five days at the introductory price var trialPeriod = new Period(PeriodFrequency.Days, 5); var subscription = new Subscription("Monthly Subscription with Introductory Price", start, end, Period.Monthly, 30, trialPeriod, 1.00, 1); Assert.AreEqual(1771, subscription.TotalAmount); Assert.AreEqual(1770, subscription.TotalBillingAmount); Assert.AreEqual(1, subscription.TotalTrialAmount.Value); Assert.AreEqual(1, subscription.TrialAmount.Value); Assert.AreEqual(60, subscription.TotalPayments); // All occurrences are paid Assert.AreEqual(1, subscription.TrialPayments); }
public void Can_create_monthly_subscription_with_paid_trial_period() { var start = DateTime.UtcNow; var end = start.AddYears(5); var trialPeriod = new Period(PeriodFrequency.Months, 1); var subscription = new Subscription("Monthly Subscription with Introductory Price", start, end, Period.Monthly, 30, trialPeriod, 15, 1); Assert.IsNotNull(subscription.TrialPayments); Assert.AreEqual(60, subscription.TotalPayments); // All occurrences are paid Assert.AreEqual(1, subscription.TrialPayments); }
public void Can_create_monthly_subscription_with_one_month_free_trial() { var start = DateTime.UtcNow; var end = start.AddYears(5); var trialPeriod = new Period(PeriodFrequency.Months, 1); var subscription = new Subscription("Monthly Subscription with One Month Free", start, end, Period.Monthly, 15, trialPeriod); var payments = subscription.TotalPayments; var trialPayments = subscription.TrialPayments; var amount = subscription.TotalAmount; Assert.AreEqual(start.AddMonths(1), subscription.PaymentStartDate); Assert.IsNull(trialPayments, "This is a free trial"); Assert.AreEqual(12*5 - 1, payments); Assert.AreEqual(5*12*15 - 15, amount); }
public void Can_create_monthly_subscription_with_non_standard_trial_period() { var start = DateTime.UtcNow; var end = start.AddYears(5); var trialPeriod = new Period(PeriodFrequency.Days, 32); var subscription = new Subscription("Monthly Subscription with 31 Days Free", start, end, Period.Monthly, 15, trialPeriod); Assert.AreEqual(start.AddDays(32), subscription.PaymentStartDate); var payments = subscription.TotalPayments; var amount = subscription.TotalAmount; Assert.AreEqual(12*5 - 1, payments); Assert.AreEqual(5*12*15 - 15, amount); var next = subscription.NextPaymentDate; var prev = subscription.PreviousPaymentDate; Assert.AreEqual(subscription.PaymentStartDate, next); Assert.IsNull(prev); Assert.IsNull(subscription.TrialPayments); Assert.IsNull(subscription.TrialAmount); }
private static IEnumerable<DateTime> GetOccurrences(DateInterval interval, Period period, Calendar calendar, DateTime start, DateTime end) { var difference = DateSpan.GetDifference(interval, start, end)/period.Quantifier; if (start.Kind == DateTimeKind.Utc) { start = start.ToLocalTime(); } for (var i = 0; i < difference; i++) { switch (period.Frequency) { case PeriodFrequency.Days: var days = calendar.AddDays(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, days); break; case PeriodFrequency.Weeks: var weeks = calendar.AddWeeks(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, weeks); break; case PeriodFrequency.Months: var months = calendar.AddMonths(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, months); break; case PeriodFrequency.Years: var years = calendar.AddYears(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, years); break; default: throw new ArgumentException("Frequency"); } } }
/// <summary> /// Gets the number of occurrences of a specified interval occuring in the given period. /// </summary> /// <param name="interval">The interval.</param> /// <param name="period">The period.</param> /// <param name="start">The start date.</param> /// <returns></returns> public static IEnumerable<DateTime> GetOccurrences(Period period, DateInterval interval, DateTime start) { var calendar = CultureInfo.CurrentCulture.Calendar; switch (period.Frequency) { case PeriodFrequency.Days: var days = calendar.AddDays(start, period.Quantifier); yield return DeferOccurrenceFallingOnWeekend(calendar, days); break; case PeriodFrequency.Weeks: var weeks = calendar.AddWeeks(start, period.Quantifier); yield return DeferOccurrenceFallingOnWeekend(calendar, weeks); break; case PeriodFrequency.Months: var months = calendar.AddMonths(start, period.Quantifier); yield return DeferOccurrenceFallingOnWeekend(calendar, months); break; case PeriodFrequency.Years: var years = calendar.AddYears(start, period.Quantifier); yield return DeferOccurrenceFallingOnWeekend(calendar, years); break; default: throw new ArgumentException("period.Frequency"); } }