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