Ejemplo n.º 1
0
        //Created By: Raphael Herrera, Created On: 8/26/2016

        /*Purpose: Handle BL for setting Vehicle In-Transit Transfer status to 'Shipped'
         * Registration Details:
         * Event/Message:
         *      Post/Update: gsc_intransittransferstatus
         * Primary Entity: gsc_iv_vehicleintransittransfer
         */
        public void ShipVehicle(Entity vehicleInTransitTransfer, bool isShipping)
        {
            _tracingService.Trace("Started ShipVehicle method...");

            //Status == Picked
            if (vehicleInTransitTransfer.GetAttributeValue <OptionSetValue>("gsc_intransittransferstatus").Value == 100000000)
            {
                _tracingService.Trace("Status is Picked...");

                EntityCollection allocatedVehicleCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransferdetail", "gsc_vehicleintransittransferid", vehicleInTransitTransfer.Id, _organizationService,
                                                                                                      null, OrderType.Ascending, new[] { "gsc_inventoryid", "gsc_sourcesiteid", "gsc_viasiteid" });

                _tracingService.Trace("AllocatedVehicle records retrieved: " + allocatedVehicleCollection.Entities.Count);
                if (allocatedVehicleCollection.Entities.Count > 0)
                {
                    // Check if request is from shipping button
                    if (isShipping)
                    {
                        // Create Vehicle In-Transit Transfer Receiving Entity
                        _receivingHandler.CreateRecevingEntity(vehicleInTransitTransfer);
                    }

                    foreach (Entity allocatedVehicleEntity in allocatedVehicleCollection.Entities)
                    {
                        Guid inventoryId = allocatedVehicleEntity.Contains("gsc_inventoryid")
                            ? allocatedVehicleEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id
                            : Guid.Empty;

                        Guid viaSiteId = allocatedVehicleEntity.Contains("gsc_viasiteid")
                            ? allocatedVehicleEntity.GetAttributeValue <EntityReference>("gsc_viasiteid").Id
                            : Guid.Empty;

                        //Retrieve 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_productid" });

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

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

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

                            //Retrieve source site product quantity
                            EntityCollection sourceProdQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", sourceProdQuantityId, _organizationService,
                                                                                                                    null, OrderType.Ascending, new[] { "gsc_onhand", "gsc_allocated", "gsc_productid", "gsc_vehiclecolorid", "gsc_vehiclemodelid" });

                            _tracingService.Trace("Source ProductQuantity records retrieved: " + sourceProdQuantityCollection.Entities.Count);

                            if (sourceProdQuantityCollection.Entities.Count > 0)
                            {
                                Entity sourceProdQuantity = sourceProdQuantityCollection.Entities[0];
                                InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService);

                                //Retrieve destination site product quantity
                                //var viaSiteId = vehicleInTransitTransfer.Contains("gsc_viasiteid") ? vehicleInTransitTransfer.GetAttributeValue<EntityReference>("gsc_viasiteid").Id
                                //: Guid.Empty;
                                var productId = sourceProdQuantity.Contains("gsc_productid") ? sourceProdQuantity.GetAttributeValue <EntityReference>("gsc_productid").Id
                                    : Guid.Empty;
                                Entity vehicleColor = inventoryMovement.GetVehicleColorReference(inventory);

                                var viaSiteConditionList = new List <ConditionExpression>
                                {
                                    new ConditionExpression("gsc_siteid", ConditionOperator.Equal, viaSiteId),
                                    new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId),
                                    new ConditionExpression("gsc_vehiclecolorid", ConditionOperator.Equal, vehicleColor.Id)
                                };

                                EntityCollection viaSiteProductQuantityRecords = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", viaSiteConditionList, _organizationService, null,
                                                                                                                           OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_productid", "gsc_vehiclecolorid", "gsc_vehiclemodelid" });

                                _tracingService.Trace("Destination ProductQuantity records retrieved: " + viaSiteProductQuantityRecords.Entities.Count);

                                Entity viaSiteProductQuantity = new Entity("gsc_iv_productquantity");
                                if (viaSiteProductQuantityRecords.Entities == null || viaSiteProductQuantityRecords.Entities.Count == 0)
                                {
                                    Guid             viaQuantityId         = inventoryMovement.CreateProductQuantity(allocatedVehicleEntity.GetAttributeValue <EntityReference>("gsc_viasiteid"), productId, new EntityReference("gsc_cmn_vehiclecolor", vehicleColor.Id));
                                    EntityCollection viaQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", viaQuantityId, _organizationService, null, OrderType.Ascending,
                                                                                                                     new[] { "gsc_onhand", "gsc_available", "gsc_productid", "gsc_vehiclecolorid", "gsc_vehiclemodelid" });
                                    viaSiteProductQuantity = viaQuantityCollection.Entities[0];
                                }

                                else
                                {
                                    viaSiteProductQuantity = viaSiteProductQuantityRecords.Entities[0];
                                }
                                #region BL execution of method

                                //Update Inventory Status = Available

                                inventory["gsc_productquantityid"] = new EntityReference(viaSiteProductQuantity.LogicalName, viaSiteProductQuantity.Id);
                                inventoryMovement.UpdateInventoryStatus(inventory, 100000000);
                                _tracingService.Trace("Updated inventory record...");

                                //Adjust source product quantity
                                Entity sourceQuantityEntity = inventoryMovement.UpdateProductQuantityDirectly(sourceProdQuantity, -1, 0, -1, 0, 0, 0, 0, 0);
                                Entity branchEntity         = GetBranchEntity(vehicleInTransitTransfer, true);
                                var    fromSiteId           = vehicleInTransitTransfer.GetAttributeValue <EntityReference>("gsc_sourcesiteid").Id;

                                inventoryMovement.CreateInventoryHistory("Vehicle In Transit Transfer", branchEntity.GetAttributeValue <string>("accountnumber"), branchEntity.GetAttributeValue <string>("name"), vehicleInTransitTransfer.GetAttributeValue <string>("gsc_vehicleintransittransferpn"),
                                                                         DateTime.UtcNow, 1, 1, sourceQuantityEntity.GetAttributeValue <Int32>("gsc_onhand"), viaSiteId, fromSiteId, fromSiteId, inventory, sourceQuantityEntity, true, false);
                                _tracingService.Trace("Source Product Quantity updated...");

                                //Adjust site product quantity
                                inventoryMovement.UpdateProductQuantityDirectly(viaSiteProductQuantity, 1, 1, 0, 0, 0, 0, 0, 0);
                                branchEntity = GetBranchEntity(vehicleInTransitTransfer, false);

                                inventoryMovement.CreateInventoryHistory("Vehicle In Transit Transfer", branchEntity.GetAttributeValue <string>("accountnumber"), branchEntity.GetAttributeValue <string>("name"), vehicleInTransitTransfer.GetAttributeValue <string>("gsc_vehicleintransittransferpn"),
                                                                         DateTime.UtcNow, 1, 1, viaSiteProductQuantity.GetAttributeValue <Int32>("gsc_onhand"), viaSiteId, fromSiteId, viaSiteId, inventory, viaSiteProductQuantity, true, true);
                                _tracingService.Trace("Destination Product Quantity updated...");

                                //Clear inventoryidtoallocate field
                                vehicleInTransitTransfer["gsc_inventoryidtoallocate"] = "";
                                _organizationService.Update(vehicleInTransitTransfer);
                                _tracingService.Trace("Updated Vehicle In-Transit Transfer record...");
                                #endregion
                            }
                        }
                    }
                }
                else
                {
                    throw new InvalidPluginExecutionException("No Allocated Vehicle to Ship.");
                }
            }

            //Status != Picked
            else
            {
                _tracingService.Trace("Status is not Picked...");
                throw new InvalidPluginExecutionException("Unable to Ship Vehicle In-Transit Transfer record with this status.");
            }
            _tracingService.Trace("Ending ShipVehicle method...");
        }
