// GET: BillReports/GenerateALSR
        public async Task <ActionResult> GenerateALSR()
        {
            var user = await ReturnCurrentUserAsync();

            var reviews = await db.BillReviews
                          .Where(r =>
                                 r.Bill.LegStatusID == 1 &&        //select only Pending Bills
                                 r.DeptID == user.DeptID &&
                                 r.Approvals.Where(a => a.ApprovalLevel == 1).Any() &&
                                 r.BillVersionID == db.BillVersions.Where(v => v.BillID == r.BillID).OrderByDescending(v => v.VersionNum).FirstOrDefault().ID)
                          .Include(r => r.Bill)
                          .Include(r => r.CreatedByUser)
                          .Include(r => r.Recommendation)
                          .OrderBy(r => r.Bill.BillPrefixID).ThenBy(r => r.Bill.Suffix)
                          .ToListAsync();

            var viewModel = new AlsrReportSelectBillReviewsViewModel();

            foreach (BillReview r in reviews)
            {
                var x = new AlsrReportIndividualBillReviewViewModel();
                x.ID = r.ID;
                x.CompositeBillNumber   = r.Bill.CompositeBillNumber;
                x.VersionNum            = r.BillVersion.VersionNum;
                x.CreatedByUserFullName = r.CreatedByUser.FullName;
                x.Recommendation        = r.Recommendation.Description;
                x.Selected = true;
                viewModel.BillReviews.Add(x);
            }
            return(View(viewModel));
        }
        public async Task <ActionResult> GenerateALSR(AlsrReportSelectBillReviewsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var selectedIds = model.getSelectedIds();

                if (selectedIds.Count() == 0)
                {
                    Danger("No Bills were selected for inclusion in the report.");
                    return(View());
                }

                var selectedBillReviews = from x in db.BillReviews
                                          .Include(r => r.Bill)
                                          .Include(r => r.CreatedByUser)
                                          .Include(r => r.Recommendation)
                                          where selectedIds.Contains(x.ID)
                                          select x;

                var report = new BillsAlsrReport();
                report.BudgetPeriodID        = (await db.AppGlobalSettings.FirstOrDefaultAsync()).BudgetPeriodID;
                report.BudgetSessionID       = (await db.AppGlobalSettings.FirstOrDefaultAsync()).BudgetSessionID;
                report.CreatedAt             = DateTime.Now;
                report.GovOfficeDeliveryDate = model.GovOfficeDeliveryDate;

                var user = await ReturnCurrentUserAsync();

                report.DeptID            = user.DeptID;
                report.DivID             = user.DivID;
                report.ApplicationUserID = user.Id;

                //var snapshots = new List<AlsrBillReviewSnapshot>();
                foreach (BillReview r in selectedBillReviews.ToList())
                {
                    var s = new AlsrBillReviewSnapshot()
                    {
                        ActivelyTracking           = r.ActivelyTracking,
                        ApplicationUserID          = r.ApplicationUserID,
                        BillID                     = r.BillID,
                        BillReviewRecommendationID = r.BillReviewRecommendationID,
                        BillVersionID              = r.BillVersionID,
                        CapturedFromBillReviewID   = r.ID,
                        CapturedFromRowVersion     = r.RowVersion,
                        Comments                   = r.Comments,
                        CreatedAt                  = r.CreatedAt,
                        CreatedAtApprovalLevel     = r.CreatedAtApprovalLevel,
                        DeptID                     = r.DeptID,
                        DivID = r.DivID,
                        FiscalImpactFuture      = r.FiscalImpactFuture,
                        FiscalImpactYr1         = r.FiscalImpactYr1,
                        FiscalImpactYr2         = r.FiscalImpactYr2,
                        FiscalNoteSubmitted     = r.FiscalNoteSubmitted,
                        InformationToBeProvided = r.InformationToBeProvided,
                        Notes             = r.Notes,
                        PolicyImpact      = r.PolicyImpact,
                        RequiresTestimony = r.RequiresTestimony,
                        Timestamp         = DateTime.Now
                                            //SupercedesPreviousSnapshotID = ()
                    };
                    report.AlsrBillReviewSnapshots.Add(s);
                }

                db.BillsAlsrReports.Add(report);
                await db.SaveChangesAsync();

                var reportService = new BillsAlsrReportsService();
                var readReport    = reportService.GetById(report.ID);
                readReport.Pdf = await CreateAlsrPdf(report.ID);

                readReport.Filename = string.Format("ALSR_{0}_{1}.pdf", readReport.Dept.Description, readReport.CreatedAt);
                reportService.Update(readReport);
                //await db.SaveChangesAsync();

                Success("ALSR Report created successfully");
                return(RedirectToAction("ALSR"));
            }
            Danger("Invalid model state");
            return(View());
        }