Example #1
0
        //Handles logic for removing vehicle adjustment details
        private Entity UnallocateVehicleAdjustment(Entity adjustedVehcileDetailsEntity, Entity vehicleAdjustmentEntity, string caller)
        {
            var inventoryId = adjustedVehcileDetailsEntity.Contains("gsc_inventoryid") ? adjustedVehcileDetailsEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                        : Guid.Empty;

            if (inventoryId != Guid.Empty)
            {
                var quantity = adjustedVehcileDetailsEntity.Contains("gsc_quantity") ? adjustedVehcileDetailsEntity.GetAttributeValue <Int32>("gsc_quantity") : 0;
                EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService,
                                                                                               null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid", "gsc_basemodelid", "gsc_productid", "gsc_modelcode", "gsc_optioncode", "gsc_modelyear", "gsc_siteid",
                                                                                                                                  "gsc_vin", "gsc_csno", "gsc_productionno", "gsc_engineno", });

                _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count);
                if (inventoryCollection.Entities.Count > 0)
                {
                    _organizationService.Delete(adjustedVehcileDetailsEntity.LogicalName, adjustedVehcileDetailsEntity.Id);

                    _tracingService.Trace("Deleted inventory record...");

                    //negative adjustment logic
                    if (quantity < 0)
                    {
                        Entity inventoryEntity = inventoryCollection.Entities[0];
                        InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService);
                        inventoryMovementHandler.UpdateInventoryStatus(inventoryEntity, 100000000);
                        Entity productQuantityEntity = inventoryMovementHandler.UpdateProductQuantity(inventoryEntity, 0, 1, -1, 0, 0, 0, 0, 0);

                        #region Inventory History Log Creation
                        InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);

                        if (caller == "Cancel")
                        {
                            inventoryMovement.CreateInventoryQuantityAllocated(vehicleAdjustmentEntity, inventoryEntity, productQuantityEntity, vehicleAdjustmentEntity.GetAttributeValue <string>("gsc_vehicleadjustmentvarianceentrypn"),
                                                                               DateTime.UtcNow, "Cancel", Guid.Empty, 100000004);
                        }
                        else
                        {
                            inventoryMovement.CreateInventoryQuantityAllocated(vehicleAdjustmentEntity, inventoryEntity, productQuantityEntity, vehicleAdjustmentEntity.GetAttributeValue <string>("gsc_vehicleadjustmentvarianceentrypn"),
                                                                               DateTime.UtcNow, "Open", Guid.Empty, 100000003);
                        }
                        #endregion
                    }
                }
            }
            else
            {
                _organizationService.Delete(adjustedVehcileDetailsEntity.LogicalName, adjustedVehcileDetailsEntity.Id);
            }

            return(adjustedVehcileDetailsEntity);
        }
Example #2
0
        //Created By: Jerome Anthony Gerero, Created On: 2/15/2017

        /*Purpose: Delete selected allocated vehicle
         * Registration Details:
         * Event/Message:
         *      Post/Update: gsc_allocateditemstodelete
         * Primary Entity: Vehicle Transfer
         */
        public Entity DeleteAllocatedVehicle(Entity vehicleTransferEntity)
        {
            _tracingService.Trace("Started DeleteAllocatedVehicle method...");

            Guid vehicleTransferDetailId = vehicleTransferEntity.Contains("gsc_allocateditemstodelete")
                ? new Guid(vehicleTransferEntity.GetAttributeValue <String>("gsc_allocateditemstodelete"))
                : Guid.Empty;

            if (vehicleTransferDetailId == Guid.Empty)
            {
                return(null);
            }

            //Retrieve allocated vehicle record
            EntityCollection vehicleTransferDetailRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicletransferdetails", "gsc_iv_vehicletransferdetailsid", vehicleTransferDetailId, _organizationService, null, OrderType.Ascending,
                                                                                                    new[] { "gsc_inventoryid" });

            if (vehicleTransferDetailRecords != null && vehicleTransferDetailRecords.Entities.Count > 0)
            {
                Entity vehicleTransferDetail = vehicleTransferDetailRecords.Entities[0];

                Guid inventoryId = vehicleTransferDetail.Contains("gsc_inventoryid")
                    ? vehicleTransferDetail.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                    : Guid.Empty;

                //Retrieve inventory record
                EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                            new[] { "gsc_status", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_optioncode", "gsc_productionno", "gsc_vin", "gsc_productquantityid", "gsc_modelyear", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });

                if (inventoryRecords != null && inventoryRecords.Entities.Count > 0)
                {
                    Entity inventory = inventoryRecords.Entities[0];

                    inventory["gsc_status"] = new OptionSetValue(100000000);

                    _organizationService.Update(inventory);

                    InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService);

                    Entity productQuantity = inventoryMovementHandler.UpdateProductQuantity(inventory, 0, 1, -1, 0, 0, 0, 0, 0);
                    inventoryMovementHandler.CreateInventoryQuantityAllocated(vehicleTransferEntity, inventory, productQuantity, vehicleTransferEntity.GetAttributeValue <string>("gsc_vehicletransferpn"),
                                                                              DateTime.UtcNow, "Cancelled", vehicleTransferEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id, 100000003);
                }

                _organizationService.Delete("gsc_iv_vehicletransferdetails", vehicleTransferDetail.Id);
            }

            vehicleTransferEntity["gsc_allocateditemstodelete"] = null;

            _organizationService.Update(vehicleTransferEntity);

            _tracingService.Trace("Ended DeleteAllocatedVehicle method...");
            return(vehicleTransferEntity);
        }
