public ReportGraph(ReportGraphType type, DateTime from, DateTime to) { Type = type; From = new DateTime(from.Year, from.Month, from.Day, 0, 0, 0); var dif = to - from; if (dif.TotalHours <= 24) { _Step = 1; TimeFormat = "HH:mm"; } else if (dif.TotalDays <= 31) { _Step = 24; TimeFormat = "dd/MM"; } else if (dif.TotalDays <= 7 * 24) { _Step = 7 * 24; TimeFormat = "dd/MM"; } else { _Step = 0; TimeFormat = "MM/yyyy"; } Revenues = new List <GraphSalePoint>(); }
public static ReportGraph Create(Report report, ReportGraphType type = ReportGraphType.Sale) { var graph = new ReportGraph(type, report.Filter.From.Value, report.Filter.To.Value); graph.Current = graph.From; graph.Warehouses = report.Warehouses; var count = 0; while (true) { var to = graph.Current.AddHours(graph.Step); var total = 0m; foreach (var wh in report.Warehouses) { var paidOrders = report.PaidOrders.Where(i => i.WarehouseID == wh.ID && i.SubmitDate >= graph.Current && i.SubmitDate <= to).Sum(i => i.Paid); var incomes = report.Incomes.Where(i => i.WarehouseID == wh.ID && i.SubmitDate >= graph.Current && i.SubmitDate <= to).Sum(i => i.Amount); var outcomes = report.Outcomes.Where(i => i.WarehouseID == wh.ID && i.SubmitDate >= graph.Current && i.SubmitDate <= to).Sum(i => i.Amount); var employees = report.Employees.Where(i => i.WarehouseID == wh.ID); var salaries = report.Salaries.Where(i => employees.FirstOrDefault(e => e.ID == i.EmployeeID) != null && i.Month.HasValue && i.Month.Value >= graph.Current && i.Month.Value < to).Sum(i => i.CalculatedTotal); var revenue = 0m; switch (type) { case ReportGraphType.Revenue: revenue = (paidOrders + incomes) * 0.65m - outcomes - salaries; break; case ReportGraphType.Sale: revenue = paidOrders; break; case ReportGraphType.SaleRelative: var store = report.Stores.FirstOrDefault(i => i.WarehouseID == wh.ID); revenue = store != null && store.SalePoint > 0 ? Math.Round(paidOrders / store.SalePoint * 100, 2) : 0m; break; default: break; } graph.Revenues.Add(new GraphSalePoint(graph.Current, revenue, wh.ID)); total += revenue; } if (!report.Filter.WarehouseID.HasValue) { graph.Revenues.Add(new GraphSalePoint(graph.Current, total)); } graph.Current = graph.Current.AddHours(graph.Step); count++; if (graph.Current >= report.Filter.To.Value || count == 31) { break; } } graph.Count = count; graph.Max = graph.Revenues.Max(i => i.Value); return(graph); }