private static void PerformStatisticsOnClosure() { try { // Read closure Dictionary <DateTime, CashRegisterClosure> closures = new Dictionary <DateTime, CashRegisterClosure>(); string path = ConfigurationManager.AppSettings["CashRegisterClosurePath"]; foreach (string filename in Directory.EnumerateFiles(path, "*.xml", SearchOption.TopDirectoryOnly)) { //Console.WriteLine($"Reading {Path.GetFileName(filename)}"); string[] parts1 = Path.GetFileNameWithoutExtension(filename).Split('_'); string[] date = parts1[1].Split('-'); int year = Convert.ToInt32(date[0]); int month = Convert.ToInt32(date[1]); int day = Convert.ToInt32(date[2]); string[] time = parts1[2].Split('-'); int hour = Convert.ToInt32(time[0]); int minute = Convert.ToInt32(time[1]); int second = Convert.ToInt32(time[2]); DateTime creationDate; if (hour < 3) { creationDate = new DateTime(year, month, day, 23, 59, 59).AddDays(-1); // previous day } else { creationDate = new DateTime(year, month, day, hour, minute, second); } string[] lines = File.ReadAllLines(filename); lines[0] = "<CashRegisterClosure xmlns:i=\"" + @"http://www.w3.org/2001/XMLSchema-instance" + "\">"; string cleanedXml = string.Join(Environment.NewLine, lines); //string xml = File.ReadAllText(filename, Encoding.UTF8); //string namespaceToRemove = "xmlns=\"" + "http://schemas.datacontract.org/2004/07/PPC.DataContracts" + "\""; //string cleanedXml = xml.Replace(namespaceToRemove, string.Empty); CashRegisterClosure closure = Deserialize <CashRegisterClosure>(cleanedXml); closures.Add(creationDate, closure); } Console.WriteLine($"{closures.Count} closure read and parsed"); // Read articles //closures.SelectMany(x => x.Value.Articles).Select(x => x.) List <Article> oldArticles = ReadArticles(@"c:\temp\ppc\articles.xml"); Console.WriteLine($"{oldArticles.Count} old articles read"); List <Article> newArticles = ReadArticles(@"c:\temp\PPC data from club\articles.xml"); Console.WriteLine($"{newArticles.Count} new articles read"); // Search closure article in old and new article list List <FullArticleWithCategoryAndDate> fullArticleWithCategoryList = new List <FullArticleWithCategoryAndDate>(); int totalArticleCount = 0; List <FullArticle> notFoundArticles = new List <FullArticle>(); foreach (KeyValuePair <DateTime, CashRegisterClosure> kv in closures) { foreach (FullArticle fullArticle in kv.Value.Articles) { Article newArticle = newArticles.FirstOrDefault(x => x.Guid == fullArticle.Guid) ?? newArticles.FirstOrDefault(x => string.Compare(x.Description, fullArticle.Description, StringComparison.OrdinalIgnoreCase) == 0); Article oldArticle = oldArticles.FirstOrDefault(x => x.Guid == fullArticle.Guid) ?? oldArticles.FirstOrDefault(x => string.Compare(x.Description, fullArticle.Description, StringComparison.OrdinalIgnoreCase) == 0); string category = newArticle?.Category ?? oldArticle?.Category; if (category == null) { string guid = fullArticle.Guid.ToString().ToLowerInvariant(); if (!UnknownArticles.TryGetValue(guid, out category)) { //Console.WriteLine($"Article not found: {fullArticle.Guid} {fullArticle.Description} {fullArticle.Price}"); notFoundArticles.Add(fullArticle); } } fullArticleWithCategoryList.Add(new FullArticleWithCategoryAndDate { FullArticle = fullArticle, SuperCategory = GetSuperCategory(category), Category = category, DateTime = kv.Key }); totalArticleCount++; } } Console.WriteLine($"Total: {totalArticleCount}. Not found: {notFoundArticles.Count}."); foreach (FullArticle article in notFoundArticles) { Console.WriteLine($"{article.Guid} {article.Ean} {article.Description} {article.Price}"); } List <string> categories = fullArticleWithCategoryList.Select(x => x.Category).Distinct().ToList(); Console.WriteLine("Categories: "); foreach (string category in categories) { Console.WriteLine(category); } // Amount by category var amountsByCategory = fullArticleWithCategoryList.GroupBy(x => x.Category).Select(x => new { category = x.Key, total = x.Sum(y => y.FullArticle.Quantity * y.FullArticle.Price) }); foreach (var amountByCategory in amountsByCategory) { Console.WriteLine($"{amountByCategory.category}: {amountByCategory.total}"); } // Amount by month and category var amountsByMonthAndCategory = fullArticleWithCategoryList.GroupBy(x => new { x.DateTime.Year, x.DateTime.Month, x.Category }).Select(x => new { year = x.Key.Year, month = x.Key.Month, category = x.Key.Category, total = x.Sum(y => y.FullArticle.Quantity * y.FullArticle.Price) }); Console.WriteLine("BY MONTH/YEAR/CATEGORY"); foreach (var amountByMonthAndCategory in amountsByMonthAndCategory.OrderBy(x => x.year).ThenBy(x => x.month).ThenBy(x => x.category)) { Console.WriteLine($"{amountByMonthAndCategory.month:D2}/{amountByMonthAndCategory.year} | {amountByMonthAndCategory.category}: {amountByMonthAndCategory.total}"); } // Amount by month and super category var amountsByMonthAndSuperCategory = fullArticleWithCategoryList.GroupBy(x => new { x.DateTime.Year, x.DateTime.Month, x.SuperCategory }).Select(x => new { year = x.Key.Year, month = x.Key.Month, superCategory = x.Key.SuperCategory, total = x.Sum(y => y.FullArticle.Quantity * y.FullArticle.Price) }); Console.WriteLine("BY MONTH/YEAR/SUPER CATEGORY"); foreach (var amountByMonthAndSuperCategory in amountsByMonthAndSuperCategory.OrderBy(x => x.year).ThenBy(x => x.month).ThenBy(x => x.superCategory)) { Console.WriteLine($"{amountByMonthAndSuperCategory.month:D2}/{amountByMonthAndSuperCategory.year} | {amountByMonthAndSuperCategory.superCategory}: {amountByMonthAndSuperCategory.total}EUR"); } } catch (Exception ex) { Console.WriteLine(ex); } }
public ArticlesViewModel(CashRegisterClosure cashRegisterClosure) { ClosureData = cashRegisterClosure; }