Example #3
0
        //Created By : Raphael Herrera, Created On : 7/18/2016

        /*Purpose: Adjust Allocated, Available and On-hand product quantity. Handles Post Transactions, Cancel Transaction and Allocate Vehicle
         * Registration Details:
         * Event/Message:
         *      Pre/Create:
         *      Post/Update:
         *      Post/Create:
         * Primary Entity: Return Transaction Detail
         */
        public void AdjustProductQuantity(Entity returnTransaction, string caller)
        {
            _tracingService.Trace("Started AdjustProductQuantity Method...");

            String   transactionNumber = returnTransaction.Contains("gsc_returntransactionpn") ? returnTransaction.GetAttributeValue <String>("gsc_returntransactionpn") : String.Empty;
            DateTime transactionDate   = DateTime.UtcNow;

            EntityCollection returnTransactionDetailCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_returntransactiondetails", "gsc_returntransactionid", returnTransaction.Id, _organizationService,
                                                                                                         null, OrderType.Ascending, new[] { "gsc_inventoryid" });

            _tracingService.Trace("TransactionDetail count: " + returnTransactionDetailCollection.Entities.Count.ToString());
            if (returnTransactionDetailCollection.Entities.Count > 0)
            {
                var inventoryId = returnTransactionDetailCollection.Entities[0].Contains("gsc_inventoryid") ? returnTransactionDetailCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                    : Guid.Empty;

                EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                               new[] { "gsc_productquantityid", "gsc_modelcode", "gsc_optioncode", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelyear", "gsc_productionno", "gsc_vin", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });
                _tracingService.Trace("Inventory count: " + inventoryCollection.Entities.Count);

                if (inventoryCollection.Entities.Count > 0)
                {
                    Entity inventoryEntity = inventoryCollection.Entities[0];

                    var productQuantityId = inventoryEntity.Contains("gsc_productquantityid") ? inventoryEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                        : Guid.Empty;

                    //retrieve and update product quantity
                    EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null,
                                                                                                         OrderType.Ascending, new[] { "gsc_allocated", "gsc_onhand", "gsc_available", "gsc_siteid", "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_productid" });
                    _tracingService.Trace("Product Quantity record count: " + productQuantityCollection.Entities.Count);

                    if (productQuantityCollection.Entities.Count > 0)
                    {
                        Entity productQuantity = productQuantityCollection.Entities[0];

                        Int32 allocated = productQuantity.Contains("gsc_allocated")
                            ? productQuantity.GetAttributeValue <Int32>("gsc_allocated")
                            : 0;
                        Int32 onHand = productQuantity.Contains("gsc_onhand")
                            ? productQuantity.GetAttributeValue <Int32>("gsc_onhand")
                            : 0;
                        Int32 available = productQuantity.Contains("gsc_available")
                            ? productQuantity.GetAttributeValue <Int32>("gsc_available")
                            : 0;

                        _tracingService.Trace("Applying product quantity adjustment for " + caller);

                        InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);

                        //BL when posting return transaction
                        if (caller == "postTransaction")
                        {
                            if (allocated > 0)
                            {
                                productQuantity["gsc_allocated"] = allocated - 1;
                            }

                            if (onHand > 0)
                            {
                                productQuantity["gsc_onhand"] = onHand - 1;
                            }

                            _tracingService.Trace("Updating product quantity. Allocated = " + (allocated - 1) + "onhand = " + (onHand - 1));

                            // Create inventory log
                            Guid fromSite = productQuantity.Contains("gsc_siteid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty;
                            InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService);
                            inventoryMovementHandler.CreateInventoryHistory("Vehicle Return", null, null, transactionNumber, transactionDate, 1, 0, onHand - 1, Guid.Empty, fromSite, fromSite, inventoryEntity, productQuantity, true, true);
                            inventoryMovement.CreateInventoryQuantityAllocated(returnTransaction, inventoryEntity, productQuantity, returnTransaction.GetAttributeValue <string>("gsc_returntransactionpn"),
                                                                               DateTime.UtcNow, "Returned", Guid.Empty, 100000007);

                            //delete associated inventory
                            _organizationService.Delete("gsc_iv_inventory", inventoryId);
                            _tracingService.Trace("Deleted inventory record...");
                        }

                        //BL when cancelling return transaction
                        else if (caller == "cancel")
                        {
                            //set inventory status to available
                            inventoryEntity["gsc_status"] = new OptionSetValue(100000000);
                            _organizationService.Update(inventoryEntity);
                            _tracingService.Trace("Updated inventory status to available...");

                            productQuantity["gsc_available"] = available + 1;

                            if (allocated > 0)
                            {
                                productQuantity["gsc_allocated"] = allocated - 1;
                            }

                            UncheckReceivingTransactionBoolean(returnTransaction);
                            //Create Inventory History Log
                            inventoryMovement.CreateInventoryQuantityAllocated(returnTransaction, inventoryEntity, productQuantity, returnTransaction.GetAttributeValue <string>("gsc_returntransactionpn"),
                                                                               DateTime.UtcNow, "Cancelled", Guid.Empty, 100000004);
                        }

                        //BL when allocating return transaction. Triggered on create/update
                        else if (caller == "allocate")
                        {
                            inventoryEntity["gsc_status"] = new OptionSetValue(100000001);
                            _organizationService.Update(inventoryEntity);
                            _tracingService.Trace("Updated inventory status to allocated...");

                            if (available > 0)
                            {
                                productQuantity["gsc_available"] = available - 1;
                            }

                            productQuantity["gsc_allocated"] = allocated + 1;

                            //Create Inventory History Log
                            inventoryMovement.CreateInventoryQuantityAllocated(returnTransaction, inventoryEntity, productQuantity, returnTransaction.GetAttributeValue <string>("gsc_returntransactionpn"),
                                                                               DateTime.UtcNow, "Open", Guid.Empty, 100000001);
                        }

                        _organizationService.Update(productQuantity);
                    }
                }
            }
            _tracingService.Trace("Ending AdjustProductQuantity Method...");
        }
Example #4
0
        //Created By: Raphael Herrera, Created On: 8/3/2016

        /*Purpose: Create new allocated vehicle record.
         * Registration Details:
         * Event/Message:
         *      Post/Update: gsc_inventoryidtoallocate
         * Primary Entity: gsc_iv_vehicletransfer
         */
        public void AllocateVehicle(Entity vehicleTransfer)
        {
            _tracingService.Trace("Started AllocateVehicle method...");

            var inventoryId = vehicleTransfer.Contains("gsc_inventoryidtoallocate")
                ? vehicleTransfer.GetAttributeValue <string>("gsc_inventoryidtoallocate")
                : String.Empty;

            EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                           new[] { "gsc_status", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_optioncode", "gsc_productionno", "gsc_vin", "gsc_productquantityid", "gsc_modelyear", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });

            _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count);

            if (inventoryCollection.Entities.Count > 0)
            {
                Entity inventoryEntity = inventoryCollection.Entities[0];
                Guid   destinationSite = vehicleTransfer.Contains("gsc_siteid") ? vehicleTransfer.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty;
                Guid   sourceSite      = inventoryEntity.Contains("gsc_siteid") ? inventoryEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty;
                if (destinationSite == sourceSite)
                {
                    throw new InvalidPluginExecutionException("Cannot transfer vehicle. Destination site should not be equal to source site.");
                }

                if (inventoryEntity.GetAttributeValue <OptionSetValue>("gsc_status").Value == 100000000)
                {
                    _tracingService.Trace("Status of inventory is available...");
                    #region Update Inventory and product quantity

                    //set status to allocated
                    inventoryEntity["gsc_status"] = new OptionSetValue(100000001);
                    _organizationService.Update(inventoryEntity);
                    _tracingService.Trace("Updated inventory status to allocated...");

                    var productQuantityId = inventoryEntity.Contains("gsc_productquantityid") ? inventoryEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                        : Guid.Empty;


                    EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService,
                                                                                                         null, OrderType.Ascending, new[] { "gsc_allocated", "gsc_available", "gsc_siteid", "gsc_productid", "gsc_vehiclemodelid", "gsc_vehiclecolorid" });

                    _tracingService.Trace("ProductQuantity records retrieved: " + productQuantityCollection.Entities.Count);
                    if (productQuantityCollection.Entities.Count > 0)
                    {
                        Entity productQuantityEntity = productQuantityCollection.Entities[0];

                        /*  Int32 allocated = productQuantityEntity.Contains("gsc_allocated") ? productQuantityEntity.GetAttributeValue<Int32>("gsc_allocated")
                         *    : 0;
                         * Int32 available = productQuantityEntity.Contains("gsc_available") ? productQuantityEntity.GetAttributeValue<Int32>("gsc_available")
                         *    : 0;
                         *
                         * productQuantityEntity["gsc_allocated"] = allocated + 1;
                         *
                         * if (available > 0)
                         * {
                         *    productQuantityEntity["gsc_available"] = available - 1;
                         * }
                         *
                         * _organizationService.Update(productQuantityEntity);;*/

                        #region Create VehicleAllocation Record

                        Entity transferredVehicleDetails = new Entity("gsc_iv_vehicletransferdetails");
                        var    destinationSiteId         = vehicleTransfer.Contains("gsc_siteid") ? vehicleTransfer.GetAttributeValue <EntityReference>("gsc_siteid").Id
                            : Guid.Empty;
                        var sourceSiteId = productQuantityEntity.Contains("gsc_siteid") ? productQuantityEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id
                            : Guid.Empty;

                        transferredVehicleDetails["gsc_color"]        = inventoryEntity.GetAttributeValue <String>("gsc_color");
                        transferredVehicleDetails["gsc_csno"]         = inventoryEntity.GetAttributeValue <String>("gsc_csno");
                        transferredVehicleDetails["gsc_engineno"]     = inventoryEntity.GetAttributeValue <String>("gsc_engineno");
                        transferredVehicleDetails["gsc_modelcode"]    = inventoryEntity.GetAttributeValue <String>("gsc_modelcode");
                        transferredVehicleDetails["gsc_optioncode"]   = inventoryEntity.GetAttributeValue <String>("gsc_optioncode");
                        transferredVehicleDetails["gsc_productionno"] = inventoryEntity.GetAttributeValue <String>("gsc_productionno");
                        transferredVehicleDetails["gsc_modelyear"]    = inventoryEntity.GetAttributeValue <String>("gsc_modelyear");
                        transferredVehicleDetails["gsc_vin"]          = inventoryEntity.GetAttributeValue <String>("gsc_vin");
                        transferredVehicleDetails["gsc_productid"]    = productQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid") != null
                        ? new EntityReference("product", productQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid").Id)
                        : null;
                        transferredVehicleDetails["gsc_basemodel"] = productQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid") != null
                        ? new EntityReference("gsc_iv_vehiclebasemodel", productQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Id)
                        : null;
                        transferredVehicleDetails["gsc_inventoryid"]       = new EntityReference(inventoryEntity.LogicalName, inventoryEntity.Id);
                        transferredVehicleDetails["gsc_vehicletransferid"] = new EntityReference(vehicleTransfer.LogicalName, vehicleTransfer.Id);
                        transferredVehicleDetails["gsc_destinationsiteid"] = new EntityReference("gsc_iv_site", destinationSiteId);
                        transferredVehicleDetails["gsc_sourcesiteid"]      = new EntityReference("gsc_iv_site", sourceSiteId);
                        _organizationService.Create(transferredVehicleDetails);

                        _tracingService.Trace("Created vehicle allocation record...");

                        vehicleTransfer["gsc_inventoryidtoallocate"] = String.Empty;

                        _organizationService.Update(vehicleTransfer);

                        #endregion

                        //Create Inventory History Log
                        InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);

                        inventoryMovement.UpdateProductQuantityDirectly(productQuantityEntity, 0, -1, 1, 0, 0, 0, 0, 0);
                        _tracingService.Trace("Updated productquantity count...");

                        inventoryMovement.CreateInventoryQuantityAllocated(vehicleTransfer, inventoryEntity, productQuantityEntity, vehicleTransfer.GetAttributeValue <string>("gsc_vehicletransferpn"),
                                                                           DateTime.UtcNow, "Open", destinationSiteId, 100000001);
                    }

                    #endregion
                }

                else
                {
                    throw new InvalidPluginExecutionException("!_The inventory for entered vehicle is not available.");
                }
            }

            _tracingService.Trace("Ending AllocateVehicle method...");
        }
