Example #1
0
        public static PartyRevenue AppsFindOrCreateAsDependable(ISession session, SalesInvoice invoice)
        {
            var partyRevenues = invoice.BilledFromInternalOrganisation.PartyRevenuesWhereParty;
            partyRevenues.Filter.AddEquals(PartyRevenues.Meta.Year, invoice.InvoiceDate.Year);
            partyRevenues.Filter.AddEquals(PartyRevenues.Meta.Month, invoice.InvoiceDate.Month);
            var partyRevenue = partyRevenues.First ?? new PartyRevenueBuilder(session)
                                                            .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                                                            .WithParty(invoice.BilledFromInternalOrganisation)
                                                            .WithYear(invoice.InvoiceDate.Year)
                                                            .WithMonth(invoice.InvoiceDate.Month)
                                                            .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                                                            .WithRevenue(0M)
                                                            .Build();

            return partyRevenue;
        }
Example #2
0
        public static StoreRevenue AppsFindOrCreateAsDependable(ISession session, SalesInvoice invoice)
        {
            var storeRevenues = invoice.Store.StoreRevenuesWhereStore;
            storeRevenues.Filter.AddEquals(Meta.InternalOrganisation, invoice.BilledFromInternalOrganisation);
            storeRevenues.Filter.AddEquals(Meta.Year, invoice.InvoiceDate.Year);
            storeRevenues.Filter.AddEquals(Meta.Month, invoice.InvoiceDate.Month);
            var storeRevenue = storeRevenues.First ?? new StoreRevenueBuilder(session)
                                                            .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                                                            .WithStore((Store)session.Instantiate(invoice.Store))
                                                            .WithYear(invoice.InvoiceDate.Year)
                                                            .WithMonth(invoice.InvoiceDate.Month)
                                                            .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                                                            .WithRevenue(0M)
                                                            .Build();

            InternalOrganisationRevenues.AppsFindOrCreateAsDependable(session, storeRevenue);

            return storeRevenue;
        }
Example #3
0
        public static SalesChannelRevenue AppsFindOrCreateAsDependable(ISession session, SalesInvoice invoice)
        {
            SalesChannelRevenue salesChannelRevenue = null;
            if (invoice.ExistSalesChannel)
            {
                var salesChannelRevenues = invoice.SalesChannel.SalesChannelRevenuesWhereSalesChannel;
                salesChannelRevenues.Filter.AddEquals(SalesChannelRevenues.Meta.InternalOrganisation, invoice.BilledFromInternalOrganisation);
                salesChannelRevenues.Filter.AddEquals(SalesChannelRevenues.Meta.Year, invoice.InvoiceDate.Year);
                salesChannelRevenues.Filter.AddEquals(SalesChannelRevenues.Meta.Month, invoice.InvoiceDate.Month);
                salesChannelRevenue = salesChannelRevenues.First ?? new SalesChannelRevenueBuilder(session)
                                                                            .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                                                                            .WithSalesChannel((SalesChannel)session.Instantiate(invoice.SalesChannel))
                                                                            .WithYear(invoice.InvoiceDate.Year)
                                                                            .WithMonth(invoice.InvoiceDate.Month)
                                                                            .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                                                                            .WithRevenue(0M)
                                                                            .Build();
            }

            return salesChannelRevenue;
        }
Example #4
0
        public static PartyProductRevenue AppsFindOrCreateAsDependable(ISession session, Product product, SalesInvoice invoice)
        {
            var partyProductRevenues = invoice.BillToCustomer.PartyProductRevenuesWhereParty;
            partyProductRevenues.Filter.AddEquals(Meta.InternalOrganisation, invoice.BilledFromInternalOrganisation);
            partyProductRevenues.Filter.AddEquals(Meta.Year, invoice.InvoiceDate.Year);
            partyProductRevenues.Filter.AddEquals(Meta.Month, invoice.InvoiceDate.Month);
            partyProductRevenues.Filter.AddEquals(Meta.Product, product);
            var partyProductRevenue = partyProductRevenues.First ?? new PartyProductRevenueBuilder(session)
                                                                        .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                                                                        .WithParty(invoice.BillToCustomer)
                                                                        .WithProduct(product)
                                                                        .WithYear(invoice.InvoiceDate.Year)
                                                                        .WithMonth(invoice.InvoiceDate.Month)
                                                                        .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                                                                        .WithRevenue(0M)
                                                                        .WithQuantity(0M)
                                                                        .Build();

            PartyRevenues.AppsFindOrCreateAsDependable(session, partyProductRevenue);
            ProductRevenues.AppsFindOrCreateAsDependable(session, partyProductRevenue);
            PartyProductCategoryRevenues.AppsFindOrCreateAsDependable(session, partyProductRevenue);

            return partyProductRevenue;
        }
