//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 : 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 : 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 : 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 : 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: 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: 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..."); }