Example #5
0
        //Created By: Raphael Herrera, Created On: 2/14/2017

        /*Purpose: BL for canceling vehicletransfer transaction
         * Registration Details:
         * Event/Message:
         *      Post/Update: gsc_transferstatus to Cancelled
         * Primary Entity: gsc_iv_vehicletransfer
         */
        public void CancelTransaction(Entity vehicleTransfer)
        {
            if (vehicleTransfer.FormattedValues["gsc_transferstatus"] != "Cancelled")
            {
                return;
            }
            _tracingService.Trace("Starting CancelTransaction Method...");
            EntityCollection transferDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicletransferdetails", "gsc_vehicletransferid", vehicleTransfer.Id, _organizationService,
                                                                                                 null, OrderType.Ascending, new[] { "gsc_inventoryid" });

            _tracingService.Trace("Transfer Details records retrieved: " + transferDetailsCollection.Entities.Count);
            if (transferDetailsCollection.Entities.Count > 0)
            {
                foreach (Entity transferDetails in transferDetailsCollection.Entities)
                {
                    var inventoryId = transferDetails.Contains("gsc_inventoryid") ? transferDetails.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                       : Guid.Empty;

                    //Retrieve and update inventory
                    EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService,
                                                                                                   null, OrderType.Ascending, new[] { "gsc_status", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_optioncode", "gsc_productionno", "gsc_vin", "gsc_productquantityid", "gsc_modelyear", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });

                    _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count);
                    if (inventoryCollection.Entities.Count > 0)
                    {
                        Entity inventory = inventoryCollection.Entities[0];

                        inventory["gsc_status"] = new OptionSetValue(100000000);



                        var productQuantityId = inventory.Contains("gsc_productquantityid") ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                            : Guid.Empty;

                        //Retrieve and update product quantity
                        EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService,
                                                                                                             null, OrderType.Ascending, new[] { "gsc_available", "gsc_allocated", "gsc_vehiclecolorid", "gsc_vehiclemodelid" });

                        _tracingService.Trace("ProductQuantity records retrieved: " + productQuantityCollection.Entities.Count);
                        if (productQuantityCollection.Entities.Count > 0)
                        {
                            Entity productQuantity = productQuantityCollection.Entities[0];
                            Int32  available       = productQuantity.GetAttributeValue <Int32>("gsc_available");
                            Int32  allocated       = productQuantity.GetAttributeValue <Int32>("gsc_allocated");

                            productQuantity["gsc_available"] = available + 1;
                            productQuantity["gsc_allocated"] = allocated - 1;

                            _organizationService.Update(productQuantity);
                            _tracingService.Trace("Product Quantity updated...");

                            _organizationService.Update(inventory);
                            _tracingService.Trace("Updated inventory record...");

                            //Create Inventory History Log
                            InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);
                            inventoryMovement.CreateInventoryQuantityAllocated(vehicleTransfer, inventory, productQuantity, vehicleTransfer.GetAttributeValue <string>("gsc_vehicletransferpn"),
                                                                               DateTime.UtcNow, "Cancelled", vehicleTransfer.GetAttributeValue <EntityReference>("gsc_siteid").Id, 100000004);
                        }
                    }
                }
            }
            _tracingService.Trace("Ending CancelTransaction Method...");
        }
