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