/// <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 ExecutePreVehicleCabChassisCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity vehicleCabChassis        = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;

            try
            {
                PriceListItemHandler priceListHandler = new PriceListItemHandler(service, trace);
                priceListHandler.OnImportGetVehicle(vehicleCabChassis);

                VehicleCabChassisHandler cahChassisHandler = new VehicleCabChassisHandler(service, trace);
                cahChassisHandler.OnImportVehicleCabChassis(vehicleCabChassis);

                if (cahChassisHandler.IsCabChassisDuplicate(vehicleCabChassis))
                {
                    throw new InvalidPluginExecutionException("Cannot add duplicate item.");
                }

                cahChassisHandler.ReplicateAccessoryDetail(vehicleCabChassis);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Example #2
0
        public void OnImportVehiclePriceListItem(Entity extendedPriceListItem)
        {
            _tracingService.Trace("Started OnImportVehiclePriceListItem Method..");

            var    productId     = CommonHandler.GetEntityReferenceIdSafe(extendedPriceListItem, "gsc_productid");
            Entity productEntity = new Entity();

            if (productId != Guid.Empty)
            {
                _tracingService.Trace("Product not empty.");

                productEntity = GetProductDetails(productId);
                var productType = productEntity.Contains("gsc_producttype")
                    ? productEntity.GetAttributeValue <OptionSetValue>("gsc_producttype").Value
                    : 0;

                //Not Vehicle
                if (productType != 100000000)
                {
                    _tracingService.Trace("Product not vehicle.");
                    return;
                }

                _tracingService.Trace("Product is a vehicle.");
            }
            else
            {
                _tracingService.Trace("Product Id empty.");

                var optionCode = extendedPriceListItem.Contains("gsc_optioncode")
                    ? extendedPriceListItem.GetAttributeValue <String>("gsc_optioncode")
                    : String.Empty;
                var modelCode = extendedPriceListItem.Contains("gsc_modelcode")
                    ? extendedPriceListItem.GetAttributeValue <String>("gsc_modelcode")
                    : String.Empty;

                //Not Vehicle Price List item
                if (optionCode == String.Empty && modelCode == String.Empty)
                {
                    _tracingService.Trace("Product not vehicle.");
                    return;
                }
                else
                {
                    _tracingService.Trace("Product is a vehicle.");
                    PriceListItemHandler priceListItemHandler = new PriceListItemHandler(_organizationService, _tracingService);
                    productEntity = priceListItemHandler.OnImportGetVehicle(extendedPriceListItem);
                }
            }

            UpdateExtendedPriceListItemDetails(extendedPriceListItem, productEntity);
            Guid priceListItemId = CreatePriceLisItem(extendedPriceListItem, productEntity);

            AssociateExtendedtoPriceListItem(extendedPriceListItem.Id, priceListItemId);

            _tracingService.Trace("Ended OnImportVehiclePriceListItem Method..");
        }
Example #3
0
        //Created By: Leslie g. Baliguat, Created On: 01/06/17

        /*Purpose: Unchecks other price list tagged as default and updates vehicle sell price from
         * Event/Message:
         *      Post/Update: Default Price List = gsc_default
         *      Post/Create: Default Price List = gsc_default
         * Primary Entity: Sales Return Detail
         */
        public void ChangeDefaultPriceList(Entity priceList)
        {
            _tracingService.Trace("Started ChangeDefaultPriceList method..");

            if (!priceList.GetAttributeValue <Boolean>("gsc_default"))
            {
                return;
            }

            var transactionType = priceList.Contains("gsc_transactiontype")
                ? priceList.GetAttributeValue <OptionSetValue>("gsc_transactiontype").Value
                : 0;

            var productConditionList = new List <ConditionExpression>
            {
                new ConditionExpression("pricelevelid", ConditionOperator.NotEqual, priceList.Id),
                new ConditionExpression("gsc_transactiontype", ConditionOperator.Equal, transactionType),
                new ConditionExpression("gsc_default", ConditionOperator.Equal, true)
            };

            EntityCollection priceListCollection = CommonHandler.RetrieveRecordsByConditions("pricelevel", productConditionList, _organizationService, null, OrderType.Ascending,
                                                                                             new[] { "gsc_default" });


            if (priceListCollection != null && priceListCollection.Entities.Count > 0)
            {
                foreach (var defaultPriceList in priceListCollection.Entities)
                {
                    defaultPriceList["gsc_default"] = false;
                    _organizationService.Update(defaultPriceList);

                    _tracingService.Trace("Other default price list updated.");
                }
            }
            //productpricelevel

            if (transactionType == 100000000)//Vehicle Sales
            {
                EntityCollection priceListItemCollection = CommonHandler.RetrieveRecordsByOneValue("productpricelevel", "pricelevelid", priceList.Id, _organizationService, null, OrderType.Ascending,
                                                                                                   new[] { "amount", "productid", "pricelevelid" });

                if (priceListItemCollection != null && priceListItemCollection.Entities.Count > 0)
                {
                    foreach (var priceListItem in priceListItemCollection.Entities)
                    {
                        _tracingService.Trace("Price List Item Retrieved.");

                        PriceListItemHandler priceListItemHandler = new PriceListItemHandler(_organizationService, _tracingService);
                        priceListItemHandler.UpdateVehicleSellPrice(priceListItem);
                    }
                }
            }

            _tracingService.Trace("Ended ChangeDefaultPriceList method..");
        }
Example #4
0
        public void OnImportVehiclePriceListItem(Entity extendedPriceListItem)
        {
            if (extendedPriceListItem.Contains("gsc_productid"))
            {
                if (extendedPriceListItem.GetAttributeValue <EntityReference>("gsc_productid") != null)
                {
                    _tracingService.Trace("Extended Created manually.");
                    return;
                }
            }

            var optionCode = extendedPriceListItem.Contains("gsc_optioncode")
                ? extendedPriceListItem.GetAttributeValue <String>("gsc_optioncode")
                : String.Empty;
            var modelCode = extendedPriceListItem.Contains("gsc_modelcode")
                ? extendedPriceListItem.GetAttributeValue <String>("gsc_modelcode")
                : String.Empty;

            //Not Vehicle Price List item
            if (optionCode == String.Empty && modelCode == String.Empty)
            {
                return;
            }

            _tracingService.Trace("Started UpdateExtendedPriceListItem Method..");

            PriceListItemHandler priceListItemHandler = new PriceListItemHandler(_organizationService, _tracingService);
            var productEntity = priceListItemHandler.OnImportGetVehicle(extendedPriceListItem);

            var priceListName = extendedPriceListItem.GetAttributeValue <EntityReference>("gsc_pricelistid") != null
                ? extendedPriceListItem.GetAttributeValue <EntityReference>("gsc_pricelistid").Name
                : "";

            extendedPriceListItem["gsc_productid"] = new EntityReference("product", productEntity.Id);
            extendedPriceListItem["gsc_extendedpricelistitempn"] = priceListName;
            extendedPriceListItem["gsc_modelyear"] = productEntity.Contains("gsc_modelyear")
                ? productEntity.GetAttributeValue <String>("gsc_modelyear")
                : String.Empty;
            extendedPriceListItem["gsc_basemodel"] = productEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid") != null
                ? productEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Name
                : String.Empty;

            _organizationService.Update(extendedPriceListItem);

            _tracingService.Trace("Ended UpdateExtendedPriceListItem Method..");

            CreatePriceLisItem(extendedPriceListItem, productEntity);
        }
Example #5
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 ExecutePostPriceListItemCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity priceListItemEntity      = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;
            string error   = "";

            try
            {
                PriceListItemHandler priceListItemHandler = new PriceListItemHandler(service, trace);

                #region Calling SetStandardSellPriceAmount method
                EntityCollection priceListItemRecords = CommonHandler.RetrieveRecordsByOneValue("productpricelevel", "productpricelevelid", priceListItemEntity.Id, service, null, OrderType.Ascending,
                                                                                                new[] { "productid", "pricelevelid", "amount", "transactioncurrencyid" });

                if (priceListItemRecords != null && priceListItemRecords.Entities.Count > 0)
                {
                    Entity priceListItem = priceListItemRecords.Entities[0];
                    priceListItemHandler.CheckifPriceListisDefault(priceListItem);
                    //    priceListItemHandler.CreateExtendedPriceListItemRecord(priceListItem);
                }
                #endregion
            }
            catch (Exception ex)
            {
                //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Example #6
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 ExecutePreVehicleColorCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }



            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService    service = localContext.OrganizationService;
            ITracingService         trace   = localContext.TracingService;
            Entity entity = (Entity)context.InputParameters["Target"];

            string message = context.MessageName;
            string error   = "";

            try
            {
                PriceListItemHandler priceListHandler = new PriceListItemHandler(service, trace);
                priceListHandler.OnImportGetVehicle(entity);

                VehicleColorHandler vehicleColorHandler = new VehicleColorHandler(service, trace);
                vehicleColorHandler.OnImportVehicleColor(entity);

                if (vehicleColorHandler.IsColorDuplicate(entity))
                {
                    throw new InvalidPluginExecutionException("Cannot add duplicate color.");
                }

                vehicleColorHandler.PopulateColorCode(entity);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Example #7
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 ExecutePostPriceListItemUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;

            Entity preImageEntity  = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

            IOrganizationService service = localContext.OrganizationService;
            ITracingService      trace   = localContext.TracingService;

            if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity))
            {
                return;
            }

            Entity priceListItemEntity = (Entity)context.InputParameters["Target"];

            if (priceListItemEntity.LogicalName != "productpricelevel")
            {
                return;
            }

            if (context.Mode == 0) //Synchronous Plugin
            {
                try
                {
                    #region Pre-images
                    var preImageAmount = preImageEntity.GetAttributeValue <Money>("amount") != null
                        ? preImageEntity.GetAttributeValue <Money>("amount").Value
                        : Decimal.Zero;

                    var preImageProductId = preImageEntity.GetAttributeValue <EntityReference>("productid") != null
                        ? preImageEntity.GetAttributeValue <EntityReference>("productid").Id
                        : Guid.Empty;

                    #endregion

                    #region Post-images
                    var postImageAmount = postImageEntity.GetAttributeValue <Money>("amount") != null
                        ? postImageEntity.GetAttributeValue <Money>("amount").Value
                        : Decimal.Zero;

                    var postImageProductId = postImageEntity.GetAttributeValue <EntityReference>("productid") != null
                        ? postImageEntity.GetAttributeValue <EntityReference>("productid").Id
                        : Guid.Empty;

                    #endregion

                    if (preImageAmount != postImageAmount || preImageProductId != postImageProductId)
                    {
                        string message = context.MessageName;

                        PriceListItemHandler priceListItemHandler = new PriceListItemHandler(service, trace);
                        priceListItemHandler.CheckifPriceListisDefault(postImageEntity);
                    }
                }

                catch (Exception ex)
                {
                    //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error));
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
        }
        public void SetStandardSellPriceAmountUnitTest()
        {
            #region 1. Setup / Arrange
            var orgServiceMock = new Mock <IOrganizationService>();
            var orgService     = orgServiceMock.Object;
            var orgTracingMock = new Mock <ITracingService>();
            var orgTracing     = orgTracingMock.Object;

            #region Price List Item EntityCollection
            var PriceListItemCollection = new EntityCollection
            {
                EntityName = "productpricelevel",
                Entities   =
                {
                    new Entity
                    {
                        Id          = Guid.NewGuid(),
                        LogicalName = "productpricelevel",
                        EntityState = EntityState.Changed,
                        Attributes  = new AttributeCollection
                        {
                            { "pricelevelid", new EntityReference("pricelevel", Guid.NewGuid())
                                {
                                    Name = "Standard Price List"
                                } },
                            { "productid",    new EntityReference("product", Guid.NewGuid())
                                {
                                    Name = "Montero"
                                } },
                            { "amount",       new Money(((Decimal)2000000.00)) }
                        }
                    }
                }
            };
            #endregion

            #region Product EntityCollection
            var ProductCollection = new EntityCollection
            {
                EntityName = "product",
                Entities   =
                {
                    new Entity
                    {
                        Id          = PriceListItemCollection.Entities[0].GetAttributeValue <EntityReference>("productid").Id,
                        LogicalName = "product",
                        EntityState = EntityState.Created,
                        Attributes  = new AttributeCollection
                        {
                            { "gsc_sellprice", new Money(Decimal.Zero) },
                        }
                    }
                }
            };
            #endregion

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

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

            #endregion

            #region 2. Call/Action

            var PriceListItemHandler = new PriceListItemHandler(orgService, orgTracing);
            //Entity product = PriceListItemHandler.SetStandardSellPriceAmount(PriceListItemCollection.Entities[0], "Create");
            #endregion

            #region 3. Verify
            //Assert.AreEqual(PriceListItemCollection.Entities[0].GetAttributeValue<Money>("amount").Value, ProductCollection.Entities[0].GetAttributeValue<Money>("gsc_sellprice").Value);
            #endregion
        }