Example #6
0
        //Created By: Raphael Herrera, Created On: 8/8/2016

        /*Purpose: BL for posting vehicletransfer transaction
         * Registration Details:
         * Event/Message:
         *      Post/Update: gsc_transferstatus
         * Primary Entity: gsc_iv_vehicletransfer
         */
        public void PostTransaction(Entity vehicleTransfer)
        {
            _tracingService.Trace("Started PostTransaction Method...");

            //Retrieve allocatedVehicle to retrieve inventoryid
            EntityCollection transferredVehicleDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicletransferdetails", "gsc_vehicletransferid", vehicleTransfer.Id, _organizationService,
                                                                                                           null, OrderType.Ascending, new[] { "gsc_inventoryid", "gsc_destinationsiteid", "gsc_sourcesiteid" });
            InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService);

            _tracingService.Trace("AllocatedVehicle records retrieved: " + transferredVehicleDetailsCollection.Entities.Count);

            if (transferredVehicleDetailsCollection != null && transferredVehicleDetailsCollection.Entities.Count > 0)
            {
                foreach (Entity allocatedVehicle in transferredVehicleDetailsCollection.Entities)
                {
                    _tracingService.Trace("Running through allocatedvehicle record...");
                    var inventoryId = allocatedVehicle.Contains("gsc_inventoryid") ? allocatedVehicle.GetAttributeValue <EntityReference>("gsc_inventoryid").Id :
                                      Guid.Empty;
                    Guid destinationSiteId = allocatedVehicle.Contains("gsc_destinationsiteid") ? allocatedVehicle.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Id :
                                             Guid.Empty;
                    String destinationSiteName = allocatedVehicle.Contains("gsc_destinationsiteid") ? allocatedVehicle.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Name :
                                                 String.Empty;
                    String   transactionNumber = vehicleTransfer.Contains("gsc_vehicletransferpn") ? vehicleTransfer.GetAttributeValue <String>("gsc_vehicletransferpn") : string.Empty;
                    DateTime transactionDate   = DateTime.UtcNow;
                    Guid     fromSite          = allocatedVehicle.Contains("gsc_sourcesiteid") ? allocatedVehicle.GetAttributeValue <EntityReference>("gsc_sourcesiteid").Id : Guid.Empty;

                    //Retrieve inventory to update status
                    EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService,
                                                                                                   null, OrderType.Ascending, new[] { "gsc_status", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_optioncode", "gsc_productionno", "gsc_vin", "gsc_productquantityid", "gsc_modelyear", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });
                    _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count);

                    if (inventoryCollection != null && inventoryCollection.Entities.Count > 0)
                    {
                        var productQuantityId = inventoryCollection.Entities[0].GetAttributeValue <EntityReference>("gsc_productquantityid").Id;

                        //Retrieve ProductQuantity where inventory came from to be updated
                        EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService,
                                                                                                             null, OrderType.Ascending, new[] { "gsc_allocated", "gsc_onhand", "gsc_productid", "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_siteid" });
                        _tracingService.Trace("ProductQuantity records retrieved: " + productQuantityCollection.Entities.Count);

                        if (productQuantityCollection != null && productQuantityCollection.Entities.Count > 0)
                        {
                            Entity productQuantity = productQuantityCollection.Entities[0];
                            //Adjustment of 'site from'
                            Int32 allocated = productQuantity.GetAttributeValue <Int32>("gsc_allocated");
                            Int32 onHand    = productQuantity.GetAttributeValue <Int32>("gsc_onhand");

                            productQuantity["gsc_allocated"] = allocated - 1;
                            productQuantity["gsc_onhand"]    = onHand - 1;
                            _organizationService.Update(productQuantity);
                            _tracingService.Trace("Updated productquantity from site record...");

                            //Log inventory history upon decreasing onhand value in source site
                            inventoryMovementHandler.CreateInventoryHistory("Vehicle Transfer", string.Empty, string.Empty, transactionNumber, transactionDate, 1, 1, onHand - 1, destinationSiteId, fromSite, fromSite, inventoryCollection.Entities[0], productQuantity, true, false);

                            Guid   productId   = productQuantity.Contains("gsc_productid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty;
                            Guid   colorId     = productQuantity.Contains("gsc_vehiclecolorid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id : Guid.Empty;
                            Guid   baseModel   = productQuantity.Contains("gsc_vehiclemodelid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Id : Guid.Empty;
                            String productName = productQuantity.Contains("gsc_productid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_productid").Name : String.Empty;

                            var destinationConditionList = new List <ConditionExpression>
                            {
                                new ConditionExpression("gsc_siteid", ConditionOperator.Equal, destinationSiteId),
                                new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId),
                                new ConditionExpression("gsc_vehiclecolorid", ConditionOperator.Equal, colorId)
                            };

                            //Retrieve productquantity of destination site to be updated
                            EntityCollection productQuantityDestinationCollection = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", destinationConditionList, _organizationService,
                                                                                                                              null, OrderType.Ascending, new[] { "gsc_allocated", "gsc_onhand", "gsc_available" });
                            _tracingService.Trace("ProductQuantity(Destination) records retrieved: " + productQuantityDestinationCollection.Entities.Count);

                            Entity inventory = new Entity("gsc_iv_inventory");
                            Entity productQuantityDestination = new Entity("gsc_iv_productquantity");
                            Int32  onHandCount = 1;
                            if (productQuantityDestinationCollection != null && productQuantityDestinationCollection.Entities.Count > 0)
                            {
                                //Adjustment of destination site
                                productQuantityDestination = productQuantityDestinationCollection.Entities[0];
                                Int32 onHandDestination = productQuantityDestination.GetAttributeValue <Int32>("gsc_onhand");

                                //Update of inventory status
                                inventory = inventoryCollection.Entities[0];
                                inventory["gsc_status"]            = new OptionSetValue(100000000);
                                inventory["gsc_productquantityid"] = new EntityReference(productQuantityDestination.LogicalName, productQuantityDestination.Id);
                                _organizationService.Update(inventory);
                                _tracingService.Trace("Updated inventory status to available...");

                                inventoryMovementHandler.UpdateProductQuantityDirectly(productQuantityDestination, 1, 1, 0, 0, 0, 0, 0, 0);
                                _tracingService.Trace("Updated productquantity destination record...");

                                onHandCount = onHandDestination + 1;
                            }
                            else
                            {
                                //Create productQuantity
                                Entity prodQuantity = new Entity("gsc_iv_productquantity");
                                _tracingService.Trace("Set product quantity count");
                                prodQuantity["gsc_onhand"]    = 1;
                                prodQuantity["gsc_available"] = 1;
                                prodQuantity["gsc_allocated"] = 0;
                                prodQuantity["gsc_onorder"]   = 0;
                                prodQuantity["gsc_sold"]      = 0;
                                prodQuantity["gsc_branchid"]  = vehicleTransfer.GetAttributeValue <EntityReference>("gsc_branchid") != null
                               ? new EntityReference("account", vehicleTransfer.GetAttributeValue <EntityReference>("gsc_branchid").Id)
                               : null;
                                prodQuantity["gsc_dealerid"] = vehicleTransfer.GetAttributeValue <EntityReference>("gsc_dealerid") != null
                                ? new EntityReference("account", vehicleTransfer.GetAttributeValue <EntityReference>("gsc_dealerid").Id)
                                : null;
                                prodQuantity["gsc_recordownerid"] = vehicleTransfer.GetAttributeValue <EntityReference>("gsc_recordownerid") != null
                                ? new EntityReference("contact", vehicleTransfer.GetAttributeValue <EntityReference>("gsc_recordownerid").Id)
                                : null;

                                _tracingService.Trace("Set site field");
                                if (destinationSiteId != Guid.Empty)
                                {
                                    prodQuantity["gsc_siteid"] = new EntityReference("gsc_iv_site", destinationSiteId);
                                }
                                _tracingService.Trace("Set Vehicle Base Model field");
                                if (baseModel != Guid.Empty)
                                {
                                    prodQuantity["gsc_vehiclemodelid"] = new EntityReference("gsc_iv_vehiclebasemodel", baseModel);
                                }

                                if (colorId != Guid.Empty)
                                {
                                    prodQuantity["gsc_vehiclecolorid"] = new EntityReference("gsc_cmn_vehiclecolor", colorId);
                                }
                                _tracingService.Trace("Set Product Name field");
                                prodQuantity["gsc_productid"]         = new EntityReference("product", productId);
                                prodQuantity["gsc_productquantitypn"] = productName + "-" + destinationSiteName;

                                Guid newProductQuantityId = _organizationService.Create(prodQuantity);

                                EntityCollection productQuantityEC = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", newProductQuantityId, _organizationService, null, OrderType.Ascending,
                                                                                                             new[] { "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_productid" });
                                productQuantityDestination = productQuantityEC.Entities[0];

                                //Update of inventory status
                                inventory = inventoryCollection.Entities[0];
                                inventory["gsc_status"]            = new OptionSetValue(100000000);
                                inventory["gsc_productquantityid"] = new EntityReference(prodQuantity.LogicalName, newProductQuantityId);
                                _organizationService.Update(inventory);
                                _tracingService.Trace("Updated inventory status to available...");
                            }

                            inventoryMovementHandler.CreateInventoryHistory("Vehicle Transfer", string.Empty, string.Empty, transactionNumber, transactionDate, 1, 1, onHandCount, destinationSiteId, fromSite, destinationSiteId, inventory, productQuantityDestination, true, true);
                            inventoryMovementHandler.CreateInventoryQuantityAllocated(vehicleTransfer, inventory, productQuantity, vehicleTransfer.GetAttributeValue <string>("gsc_vehicletransferpn"),
                                                                                      DateTime.UtcNow, "Posted", destinationSiteId, 100000008);
                        }
                    }
                }
            }
            else
            {
                throw new InvalidPluginExecutionException("!_Please select first vehicle to transfer");
            }
            _tracingService.Trace("Ending PostTransasction method...");
        }
