Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="xl"></param>
        /// <param name="goodList"></param>
        private void GenerateMissionReadySheets(SarUnit unit, ExcelFile xl, ExcelSheet goodList)
        {
            IMissionReadyPlugin extension = null;

            string longName = this.settings.GroupFullName ?? this.settings.GroupName;
            IQueryable <UnitMembership> memberships = this.db.UnitMemberships.Include("Person.ComputedAwards.Course").Include("Status");

            if (unit != null)
            {
                memberships = memberships.Where(um => um.Unit.Id == unit.Id);
                longName    = unit.LongName;
                extension   = this.extensions.For <IMissionReadyPlugin>(unit);
            }
            memberships = memberships.Where(um => um.EndTime == null && um.Status.IsActive);
            memberships = memberships.OrderBy(f => f.Person.LastName).ThenBy(f => f.Person.FirstName);

            goodList.Header = longName + " Mission Active Roster";
            goodList.Footer = DateTime.Now.ToShortDateString();

            var courses = (from c in this.db.TrainingCourses where c.WacRequired > 0 select c).OrderBy(x => x.DisplayName).ToList();

            int             headerIdx    = 0;
            Action <string> appendHeader = head =>
            {
                var cell = goodList.CellAt(0, headerIdx++);
                cell.SetValue(head);
                cell.SetBold(true);
                cell.SetTextWrap(true);
            };

            Action <MissionReadyColumns> insertExtensionHeaders = group =>
            {
                if (extension == null)
                {
                    return;
                }
                foreach (var value in extension.GetHeadersAfter(group))
                {
                    appendHeader(value);
                }
            };


            insertExtensionHeaders(MissionReadyColumns.Start);
            appendHeader("DEM");
            insertExtensionHeaders(MissionReadyColumns.WorkerNumber);
            appendHeader("Lastname");
            appendHeader("Firstname");
            insertExtensionHeaders(MissionReadyColumns.Name);
            appendHeader("Card Type");
            insertExtensionHeaders(MissionReadyColumns.WorkerType);
            foreach (var course in courses)
            {
                var cell = goodList.CellAt(0, headerIdx++);
                cell.SetValue(course.DisplayName);

                cell.SetBold(true);
                cell.SetTextWrap(true);
            }
            insertExtensionHeaders(MissionReadyColumns.Courses);


            ExcelSheet badList      = xl.CopySheet(goodList.Name, "Non-Mission Members");
            ExcelSheet nonFieldList = xl.CopySheet(goodList.Name, "Admin Members");

            using (SheetAutoFitWrapper good = new SheetAutoFitWrapper(xl, goodList))
            {
                using (SheetAutoFitWrapper bad = new SheetAutoFitWrapper(xl, badList))
                {
                    using (SheetAutoFitWrapper admin = new SheetAutoFitWrapper(xl, nonFieldList))
                    {
                        int  idx    = 1;
                        int  c      = 0;
                        Guid lastId = Guid.Empty;

                        foreach (UnitMembership membership in memberships)
                        {
                            Member member = membership.Person;
                            if (member.Id == lastId)
                            {
                                continue;
                            }
                            lastId = member.Id;

                            CompositeTrainingStatus stats = CompositeTrainingStatus.Compute(member, courses, DateTime.Now);

                            SheetAutoFitWrapper wrap = bad;
                            // If the person isn't supposed to keep up a WAC card, then they're administrative...
                            if (membership.Status.WacLevel == WacLevel.None)
                            {
                                wrap = admin;
                            }
                            // If they're current on training and have a DEM card, they're good...
                            else if (stats.IsGood && member.WacLevel != WacLevel.None)
                            {
                                wrap = good;
                            }
                            idx = wrap.Sheet.NumRows + 1;
                            c   = 0;

                            Action <MissionReadyColumns> insertExtensionColumns = group =>
                            {
                                if (extension == null)
                                {
                                    return;
                                }
                                foreach (var value in extension.GetColumnsAfter(group, member))
                                {
                                    wrap.SetCellValue(value, idx, c++);
                                }
                            };

                            insertExtensionColumns(MissionReadyColumns.Start);
                            wrap.SetCellValue(string.Format("{0:0000}", member.DEM), idx, c++);
                            insertExtensionColumns(MissionReadyColumns.WorkerNumber);
                            wrap.SetCellValue(member.LastName, idx, c++);
                            wrap.SetCellValue(member.FirstName, idx, c++);
                            insertExtensionColumns(MissionReadyColumns.Name);
                            ExcelCell cell = wrap.Sheet.CellAt(idx, c);
                            switch (member.WacLevel)
                            {
                            case WacLevel.Field:
                                cell.SetFillColor(Color.Green);
                                cell.SetFontColor(Color.White);
                                break;

                            case WacLevel.Novice:
                                cell.SetFillColor(Color.Red);
                                cell.SetFontColor(Color.White);
                                break;

                            case WacLevel.Support:
                                cell.SetFillColor(Color.Orange);
                                break;
                            }
                            wrap.SetCellValue(member.WacLevel.ToString(), idx, c++);
                            insertExtensionColumns(MissionReadyColumns.WorkerType);

                            foreach (var course in courses)
                            {
                                TrainingStatus stat = stats.Expirations[course.Id];

                                if ((stat.Status & ExpirationFlags.Okay) != ExpirationFlags.Okay)
                                {
                                    wrap.Sheet.CellAt(idx, c).SetFillColor(Color.Pink);
                                    wrap.Sheet.CellAt(idx, c).SetBorderColor(Color.Red);
                                }

                                wrap.SetCellValue(stat.ToString(), idx, c);
                                if (stat.Expires.HasValue)
                                {
                                    wrap.Sheet.CellAt(idx, c).SetValue(stat.Expires.Value.Date.ToString("yyyy-MM-dd"));
                                }

                                c++;
                            }
                            insertExtensionColumns(MissionReadyColumns.Courses);

                            if (wrap == bad)
                            {
                                wrap.Sheet.CellAt(idx, c).SetValue(member.ContactNumbers.Where(f => f.Type == "email").OrderBy(f => f.Priority).Select(f => f.Value).FirstOrDefault());
                            }
                            insertExtensionColumns(MissionReadyColumns.End);
                            idx++;
                        }
                        admin.Sheet.AutoFitAll();
                        good.Sheet.AutoFitAll();
                        bad.Sheet.AutoFitAll();
                    }
                }
            }
        }