PageRenderTime 3ms CodeModel.GetById 2ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsWeb/Areas/Public/Models/CheckIn/CheckInModel.cs

https://bitbucket.org/rbhakser/bvcms
C# | 523 lines | 498 code | 13 blank | 12 comment | 132 complexity | 0ff3d15c0f7ca30685ae0fae12e9c6b7 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Data.Linq;
  5using System.Web;
  6using UtilityExtensions;
  7using CmsData;
  8using System.Web.Mvc;
  9using System.Data.Linq.SqlClient;
 10using CmsData.Codes;
 11using System.Xml.Linq;
 12
 13namespace CmsWeb.Models
 14{
 15    public class CheckInModel
 16    {
 17        public string GetNextPrintJobs(string kiosks)
 18        {
 19            var a = kiosks.Replace(" ", "").Split(',');
 20            var q = from d in DbUtil.Db.PrintJobs
 21                    where a.Contains(d.Id)
 22                    orderby d.Stamp
 23                    select d;
 24            var x = XDocument.Parse("<PrintJobs><jobs /></PrintJobs>");
 25            foreach (var j in q)
 26            {
 27                var d = XDocument.Parse(j.Data);
 28                var jobs = (XElement)x.Root.FirstNode;
 29                jobs.Add(d.Root);
 30            }
 31            DbUtil.Db.PrintJobs.DeleteAllOnSubmit(q);
 32            DbUtil.Db.SubmitChanges();
 33            return x.ToString();
 34        }
 35        public void SavePrintJob(string kiosk, string xml)
 36        {
 37            var d = new PrintJob { Id = kiosk.Replace(" ", ""), Data = xml, Stamp = DateTime.Now };
 38            DbUtil.Db.PrintJobs.InsertOnSubmit(d);
 39            DbUtil.Db.SubmitChanges();
 40        }
 41        public List<FamilyInfo> Match(string id, int campus, int thisday)
 42        {
 43			id = Util.GetDigits(id);
 44            var ph = id.PadLeft(10, '0');
 45            var p7 = ph.Substring(3);
 46            var ac = ph.Substring(0, 3);
 47            var q1 = from f in DbUtil.Db.Families
 48                     where f.HeadOfHousehold.DeceasedDate == null
 49                     where f.HomePhoneLU.StartsWith(p7)
 50                        || f.People.Any(p => p.CellPhoneLU.StartsWith(p7)
 51						|| p.PeopleExtras.Any(ee => ee.Data == id && ee.Field == "PIN"))
 52                     let flock = f.FamilyCheckinLocks
 53                        .FirstOrDefault(l => SqlMethods.DateDiffSecond(l.Created, DateTime.Now) < 60)
 54                     orderby f.FamilyId
 55                     select new FamilyInfo
 56                     {
 57                         FamilyId = f.FamilyId,
 58                         AreaCode = f.HomePhoneAC,
 59                         Name = f.HeadOfHousehold.Name,
 60                         Phone = id,
 61                         Locked = flock == null ? false : flock.Locked,
 62                     };
 63            var matches = q1.ToList();
 64            if (matches.Count > 1)
 65                matches = matches.Where(m => m.AreaCode == ac || ac == "000").ToList();
 66            return matches;
 67        }
 68        public List<FamilyInfo> Find(string id)
 69        {
 70			var q2 = from kc in DbUtil.Db.CardIdentifiers
 71					 where kc.Id == id
 72                     select new FamilyInfo
 73                     {
 74                         FamilyId = kc.Person.FamilyId,
 75                         AreaCode = kc.Person.Family.HomePhoneAC,
 76                         Name = kc.Person.Family.HeadOfHousehold.Name,
 77                         Phone = id
 78                     };
 79            var matches = q2.ToList();
 80
 81			if (matches.Count == 0)
 82			{
 83				var ph = Util.GetDigits(id).PadLeft(10, '0');
 84				var p7 = ph.Substring(3);
 85				var ac = ph.Substring(0, 3);
 86				var q1 = from f in DbUtil.Db.Families
 87				         where f.HeadOfHousehold.DeceasedDate == null
 88				         where f.HomePhoneLU.StartsWith(p7) && (f.HomePhoneAC == ac || ac == "000")
 89				               || f.People.Any(p => p.CellPhoneLU.StartsWith(p7)
 90						 				&& (p.CellPhoneAC == ac || ac == "000"))
 91				         orderby f.FamilyId
 92				         select new FamilyInfo
 93				                {
 94				                	FamilyId = f.FamilyId,
 95				                	AreaCode = f.HomePhoneAC,
 96				                	Name = f.HeadOfHousehold.Name,
 97				                	Phone = id,
 98				                };
 99				matches = q1.ToList();
100			}
101            return matches;
102        }
103        public List<Attendee> FamilyMembers(int id, int campus, int thisday)
104        {
105            var normalLabelsMemTypes = new int[] 
106            {
107                MemberTypeCode.Member,
108                MemberTypeCode.InActive
109            };
110            var now = Util.Now;
111            // get org members first
112            var members =
113                from om in DbUtil.Db.OrganizationMembers
114                let meetingHours = DbUtil.Db.GetTodaysMeetingHours(om.OrganizationId, thisday)
115                let recreg = om.Person.RecRegs.FirstOrDefault()
116                where om.Organization.CanSelfCheckin.Value
117                where (om.Pending ?? false) == false
118                where om.Organization.CampusId == campus || om.Organization.CampusId == null
119                where om.Organization.OrganizationStatusId == OrgStatusCode.Active
120                where om.Person.FamilyId == id
121                where om.Person.DeceasedDate == null
122                from meeting in meetingHours
123                let CheckedIn = DbUtil.Db.Attends.FirstOrDefault(aa =>
124                    aa.OrganizationId == om.OrganizationId
125                    && aa.PeopleId == om.PeopleId
126                    && aa.MeetingDate == meeting.Hour
127                    && aa.AttendanceFlag == true) != null
128                select new Attendee
129                {
130                    Id = om.PeopleId,
131                    Position = om.Person.PositionInFamilyId,
132                    MemberVisitor = "M",
133                    Name = om.Person.Name,
134                    First = om.Person.FirstName,
135                    PreferredName = om.Person.PreferredName,
136                    Last = om.Person.LastName,
137                    BYear = om.Person.BirthYear,
138                    BMon = om.Person.BirthMonth,
139                    BDay = om.Person.BirthDay,
140                    Class = om.Organization.OrganizationName,
141                    Leader = om.Organization.LeaderName,
142                    OrgId = om.OrganizationId,
143                    Location = om.Organization.Location,
144                    Age = om.Person.Age ?? 0,
145                    Gender = om.Person.Gender.Code,
146                    NumLabels = normalLabelsMemTypes.Contains(om.MemberTypeId)
147                            ? (om.Organization.NumCheckInLabels ?? 1)
148                            : (om.Organization.NumWorkerCheckInLabels ?? 0),
149                    Hour = meeting.Hour,
150                    CheckedIn = CheckedIn,
151                    goesby = om.Person.NickName,
152                    email = om.Person.EmailAddress,
153                    addr = om.Person.Family.AddressLineOne,
154                    zip = om.Person.Family.ZipCode,
155                    home = om.Person.Family.HomePhone,
156                    cell = om.Person.CellPhone,
157                    marital = om.Person.MaritalStatusId,
158                    gender = om.Person.GenderId,
159                    CampusId = om.Person.CampusId,
160                    allergies = recreg.MedicalDescription,
161                    emfriend = recreg.Emcontact,
162                    emphone = recreg.Emphone,
163                    activeother = recreg.ActiveInAnotherChurch ?? false,
164                    parent = recreg.Mname ?? recreg.Fname,
165                    grade = om.Person.Grade,
166                    HasPicture = om.Person.PictureId != null,
167                    Custody = (om.Person.CustodyIssue ?? false) == true,
168                    Transport = (om.Person.OkTransport ?? false) == true,
169                    RequiresSecurityLabel = 
170						normalLabelsMemTypes.Contains(om.MemberTypeId) // regular member
171						&& (om.Person.Age ?? 0) < 18 // less than 18
172						&& (om.Organization.NoSecurityLabel ?? false) == false, // org uses security
173
174                    church = om.Person.OtherNewChurch,
175                };
176
177            // now get recent visitors
178			var today = DateTime.Today;
179
180            var visitors =
181                from a in DbUtil.Db.Attends
182                where a.Person.FamilyId == id
183                where a.Person.DeceasedDate == null
184                where a.Organization.CanSelfCheckin.Value
185                where a.Organization.AllowNonCampusCheckIn == true 
186								|| a.Organization.CampusId == campus || campus == 0
187				where a.Organization.OrganizationStatusId == CmsData.Codes.OrgStatusCode.Active
188                where a.AttendanceFlag && 
189					(a.MeetingDate >= a.Organization.FirstMeetingDate.Value.Date || a.Organization.FirstMeetingDate == null)
190				where a.AttendanceFlag && (a.MeetingDate >= a.Organization.VisitorDate.Value.Date)
191				where Attend.VisitAttendTypes.Contains(a.AttendanceTypeId.Value)
192                where !a.Organization.OrganizationMembers.Any(om => om.PeopleId == a.PeopleId)
193                group a by new { a.PeopleId, a.OrganizationId } into g
194                let a = g.OrderByDescending(att => att.MeetingDate).First()
195                let meetingHours = DbUtil.Db.GetTodaysMeetingHours(a.OrganizationId, thisday)
196                let recreg = a.Person.RecRegs.FirstOrDefault()
197                from meeting in meetingHours
198                let CheckedIn = DbUtil.Db.Attends.FirstOrDefault(aa =>
199                    aa.OrganizationId == a.OrganizationId
200                    && aa.PeopleId == a.PeopleId
201                    && aa.MeetingDate == meeting.Hour
202                    && aa.AttendanceFlag == true) != null
203                select new Attendee
204                {
205                    Id = a.PeopleId,
206                    Position = a.Person.PositionInFamilyId,
207                    MemberVisitor = "V",
208                    Name = a.Person.Name,
209                    First = a.Person.FirstName,
210                    PreferredName = a.Person.PreferredName,
211                    Last = a.Person.LastName,
212                    BYear = a.Person.BirthYear,
213                    BMon = a.Person.BirthMonth,
214                    BDay = a.Person.BirthDay,
215                    Class = a.Organization.OrganizationName,
216                    Leader = a.Organization.LeaderName,
217                    OrgId = a.OrganizationId,
218                    //OrgId = (a.Organization.CanSelfCheckin ?? false) ? a.OrganizationId : 0,
219                    Location = a.Organization.Location,
220                    Age = a.Person.Age ?? 0,
221                    Gender = a.Person.Gender.Code,
222                    NumLabels = a.Organization.NumCheckInLabels ?? 1,
223                    Hour = meeting.Hour,
224                    CheckedIn = CheckedIn,
225
226                    goesby = a.Person.NickName,
227                    email = a.Person.EmailAddress,
228                    addr = a.Person.Family.AddressLineOne,
229                    zip = a.Person.Family.ZipCode,
230                    home = a.Person.Family.HomePhone,
231                    cell = a.Person.CellPhone,
232                    marital = a.Person.MaritalStatusId,
233                    gender = a.Person.GenderId,
234                    CampusId = a.Person.CampusId,
235                    allergies = recreg.MedicalDescription,
236                    emfriend = recreg.Emcontact,
237                    emphone = recreg.Emphone,
238                    activeother = recreg.ActiveInAnotherChurch ?? false,
239                    parent = recreg.Mname ?? recreg.Fname,
240                    grade = a.Person.Grade,
241                    HasPicture = a.Person.PictureId != null,
242                    Custody = (a.Person.CustodyIssue ?? false) == true,
243                    Transport = (a.Person.OkTransport ?? false) == true,
244                    RequiresSecurityLabel = ((a.Person.Age ?? 0) < 18) && (a.Organization.NoSecurityLabel ?? false) == false,
245                    church = a.Person.OtherNewChurch,
246                };
247
248            var list = members.ToList();
249            list.AddRange(visitors.ToList());
250
251            // now get rest of family
252            const string PleaseVisit = "No self check-in meetings available";
253            // find a org on campus that allows an older, new visitor to check in to
254            var qv = from o in DbUtil.Db.Organizations
255                     let meetingHours = DbUtil.Db.GetTodaysMeetingHours(o.OrganizationId, thisday)
256					 where o.OrganizationStatusId == CmsData.Codes.OrgStatusCode.Active
257                     where o.CampusId == campus || o.CampusId == null
258                     where o.CanSelfCheckin == true
259                     where o.AllowNonCampusCheckIn == true
260                     from meeting in meetingHours
261                     select new
262                     {
263                         VisitorOrgName = o.OrganizationName,
264                         VisitorOrgId = o.OrganizationId,
265                         VisitorOrgHour = meeting.Hour
266                     };
267            var vo = qv.FirstOrDefault() ?? new
268                                            {
269                                                VisitorOrgName = PleaseVisit,
270                                                VisitorOrgId = 0,
271                                                VisitorOrgHour = (DateTime?)null
272                                            };
273            var otherfamily =
274                from p in DbUtil.Db.People
275                where p.FamilyId == id
276                where p.DeceasedDate == null
277                where !list.Select(a => a.Id).Contains(p.PeopleId)
278                let oldervisitor = (p.CampusId != campus || p.CampusId == null) && p.Age > 12
279                let recreg = p.RecRegs.FirstOrDefault()
280                select new Attendee
281                {
282                    Id = p.PeopleId,
283                    Position = p.PositionInFamilyId,
284                    Name = p.Name,
285                    First = p.FirstName,
286                    PreferredName = p.PreferredName,
287                    Last = p.LastName,
288                    BYear = p.BirthYear,
289                    BMon = p.BirthMonth,
290                    BDay = p.BirthDay,
291                    Class = oldervisitor ? vo.VisitorOrgName : PleaseVisit,
292                    OrgId = oldervisitor ? vo.VisitorOrgId : 0,
293                    Leader = "",
294                    Age = p.Age ?? 0,
295                    Gender = p.Gender.Code,
296                    NumLabels = 1,
297                    Hour = vo.VisitorOrgHour,
298
299                    goesby = p.NickName,
300                    email = p.EmailAddress,
301                    addr = p.Family.AddressLineOne,
302                    zip = p.Family.ZipCode,
303                    home = p.Family.HomePhone,
304                    cell = p.CellPhone,
305                    marital = p.MaritalStatusId,
306                    gender = p.GenderId,
307                    CampusId = p.CampusId,
308                    allergies = recreg.MedicalDescription,
309                    emfriend = recreg.Emcontact,
310                    emphone = recreg.Emphone,
311                    activeother = recreg.ActiveInAnotherChurch ?? false,
312                    parent = recreg.Mname ?? recreg.Fname,
313                    grade = p.Grade,
314                    HasPicture = p.PictureId != null,
315                    Custody = p.CustodyIssue ?? false,
316                    Transport = p.OkTransport ?? false,
317                    RequiresSecurityLabel = false,
318                    church = p.OtherNewChurch,
319                };
320            list.AddRange(otherfamily.ToList());
321            var list2 = list.OrderBy(a => a.Position)
322                .ThenByDescending(a => a.Position == 10 ? a.Gender : "U")
323                .ThenBy(a => a.Age).ToList();
324            return list2;
325        }
326        private bool HasImage(int? imageid)
327        {
328            var q = from i in ImageData.DbUtil.Db.Images
329                    where i.Id == imageid
330                    select i.Length;
331            var len = q.SingleOrDefault();
332            return len > 0;
333        }
334        public List<Attendee> FamilyMembersKiosk(int id, int campus, int thisday)
335        {
336            DbUtil.Db.SetNoLock();
337            var now = Util.Now;
338            var day = now.Date.AddDays(-(int)now.DayOfWeek).AddDays(thisday);
339            // get org members first
340            var members =
341                from om in DbUtil.Db.OrganizationMembers
342                where om.Organization.AllowKioskRegister == true
343                where om.Organization.CampusId == campus || campus == 0
344                where om.Person.FamilyId == id
345                where (om.Pending ?? false) == false
346                where om.Person.DeceasedDate == null
347                let meetingHours = DbUtil.Db.GetTodaysMeetingHours2(om.OrganizationId, thisday, true)
348                let recreg = om.Person.RecRegs.FirstOrDefault()
349                from meeting in meetingHours
350                let CheckedIn = DbUtil.Db.Attends.FirstOrDefault(aa =>
351                    aa.OrganizationId == om.OrganizationId
352                    && aa.PeopleId == om.PeopleId
353                    && aa.MeetingDate == meeting.Hour
354                    && aa.AttendanceFlag == true) != null
355                select new Attendee
356                {
357                    Id = om.PeopleId,
358                    Position = om.Person.PositionInFamilyId,
359                    MemberVisitor = "M",
360                    Name = om.Person.Name,
361                    First = om.Person.FirstName,
362                    PreferredName = om.Person.PreferredName,
363                    Last = om.Person.LastName,
364                    BYear = om.Person.BirthYear,
365                    BMon = om.Person.BirthMonth,
366                    BDay = om.Person.BirthDay,
367                    Class = om.Organization.OrganizationName,
368                    Leader = om.Organization.LeaderName,
369                    OrgId = om.OrganizationId,
370                    Location = om.Organization.Location,
371                    Age = om.Person.Age ?? 0,
372                    Gender = om.Person.Gender.Code,
373                    NumLabels = om.MemberTypeId == MemberTypeCode.Member ?
374                            (om.Organization.NumCheckInLabels ?? 1)
375                            : (om.Organization.NumWorkerCheckInLabels ?? 0),
376                    CheckedIn = CheckedIn,
377                    Hour = meeting.Hour,
378                    goesby = om.Person.NickName,
379                    email = om.Person.EmailAddress,
380                    addr = om.Person.Family.AddressLineOne,
381                    zip = om.Person.Family.ZipCode,
382                    home = om.Person.Family.HomePhone,
383                    cell = om.Person.CellPhone,
384                    marital = om.Person.MaritalStatusId,
385                    gender = om.Person.GenderId,
386                    CampusId = om.Person.CampusId,
387                    allergies = recreg.MedicalDescription,
388                    emfriend = recreg.Emcontact,
389                    emphone = recreg.Emphone,
390                    activeother = recreg.ActiveInAnotherChurch ?? false,
391                    parent = recreg.Mname ?? recreg.Fname,
392                    grade = om.Person.Grade,
393                    church = om.Person.OtherNewChurch,
394                    HasPicture = om.Person.PictureId != null,
395                };
396
397            var list = members.ToList();
398
399            // now get rest of family
400            const string PleaseVisit = "Choose a Class";
401            var otherfamily =
402                from p in DbUtil.Db.People
403                where p.FamilyId == id
404                where p.DeceasedDate == null
405                //where !list.Select(a => a.Id).Contains(p.PeopleId)
406                let recreg = p.RecRegs.FirstOrDefault()
407                select new Attendee
408                {
409                    Id = p.PeopleId,
410                    Position = p.PositionInFamilyId,
411                    Name = p.Name,
412                    First = p.FirstName,
413                    PreferredName = p.PreferredName,
414                    Last = p.LastName,
415                    BYear = p.BirthYear,
416                    BMon = p.BirthMonth,
417                    BDay = p.BirthDay,
418                    Class = PleaseVisit,
419                    OrgId = 0,
420                    Age = p.Age ?? 0,
421                    Gender = p.Gender.Code,
422                    NumLabels = 1,
423
424                    goesby = p.NickName,
425                    email = p.EmailAddress,
426                    addr = p.Family.AddressLineOne,
427                    zip = p.Family.ZipCode,
428                    home = p.Family.HomePhone,
429                    cell = p.CellPhone,
430                    marital = p.MaritalStatusId,
431                    gender = p.GenderId,
432                    CampusId = p.CampusId,
433                    allergies = recreg.MedicalDescription,
434                    emfriend = recreg.Emcontact,
435                    emphone = recreg.Emphone,
436                    activeother = recreg.ActiveInAnotherChurch ?? false,
437                    parent = recreg.Mname ?? recreg.Fname,
438                    grade = p.Grade,
439                    church = p.OtherNewChurch,
440                    HasPicture = p.PictureId != null,
441                };
442            list.AddRange(otherfamily.ToList());
443            var list2 = list.OrderBy(a => a.Position)
444                .ThenByDescending(a => a.Position == 10 ? a.Gender : "U")
445                .ThenBy(a => a.Age).ToList();
446            return list2;
447        }
448        public IEnumerable<Campu> Campuses()
449        {
450            var q = from c in DbUtil.Db.Campus
451                    where c.Organizations.Any(o => o.CanSelfCheckin == true)
452                    orderby c.Id
453                    select c;
454            return q;
455        }
456        public void RecordAttend(int PeopleId, int OrgId, bool Present, int thisday)
457        {
458            var q = from o in DbUtil.Db.Organizations
459                    where o.OrganizationId == OrgId
460                    let p = DbUtil.Db.People.Single(pp => pp.PeopleId == PeopleId)
461                    select new
462                    {
463                        MeetingId = DbUtil.Db.GetTodaysMeetingId(OrgId, thisday),
464                        MeetingTime = DbUtil.Db.GetTodaysMeetingHours(OrgId, thisday).First().Hour,
465                        o.Location,
466                        OrgEntryPoint = o.EntryPointId,
467                        p.EntryPointId,
468                    };
469            var info = q.Single();
470            var meeting = DbUtil.Db.Meetings.SingleOrDefault(m => m.MeetingId == info.MeetingId);
471            if (info.EntryPointId == null)
472            {
473                var p = DbUtil.Db.LoadPersonById(PeopleId);
474                if (info.OrgEntryPoint > 0)
475                    p.EntryPointId = info.OrgEntryPoint;
476            }
477            if (meeting == null)
478            {
479                var acr = (from s in DbUtil.Db.OrgSchedules
480                           where s.OrganizationId == OrgId
481                           where s.SchedTime.Value.TimeOfDay == info.MeetingTime.Value.TimeOfDay
482                           where s.SchedDay == thisday
483                           select s.AttendCreditId).SingleOrDefault();
484                meeting = new CmsData.Meeting
485                {
486                    OrganizationId = OrgId,
487                    MeetingDate = info.MeetingTime,
488                    CreatedDate = Util.Now,
489                    CreatedBy = Util.UserId1,
490                    GroupMeetingFlag = false,
491                    Location = info.Location,
492                    AttendCreditId = acr
493                };
494                DbUtil.Db.Meetings.InsertOnSubmit(meeting);
495                DbUtil.Db.SubmitChanges();
496            }
497            Attend.RecordAttendance(PeopleId, meeting.MeetingId, Present);
498            DbUtil.Db.UpdateMeetingCounters(meeting.MeetingId);
499        }
500        public void JoinUnJoinOrg(int PeopleId, int OrgId, bool Member)
501        {
502            var om = DbUtil.Db.OrganizationMembers.SingleOrDefault(m => m.PeopleId == PeopleId && m.OrganizationId == OrgId);
503            if (om == null && Member)
504                om = OrganizationMember.InsertOrgMembers(DbUtil.Db,
505                    OrgId, PeopleId, MemberTypeCode.Member, DateTime.Now, null, false);
506            else if (om != null && !Member)
507                om.Drop(DbUtil.Db, addToHistory: true);
508            DbUtil.Db.SubmitChanges();
509
510            var org = DbUtil.Db.LoadOrganizationById(OrgId);
511            if (org != null && org.NotifyIds.HasValue())
512            {
513                var p = DbUtil.Db.LoadPersonById(PeopleId);
514                var what = Member ? "joined" : "dropped";
515                //DbUtil.Db.Email(DbUtil.AdminMail, 
516                //    DbUtil.Db.PeopleFromPidString(org.NotifyIds), 
517                //    "cms check-in, {0} class on ".Fmt(what) + DbUtil.Db.CmsHost, 
518                //    "<a href='{0}/Person/Index/{1}'>{2}</a> {3} {4}".Fmt(Util.ServerLink("/Person/Index/" + PeopleId), PeopleId, p.Name, what, org.OrganizationName));
519                DbUtil.LogActivity("cms check-in, {0} class ({1})".Fmt(what, p.PeopleId));
520            }
521        }
522    }
523}