Example #7
0
        //Created By: Raphael Herrera, Created On: 8/4/2016

        /*Purpose: Handle Delete BL for vehicle transfer record
         * Registration Details:
         * Event/Message:
         *      Pre/Delete: VehicleTransfer
         * Primary Entity: gsc_iv_vehicletransfer
         */
        public void ValidateDelete(Entity vehicleTransfer)
        {
            _tracingService.Trace("Started ValidateDelete method...");
            //Unposted status
            if (vehicleTransfer.GetAttributeValue <OptionSetValue>("gsc_transferstatus").Value == 100000001)
            {
                _tracingService.Trace("Status is Unposted...");
                EntityCollection transferDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicletransferdetails", "gsc_vehicletransferid", vehicleTransfer.Id, _organizationService,
                                                                                                     null, OrderType.Ascending, new[] { "gsc_inventoryid" });

                _tracingService.Trace("Transfer Details records retrieved: " + transferDetailsCollection.Entities.Count);
                if (transferDetailsCollection.Entities.Count > 0)
                {
                    foreach (Entity transferDetails in transferDetailsCollection.Entities)
                    {
                        var inventoryId = transferDetails.Contains("gsc_inventoryid") ? transferDetails.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                           : Guid.Empty;

                        //Retrieve and update inventory
                        EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService,
                                                                                                       null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid", "gsc_modelcode", "gsc_optioncode", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelyear", "gsc_productionno", "gsc_vin", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });

                        _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count);
                        if (inventoryCollection.Entities.Count > 0)
                        {
                            Entity inventory = inventoryCollection.Entities[0];
                            inventory["gsc_status"] = new OptionSetValue(100000000);
                            _organizationService.Update(inventory);

                            var productQuantityId = inventory.Contains("gsc_productquantityid") ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                                : Guid.Empty;

                            //Retrieve and update product quantity
                            EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService,
                                                                                                                 null, OrderType.Ascending, new[] { "gsc_available", "gsc_allocated", "gsc_vehiclemodelid", "gsc_vehiclecolorid" });

                            _tracingService.Trace("ProductQuantity records retrieved: " + productQuantityCollection.Entities.Count);
                            if (productQuantityCollection.Entities.Count > 0)
                            {
                                Entity productQuantity = productQuantityCollection.Entities[0];

                                //Create inventory history log
                                InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);

                                inventoryMovement.UpdateProductQuantityDirectly(productQuantity, 0, 1, -1, 0, 0, 0, 0, 0);
                                _tracingService.Trace("Updated productquantity count...");

                                inventoryMovement.CreateInventoryQuantityAllocated(vehicleTransfer, inventory, productQuantity, vehicleTransfer.GetAttributeValue <string>("gsc_vehicletransferpn"),
                                                                                   DateTime.UtcNow, "Deleted", vehicleTransfer.GetAttributeValue <EntityReference>("gsc_siteid").Id, 100000005);
                            }
                        }

                        //Delete Transfer Details
                        _organizationService.Delete(transferDetails.LogicalName, transferDetails.Id);
                    }
                }
            }

            //Posted status
            else if (vehicleTransfer.GetAttributeValue <OptionSetValue>("gsc_transferstatus").Value == 100000000)
            {
                _tracingService.Trace("Status is posted...");
                throw new InvalidPluginExecutionException("!_Unable to delete Posted Vehicle Transfer transactions.");
            }
            _tracingService.Trace("Ending ValidateDelete method...");
        }
Example #8
0
        //Created By : Jerome Anthony Gerero, Created On : 8/25/2016
        //Modified By : Jessica Casupanan, Modified On : 01/09/2017

        /*Purpose: Create new allocated vehicle record
         * Registration Details:
         * Event/Message:
         *      Pre/Create:
         *      Post/Update: Inventory Id to Allocate = gsc_inventoryidtoallocate
         *      Post/Create:
         * Primary Entity: Vehicle Adjustment/Variance Entry
         */
        public Entity CreateVehicleAdjustmentVarianceEntryDetailRecord(Entity vehicleAdjustmentVarianceEntryEntity)
        {
            _tracingService.Trace("Started CreateVehicleAdjustmentVarianceEntryDetailRecord Method...");

            //Return if Inventory ID to Allocate is null
            if (vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate") == null)
            {
                _tracingService.Trace("Inventory ID to Allocate is null.. exiting.");
                return(null);
            }

            Guid inventoryId = vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate") != null
                ? new Guid(vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <String>("gsc_inventoryidtoallocate"))
                : Guid.Empty;

            EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                        new[] { "gsc_status", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_modelyear", "gsc_optioncode", "gsc_productionno", "gsc_vin", "gsc_productquantityid", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });

            if (inventoryRecords != null && inventoryRecords.Entities.Count > 0)
            {
                Entity inventory = inventoryRecords.Entities[0];

                if (!inventory.FormattedValues["gsc_status"].Equals("Available"))
                {
                    throw new InvalidPluginExecutionException("The inventory for entered vehicle is not available.");
                }

                Entity vehicleAdjustmentVarianceEntryDetail = new Entity("gsc_sls_adjustmentvariancedetail");

                vehicleAdjustmentVarianceEntryDetail["gsc_vehicleadjustmentvarianceentryid"] = new EntityReference(vehicleAdjustmentVarianceEntryEntity.LogicalName, vehicleAdjustmentVarianceEntryEntity.Id);
                vehicleAdjustmentVarianceEntryDetail["gsc_inventoryid"] = new EntityReference(inventory.LogicalName, inventory.Id);
                vehicleAdjustmentVarianceEntryDetail["gsc_modelcode"]   = inventory.Contains("gsc_modelcode")
                    ? inventory.GetAttributeValue <String>("gsc_modelcode")
                    : String.Empty;
                vehicleAdjustmentVarianceEntryDetail["gsc_modelyear"] = inventory.Contains("gsc_modelyear")
                    ? inventory.GetAttributeValue <String>("gsc_modelyear")
                    : String.Empty;
                vehicleAdjustmentVarianceEntryDetail["gsc_optioncode"] = inventory.Contains("gsc_optioncode")
                    ? inventory.GetAttributeValue <String>("gsc_optioncode")
                    : String.Empty;
                String color = inventory.Contains("gsc_color")
                    ? inventory.GetAttributeValue <String>("gsc_color")
                    : String.Empty;

                //Retrieve Vehicle Color
                EntityCollection vehicleColorRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_vehiclecolor", "gsc_vehiclecolorpn", color, _organizationService, null, OrderType.Ascending,
                                                                                               new[] { "gsc_cmn_vehiclecolorid" });

                if (vehicleColorRecords != null && vehicleColorRecords.Entities.Count > 0)
                {
                    Entity vehicleColor = vehicleColorRecords.Entities[0];

                    vehicleAdjustmentVarianceEntryDetail["gsc_vehiclecolorid"] = new EntityReference(vehicleColor.LogicalName, vehicleColor.Id);
                }

                vehicleAdjustmentVarianceEntryDetail["gsc_csno"] = inventory.Contains("gsc_csno")
                    ? inventory.GetAttributeValue <String>("gsc_csno")
                    : String.Empty;
                vehicleAdjustmentVarianceEntryDetail["gsc_vin"] = inventory.Contains("gsc_vin")
                    ? inventory.GetAttributeValue <String>("gsc_vin")
                    : String.Empty;
                vehicleAdjustmentVarianceEntryDetail["gsc_productionno"] = inventory.Contains("gsc_productionno")
                    ? inventory.GetAttributeValue <String>("gsc_productionno")
                    : String.Empty;
                vehicleAdjustmentVarianceEntryDetail["gsc_engineno"] = inventory.Contains("gsc_engineno")
                    ? inventory.GetAttributeValue <String>("gsc_engineno")
                    : String.Empty;
                vehicleAdjustmentVarianceEntryDetail["gsc_quantity"]      = -1;
                vehicleAdjustmentVarianceEntryDetail["gsc_operation"]     = new OptionSetValue(100000001);
                vehicleAdjustmentVarianceEntryDetail["gsc_recordownerid"] = vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <EntityReference>("gsc_recordownerid") != null
                    ? new EntityReference("contact", vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <EntityReference>("gsc_recordownerid").Id)
                    : null;
                vehicleAdjustmentVarianceEntryDetail["gsc_dealerid"] = vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <EntityReference>("gsc_dealerid") != null
                    ? new EntityReference("account", vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <EntityReference>("gsc_dealerid").Id)
                    : null;
                vehicleAdjustmentVarianceEntryDetail["gsc_branchid"] = vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <EntityReference>("gsc_branchid") != null
                    ? new EntityReference("account", vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <EntityReference>("gsc_branchid").Id)
                    : null;

                Guid productQuantityId = inventory.Contains("gsc_productquantityid")
                    ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                    : Guid.Empty;

                //Retrieve Product Quantity record for additional fields
                EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending,
                                                                                                  new[] { "gsc_siteid", "gsc_productid", "gsc_vehiclemodelid", "gsc_allocated", "gsc_available", "gsc_vehiclemodelid" });

                Entity productQuantity = new Entity("gsc_iv_productquantity");
                if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0)
                {
                    productQuantity = productQuantityRecords.Entities[0];

                    vehicleAdjustmentVarianceEntryDetail["gsc_vehiclebasemodelid"] = productQuantity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid") != null
                        ? new EntityReference("gsc_iv_vehiclebasemodel", productQuantity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Id)
                        : null;
                    vehicleAdjustmentVarianceEntryDetail["gsc_productid"] = productQuantity.GetAttributeValue <EntityReference>("gsc_productid") != null
                        ? new EntityReference("product", productQuantity.GetAttributeValue <EntityReference>("gsc_productid").Id)
                        : null;
                    vehicleAdjustmentVarianceEntryDetail["gsc_siteid"] = productQuantity.GetAttributeValue <EntityReference>("gsc_siteid") != null
                        ? new EntityReference("gsc_iv_site", productQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id)
                        : null;

                    Int32 allocatedCount = productQuantity.Contains("gsc_allocated")
                        ? productQuantity.GetAttributeValue <Int32>("gsc_allocated")
                        : 0;
                    Int32 availableCount = productQuantity.Contains("gsc_available")
                        ? productQuantity.GetAttributeValue <Int32>("gsc_available")
                        : 1;

                    productQuantity["gsc_allocated"] = allocatedCount + 1;

                    if (availableCount != 0)
                    {
                        productQuantity["gsc_available"] = availableCount - 1;
                    }

                    // throw new InvalidPluginExecutionException("test" + (allocatedCount + 1).ToString() + " " + (availableCount - 1).ToString());
                    _organizationService.Update(productQuantity);
                }

                _organizationService.Create(vehicleAdjustmentVarianceEntryDetail);

                //Set Inventory status to 'Allocated'
                inventory["gsc_status"] = new OptionSetValue(100000001);

                _organizationService.Update(inventory);

                InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);
                inventoryMovement.CreateInventoryQuantityAllocated(vehicleAdjustmentVarianceEntryEntity, inventory, productQuantity, vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <string>("gsc_vehicleadjustmentvarianceentrypn"),
                                                                   DateTime.UtcNow, vehicleAdjustmentVarianceEntryEntity.FormattedValues["gsc_adjustmentvariancestatus"], Guid.Empty, 100000001);
            }

            //Clear Inventory ID to Allocate field
            vehicleAdjustmentVarianceEntryEntity["gsc_inventoryidtoallocate"] = String.Empty;
            _organizationService.Update(vehicleAdjustmentVarianceEntryEntity);

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



            return(vehicleAdjustmentVarianceEntryEntity);
        }