Ejemplo n.º 2
0
        //Created By: Raphael Herrera, Created On: 8/31/2016

        /*Purpose: Perform BL for receiving Vehicle In-Transit Transfer records
         * Registration Details:
         * Event/Message:
         *      Post/Update: gsc_intransitstatus
         * Primary Entity: gsc_iv_vehicleintransittransferreceiving
         */
        public void ReceiveTransfer(Entity vehicleTransferReceiving)
        {
            _tracingService.Trace("Started ReceiveTransfer Method...");
            var inTransitTransferId = vehicleTransferReceiving.Contains("gsc_intransittransferid") ? vehicleTransferReceiving.GetAttributeValue <EntityReference>("gsc_intransittransferid").Id
                : Guid.Empty;

            //Retrieve Vehicle In-Transit Transfer
            EntityCollection inTransitCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", inTransitTransferId, _organizationService,
                                                                                           null, OrderType.Ascending, new[] { "gsc_intransittransferstatus" });

            _tracingService.Trace("Vehicle In-Transit Transfer records retrieved: " + inTransitCollection.Entities.Count);
            if (inTransitCollection.Entities.Count > 0)
            {
                Entity           vehicleInTransit           = inTransitCollection.Entities[0];
                EntityCollection receivingDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransitreceivingdetail", "gsc_intransitreceivingid", vehicleTransferReceiving.Id, _organizationService,
                                                                                                      null, OrderType.Ascending, new[] { "gsc_inventoryid", "gsc_destinationsiteid" });

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

                        //Retrieve inventory
                        EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService,
                                                                                                       null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid", "gsc_color", "gsc_productid" });

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

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

                            //Retrieve product quantity of Via Site
                            EntityCollection viaQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService,
                                                                                                             null, OrderType.Ascending, new[] { "gsc_available", "gsc_onhand", "gsc_allocated", "gsc_productid", "gsc_vehiclemodelid", "gsc_siteid", "gsc_vehiclecolorid" });

                            _tracingService.Trace("Via Site ProductQuantity records retrieved: " + viaQuantityCollection.Entities.Count);
                            if (viaQuantityCollection.Entities.Count > 0)
                            {
                                Entity viaProdQuantity = viaQuantityCollection.Entities[0];

                                //Retrieve Product Quantity of Destination Site
                                var destinationSite = vehicleTransferReceiving.Contains("gsc_destinationsiteid") ? vehicleTransferReceiving.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Id
                                    : Guid.Empty;
                                var productId = viaProdQuantity.Contains("gsc_productid") ? viaProdQuantity.GetAttributeValue <EntityReference>("gsc_productid").Id
                                  : Guid.Empty;
                                InventoryMovementHandler inventoryHandler = new InventoryMovementHandler(_organizationService, _tracingService);
                                Entity vehicleColor = inventoryHandler.GetVehicleColorReference(inventory);

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

                                EntityCollection destinationQuantityCollection = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", destinationConditionList, _organizationService, null,
                                                                                                                           OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_allocated", "gsc_vehiclemodelid", "gsc_productid", "gsc_vehiclecolorid" });

                                Entity destinationQuantityEntity = new Entity("gsc_iv_productquantity");
                                _tracingService.Trace("Destination ProductQuantity records retrieved: " + destinationQuantityCollection.Entities.Count);

                                if (destinationQuantityCollection.Entities.Count == 0)
                                {
                                    Guid             destinationQuantityId = inventoryHandler.CreateProductQuantity(receivingDetailsEntity.GetAttributeValue <EntityReference>("gsc_destinationsiteid"), productId, new EntityReference("gsc_cmn_vehiclecolor", vehicleColor.Id));
                                    EntityCollection destinationCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", destinationQuantityId, _organizationService, null, OrderType.Ascending,
                                                                                                                     new[] { "gsc_onhand", "gsc_available", "gsc_productid", "gsc_vehiclecolorid", "gsc_vehiclemodelid" });
                                    destinationQuantityEntity = destinationCollection.Entities[0];

                                    _tracingService.Trace("Created Product Quantity...");
                                }
                                else
                                {
                                    destinationQuantityEntity = destinationQuantityCollection.Entities[0];
                                }


                                #region BL for Receiving Vehicle In-Transit Transfer Record

                                Int32 viaAvailable         = viaProdQuantity.GetAttributeValue <Int32>("gsc_available");
                                Int32 viaOnHand            = viaProdQuantity.GetAttributeValue <Int32>("gsc_onhand");
                                Int32 destinationAvailable = destinationQuantityEntity.GetAttributeValue <Int32>("gsc_available");
                                Int32 destinationOnHand    = destinationQuantityEntity.GetAttributeValue <Int32>("gsc_onhand");

                                //Update Inventory Product Quantity
                                inventory["gsc_productquantityid"] = new EntityReference(destinationQuantityEntity.LogicalName, destinationQuantityEntity.Id);
                                inventoryHandler.UpdateInventoryFields(inventory, "Update");
                                _tracingService.Trace("Updated Inventory Status...");

                                //Update Product Quantity of Via Site
                                inventoryHandler.UpdateProductQuantityDirectly(viaProdQuantity, -1, -1, -1, 0, 0, 0, 0, 0);//Included value in allocated for recount
                                _tracingService.Trace("Updated Via Site Product Quantity...");
                                Entity branchEntity = GetBranchEntity(vehicleTransferReceiving);
                                Guid   viaSiteId    = viaProdQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id;
                                inventoryHandler.CreateInventoryHistory("Vehicle In Transit Transfer Receiving", branchEntity.GetAttributeValue <string>("accountnumber"), branchEntity.GetAttributeValue <string>("name"),
                                                                        vehicleTransferReceiving.GetAttributeValue <string>("gsc_vehicleintransittransferreceivingpn"), DateTime.UtcNow, 1, 1, viaProdQuantity.GetAttributeValue <Int32>("gsc_onhand"), destinationSite,
                                                                        viaSiteId, viaSiteId, inventory, viaProdQuantity, true, false);
                                _tracingService.Trace("Created inventory history log...");

                                //Update Product Quantity of Destination Site
                                inventoryHandler.UpdateProductQuantityDirectly(destinationQuantityEntity, 1, 1, 1, 0, 0, 0, 0, 0);//Included value in allocated for recount
                                _tracingService.Trace("Updated Destination Site Product Quantity...");
                                inventoryHandler.CreateInventoryHistory("Vehicle In Transit Transfer Receiving", branchEntity.GetAttributeValue <string>("accountnumber"), branchEntity.GetAttributeValue <string>("name"),
                                                                        vehicleTransferReceiving.GetAttributeValue <string>("gsc_vehicleintransittransferreceivingpn"), DateTime.UtcNow, 1, 1, destinationQuantityEntity.GetAttributeValue <Int32>("gsc_onhand"), destinationSite,
                                                                        viaSiteId, destinationSite, inventory, destinationQuantityEntity, true, true);
                                _tracingService.Trace("Created inventory history log...");

                                //Update Vehicle In-Transit Transfer. Status = Received
                                vehicleInTransit["gsc_intransittransferstatus"] = new OptionSetValue(100000002);
                                _organizationService.Update(vehicleInTransit);
                                _tracingService.Trace("Updated Vehicle In-Transit Transfer...");
                                #endregion
                            }
                            else
                            {
                                throw new InvalidPluginExecutionException("No Via Site Found...");
                            }
                        }
                        else
                        {
                            throw new InvalidPluginExecutionException("No Inventory Record Found...");
                        }
                    }
                }
                else
                {
                    throw new InvalidPluginExecutionException("No Receiving Details Found...");
                }
            }
            _tracingService.Trace("Ending ReceiveTransfer Method...");
        }