private ActionResult SearchRecords(DataEntryProductivityReportViewModel model)
        {
            var mgr = new DataEntryProductivityReportManager(model.AssignedService);

            if (model.TransactionsForPeriodType != "Range")
            {
                model.ToDate   = DateTime.Now;
                model.FromDate = model.ToDate.AddDays(-Int32.Parse(model.TransactionsForPeriodType));
            }
            else
            {
                // Add one day to the end of the range. Legacy does this.
                model.ToDate = model.ToDate.AddDays(1);
            }

            model.IndexedRecords    = mgr.GetIndexedRecordsByDateRange(model.FromDate.ToString("MM/dd/yyyy"), model.ToDate.ToString("MM/dd/yyyy"));
            model.KeyedRecords      = mgr.GetKeyedRecordsByDateRange(model.FromDate.ToString("MM/dd/yyyy"), model.ToDate.ToString("MM/dd/yyyy"));
            model.GrandTotalIndexed = mgr.indexTotal.ToString();
            model.GrandTotalKeyed   = mgr.keyedTotal.ToString();
            model.GrandTotal        = (mgr.indexTotal + mgr.keyedTotal).ToString();

            // Did the user ask to save to file?
            if (model.SaveToFile == "true")
            {
                // Return a CSV formatted file.
                return(ProductivityResultsToFile(model));
            }

            // Fall-through (this was just a search).
            return(View(model));
        }
        public ActionResult DataEntryProductivityReport()
        {
            var model = new DataEntryProductivityReportViewModel();

            model.AssignedService = this.AssignSrv;

            // Default to a yesterday search.
            model.TransactionsForPeriodType = "1";

            return(SearchRecords(model));
        }
        private ActionResult ProductivityResultsToFile(DataEntryProductivityReportViewModel model)
        {
            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);

            // Service-specific formatting.
            string tops    = "";
            string nontops = "";

            switch (model.AssignedService)
            {
            case "FTS":
                tops    = "tops";
                nontops = "nontops";
                break;

            case "PBS":
                tops    = "const";
                nontops = "nonconst";
                break;

            default:
                tops    = "tops_const";
                nontops = "nontops_const";
                break;
            }

            // Join indexed and keyed records.
            var leftOuterJoin = from index in model.IndexedRecords
                                //join keyed in model.KeyedRecords
                                //on index.PC equals keyed.PC
                                //into temp
                                //from keyed in temp.DefaultIfEmpty()
                                select new
            {
                NAME    = index.Name,
                pc      = index.PC,
                po      = index.PO,
                rr      = index.RR,
                nontops = index.NonTops,
                vcpo    = index.Vcpo,
                tops    = index.Tops,
                misc    = index.Misc,
                acrn    = index.Acrn,
                a3881   = index.ThreeEightEightOne,
                bid     = index.Bid,
                cncs    = index.Cncs,
                jv      = index.Jv,
                mipr    = index.Mipr,
                ol      = index.Outl,
                opac    = index.Opac,
                pd      = index.Pd,
                recy    = index.RecY,
                rwa     = index.Rwa,
                pdir    = index.PDir,
                rei     = index.Rei,
                noc     = index.Noc,
                ipac    = index.Ipac,
                itotal  = index.Total,
                //kpo = (keyed != null ? keyed.PO : "0"),
                //krr = (keyed != null ? keyed.RR : "0"),
                //knontops = (keyed != null ? keyed.NonTops : "0"),
                //kvcpo = (keyed != null ? keyed.Vcpo : "0"),
                //ktops = (keyed != null ? keyed.Tops : "0"),
                //kmisc = (keyed != null ? keyed.Misc : "0"),
                //krwa = (keyed != null ? keyed.Rwa : "0"),
                //ktotal = (keyed != null ? keyed.Total : "0"),
                //Gtotal = (Int32.Parse(index.Total) + (keyed != null ? Int32.Parse(keyed.Total) : 0)).ToString(),
            };
            var rightOuterJoin = from keyed in model.KeyedRecords
                                 //join index in model.IndexedRecords
                                 //on keyed.PC equals index.PC
                                 //into temp
                                 //from index in temp.DefaultIfEmpty()
                                 select new
            {
                NAME = keyed.Name,
                pc   = keyed.PC,
                //po = (index != null ? index.PO : "0"),
                //rr = (index != null ? index.RR : "0"),
                //nontops = (index != null ? index.NonTops : "0"),
                //vcpo = (index != null ? index.Vcpo : "0"),
                //tops = (index != null ? index.Tops : "0"),
                //misc = (index != null ? index.Misc : "0"),
                //acrn = (index != null ? index.Acrn : "0"),
                //a3881 = (index != null ? index.ThreeEightEightOne : "0"),
                //bid = (index != null ? index.Bid : "0"),
                //cncs = (index != null ? index.Cncs : "0"),
                //jv = (index != null ? index.Jv : "0"),
                //mipr = (index != null ? index.Mipr : "0"),
                //ol = (index != null ? index.Outl : "0"),
                //opac = (index != null ? index.Opac : "0"),
                //pd = (index != null ? index.Pd : "0"),
                //recy = (index != null ? index.RecY : "0"),
                //rwa = (index != null ? index.Rwa : "0"),
                //pdir = (index != null ? index.PDir : "0"),
                //rei = (index != null ? index.Rei : "0"),
                //noc = (index != null ? index.Noc : "0"),
                //ipac = (index != null ? index.Ipac : "0"),
                //itotal = (index != null ? index.Total : "0"),
                kpo      = keyed.PO,
                krr      = keyed.RR,
                knontops = keyed.NonTops,
                kvcpo    = keyed.Vcpo,
                ktops    = keyed.Tops,
                kmisc    = keyed.Misc,
                krwa     = keyed.Rwa,
                ktotal   = keyed.Total,
                //Gtotal = (Int32.Parse(keyed.Total) + (index != null ? Int32.Parse(index.Total) : 0)).ToString(),
            };
            //var combinedResults = leftOuterJoin.Union(rightOuterJoin);

            // Indexed
            bool wroteHeader = false;

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

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

            // Keyed
            wroteHeader = false;
            foreach (var result in rightOuterJoin)
            {
                var type  = result.GetType();
                var props = type.GetProperties();

                if (!wroteHeader)
                {
                    // Replace some column names based on service.
                    var keys = props.Select(x => (x.Name == "nontops" ? nontops : (x.Name == "knontops" ? "k" + nontops : (x.Name == "tops" ? tops : (x.Name == "ktops" ? "k" + tops : x.Name))))).ToArray();
                    writer.WriteLine("Keyed");
                    writer.Write(string.Join(",", keys));
                    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", "DataEntryProductivityReport.csv"));
        }
 public ActionResult DataEntryProductivityReport(DataEntryProductivityReportViewModel model)
 {
     return(SearchRecords(model));
 }