PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsWeb/Areas/Main/Models/Other/CouponModel.cs

https://bitbucket.org/davcar/bvcms/
C# | 287 lines | 268 code | 13 blank | 6 comment | 67 complexity | 9d6e3a862caad0bc81a5d072f9ddaaad MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. /* Author: David Carroll
  2. * Copyright (c) 2008, 2009 Bellevue Baptist Church
  3. * Licensed under the GNU General Public License (GPL v2)
  4. * you may not use this code except in compliance with the License.
  5. * You may obtain a copy of the License at http://bvcms.codeplex.com/license
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Data.Linq;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. using System.Web.Routing;
  14. using CmsData.Registration;
  15. using UtilityExtensions;
  16. using System.Text;
  17. using CmsData;
  18. using System.Data.Linq.SqlClient;
  19. using CmsData.Codes;
  20. namespace CmsWeb.Models
  21. {
  22. public class CouponModel
  23. {
  24. public string regid { get; set; }
  25. public decimal amount { get; set; }
  26. public string name { get; set; }
  27. public string couponcode { get; set; }
  28. public int useridfilter { get; set; }
  29. public string regidfilter { get; set; }
  30. public string usedfilter { get; set; }
  31. public string date { get; set; }
  32. public string Registration()
  33. {
  34. return OnlineRegs().Single(r => r.Value == regid).Text;
  35. }
  36. public IEnumerable<CouponInfo> Coupons()
  37. {
  38. var roles = DbUtil.Db.CurrentUser.UserRoles.Select(uu => uu.Role.RoleName).ToArray();
  39. var q = from c in DbUtil.Db.Coupons
  40. let o = c.Organization
  41. where o.LimitToRole == null || roles.Contains(o.LimitToRole)
  42. where c.DivOrg == regidfilter || regidfilter == "0" || regidfilter == null
  43. where c.UserId == useridfilter || useridfilter == 0
  44. select c;
  45. switch (usedfilter)
  46. {
  47. case "Used":
  48. q = q.Where(c => c.Used != null && c.Canceled == null);
  49. break;
  50. case "UnUsed":
  51. q = q.Where(c => c.Used == null && c.Canceled == null);
  52. break;
  53. case "Canceled":
  54. q = q.Where(c => c.Canceled != null);
  55. break;
  56. }
  57. if (name.HasValue())
  58. q = q.Where(c => c.Name.Contains(name) || c.Person.Name.Contains(name));
  59. if (date.HasValue())
  60. {
  61. DateTime bd;
  62. if (DateTime.TryParse(date, out bd))
  63. q = q.Where(c => c.Created.Date == bd);
  64. }
  65. var q2 = from c in q
  66. orderby c.Created descending
  67. select new CouponInfo
  68. {
  69. Amount = c.Amount,
  70. Canceled = c.Canceled,
  71. Code = c.Id,
  72. Created = c.Created,
  73. OrgDivName = c.OrgId != null ? c.Organization.OrganizationName : c.Division.Name,
  74. Used = c.Used,
  75. PeopleId = c.PeopleId,
  76. Name = c.Name,
  77. Person = c.Person.Name,
  78. UserId = c.UserId,
  79. UserName = c.User.Name,
  80. RegAmt = c.RegAmount
  81. };
  82. return q2.Take(200);
  83. }
  84. public IEnumerable<CouponInfo2> Coupons2()
  85. {
  86. var roles = DbUtil.Db.CurrentUser.UserRoles.Select(uu => uu.Role.RoleName).ToArray();
  87. var q = from c in DbUtil.Db.Coupons
  88. let o = c.Organization
  89. where o.LimitToRole == null || roles.Contains(o.LimitToRole)
  90. where c.DivOrg == regidfilter || regidfilter == "0" || regidfilter == null
  91. where c.UserId == useridfilter || useridfilter == 0
  92. select c;
  93. switch (usedfilter)
  94. {
  95. case "Used":
  96. q = q.Where(c => c.Used != null && c.Canceled == null);
  97. break;
  98. case "UnUsed":
  99. q = q.Where(c => c.Used == null && c.Canceled == null);
  100. break;
  101. case "Canceled":
  102. q = q.Where(c => c.Canceled != null);
  103. break;
  104. }
  105. if (name.HasValue())
  106. q = q.Where(c => c.Name.Contains(name) || c.Person.Name.Contains(name));
  107. if (date.HasValue())
  108. {
  109. DateTime bd;
  110. if (DateTime.TryParse(date, out bd))
  111. q = q.Where(c => c.Created.Date == bd);
  112. }
  113. var q2 = from c in q
  114. orderby c.Created descending
  115. select new CouponInfo2
  116. {
  117. Amount = c.Amount ?? 0,
  118. Canceled = c.Canceled ?? DateTime.Parse("1/1/80"),
  119. Code = c.Id,
  120. Created = c.Created,
  121. OrgDivName = c.OrgId != null ? c.Organization.OrganizationName : c.Division.Name,
  122. Used = c.Used ?? DateTime.Parse("1/1/80"),
  123. PeopleId = c.PeopleId ?? 0,
  124. Name = c.Name,
  125. Person = c.Person.Name,
  126. UserId = c.UserId ?? 0,
  127. UserName = c.User.Name,
  128. RegAmt = c.RegAmount ?? 0
  129. };
  130. return q2.Take(200);
  131. }
  132. public List<SelectListItem> OnlineRegs()
  133. {
  134. var roles = DbUtil.Db.CurrentUser.UserRoles.Select(uu => uu.Role.RoleName).ToArray();
  135. var organizations = from o in DbUtil.Db.Organizations
  136. where o.LimitToRole == null || roles.Contains(o.LimitToRole)
  137. select o;
  138. var orgregtypes = new int[]
  139. {
  140. RegistrationTypeCode.JoinOrganization,
  141. RegistrationTypeCode.UserSelectsOrganization,
  142. RegistrationTypeCode.ComputeOrganizationByAge,
  143. RegistrationTypeCode.UserSelectsOrganization2,
  144. RegistrationTypeCode.ComputeOrganizationByAge2,
  145. };
  146. var q = (from o in organizations
  147. where orgregtypes.Contains(o.RegistrationTypeId.Value)
  148. where (o.ClassFilled ?? false) != true
  149. where (o.RegistrationClosed ?? false) == false
  150. select new { DivisionName = o.Division.Name, o.OrganizationName, o.RegSetting, o.OrganizationId }).ToList();
  151. var qq = from i in q
  152. let os = new Settings(i.RegSetting, DbUtil.Db, i.OrganizationId)
  153. where os.Fee > 0
  154. || os.AskItems.Where(aa => aa.Type == "AskDropdown").Any(aa => ((AskDropdown)aa).list.Any(dd => dd.Fee > 0))
  155. || os.AskItems.Where(aa => aa.Type == "AskCheckboxes").Any(aa => ((AskCheckboxes)aa).list.Any(dd => dd.Fee > 0))
  156. select new SelectListItem
  157. {
  158. Text = i.DivisionName + ":" + i.OrganizationName,
  159. Value = "org." + i.OrganizationId
  160. };
  161. var list = qq.OrderBy(n => n.Text).ToList();
  162. list.Insert(0, new SelectListItem { Text = "(not specified)", Value = "0" });
  163. return list;
  164. }
  165. public List<SelectListItem> Users()
  166. {
  167. var q = from c in DbUtil.Db.Coupons
  168. where c.UserId != null
  169. group c by c.UserId into g
  170. select new SelectListItem
  171. {
  172. Value = g.Key.ToString(),
  173. Text = g.First().User.Name,
  174. };
  175. var list = q.ToList();
  176. list.Insert(0, new SelectListItem { Text = "(not specified)", Value = "0" });
  177. return list;
  178. }
  179. public List<SelectListItem> CouponStatus()
  180. {
  181. return new List<SelectListItem>
  182. {
  183. new SelectListItem { Text = "(not specified)" },
  184. new SelectListItem { Text = "Used" },
  185. new SelectListItem { Text = "UnUsed" },
  186. new SelectListItem { Text = "Canceled" },
  187. };
  188. }
  189. public static bool IsExisting(string code)
  190. {
  191. bool existing = DbUtil.Db.Coupons.Any(cp => cp.Id == code && cp.Used == null && cp.Canceled == null);
  192. return existing;
  193. }
  194. public Coupon CreateCoupon()
  195. {
  196. string code = couponcode;
  197. if (!couponcode.HasValue())
  198. {
  199. do
  200. {
  201. code = Util.RandomPassword(12);
  202. }
  203. while (IsExisting(code));
  204. }
  205. var c = new Coupon
  206. {
  207. Id = code,
  208. Created = DateTime.Now,
  209. Amount = amount,
  210. Name = name,
  211. UserId = Util.UserId,
  212. };
  213. SetDivOrgIds(c);
  214. DbUtil.Db.Coupons.InsertOnSubmit(c);
  215. DbUtil.Db.SubmitChanges();
  216. couponcode = Util.fmtcoupon(c.Id);
  217. return c;
  218. }
  219. private void SetDivOrgIds(Coupon c)
  220. {
  221. if (regid.HasValue())
  222. {
  223. var a = regid.Split('.');
  224. switch (a[0])
  225. {
  226. case "org":
  227. c.OrgId = a[1].ToInt();
  228. break;
  229. case "div":
  230. c.DivId = a[1].ToInt();
  231. break;
  232. }
  233. }
  234. }
  235. public class CouponInfo
  236. {
  237. public string Code { get; set; }
  238. public string Coupon
  239. {
  240. get { return Util.fmtcoupon(Code); }
  241. }
  242. public string OrgDivName { get; set; }
  243. public DateTime Created { get; set; }
  244. public DateTime? Used { get; set; }
  245. public DateTime? Canceled { get; set; }
  246. public decimal? Amount { get; set; }
  247. public decimal? RegAmt { get; set; }
  248. public int? PeopleId { get; set; }
  249. public string Name { get; set; }
  250. public string Person { get; set; }
  251. public int? UserId { get; set; }
  252. public string UserName { get; set; }
  253. }
  254. public class CouponInfo2
  255. {
  256. public string Code;
  257. public string Coupon
  258. {
  259. get { return Code.Insert(8, " ").Insert(4, " "); }
  260. }
  261. public string OrgDivName { get; set; }
  262. public DateTime Created { get; set; }
  263. public DateTime Used { get; set; }
  264. public DateTime Canceled { get; set; }
  265. public decimal Amount { get; set; }
  266. public decimal RegAmt { get; set; }
  267. public int PeopleId { get; set; }
  268. public string Name { get; set; }
  269. public string Person { get; set; }
  270. public int UserId { get; set; }
  271. public string UserName { get; set; }
  272. }
  273. }
  274. }