// Create or Update public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata) { string data = string.Empty; string bvin = FirstParameter(parameters); ApiResponse <ProductInventoryDTO> response = new ApiResponse <ProductInventoryDTO>(); ProductInventoryDTO postedItem = null; try { postedItem = MerchantTribe.Web.Json.ObjectFromJson <ProductInventoryDTO>(postdata); } catch (Exception ex) { response.Errors.Add(new ApiError("EXCEPTION", ex.Message)); return(MerchantTribe.Web.Json.ObjectToJson(response)); } ProductInventory item = new ProductInventory(); item.FromDto(postedItem); if (bvin == string.Empty) { bool mustCreate = false; // see if there is already an inventory object for product List <ProductInventory> existing = MTApp.CatalogServices.ProductInventories.FindByProductId(item.ProductBvin); if (existing == null || existing.Count < 1) { mustCreate = true; } else { var pi = existing.Where(y => y.VariantId == string.Empty).FirstOrDefault(); if (pi == null) { mustCreate = true; } else { pi.LowStockPoint = item.LowStockPoint; pi.QuantityOnHand = item.QuantityOnHand; pi.QuantityReserved = item.QuantityReserved; MTApp.CatalogServices.ProductInventories.Update(pi); bvin = pi.Bvin; } } // if inventory object doesn't exist yet, create one. if (mustCreate) { if (MTApp.CatalogServices.ProductInventories.Create(item)) { bvin = item.Bvin; } } } else { MTApp.CatalogServices.ProductInventories.Update(item); } ProductInventory resultItem = MTApp.CatalogServices.ProductInventories.Find(bvin); if (resultItem != null) { response.Content = resultItem.ToDto(); } data = MerchantTribe.Web.Json.ObjectToJson(response); return(data); }
/// <summary> /// Allows the REST API to create or update inventory for a product /// </summary> /// <param name="parameters"> /// Parameters passed in the URL of the REST API call. If there is a first parameter found in the /// URL, the method will assume it is the product inventory ID (bvin) and that this is an update, otherwise it assumes /// to create product inventory. /// </param> /// <param name="querystring">Name/value pairs from the REST API call querystring. This is not used in this method.</param> /// <param name="postdata">Serialized (JSON) version of the ProductInventoryDTO object</param> /// <returns>ProductInventoryDTO - Serialized (JSON) version of the product inventory</returns> public override string PostAction(string parameters, NameValueCollection querystring, string postdata) { var bvin = FirstParameter(parameters); ProductInventoryDTO postedItem = null; try { postedItem = Json.ObjectFromJson <ProductInventoryDTO>(postdata); } catch (Exception ex) { return(JsonApiResponseException(ex)); } var item = new ProductInventory(); // convert the dto to a cbo item.FromDto(postedItem); // update the store id if it isn't populated (prevents an obscured issue with updates) if (item.StoreId == 0) { item.StoreId = HccApp.CurrentStore.Id; } if (string.IsNullOrEmpty(bvin)) { // see if there is already an inventory object for product var pi = HccApp.CatalogServices.ProductInventories.FindByProductIdAndVariantId(item.ProductBvin, item.VariantId); if (pi != null) { // update the existing object with the posted information pi.OutOfStockPoint = item.OutOfStockPoint; pi.LowStockPoint = item.LowStockPoint; pi.QuantityOnHand = item.QuantityOnHand; pi.QuantityReserved = item.QuantityReserved; // update the inventory HccApp.CatalogServices.ProductInventories.Update(pi); // update the local variable to get updated data later bvin = pi.Bvin; } else { // if inventory object doesn't exist yet, create one. if (HccApp.CatalogServices.ProductInventories.Create(item)) { // update the local variable to get updated data later bvin = item.Bvin; } } } else { // update the inventory HccApp.CatalogServices.ProductInventories.Update(item); } // update the visibility/availability status of the product if (!string.IsNullOrEmpty(item.ProductBvin)) { var product = HccApp.CatalogServices.Products.Find(item.ProductBvin); if (product != null) { HccApp.CatalogServices.UpdateProductVisibleStatusAndSave(product); } } // get an updated instance of the inventory var resultItem = HccApp.CatalogServices.ProductInventories.Find(bvin); // return the result return(JsonApiResponse(resultItem.ToDto())); }