private ActionResult AgedExceptionsResultsToFile(AgedExceptionsViewModel model)
        {
            MemoryStream output  = new MemoryStream();
            StreamWriter writer  = new StreamWriter(output, Encoding.UTF8);
            var          mgr     = new AgedExceptionsManager();
            var          results = mgr.GetAgedExceptionsOverThree(model.DateFrom, model.DateTo, model.PrepCode);

            bool wroteHeader = false;

            foreach (var result in results)
            {
                var type  = result.GetType();
                var props = type.GetProperties();

                if (!wroteHeader)
                {
                    // Replace some column names based on service.
                    var keys = props.Select(x => x.Name).ToArray();
                    writer.Write(string.Join(";", keys));
                    writer.WriteLine();
                    writer.WriteLine();
                    wroteHeader = true;
                }
                var values = props.Select(x => x.GetValue(result, null)).ToArray();
                writer.Write(string.Join(";", values));
                writer.WriteLine();
            }

            writer.Flush();
            output.Position = 0;

            return(File(output, "text/comma-separated-values", "AgedExceptionsUserReport.csv"));
        }
        public ActionResult AgedExceptionsDetail([DataSourceRequest] DataSourceRequest request, AgedExceptionsViewModel search)
        {
            var mgr    = new AgedExceptionsManager();
            var items  = mgr.FetchAgedExceptionsDetail(search).AsQueryable();
            var result = items.ToDataSourceResult(request);

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        private ActionResult AgedExceptionInternal(AgedExceptionsViewModel model)
        {
            // Did the user ask to save to file?
            if (model.SaveToFile == "true")
            {
                // Return a CSV formatted file.
                return(AgedExceptionsResultsToFile(model));
            }

            // Fall-through common path: search by date range and PrepCode.
            var mgr = new AgedExceptionsManager();

            model.AgedExceptionTotals    = mgr.GetAgedExceptionTotals(model.DateFrom, model.DateTo, model.PrepCode).ToList();
            model.AgedExceptionSummaries = mgr.GetAgedExceptionSummary().ToList();

            // Reset the detail view.
            model.ErrCode = "";
            return(View("AgedExceptions", model));
        }