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