// GET: Sales public ActionResult Index(DateTime?salesBeginDate = null, DateTime?salesEndDate = null) { var salesList = _db.Sales.ToList(); if (salesBeginDate != null && salesEndDate != null) { salesList = (from p in salesList where p.Sales_Date > salesBeginDate && p.Sales_Date < salesEndDate select p).ToList(); } List <SalesDetailView> salesDetailList = new List <SalesDetailView>(); foreach (Sale sold in salesList) { SalesDetailView salesDetail = new SalesDetailView(); salesDetail.Product = sold.Product1; salesDetail.Customer = sold.Customer1; salesDetail.Salesperson = sold.Salesperson1; salesDetail.Sales_Date = sold.Sales_Date; //Check for discount on this product for the date of sale var discount = (from p in _db.Discounts where p.Product == sold.Product where p.Begin_Date <sold.Sales_Date && p.End_Date> sold.Sales_Date select p).FirstOrDefault(); if (discount != null) { decimal actualSalePrice = sold.Product1.Sale_Price * (1 - discount.Discount_Percentage) ?? 0; salesDetail.Sale_Price = actualSalePrice; salesDetail.Comission = actualSalePrice * sold.Product1.Comission ?? 0; } else { salesDetail.Sale_Price = sold.Product1.Sale_Price ?? 0; salesDetail.Comission = sold.Product1.Sale_Price * sold.Product1.Comission ?? 0; } salesDetailList.Add(salesDetail); } //return View(_db.Sales.ToList()); return(View(salesDetailList)); }
// GET: Report public ActionResult Index(string quarterNames = null) { ReportQuarter reportInfo = new ReportQuarter(); reportInfo.id = 1; reportInfo.quarterNames = new List <string>(); reportInfo.reportData = new List <CommissionReport>(); DateTime beginDate = new DateTime(); DateTime endDate = new DateTime(); if (quarterNames == null) { // Default to current quarter var currentDate = DateTime.Now; if (currentDate.Month < 4) { beginDate = new DateTime(currentDate.Year, 1, 1); endDate = new DateTime(currentDate.Year, 3, 31); } else if (currentDate.Month < 7) { beginDate = new DateTime(currentDate.Year, 4, 1); endDate = new DateTime(currentDate.Year, 6, 30); } else if (currentDate.Month < 10) { beginDate = new DateTime(currentDate.Year, 6, 1); endDate = new DateTime(currentDate.Year, 9, 30); } else { beginDate = new DateTime(currentDate.Year, 10, 1); endDate = new DateTime(currentDate.Year, 12, 31); } } else { // Get dates for selected quarter int quarter = Int16.Parse(quarterNames.Substring(1, 2)); int year = Int16.Parse(quarterNames.Substring(2, 5)); switch (quarter) { case 1: beginDate = new DateTime(year, 1, 1); endDate = new DateTime(year, 3, 31); break; case 2: beginDate = new DateTime(year, 4, 1); endDate = new DateTime(year, 6, 30); break; case 3: beginDate = new DateTime(year, 7, 1); endDate = new DateTime(year, 9, 30); break; case 4: beginDate = new DateTime(year, 10, 1); endDate = new DateTime(year, 12, 31); break; } } List <CommissionReport> reportData = new List <CommissionReport>(); var activeSalespeople = (from p in _db.Sales where p.Sales_Date >= beginDate && p.Sales_Date <= endDate select p.Salesperson).Distinct().ToList(); foreach (int agentId in activeSalespeople) { CommissionReport salespersonReport = new CommissionReport(); Salesperson salesAgent = (from p in _db.Salespersons where p.Id == agentId select p).FirstOrDefault(); salespersonReport.Name = salesAgent.First_Name + " " + salesAgent.Last_Name; List <Sale> agentSales = (from p in _db.Sales where p.Sales_Date >= beginDate && p.Sales_Date <= endDate && p.Salesperson == agentId select p).ToList(); salespersonReport.bikesSold = agentSales.Count; List <SalesDetailView> agentSalesDetail = new List <SalesDetailView>(); foreach (Sale sold in agentSales) { SalesDetailView salesDetail = new SalesDetailView(); //Check for discount on this product for the date of sale var discount = (from p in _db.Discounts where p.Product == sold.Product where p.Begin_Date <sold.Sales_Date && p.End_Date> sold.Sales_Date select p).FirstOrDefault(); if (discount != null) { decimal actualSalePrice = sold.Product1.Sale_Price * (1 - discount.Discount_Percentage) ?? 0; salesDetail.Sale_Price = actualSalePrice; salesDetail.Comission = actualSalePrice * sold.Product1.Comission ?? 0; } else { salesDetail.Sale_Price = sold.Product1.Sale_Price ?? 0; salesDetail.Comission = sold.Product1.Sale_Price * sold.Product1.Comission ?? 0; } agentSalesDetail.Add(salesDetail); } salespersonReport.totalSales = agentSalesDetail.Sum(x => x.Sale_Price); salespersonReport.totalComissions = agentSalesDetail.Sum(x => x.Comission); reportData.Add(salespersonReport); } reportInfo.reportData = reportData; DateTime firstSale = (from p in _db.Sales orderby p.Sales_Date ascending select p.Sales_Date).First(); DateTime currentYear = DateTime.Now; for (var i = firstSale.Year; i <= currentYear.Year; i++) { int startMonth = 1; if (i == firstSale.Year) { startMonth = firstSale.Month; } int endMonth = 12; if (i == currentYear.Year) { endMonth = currentYear.Month; } for (var j = startMonth; j <= endMonth; j = j + 3) { string quarterAvailable; if (j < 4) { quarterAvailable = "Q1 " + i; } else if (j < 7) { quarterAvailable = "Q2 " + i; } else if (j < 10) { quarterAvailable = "Q3 " + i; } else { quarterAvailable = "Q4 " + i; } reportInfo.quarterNames.Add(quarterAvailable); } } //ViewBag.Quarters = new SelectList(reportInfo.quarterNames, "Value", "Text"); return(View(reportInfo)); }
public ActionResult Index() { Dashboard homeDashboard = new Dashboard(); homeDashboard.salesThisMonth = new List <CommissionReport>(); homeDashboard.hotBike = new List <BikeSummary>(); DateTime currentDate = DateTime.Now; DateTime beginDate = new DateTime(currentDate.Year, currentDate.Month, 1); DateTime endDate = beginDate.AddMonths(1); // Dashboard get current Discounts (Sales) homeDashboard.currentDiscounts = (from p in _db.Discounts where beginDate < currentDate && currentDate < endDate select p).ToList(); // Dashboard get sales summary for the current month var activeSalespeople = (from p in _db.Sales where p.Sales_Date >= beginDate && p.Sales_Date <= endDate select p.Salesperson).Distinct().ToList(); foreach (int agentId in activeSalespeople) { CommissionReport salespersonReport = new CommissionReport(); Salesperson salesAgent = (from p in _db.Salespersons where p.Id == agentId select p).FirstOrDefault(); salespersonReport.Name = salesAgent.First_Name + " " + salesAgent.Last_Name; List <Sale> agentSales = (from p in _db.Sales where p.Sales_Date >= beginDate && p.Sales_Date <= endDate && p.Salesperson == agentId select p).ToList(); salespersonReport.bikesSold = agentSales.Count; List <SalesDetailView> agentSalesDetail = new List <SalesDetailView>(); foreach (Sale sold in agentSales) { SalesDetailView salesDetail = new SalesDetailView(); //Check for discount on this product for the date of sale var discount = (from p in _db.Discounts where p.Product == sold.Product where p.Begin_Date <sold.Sales_Date && p.End_Date> sold.Sales_Date select p).FirstOrDefault(); if (discount != null) { decimal actualSalePrice = sold.Product1.Sale_Price * (1 - discount.Discount_Percentage) ?? 0; salesDetail.Sale_Price = actualSalePrice; salesDetail.Comission = actualSalePrice * sold.Product1.Comission ?? 0; } else { salesDetail.Sale_Price = sold.Product1.Sale_Price ?? 0; salesDetail.Comission = sold.Product1.Sale_Price * sold.Product1.Comission ?? 0; } agentSalesDetail.Add(salesDetail); } salespersonReport.totalSales = agentSalesDetail.Sum(x => x.Sale_Price); salespersonReport.totalComissions = agentSalesDetail.Sum(x => x.Comission); homeDashboard.salesThisMonth.Add(salespersonReport); } homeDashboard.salesThisMonth = homeDashboard.salesThisMonth.OrderByDescending(x => x.totalSales).ToList(); // Dashboard get bikes popular for last 3 months DateTime popularStartDate = currentDate.AddMonths(-3); var bikesSold = (from p in _db.Sales where p.Sales_Date > popularStartDate && p.Sales_Date <= currentDate select p.Product1.Name).Distinct().ToList(); foreach (string bikeName in bikesSold) { BikeSummary bikeTrend = new BikeSummary(); bikeTrend.bikeName = bikeName; bikeTrend.bikesSold = (from p in _db.Sales where p.Sales_Date > popularStartDate && p.Sales_Date <= currentDate && p.Product1.Name == bikeName select p).ToList().Count(); homeDashboard.hotBike.Add(bikeTrend); } homeDashboard.hotBike = homeDashboard.hotBike.OrderByDescending(x => x.bikesSold).ToList(); return(View(homeDashboard)); }
public CustomWebViewClient(SalesDetailView activity) { myActivity = activity; }