private static string GetDemandIdFromSklad(WebClient webClient, DemandsFromCSV item) { Console.WriteLine("Поиск отгрузки по имени и дате..."); var offset = 0; while (true) { string url = "https://online.moysklad.ru/api/remap/1.1/entity/demand?limit=100&offset=" + offset; webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json"); var HtmlResult = webClient.DownloadString(url); var jsonString = JsonConvert.DeserializeObject <Demands>(HtmlResult); if (jsonString.rows.Count == 0) { break; } var demand = jsonString.rows.FirstOrDefault( rowDemand => rowDemand.name.Contains(item.NumberDoc) && rowDemand.moment.Contains(item.RealizationDate.Substring(0, 9))); if (demand != null) { var demandId = demand.id; Console.WriteLine("Отгрузка найдена. id: " + demandId); return(demandId); } offset = offset + 100; } Console.WriteLine("!Отгрузка не найдена! name: " + item.NumberDoc + ", date: " + item.RealizationDate); return(null); }
/* private static IEnumerable<DemandsFromCSV> ReadCsvRealization(string year, string month) * { * // using (var reader = new StreamReader(@"D:\user\sklad\all realization.csv", Encoding.UTF8)) * { * var list = new List<DemandsFromCSV>(); * var lines = File.ReadAllLines(@"D:\user\sklad\all realization.csv"); * foreach (var line in lines/*.Take(2)#1#) * { * var cells = line.Split(';'); * var item = new DemandsFromCSV() * { * RealizationDate = cells[1], * NumberDoc = cells[2], * ProductNameReal = cells[3], * CustomerName = cells[4], * VolumeReal = cells[5], * SellPrice = cells[6] * }; * * list.Add(item); * Console.WriteLine(item.RealizationDate + " " + item.NumberDoc + " " + item.ProductNameReal + " " + item.CustomerName + " " + item.VolumeReal + " " + item.SellPrice); * } * * Console.WriteLine(list.Count); * * var listDemandsForYearAndMonth = list.Where(csv => csv.RealizationDate.Contains(year + "-" + month)); * * Console.WriteLine(listDemandsForYearAndMonth.Count()); * * /*using (StreamWriter fs = new StreamWriter(new FileStream(@"D:\user\MS\ms.txt", FileMode.Create, FileAccess.Write), Encoding.UTF8)) * { * //list.Where(csv => csv.RealizationDate.Contains("2017-06")); * foreach (var item in listMonth) * { * fs.WriteLine(item.RealizationDate + " " + item.NumberDoc + " " + item.ProductNameReal + " " + item.CustomerName + " " + item.VolumeReal + " " + item.SellPrice); * } * fs.Close(); * }#1# * * return listDemandsForYearAndMonth; * } * }*/ private static IEnumerable <DemandsFromCSV> ReadCsvRealization(DateTime fromTime, DateTime toTime) { var resultList = new List <DemandsFromCSV>(); // var lines = File.ReadAllLines(@"D:\user\sklad\all realization.csv", Encoding.GetEncoding("Windows-1251")); var lines = File.ReadAllLines(@"D:\user\sklad\all realization.csv", Encoding.Default); foreach (var line in lines) { var cells = line.Split(';'); var realizationDate = DateTime.Parse(cells[1]); if (realizationDate < fromTime || realizationDate > toTime) { continue; } var item = new DemandsFromCSV() { RealizationDate = cells[1], NumberDoc = cells[2], ProductNameReal = cells[3], CustomerName = cells[4], VolumeReal = cells[5], SellPrice = cells[6] }; resultList.Add(item); Console.WriteLine(item.RealizationDate + " " + item.NumberDoc + " " + item.ProductNameReal + " " + item.CustomerName + " " + item.VolumeReal + " " + item.SellPrice); } Console.WriteLine(resultList.Count); return(resultList); }
private static string CreateDemand(WebClient webClient, DemandsFromCSV item, string agentId) { string url = "https://online.moysklad.ru/api/remap/1.1/entity/demand"; var organizationId = "ad167a55-6df5-11e7-7a31-d0fd0005b555"; var storeId = "ad1729de-6df5-11e7-7a31-d0fd0005b557"; var demand = new Demand() { name = item.NumberDoc, moment = item.RealizationDate + " 15:00:00", organization = new Organization { meta = new Meta() { href = "https://online.moysklad.ru/api/remap/1.1/entity/organization/" + organizationId, type = "organization", mediaType = "application/json" } }, agent = new Agent { meta = new Meta() { href = "https://online.moysklad.ru/api/remap/1.1/entity/counterparty/" + agentId, type = "counterparty", mediaType = "application/json" } }, store = new Store { meta = new Meta { href = "https://online.moysklad.ru/api/remap/1.1/entity/store/" + storeId, type = "store", mediaType = "application/json" } } }; try { var myParameters = JsonConvert.SerializeObject(demand); webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json"); string htmlResult = webClient.UploadString(url, "POST", myParameters); var jsonString = JsonConvert.DeserializeObject <JsonDemand>(htmlResult); var id = jsonString.id; Console.WriteLine(" Отгрузка создана id = " + id); return(id); } catch (Exception e) { Console.WriteLine(" Отгрузка не создана!!!"); throw new Exception(); } }
private static void AddPosition(WebClient webClient, string demandId, string productId, DemandsFromCSV item) { Console.WriteLine("Добавляем позицию..."); Console.WriteLine("demandId: " + demandId); Console.WriteLine("productId: " + productId); string url = "https://online.moysklad.ru/api/remap/1.1/entity/demand/" + demandId + "/positions"; var position = new Position { quantity = decimal.Parse(item.VolumeReal) * 100, price = decimal.Parse(item.SellPrice) * 100, assortment = new Assortment() { meta = new Meta() { href = "https://online.moysklad.ru/api/remap/1.1/entity/product/" + productId, type = "product", mediaType = "application/json" } } }; try { var myParameters = JsonConvert.SerializeObject(position); webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json"); webClient.UploadString(url, "POST", myParameters); Console.WriteLine("Позиция добавлена"); } catch (Exception) { Console.WriteLine("!Позиция не добавлена!"); throw new Exception(); } }