PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsWeb/Areas/OnlineReg/Models/OnlineRegPerson/OnlineRegPersonModel.cs

https://bitbucket.org/mahalowe/bvcms
C# | 380 lines | 356 code | 23 blank | 1 comment | 98 complexity | 691408f8b9d94bfcd828d52387a2255d 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.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Data.Linq;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Xml;
  9. using System.Xml.Linq;
  10. using System.Xml.Schema;
  11. using System.Xml.Serialization;
  12. using CmsData;
  13. using CmsData.API;
  14. using UtilityExtensions;
  15. using System.Runtime.Serialization;
  16. using System.Web.Mvc;
  17. using CmsData.Codes;
  18. namespace CmsWeb.Models
  19. {
  20. [Serializable]
  21. public partial class OnlineRegPersonModel : IXmlSerializable
  22. {
  23. public int? orgid { get; set; }
  24. public int? masterorgid { get; set; }
  25. public int? divid { get; set; }
  26. public int? classid { get; set; }
  27. public int? PeopleId { get; set; }
  28. public bool? Found { get; set; }
  29. public bool IsNew { get; set; }
  30. public bool OtherOK { get; set; }
  31. public bool LoggedIn { get; set; }
  32. public bool IsValidForExisting { get; set; }
  33. public bool ShowAddress { get; set; }
  34. public bool CreatingAccount { get; set; }
  35. public string first { get; set; }
  36. public string middle { get; set; }
  37. public string last { get; set; }
  38. public string suffix { get; set; }
  39. public string dob { get; set; }
  40. public string phone { get; set; }
  41. public string homephone { get; set; }
  42. public string address { get; set; }
  43. public string address2 { get; set; }
  44. public string city { get; set; }
  45. public string state { get; set; }
  46. public string zip { get; set; }
  47. public string country { get; set; }
  48. public int? gender { get; set; }
  49. public int? married { get; set; }
  50. public bool IsFilled { get; set; }
  51. public string shirtsize { get; set; }
  52. public string emcontact { get; set; }
  53. public string emphone { get; set; }
  54. public string insurance { get; set; }
  55. public string policy { get; set; }
  56. public string doctor { get; set; }
  57. public string docphone { get; set; }
  58. public string medical { get; set; }
  59. public string mname { get; set; }
  60. public string fname { get; set; }
  61. public bool memberus { get; set; }
  62. public bool otherchurch { get; set; }
  63. public bool? coaching { get; set; }
  64. public bool? tylenol { get; set; }
  65. public bool? advil { get; set; }
  66. public bool? maalox { get; set; }
  67. public bool? robitussin { get; set; }
  68. public bool? paydeposit { get; set; }
  69. public string request { get; set; }
  70. public string grade { get; set; }
  71. public int? ntickets { get; set; }
  72. public int? whatfamily { get; set; }
  73. public string gradeoption { get; set; }
  74. public bool IsFamily { get; set; }
  75. [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]
  76. public decimal? Suggestedfee { get; set; }
  77. public Dictionary<int, decimal?> FundItem { get; set; }
  78. public List<Dictionary<string, string>> ExtraQuestion { get; set; }
  79. public Dictionary<string, bool?> YesNoQuestion { get; set; }
  80. public List<string> option { get; set; }
  81. public List<string> Checkbox { get; set; }
  82. public Dictionary<string, int?> MenuItem { get; set; }
  83. public void ReadXml(XmlReader reader)
  84. {
  85. var s = reader.ReadOuterXml();
  86. var x = XDocument.Parse(s);
  87. if (x.Root == null) return;
  88. var eqset = 0;
  89. foreach (var e in x.Root.Elements())
  90. {
  91. var name = e.Name.ToString();
  92. switch (name)
  93. {
  94. case "FundItem":
  95. if (FundItem == null)
  96. FundItem = new Dictionary<int, decimal?>();
  97. var fu = e.Attribute("fund");
  98. if (fu != null)
  99. FundItem.Add(fu.Value.ToInt(), e.Value.ToDecimal());
  100. break;
  101. case "ExtraQuestion":
  102. if (ExtraQuestion == null)
  103. ExtraQuestion = new List<Dictionary<string, string>>();
  104. var eqsetattr = e.Attribute("set");
  105. if(eqsetattr != null)
  106. eqset = eqsetattr.Value.ToInt();
  107. if (ExtraQuestion.Count == eqset)
  108. ExtraQuestion.Add(new Dictionary<string, string>());
  109. var eq = e.Attribute("question");
  110. if (eq != null)
  111. ExtraQuestion[eqset].Add(eq.Value, e.Value);
  112. break;
  113. case "YesNoQuestion":
  114. if (YesNoQuestion == null)
  115. YesNoQuestion = new Dictionary<string, bool?>();
  116. var ynq = e.Attribute("question");
  117. if (ynq != null)
  118. YesNoQuestion.Add(ynq.Value, e.Value.ToBool());
  119. break;
  120. case "option":
  121. if (option == null)
  122. option = new List<string>();
  123. option.Add(e.Value);
  124. break;
  125. case "Checkbox":
  126. if (Checkbox == null)
  127. Checkbox = new List<string>();
  128. Checkbox.Add(e.Value);
  129. break;
  130. case "MenuItem":
  131. if (MenuItem == null)
  132. MenuItem = new Dictionary<string, int?>();
  133. var aname = e.Attribute("name");
  134. var number = e.Attribute("number");
  135. if (aname != null && number != null)
  136. MenuItem.Add(aname.Value, number.Value.ToInt());
  137. break;
  138. default:
  139. Util.SetPropertyFromText(this, name, e.Value);
  140. break;
  141. }
  142. }
  143. }
  144. public void WriteXml(XmlWriter writer)
  145. {
  146. var optionsAdded = false;
  147. var checkoxesAdded = false;
  148. var menuitemsAdded = false;
  149. var w = new APIWriter(writer);
  150. foreach (PropertyInfo pi in typeof(OnlineRegPersonModel).GetProperties(BindingFlags.Public | BindingFlags.Instance)
  151. .Where(vv => vv.CanRead && vv.CanWrite))
  152. {
  153. switch (pi.Name)
  154. {
  155. case "FundItem":
  156. if (FundItem != null && FundItem.Count > 0)
  157. foreach (var f in FundItem.Where(ff => ff.Value > 0))
  158. {
  159. w.Start("FundItem");
  160. w.Attr("fund", f.Key);
  161. w.AddText(f.Value.Value.ToString());
  162. w.End();
  163. }
  164. break;
  165. case "ExtraQuestion":
  166. if(ExtraQuestion != null)
  167. for (var i = 0; i < ExtraQuestion.Count; i++)
  168. if (ExtraQuestion[i] != null && ExtraQuestion[i].Count > 0)
  169. foreach (var q in ExtraQuestion[i])
  170. {
  171. w.Start("ExtraQuestion");
  172. w.Attr("set", i);
  173. w.Attr("question", q.Key);
  174. w.AddText(q.Value);
  175. w.End();
  176. }
  177. break;
  178. case "YesNoQuestion":
  179. if (YesNoQuestion != null && YesNoQuestion.Count > 0)
  180. foreach (var q in YesNoQuestion)
  181. {
  182. w.Start("YesNoQuestion");
  183. w.Attr("question", q.Key);
  184. w.AddText(q.Value.ToString());
  185. w.End();
  186. }
  187. break;
  188. case "option":
  189. if (option != null && option.Count > 0 && !optionsAdded)
  190. foreach(var o in option)
  191. w.Add("option", o);
  192. optionsAdded = true;
  193. break;
  194. case "Checkbox":
  195. if (Checkbox != null && Checkbox.Count > 0 && !checkoxesAdded)
  196. foreach (var c in Checkbox)
  197. w.Add("Checkbox", c);
  198. checkoxesAdded = true;
  199. break;
  200. case "MenuItem":
  201. if (MenuItem != null && !menuitemsAdded)
  202. foreach (var kv in MenuItem)
  203. {
  204. w.Start("MenuItem");
  205. w.Attr("name", kv.Key);
  206. w.Attr("number", kv.Value);
  207. w.End();
  208. }
  209. menuitemsAdded = true;
  210. break;
  211. default:
  212. w.Add(pi.Name, pi.GetValue(this, null));
  213. break;
  214. }
  215. }
  216. }
  217. public OnlineRegPersonModel()
  218. {
  219. YesNoQuestion = new Dictionary<string, bool?>();
  220. FundItem = new Dictionary<int, decimal?>();
  221. Parent = HttpContext.Current.Items["OnlineRegModel"] as OnlineRegModel;
  222. }
  223. private void AfterSettingConstructor()
  224. {
  225. if (_setting == null)
  226. return;
  227. var ndd = setting.AskItems.Count(aa => aa.Type == "AskDropdown");
  228. if (ndd > 0 && option == null)
  229. option = new string[ndd].ToList();
  230. var neqsets = setting.AskItems.Count(aa => aa.Type == "AskExtraQuestions");
  231. if (neqsets > 0 && ExtraQuestion == null)
  232. {
  233. ExtraQuestion = new List<Dictionary<string, string>>();
  234. for(var i = 0; i < neqsets; i++)
  235. ExtraQuestion.Add(new Dictionary<string, string>());
  236. }
  237. var nmi = setting.AskItems.Count(aa => aa.Type == "AskMenu");
  238. if (nmi > 0 && MenuItem == null)
  239. MenuItem = new Dictionary<string, int?>();
  240. var ncb = setting.AskItems.Count(aa => aa.Type == "AskCheckboxes");
  241. if (ncb > 0 && Checkbox == null)
  242. Checkbox = new List<string>();
  243. if (!Suggestedfee.HasValue && setting.AskVisible("AskSuggestedFee"))
  244. Suggestedfee = setting.Fee;
  245. }
  246. public OnlineRegModel Parent;
  247. public int? index;
  248. public int Index()
  249. {
  250. if (!index.HasValue)
  251. index = Parent.List.IndexOf(this);
  252. if (index == -1)
  253. index = 0;
  254. return index.Value;
  255. }
  256. public bool LastItem()
  257. {
  258. return Index() == Parent.List.Count - 1;
  259. }
  260. public bool SawExistingAccount;
  261. public bool CannotCreateAccount;
  262. public bool CreatedAccount;
  263. public string email { get; set; }
  264. public string fromemail
  265. {
  266. get { return first + " " + last + " <" + email + ">"; }
  267. }
  268. public int? MenuItemValue(string s)
  269. {
  270. if (MenuItem.ContainsKey(s))
  271. return MenuItem[s];
  272. return null;
  273. }
  274. public decimal? FundItemValue(int n)
  275. {
  276. if (FundItem.ContainsKey(n))
  277. return FundItem[n];
  278. return null;
  279. }
  280. private DateTime _Birthday;
  281. public DateTime? birthday
  282. {
  283. get
  284. {
  285. if (_Birthday == DateTime.MinValue)
  286. Util.BirthDateValid(dob, out _Birthday);
  287. return _Birthday == DateTime.MinValue ? (DateTime?)null : _Birthday;
  288. }
  289. }
  290. public string NotFoundText;
  291. private int count;
  292. private Person _Person;
  293. public Person person
  294. {
  295. get
  296. {
  297. if (_Person == null)
  298. if (PeopleId.HasValue)
  299. {
  300. _Person = DbUtil.Db.LoadPersonById(PeopleId.Value);
  301. count = 1;
  302. }
  303. else
  304. {
  305. //_Person = SearchPeopleModel.FindPerson(first, last, birthday, email, phone, out count);
  306. var list = DbUtil.Db.FindPerson(first, last, birthday, email, phone).ToList();
  307. count = list.Count;
  308. if (count == 1)
  309. _Person = DbUtil.Db.LoadPersonById(list[0].PeopleId.Value);
  310. if (_Person != null)
  311. PeopleId = _Person.PeopleId;
  312. }
  313. return _Person;
  314. }
  315. }
  316. public void AddPerson(Person p, int entrypoint)
  317. {
  318. Family f;
  319. if (p == null)
  320. f = new Family
  321. {
  322. AddressLineOne = address,
  323. AddressLineTwo = address2,
  324. CityName = city,
  325. StateCode = state,
  326. ZipCode = zip,
  327. CountryName = country,
  328. HomePhone = homephone,
  329. };
  330. else
  331. f = p.Family;
  332. _Person = Person.Add(f, PositionInFamily.Child,
  333. null, first.Trim(), null, last.Trim(), dob, married == 20, gender ?? 0,
  334. OriginCode.Enrollment, entrypoint);
  335. person.EmailAddress = email.Trim();
  336. person.SuffixCode = suffix;
  337. person.MiddleName = middle;
  338. person.CampusId = DbUtil.Db.Setting("DefaultCampusId", "").ToInt2();
  339. if (person.Age >= 18)
  340. person.PositionInFamilyId = PositionInFamily.PrimaryAdult;
  341. person.CellPhone = phone.GetDigits();
  342. DbUtil.Db.SubmitChanges();
  343. DbUtil.Db.Refresh(RefreshMode.OverwriteCurrentValues, person);
  344. PeopleId = person.PeopleId;
  345. }
  346. public bool IsCreateAccount()
  347. {
  348. if (org != null)
  349. return org.RegistrationTypeId == RegistrationTypeCode.CreateAccount;
  350. return false;
  351. }
  352. public XmlSchema GetSchema()
  353. {
  354. throw new System.NotImplementedException("The method or operation is not implemented.");
  355. }
  356. }
  357. }