Example #9
0
        //Created By : Jerome Anthony Gerero, Created On : 9/2/2016
        //Modified By : Jessica Casupanan, Modified On : 01/10/2017

        /*Purpose: Update Inventory record fields on Vehicle Adjustment/Variance Entry 'Posted' status
         * Registration Details:
         * Event/Message:
         *      Pre-Validate/Delete:
         *      Post/Update: gsc_adjustmentvariancestatus
         *      Post/Create:
         * Primary Entity: Vehicle Adjustment/Variance Entry Detail
         */
        public Entity PostVehicleAdjustmentVarianceEntry(Entity vehicleAdjustmentVarianceEntryEntity)
        {
            _tracingService.Trace("Started PostVehicleAdjustmentVarianceEntry Method...");

            if (!vehicleAdjustmentVarianceEntryEntity.FormattedValues["gsc_adjustmentvariancestatus"].Equals("Posted"))
            {
                return(null);
            }

            String   transactionNumber = vehicleAdjustmentVarianceEntryEntity.Contains("gsc_vehicleadjustmentvarianceentrypn") ? vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <String>("gsc_vehicleadjustmentvarianceentrypn") : String.Empty;
            DateTime transactionDate   = DateTime.UtcNow;
            InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService);

            _tracingService.Trace("Retrieve Vehicle Adjustment/Variance Entry Detail records");
            EntityCollection vehicleAdjustmentVarianceEntryDetailRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_adjustmentvariancedetail", "gsc_vehicleadjustmentvarianceentryid", vehicleAdjustmentVarianceEntryEntity.Id, _organizationService, null, OrderType.Ascending,
                                                                                                                   new[] { "gsc_vehiclebasemodelid", "gsc_vehiclecolorid", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_productid", "gsc_modelyear", "gsc_optioncode", "gsc_productionno", "gsc_siteid", "gsc_vin", "gsc_operation", "gsc_inventoryid", "statecode", "gsc_quantity" });

            if (vehicleAdjustmentVarianceEntryDetailRecords != null && vehicleAdjustmentVarianceEntryDetailRecords.Entities.Count > 0)
            {
                foreach (Entity vehicleAdjustmentVarianceEntryDetail in vehicleAdjustmentVarianceEntryDetailRecords.Entities)
                {
                    Int32 quantity = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_quantity")
                         ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <Int32>("gsc_quantity") : 0;

                    #region Subtract
                    if (vehicleAdjustmentVarianceEntryDetail.FormattedValues["gsc_operation"].Equals("Subtract"))
                    {
                        Guid inventoryId = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_inventoryid") != null
                           ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                           : Guid.Empty;

                        _tracingService.Trace("Retrieve Inventory records using value from Inventory field");
                        EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                                    new[] { "gsc_productquantityid", "gsc_optioncode", "gsc_productid", "gsc_modelcode", "gsc_modelyear", "gsc_siteid",
                                                                                                            "gsc_vin", "gsc_csno", "gsc_productionno", "gsc_engineno" });

                        if (inventoryRecords != null && inventoryRecords.Entities.Count > 0)
                        {
                            Entity inventory = inventoryRecords.Entities[0];

                            Guid productQuantityId = inventory.GetAttributeValue <EntityReference>("gsc_productquantityid") != null
                                ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                                : Guid.Empty;

                            _tracingService.Trace("Retrieve Product Quantity record using value from Product Quantity field");
                            EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending,
                                                                                                              new[] { "gsc_allocated", "gsc_onhand", "gsc_available", "gsc_siteid", "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_productid" });

                            if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0)
                            {
                                Entity productQuantity = productQuantityRecords.Entities[0];

                                Int32 allocatedCount = productQuantity.Contains("gsc_allocated")
                                    ? productQuantity.GetAttributeValue <Int32>("gsc_allocated")
                                    : 1;
                                Int32 availableCount = productQuantity.Contains("gsc_available")
                                    ? productQuantity.GetAttributeValue <Int32>("gsc_available")
                                    : 0;
                                Int32 onHandCount = productQuantity.Contains("gsc_onhand")
                                    ? productQuantity.GetAttributeValue <Int32>("gsc_onhand")
                                    : 1;

                                _tracingService.Trace("Adjust Allocated count");
                                if (allocatedCount != 0)
                                {
                                    productQuantity["gsc_allocated"] = allocatedCount - 1;
                                }
                                _tracingService.Trace("Adjust On Hand count");
                                if (onHandCount != 0)
                                {
                                    productQuantity["gsc_onhand"] = onHandCount - 1;
                                }

                                _organizationService.Update(productQuantity);
                                Guid fromSite = productQuantity.Contains("gsc_siteid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty;
                                inventoryMovementHandler.CreateInventoryHistory("Negative Adjustment", null, null, transactionNumber, transactionDate, 1, 0, onHandCount - 1, Guid.Empty, fromSite, fromSite, inventory, productQuantity, true, true);
                                inventoryMovementHandler.CreateInventoryQuantityAllocated(vehicleAdjustmentVarianceEntryEntity, inventory, productQuantity, vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <string>("gsc_vehicleadjustmentvarianceentrypn"),
                                                                                          DateTime.UtcNow, "Posted", Guid.Empty, 100000008);

                                _tracingService.Trace("Deactivate record");
                                if (vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <OptionSetValue>("statecode").Value == 0)
                                {
                                    SetStateRequest setStateRequest = new SetStateRequest()
                                    {
                                        EntityMoniker = new EntityReference
                                        {
                                            Id          = inventory.Id,
                                            LogicalName = inventory.LogicalName,
                                        },
                                        State  = new OptionSetValue(1),
                                        Status = new OptionSetValue(2)
                                    };
                                    _organizationService.Execute(setStateRequest);
                                }
                            }
                        }
                    }
                    #endregion

                    #region Add
                    else if (vehicleAdjustmentVarianceEntryDetail.FormattedValues["gsc_operation"].Equals("Add"))
                    {
                        _tracingService.Trace("Get Vehicle Adjustment/Variance Entry Detail fields");
                        Guid vehicleBaseModelId = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_vehiclebasemodelid").Id
                            : Guid.Empty;

                        String color = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Name
                            : String.Empty;

                        Guid colorId = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id
                            : Guid.Empty;

                        String csNo = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_csno")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_csno")
                            : String.Empty;
                        String engineNo = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_engineno")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_engineno")
                            : String.Empty;
                        String modelCode = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_modelcode")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_modelcode")
                            : String.Empty;
                        Guid productId = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_productid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_productid").Id
                            : Guid.Empty;

                        String productName = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_productid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_productid").Name
                            : String.Empty;

                        String modelYear = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_modelyear")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_modelyear")
                            : String.Empty;
                        String optionCode = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_optioncode")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_optioncode")
                            : String.Empty;
                        String productionNo = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_productionno")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_productionno")
                            : String.Empty;
                        Guid siteId = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_siteid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_siteid").Id
                            : Guid.Empty;

                        String siteName = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_siteid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_siteid").Name
                            : String.Empty;

                        String vin = vehicleAdjustmentVarianceEntryDetail.Contains("gsc_vin")
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <String>("gsc_vin")
                            : String.Empty;

                        _tracingService.Trace("Create filter for Product in Product Relationship entity");
                        var productQuantityConditionList = new List <ConditionExpression>
                        {
                            new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId),
                            new ConditionExpression("gsc_siteid", ConditionOperator.Equal, siteId),
                            new ConditionExpression("gsc_vehiclecolorid", ConditionOperator.Equal, colorId)
                        };

                        _tracingService.Trace("Retrieve Product Quantity records");
                        EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", productQuantityConditionList, _organizationService, null, OrderType.Ascending,
                                                                                                            new[] { "gsc_onhand", "gsc_available", "gsc_siteid", "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_productid" });

                        Entity productQuantity;
                        Entity inventory = new Entity("gsc_iv_inventory");

                        Int32 onHandCount = 0;
                        Int32 availableCount;

                        if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0)
                        {
                            _tracingService.Trace("Update existing product quantity record");
                            productQuantity = productQuantityRecords.Entities[0];

                            onHandCount = productQuantity.Contains("gsc_onhand")
                                ? productQuantity.GetAttributeValue <Int32>("gsc_onhand")
                                : 0;
                            availableCount = productQuantity.Contains("gsc_available")
                                ? productQuantity.GetAttributeValue <Int32>("gsc_available")
                                : 0;

                            _tracingService.Trace("Set product quantity count");
                            productQuantity["gsc_onhand"]    = onHandCount + 1;
                            productQuantity["gsc_available"] = availableCount + 1;

                            _organizationService.Update(productQuantity);

                            inventory["gsc_productquantityid"] = new EntityReference("gsc_iv_productquantity", productQuantity.Id);
                        }
                        else
                        {
                            _tracingService.Trace("Create new product quantity product");
                            productQuantity = new Entity("gsc_iv_productquantity");

                            _tracingService.Trace("Set product quantity count");
                            productQuantity["gsc_onhand"]    = 1;
                            productQuantity["gsc_available"] = 1;

                            _tracingService.Trace("Set site field");
                            if (siteId != Guid.Empty)
                            {
                                productQuantity["gsc_siteid"] = new EntityReference("gsc_iv_site", siteId);
                            }
                            _tracingService.Trace("Set Vehicle Base Model field");
                            if (vehicleBaseModelId != Guid.Empty)
                            {
                                productQuantity["gsc_vehiclemodelid"] = new EntityReference("gsc_iv_vehiclebasemodel", vehicleBaseModelId);
                            }

                            if (colorId != Guid.Empty)
                            {
                                productQuantity["gsc_vehiclecolorid"] = new EntityReference("gsc_cmn_vehiclecolor", colorId);
                            }
                            _tracingService.Trace("Set Product Name field");
                            productQuantity["gsc_productid"]         = new EntityReference("product", productId);
                            productQuantity["gsc_productquantitypn"] = productName;

                            Guid newProductQuantityId = _organizationService.Create(productQuantity);

                            inventory["gsc_productquantityid"] = new EntityReference("gsc_iv_productquantity", newProductQuantityId);
                        }

                        _tracingService.Trace("Create Inventory record");
                        inventory["gsc_inventorypn"]  = productName + "-" + siteName;
                        inventory["gsc_status"]       = new OptionSetValue(100000000);
                        inventory["gsc_color"]        = color;
                        inventory["gsc_engineno"]     = engineNo;
                        inventory["gsc_csno"]         = csNo;
                        inventory["gsc_productionno"] = productionNo;
                        inventory["gsc_vin"]          = vin;
                        inventory["gsc_modelcode"]    = modelCode;
                        inventory["gsc_optioncode"]   = optionCode;
                        inventory["gsc_modelyear"]    = modelYear;
                        inventory["gsc_siteid"]       = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_siteid") != null
                            ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_siteid")
                            : null;

                        Guid             inventoryId      = _organizationService.Create(inventory);
                        EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                                    new[] { "gsc_productquantityid", "gsc_modelcode", "gsc_optioncode", "gsc_productid", "gsc_modelyear", "gsc_siteid",
                                                                                                            "gsc_vin", "gsc_csno", "gsc_productionno", "gsc_engineno" });
                        if (inventoryRecords != null && inventoryRecords.Entities.Count > 0)
                        {
                            Entity inventoryE = inventoryRecords.Entities[0];
                            Guid   fromSite   = productQuantity.Contains("gsc_siteid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty;
                            inventoryMovementHandler.CreateInventoryHistory("Positive Adjustment", null, null, transactionNumber, transactionDate, 0, 1, onHandCount + 1, Guid.Empty, fromSite, fromSite, inventoryE, productQuantity, true, true);
                        }
                    }

                    #endregion
                }
            }

            _tracingService.Trace("Ended PostVehicleAdjustmentVarianceEntry Method...");
            return(vehicleAdjustmentVarianceEntryEntity);
        }
