From aeef35b57c1ecfb224e9f0e7a7c3cb2b56e6829f Mon Sep 17 00:00:00 2001 From: Bobbie Hodgetts Date: Mon, 8 Jun 2020 15:13:38 +0100 Subject: [PATCH] wip --- .../Logic/Sku/Price/FbaPricing.cs | 160 ++++++++++-------- 1 file changed, 86 insertions(+), 74 deletions(-) diff --git a/src/bnhtrade.Core/Logic/Sku/Price/FbaPricing.cs b/src/bnhtrade.Core/Logic/Sku/Price/FbaPricing.cs index c8e8ede..3081a92 100644 --- a/src/bnhtrade.Core/Logic/Sku/Price/FbaPricing.cs +++ b/src/bnhtrade.Core/Logic/Sku/Price/FbaPricing.cs @@ -190,87 +190,18 @@ namespace bnhtrade.Core.Logic.Sku.Price } // finish loop - // validate and save values to database - var validate = new Core.Logic.Validate.SkuPriceInfo(); - if (!validate.IsValidDatabaseCreate(crDictionary.Values.ToList())) - { - err += "Database object create validation failed"; - log.LogError(err, validate.ValidationResultListToString()); - throw new Exception(err); - } + // save values to database + SaveToDatabase(crDictionary); - new Data.Database.Sku.Price.CreatePricingDetail(sqlConnectionString).Executue(crDictionary.Values.ToList()); + // upload to amazon + UploadToAmazon(crDictionary); - // create and upload inventory loader file to amazon - var exportList = new List(); - foreach (var item in crDictionary.Values) - { - var listItem = new Model.Export.AmazonIventoryLoaderFile(); - listItem.Sku = item.SkuNumber; - listItem.MinimumAllowedPrice = item.MinPrice; - listItem.MaximumAllowedPrice = item.MaxPrice; - listItem.Price = item.MaxPrice; - listItem.SetFulfillmentCenterId(true); - exportList.Add(listItem); - } - - // validate - var vaildateInvLoader = new Validate.AmazonIventoryLoaderFile(); - if (!vaildateInvLoader.IsValidFbaPricing(exportList)) - { - err += "Inventory loader object validation failed"; - log.LogError(err, vaildateInvLoader.ValidationResultListToString()); - throw new Exception(err); - } - - // create file stream - var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.CurrentCulture); - config.Delimiter = "\t"; - config.Encoding = Encoding.UTF8; - - var stream = new MemoryStream(); - using (var writer = new StreamWriter(stream, Encoding.UTF8)) - using (var csv = new CsvWriter(writer, config)) - { - csv.WriteRecords(exportList); - } - - // submit file to database and amazon mws - var submit = new Logic.Export.AmazonSubmitFile(sqlConnectionString); - - return; - - submit.SubmitInventoryLoader(stream); + return; // remove after testing scope.Complete(); } } - private bool OkayToReprice(DateTime lastPriceUpdate) - { - if (lastPriceUpdate == default(DateTime)) - { - err += "Invalid, datetime is default."; - log.LogError(err); - throw new Exception(err); - } - - bool update = false; - lastPriceUpdate = new DateTime(lastPriceUpdate.Year, lastPriceUpdate.Month, lastPriceUpdate.Day); - DateTime today = new DateTime(newTimeStamp.Year, newTimeStamp.Month, newTimeStamp.Day); - - // will only update once on tue, wed or thurs each week. - if (today.DayOfWeek == DayOfWeek.Tuesday || today.DayOfWeek == DayOfWeek.Wednesday || today.DayOfWeek == DayOfWeek.Thursday) - { - if (today > lastPriceUpdate.AddDays(3)) - { - update = true; - } - } - - return update; - } - /// /// Get the minimum sale price to break even. /// @@ -382,6 +313,44 @@ namespace bnhtrade.Core.Logic.Sku.Price } } + private bool OkayToReprice(DateTime lastPriceUpdate) + { + if (lastPriceUpdate == default(DateTime)) + { + err += "Invalid, datetime is default."; + log.LogError(err); + throw new Exception(err); + } + + bool update = false; + lastPriceUpdate = new DateTime(lastPriceUpdate.Year, lastPriceUpdate.Month, lastPriceUpdate.Day); + DateTime today = new DateTime(newTimeStamp.Year, newTimeStamp.Month, newTimeStamp.Day); + + // will only update once on tue, wed or thurs each week. + if (today.DayOfWeek == DayOfWeek.Tuesday || today.DayOfWeek == DayOfWeek.Wednesday || today.DayOfWeek == DayOfWeek.Thursday) + { + if (today > lastPriceUpdate.AddDays(3)) + { + update = true; + } + } + + return update; + } + + private void SaveToDatabase(Dictionary crDictionary) + { + var validate = new Core.Logic.Validate.SkuPriceInfo(); + if (!validate.IsValidDatabaseCreate(crDictionary.Values.ToList())) + { + err += "Database object create validation failed"; + log.LogError(err, validate.ValidationResultListToString()); + throw new Exception(err); + } + + new Data.Database.Sku.Price.CreatePricingDetail(sqlConnectionString).Executue(crDictionary.Values.ToList()); + } + /// /// Before running reprice update, this method ensures that the relevant data needed is up to date. /// @@ -396,5 +365,48 @@ namespace bnhtrade.Core.Logic.Sku.Price // check last amazon sku fees updates } + + private void UploadToAmazon(Dictionary crDictionary) + { + var exportList = new List(); + foreach (var item in crDictionary.Values) + { + var listItem = new Model.Export.AmazonIventoryLoaderFile(); + listItem.Sku = item.SkuNumber; + listItem.MinimumAllowedPrice = item.MinPrice; + listItem.MaximumAllowedPrice = item.MaxPrice; + listItem.Price = item.MaxPrice; + listItem.SetFulfillmentCenterId(true); + exportList.Add(listItem); + } + + // validate + var vaildateInvLoader = new Validate.AmazonIventoryLoaderFile(); + if (!vaildateInvLoader.IsValidFbaPricing(exportList)) + { + err += "Inventory loader object validation failed"; + log.LogError(err, vaildateInvLoader.ValidationResultListToString()); + throw new Exception(err); + } + + // create file stream + var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.CurrentCulture); + config.Delimiter = "\t"; + config.Encoding = Encoding.UTF8; + + var stream = new MemoryStream(); + using (var writer = new StreamWriter(stream, Encoding.UTF8)) + using (var csv = new CsvWriter(writer, config)) + { + csv.WriteRecords(exportList); + } + + // submit file to database and amazon mws + var submit = new Logic.Export.AmazonSubmitFile(sqlConnectionString); + + return; // remove after testing + + submit.SubmitInventoryLoader(stream); + } } }