示例#1
0
        public void Calculate_MissionHours()
        {
            TestContext db = new TestContext();
            var         me = db.Members.Add(new Member());

            Func <int, double, string, string, MissionRoster> createMission = (daysAgo, hours, role, type) =>
            {
                Mission m = db.Missions.Add(new Mission {
                    StartTime = DateTime.Now.AddDays(-daysAgo).AddHours(-1), MissionType = type
                });
                MissionRoster r = db.MissionRosters.Add(new MissionRoster
                {
                    Person       = me,
                    Mission      = m,
                    TimeIn       = DateTime.Now.AddDays(-daysAgo),
                    TimeOut      = DateTime.Now.AddDays(-daysAgo).AddHours(hours),
                    InternalRole = role
                });
                return(r);
            };

            createMission(15, 3.5, "field", "search");
            createMission(63, 8.0, "field", "resecue");
            var satisfies = createMission(80, 4, "field", "urban,search");

            createMission(100, 23, "field", "search");

            var course = db.TrainingCourses.Add(new TrainingCourse {
                DisplayName = "Target", FullName = "Target", ValidMonths = 36
            });

            db.TrainingRules.Add(new TrainingRule {
                RuleText = string.Format("Mission(12:%:36)>{0}", course.Id)
            });

            TrainingRecordCalculator calc = new TrainingRecordCalculator(db);
            var result = calc.Calculate(new[] { me }, DateTime.Now);

            Assert.AreEqual(1, result.Count, "awarded records");
            var record = db.ComputedTrainingAwards.Single();

            Assert.AreEqual(satisfies.TimeIn.Value.AddMonths(course.ValidMonths.Value), record.Expiry.Value, "go back until we have the hours, add course months");
        }
        protected override void AddRosterRowFrom4x4Sheet(ExpandedRowsContext model, SarUnit unit, IRosterEntry row)
        {
            MissionRoster mrow = (MissionRoster)row;

            Guid          personId = mrow.Person.Id;
            MissionRoster newRow   = new MissionRoster
            {
                Mission      = (Mission)model.SarEvent,
                Unit         = unit,
                Person       = this.db.Members.Include("MissionRosters").Single(f => f.Id == personId),
                TimeIn       = mrow.TimeIn,
                TimeOut      = mrow.TimeOut,
                Miles        = mrow.Miles,
                InternalRole = mrow.InternalRole
            };

            mrow.Id = newRow.Id;
            this.db.MissionRosters.Add(newRow);
        }
        protected override void AddRosterRowFrom4x4Sheet(ExpandedRowsContext model, SarUnit unit, IRosterEntry row)
        {
            MissionRoster mrow = (MissionRoster)row;

              Guid personId = mrow.Person.Id;
              MissionRoster newRow = new MissionRoster
              {
            Mission = (Mission)model.SarEvent,
            Unit = unit,
            Person = this.db.Members.Include("MissionRosters").Single(f => f.Id == personId),
            TimeIn = mrow.TimeIn,
            TimeOut = mrow.TimeOut,
            Miles = mrow.Miles,
            InternalRole = mrow.InternalRole
              };
              mrow.Id = newRow.Id;
              this.db.MissionRosters.Add(newRow);
        }