Example #5
0
 private static StoreRevenue CreateStoreRevenue(ISession session, SalesInvoice invoice)
 {
     return new StoreRevenueBuilder(session)
                 .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                 .WithStore(invoice.Store)
                 .WithYear(invoice.InvoiceDate.Year)
                 .WithMonth(invoice.InvoiceDate.Month)
                 .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                 .WithRevenue(0M)
                 .Build();
 }
Example #6
0
        public static void BaseMonthly(ISession session)
        {
            var customers = new Parties(session).Extent();

            customers.Filter.AddEquals(M.Party.CollectiveWorkEffortInvoice, true);

            var workTasks = new WorkTasks(session).Extent();

            workTasks.Filter.AddEquals(M.WorkEffort.WorkEffortState, new WorkEffortStates(session).Completed);
            workTasks.Filter.AddContainedIn(M.WorkEffort.Customer, (Extent)customers);

            var workTasksByCustomer = workTasks.Select(v => v.Customer).Distinct()
                                      .ToDictionary(v => v, v => v.WorkEffortsWhereCustomer.Where(w => w.WorkEffortState.Equals(new WorkEffortStates(session).Completed)).ToArray());

            SalesInvoice salesInvoice = null;

            foreach (var customerWorkTasks in workTasksByCustomer)
            {
                var customer = customerWorkTasks.Key;

                var customerWorkTasksByInternalOrganisation = customerWorkTasks.Value
                                                              .GroupBy(v => v.TakenBy)
                                                              .Select(v => v)
                                                              .ToArray();

                if (customerWorkTasks.Value.Any(v => v.CanInvoice))
                {
                    foreach (var group in customerWorkTasksByInternalOrganisation)
                    {
                        if (group.Any(v => v.CanInvoice))
                        {
                            salesInvoice = new SalesInvoiceBuilder(session)
                                           .WithBilledFrom(group.Key)
                                           .WithBillToCustomer(customer)
                                           .WithInvoiceDate(session.Now())
                                           .WithSalesInvoiceType(new SalesInvoiceTypes(session).SalesInvoice)
                                           .Build();
                        }

                        var timeEntriesByBillingRate = group.SelectMany(v => v.ServiceEntriesWhereWorkEffort.OfType <TimeEntry>())
                                                       .Where(v => (v.IsBillable && !v.BillableAmountOfTime.HasValue && v.AmountOfTime.HasValue) || v.BillableAmountOfTime.HasValue)
                                                       .GroupBy(v => v.BillingRate)
                                                       .Select(v => v)
                                                       .ToArray();

                        foreach (var rateGroup in timeEntriesByBillingRate)
                        {
                            var timeEntries = rateGroup.ToArray();

                            var invoiceItem = new SalesInvoiceItemBuilder(session)
                                              .WithInvoiceItemType(new InvoiceItemTypes(session).Service)
                                              .WithAssignedUnitPrice(rateGroup.Key)
                                              .WithQuantity(timeEntries.Sum(v => v.BillableAmountOfTime ?? v.AmountOfTime ?? 0.0m))
                                              .Build();

                            salesInvoice.AddSalesInvoiceItem(invoiceItem);

                            foreach (var billableEntry in timeEntries)
                            {
                                new TimeEntryBillingBuilder(session)
                                .WithTimeEntry(billableEntry)
                                .WithInvoiceItem(invoiceItem)
                                .Build();
                            }
                        }

                        foreach (var workEffort in group)
                        {
                            if (workEffort.CanInvoice)
                            {
                                if (string.IsNullOrEmpty(salesInvoice.CustomerReference))
                                {
                                    salesInvoice.CustomerReference = $"WorkOrder(s): {workEffort.WorkEffortNumber}";
                                }
                                else
                                {
                                    salesInvoice.CustomerReference += $", {workEffort.WorkEffortNumber}";
                                }

                                foreach (WorkEffortInventoryAssignment inventoryAssignment in workEffort.WorkEffortInventoryAssignmentsWhereAssignment)
                                {
                                    var part = inventoryAssignment.InventoryItem.Part;

                                    var invoiceItem = new SalesInvoiceItemBuilder(session)
                                                      .WithInvoiceItemType(new InvoiceItemTypes(session).PartItem)
                                                      .WithPart(part)
                                                      .WithAssignedUnitPrice(inventoryAssignment.UnitSellingPrice)
                                                      .WithQuantity(inventoryAssignment.DerivedBillableQuantity)
                                                      .Build();

                                    salesInvoice.AddSalesInvoiceItem(invoiceItem);

                                    new WorkEffortBillingBuilder(session)
                                    .WithWorkEffort(workEffort)
                                    .WithInvoiceItem(invoiceItem)
                                    .Build();
                                }

                                foreach (WorkEffortPurchaseOrderItemAssignment purchaseOrderItemAssignment in workEffort.WorkEffortPurchaseOrderItemAssignmentsWhereAssignment)
                                {
                                    var invoiceItem = new SalesInvoiceItemBuilder(session)
                                                      .WithInvoiceItemType(new InvoiceItemTypes(session).Service)
                                                      .WithAssignedUnitPrice(purchaseOrderItemAssignment.UnitSellingPrice)
                                                      .WithQuantity(purchaseOrderItemAssignment.Quantity)
                                                      .Build();

                                    salesInvoice.AddSalesInvoiceItem(invoiceItem);

                                    new WorkEffortBillingBuilder(session)
                                    .WithWorkEffort(workEffort)
                                    .WithInvoiceItem(invoiceItem)
                                    .Build();
                                }

                                workEffort.WorkEffortState = new WorkEffortStates(session).Finished;
                            }
                        }
                    }
                }
            }
        }
