private async void btnGenerateDiagram_Click(object sender, EventArgs e) { labelErrorDiagram.Visible = false; chartMovement.Series.Clear(); int diagram; int temp1; if (DataHolder.UserCulture.TwoLetterISOLanguageName == "bg") { Enums.DiagramTypeBg diagramType; Enum.TryParse(cbDiagramType.SelectedValue.ToString(), out diagramType); diagram = (int)diagramType; Enums.ItemTypesBg temp; Enum.TryParse(cbItemTypesDiagram.SelectedValue.ToString(), out temp); temp1 = (int)temp; } else { Enums.DiagramType diagramType; Enum.TryParse(cbDiagramType.SelectedValue.ToString(), out diagramType); diagram = (int)diagramType; Enums.ItemTypes temp; Enum.TryParse(cbItemTypesDiagram.SelectedValue.ToString(), out temp); temp1 = (int)temp; } DiagramDTO dto = new DiagramDTO { ReportType = diagram, FromDate = dtFromDiagram.Value, ToDate = dtToDiagram.Value, IsType = cbItemTypeDiagram.Checked, ItemType = temp1 }; List <ReportMovement> result = await SAReports.PostGenerateDiagramMovement(dto); var counter = result.SelectMany(row => row.QuantityesList).Aggregate(0, (current, n) => current + n.Value); // check if everywhere is 0(empty result) if (counter == 0) { labelErrorDiagram.Visible = true; labelErrorDiagram.Text = DataHolder.GetString("no_results_found"); } else { List <ReportMovement> parsedResults = new List <ReportMovement>(); if (!cbItemTypeDiagram.Checked) { foreach (ReportMovement row in result) { if (DataHolder.UserCulture.TwoLetterISOLanguageName == "bg") { int value; int.TryParse(row.Name, out value); Enums.ItemTypesBg types = (Enums.ItemTypesBg)value; ReportMovement element = new ReportMovement(); element.Name = types.ToString(); element.QuantityesList = row.QuantityesList; parsedResults.Add(element); } else { int value; int.TryParse(row.Name, out value); Enums.ItemTypes types = (Enums.ItemTypes)value; ReportMovement element = new ReportMovement(); element.Name = types.ToString(); element.QuantityesList = row.QuantityesList; parsedResults.Add(element); } } foreach (var row in parsedResults) { chartMovement.Series.Add(row.Name); foreach (var rowvalue in row.QuantityesList) { chartMovement.Series[row.Name].Points.AddXY(rowvalue.Key, rowvalue.Value); } } } else { foreach (var row in result) { chartMovement.Series.Add(row.Name); foreach (var rowvalue in row.QuantityesList) { chartMovement.Series[row.Name].Points.AddXY(rowvalue.Key, rowvalue.Value); } } } } }
internal static List <ReportMovement> GenerateDiagramMovement(DiagramDTO dto) { DatabaseEntities entities = new DatabaseEntities(); List <ReportMovement> results = new List <ReportMovement>(); if (dto.ToDate <= dto.FromDate) { return(new List <ReportMovement>()); } if (!dto.IsType) { foreach (var type in entities.ItemTypesDDS) { ReportMovement temp = new ReportMovement(); temp.Name = type.Type.ToString(); temp.QuantityesList = new Dictionary <DateTime, int>(); for (var dt = dto.FromDate.Date; dt <= dto.ToDate.Date; dt = dt.AddDays(1)) { temp.QuantityesList.Add(dt, 0); } results.Add(temp); } } else { List <Item> allItems = entities.Items.Where(p => p.Type.Value == dto.ItemType).ToList(); foreach (var item in allItems) { ReportMovement temp = new ReportMovement(); temp.Name = item.Name; temp.QuantityesList = new Dictionary <DateTime, int>(); for (var dt = dto.FromDate.Date; dt <= dto.ToDate.Date; dt = dt.AddDays(1)) { temp.QuantityesList.Add(dt, 0); } results.Add(temp); } } if (dto.ReportType == 0) { List <Purchase> dbSet = new List <Purchase>(entities.Purchases.Where(p => p.Date >= dto.FromDate && p.Date <= dto.ToDate)); foreach (Purchase purchase in dbSet) { if (!dto.IsType) // for all { foreach (var item in purchase.PurchasedItems) { Item dbItem = entities.Items.FirstOrDefault(p => p.ItemId == item.ItemId); ReportMovement element = results.FirstOrDefault(p => p.Name == dbItem.Type.Value.ToString()); if (element != null && dbItem != null && element.QuantityesList.ContainsKey(purchase.Date.Date)) { element.QuantityesList[purchase.Date.Date] = element.QuantityesList[purchase.Date.Date] + item.Quantity; } else { element.QuantityesList[purchase.Date.Date] = element.QuantityesList[purchase.Date.Date] + item.Quantity; } } } else // specific type { foreach (var item in purchase.PurchasedItems) { Item dbItem = entities.Items.FirstOrDefault(p => p.ItemId == item.ItemId); ReportMovement element = results.FirstOrDefault(p => p.Name == dbItem.Name); if (dbItem != null && element != null && dbItem.Type == dto.ItemType) { if (element.QuantityesList.ContainsKey(purchase.Date.Date)) { element.QuantityesList[purchase.Date.Date] = element.QuantityesList[purchase.Date.Date] + item.Quantity; } else { element.QuantityesList[purchase.Date.Date] = element.QuantityesList[purchase.Date.Date] + item.Quantity; } } } } } } else if (dto.ReportType == 1) { List <Sale> dbSet = new List <Sale>(entities.Sales.Where(p => p.Date >= dto.FromDate && p.Date <= dto.ToDate)); foreach (Sale sale in dbSet) { if (!dto.IsType) // for all { foreach (var item in sale.SoldItems) { Item dbItem = entities.Items.FirstOrDefault(p => p.ItemId == item.ItemId); ReportMovement element = results.FirstOrDefault(p => p.Name == dbItem.Type.Value.ToString()); if (element != null && dbItem != null && element.QuantityesList.ContainsKey(sale.Date.Date)) { element.QuantityesList[sale.Date.Date] = element.QuantityesList[sale.Date.Date] + item.Quantity; } else { element.QuantityesList[sale.Date.Date] = element.QuantityesList[sale.Date.Date] + item.Quantity; } } } else // specific type { foreach (var item in sale.SoldItems) { Item dbItem = entities.Items.FirstOrDefault(p => p.ItemId == item.ItemId); ReportMovement element = results.FirstOrDefault(p => p.Name == dbItem.Name); if (dbItem != null && element != null && dbItem.Type == dto.ItemType) { if (element.QuantityesList.ContainsKey(sale.Date.Date)) { element.QuantityesList[sale.Date.Date] = element.QuantityesList[sale.Date.Date] + item.Quantity; } else { element.QuantityesList[sale.Date.Date] = element.QuantityesList[sale.Date.Date] + item.Quantity; } } } } } } else { return(new List <ReportMovement>()); } return(results); }