diff --git a/Mapp.DataAccess/StockQuantityUpdater.cs b/Mapp.DataAccess/StockQuantityUpdater.cs index 8b823bb..97500c3 100644 --- a/Mapp.DataAccess/StockQuantityUpdater.cs +++ b/Mapp.DataAccess/StockQuantityUpdater.cs @@ -5,7 +5,9 @@ using System.Text.RegularExpressions; using System.Xml; using System; +using System.Linq; using System.Threading.Tasks; +using Shmap.CommonServices; namespace Shmap.DataAccess; @@ -17,27 +19,41 @@ public interface IStockQuantityUpdater public class StockQuantityUpdater : IStockQuantityUpdater { private readonly IJsonManager _jsonManager; + private readonly IDialogService _dialogService; private readonly IEnumerable _sourceDefinitions; - public StockQuantityUpdater(IJsonManager jsonManager) + public StockQuantityUpdater(IJsonManager jsonManager, IDialogService dialogService) { _jsonManager = jsonManager; + _dialogService = dialogService; _sourceDefinitions = _jsonManager.LoadStockQuantityUpdaterConfigs(); } public async Task> ConvertWarehouseData() { - var httpClient = new HttpClient(); - var stockData = new List(); + var stockDataTotal = new List(); + + Dictionary statistics = new Dictionary(); + foreach (var source in _sourceDefinitions) { var stream = await (await httpClient.GetAsync(source.Url)).Content.ReadAsStreamAsync(); - stockData.AddRange(ExtractStockData(stream, source)); + var stockData = ExtractStockData(stream, source); + stockDataTotal.AddRange(stockData); + + statistics.Add(source.Url, stockData.Count()); } - return stockData; + string msg = "Konvertovano:\n"; + foreach (var (url, count) in statistics) + { + msg += $"{count} zaznamu z: {url} \n"; + } + _dialogService.ShowMessage(msg); + + return stockDataTotal; } private IEnumerable ExtractStockData(Stream stream, StockDataXmlSourceDefinition source) diff --git a/Mapp.UI/Bootstrapper.cs b/Mapp.UI/Bootstrapper.cs index d73bc3a..f9f2bb0 100644 --- a/Mapp.UI/Bootstrapper.cs +++ b/Mapp.UI/Bootstrapper.cs @@ -49,6 +49,7 @@ private void ConfigureContainer() Container.RegisterTypeAsSingleton(); Container.RegisterTypeAsSingleton(); Container.RegisterTypeAsSingleton(); + Container.RegisterTypeAsSingleton(); Container.RegisterType(); diff --git a/Mapp.UI/BrowserService.cs b/Mapp.UI/BrowserService.cs new file mode 100644 index 0000000..9f0c363 --- /dev/null +++ b/Mapp.UI/BrowserService.cs @@ -0,0 +1,18 @@ +using System.Diagnostics; +using System; + + +namespace Shmap.UI; + +public interface IBrowserService +{ + void OpenBrowserOnUrl(string url); +} + +public class BrowserService : IBrowserService +{ + public void OpenBrowserOnUrl(string url) + { + Process.Start(new ProcessStartInfo(url.ToString()) { UseShellExecute = true }); + } +} \ No newline at end of file diff --git a/Mapp.UI/ViewModels/InvoiceItemViewModel.cs b/Mapp.UI/ViewModels/InvoiceItemViewModel.cs index 0745cf0..6382451 100644 --- a/Mapp.UI/ViewModels/InvoiceItemViewModel.cs +++ b/Mapp.UI/ViewModels/InvoiceItemViewModel.cs @@ -7,6 +7,7 @@ using GalaSoft.MvvmLight.CommandWpf; using Shmap.CommonServices; using Shmap.DataAccess; +using Shmap.UI; using NLog.LayoutRenderers; namespace Shmap.ViewModels @@ -14,6 +15,7 @@ namespace Shmap.ViewModels public class InvoiceItemViewModel : ViewModelWithErrorValidationBase { private readonly InvoiceItemBase _model; + private readonly IBrowserService _browserService; private readonly IAutocompleteData _autocompleteData; private string _amazonProductName; private string _warehouseProductCode; @@ -69,7 +71,7 @@ public uint? PackQuantityMultiplier } } - public InvoiceItemViewModel(InvoiceItemBase model, IAutocompleteData autocompleteData) + public InvoiceItemViewModel(InvoiceItemBase model, IAutocompleteData autocompleteData, IBrowserService browserService) { AmazonNumber = model.ParentInvoice.VariableSymbolFull; SalesChannel = model.ParentInvoice.SalesChannel; @@ -96,6 +98,7 @@ public InvoiceItemViewModel(InvoiceItemBase model, IAutocompleteData autocomplet // temp _model = model; + _browserService = browserService; } private void GoToInvoicePage() // TODO into separate provider + tests @@ -112,7 +115,7 @@ private void GoToInvoicePage() // TODO into separate provider + tests ?? defaultAmazonCentralUrl; url += AmazonNumber; - Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); + _browserService.OpenBrowserOnUrl(url); } private bool ValidateProductCode() diff --git a/Mapp.UI/ViewModels/MainWindowViewModel.cs b/Mapp.UI/ViewModels/MainWindowViewModel.cs index 0697fde..40e1f1d 100644 --- a/Mapp.UI/ViewModels/MainWindowViewModel.cs +++ b/Mapp.UI/ViewModels/MainWindowViewModel.cs @@ -9,12 +9,12 @@ using System.Net.Http; using System.Net.Mail; using System.Reflection; -using System.Security.Policy; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; +using System.Windows.Navigation; using System.Xml; using Castle.Core.Internal; using GalaSoft.MvvmLight.CommandWpf; @@ -40,6 +40,7 @@ internal class MainWindowViewModel : ViewModelWithErrorValidationBase, IMainWind private readonly ITransactionsReader _transactionsReader; private readonly IGpcGenerator _gpcGenerator; private readonly IAutocompleteData _autocompleteData; + private readonly IBrowserService _browserService; private readonly IStockQuantityUpdater _quantityUpdater; private readonly IDialogService _dialogService; private int _windowWidth; @@ -57,6 +58,7 @@ internal class MainWindowViewModel : ViewModelWithErrorValidationBase, IMainWind public RelayCommand ExportConvertedAmazonInvoicesCommand { get; } public RelayCommand ConvertTransactionsCommand { get; } public RelayCommand ConvertWarehouseDataCommand { get; set; } + public RelayCommand OpenAmazonMarketPlaceUriCommand { get; set; } public ObservableCollection InvoiceItems { get; } = new(); // TODO make private field? @@ -206,7 +208,7 @@ public MainWindowViewModel() // Design-time ctor var dataMock = Mock.Of(); foreach (var item in items) { - InvoiceItems.Add(new InvoiceItemViewModel(item, dataMock)); // TODO create bindable collection with AddRange method + InvoiceItems.Add(new InvoiceItemViewModel(item, dataMock, new BrowserService())); // TODO create bindable collection with AddRange method } Invoices.Add(new InvoiceViewModel(invoice, dataMock)); } @@ -217,6 +219,7 @@ public MainWindowViewModel(IConfigProvider configProvider, ITransactionsReader transactionsReader, IGpcGenerator gpcGenerator, IAutocompleteData autocompleteData, + IBrowserService browserService, IStockQuantityUpdater quantityUpdater, IDialogService dialogService) { @@ -226,6 +229,7 @@ public MainWindowViewModel(IConfigProvider configProvider, _transactionsReader = transactionsReader; _gpcGenerator = gpcGenerator; _autocompleteData = autocompleteData; + _browserService = browserService; _quantityUpdater = quantityUpdater; _dialogService = dialogService; @@ -233,6 +237,7 @@ public MainWindowViewModel(IConfigProvider configProvider, ExportConvertedAmazonInvoicesCommand = new RelayCommand(ExportConvertedAmazonInvoices, ExportConvertedAmazonInvoicesCanExecute); ConvertTransactionsCommand = new RelayCommand(ConvertTransactions, () => true); ConvertWarehouseDataCommand = new RelayCommand(ConvertWarehouseData, () => true); + OpenAmazonMarketPlaceUriCommand = new RelayCommand(OpenAmazonMarketPlaceUri); InvoiceItemsCollectionView = InitializeCollectionView(); @@ -251,43 +256,17 @@ public MainWindowViewModel(IConfigProvider configProvider, } + private void OpenAmazonMarketPlaceUri(string uri) + { + _browserService.OpenBrowserOnUrl(uri); + } + + private async void ConvertWarehouseData() { IsReadyForProcessing = false; try { - //StockDataXmlSourceDefinition[] sources = - //{ - // new() - // { - // Url = "https://www.rappa.cz/export/vo.xml", - // ItemNodeName = "SHOPITEM", - // SkuNodeParsingOptions = new[] - // { - // new ValueParsingOption("EAN", null), - // }, - // StockQuantityNodeParsingOptions = new[] - // { - // new ValueParsingOption("STOCK", null), - // }, - // }, - // new() - // { - // Url = "https://en.bushman.eu/content/feeds/uQ5TueFNQh_expando_4.xml", - // ItemNodeName = "item", - // SkuNodeParsingOptions = new[] - // { - // new ValueParsingOption("g:gtin", null), - // new ValueParsingOption("g:sku_with_ean", @"\d{13}"), - // }, - // StockQuantityNodeParsingOptions = new[] - // { - // new ValueParsingOption("g:quantity", null), - // }, - // } - //}; - - var stockData = await _quantityUpdater.ConvertWarehouseData(); var columnNamesLine = "sku\tprice\tminimum-seller-allowed-price\tmaximum-seller-allowed-price\tquantity\thandling-time\tfulfillment-channel"; @@ -380,7 +359,7 @@ private void SelectAmazonInvoices() foreach (var invoiceItem in invoices.SelectMany(di => di.InvoiceItems)) { - InvoiceItems.Add(new InvoiceItemViewModel(invoiceItem, _autocompleteData)); + InvoiceItems.Add(new InvoiceItemViewModel(invoiceItem, _autocompleteData, _browserService)); } foreach (var invoice in invoices) @@ -395,8 +374,9 @@ private void ExportConvertedAmazonInvoices() if (string.IsNullOrWhiteSpace(fileName)) return; var invoices = Invoices.Select(i => i.ExportModel()).ToList(); - var invoiceItems = InvoiceItems.Select(i => i.ExportModel()).ToList(); + // TODO how to avoid need for calling ToList (due to laziness of linq)? + var invoiceItems = InvoiceItems.Select(i => i.ExportModel()).ToList(); if (!invoices.Any()) { diff --git a/Mapp.UI/Views/MainWindow.xaml b/Mapp.UI/Views/MainWindow.xaml index 9c1d6b8..f1bcb54 100644 --- a/Mapp.UI/Views/MainWindow.xaml +++ b/Mapp.UI/Views/MainWindow.xaml @@ -137,7 +137,7 @@ - +