Example #7
0
 private static PartyRevenue CreatePartyRevenue(ISession session, SalesInvoice invoice)
 {
     return new PartyRevenueBuilder(session)
                 .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                 .WithParty(invoice.BillToCustomer)
                 .WithYear(invoice.InvoiceDate.Year)
                 .WithMonth(invoice.InvoiceDate.Month)
                 .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                 .WithRevenue(0M)
                 .Build();
 }
Example #8
0
        private static void CreateInvoiceItems(this WorkEffort @this, SalesInvoice salesInvoice)
        {
            var session           = @this.Strategy.Session;
            var timeBillingAmount = 0M;
            var hours             = 0M;
            var billableEntries   = new List <TimeEntry>();
            var frequencies       = new TimeFrequencies(session);

            foreach (TimeEntry timeEntry in @this.ServiceEntriesWhereWorkEffort)
            {
                if (timeEntry.IsBillable)
                {
                    billableEntries.Add(timeEntry);
                    var entryTimeSpan = (decimal)(timeEntry.ThroughDate - timeEntry.FromDate).Value.TotalMinutes;

                    if (timeEntry.ExistBillingRate)
                    {
                        var timeInTimeEntryRateFrequency = frequencies.Minute.ConvertToFrequency(entryTimeSpan, timeEntry.BillingFrequency);
                        timeBillingAmount += Math.Round((decimal)(timeEntry.BillingRate * timeInTimeEntryRateFrequency), 2);
                    }
                    else
                    {
                        var workEffortAssignmentRate      = @this.WorkEffortAssignmentRatesWhereWorkEffort.First;
                        var timeInWorkEffortRateFrequency = frequencies.Minute.ConvertToFrequency(entryTimeSpan, workEffortAssignmentRate.Frequency);
                        timeBillingAmount += Math.Round((decimal)(workEffortAssignmentRate.Rate * timeInWorkEffortRateFrequency), 2);
                    }
                }
            }

            if (timeBillingAmount > 0)
            {
                var invoiceItem = new SalesInvoiceItemBuilder(session)
                                  .WithInvoiceItemType(new InvoiceItemTypes(session).Time)
                                  .WithActualUnitPrice(timeBillingAmount)
                                  .WithQuantity(hours)
                                  .Build();

                salesInvoice.AddSalesInvoiceItem(invoiceItem);

                foreach (TimeEntry billableEntry in billableEntries)
                {
                    new TimeEntryBillingBuilder(session)
                    .WithTimeEntry(billableEntry)
                    .WithInvoiceItem(invoiceItem)
                    .Build();
                }
            }

            foreach (WorkEffortInventoryAssignment workEffortInventoryAssignment in @this.WorkEffortInventoryAssignmentsWhereAssignment)
            {
                var part            = workEffortInventoryAssignment.InventoryItem.Part;
                var priceComponents = GetPriceComponents(@this, part);

                var basePrice = 0M;
                foreach (var priceComponent in priceComponents)
                {
                    if (priceComponent.Strategy.Class.Equals(M.BasePrice.ObjectType))
                    {
                        if (priceComponent.ExistPrice)
                        {
                            if (basePrice < priceComponent.Price)
                            {
                                basePrice = (decimal)priceComponent.Price;
                            }
                        }
                    }
                }

                var invoiceItem = new SalesInvoiceItemBuilder(session)
                                  .WithInvoiceItemType(new InvoiceItemTypes(session).PartItem)
                                  .WithPart(part)
                                  .WithActualUnitPrice(basePrice)
                                  .WithQuantity(workEffortInventoryAssignment.Quantity)
                                  .Build();

                salesInvoice.AddSalesInvoiceItem(invoiceItem);

                new WorkEffortBillingBuilder(session)
                .WithWorkEffort(@this)
                .WithInvoiceItem(invoiceItem)
                .Build();
            }
        }
