public void GivenWeeklyRepeatingSalesInvoice_WhenDayArrives_ThenNextInvoiceIsCreated() { var customer = new OrganisationBuilder(this.Session).WithName("customer").Build(); var contactMechanism = new PostalAddressBuilder(this.Session) .WithAddress1("Haverwerf 15") .WithLocality("Mechelen") .WithCountry(new Countries(this.Session).FindBy(M.Country.IsoCode, "BE")) .Build(); var homeAddress = new PostalAddressBuilder(this.Session) .WithAddress1("Sint-Lambertuslaan 78") .WithLocality("Muizen") .WithCountry(new Countries(this.Session).FindBy(M.Country.IsoCode, "BE")) .Build(); var billingAddress = new PartyContactMechanismBuilder(this.Session) .WithContactMechanism(homeAddress) .WithContactPurpose(new ContactMechanismPurposes(this.Session).BillingAddress) .WithUseAsDefault(true) .Build(); customer.AddPartyContactMechanism(billingAddress); new CustomerRelationshipBuilder(this.Session).WithFromDate(this.Session.Now()).WithCustomer(customer).Build(); this.Session.Derive(); var invoice = new SalesInvoiceBuilder(this.Session) .WithInvoiceNumber("1") .WithBillToCustomer(customer) .WithAssignedBillToContactMechanism(contactMechanism) .WithSalesInvoiceType(new SalesInvoiceTypes(this.Session).SalesInvoice) .Build(); var mayFourteen2018 = new DateTime(2018, 5, 14, 12, 0, 0, DateTimeKind.Utc); var timeShift = mayFourteen2018 - this.Session.Now(); this.TimeShift = timeShift; var countBefore = new SalesInvoices(this.Session).Extent().Count; var repeatingInvoice = new RepeatingSalesInvoiceBuilder(this.Session) .WithSource(invoice) .WithFrequency(new TimeFrequencies(this.Session).Week) .WithDayOfWeek(new DaysOfWeek(this.Session).Monday) .WithNextExecutionDate(mayFourteen2018) .Build(); Assert.False(this.Session.Derive(false).HasErrors); RepeatingSalesInvoices.Daily(this.Session); Assert.Equal(countBefore + 1, new SalesInvoices(this.Session).Extent().Count); Assert.Equal(new DateTime(2018, 5, 21), repeatingInvoice.NextExecutionDate.Date); Assert.Single(repeatingInvoice.SalesInvoices); }
public void GivenWeeklyRepeatingSalesInvoice_WhenRepeating_ThenRepeatStopReachingLastExecutionDate() { var customer = new OrganisationBuilder(this.Session).WithName("customer").Build(); var contactMechanism = new PostalAddressBuilder(this.Session) .WithAddress1("Haverwerf 15") .WithPostalBoundary(new PostalBoundaryBuilder(this.Session) .WithLocality("Mechelen") .WithCountry(new Countries(this.Session).FindBy(M.Country.IsoCode, "BE")) .Build()) .Build(); var homeAddress = new PostalAddressBuilder(this.Session) .WithAddress1("Sint-Lambertuslaan 78") .WithPostalBoundary(new PostalBoundaryBuilder(this.Session) .WithLocality("Muizen") .WithCountry(new Countries(this.Session).FindBy(M.Country.IsoCode, "BE")) .Build()) .Build(); var billingAddress = new PartyContactMechanismBuilder(this.Session) .WithContactMechanism(homeAddress) .WithContactPurpose(new ContactMechanismPurposes(this.Session).BillingAddress) .WithUseAsDefault(true) .Build(); customer.AddPartyContactMechanism(billingAddress); new CustomerRelationshipBuilder(this.Session).WithFromDate(this.Session.Now()).WithCustomer(customer).Build(); this.Session.Derive(); var invoice = new SalesInvoiceBuilder(this.Session) .WithInvoiceNumber("1") .WithBillToCustomer(customer) .WithBillToContactMechanism(contactMechanism) .WithSalesInvoiceType(new SalesInvoiceTypes(this.Session).SalesInvoice) .Build(); var mayFourteen2018 = new DateTime(2018, 5, 14, 12, 0, 0, DateTimeKind.Utc); var timeShift = mayFourteen2018 - DateTime.UtcNow; this.TimeShift = timeShift; var countBefore = new SalesInvoices(this.Session).Extent().Count; var repeatingInvoice = new RepeatingSalesInvoiceBuilder(this.Session) .WithSource(invoice) .WithFrequency(new TimeFrequencies(this.Session).Week) .WithDayOfWeek(new DaysOfWeek(this.Session).Monday) .WithNextExecutionDate(mayFourteen2018) .WithFinalExecutionDate(mayFourteen2018.AddDays(21)) .Build(); Assert.False(this.Session.Derive(false).HasErrors); RepeatingSalesInvoices.Daily(this.Session); Assert.Equal(countBefore + 1, new SalesInvoices(this.Session).Extent().Count); Assert.Equal(new DateTime(2018, 5, 21), repeatingInvoice.NextExecutionDate.Date); Assert.Equal(new DateTime(2018, 5, 14), repeatingInvoice.PreviousExecutionDate.Value.Date); Assert.Single(repeatingInvoice.SalesInvoices); var mayTwentyOne2018 = new DateTime(2018, 5, 21, 12, 0, 0, DateTimeKind.Utc); timeShift = mayTwentyOne2018 - DateTime.UtcNow; this.TimeShift = timeShift; Assert.False(this.Session.Derive(false).HasErrors); RepeatingSalesInvoices.Daily(this.Session); Assert.Equal(countBefore + 2, new SalesInvoices(this.Session).Extent().Count); Assert.Equal(new DateTime(2018, 5, 28), repeatingInvoice.NextExecutionDate.Date); Assert.Equal(2, repeatingInvoice.SalesInvoices.Count); var mayTwentyEight2018 = new DateTime(2018, 5, 28, 12, 0, 0, DateTimeKind.Utc); timeShift = mayTwentyEight2018 - DateTime.UtcNow; this.TimeShift = timeShift; Assert.False(this.Session.Derive(false).HasErrors); RepeatingSalesInvoices.Daily(this.Session); Assert.Equal(countBefore + 3, new SalesInvoices(this.Session).Extent().Count); Assert.Equal(new DateTime(2018, 6, 4), repeatingInvoice.NextExecutionDate.Date); Assert.Equal(new DateTime(2018, 5, 28), repeatingInvoice.PreviousExecutionDate.Value.Date); Assert.Equal(3, repeatingInvoice.SalesInvoices.Count); var juneFour2018 = new DateTime(2018, 6, 4, 12, 0, 0, DateTimeKind.Utc); timeShift = juneFour2018 - DateTime.UtcNow; this.TimeShift = timeShift; Assert.False(this.Session.Derive(false).HasErrors); RepeatingSalesInvoices.Daily(this.Session); Assert.Equal(countBefore + 4, new SalesInvoices(this.Session).Extent().Count); Assert.Equal(new DateTime(2018, 6, 4), repeatingInvoice.NextExecutionDate.Date); Assert.Equal(new DateTime(2018, 6, 4), repeatingInvoice.PreviousExecutionDate.Value.Date); Assert.Equal(4, repeatingInvoice.SalesInvoices.Count); var juneEleven2018 = new DateTime(2018, 6, 11, 12, 0, 0, DateTimeKind.Utc); timeShift = juneEleven2018 - DateTime.UtcNow; this.TimeShift = timeShift; Assert.False(this.Session.Derive(false).HasErrors); RepeatingSalesInvoices.Daily(this.Session); Assert.Equal(countBefore + 4, new SalesInvoices(this.Session).Extent().Count); Assert.Equal(new DateTime(2018, 6, 4), repeatingInvoice.NextExecutionDate.Date); Assert.Equal(new DateTime(2018, 6, 4), repeatingInvoice.PreviousExecutionDate.Value.Date); Assert.Equal(4, repeatingInvoice.SalesInvoices.Count); }