/// <summary> /// Gather all revenue date during a month/year combination. /// </summary> /// <remarks> /// Separate the incoming data string into the month and year component. /// Check if any live event took place during the month in question. /// If so, add their combined revenue to the data model. /// Search the financial gateways for membership renewals payments. /// Add any matches to the relevant gold, silver or bronze revenue totals in the model. /// Search the database for any accepted private session requested that had been paid for. /// Add any matches to the private fee count, and add the total revenue to the model. /// Search for any class cancellation fines settled during the month in question. /// Add the total of any fines to the viewmodel. /// Instantiate the model and return it. /// </remarks> /// <param name="selectedData">A <see cref="string"/> containing a numeric month and year</param> /// <returns></returns> public RevenueReportModel GetRevenueData(string selectedData) { int year = int.Parse(selectedData.Substring(0, 4)); int month = int.Parse(selectedData.Substring(5, 2)); RevenueReportModel model; //Create ViewModel //Add Data to Sending Model using (_db) { //Get Event Revenue var events = _db.LiveEvents .Where(e => e.EventDate.Month == month && e.EventDate.Year == year) .ToList(); float eventRevenueTotal = 0; foreach (var show in events) { eventRevenueTotal = eventRevenueTotal + show.EventRevenue; } //Get Membership Revenue DateTime startDate = new DateTime(year, month, 1); DateTime endDate = new DateTime(year, month, DateTime.DaysInMonth(year, month)); var bronzeRequest = new TransactionSearchRequest().SettledAt.Between(startDate, endDate).Amount.Is(40); var silverRequest = new TransactionSearchRequest().SettledAt.Between(startDate, endDate).Amount.Is(55); var goldRequest = new TransactionSearchRequest().SettledAt.Between(startDate, endDate).Amount.Is(70); ResourceCollection <Transaction> bResults = PaymentGateways.Gateway.Transaction.Search(bronzeRequest); var bFeesCollected = bResults.Ids.Count; var bTotal = bFeesCollected * 40; ResourceCollection <Transaction> sResults = PaymentGateways.Gateway.Transaction.Search(silverRequest); var sFeesCollected = sResults.Ids.Count; var sTotal = sFeesCollected * 55; ResourceCollection <Transaction> gResults = PaymentGateways.Gateway.Transaction.Search(goldRequest); var gFeesCollected = gResults.Ids.Count; var gTotal = gFeesCollected * 70; //Get Private Session Revenue var pFeesCollected = 0; foreach (var payment in _db.Payments.Where(p => p.PaymentDate.Month == month && p.PaymentDate.Year == year).ToList()) { if (payment.PaymentDescription.Equals("Private Session Booking Fee") && payment.PaymentSettled) { pFeesCollected++; } } var privateTotal = pFeesCollected * 30; //Get Fine Revenue var fineRequest = new TransactionSearchRequest().SettledAt.Between(startDate, endDate).Amount.Is(30).Refund.Is(1.5); ResourceCollection <Transaction> fineResults = PaymentGateways.Gateway.Transaction.Search(fineRequest); var finesCollected = fineResults.Ids.Count; double fineTotal = finesCollected * 1.5; model = new RevenueReportModel { BronzeFeesCollected = bTotal, CurrentDate = new DateTime(year, month, 1), Events = events.ToList(), FinesCollected = fineTotal, GoldFeesCollected = gTotal, NoBronzeMemberships = bFeesCollected, NoCancellationFines = finesCollected, NoGoldMemberships = gFeesCollected, NoPrivateSessions = pFeesCollected, NoSilverMemberships = sFeesCollected, PrivateFeesCollected = privateTotal, SilverFeesCollected = sTotal, TotalEventRevenue = eventRevenueTotal, TotalMembershipRevenue = bTotal + sTotal + gTotal, TotalOtherRevenue = privateTotal + fineTotal, TotalMonthRevenue = eventRevenueTotal + bTotal + sTotal + gTotal + privateTotal + fineTotal }; } return(model); }
public RevenueReportModel GetRevenueReport(DateTime dateFrom, DateTime dateTo, string createdBy) { var addMonth = dateTo.AddMonths(1); var orders = _shoppingContext.Orders.AsNoTracking().Where(w => (w.CreatedDateTime >= dateFrom && w.CreatedDateTime < addMonth) && !w.CanceledBy.HasValue).OrderBy(w => w.CreatedDateTime).ToList(); var invoices = _shoppingContext.Invoices.AsNoTracking().Where(w => w.ApprovedBy.HasValue && !w.OrderId.HasValue && (w.ApprovedDateTime >= dateFrom && w.ApprovedDateTime < addMonth)) .OrderBy(w => w.CreatedDateTime).ToList(); var model = new RevenueReportModel() { TimeFrom = dateFrom.ToString("MM/yyyy"), TimeTo = dateTo.ToString("MM/yyyy"), CreatedBy = createdBy }; foreach (var item in orders) { foreach (var orderDetail in item.OrderDetails) { var existedItem = model.Details.FirstOrDefault(w => item.CreatedDateTime.ToString("MM/yyyy").Equals(w.Time, StringComparison.OrdinalIgnoreCase)); if (existedItem != null) { existedItem.Quantity += orderDetail.Quantity; existedItem.Amount += orderDetail.Quantity * orderDetail.UnitPrice; } else { model.Details.Add(new RevenueDetailReportModel() { Quantity = orderDetail.Quantity, Amount = orderDetail.Quantity * orderDetail.UnitPrice, CategoryId = orderDetail.Product.ProductCategoryId, CategoryName = orderDetail.Product.ProductCategory.Name, Time = item.CreatedDateTime.ToString("MM/yyyy") }); } } } foreach (var item in invoices) { foreach (var detail in item.InvoiceDetails) { var existedItem = model.Details.FirstOrDefault(w => item.CreatedDateTime.ToString("MM/yyyy").Equals(w.Time, StringComparison.OrdinalIgnoreCase)); if (existedItem != null) { existedItem.Quantity += detail.Quantity; existedItem.Amount += detail.Quantity * detail.UnitPrice; } else { model.Details.Add(new RevenueDetailReportModel() { Quantity = detail.Quantity, Amount = detail.Quantity * detail.UnitPrice, CategoryId = detail.Product.ProductCategoryId, CategoryName = detail.Product.ProductCategory.Name, Time = item.CreatedDateTime.ToString("MM/yyyy") }); } } } model.Details = model.Details.OrderBy(p => DateTime.Parse(p.Time)).ToList(); return(model); }