Example #9
0
 private void InstantiateObjects(ISession session)
 {
     this.good = (Good)session.Instantiate(this.good);
     this.feature1 = (Colour)session.Instantiate(this.feature1);
     this.feature2 = (Colour)session.Instantiate(this.feature2);
     this.internalOrganisation = (InternalOrganisation)session.Instantiate(this.internalOrganisation);
     this.billToCustomer = (Organisation)session.Instantiate(this.billToCustomer);
     this.shipToCustomer = (Organisation)session.Instantiate(this.shipToCustomer);
     this.supplier = (Organisation)session.Instantiate(this.supplier);
     this.mechelen = (City)session.Instantiate(this.mechelen);
     this.kiev = (City)session.Instantiate(this.kiev);
     this.billToContactMechanismMechelen = (PostalAddress)session.Instantiate(this.billToContactMechanismMechelen);
     this.shipToContactMechanismKiev = (PostalAddress)session.Instantiate(this.shipToContactMechanismKiev);
     this.goodPurchasePrice = (ProductPurchasePrice)session.Instantiate(this.goodPurchasePrice);
     this.goodSupplierOffering = (SupplierOffering)session.Instantiate(this.goodSupplierOffering);
     this.currentBasePriceGeoBoundary = (BasePrice)session.Instantiate(this.currentBasePriceGeoBoundary);
     this.currentFeature1BasePrice = (BasePrice)session.Instantiate(this.currentFeature1BasePrice);
     this.currentGood1BasePrice = (BasePrice)session.Instantiate(this.currentGood1BasePrice);
     this.currentGood1Feature1BasePrice = (BasePrice)session.Instantiate(this.currentGood1Feature1BasePrice);
     this.invoice = (SalesInvoice)session.Instantiate(this.invoice);
     this.vatRate21 = (VatRate)session.Instantiate(this.vatRate21);
 }