示例#4
0
        public ActionResult Review4x4Roster(bool?bot)
        {
            if (!Has4x4RosterPermissions())
            {
                return(CreateLoginRedirect());
            }

            // Value is pre-incremented in while loop.
            int startRow = 13;

            if (Request.Files.Count != 1)
            {
                throw new InvalidOperationException("Can only submit one roster");
            }

            var postedFile = Request.Files[0];

            byte[] fileData = new byte[postedFile.ContentLength];
            postedFile.InputStream.Read(fileData, 0, fileData.Length);

            var stream = new System.IO.MemoryStream(fileData);

            stream.Position = 0;

            ExcelFile xl = null; // new ExcelFile();

            if (postedFile.FileName.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
            {
                xl = ExcelService.Read(postedFile.InputStream, ExcelFileType.XLSX);
            }
            else
            {
                xl = ExcelService.Read(postedFile.InputStream, ExcelFileType.XLS);
            }
            ExcelSheet sheet = xl.GetSheet(0);

            object   start     = sheet.CellAt(13, 4).StringValue.ToUpperInvariant().Replace("DATE", "").Replace("TIME", "").Replace("IN", "").Replace("OUT", "").Trim();
            DateTime startDate = DateTime.Parse(start.ToString());

            ExpandedRowsContext model = new ExpandedRowsContext();

            model.EventId = Guid.NewGuid();
            if (typeof(E) == typeof(Mission))
            {
                model.Type     = RosterType.Mission;
                model.SarEvent = new Mission {
                    Id = model.EventId
                };
            }
            else
            {
                model.Type     = RosterType.Training;
                model.SarEvent = new Training {
                    Id = model.EventId
                };
            }
            model.SarEvent.StateNumber = sheet.CellAt(9, 2).StringValue.Split(' ')[0];
            model.SarEvent.Title       = sheet.CellAt(10, 2).StringValue;
            model.SarEvent.Location    = sheet.CellAt(11, 2).StringValue;
            model.SarEvent.County      = sheet.CellAt(9, 10).StringValue.ToLowerInvariant();

            model.BadRows     = new List <IRosterEntry>();
            model.Rows        = new List <IRosterEntry>();
            model.NumDays     = 3;
            model.RosterStart = startDate;


            SarUnit unit = this.db.Units.Where(f => f.DisplayName == "4x4").Single();

            //ctx.Detach(unit);

            while (!string.IsNullOrWhiteSpace((sheet.CellAt(++startRow, 1).StringValue ?? "").ToString()))
            {
                R      roster = new R();
                string name   = sheet.CellAt(startRow, 1).StringValue;
                string dem    = sheet.CellAt(startRow, 2).StringValue.PadLeft(4, '0');
                var    member = this.db.Members
                                .Where(f => f.LastName + ", " + f.FirstName == name && f.DEM == dem)
                                .SingleOrDefault();

                if (member == null)
                {
                    //throw new InvalidOperationException(string.Format("Member {0} not found", sheet.CellAt(startRow, 1]));
                    continue;
                }

                //                    ctx.Detach(member);
                roster.Person = member;

                for (int i = 0; i < 3; i++)
                {
                    string v = (sheet.CellAt(startRow, 8 - i * 2).StringValue ?? "").ToString();
                    if (!string.IsNullOrWhiteSpace(v))
                    {
                        v = v.PadLeft(4, '0');
                        int hours   = int.Parse(v.Substring(0, 2));
                        int minutes = int.Parse(v.Substring(2, 2));
                        roster.TimeIn = startDate.AddDays(2 - i).AddHours(hours).AddMinutes(minutes);
                    }
                    v = (sheet.CellAt(startRow, 5 + i * 2).StringValue ?? "").ToString();
                    if (!string.IsNullOrWhiteSpace(v))
                    {
                        v = v.PadLeft(4, '0');
                        int hours   = int.Parse(v.Substring(0, 2));
                        int minutes = int.Parse(v.Substring(2, 2));
                        roster.TimeOut = startDate.AddDays(i).AddHours(hours).AddMinutes(minutes);
                    }
                }

                string miles = (sheet.CellAt(startRow, 11).StringValue ?? "").ToString();
                if (miles != "")
                {
                    double numericMiles;
                    if (double.TryParse(miles, out numericMiles))
                    {
                        roster.Miles = (int)Math.Ceiling(numericMiles);
                    }
                }

                MissionRoster missionRoster = roster as MissionRoster;
                if (missionRoster != null)
                {
                    missionRoster.Unit = unit;

                    if ("CO".Equals(sheet.CellAt(startRow, 3).StringValue))
                    {
                        missionRoster.InternalRole = "InTown";
                    }
                    else if ("OL".Equals(sheet.CellAt(startRow, 3).StringValue))
                    {
                        missionRoster.InternalRole = "OL";
                    }
                    else if (new[] { "AS", "ATL", "OUA", "TL", "TLT", "TM" }.Contains(sheet.CellAt(startRow, 3).StringValue))
                    {
                        missionRoster.InternalRole = "Field";
                    }
                    else if ("VAN".Equals(sheet.CellAt(startRow, 3).StringValue))
                    {
                        missionRoster.InternalRole = "Base";
                    }
                    else
                    {
                        missionRoster.InternalRole = "Responder";
                    }
                }
                model.Rows.Add(roster);
            }

            if (model.Rows.Count > 0)
            {
                model.SarEvent.StartTime = model.Rows.Min(f => f.TimeIn).Value;
                model.SarEvent.StopTime  = model.Rows.Max(f => f.TimeOut).Value;
            }

            DateTime early = model.SarEvent.StartTime.AddDays(-1);
            DateTime late  = model.SarEvent.StartTime.AddDays(1);

            E[] alternates = this.GetEventSource().Where(f => f.StateNumber == model.SarEvent.StateNumber || (f.StartTime > early && f.StartTime < late)).OrderBy(f => f.StartTime).ToArray();

            ViewData["alternateMissions"] = alternates;

            ViewData["CanEditRoster"] = false;
            ViewData["IsTemp"]        = true;

            SetSessionValue("reviewRosterBot", bot);
            SetSessionValue("reviewRoster", model);
            SetSessionValue("reviewRosterData", fileData);
            SetSessionValue("reviewRosterName", postedFile.FileName);
            return(View(model));
        }