/// <summary> /// Sets the new price asynchronous. /// </summary> public static async Task SetNewPriceAsync(string itemKey) { // Validate Parameters if (string.IsNullOrEmpty(itemKey)) { ConsoleHelper.WriteErrorLine(""); ConsoleHelper.WriteErrorLine("SetNewPriceAsync: 'itemKey' parameter is required"); ConsoleHelper.WriteErrorLine(""); return; } SalesItemPriceListLineResource lineResource = await GetItemFirstPriceLineAsync(itemKey); if (lineResource == null) { // Simply inset new line await InsertPriceOnSalesItemAsync(itemKey); return; } try { double newPrice = lineResource.Price.Value + 10D; // Create the HTTP client to perform the request using (HttpClient client = new HttpClient()) { await AuthenticationProvider.SetAccessTokenAsync(client); // Build the request string request = string.Format("salesCore/salesItems/{0}/priceListLines/{1}/priceAmount", itemKey, lineResource.Id); string resourceLocation = string.Format("{0}/api/{1}/{2}/{3}", Constants.baseAppUrl, AccountKey, SubscriptionKey, request); client.SetDefaultRequestHeaders(CultureKey); // It's a PUT HttpRequestMessage putSalesItemMessage = new HttpRequestMessage(HttpMethod.Put, resourceLocation); JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }; string salesItemContent = JsonConvert.SerializeObject(newPrice, settings); putSalesItemMessage.Content = new StringContent(salesItemContent, Encoding.UTF8, "application/json"); // Send Console.WriteLine("Request - PUT"); Console.WriteLine("{0}", resourceLocation); Console.WriteLine("Request - BODY "); Console.WriteLine("{0}", salesItemContent); using (HttpResponseMessage responseContent = await client.SendAsync(putSalesItemMessage)) { // Get the response if (!responseContent.IsSuccessStatusCode) { ConsoleHelper.WriteErrorLine(string.Format("Failed. {0}", responseContent.ToString())); string errorResult = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteErrorLine(string.Format("Content: {0}", errorResult)); throw new Exception(string.Format("Unable to update the price in the first price list line of the SalesItem '{0}'", itemKey)); } // Succeeded string result = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteSuccessLine(string.Format("Update first price list line price on SalesItem: {0}", itemKey)); } } } catch (Exception exception) { ConsoleHelper.WriteErrorLine("Error found!"); ConsoleHelper.WriteErrorLine(exception.Message); throw new Exception(string.Format("Error updating the price in the first price list line of the SalesItem '{0}'", itemKey)); } }
private static async Task <SalesItemPriceListLineResource> GetItemFirstPriceLineAsync(string itemKey) { SalesItemPriceListLineResource foundPriceListLine = null; try { // Create the HTTP client to perform the request using (HttpClient client = new HttpClient()) { await AuthenticationProvider.SetAccessTokenAsync(client); // Build the request string request = "salescore/salesItems/odata"; // build the odata expression string odataExpression = string.Format("?$top=1&$filter=ItemKey eq '{0}' and IsActive eq true and IsSystem eq false and IsDeleted eq false", itemKey); // full request request = string.Concat(request, odataExpression); string resourceLocation = string.Format("{0}/api/{1}/{2}/{3}", Constants.baseAppUrl, AccountKey, SubscriptionKey, request); client.SetDefaultRequestHeaders(CultureKey); // Send using (HttpResponseMessage responseContent = await client.GetAsync(resourceLocation)) { // Get the response if (!responseContent.IsSuccessStatusCode) { ConsoleHelper.WriteErrorLine(string.Format("Failed. {0}", responseContent.ToString())); string result = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteErrorLine(string.Format("Content: {0}", result)); throw new Exception("Unable to get the first sales price list."); } // Succeeded string json = await((StreamContent)responseContent.Content).ReadAsStringAsync(); var objectResult = JsonConvert.DeserializeObject <ODataResponse <SalesItemResource> >(json); IList <SalesItemResource> salesItems = objectResult.Items; foundPriceListLine = salesItems[0].PriceListLines.FirstOrDefault(); } } } catch (Exception exception) { ConsoleHelper.WriteErrorLine("Error found!"); ConsoleHelper.WriteErrorLine(exception.Message); throw new Exception("Error getting the first sales price list."); } return(foundPriceListLine); }
/// <summary> /// Inserts the price on sales item asynchronous. /// </summary> public static async Task InsertPriceOnSalesItemAsync(string itemKey) { // Validate Parameters if (string.IsNullOrEmpty(itemKey)) { ConsoleHelper.WriteErrorLine(""); ConsoleHelper.WriteErrorLine("InsertPriceOnSalesItemAsync: 'itemKey' parameter is required"); ConsoleHelper.WriteErrorLine(""); return; } string priceListKey = await GetFirstPriceListAsync(); try { SalesItemPriceListLineResource newPriceListLine = new SalesItemPriceListLineResource { PriceList = priceListKey, Unit = "KM", Price = new MoneyResource() { Value = 123, Currency = "€" } }; // Create the HTTP client to perform the request using (HttpClient client = new HttpClient()) { await AuthenticationProvider.SetAccessTokenAsync(client); // Build the request string request = string.Format("salesCore/salesItems/{0}/priceListLines", itemKey); string resourceLocation = string.Format("{0}/api/{1}/{2}/{3}", Constants.baseAppUrl, AccountKey, SubscriptionKey, request); client.SetDefaultRequestHeaders(CultureKey); // It's a POST HttpRequestMessage postSalesItemMessage = new HttpRequestMessage(HttpMethod.Post, resourceLocation); JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }; string salesItemContent = JsonConvert.SerializeObject(newPriceListLine, settings); postSalesItemMessage.Content = new StringContent(salesItemContent, Encoding.UTF8, "application/json"); // Send Console.WriteLine("Request - POST"); Console.WriteLine("{0}", resourceLocation); Console.WriteLine("Request - BODY "); Console.WriteLine("{0}", salesItemContent); using (HttpResponseMessage responseContent = await client.SendAsync(postSalesItemMessage)) { // Get the response if (!responseContent.IsSuccessStatusCode) { ConsoleHelper.WriteErrorLine(string.Format("Failed. {0}", responseContent.ToString())); string errorResult = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteErrorLine(string.Format("Content: {0}", errorResult)); throw new Exception(string.Format("Unable to create a new price line on salesItem '{0}", itemKey)); } // Succeeded string result = await((StreamContent)responseContent.Content).ReadAsStringAsync(); ConsoleHelper.WriteSuccessLine(string.Format("SalesItem Line (ID {0}) created on SalesItem '{1}'", result, itemKey)); } } } catch (Exception exception) { ConsoleHelper.WriteErrorLine("Error found!"); ConsoleHelper.WriteErrorLine(exception.Message); throw new Exception(string.Format("Error creating the new price list line on salesItem '{0}'", itemKey)); } }