using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Common; using Newtonsoft.Json; public partial class Reports_Pickup : Page { public string LocationsJson { get; private set; } protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; using (var db = new DataModel()) { var operatorLogin = db.OperatorLogins.FirstOrDefault(f => f.Username == HttpContext.Current.User.Identity.Name); if (operatorLogin != null) { Customer.DataSource = db.Customers.ToList(); Customer.DataTextField = "Name"; Customer.DataValueField = "CustomerId"; Customer.DataBind(); } else { Customer.Visible = false; CustomerLabel.Visible = false; } } // echo date("F j, Y", strtotime('-30 day')); StartDateText.Text = DateTime.Now.AddDays(-30).ToString("MMMM d, yyyy"); StartDate.Value = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd"); EndDateText.Text = DateTime.Now.ToString("MMMM d, yyyy"); EndDate.Value = DateTime.Now.ToString("yyyy-MM-dd"); RefreshReportData(); } protected void RunReport_Click(object sender, EventArgs e) { RefreshReportData(); } private void RefreshReportData() { DateTime start; DateTime end; Trace.Warn("[RefreshReportData]"); Trace.Warn("StartDate.Value", StartDate.Value); Trace.Warn("EndDate.Value", EndDate.Value); if (!DateTime.TryParseExact(StartDate.Value, "yyyy-MM-dd", Thread.CurrentThread.CurrentCulture, System.Globalization.DateTimeStyles.None, out start) || !DateTime.TryParseExact(EndDate.Value, "yyyy-MM-dd", Thread.CurrentThread.CurrentCulture, System.Globalization.DateTimeStyles.None, out end)) return; end = end.AddDays(1); var log = new List(); using (var db = new DataModel()) { var operatorLogin = db.OperatorLogins.FirstOrDefault(f => f.Username == HttpContext.Current.User.Identity.Name); var customerLogin = db.GetCustomerLoginFromGuid(HttpContext.Current.User.Identity.Name); var customerId = 0; if (operatorLogin != null) { if (!int.TryParse(Customer.SelectedValue, out customerId)) customerId = 0; } else if (customerLogin != null) { var customer = customerLogin.FirstOrDefault(); customerId = customer == null ? 0 : customer.CustomerId; } log = db.DriverLogs.Where(w => w.CustomerId == customerId && w.TimeStamp >= start && w.TimeStamp < end).ToList(); DriversLog.DataSource = log; DriversLog.DataBind(); var locations = log.Select(s => new { s.DriverLogId, s.DriverAction.Action, s.LocationId, s.Location.Address, s.Location.Lat, s.Location.Lng }).ToList(); LocationsJson = JsonConvert.SerializeObject(locations); } } }