Example #10
0
        public override void Init()
        {
            base.Init();

            var euro = new Currencies(this.DatabaseSession).FindBy(Currencies.Meta.IsoCode, "EUR");

            this.supplier = new OrganisationBuilder(this.DatabaseSession).WithName("supplier").WithLocale(new Locales(this.DatabaseSession).EnglishGreatBritain).Build();

            this.internalOrganisation = new InternalOrganisations(this.DatabaseSession).FindBy(InternalOrganisations.Meta.Name, "internalOrganisation");

            this.vatRate21 = new VatRateBuilder(this.DatabaseSession).WithRate(21).Build();

            this.mechelen = new CityBuilder(this.DatabaseSession).WithName("Mechelen").Build();
            this.kiev = new CityBuilder(this.DatabaseSession).WithName("Kiev").Build();

            this.billToContactMechanismMechelen = new PostalAddressBuilder(this.DatabaseSession).WithAddress1("Mechelen").WithGeographicBoundary(this.mechelen).Build();
            this.shipToContactMechanismKiev = new PostalAddressBuilder(this.DatabaseSession).WithAddress1("Kiev").WithGeographicBoundary(this.kiev).Build();
            this.billToCustomer = new OrganisationBuilder(this.DatabaseSession).WithName("billToCustomer").WithPreferredCurrency(euro).Build();

            this.shipToCustomer = new OrganisationBuilder(this.DatabaseSession).WithName("shipToCustomer").WithPreferredCurrency(euro).Build();

            new CustomerRelationshipBuilder(this.DatabaseSession).WithFromDate(DateTime.UtcNow).WithCustomer(this.billToCustomer).WithInternalOrganisation(this.internalOrganisation).Build();
            new CustomerRelationshipBuilder(this.DatabaseSession).WithFromDate(DateTime.UtcNow).WithCustomer(this.shipToCustomer).WithInternalOrganisation(this.internalOrganisation).Build();

            this.DatabaseSession.Derive(true);

            this.good = new GoodBuilder(this.DatabaseSession)
                .WithSku("10101")
                .WithVatRate(this.vatRate21)
                .WithName("good")
                .WithInventoryItemKind(new InventoryItemKinds(this.DatabaseSession).NonSerialized)
                .WithUnitOfMeasure(new UnitsOfMeasure(this.DatabaseSession).Piece)
                .Build();

            this.feature1 = new ColourBuilder(this.DatabaseSession)
                .WithName("white")
                .WithVatRate(this.vatRate21)
                .WithLocalisedName(new LocalisedTextBuilder(this.DatabaseSession)
                                            .WithText("white")
                                            .WithLocale(Singleton.Instance(this.DatabaseSession).DefaultLocale)
                                            .Build())
                .Build();

            this.feature2 = new ColourBuilder(this.DatabaseSession)
                .WithName("black")
                .WithLocalisedName(new LocalisedTextBuilder(this.DatabaseSession)
                                            .WithText("black")
                                            .WithLocale(Singleton.Instance(this.DatabaseSession).DefaultLocale)
                                            .Build())
                .Build();

            this.goodPurchasePrice = new ProductPurchasePriceBuilder(this.DatabaseSession)
                .WithCurrency(euro)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .WithPrice(7)
                .WithUnitOfMeasure(new UnitsOfMeasure(this.DatabaseSession).Piece)
                .Build();

            this.goodSupplierOffering = new SupplierOfferingBuilder(this.DatabaseSession)
                .WithProduct(this.good)
                .WithSupplier(this.supplier)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .WithProductPurchasePrice(this.goodPurchasePrice)
                .Build();

            this.currentBasePriceGeoBoundary = new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("current BasePriceGeoBoundary")
                .WithGeographicBoundary(this.mechelen)
                .WithProduct(this.good)
                .WithPrice(8)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .Build();

            // previous basePrice for good
            new BasePriceBuilder(this.DatabaseSession).WithDescription("previous good")
                .WithSpecifiedFor(this.internalOrganisation)
                .WithProduct(this.good)
                .WithPrice(8)
                .WithFromDate(DateTime.UtcNow.AddYears(-1))
                .WithThroughDate(DateTime.UtcNow.AddDays(-1))
                .Build();

            this.currentGood1BasePrice = new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("current good")
                .WithProduct(this.good)
                .WithPrice(10)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .WithThroughDate(DateTime.UtcNow.AddYears(1).AddDays(-1))
                .Build();

            // future basePrice for good
            new BasePriceBuilder(this.DatabaseSession).WithDescription("future good")
                .WithSpecifiedFor(this.internalOrganisation)
                .WithProduct(this.good)
                .WithPrice(11)
                .WithFromDate(DateTime.UtcNow.AddYears(1))
                .Build();

            // previous basePrice for feature1
            new BasePriceBuilder(this.DatabaseSession).WithDescription("previous feature1")
                .WithSpecifiedFor(this.internalOrganisation)
                .WithProductFeature(this.feature1)
                .WithPrice(0.5M)
                .WithFromDate(DateTime.UtcNow.AddYears(-1))
                .WithThroughDate(DateTime.UtcNow.AddDays(-1))
                .Build();

            // future basePrice for feature1
            new BasePriceBuilder(this.DatabaseSession).WithDescription("future feature1")
                .WithSpecifiedFor(this.internalOrganisation)
                .WithProductFeature(this.feature1)
                .WithPrice(2.5M)
                .WithFromDate(DateTime.UtcNow.AddYears(1))
                .Build();

            this.currentFeature1BasePrice = new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("current feature1")
                .WithProductFeature(this.feature1)
                .WithPrice(2)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .WithThroughDate(DateTime.UtcNow.AddYears(1).AddDays(-1))
                .Build();

            // previous basePrice for feature2
            new BasePriceBuilder(this.DatabaseSession).WithDescription("previous feature2")
                .WithSpecifiedFor(this.internalOrganisation)
                .WithProductFeature(this.feature2)
                .WithPrice(2)
                .WithFromDate(DateTime.UtcNow.AddYears(-1))
                .WithThroughDate(DateTime.UtcNow.AddDays(-1))
                .Build();

            // future basePrice for feature2
            new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("future feature2")
                .WithProductFeature(this.feature2)
                .WithPrice(4)
                .WithFromDate(DateTime.UtcNow.AddYears(1))
                .Build();

            new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("current feature2")
                .WithProductFeature(this.feature2)
                .WithPrice(3)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .WithThroughDate(DateTime.UtcNow.AddYears(1).AddDays(-1))
                .Build();

            // previous basePrice for good with feature1
            new BasePriceBuilder(this.DatabaseSession).WithDescription("previous good/feature1")
                .WithSpecifiedFor(this.internalOrganisation)
                .WithProduct(this.good)
                .WithProductFeature(this.feature1)
                .WithPrice(4)
                .WithFromDate(DateTime.UtcNow.AddYears(-1))
                .WithThroughDate(DateTime.UtcNow.AddDays(-1))
                .Build();

            // future basePrice for good with feature1
            new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("future good/feature1")
                .WithProduct(this.good)
                .WithProductFeature(this.feature1)
                .WithPrice(6)
                .WithFromDate(DateTime.UtcNow.AddYears(1))
                .Build();

            this.currentGood1Feature1BasePrice = new BasePriceBuilder(this.DatabaseSession)
                .WithSpecifiedFor(this.internalOrganisation)
                .WithDescription("current good/feature1")
                .WithProduct(this.good)
                .WithProductFeature(this.feature1)
                .WithPrice(5)
                .WithFromDate(DateTime.UtcNow.AddMinutes(-1))
                .WithThroughDate(DateTime.UtcNow.AddYears(1).AddDays(-1))
                .Build();

            this.invoice = new SalesInvoiceBuilder(this.DatabaseSession)
                .WithSalesInvoiceType(new SalesInvoiceTypes(this.DatabaseSession).SalesInvoice)
                .WithBillToContactMechanism(this.billToContactMechanismMechelen)
                .WithBillToCustomer(this.billToCustomer)
                .WithShipToAddress(this.shipToContactMechanismKiev)
                .WithShipToCustomer(this.shipToCustomer)
                .WithBilledFromInternalOrganisation(this.internalOrganisation)
                .Build();

            this.DatabaseSession.Derive(true);
            this.DatabaseSession.Commit();
        }
