PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsWeb/Areas/Dialog/Controllers/SearchAddController.cs

https://bitbucket.org/mahalowe/bvcms
C# | 474 lines | 453 code | 8 blank | 13 comment | 95 complexity | d90e7b902fbe9dabd75d6723b6eb27de MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Mail;
  6. using System.Runtime.Serialization;
  7. using System.Text;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. using System.Web.Routing;
  11. using CmsData;
  12. using CmsWeb.Models;
  13. using UtilityExtensions;
  14. using System.Text.RegularExpressions;
  15. using System.Data.Linq;
  16. using CmsData.Codes;
  17. namespace CmsWeb.Areas.Dialog.Controllers
  18. {
  19. public class SearchAddController : CmsStaffController
  20. {
  21. public ActionResult Index(int? id, string type, string from)
  22. {
  23. var m = new SearchModel { typeid = id, type = type, from = from };
  24. #if DEBUG
  25. #endif
  26. return View(m);
  27. }
  28. [AcceptVerbs(HttpVerbs.Post)]
  29. public ActionResult Results(SearchModel m)
  30. {
  31. DbUtil.Db.SetNoLock();
  32. return View(m);
  33. }
  34. [AcceptVerbs(HttpVerbs.Post)]
  35. public ActionResult ResultsFamily(SearchModel m)
  36. {
  37. DbUtil.Db.SetNoLock();
  38. return View(m);
  39. }
  40. [AcceptVerbs(HttpVerbs.Post)]
  41. public ActionResult SearchPerson(SearchModel m)
  42. {
  43. return View(m);
  44. }
  45. [AcceptVerbs(HttpVerbs.Post)]
  46. public ActionResult SearchFamily(SearchModel m)
  47. {
  48. m.dob = null;
  49. var a = m.name.SplitStr(" ");
  50. m.name = a[a.Length - 1];
  51. return View(m);
  52. }
  53. [AcceptVerbs(HttpVerbs.Post)]
  54. public ActionResult SearchCancel(SearchModel m)
  55. {
  56. if (m.List.Count > 0)
  57. return View("List", m);
  58. return Complete("0", m);
  59. }
  60. [AcceptVerbs(HttpVerbs.Post)]
  61. public ActionResult SearchFamilyCancel(SearchModel m)
  62. {
  63. return View("SearchPerson", m);
  64. }
  65. [AcceptVerbs(HttpVerbs.Post)]
  66. public ActionResult PersonCancel(int id, SearchModel m)
  67. {
  68. m.List.RemoveAt(id);
  69. if (m.List.Count > 0)
  70. return View("List", m);
  71. return View("SearchPerson", m);
  72. }
  73. [AcceptVerbs(HttpVerbs.Post)]
  74. public ActionResult Select(int id, SearchModel m)
  75. {
  76. if (m.List.AsEnumerable().Any(li => li.PeopleId == id))
  77. return View("List", m);
  78. var p = DbUtil.Db.LoadPersonById(id);
  79. var s = new SearchPersonModel
  80. {
  81. PeopleId = id,
  82. FamilyId = m.type == "family" ? m.typeid.Value : p.FamilyId,
  83. first = p.FirstName,
  84. goesby = p.NickName,
  85. last = p.LastName,
  86. marital = p.MaritalStatusId,
  87. email = p.EmailAddress,
  88. suffix = p.SuffixCode,
  89. title = p.TitleCode,
  90. dob = p.DOB,
  91. gender = p.GenderId,
  92. phone = p.CellPhone,
  93. };
  94. s.LoadFamily();
  95. m.List.Add(s);
  96. if (m.OnlyOne)
  97. return Complete(m.typeid.ToString(), m);
  98. return View("List", m);
  99. }
  100. [AcceptVerbs(HttpVerbs.Post)]
  101. public ActionResult AddNewFamily(SearchModel m)
  102. {
  103. var p = m.List[m.List.Count - 1];
  104. p.ValidateModelForNew(ModelState, true);
  105. if (!ModelState.IsValid)
  106. return View("FormFull", m);
  107. return View("List", m);
  108. }
  109. [AcceptVerbs(HttpVerbs.Post)]
  110. public ActionResult AddToFamily(SearchModel m)
  111. {
  112. var p = m.List[m.List.Count - 1];
  113. p.ValidateModelForNew(ModelState, false);
  114. if (!ModelState.IsValid)
  115. return View("FormAbbreviated", m);
  116. return View("List", m);
  117. }
  118. private SearchPersonModel NewPerson(int FamilyId, SearchModel m)
  119. {
  120. var p = new SearchPersonModel
  121. {
  122. FamilyId = FamilyId,
  123. index = m.List.Count,
  124. gender = 99,
  125. marital = 99,
  126. };
  127. #if DEBUG
  128. //p.title = "Mr.";
  129. //p.first = "David5";
  130. //p.last = "Carroll";
  131. //p.gender = 1;
  132. //p.marital = 20;
  133. //p.dob = "5/30/52";
  134. //p.email = "david@bvcms.com";
  135. //p.phone = "9014890611";
  136. #endif
  137. m.List.Add(p);
  138. return p;
  139. }
  140. [AcceptVerbs(HttpVerbs.Post)]
  141. public ActionResult FormAbbreviated(int id, SearchModel m)
  142. {
  143. var p = NewPerson(id, m);
  144. if (id < 0)
  145. {
  146. var f = m.List.FirstOrDefault(fm => fm.FamilyId == id);
  147. p.address = f.address;
  148. p.city = f.city;
  149. p.state = f.state;
  150. p.zip = f.zip;
  151. p.homephone = f.homephone;
  152. }
  153. else
  154. p.LoadFamily();
  155. return View(m);
  156. }
  157. [AcceptVerbs(HttpVerbs.Post)]
  158. public ActionResult FormFull(SearchModel m)
  159. {
  160. int id = 0;
  161. if (m.List.Count > 0)
  162. id = m.List.Min(i => i.FamilyId) - 1;
  163. if (id >= 0)
  164. id = -1;
  165. var p = NewPerson(id, m);
  166. #if DEBUG
  167. //p.address = "235 Riveredge Cv";
  168. //p.city = "Cordova";
  169. //p.state = "TN";
  170. //p.zip = "38018";
  171. //p.homephone = "9017581862";
  172. #endif
  173. return View(m);
  174. }
  175. [AcceptVerbs(HttpVerbs.Post)]
  176. public ActionResult Complete(string id, SearchModel m)
  177. {
  178. var iid = id.ToInt();
  179. switch (m.type)
  180. {
  181. case "addpeople":
  182. return AddPeople(m);
  183. case "addtotag":
  184. return AddPeopleToTag(id, m);
  185. case "family":
  186. return AddFamilyMembers(iid, m, false);
  187. case "relatedfamily":
  188. return AddRelatedFamilys(iid, m, false);
  189. case "org":
  190. return AddOrgMembers(iid, m, false);
  191. case "pending":
  192. return AddOrgMembers(iid, m, true);
  193. case "visitor":
  194. return AddVisitors(iid, m);
  195. case "registered":
  196. return AddRegistered(iid, m);
  197. case "contactee":
  198. return AddContactees(iid, m);
  199. case "contactor":
  200. return AddContactors(iid, m);
  201. case "contributor":
  202. return AddContributor(iid, m);
  203. case "taskdelegate":
  204. if (m.List.Count > 0)
  205. return Json(new { close = true, how = "addselected", url="/Task/Delegate/", pid = m.List[0].PeopleId });
  206. break;
  207. case "taskdelegate2":
  208. if (m.List.Count > 0)
  209. return Json(new { close = true, how = "addselected2", url = "/Task/Action/", pid = m.List[0].PeopleId });
  210. break;
  211. case "taskabout":
  212. if (m.List.Count > 0)
  213. return Json(new { close = true, how = "addselected", url = "/Task/ChangeAbout/", pid = m.List[0].PeopleId });
  214. break;
  215. case "mergeto":
  216. if (m.List.Count > 0)
  217. return Json(new { close = true, how = "addselected", pid = m.List[0].PeopleId });
  218. break;
  219. case "taskowner":
  220. if (m.List.Count > 0)
  221. return Json(new { close = true, how = "addselected", url = "/Task/ChangeOwner/", pid = m.List[0].PeopleId });
  222. break;
  223. }
  224. return Json(new { close = true });
  225. }
  226. private JsonResult AddContactees(int id, SearchModel m)
  227. {
  228. if (id > 0)
  229. {
  230. var c = DbUtil.Db.Contacts.Single(ct => ct.ContactId == id);
  231. foreach (var p in m.List)
  232. {
  233. AddPerson(p, m.List, OriginCode.Visit, 0);
  234. var ctee = c.contactees.SingleOrDefault(ct =>
  235. ct.ContactId == id && ct.PeopleId == p.person.PeopleId);
  236. if (ctee == null)
  237. {
  238. ctee = new Contactee
  239. {
  240. ContactId = id,
  241. PeopleId = p.person.PeopleId,
  242. };
  243. c.contactees.Add(ctee);
  244. }
  245. }
  246. DbUtil.Db.SubmitChanges();
  247. }
  248. return Json(new { close = true, how = "addselected" });
  249. }
  250. private JsonResult AddContactors(int id, SearchModel m)
  251. {
  252. if (id > 0)
  253. {
  254. var c = DbUtil.Db.Contacts.SingleOrDefault(ct => ct.ContactId == id);
  255. if (c == null)
  256. return Json(new { close = true, how = "CloseAddDialog" });
  257. foreach (var p in m.List)
  258. {
  259. AddPerson(p, m.List, 0, 0);
  260. var ctor = c.contactsMakers.SingleOrDefault(ct =>
  261. ct.ContactId == id && ct.PeopleId == p.person.PeopleId);
  262. if (ctor == null)
  263. {
  264. ctor = new Contactor
  265. {
  266. ContactId = id,
  267. PeopleId = p.person.PeopleId,
  268. };
  269. c.contactsMakers.Add(ctor);
  270. }
  271. }
  272. DbUtil.Db.SubmitChanges();
  273. }
  274. return Json(new { close = true, how = "addselected" });
  275. }
  276. private JsonResult AddFamilyMembers(int id, SearchModel m, bool pending)
  277. {
  278. if (id > 0)
  279. {
  280. var f = DbUtil.Db.Families.Single(fa => fa.FamilyId == id);
  281. foreach (var i in m.List)
  282. {
  283. var isnew = i.IsNew;
  284. AddPerson(i, m.List, OriginCode.NewFamilyMember, 0);
  285. if (!isnew)
  286. {
  287. var fm = f.People.SingleOrDefault(fa => fa.PeopleId == i.person.PeopleId);
  288. if (fm != null)
  289. continue; // already a member of this family
  290. if (i.person.Age < 18)
  291. i.person.PositionInFamilyId = PositionInFamily.Child;
  292. else if (i.family.People.Count(per =>
  293. per.PositionInFamilyId == PositionInFamily.PrimaryAdult)
  294. < 2)
  295. i.person.PositionInFamilyId = PositionInFamily.PrimaryAdult;
  296. else
  297. i.person.PositionInFamilyId = PositionInFamily.SecondaryAdult;
  298. i.family.People.Add(i.person); // add selected person to target family
  299. }
  300. }
  301. DbUtil.Db.SubmitChanges();
  302. }
  303. return Json(new { close = true, how = "addselected" });
  304. }
  305. private JsonResult AddRelatedFamilys(int id, SearchModel m, bool pending)
  306. {
  307. if (id > 0)
  308. {
  309. foreach (var p in m.List)
  310. {
  311. AddPerson(p, m.List, OriginCode.NewFamilyMember, 0);
  312. SearchModel.AddRelatedFamily(id, p.PeopleId.Value);
  313. }
  314. DbUtil.Db.SubmitChanges();
  315. }
  316. return Json(new { close = true, how = "addselected" });
  317. }
  318. private JsonResult AddPeople(SearchModel m)
  319. {
  320. foreach (var p in m.List)
  321. AddPerson(p, m.List, 0, 0);
  322. DbUtil.Db.SubmitChanges();
  323. return Json(new { close = true, how = "CloseAddDialog" });
  324. }
  325. private JsonResult AddOrgMembers(int id, SearchModel m, bool pending)
  326. {
  327. string message = null;
  328. if (id > 0)
  329. {
  330. var org = DbUtil.Db.LoadOrganizationById(id);
  331. if (pending == false && m.List.Count == 1 && org.AllowAttendOverlap != true)
  332. {
  333. var om = DbUtil.Db.OrganizationMembers.FirstOrDefault(mm =>
  334. mm.OrganizationId != id
  335. && mm.MemberTypeId != 230 // inactive
  336. && mm.MemberTypeId != 500 // inservice
  337. && mm.Organization.AllowAttendOverlap != true
  338. && mm.PeopleId == m.List[0].PeopleId
  339. && mm.Organization.OrgSchedules.Any(ss =>
  340. DbUtil.Db.OrgSchedules.Any(os =>
  341. os.OrganizationId == id
  342. && os.ScheduleId == ss.ScheduleId)));
  343. if (om != null)
  344. {
  345. message = "Already a member of {0} (orgid) with same schedule".Fmt(om.OrganizationId);
  346. return Json(new { close = true, how = "CloseAddDialog", message = message });
  347. }
  348. }
  349. foreach (var p in m.List)
  350. {
  351. AddPerson(p, m.List, OriginCode.Enrollment, org.EntryPointId ?? 0);
  352. OrganizationMember.InsertOrgMembers(DbUtil.Db,
  353. id, p.PeopleId.Value, 220, Util.Now, null, pending);
  354. }
  355. DbUtil.Db.SubmitChanges();
  356. DbUtil.Db.UpdateMainFellowship(id);
  357. }
  358. return Json(new { close = true, how = "rebindgrids", message = message });
  359. }
  360. private JsonResult AddContributor(int id, SearchModel m)
  361. {
  362. if (id > 0)
  363. {
  364. var p = m.List[0];
  365. var c = DbUtil.Db.Contributions.Single(cc => cc.ContributionId == id);
  366. AddPerson(p, m.List, OriginCode.Contribution, 0);
  367. c.PeopleId = p.PeopleId;
  368. if (c.BankAccount.HasValue())
  369. {
  370. var ci = DbUtil.Db.CardIdentifiers.SingleOrDefault(k => k.Id == c.BankAccount);
  371. if (ci == null)
  372. {
  373. ci = new CardIdentifier
  374. {
  375. Id = c.BankAccount,
  376. CreatedOn = Util.Now,
  377. };
  378. DbUtil.Db.CardIdentifiers.InsertOnSubmit(ci);
  379. }
  380. ci.PeopleId = p.PeopleId;
  381. }
  382. DbUtil.Db.SubmitChanges();
  383. return Json(new { close = true, how = "addselected", cid = id, pid = p.PeopleId, name = p.person.Name2 });
  384. }
  385. return Json(new { close = true, how = "addselected" });
  386. }
  387. private JsonResult AddPeopleToTag(string id, SearchModel m)
  388. {
  389. if (id.HasValue())
  390. {
  391. foreach (var p in m.List)
  392. {
  393. AddPerson(p, m.List, 0, null);
  394. Person.Tag(DbUtil.Db, p.person.PeopleId, id, Util2.CurrentTagOwnerId, DbUtil.TagTypeId_Personal);
  395. }
  396. DbUtil.Db.SubmitChanges();
  397. }
  398. return Json(new { close = true, how = "addselected" });
  399. }
  400. private JsonResult AddVisitors(int id, SearchModel m)
  401. {
  402. var sb = new StringBuilder();
  403. if (id > 0)
  404. {
  405. var meeting = DbUtil.Db.Meetings.SingleOrDefault(me => me.MeetingId == id);
  406. foreach (var p in m.List)
  407. {
  408. var isnew = p.IsNew;
  409. AddPerson(p, m.List, OriginCode.Visit, meeting.Organization.EntryPointId ?? 0);
  410. if (isnew)
  411. p.person.UpdateValue("CampusId", meeting.Organization.CampusId);
  412. var err = Attend.RecordAttendance(p.PeopleId.Value, id, true);
  413. if (err != "ok")
  414. sb.AppendLine(err);
  415. }
  416. DbUtil.Db.SubmitChanges();
  417. DbUtil.Db.UpdateMeetingCounters(meeting.MeetingId);
  418. }
  419. return Json(new { close = true, how = "addselected", error = sb.ToString() });
  420. }
  421. private JsonResult AddRegistered(int id, SearchModel m)
  422. {
  423. if (id > 0)
  424. {
  425. var meeting = DbUtil.Db.Meetings.SingleOrDefault(me => me.MeetingId == id);
  426. foreach (var p in m.List)
  427. {
  428. var isnew = p.IsNew;
  429. AddPerson(p, m.List, OriginCode.Visit, meeting.Organization.EntryPointId ?? 0);
  430. if (isnew)
  431. p.person.CampusId = meeting.Organization.CampusId;
  432. Attend.MarkRegistered(DbUtil.Db, p.PeopleId.Value, id, 1);
  433. }
  434. DbUtil.Db.SubmitChanges();
  435. DbUtil.Db.UpdateMeetingCounters(meeting.MeetingId);
  436. }
  437. return Json(new { close = true, how = "addselected" });
  438. }
  439. private void AddPerson(SearchPersonModel p, IList<SearchPersonModel> list, int Origin, int? EntryPoint)
  440. {
  441. if (p.IsNew)
  442. p.AddPerson(Origin, EntryPoint);
  443. else
  444. {
  445. if (EntryPoint != 0 &&
  446. (!p.person.EntryPointId.HasValue || p.person.EntryPointId == 0))
  447. p.person.EntryPointId = EntryPoint;
  448. if (Origin != 0 &&
  449. (!p.person.OriginId.HasValue || p.person.OriginId == 0))
  450. p.person.OriginId = Origin;
  451. DbUtil.Db.SubmitChanges();
  452. }
  453. if (p.FamilyId < 0) // fix up new family pointers
  454. {
  455. var q = from m in list
  456. where m.FamilyId == p.FamilyId
  457. select m;
  458. var list2 = q.ToList();
  459. foreach (var m in list2)
  460. m.FamilyId = p.person.FamilyId;
  461. }
  462. Util2.CurrentPeopleId = p.person.PeopleId;
  463. Session["ActivePerson"] = p.person.Name;
  464. }
  465. }
  466. }