public static YearlyIncomeModel GetRevenueByYear(int year, AppContext context) { ModelFactory factory = new ModelFactory(); YearlyIncomeModel model = new YearlyIncomeModel(); Repository <InvoiceItem> invoiceRepository = new Repository <InvoiceItem>(context); ItemModel iModel = new ItemModel(); double total = 0; var list = invoiceRepository.context.InvoiceItems.ToList(); //Go through a list, and add the price of every sold item foreach (var item in list) { if (item.Invoice.Date.Year == year && item.Invoice.Status == Status.Paid) { total += item.Price * item.Quantity; iModel = factory.Create(item.Item); iModel.Quantity = item.Quantity; iModel.UnitPrice = item.Price; model.Items.Add(iModel); } } model.Total = total; model.year = year; return(model); }
public IHttpActionResult GetByYear(int id) { try { YearlyIncomeModel yearlyIncome = Helpers.YearlyIncomeHelper.GetRevenueByYear(id, Repository.HomeContext()); if (yearlyIncome != null) { return(Ok(yearlyIncome)); } return(NotFound()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public static YearlyIncomeModel GetRevenueByYear(int year, AppContext context) { var factory = new ModelFactory(); var model = new YearlyIncomeModel(); var invoiceRepository = new Repository <InvoiceItem>(context); double total = 0; var list = invoiceRepository.context.InvoiceItems.Where(i => i.Invoice.Date.Year == year).ToList(); foreach (var item in list) { if (item.Invoice.Status != Status.Paid) { continue; } total += item.Price * item.Quantity; var iModel = factory.Create(item.Item); iModel.Quantity = item.Quantity; iModel.UnitPrice = item.Price; model.Items.Add(iModel); } model.Total = total; model.year = year; return(model); }