Example #10
0
        //Created By : Jerome Anthony Gerero, Created On : 8/31/2016

        /*Purpose: Update Inventory record fields on Vehicle Adjustment/Variance Entry record delete
         * Registration Details:
         * Event/Message:
         *      Pre-Validate/Delete: gsc_sls_adjustmentvarianceid
         *      Post/Update:
         *      Post/Create:
         * Primary Entity: Vehicle Adjustment/Variance Entry Detail
         */
        public Entity AdjustInventoryOnUnpostedDelete(Entity vehicleAdjustmentVarianceEntryEntity)
        {
            _tracingService.Trace("Started AdjustInventoryOnUnpostedDelete Method...");

            if (vehicleAdjustmentVarianceEntryEntity.FormattedValues["gsc_adjustmentvariancestatus"].Equals("Posted"))
            {
                throw new InvalidPluginExecutionException("Unable to delete already posted Vehicle Adjustment/Variance Entry");
            }
            else if (vehicleAdjustmentVarianceEntryEntity.FormattedValues["gsc_adjustmentvariancestatus"].Equals("Cancelled"))
            {
                throw new InvalidPluginExecutionException("Unable to delete cancelled Vehicle Adjustment/Variance Entry");
            }

            //Retrieve Vehicle Adjustment/Variance Entry Detail records
            EntityCollection vehicleAdjustmentVarianceEntryDetailRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_adjustmentvariancedetail", "gsc_vehicleadjustmentvarianceentryid", vehicleAdjustmentVarianceEntryEntity.Id, _organizationService, null, OrderType.Ascending,
                                                                                                                   new[] { "gsc_inventoryid" });

            if (vehicleAdjustmentVarianceEntryDetailRecords != null && vehicleAdjustmentVarianceEntryDetailRecords.Entities.Count > 0)
            {
                foreach (Entity vehicleAdjustmentVarianceEntryDetail in vehicleAdjustmentVarianceEntryDetailRecords.Entities)
                {
                    Guid inventoryId = vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_inventoryid") != null
                        ? vehicleAdjustmentVarianceEntryDetail.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                        : Guid.Empty;

                    //Retrieve Inventory records
                    EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                                new[] { "gsc_status", "gsc_productquantityid", "gsc_optioncode", "gsc_productid", "gsc_modelcode", "gsc_modelyear", "gsc_siteid",
                                                                                                        "gsc_vin", "gsc_csno", "gsc_productionno", "gsc_engineno" });

                    if (inventoryRecords != null && inventoryRecords.Entities.Count > 0)
                    {
                        Entity inventory = inventoryRecords.Entities[0];

                        inventory["gsc_status"] = new OptionSetValue(100000000);

                        _organizationService.Update(inventory);

                        Guid productQuantityId = inventory.GetAttributeValue <EntityReference>("gsc_productquantityid") != null
                            ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id
                            : Guid.Empty;

                        //Retrieve Product Quantity records
                        EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending,
                                                                                                          new[] { "gsc_allocated", "gsc_available", "gsc_vehiclecolorid", "gsc_vehiclemodelid" });

                        if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0)
                        {
                            Entity productQuantity = productQuantityRecords.Entities[0];

                            Int32 allocatedCount = productQuantity.Contains("gsc_allocated")
                                ? productQuantity.GetAttributeValue <Int32>("gsc_allocated")
                                : 1;
                            Int32 availableCount = productQuantity.Contains("gsc_available")
                                ? productQuantity.GetAttributeValue <Int32>("gsc_available")
                                : 0;

                            if (allocatedCount != 0)
                            {
                                productQuantity["gsc_allocated"] = allocatedCount - 1;
                            }
                            productQuantity["gsc_available"] = availableCount + 1;

                            _organizationService.Update(productQuantity);


                            //Create inventory history log
                            InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);
                            inventoryMovement.CreateInventoryQuantityAllocated(vehicleAdjustmentVarianceEntryEntity, inventory, productQuantity, vehicleAdjustmentVarianceEntryEntity.GetAttributeValue <string>("gsc_vehicleadjustmentvarianceentrypn"),
                                                                               DateTime.UtcNow, "Deleted", Guid.Empty, 100000005);
                        }
                    }
                    _organizationService.Delete("gsc_sls_adjustmentvariancedetail", vehicleAdjustmentVarianceEntryDetail.Id);
                }
            }

            _tracingService.Trace("Ended AdjustInventoryOnUnpostedDelete Method...");
            return(vehicleAdjustmentVarianceEntryEntity);
        }
