示例#1
0
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>
        /// and <see cref="ITracingService"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
        /// The plug-in's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the plug-in. Also, multiple system threads
        /// could execute the plug-in at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in plug-ins.
        /// </remarks>
        protected void ExecutePreValidateAllocatedVehicleDelete(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            var    allocatedVehicle         = (EntityReference)context.InputParameters["Target"];
            string message = context.MessageName;
            string error   = "";

            trace.Trace("Condition Context");
            //if (context.Depth > 1) { return; }

            trace.Trace("Start Try catch");
            try
            {
                AllocatedVehicleHandler allocatedVehicleHandler = new AllocatedVehicleHandler(service, trace);

                EntityCollection allocatedRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_allocatedvehicle", "gsc_iv_allocatedvehicleid", allocatedVehicle.Id, service,
                                                                                            null, OrderType.Ascending, new[] { "gsc_orderid", "gsc_inventoryid", "gsc_vehicletransferid", "gsc_vehicleintransittransferid" });

                if (allocatedRecords != null && allocatedRecords.Entities.Count > 0)
                {
                    Entity allocatedEntity = allocatedRecords.Entities[0];
                    allocatedVehicleHandler.IsSubjectforPDI(allocatedEntity);
                    allocatedVehicleHandler.RemoveAllocation(allocatedEntity);
                }
            }
            catch (Exception ex)
            {
                //if(ex.Message.Contains("Unable to delete record that is already shipped."))
                // throw new InvalidPluginExecutionException("Unable to delete record that is already shipped.");
                // else
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
        public void AddTotalPremium()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Insurance Entity
            var InsuranceEntity = new EntityCollection()
            {
                EntityName = "gsc_cmn_insurance",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_insurance",
                        Attributes  =
                        {
                            { "gsc_totalpremium", new Money(0) },
                        }
                    }
                }
            };
            #endregion

            #region Coverage Entity Collection
            var CoverageEntity = new EntityCollection()
            {
                EntityName = "gsc_cmn_insurancecoverage",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_cmn_insurancecoverage",
                        Attributes  =
                        {
                            { "gsc_premium", new Money(1000) }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == OrderEntity.EntityName)
                                      ))).Returns(OrderEntity);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == Inventory.EntityName)
                                      ))).Returns(Inventory);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == ProductQuantity.EntityName)
                                      ))).Returns(ProductQuantity);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == OrderEntity.EntityName)))).Callback <Entity>(s => OrderEntity.Entities[0] = s);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == Inventory.EntityName)))).Callback <Entity>(s => Inventory.Entities[0] = s);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == ProductQuantity.EntityName)))).Callback <Entity>(s => ProductQuantity.Entities[0] = s);

            #endregion

            #region 2. Call / Action
            var AllocateVehicleHandler = new AllocatedVehicleHandler(orgService, orgTracing);
            AllocateVehicleHandler.RemoveAllocation(AllocatedVehicle.Entities[0]);
            #endregion

            #region 3. Verify
            Assert.AreEqual(100000002, OrderEntity.Entities[0].GetAttributeValue <OptionSetValue>("gsc_status").Value);
            Assert.AreEqual(null, OrderEntity.Entities[0].GetAttributeValue <String>("gsc_inventoryidtoallocate"));
            Assert.AreEqual(null, OrderEntity.Entities[0].GetAttributeValue <DateTime>("gsc_vehicleallocateddate"));
            Assert.AreEqual(100000000, Inventory.Entities[0].GetAttributeValue <OptionSetValue>("gsc_status").Value);
            Assert.AreEqual(3, ProductQuantity.Entities[0].GetAttributeValue <Int32>("gsc_available"));
            Assert.AreEqual(0, ProductQuantity.Entities[0].GetAttributeValue <Int32>("gsc_allocated"));
            #endregion
        }
        public void RemoveAllocation()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Order Entity
            var OrderEntity = new EntityCollection()
            {
                EntityName = "order",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "order",
                        Attributes  =
                        {
                            { "gsc_inventoryidtoallocate", "1a0effa2-2d1b-e611-80d8-00155d010e2c"                },
                            { "gsc_status",                new OptionSetValue(100000004)                         },
                            { "gsc_vehicleallocateddate",  DateTime.Today.ToString("MM-dd-yyyy")                 }
                        }
                    }
                }
            };
            #endregion

            #region Product Quantity Entity Collection
            var ProductQuantity = new EntityCollection()
            {
                EntityName = "gsc_iv_productquantity",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_productquantity",
                        Attributes  =
                        {
                            { "gsc_available", 2 },
                            { "gsc_allocated", 1 }
                        }
                    }
                }
            };
            #endregion

            #region Inventory Entity Collection
            var Inventory = new EntityCollection()
            {
                EntityName = "gsc_iv_inventory",
                Entities   =
                {
                    new Entity
                    {
                        Id          = new Guid("1a0effa2-2d1b-e611-80d8-00155d010e2c"),
                        LogicalName = "gsc_iv_inventory",
                        Attributes  =
                        {
                            { "gsc_color",             "Black"                                       },
                            { "gsc_csno",              "1"                                           },
                            { "gsc_engineno",          "2"                                           },
                            { "gsc_modelcode",         "3"                                           },
                            { "gsc_optioncode",        "4"                                           },
                            { "gsc_productionno",      "5"                                           },
                            { "gsc_vin",               "6"                                           },
                            { "gsc_status",            new OptionSetValue(100000001)                 },
                            { "gsc_productquantityid", new EntityReference(ProductQuantity.EntityName, ProductQuantity.Entities[0].Id)}
                        }
                    }
                }
            };
            #endregion

            #region Allocated Vehicle Entity
            var AllocatedVehicle = new EntityCollection()
            {
                EntityName = "gsc_iv_allocatedvehicle",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "gsc_iv_allocatedvehicle",
                        Attributes  =
                        {
                            { "gsc_orderid",     new EntityReference(OrderEntity.EntityName,     OrderEntity.Entities[0].Id)   },
                            { "gsc_inventoryid", new EntityReference(Inventory.EntityName,       Inventory.Entities[0].Id)     }
                        }
                    }
                }
            };
            #endregion

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == OrderEntity.EntityName)
                                      ))).Returns(OrderEntity);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == Inventory.EntityName)
                                      ))).Returns(Inventory);

            orgServiceMock.Setup((service => service.RetrieveMultiple(
                                      It.Is <QueryExpression>(expression => expression.EntityName == ProductQuantity.EntityName)
                                      ))).Returns(ProductQuantity);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == OrderEntity.EntityName)))).Callback <Entity>(s => OrderEntity.Entities[0] = s);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == Inventory.EntityName)))).Callback <Entity>(s => Inventory.Entities[0] = s);

            orgServiceMock.Setup((service => service.Update(It.Is <Entity>(entity => entity.LogicalName == ProductQuantity.EntityName)))).Callback <Entity>(s => ProductQuantity.Entities[0] = s);

            #endregion

            #region 2. Call / Action
            var AllocateVehicleHandler = new AllocatedVehicleHandler(orgService, orgTracing);
            AllocateVehicleHandler.RemoveAllocation(AllocatedVehicle.Entities[0]);
            #endregion

            #region 3. Verify
            Assert.AreEqual(100000002, OrderEntity.Entities[0].GetAttributeValue <OptionSetValue>("gsc_status").Value);
            Assert.AreEqual(null, OrderEntity.Entities[0].GetAttributeValue <String>("gsc_inventoryidtoallocate"));
            Assert.AreEqual(null, OrderEntity.Entities[0].GetAttributeValue <DateTime>("gsc_vehicleallocateddate"));
            Assert.AreEqual(100000000, Inventory.Entities[0].GetAttributeValue <OptionSetValue>("gsc_status").Value);
            Assert.AreEqual(3, ProductQuantity.Entities[0].GetAttributeValue <Int32>("gsc_available"));
            Assert.AreEqual(0, ProductQuantity.Entities[0].GetAttributeValue <Int32>("gsc_allocated"));
            #endregion
        }