Summary

Class:FakeXrmEasy.Services.InvoiceDetailInitializerService
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\Services\EntityInitializer\InvoiceDetailInitializerService.cs
Covered lines:75
Uncovered lines:3
Coverable lines:78
Total lines:123
Line coverage:96.1%
Branch coverage:100%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
Initialize(...)23100100
Initialize(...)100

File(s)

F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\Services\EntityInitializer\InvoiceDetailInitializerService.cs

#LineLine coverage
 1using Microsoft.Xrm.Sdk;
 2using Microsoft.Xrm.Sdk.Query;
 3using System;
 4using System.Collections.Generic;
 5using System.Text;
 6
 7namespace FakeXrmEasy.Services
 8{
 9    public class InvoiceDetailInitializerService : IEntityInitializerService
 10    {
 11        public const string EntityLogicalName = "invoicedetail";
 12
 13        public Entity Initialize(Entity e, Guid gCallerId, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = fa
 7214        {
 7215            Entity invoice = null;
 16
 7217            var invoiceReference = e.GetAttributeValue<EntityReference>("invoiceid");
 7218             if (invoiceReference != null)
 6619            {
 6620                invoice = ctx.Service.Retrieve(invoiceReference.LogicalName, invoiceReference.Id, new ColumnSet(true));
 6621            }
 22
 7223            var isPriceOverriden = e.GetAttributeValue<bool>("ispriceoverridden");
 24            //if Pricing set to "Use Default"
 7225             if (!isPriceOverriden)
 6626            {
 27                //retrieve price per unit from product price list item if it's not set in the invoice
 6628                Entity product = null;
 6629                var productReference = e.GetAttributeValue<EntityReference>("productid");
 30
 6631                 if (productReference != null)
 5432                    product = ctx.Service.Retrieve(productReference.LogicalName, productReference.Id, new ColumnSet(true
 33
 6634                Entity productOrInvoice = invoice;
 6635                 if (productOrInvoice == null)
 636                    productOrInvoice = product;
 37
 6638                EntityReference priceLevelReference = null;
 39
 40                //get price level
 6641                 if (productOrInvoice != null)
 6042                    priceLevelReference = productOrInvoice.GetAttributeValue<EntityReference>("pricelevelid");
 43
 44                //get UoM
 6645                var uomReference = e.GetAttributeValue<EntityReference>("uomid");
 6646                 if (uomReference == null && product != null)
 2447                {
 2448                    uomReference = product.GetAttributeValue<EntityReference>("defaultuomscheduleid");
 2449                     if (uomReference == null)
 1850                        uomReference = product.GetAttributeValue<EntityReference>("defaultuomid");
 51
 52                    //set uom to invoice detail
 2453                     if (uomReference != null)
 1254                        e["uomid"] = uomReference;
 2455                }
 56
 6657                 if (priceLevelReference != null && productReference != null && uomReference != null)
 4258                {
 4259                    var queryByAttribute = new QueryByAttribute
 4260                    {
 4261                        ColumnSet = new ColumnSet(true),
 4262                        EntityName = "productpricelevel"
 4263                    };
 64
 4265                    queryByAttribute.AddAttributeValue("pricelevelid", priceLevelReference.Id);
 4266                    queryByAttribute.AddAttributeValue("productid", productReference.Id);
 4267                    queryByAttribute.AddAttributeValue("uomid", uomReference.Id);
 68
 4269                    var result = ctx.Service.RetrieveMultiple(queryByAttribute);
 4270                     if (result.Entities.Count > 0)
 3671                    {
 3672                        e["priceperunit"] = result.Entities[0].GetAttributeValue<Money>("amount");
 3673                    }
 4274                }
 6675            }
 76
 7277             if (!e.Contains("priceperunit") || (e.Contains("priceperunit") && e["priceperunit"] == null))
 3078            {
 3079                e["priceperunit"] = new Money(0m);
 3080            }
 81
 82            //calculate other amounts
 7283            var quantity = e.GetAttributeValue<decimal>("quantity");
 7284             if (quantity <= 0m)
 5485            {
 5486                quantity = 1m;
 5487                e["quantity"] = quantity;
 5488            }
 89
 7290            var pricePerUnit = e.GetAttributeValue<Money>("priceperunit");
 91
 7292            decimal extendedAmount = Math.Round(pricePerUnit.Value * quantity, 2);
 93
 7294            e["amount"] = new Money(extendedAmount);
 95
 7296            var manualDiscount = e.GetAttributeValue<Money>("manualdiscountamount");
 7297             if (manualDiscount != null)
 698                extendedAmount -= manualDiscount.Value;
 99
 72100            var tax = e.GetAttributeValue<Money>("tax");
 72101             if (tax != null)
 6102                extendedAmount += tax.Value;
 103
 72104            e["extendedamount"] = new Money(extendedAmount);
 105
 72106             if (invoice != null)
 66107            {
 66108                 var totalAmount = invoice.GetAttributeValue<Money>("totalamount") ?? new Money(0m);
 66109                totalAmount.Value += ((Money)e["extendedamount"]).Value;
 110
 66111                invoice["totalamount"] = totalAmount;
 66112                ctx.Service.Update(invoice);
 66113            }
 114
 72115            return e;
 72116        }
 117
 118        public Entity Initialize(Entity e, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = false)
 0119        {
 0120            return this.Initialize(e, Guid.NewGuid(), ctx, isManyToManyRelationshipEntity);
 0121        }
 122    }
 123}