Example #11
0
        private static decimal BaseCatalogPrice(
            SalesOrder salesOrder,
            SalesInvoice salesInvoice,
            Product product,
            DateTime date)
        {
            var productBasePrice = 0M;
            var productDiscount  = 0M;
            var productSurcharge = 0M;

            var baseprices = new PriceComponent[0];

            if (product.ExistBasePrices)
            {
                baseprices = product.BasePrices;
            }

            var party = salesOrder != null ? salesOrder.ShipToCustomer : salesInvoice?.BillToCustomer;

            foreach (BasePrice priceComponent in baseprices)
            {
                if (priceComponent.FromDate <= date &&
                    (!priceComponent.ExistThroughDate || priceComponent.ThroughDate >= date))
                {
                    if (PriceComponents.BaseIsApplicable(new PriceComponents.IsApplicable
                    {
                        PriceComponent = priceComponent,
                        Customer = party,
                        Product = product,
                        SalesOrder = salesOrder,
                        SalesInvoice = salesInvoice,
                    }))
                    {
                        if (priceComponent.ExistPrice)
                        {
                            if (productBasePrice == 0 || priceComponent.Price < productBasePrice)
                            {
                                productBasePrice = priceComponent.Price ?? 0;
                            }
                        }
                    }
                }
            }

            var currentPriceComponents = new PriceComponents(product.Strategy.Session).CurrentPriceComponents(date);
            var priceComponents        = product.GetPriceComponents(currentPriceComponents);

            foreach (var priceComponent in priceComponents)
            {
                if (priceComponent.Strategy.Class.Equals(M.DiscountComponent.ObjectType) || priceComponent.Strategy.Class.Equals(M.SurchargeComponent.ObjectType))
                {
                    if (PriceComponents.BaseIsApplicable(new PriceComponents.IsApplicable
                    {
                        PriceComponent = priceComponent,
                        Customer = party,
                        Product = product,
                        SalesOrder = salesOrder,
                        SalesInvoice = salesInvoice,
                    }))
                    {
                        if (priceComponent.Strategy.Class.Equals(M.DiscountComponent.ObjectType))
                        {
                            var     discountComponent = (DiscountComponent)priceComponent;
                            decimal discount;

                            if (discountComponent.Price.HasValue)
                            {
                                discount         = discountComponent.Price.Value;
                                productDiscount += discount;
                            }
                            else
                            {
                                var percentage = discountComponent.Percentage ?? 0;
                                discount         = Rounder.RoundDecimal(productBasePrice * percentage / 100, 2);
                                productDiscount += discount;
                            }
                        }

                        if (priceComponent.Strategy.Class.Equals(M.SurchargeComponent.ObjectType))
                        {
                            var     surchargeComponent = (SurchargeComponent)priceComponent;
                            decimal surcharge;

                            if (surchargeComponent.Price.HasValue)
                            {
                                surcharge         = surchargeComponent.Price.Value;
                                productSurcharge += surcharge;
                            }
                            else
                            {
                                var percentage = surchargeComponent.Percentage ?? 0;
                                surcharge         = Rounder.RoundDecimal(productBasePrice * percentage / 100, 2);
                                productSurcharge += surcharge;
                            }
                        }
                    }
                }
            }

            return(productBasePrice - productDiscount + productSurcharge);
        }
Example #12
0
 private static PackageRevenue CreatePackageRevenue(ISession session, SalesInvoice invoice, Package package)
 {
     return new PackageRevenueBuilder(session)
                 .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                 .WithPackage(package)
                 .WithPackageName(package.Name)
                 .WithYear(invoice.InvoiceDate.Year)
                 .WithMonth(invoice.InvoiceDate.Month)
                 .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                 .WithRevenue(0M)
                 .Build();
 }