Example #11
0
        //Created By: Leslie Baliguat, Created On: 5/18/2016

        /*Purpose: Update Order Status, Vehcicle Allocated Date and Inventory Id to Allocate fields of Sales Order to where Allocated vehicle is associated
         *         Update Inventory Status of Inventory Record that is related to Allocated Vehicle
         *         Update Available and Allocated fields of Product Quantity to where Inventory is associated
         * Registration Details:
         * Event/Message:
         *      PreValidate/Delete:
         * Primary Entity: Allocated Vehicle
         */
        public void RemoveAllocation(Entity allocatedEntity)
        {
            _tracingService.Trace("Started RemoveAllocation Method");

            Entity salesOrderToUpdate = new Entity("salesorder");

            if (allocatedEntity.GetAttributeValue <EntityReference>("gsc_orderid") != null)
            {
                _tracingService.Trace("Order Id not null");

                var orderId = allocatedEntity.GetAttributeValue <EntityReference>("gsc_orderid").Id;

                EntityCollection orderRecords = CommonHandler.RetrieveRecordsByOneValue("salesorder", "salesorderid", orderId, _organizationService, null, OrderType.Ascending,
                                                                                        new[] { "gsc_status", "gsc_vehicleallocateddate", "gsc_inventoryidtoallocate", "name" });


                if (orderRecords != null && orderRecords.Entities.Count > 0)
                {
                    _tracingService.Trace("Retrieved Related Order Details ");

                    salesOrderToUpdate = orderRecords.Entities[0];

                    var status = salesOrderToUpdate.Contains("gsc_status")
                        ? salesOrderToUpdate.GetAttributeValue <OptionSetValue>("gsc_status")
                        : null;

                    if (status.Value == 100000003)
                    {
                        salesOrderToUpdate["gsc_status"] = new OptionSetValue(100000002);
                        salesOrderToUpdate["gsc_vehicleallocateddate"]  = (DateTime?)null;
                        salesOrderToUpdate["gsc_inventoryidtoallocate"] = null;
                        _organizationService.Update(salesOrderToUpdate);
                    }

                    _tracingService.Trace("Related Order Updated");
                }
            }
            /*************************************************************/
            //Modified By: Raphael Herrera, Modified On: 8/14/2016
            //Included cleanup for VehicleTransfer entity
            if (allocatedEntity.GetAttributeValue <EntityReference>("gsc_vehicletransferid") != null)
            {
                _tracingService.Trace("VehicleTransfer is not null");

                var vehicleTransferId = allocatedEntity.GetAttributeValue <EntityReference>("gsc_vehicletransferid").Id;

                EntityCollection vehicleTransferCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicletransfer", "gsc_iv_vehicletransferid", vehicleTransferId, _organizationService, null, OrderType.Ascending,
                                                                                                     new[] { "gsc_inventoryidtoallocate" });

                _tracingService.Trace("Vehicle Transfer records retrieved: " + vehicleTransferCollection.Entities.Count);
                if (vehicleTransferCollection != null && vehicleTransferCollection.Entities.Count > 0)
                {
                    Entity vehicleTransferEntity = vehicleTransferCollection.Entities[0];

                    vehicleTransferEntity["gsc_inventoryidtoallocate"] = null;
                    _organizationService.Update(vehicleTransferEntity);

                    _tracingService.Trace("Vehicle Transfer Record Updated");
                }
            }
            /*************************************************************/

            /*************************************************************/
            //Modified By: Raphael Herrera, Modified On: 8/25/2016
            //Included cleanup for Vehicle In-Transit Transfer Entity
            if (allocatedEntity.GetAttributeValue <EntityReference>("gsc_vehicleintransittransferid") != null)
            {
                _tracingService.Trace("Vehicle In-Transit Transfer is not null");

                var vehicleInTransitId = allocatedEntity.GetAttributeValue <EntityReference>("gsc_vehicleintransittransferid").Id;

                EntityCollection vehicleInTransitCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", vehicleInTransitId, _organizationService, null, OrderType.Ascending,
                                                                                                      new[] { "gsc_inventoryidtoallocate", "gsc_intransittransferstatus" });

                _tracingService.Trace("Vehicle In-Transit Transfer records retrieved: " + vehicleInTransitCollection.Entities.Count);
                if (vehicleInTransitCollection != null && vehicleInTransitCollection.Entities.Count > 0)
                {
                    Entity vehicleInTransit = vehicleInTransitCollection.Entities[0];

                    //In-Transit Transfer Status != Picked
                    if (vehicleInTransit.GetAttributeValue <OptionSetValue>("gsc_intransittransferstatus").Value != 100000000)
                    {
                        throw new InvalidPluginExecutionException("Unable to delete record that is already shipped.");
                    }

                    vehicleInTransit["gsc_inventoryidtoallocate"] = null;
                    _organizationService.Update(vehicleInTransit);

                    _tracingService.Trace("Vehicle Transfer Record Updated");
                }
            }
            /*************************************************************/

            if (allocatedEntity.GetAttributeValue <EntityReference>("gsc_inventoryid") != null)
            {
                _tracingService.Trace("Inventory Id not null");

                var inventoryId = allocatedEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id;

                EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending,
                                                                                            new[] { "gsc_status", "gsc_color", "gsc_csno", "gsc_engineno", "gsc_modelcode", "gsc_optioncode", "gsc_productionno", "gsc_vin", "gsc_productquantityid", "gsc_modelyear", "gsc_siteid", "gsc_productid", "gsc_basemodelid" });

                if (inventoryRecords != null && inventoryRecords.Entities.Count > 0)
                {
                    _tracingService.Trace("Retrieved Related Inventory Details");

                    InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService);
                    inventoryMovementHandler.UpdateInventoryStatus(inventoryRecords.Entities[0], 100000000);
                    Entity productQuantityEntity = inventoryMovementHandler.UpdateProductQuantity(inventoryRecords.Entities[0], 0, 1, -1, 0, 0, 0, 0, 0);

                    // Create Inventory History Log
                    inventoryMovementHandler.CreateInventoryQuantityAllocated(salesOrderToUpdate, inventoryRecords.Entities[0], productQuantityEntity, salesOrderToUpdate.GetAttributeValue <string>("name"),
                                                                              DateTime.UtcNow, "For Allocation", Guid.Empty, 100000003);
                }
            }

            _tracingService.Trace("Ended RemoveAllocation Method.");
        }