/// <summary> /// Executes the plug-in. /// </summary> /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the /// <see cref="IPluginExecutionContext"/>, /// <see cref="IOrganizationService"/> /// and <see cref="ITracingService"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. /// The plug-in's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the plug-in. Also, multiple system threads /// could execute the plug-in at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in plug-ins. /// </remarks> protected void ExecutePreValidateInventoryCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity inventoryEntity = (Entity)context.InputParameters["Target"]; string message = context.MessageName; string error = ""; try { InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(service, trace); inventoryMovementHandler.UpdateInventoryFields(inventoryEntity, message); } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); } }
private Entity RetrieveOldProductQuantity(Entity inventory, Entity vehicleSalesReturnEntity, Guid siteId) { _tracingService.Trace("Update Sold Quantity From Old Site."); InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService); var productQuantityId = inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id; EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_sold", "gsc_available", "gsc_onhand", "gsc_siteid", "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_productid" }); _tracingService.Trace(productQuantityRecords.Entities.Count + " Product Quantity Records Retrieved..."); if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0) { _tracingService.Trace("Product Quantity Retrieved."); return(productQuantityRecords.Entities[0]); /* var presold = productQuantity.GetAttributeValue<Int32>("gsc_sold"); * var sold = 0; * if (presold != 0) * { * sold = presold - 1; * } * productQuantity["gsc_sold"] = sold; * _tracingService.Trace("Adjusting Product Quantity...");*/ // _organizationService.Update(productQuantity); //return productQuantity; } return(null); }
/// <summary> /// Executes the plug-in. /// </summary> /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the /// <see cref="IPluginExecutionContext"/>, /// <see cref="IOrganizationService"/> /// and <see cref="ITracingService"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. /// The plug-in's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the plug-in. Also, multiple system threads /// could execute the plug-in at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in plug-ins. /// </remarks> protected void ExecutePreProductQuantityCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity entity = (Entity)context.InputParameters["Target"]; try { string message = context.MessageName; InventoryMovementHandler handler = new InventoryMovementHandler(service, trace); handler.PopulateVehicleDetails(entity, message); } catch (Exception ex) { throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, ex)); } }
//Created By: Jerome Anthony Gerero, Created On: 7/19/2017 /*Purpose: Delete transferred vehicles * Registration Details: * Event/Message: * Pre/Delete: Allocated Items to Delete * Primary Entity: Vehicle In-Transit Transfer */ public Entity DeleteInTransitTransferVehicle(Entity vehicleInTransitTransferEntity) { _tracingService.Trace("Started DeleteInTransitTransferVehicle method..."); if (vehicleInTransitTransferEntity.GetAttributeValue <OptionSetValue>("gsc_intransittransferstatus").Value != 100000000) { return(null); } Guid vehicleInTransitTransferDetailId = vehicleInTransitTransferEntity.Contains("gsc_allocateditemstodelete") ? new Guid(vehicleInTransitTransferEntity.GetAttributeValue <String>("gsc_allocateditemstodelete")) : Guid.Empty; EntityCollection vehicleInTransitTransferDetailRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransferdetail", "gsc_iv_vehicleintransittransferdetailid", vehicleInTransitTransferDetailId, _organizationService, null, OrderType.Ascending, new[] { "gsc_inventoryid" }); if (vehicleInTransitTransferDetailRecords.Entities.Count > 0) { Entity vehicleInTransitTransferDetail = vehicleInTransitTransferDetailRecords.Entities[0]; Guid inventoryId = vehicleInTransitTransferDetail.Contains("gsc_inventoryid") ? vehicleInTransitTransferDetail.GetAttributeValue <EntityReference>("gsc_inventoryid").Id : Guid.Empty; //Retrieve and update inventory EntityCollection inventoryRecords = 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" }); if (inventoryRecords.Entities.Count > 0) { Entity inventory = inventoryRecords.Entities[0]; InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService); inventoryMovement.UpdateInventoryStatus(inventory, 100000000); Guid productQuantityId = inventory.Contains("gsc_productquantityid") ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_available", "gsc_allocated", "gsc_vehiclemodelid", "gsc_vehiclecolorid" }); if (productQuantityRecords.Entities.Count > 0) { Entity productQuantity = productQuantityRecords.Entities[0]; inventoryMovement.UpdateProductQuantityDirectly(productQuantity, 0, 1, -1, 0, 0, 0, 0, 0); _organizationService.Delete(vehicleInTransitTransferDetail.LogicalName, vehicleInTransitTransferDetail.Id); } } } vehicleInTransitTransferEntity["gsc_allocateditemstodelete"] = String.Empty; _organizationService.Update(vehicleInTransitTransferEntity); _tracingService.Trace("Ending DeleteInTransitTransferVehicle method..."); return(vehicleInTransitTransferEntity); }
//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); }
/// <summary> /// Executes the plug-in. /// </summary> /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the /// <see cref="IPluginExecutionContext"/>, /// <see cref="IOrganizationService"/> /// and <see cref="ITracingService"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. /// The plug-in's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the plug-in. Also, multiple system threads /// could execute the plug-in at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in plug-ins. /// </remarks> protected void ExecutePostInventoryUpdate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null; Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; string message = context.MessageName; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } Entity quoteDiscountEntity = (Entity)context.InputParameters["Target"]; if (quoteDiscountEntity.LogicalName != "gsc_iv_inventory") { return; } if (context.Mode == 0) //Synchronous Plugin { try { var preImageProductQuantityId = preImageEntity.Contains("gsc_productquantityid") ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; var postImageProductQuantityId = postImageEntity.Contains("gsc_productquantityid") ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(service, trace); if (preImageProductQuantityId != postImageProductQuantityId) { inventoryMovementHandler.UpdateInventoryFields(postImageEntity, message); } } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } }
//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); }
private Entity TransferInventoryToNewSite(Entity productQuantityDestination, Entity inventory, Boolean newlyCreated) { //Update of inventory status inventory["gsc_status"] = new OptionSetValue(100000000); inventory["gsc_productquantityid"] = new EntityReference("gsc_iv_productquantity", productQuantityDestination.Id); _organizationService.Update(inventory); _tracingService.Trace("Updated inventory status to available..."); if (!newlyCreated) { InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService); return(inventoryMovement.UpdateProductQuantityDirectly(productQuantityDestination, 1, 1, 0, 0, 0, 0, 0, 0)); } return(productQuantityDestination); }
//Created By: Leslie G. Baliguat, Created On: 02/02/17 //Increase in on-hand and available in In-transit Site Upon creation public void AddOnHandAvailable(Entity vehicleReceivingItem) { var inventoryId = vehicleReceivingItem.GetAttributeValue <EntityReference>("gsc_inventoryid") != null ? vehicleReceivingItem.GetAttributeValue <EntityReference>("gsc_inventoryid").Id : Guid.Empty; EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending, new[] { "gsc_inventorypn" }); if (inventoryRecords != null && inventoryRecords.Entities.Count > 0) { Entity inventory = inventoryRecords.Entities[0]; InventoryMovementHandler handler = new InventoryMovementHandler(_organizationService, _tracingService); handler.UpdateProductQuantity(inventory, 1, 1, 0, 0, 0, 0, 0, 0); } }
public Entity SubtractUnservedVPO(Entity purchaseOrder) { if (!purchaseOrder.FormattedValues["gsc_vpostatus"].Equals("Cancelled")) { return(null); } _tracingService.Trace("Started SubtractUnservedVPO method..."); var productQuantityId = CommonHandler.GetEntityReferenceIdSafe(purchaseOrder, "gsc_productquantityid"); EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_onorder" }); if (productQuantityCollection != null && productQuantityCollection.Entities.Count > 0) { _tracingService.Trace("Retrieve Product Quantity..."); Entity productQuantity = productQuantityCollection.Entities[0]; var onOrder = productQuantity.Contains("gsc_onorder") ? productQuantity.GetAttributeValue <Int32>("gsc_onorder") : 0; productQuantity["gsc_onorder"] = onOrder - 1; _organizationService.Update(productQuantity); EntityCollection purchaseOrderDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_purchaseorderitemdetails", "gsc_purchaseorderid", purchaseOrder.Id, _organizationService, null, OrderType.Ascending, new[] { "gsc_productid", "gsc_vehiclecolorid", "gsc_basemodelid", "gsc_modelcode", "gsc_optioncode", "gsc_modelyear" }); if (purchaseOrderDetailsCollection.Entities.Count > 0) { InventoryMovementHandler handler = new InventoryMovementHandler(_organizationService, _tracingService); handler.LogInventoryQuantityOnorder(purchaseOrder, purchaseOrderDetailsCollection.Entities[0], productQuantity, false, 100000004); } } _tracingService.Trace("Ended SubtractUnservedVPO method..."); return(purchaseOrder); }
//Descrease in Unserved PO in Destination Site public void SubtractinUnservedPO(Entity vehicleReceivingItem) { var vehicleReceivingId = vehicleReceivingItem.GetAttributeValue <EntityReference>("gsc_receivingtransactionid") != null ? vehicleReceivingItem.GetAttributeValue <EntityReference>("gsc_receivingtransactionid").Id : Guid.Empty; EntityCollection receivingRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_receivingtransaction", "gsc_cmn_receivingtransactionid", vehicleReceivingId, _organizationService, null, OrderType.Ascending, new[] { "gsc_purchaseorderid" }); if (receivingRecords != null && receivingRecords.Entities.Count > 0) { Entity vehicleReceiving = receivingRecords.Entities[0]; var purchaseOrderId = vehicleReceiving.GetAttributeValue <EntityReference>("gsc_purchaseorderid") != null ? vehicleReceiving.GetAttributeValue <EntityReference>("gsc_purchaseorderid").Id : Guid.Empty; EntityCollection poRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_purchaseorder", "gsc_cmn_purchaseorderid", purchaseOrderId, _organizationService, null, OrderType.Ascending, new[] { "gsc_productquantityid" }); if (poRecords != null && poRecords.Entities.Count > 0) { Entity purchaseOrder = poRecords.Entities[0]; var productQuantityId = purchaseOrder.GetAttributeValue <EntityReference>("gsc_productquantityid") != null ? purchaseOrder.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_allocated", "gsc_onorder", "gsc_sold", "gsc_intransit", "gsc_damaged", "gsc_backorder" }); if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0) { InventoryMovementHandler handler = new InventoryMovementHandler(_organizationService, _tracingService); handler.UpdateProductQuantityDirectly(productQuantityRecords.Entities[0], 0, 0, 0, -1, 0, 0, 0, 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..."); }
//Created By : Raphael Herrera, Created On : 6/3/2016 //Modified By : Jerome Anthony Gerero, Modified On : 1/5/2017 /*Purpose: Set status to posted then update inventory count * Event/Message: * Post/Update: gsc_posttransaction * Primary Entity: Vehicle Sales Return */ public Entity PostTransaction(Entity vehicleSalesReturnEntity) { _tracingService.Trace("Started PostTransaction method..."); Guid inventoryId = vehicleSalesReturnEntity.Contains("gsc_inventoryid") ? vehicleSalesReturnEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id : Guid.Empty; EntityCollection inventoryRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid", "gsc_modelcode", "gsc_optioncode" }); String customerId = vehicleSalesReturnEntity.Contains("gsc_customerid") ? vehicleSalesReturnEntity.GetAttributeValue <String>("gsc_customerid"): String.Empty; String customerName = vehicleSalesReturnEntity.Contains("gsc_customername") ? vehicleSalesReturnEntity.GetAttributeValue <String>("gsc_customername"): String.Empty; String transactionNumber = vehicleSalesReturnEntity.Contains("gsc_vehiclesalesreturnpn") ? vehicleSalesReturnEntity.GetAttributeValue <String>("gsc_vehiclesalesreturnpn") : String.Empty; DateTime transactionDate = DateTime.UtcNow; _tracingService.Trace(inventoryRecords.Entities.Count + " Inventory Records Retrieved..."); if (inventoryRecords != null && inventoryRecords.Entities.Count > 0) { Entity inventory = inventoryRecords.Entities[0]; Guid productQuantityId = inventory.Contains("gsc_productquantityid") ? inventory.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; _tracingService.Trace("Retrieved ProductQuantity..."); inventory["gsc_status"] = new OptionSetValue(100000000); _organizationService.Update(inventory); _tracingService.Trace("Updated Inventory Status to Release..."); EntityCollection productQuantityRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_sold", "gsc_available", "gsc_onhand", "gsc_siteid", "gsc_vehiclecolorid", "gsc_vehiclemodelid", "gsc_productid" }); _tracingService.Trace(productQuantityRecords.Entities.Count + " Product Quantity Records Retrieved..."); if (productQuantityRecords != null && productQuantityRecords.Entities.Count > 0) { Entity productQuantity = productQuantityRecords.Entities[0]; var presold = productQuantity.GetAttributeValue <Int32>("gsc_sold"); var sold = 0; if (presold != 0) { sold = presold - 1; } Int32 onHand = productQuantity.GetAttributeValue <Int32>("gsc_onhand") + 1; productQuantity["gsc_sold"] = sold; productQuantity["gsc_available"] = productQuantity.GetAttributeValue <Int32>("gsc_available") + 1; productQuantity["gsc_onhand"] = onHand; _tracingService.Trace("Adjusting Product Quantity..."); _organizationService.Update(productQuantity); InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService); Guid fromSite = productQuantity.Contains("gsc_siteid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty; inventoryMovementHandler.CreateInventoryHistory("Vehicle Sales Return", customerId, customerName, transactionNumber, transactionDate, 0, 1, onHand, Guid.Empty, fromSite, fromSite, inventory, productQuantity, true, true); } } _tracingService.Trace("Update gsc_vehiclesalesreturnstatus"); vehicleSalesReturnEntity["gsc_vehiclesalesreturnstatus"] = new OptionSetValue(100000001); _organizationService.Update(vehicleSalesReturnEntity); _tracingService.Trace("Ended PostTransaction method..."); return(vehicleSalesReturnEntity); }
//Created By: Raphael Herrera, Created On: 01/30/2017 /* Purpose: Create Product Qunatity record for PO * Registration Details: * Event/Message: * Post/Update: gsc_vpostatus = 100,000,002 Ordered * Primary Entity: Purchase Order */ public Entity CreateProductQuantity(Entity vehiclePurchaseOrder, Entity preVehiclePurchaseOrder) { if (!vehiclePurchaseOrder.FormattedValues["gsc_vpostatus"].Equals("Ordered")) { return(null); } if (ValidateSubmit(preVehiclePurchaseOrder) == true) { _tracingService.Trace("Starting CreateProductQuantity Method"); var siteId = vehiclePurchaseOrder.Contains("gsc_siteid") ? vehiclePurchaseOrder.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty; String siteName = vehiclePurchaseOrder.Contains("gsc_siteid") ? vehiclePurchaseOrder.GetAttributeValue <EntityReference>("gsc_siteid").Name : String.Empty; EntityCollection purchaseOrderDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_purchaseorderitemdetails", "gsc_purchaseorderid", vehiclePurchaseOrder.Id, _organizationService, null, OrderType.Ascending, new[] { "gsc_productid", "gsc_vehiclecolorid", "gsc_basemodelid", "gsc_modelcode", "gsc_optioncode", "gsc_modelyear" }); _tracingService.Trace("VPO Details Records Retrieved: " + purchaseOrderDetailsCollection.Entities.Count); if (purchaseOrderDetailsCollection.Entities.Count > 0) { Entity purchaseOrderDetailsEntity = purchaseOrderDetailsCollection.Entities[0]; var productId = purchaseOrderDetailsEntity.Contains("gsc_productid") ? purchaseOrderDetailsEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; var colorId = purchaseOrderDetailsEntity.Contains("gsc_vehiclecolorid") ? purchaseOrderDetailsEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid").Id : Guid.Empty; String productName = purchaseOrderDetailsEntity.Contains("gsc_productid") ? purchaseOrderDetailsEntity.GetAttributeValue <EntityReference>("gsc_productid").Name : String.Empty; var productQuantityId = Guid.Empty; var productQuantityConditionList = new List <ConditionExpression> { new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId), // new ConditionExpression("gsc_isonorder", ConditionOperator.Equal, true), new ConditionExpression("gsc_vehiclecolorid", ConditionOperator.Equal, colorId) }; if (siteId == Guid.Empty) { _tracingService.Trace("Empty Site..."); productQuantityConditionList.Add(new ConditionExpression("gsc_siteid", ConditionOperator.Null)); } else//filter by site id { productQuantityConditionList.Add(new ConditionExpression("gsc_siteid", ConditionOperator.Equal, siteId)); } EntityCollection productQuantityCollection = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", productQuantityConditionList, _organizationService, null, OrderType.Ascending, new[] { "gsc_onorder" }); _tracingService.Trace("Product Quantity Records Retrieved: " + productQuantityCollection.Entities.Count); if (productQuantityCollection.Entities.Count > 0)//Update Existing Product Quantity { Entity productQuantityEntity = productQuantityCollection.Entities[0]; Int32 onOrder = productQuantityEntity.Contains("gsc_onorder") ? productQuantityEntity.GetAttributeValue <Int32>("gsc_onorder") : 0; productQuantityEntity["gsc_onorder"] = onOrder + 1; _organizationService.Update(productQuantityEntity); productQuantityId = productQuantityEntity.Id; _tracingService.Trace("Updated Existing Product Quantity"); InventoryMovementHandler handler = new InventoryMovementHandler(_organizationService, _tracingService); handler.LogInventoryQuantityOnorder(vehiclePurchaseOrder, purchaseOrderDetailsEntity, productQuantityEntity, true, 100000000); } else//Create New Product Quantity Record { Entity productQuantityEntity = new Entity("gsc_iv_productquantity"); if (siteId != Guid.Empty) { productQuantityEntity["gsc_siteid"] = new EntityReference("gsc_iv_site", siteId); } productQuantityEntity["gsc_vehiclecolorid"] = new EntityReference("gsc_cmn_vehiclecolor", colorId); productQuantityEntity["gsc_productid"] = new EntityReference("product", productId); productQuantityEntity["gsc_vehiclemodelid"] = purchaseOrderDetailsEntity.GetAttributeValue <EntityReference>("gsc_basemodelid") != null ? purchaseOrderDetailsEntity.GetAttributeValue <EntityReference>("gsc_basemodelid") : null; //productQuantityEntity["gsc_isonorder"] = true; productQuantityEntity["gsc_onorder"] = 1; productQuantityEntity["gsc_productquantitypn"] = productName; productQuantityEntity["gsc_dealerid"] = vehiclePurchaseOrder.Contains("gsc_dealerid") ? vehiclePurchaseOrder.GetAttributeValue <EntityReference>("gsc_dealerid") : null; productQuantityEntity["gsc_branchid"] = vehiclePurchaseOrder.Contains("gsc_branchid") ? vehiclePurchaseOrder.GetAttributeValue <EntityReference>("gsc_branchid") : null; productQuantityEntity["gsc_recordownerid"] = vehiclePurchaseOrder.Contains("gsc_recordownerid") ? vehiclePurchaseOrder.GetAttributeValue <EntityReference>("gsc_recordownerid") : null; productQuantityEntity["gsc_onhand"] = 0; productQuantityEntity["gsc_available"] = 0; productQuantityEntity["gsc_allocated"] = 0; productQuantityEntity["gsc_sold"] = 0; productQuantityId = _organizationService.Create(productQuantityEntity); _tracingService.Trace("Created New Product QUantity"); EntityCollection newPQCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", productQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_productquantitypn" }); if (newPQCollection.Entities.Count > 0)//Update Existing Product Quantity { InventoryMovementHandler handler = new InventoryMovementHandler(_organizationService, _tracingService); handler.LogInventoryQuantityOnorder(vehiclePurchaseOrder, purchaseOrderDetailsEntity, newPQCollection.Entities[0], true, 100000000); } } vehiclePurchaseOrder["gsc_productquantityid"] = new EntityReference("gsc_iv_productquantity", productQuantityId); _organizationService.Update(vehiclePurchaseOrder); _tracingService.Trace("Updated Purchase Order Record..."); } _tracingService.Trace("Ending CreateProductQuantity Method..."); return(vehiclePurchaseOrder); } else { throw new InvalidPluginExecutionException("Unable to post this record."); } }
//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..."); }
//Created By : Raphael Herrera, Created On : 7/19/2016 /*Purpose: Replicate Receiving Transaction Fields * Registration Details: * Event/Message: * Pre/Create: * Post/Update: * Post/Create: Return Transaction * Primary Entity: Return Transaction */ public void PopulateReturnTransactionFields(Entity returnTransaction) { _tracingService.Trace("Started PopulateReturnTransactionFields Method..."); var receivingTransactionId = returnTransaction.Contains("gsc_receivingtransactionid") ? returnTransaction.GetAttributeValue <EntityReference>("gsc_receivingtransactionid").Id : Guid.Empty; EntityCollection receivingTransactionCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_receivingtransaction", "gsc_cmn_receivingtransactionid", receivingTransactionId, _organizationService, null, OrderType.Ascending, new [] { "gsc_invoiceno", "gsc_purchaseorderid", "gsc_siteid", "gsc_isreturnrecordcreated" }); EntityCollection receivingDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_receivingtransactiondetail", "gsc_receivingtransactionid", receivingTransactionId, _organizationService, null, OrderType.Ascending, new[] { "gsc_productid", "gsc_modelcode", "gsc_optioncode", "gsc_modelyear", "gsc_vehiclecolorid", "gsc_csno", "gsc_vin", "gsc_productionno", "gsc_engineno", "gsc_inventoryid", "gsc_receivingtransactiondetailpn", "gsc_basemodelid" }); _tracingService.Trace("ReceivingTransaction Records: " + receivingTransactionCollection.Entities.Count + " | ReceivingDetails Records: " + receivingDetailsCollection.Entities.Count); //retrieve and delete pre existing return details EntityCollection returnDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_returntransactiondetails", "gsc_returntransactionid", returnTransaction.Id, _organizationService, null, OrderType.Ascending, new[] { "gsc_cmn_returntransactiondetailsid" }); _tracingService.Trace("Existing return details: " + returnDetailsCollection.Entities.Count); foreach (Entity returnDetails in returnDetailsCollection.Entities) { _organizationService.Delete(returnDetails.LogicalName, returnDetails.Id); } EntityReference siteid = null; InventoryMovementHandler inventoryMovementHandler = new InventoryMovementHandler(_organizationService, _tracingService); //create return transaction details if (receivingDetailsCollection.Entities.Count > 0) { foreach (Entity receivingDetailEntity in receivingDetailsCollection.Entities) { Guid inventoryid = receivingDetailEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id; siteid = inventoryMovementHandler.getSiteId(inventoryid); Entity returnTransactionDetail = new Entity("gsc_cmn_returntransactiondetails"); returnTransactionDetail["gsc_returntransactionid"] = new EntityReference(returnTransaction.LogicalName, returnTransaction.Id); returnTransactionDetail["gsc_productid"] = receivingDetailEntity.Contains("gsc_productid") ? receivingDetailEntity.GetAttributeValue <EntityReference>("gsc_productid") : null; returnTransactionDetail["gsc_modelcode"] = receivingDetailEntity.Contains("gsc_modelcode") ? receivingDetailEntity.GetAttributeValue <string>("gsc_modelcode") : string.Empty; returnTransactionDetail["gsc_optioncode"] = receivingDetailEntity.Contains("gsc_optioncode") ? receivingDetailEntity.GetAttributeValue <string>("gsc_optioncode") : string.Empty; returnTransactionDetail["gsc_modelyear"] = receivingDetailEntity.Contains("gsc_modelyear") ? receivingDetailEntity.GetAttributeValue <string>("gsc_modelyear") : string.Empty; returnTransactionDetail["gsc_vehiclecolorid"] = receivingDetailEntity.Contains("gsc_vehiclecolorid") ? receivingDetailEntity.GetAttributeValue <EntityReference>("gsc_vehiclecolorid") : null; returnTransactionDetail["gsc_csno"] = receivingDetailEntity.Contains("gsc_csno") ? receivingDetailEntity.GetAttributeValue <string>("gsc_csno") : string.Empty; returnTransactionDetail["gsc_vin"] = receivingDetailEntity.Contains("gsc_vin") ? receivingDetailEntity.GetAttributeValue <string>("gsc_vin") : string.Empty; returnTransactionDetail["gsc_productionno"] = receivingDetailEntity.Contains("gsc_productionno") ? receivingDetailEntity.GetAttributeValue <string>("gsc_productionno") : string.Empty; returnTransactionDetail["gsc_engineno"] = receivingDetailEntity.Contains("gsc_engineno") ? receivingDetailEntity.GetAttributeValue <string>("gsc_engineno") : string.Empty; returnTransactionDetail["gsc_inventoryid"] = new EntityReference("gsc_iv_inventory", inventoryid); returnTransactionDetail["gsc_returntransactionpn"] = receivingDetailEntity.Contains("gsc_receivingtransactiondetailpn") ? receivingDetailEntity.GetAttributeValue <string>("gsc_receivingtransactiondetailpn") : string.Empty; returnTransactionDetail["gsc_basemodelid"] = receivingDetailEntity.Contains("gsc_basemodelid") ? receivingDetailEntity.GetAttributeValue <EntityReference>("gsc_basemodelid") : null; _tracingService.Trace("Creating Return Transaction Detail..."); _organizationService.Create(returnTransactionDetail); } } //Update return transaction fields if (receivingTransactionCollection.Entities.Count > 0) { Entity receivingTransaction = receivingTransactionCollection.Entities[0]; returnTransaction["gsc_invoiceno"] = receivingTransaction.Contains("gsc_invoiceno") ?receivingTransaction.GetAttributeValue <string>("gsc_invoiceno") : string.Empty; returnTransaction["gsc_vpono"] = receivingTransaction.Contains("gsc_purchaseorderid") ? receivingTransaction.GetAttributeValue <EntityReference>("gsc_purchaseorderid").Name : string.Empty; returnTransaction["gsc_site"] = siteid.Name; //set returnstatus to Open returnTransaction["gsc_returnstatus"] = new OptionSetValue(100000000); returnTransaction["gsc_vrstatus"] = new OptionSetValue(100000000); _tracingService.Trace("Updating Return Transaction..."); _organizationService.Update(returnTransaction); //Set 'Is Return Record Created' field to true receivingTransaction["gsc_isreturnrecordcreated"] = true; _tracingService.Trace("Updating Receiving Transaction..."); _organizationService.Update(receivingTransaction); } _tracingService.Trace("Ending PopulateReturnTransactionFields Method..."); }
//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..."); }
//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); }
//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 CancelTransfer(Entity vehicleTransferReceiving) { _tracingService.Trace("Started Cancel Transfer Method..."); if (vehicleTransferReceiving.GetAttributeValue <OptionSetValue>("gsc_intransitstatus").Value != 100000000) { throw new InvalidPluginExecutionException("Only records with Shipped status can be canceled."); } var inTransitTransferId = vehicleTransferReceiving.Contains("gsc_intransittransferid") ? vehicleTransferReceiving.GetAttributeValue <EntityReference>("gsc_intransittransferid").Id : Guid.Empty; //Retrieve In-Transit Transfer of In-Transit Transfer Receiving EntityCollection inTransitTransferCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransfer", "gsc_iv_vehicleintransittransferid", inTransitTransferId, _organizationService, null, OrderType.Ascending, new[] { "gsc_sourcesiteid", "gsc_viasiteid" }); _tracingService.Trace("In-Transit Transfer Records Retrieved: " + inTransitTransferCollection.Entities.Count); if (inTransitTransferCollection.Entities.Count > 0) { Entity inTransitTransferEntity = inTransitTransferCollection.Entities[0]; //Retrieve Allocated Vehicles of In-Transit Transfer EntityCollection transferDetailsCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_vehicleintransittransferdetail", "gsc_vehicleintransittransferid", inTransitTransferEntity.Id, _organizationService, null, OrderType.Ascending, new[] { "gsc_sourcesiteid", "gsc_viasiteid", "gsc_inventoryid" }); _tracingService.Trace("In Transit Transfer Detail Records Retrieved: " + transferDetailsCollection.Entities.Count); if (transferDetailsCollection.Entities.Count > 0) { foreach (Entity transferDetailsEntity in transferDetailsCollection.Entities) { var sourceSiteId = transferDetailsEntity.Contains("gsc_sourcesiteid") ? transferDetailsEntity.GetAttributeValue <EntityReference>("gsc_sourcesiteid").Id : Guid.Empty; var inventoryId = transferDetailsEntity.Contains("gsc_inventoryid") ? transferDetailsEntity.GetAttributeValue <EntityReference>("gsc_inventoryid").Id : Guid.Empty; //Retrieve Inventory of Allocated Vehicle EntityCollection inventoryCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_inventory", "gsc_iv_inventoryid", inventoryId, _organizationService, null, OrderType.Ascending, new[] { "gsc_status", "gsc_productquantityid" }); _tracingService.Trace("Inventory Records Retrieved: " + inventoryCollection.Entities.Count); if (inventoryCollection.Entities.Count > 0) { Entity inventoryEntity = inventoryCollection.Entities[0]; var viaProdQuantityId = inventoryEntity.Contains("gsc_productquantityid") ? inventoryEntity.GetAttributeValue <EntityReference>("gsc_productquantityid").Id : Guid.Empty; if (inventoryEntity.GetAttributeValue <OptionSetValue>("gsc_status").Value == 100000001)//status == allocated { throw new InvalidPluginExecutionException("Vehicle in this transaction is still allocated. Please remove allocation in Sales Order before cancelling."); } //Retrieve Product Quantity of Via Site EntityCollection viaQuantityCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_iv_productquantity", "gsc_iv_productquantityid", viaProdQuantityId, _organizationService, null, OrderType.Ascending, new[] { "gsc_available", "gsc_onhand", "gsc_productid", "gsc_allocated" }); _tracingService.Trace("Via Site Product Quantity Records Retrieved: " + viaQuantityCollection.Entities.Count); if (viaQuantityCollection.Entities.Count > 0) { Entity viaQuantityEntity = viaQuantityCollection.Entities[0]; var productId = viaQuantityEntity.Contains("gsc_productid") ? viaQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; //Retrieve Product Quantity of Source Site var sourceConditionList = new List <ConditionExpression> { new ConditionExpression("gsc_siteid", ConditionOperator.Equal, sourceSiteId), new ConditionExpression("gsc_productid", ConditionOperator.Equal, productId) }; EntityCollection sourceQuantityCollection = CommonHandler.RetrieveRecordsByConditions("gsc_iv_productquantity", sourceConditionList, _organizationService, null, OrderType.Ascending, new[] { "gsc_onhand", "gsc_available", "gsc_allocated" }); _tracingService.Trace("Source Site Product Quantity Records Retrieved: " + sourceQuantityCollection.Entities.Count); if (sourceQuantityCollection.Entities.Count > 0) { #region BL for Cancellation of Vehicle In-Transit Transfer Receiving Entity sourceQuantityEntity = sourceQuantityCollection.Entities[0]; InventoryMovementHandler inventoryHandler = new InventoryMovementHandler(_organizationService, _tracingService); // Update Inventory. Status = Available inventoryEntity["gsc_productquantityid"] = new EntityReference(sourceQuantityEntity.LogicalName, sourceQuantityEntity.Id); inventoryEntity["gsc_status"] = new OptionSetValue(100000000); inventoryHandler.UpdateInventoryFields(inventoryEntity, "Update"); _tracingService.Trace("Updated Inventory Status..."); // Update Product Quantity of Via Site inventoryHandler.UpdateProductQuantityDirectly(viaQuantityEntity, -1, -1, -1, 0, 0, 0, 0, 0);//Included value in allocated for recount _tracingService.Trace("Updated Via Site Product Quantity..."); //Update Product Quantity of Source Site inventoryHandler.UpdateProductQuantityDirectly(sourceQuantityEntity, 1, 1, 1, 0, 0, 0, 0, 0);//Included value in allocated for recount _tracingService.Trace("Updated Source Site Product Quantity..."); // Delete Allocated Vehicle record _organizationService.Delete(transferDetailsEntity.LogicalName, transferDetailsEntity.Id); _tracingService.Trace("Deleted Transfer Details..."); } else { throw new InvalidPluginExecutionException("Product Quantity of source site not found..."); } } else { throw new InvalidPluginExecutionException("Product Quantity of via site not found..."); } } else { throw new InvalidPluginExecutionException("No Inventory Records Found..."); } } // Update Vehicle In-Transit Transfer Status to Picked inTransitTransferEntity["gsc_intransittransferstatus"] = new OptionSetValue(100000000); _organizationService.Update(inTransitTransferEntity); //Clear vehicle in-transit transfer lookup field vehicleTransferReceiving["gsc_intransittransferid"] = null; _organizationService.Update(vehicleTransferReceiving); _tracingService.Trace("Updated Vehicle In-Transit Transfer status to cancelled..."); #endregion } } else { throw new InvalidPluginExecutionException("No In Transit Transfer Records Found..."); } _tracingService.Trace("Ending CancelTransfer Method..."); }
//Created By : Raphael Herrera, Created On : 6/3/2016 //Modified By : Jerome Anthony Gerero, Modified On : 1/5/2017 //Modified By : Artum Ramos, Modified On : 5/11/2017 /*Purpose: Set status to posted then update inventory count * Event/Message: * Post/Update: gsc_posttransaction * Primary Entity: Vehicle Sales Return */ public Entity PostTransaction(Entity vehicleSalesReturnEntity) { _tracingService.Trace("Started PostTransaction method..."); if (IsValidInvoice(vehicleSalesReturnEntity) == false) { throw new InvalidPluginExecutionException("Sales invoice selected already returned."); } Entity inventory = RetrieveInventory(vehicleSalesReturnEntity); if (inventory != null) { _tracingService.Trace("Inventory Not Null."); Guid siteId = vehicleSalesReturnEntity.Contains("gsc_site") ? vehicleSalesReturnEntity.GetAttributeValue <EntityReference>("gsc_site").Id : Guid.Empty; Entity productQuantity = RetrieveOldProductQuantity(inventory, vehicleSalesReturnEntity, siteId); String customerId = vehicleSalesReturnEntity.Contains("gsc_customerid") ? vehicleSalesReturnEntity.GetAttributeValue <String>("gsc_customerid") : String.Empty; String customerName = vehicleSalesReturnEntity.Contains("gsc_customername") ? vehicleSalesReturnEntity.GetAttributeValue <String>("gsc_customername") : String.Empty; String transactionNumber = vehicleSalesReturnEntity.Contains("gsc_vehiclesalesreturnpn") ? vehicleSalesReturnEntity.GetAttributeValue <String>("gsc_vehiclesalesreturnpn") : String.Empty; DateTime transactionDate = DateTime.UtcNow; Guid fromSite = productQuantity.Contains("gsc_siteid") ? productQuantity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty; 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; _tracingService.Trace("Inventory History Created."); Entity productQuantityDestination = new Entity(); if (siteId != fromSite) { productQuantityDestination = RetrieveNewProductQuantity(vehicleSalesReturnEntity, productQuantity, siteId); if (productQuantityDestination != null) { TransferInventoryToNewSite(productQuantityDestination, inventory, false); } else { productQuantityDestination = CreateNewProductQuantity(vehicleSalesReturnEntity, productQuantity, siteId); productQuantityDestination = TransferInventoryToNewSite(productQuantityDestination, inventory, true); } } else { productQuantityDestination = TransferInventoryToNewSite(productQuantity, inventory, false); } InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService); Int32 onHandDestination = productQuantityDestination.GetAttributeValue <Int32>("gsc_onhand"); inventoryMovement.CreateInventoryHistory("Vehicle Sales Return", customerId, customerName, transactionNumber, transactionDate, 0, 1, onHandDestination, siteId, Guid.Empty, siteId, inventory, productQuantityDestination, true, true); inventoryMovement.UpdateProductQuantityDirectly(productQuantity, 0, 0, 0, 0, -1, 0, 0, 0); /*Int32 onHandFrom = productQuantity.GetAttributeValue<Int32>("gsc_onhand"); * inventoryMovement.CreateInventoryHistory("Vehicle Sales Return", customerId, customerName, transactionNumber, transactionDate, 1, 0, onHandFrom, siteId, fromSite, fromSite, inventory, productQuantity, true, true);*/ _tracingService.Trace("Inventory History Created."); } _tracingService.Trace("Update gsc_vehiclesalesreturnstatus"); Entity quoteToUpdate = _organizationService.Retrieve(vehicleSalesReturnEntity.LogicalName, vehicleSalesReturnEntity.Id, new ColumnSet("gsc_vehiclesalesreturnstatus", "gsc_isinvoicereturned")); vehicleSalesReturnEntity["gsc_vehiclesalesreturnstatus"] = new OptionSetValue(100000001); //Added by: JGC_05222017, Description: This will call Sales Return - Update Invoice isSalesReturned Tagging Workflow vehicleSalesReturnEntity["gsc_isinvoicereturned"] = true; //End _organizationService.Update(vehicleSalesReturnEntity); _tracingService.Trace("Ended PostTransaction method..."); return(vehicleSalesReturnEntity); }
//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."); }
/// <summary> /// Executes the plug-in. /// </summary> /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the /// <see cref="IPluginExecutionContext"/>, /// <see cref="IOrganizationService"/> /// and <see cref="ITracingService"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics CRM caches plug-in instances. /// The plug-in's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the plug-in. Also, multiple system threads /// could execute the plug-in at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in plug-ins. /// </remarks> protected void ExecutePostProductQuantityUpdate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null; Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null; IOrganizationService service = localContext.OrganizationService; ITracingService trace = localContext.TracingService; Entity receivingTransactionEntity = (Entity)context.InputParameters["Target"]; if (!(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)) { return; } if (receivingTransactionEntity.LogicalName != "gsc_cmn_receivingtransaction") { return; } if (context.Mode == 0) //Synchronous Plug-in { string message = context.MessageName; try { var preImageOnOrder = preImageEntity.Contains("gsc_onorder") ? preImageEntity.GetAttributeValue <Int32>("gsc_onorder") : 0; var preImageProduct = preImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? preImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; var postImageOnOrder = postImageEntity.Contains("gsc_onorder") ? postImageEntity.GetAttributeValue <Int32>("gsc_onorder") : 0; var postImageProduct = postImageEntity.GetAttributeValue <EntityReference>("gsc_productid") != null ? postImageEntity.GetAttributeValue <EntityReference>("gsc_productid").Id : Guid.Empty; InventoryMovementHandler handler = new InventoryMovementHandler(service, trace); if (preImageOnOrder != postImageOnOrder) { //handler.DeleteProductQuantity(postImageEntity); } if (preImageProduct != postImageProduct) { handler.PopulateVehicleDetails(postImageEntity, message); } } catch (Exception ex) { //throw new InvalidPluginExecutionException(String.Concat("(Exception)\n", ex.Message, Environment.NewLine, ex.StackTrace, Environment.NewLine, error)); throw new InvalidPluginExecutionException(ex.Message); } } }
//Created By: Raphael Herrera, Created On: 8/24/2016 //Modified By: Artum Ramos, Modified On: 8/2/2017 /*Purpose: Handle Delete AND Cancel BL for Vehicle In-Transit Transfer record * Registration Details: * Event/Message: * Pre/Delete: VehicleInTransitTransfer * Post/Update: gsc_intransittransferstatus * Primary Entity: gsc_iv_vehicleintransittransfer */ public void ValidateTransaction(Entity vehicleInTransitTransfer, string message) { _tracingService.Trace("Started ValidateDelete 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" }); _tracingService.Trace("AllocatedVehicle records retrieved: " + allocatedVehicleCollection.Entities.Count); if (allocatedVehicleCollection.Entities.Count > 0) { foreach (Entity allocatedVehicleEntity in allocatedVehicleCollection.Entities) { var inventoryId = allocatedVehicleEntity.Contains("gsc_inventoryid") ? allocatedVehicleEntity.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" }); _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count); if (inventoryCollection.Entities.Count > 0) { Entity inventory = inventoryCollection.Entities[0]; InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService); inventoryMovement.UpdateInventoryStatus(inventory, 100000000); _tracingService.Trace("Updated inventory record..."); 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" }); _tracingService.Trace("ProductQuantity records retrieved: " + productQuantityCollection.Entities.Count); if (productQuantityCollection.Entities.Count > 0) { Entity productQuantity = productQuantityCollection.Entities[0]; inventoryMovement.UpdateProductQuantityDirectly(productQuantity, 0, 1, -1, 0, 0, 0, 0, 0); _tracingService.Trace("Product Quantity updated..."); //Delete Vehicle Allocation _organizationService.Delete(allocatedVehicleEntity.LogicalName, allocatedVehicleEntity.Id); _tracingService.Trace("Deleted associated Allocated Vehicle record..."); //Clear inventoryidtoallocate field vehicleInTransitTransfer["gsc_inventoryidtoallocate"] = ""; vehicleInTransitTransfer["gsc_intransittransferstatus"] = new OptionSetValue(100000003); _organizationService.Update(vehicleInTransitTransfer); _tracingService.Trace("Updated Vehicle In-Transit Transfer record..."); } } } } } //Status != Picked else { _tracingService.Trace("Status is not Picked..."); if (message == "Delete") { throw new InvalidPluginExecutionException("Unable to delete Shipped/Received Vehicle In-Transit Transfer record."); } else if (message == "Update") { throw new InvalidPluginExecutionException("Unable to cancel Shipped/Received Vehicle In-Transit Transfer record."); } } _tracingService.Trace("Ending ValidateDelete method..."); }
//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..."); }
//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..."); }
//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); }
//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); }
//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..."); }
//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..."); }
//Created By: Raphael Herrera, Created On: 8/23/2016 /*Purpose: Create new allocated vehicle record. * Registration Details: * Event/Message: * Post/Update: gsc_inventoryidtoallocate * Primary Entity: gsc_iv_vehicleintransittransfer */ public Entity AllocateVehicle(Entity vehicleInTransitTransfer) { _tracingService.Trace("Started AllocateVehicle method..."); Guid inventoryId = vehicleInTransitTransfer.Contains("gsc_inventoryidtoallocate") ? new Guid(vehicleInTransitTransfer.GetAttributeValue <String>("gsc_inventoryidtoallocate")) : Guid.Empty; if (inventoryId == Guid.Empty) { return(null); } 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" }); _tracingService.Trace("Inventory records retrieved: " + inventoryCollection.Entities.Count); if (inventoryCollection.Entities.Count > 0) { Entity inventoryEntity = inventoryCollection.Entities[0]; 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 InventoryMovementHandler inventoryMovement = new InventoryMovementHandler(_organizationService, _tracingService); inventoryMovement.UpdateInventoryStatus(inventoryEntity, 100000001); _tracingService.Trace("Updated inventory status to allocated..."); Guid 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_vehiclemodelid", "gsc_productid" }); _tracingService.Trace("ProductQuantity records retrieved: " + productQuantityCollection.Entities.Count); if (productQuantityCollection != null && productQuantityCollection.Entities.Count > 0) { Entity productQuantityEntity = productQuantityCollection.Entities[0]; inventoryMovement.UpdateProductQuantityDirectly(productQuantityEntity, 0, -1, 1, 0, 0, 0, 0, 0); _tracingService.Trace("Updated productquantity count..."); #region Create VehicleAllocation Record Entity allocatedVehicle = new Entity("gsc_iv_vehicleintransittransferdetail"); var destinationSiteId = vehicleInTransitTransfer.Contains("gsc_destinationsiteid") ? vehicleInTransitTransfer.GetAttributeValue <EntityReference>("gsc_destinationsiteid").Id : Guid.Empty; var sourceSiteId = productQuantityEntity.Contains("gsc_siteid") ? productQuantityEntity.GetAttributeValue <EntityReference>("gsc_siteid").Id : Guid.Empty; var viaSiteId = vehicleInTransitTransfer.Contains("gsc_viasiteid") ? vehicleInTransitTransfer.GetAttributeValue <EntityReference>("gsc_viasiteid").Id : Guid.Empty; var baseModelName = productQuantityEntity.Contains("gsc_vehiclemodelid") ? productQuantityEntity.GetAttributeValue <EntityReference>("gsc_vehiclemodelid").Name : String.Empty; var productName = productQuantityEntity.Contains("gsc_productid") ? productQuantityEntity.GetAttributeValue <EntityReference>("gsc_productid").Name : String.Empty; allocatedVehicle["gsc_color"] = inventoryEntity.GetAttributeValue <String>("gsc_color"); allocatedVehicle["gsc_csno"] = inventoryEntity.GetAttributeValue <String>("gsc_csno"); allocatedVehicle["gsc_engineno"] = inventoryEntity.GetAttributeValue <String>("gsc_engineno"); allocatedVehicle["gsc_modelcode"] = inventoryEntity.GetAttributeValue <String>("gsc_modelcode"); allocatedVehicle["gsc_optioncode"] = inventoryEntity.GetAttributeValue <String>("gsc_optioncode"); allocatedVehicle["gsc_productionno"] = inventoryEntity.GetAttributeValue <String>("gsc_productionno"); allocatedVehicle["gsc_vin"] = inventoryEntity.GetAttributeValue <String>("gsc_vin"); allocatedVehicle["gsc_basemodel"] = baseModelName; allocatedVehicle["gsc_modeldescription"] = productName; allocatedVehicle["gsc_modelyear"] = inventoryEntity.GetAttributeValue <String>("gsc_modelyear"); allocatedVehicle["gsc_inventoryid"] = new EntityReference(inventoryEntity.LogicalName, inventoryEntity.Id); allocatedVehicle["gsc_vehicleintransittransferid"] = new EntityReference(vehicleInTransitTransfer.LogicalName, vehicleInTransitTransfer.Id); //allocatedVehicle["gsc_destinationsiteid"] = new EntityReference("gsc_iv_site", destinationSiteId); allocatedVehicle["gsc_sourcesiteid"] = new EntityReference("gsc_iv_site", sourceSiteId); allocatedVehicle["gsc_viasiteid"] = new EntityReference("gsc_iv_site", viaSiteId); _organizationService.Create(allocatedVehicle); _tracingService.Trace("Created vehicle allocation record..."); #endregion } #endregion } } vehicleInTransitTransfer["gsc_inventoryidtoallocate"] = String.Empty; _organizationService.Update(vehicleInTransitTransfer); _tracingService.Trace("Ending AllocateVehicle method..."); return(vehicleInTransitTransfer); }