Example #13
0
        private static decimal AppsCatalogPrice(
            SalesOrder salesOrder,
            SalesInvoice salesInvoice,
            Product product,
            DateTime date)
        {
            var productBasePrice = 0M;
            var productDiscount = 0M;
            var productSurcharge = 0M;

            var baseprices = new PriceComponent[0];
            if (product.ExistBasePrices)
            {
                baseprices = product.BasePrices;
            }

            var party = salesOrder != null ? salesOrder.ShipToCustomer : salesInvoice != null ? salesInvoice.BillToCustomer : null;
            var internalOrganisation = salesOrder != null ? salesOrder.TakenByInternalOrganisation : salesInvoice != null ? salesInvoice.BilledFromInternalOrganisation : null;

            foreach (BasePrice priceComponent in baseprices)
            {
                if (priceComponent.FromDate <= date &&
                    (!priceComponent.ExistThroughDate || priceComponent.ThroughDate >= date))
                {
                    if (PriceComponents.AppsIsEligible(new PriceComponents.IsEligibleParams
                                                           {
                                                               PriceComponent = priceComponent,
                                                               Customer = party,
                                                               Product = product,
                                                               SalesOrder = salesOrder,
                                                               SalesInvoice = salesInvoice,
                                                           }))
                    {
                        if (priceComponent.ExistPrice)
                        {
                            if (productBasePrice == 0 || priceComponent.Price < productBasePrice)
                            {
                                productBasePrice = priceComponent.Price;
                            }
                        }
                    }
                }
            }

            var partyRevenueHistories = party.PartyRevenueHistoriesWhereParty;
            partyRevenueHistories.Filter.AddEquals(PartyRevenueHistories.Meta.InternalOrganisation, internalOrganisation);
            var partyRevenueHistory = partyRevenueHistories.First;

            var partyProductCategoryRevenueHistoryByProductCategory = PartyProductCategoryRevenueHistories.PartyProductCategoryRevenueHistoryByProductCategory(internalOrganisation, party);

            var partyPackageRevenuesHistories = party.PartyPackageRevenueHistoriesWhereParty;
            partyPackageRevenuesHistories.Filter.AddEquals(PartyPackageRevenueHistories.Meta.InternalOrganisation, internalOrganisation);

            var priceComponents = GetPriceComponents(internalOrganisation, product, date);

            var revenueBreakDiscount = 0M;
            var revenueBreakSurcharge = 0M;

            foreach (var priceComponent in priceComponents)
            {
                if (priceComponent.Strategy.Class.Equals(DiscountComponents.Meta.ObjectType) || priceComponent.Strategy.Class.Equals(SurchargeComponents.Meta.ObjectType))
                {
                    if (PriceComponents.AppsIsEligible(new PriceComponents.IsEligibleParams
                                                           {
                                                               PriceComponent = priceComponent,
                                                               Customer = party,
                                                               Product = product,
                                                               SalesOrder = salesOrder,
                                                               SalesInvoice = salesInvoice,
                                                               PartyPackageRevenueHistoryList = partyPackageRevenuesHistories,
                                                               PartyRevenueHistory = partyRevenueHistory,
                                                               PartyProductCategoryRevenueHistoryByProductCategory = partyProductCategoryRevenueHistoryByProductCategory,
                                                           }))
                    {
                        if (priceComponent.Strategy.Class.Equals(DiscountComponents.Meta.ObjectType))
                        {
                            var discountComponent = (DiscountComponent)priceComponent;
                            decimal discount;

                            if (discountComponent.Price.HasValue)
                            {
                                discount = discountComponent.Price.Value;
                                productDiscount += discount;
                            }
                            else
                            {
                                var percentage = discountComponent.Percentage.HasValue ? discountComponent.Percentage.Value : 0;
                                discount = decimal.Round(((productBasePrice * percentage) / 100), 2);
                                productDiscount += discount;
                            }

                            ////Revenuebreaks on quantity and value are mutually exclusive.
                            if (priceComponent.ExistRevenueQuantityBreak || priceComponent.ExistRevenueValueBreak)
                            {
                                if (revenueBreakDiscount == 0)
                                {
                                    revenueBreakDiscount = discount;
                                }
                                else
                                {
                                    ////Apply highest of the two. Revert the other one.
                                    if (discount > revenueBreakDiscount)
                                    {
                                        productDiscount -= revenueBreakDiscount;
                                    }
                                    else
                                    {
                                        productDiscount -= discount;
                                    }
                                }
                            }
                        }

                        if (priceComponent.Strategy.Class.Equals(SurchargeComponents.Meta.ObjectType))
                        {
                            var surchargeComponent = (SurchargeComponent)priceComponent;
                            decimal surcharge;

                            if (surchargeComponent.Price.HasValue)
                            {
                                surcharge = surchargeComponent.Price.Value;
                                productSurcharge += surcharge;
                            }
                            else
                            {
                                var percentage = surchargeComponent.Percentage.HasValue ? surchargeComponent.Percentage.Value : 0;
                                surcharge = decimal.Round(((productBasePrice * percentage) / 100), 2);
                                productSurcharge += surcharge;
                            }

                            ////Revenuebreaks on quantity and value are mutually exclusive.
                            if (priceComponent.ExistRevenueQuantityBreak || priceComponent.ExistRevenueValueBreak)
                            {
                                if (revenueBreakSurcharge == 0)
                                {
                                    revenueBreakSurcharge = surcharge;
                                }
                                else
                                {
                                    ////Apply highest of the two. Revert the other one.
                                    if (surcharge > revenueBreakSurcharge)
                                    {
                                        productSurcharge -= revenueBreakSurcharge;
                                    }
                                    else
                                    {
                                        productSurcharge -= surcharge;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return productBasePrice - productDiscount + productSurcharge;
        }
Example #14
0
        private static decimal AppsCatalogPrice(
            SalesOrder salesOrder,
            SalesInvoice salesInvoice,
            Product product,
            DateTime date)
        {
            var productBasePrice = 0M;
            var productDiscount  = 0M;
            var productSurcharge = 0M;

            var baseprices = new PriceComponent[0];

            if (product.ExistBasePrices)
            {
                baseprices = product.BasePrices;
            }

            var party = salesOrder != null ? salesOrder.ShipToCustomer : salesInvoice != null ? salesInvoice.BillToCustomer : null;

            foreach (BasePrice priceComponent in baseprices)
            {
                if (priceComponent.FromDate <= date &&
                    (!priceComponent.ExistThroughDate || priceComponent.ThroughDate >= date))
                {
                    if (PriceComponents.AppsIsEligible(new PriceComponents.IsEligibleParams
                    {
                        PriceComponent = priceComponent,
                        Customer = party,
                        Product = product,
                        SalesOrder = salesOrder,
                        SalesInvoice = salesInvoice,
                    }))
                    {
                        if (priceComponent.ExistPrice)
                        {
                            if (productBasePrice == 0 || priceComponent.Price < productBasePrice)
                            {
                                productBasePrice = priceComponent.Price ?? 0;
                            }
                        }
                    }
                }
            }

            var priceComponents = GetPriceComponents(product, date);

            var revenueBreakDiscount  = 0M;
            var revenueBreakSurcharge = 0M;

            foreach (var priceComponent in priceComponents)
            {
                if (priceComponent.Strategy.Class.Equals(M.DiscountComponent.ObjectType) || priceComponent.Strategy.Class.Equals(M.SurchargeComponent.ObjectType))
                {
                    if (PriceComponents.AppsIsEligible(new PriceComponents.IsEligibleParams
                    {
                        PriceComponent = priceComponent,
                        Customer = party,
                        Product = product,
                        SalesOrder = salesOrder,
                        SalesInvoice = salesInvoice,
                    }))
                    {
                        if (priceComponent.Strategy.Class.Equals(M.DiscountComponent.ObjectType))
                        {
                            var     discountComponent = (DiscountComponent)priceComponent;
                            decimal discount;

                            if (discountComponent.Price.HasValue)
                            {
                                discount         = discountComponent.Price.Value;
                                productDiscount += discount;
                            }
                            else
                            {
                                var percentage = discountComponent.Percentage.HasValue ? discountComponent.Percentage.Value : 0;
                                discount         = Math.Round((productBasePrice * percentage) / 100, 2);
                                productDiscount += discount;
                            }
                        }

                        if (priceComponent.Strategy.Class.Equals(M.SurchargeComponent.ObjectType))
                        {
                            var     surchargeComponent = (SurchargeComponent)priceComponent;
                            decimal surcharge;

                            if (surchargeComponent.Price.HasValue)
                            {
                                surcharge         = surchargeComponent.Price.Value;
                                productSurcharge += surcharge;
                            }
                            else
                            {
                                var percentage = surchargeComponent.Percentage.HasValue ? surchargeComponent.Percentage.Value : 0;
                                surcharge         = Math.Round((productBasePrice * percentage) / 100, 2);
                                productSurcharge += surcharge;
                            }
                        }
                    }
                }
            }

            return(productBasePrice - productDiscount + productSurcharge);
        }
Example #15
0
 private static ProductCategoryRevenue CreateProductCategoryRevenue(ISession session, SalesInvoice invoice, ProductCategory productCategory)
 {
     return new ProductCategoryRevenueBuilder(session)
                 .WithInternalOrganisation(invoice.BilledFromInternalOrganisation)
                 .WithProductCategory(productCategory)
                 .WithProductCategoryName(productCategory.Description)
                 .WithYear(invoice.InvoiceDate.Year)
                 .WithMonth(invoice.InvoiceDate.Month)
                 .WithCurrency(invoice.BilledFromInternalOrganisation.PreferredCurrency)
                 .WithRevenue(0M)
                 .Build();
 }