| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using Microsoft.Xrm.Sdk.Query; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Text; |
| | 6 | |
|
| | 7 | | namespace 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 |
| 72 | 14 | | { |
| 72 | 15 | | Entity invoice = null; |
| | 16 | |
|
| 72 | 17 | | var invoiceReference = e.GetAttributeValue<EntityReference>("invoiceid"); |
| 72 | 18 | | if (invoiceReference != null) |
| 66 | 19 | | { |
| 66 | 20 | | invoice = ctx.Service.Retrieve(invoiceReference.LogicalName, invoiceReference.Id, new ColumnSet(true)); |
| 66 | 21 | | } |
| | 22 | |
|
| 72 | 23 | | var isPriceOverriden = e.GetAttributeValue<bool>("ispriceoverridden"); |
| | 24 | | //if Pricing set to "Use Default" |
| 72 | 25 | | if (!isPriceOverriden) |
| 66 | 26 | | { |
| | 27 | | //retrieve price per unit from product price list item if it's not set in the invoice |
| 66 | 28 | | Entity product = null; |
| 66 | 29 | | var productReference = e.GetAttributeValue<EntityReference>("productid"); |
| | 30 | |
|
| 66 | 31 | | if (productReference != null) |
| 54 | 32 | | product = ctx.Service.Retrieve(productReference.LogicalName, productReference.Id, new ColumnSet(true |
| | 33 | |
|
| 66 | 34 | | Entity productOrInvoice = invoice; |
| 66 | 35 | | if (productOrInvoice == null) |
| 6 | 36 | | productOrInvoice = product; |
| | 37 | |
|
| 66 | 38 | | EntityReference priceLevelReference = null; |
| | 39 | |
|
| | 40 | | //get price level |
| 66 | 41 | | if (productOrInvoice != null) |
| 60 | 42 | | priceLevelReference = productOrInvoice.GetAttributeValue<EntityReference>("pricelevelid"); |
| | 43 | |
|
| | 44 | | //get UoM |
| 66 | 45 | | var uomReference = e.GetAttributeValue<EntityReference>("uomid"); |
| 66 | 46 | | if (uomReference == null && product != null) |
| 24 | 47 | | { |
| 24 | 48 | | uomReference = product.GetAttributeValue<EntityReference>("defaultuomscheduleid"); |
| 24 | 49 | | if (uomReference == null) |
| 18 | 50 | | uomReference = product.GetAttributeValue<EntityReference>("defaultuomid"); |
| | 51 | |
|
| | 52 | | //set uom to invoice detail |
| 24 | 53 | | if (uomReference != null) |
| 12 | 54 | | e["uomid"] = uomReference; |
| 24 | 55 | | } |
| | 56 | |
|
| 66 | 57 | | if (priceLevelReference != null && productReference != null && uomReference != null) |
| 42 | 58 | | { |
| 42 | 59 | | var queryByAttribute = new QueryByAttribute |
| 42 | 60 | | { |
| 42 | 61 | | ColumnSet = new ColumnSet(true), |
| 42 | 62 | | EntityName = "productpricelevel" |
| 42 | 63 | | }; |
| | 64 | |
|
| 42 | 65 | | queryByAttribute.AddAttributeValue("pricelevelid", priceLevelReference.Id); |
| 42 | 66 | | queryByAttribute.AddAttributeValue("productid", productReference.Id); |
| 42 | 67 | | queryByAttribute.AddAttributeValue("uomid", uomReference.Id); |
| | 68 | |
|
| 42 | 69 | | var result = ctx.Service.RetrieveMultiple(queryByAttribute); |
| 42 | 70 | | if (result.Entities.Count > 0) |
| 36 | 71 | | { |
| 36 | 72 | | e["priceperunit"] = result.Entities[0].GetAttributeValue<Money>("amount"); |
| 36 | 73 | | } |
| 42 | 74 | | } |
| 66 | 75 | | } |
| | 76 | |
|
| 72 | 77 | | if (!e.Contains("priceperunit") || (e.Contains("priceperunit") && e["priceperunit"] == null)) |
| 30 | 78 | | { |
| 30 | 79 | | e["priceperunit"] = new Money(0m); |
| 30 | 80 | | } |
| | 81 | |
|
| | 82 | | //calculate other amounts |
| 72 | 83 | | var quantity = e.GetAttributeValue<decimal>("quantity"); |
| 72 | 84 | | if (quantity <= 0m) |
| 54 | 85 | | { |
| 54 | 86 | | quantity = 1m; |
| 54 | 87 | | e["quantity"] = quantity; |
| 54 | 88 | | } |
| | 89 | |
|
| 72 | 90 | | var pricePerUnit = e.GetAttributeValue<Money>("priceperunit"); |
| | 91 | |
|
| 72 | 92 | | decimal extendedAmount = Math.Round(pricePerUnit.Value * quantity, 2); |
| | 93 | |
|
| 72 | 94 | | e["amount"] = new Money(extendedAmount); |
| | 95 | |
|
| 72 | 96 | | var manualDiscount = e.GetAttributeValue<Money>("manualdiscountamount"); |
| 72 | 97 | | if (manualDiscount != null) |
| 6 | 98 | | extendedAmount -= manualDiscount.Value; |
| | 99 | |
|
| 72 | 100 | | var tax = e.GetAttributeValue<Money>("tax"); |
| 72 | 101 | | if (tax != null) |
| 6 | 102 | | extendedAmount += tax.Value; |
| | 103 | |
|
| 72 | 104 | | e["extendedamount"] = new Money(extendedAmount); |
| | 105 | |
|
| 72 | 106 | | if (invoice != null) |
| 66 | 107 | | { |
| 66 | 108 | | var totalAmount = invoice.GetAttributeValue<Money>("totalamount") ?? new Money(0m); |
| 66 | 109 | | totalAmount.Value += ((Money)e["extendedamount"]).Value; |
| | 110 | |
|
| 66 | 111 | | invoice["totalamount"] = totalAmount; |
| 66 | 112 | | ctx.Service.Update(invoice); |
| 66 | 113 | | } |
| | 114 | |
|
| 72 | 115 | | return e; |
| 72 | 116 | | } |
| | 117 | |
|
| | 118 | | public Entity Initialize(Entity e, XrmFakedContext ctx, bool isManyToManyRelationshipEntity = false) |
| 0 | 119 | | { |
| 0 | 120 | | return this.Initialize(e, Guid.NewGuid(), ctx, isManyToManyRelationshipEntity); |
| 0 | 121 | | } |
| | 122 | | } |
| | 123 | | } |