API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WeekRepository.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using UnivateProperties_API.Containers.Timeshare;
  10. using UnivateProperties_API.Containers.Timeshare.Detailed;
  11. using UnivateProperties_API.Containers.Users;
  12. using UnivateProperties_API.Context;
  13. using UnivateProperties_API.Helpers;
  14. using UnivateProperties_API.Model;
  15. using UnivateProperties_API.Model.Communication;
  16. using UnivateProperties_API.Model.Region;
  17. using UnivateProperties_API.Model.Timeshare;
  18. using UnivateProperties_API.Repository.Communication;
  19. using UnivateProperties_API.Repository.Region;
  20. using UnivateProperties_API.Repository.Users;
  21. namespace UnivateProperties_API.Repository.Timeshare
  22. {
  23. public class WeekRepository : IRepository<TimeshareWeek>
  24. {
  25. private readonly DataContext _dbContext;
  26. public WeekRepository(DataContext dbContext)
  27. {
  28. _dbContext = dbContext;
  29. }
  30. public List<TimeshareWeek> Get(Func<TimeshareWeek, bool> where)
  31. {
  32. return _dbContext.Weeks.Where(where).ToList();
  33. }
  34. public List<TimeshareWeek> GetAll()
  35. {
  36. return _dbContext.Weeks.ToList();
  37. }
  38. public TimeshareWeek GetDetailed(Func<TimeshareWeek, bool> first)
  39. {
  40. var item = _dbContext.Weeks.FirstOrDefault(first);
  41. item = GetDetailedWeek(item);
  42. return item;
  43. }
  44. public DetailedWeekDto GetMyDetailed(Func<TimeshareWeek, bool> first)
  45. {
  46. var item = GetDetailed(first);
  47. if(item != null)
  48. {
  49. return new DetailedWeekDto(item);
  50. }
  51. return null;
  52. }
  53. public List<WeekDto> GetDtoListAll()
  54. {
  55. List<WeekDto> list = new List<WeekDto>();
  56. foreach (var item in GetDetailedAll())
  57. {
  58. list.Add(new WeekDto(item));
  59. }
  60. foreach (var item in GetTenderWeeks())
  61. {
  62. list.Add(item);
  63. }
  64. return list;
  65. }
  66. public List<WeekDto> GetMyWeeks(int userId)
  67. {
  68. List<WeekDto> list = new List<WeekDto>();
  69. UserRepository userRepository = new UserRepository(_dbContext);
  70. var user = userRepository.Get(x => x.Id == userId).FirstOrDefault();
  71. if (user.IsUserInRole(Role.SuperAdmin))
  72. {
  73. foreach (var item in GetDetailedAll())
  74. {
  75. list.Add(new WeekDto(item));
  76. }
  77. }
  78. else if (user.IsUserInRole(Role.Agency))
  79. {
  80. var agent = _dbContext.Agents.FirstOrDefault(x => x.UserId == userId);
  81. if(agent != null)
  82. {
  83. foreach (var item in GetDetailedAll().Where(x => x.AgencyId == agent.AgencyId))
  84. {
  85. list.Add(new WeekDto(item));
  86. }
  87. }
  88. }
  89. else if (user.IsUserInRole(Role.Agent) || user.IsUserInRole(Role.ManagingAgent))
  90. {
  91. var agent = _dbContext.Agents.FirstOrDefault(x => x.UserId == userId);
  92. if(agent != null)
  93. {
  94. foreach (var item in GetDetailedAll().Where(x => x.AgentId == agent.Id))
  95. {
  96. list.Add(new WeekDto(item));
  97. }
  98. }
  99. }
  100. else
  101. {
  102. var individual = _dbContext.Individuals.FirstOrDefault(x => x.UserId == userId);
  103. if(individual != null)
  104. {
  105. foreach (var item in GetDetailedAll().Where(x => x.OwnerId == userId))
  106. {
  107. list.Add(new WeekDto(item));
  108. }
  109. }
  110. }
  111. return list;
  112. }
  113. public List<RegionDto> GetAvailResort()
  114. {
  115. List<RegionDto> list = new List<RegionDto>();
  116. var allItems = GetDtoListAll();
  117. foreach (var item in allItems)
  118. {
  119. if (item.Region != null)
  120. {
  121. if (!list.Any(x => x.RegionCode == item.Region.RegionCode))
  122. {
  123. list.Add(new RegionDto(item.Region.RegionCode, item.Region.RegionName));
  124. }
  125. foreach (var i in list.Where(x => x.RegionCode == item.Region.RegionCode))
  126. {
  127. i.TryAddResort(item.Resort.ResortCode, item.Resort.ResortName);
  128. }
  129. }
  130. }
  131. list = list.OrderBy(x => x.RegionName).ToList();
  132. foreach (var region in list)
  133. {
  134. region.OrderResorts();
  135. foreach (var resort in region.Resorts)
  136. {
  137. resort.Available = allItems.Count(x => x.Resort.ResortCode == resort.ResortCode);
  138. }
  139. }
  140. return list;
  141. }
  142. public List<TimeshareWeek> GetDetailedAll()
  143. {
  144. var list = GetAll();
  145. List<TimeshareWeek> weeklist = new List<TimeshareWeek>();
  146. foreach (var item in list)
  147. {
  148. weeklist.Add(GetDetailedWeek(item));
  149. }
  150. return weeklist;
  151. }
  152. private TimeshareWeek GetDetailedWeek(TimeshareWeek week)
  153. {
  154. if (week.AgencyId != null)
  155. {
  156. AgencyRepository agency = new AgencyRepository(_dbContext);
  157. week.Agency = agency.Get(x => x.Id == week.AgencyId).FirstOrDefault();
  158. }
  159. if (week.AgentId != null)
  160. {
  161. AgentRepository agent = new AgentRepository(_dbContext);
  162. week.Agent = agent.Get(x => x.Id == week.AgentId).FirstOrDefault();
  163. }
  164. if (week.StatusId != 0)
  165. {
  166. StatusRepository status = new StatusRepository(_dbContext);
  167. week.Status = status.Get(x => x.Id == week.StatusId).FirstOrDefault();
  168. }
  169. else
  170. {
  171. StatusRepository repo = new StatusRepository(_dbContext);
  172. week.Status = repo.GetDetailed(s => s.Code == "A1");
  173. }
  174. if (week.RegionId != 0)
  175. {
  176. ProvinceRepository province = new ProvinceRepository(_dbContext);
  177. week.Region = province.Get(x => x.Id == week.RegionId).FirstOrDefault();
  178. }
  179. if (week.OwnerId != 0)
  180. {
  181. IndividualRepository individual = new IndividualRepository(_dbContext);
  182. week.Owner = individual.Get(x => x.Id == week.OwnerId).FirstOrDefault();
  183. }
  184. return week;
  185. }
  186. public void Insert(TimeshareWeek item)
  187. {
  188. item = GetDetailedWeek(item);
  189. StatusRepository repo = new StatusRepository(_dbContext);
  190. var stat = repo.Get(x => x.Code == "A1" && x.StatusType == StatusType.Timeshare).FirstOrDefault();
  191. if (stat == null)
  192. {
  193. //Create initial
  194. stat = new Status()
  195. {
  196. Code = "A1",
  197. Description = "Pending verification",
  198. StatusType = StatusType.Timeshare,
  199. ModifiedBy = "KobusB"
  200. };
  201. repo.Insert(stat);
  202. }
  203. item.Id = NewId();
  204. item.ProcessFlows.Add(new Model.ProcessFlow.ProcessFlow()
  205. {
  206. Status = item.Status,
  207. Timeshare = item
  208. });
  209. TemplateRepository templateRepository = new TemplateRepository(_dbContext);
  210. if(item.Owner != null)
  211. {
  212. var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekLoaded-Owner");
  213. if(template != null)
  214. {
  215. templateRepository.SendEmailTemplate(template, item.Owner, new List<BaseEntity>() { item, item.Owner });
  216. }
  217. template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekLoaded-UV");
  218. if(template != null)
  219. {
  220. templateRepository.SendEmailTemplate(template, item.Owner, new List<BaseEntity>() { item, item.Owner });
  221. }
  222. }
  223. _dbContext.Add(item);
  224. Save();
  225. }
  226. public void Insert(IEnumerable<TimeshareWeek> items)
  227. {
  228. int id = NewId();
  229. foreach (var item in items)
  230. {
  231. item.Id = id;
  232. _dbContext.Add(item);
  233. id += 1;
  234. }
  235. Save();
  236. }
  237. public void Remove(TimeshareWeek item)
  238. {
  239. var i = _dbContext.Weeks.Find(item);
  240. _dbContext.Weeks.Remove(i);
  241. Save();
  242. }
  243. public void Remove(IEnumerable<TimeshareWeek> items)
  244. {
  245. foreach (var item in items)
  246. {
  247. var i = _dbContext.Weeks.Find(item);
  248. _dbContext.Weeks.Remove(i);
  249. }
  250. Save();
  251. }
  252. public void RemoveAtId(int item)
  253. {
  254. var i = _dbContext.Weeks.Find(item);
  255. _dbContext.Weeks.Remove(i);
  256. Save();
  257. }
  258. public void Save()
  259. {
  260. _dbContext.SaveChanges();
  261. }
  262. public void Update(TimeshareWeek item)
  263. {
  264. _dbContext.Entry(item).State = EntityState.Modified;
  265. Save();
  266. }
  267. public List<TimeshareWeek> GetBy(WeekFilterDto week)
  268. {
  269. List<TimeshareWeek> item = GetDetailedAll();
  270. if (!string.IsNullOrEmpty(week.RegionCode))
  271. {
  272. item = item.Where(x => x.Region != null && x.Region.Code?.ToLower() == week.RegionCode.ToLower()).ToList();
  273. }
  274. if (!string.IsNullOrEmpty(week.ResortCode))
  275. {
  276. item = item.Where(x => x.ResortCode?.ToLower() == week.ResortCode).ToList();
  277. }
  278. if (week.Date != null)
  279. {
  280. item = item.Where(x =>
  281. x.ArrivalDate >= week.Date.Value.AddDays(-7)
  282. && x.DepartureDate <= week.Date.Value.AddDays(7)).ToList();
  283. }
  284. if (week.MinAmount != null && week.MinAmount != 0)
  285. {
  286. item = item.Where(x => x.SellPrice >= week.MinAmount).ToList();
  287. }
  288. if (week.MaxAmount != null && week.MaxAmount != 0)
  289. {
  290. item.Where(x => x.SellPrice <= week.MaxAmount).ToList();
  291. }
  292. return item;
  293. }
  294. public int NewId()
  295. {
  296. int id = 0;
  297. if (_dbContext.Weeks.Count() > 0)
  298. {
  299. id = _dbContext.Weeks.Max(x => x.Id);
  300. }
  301. id += 1;
  302. return id;
  303. }
  304. private int _TenderId = 10000;
  305. private int GetTenderId()
  306. {
  307. return _TenderId++;
  308. }
  309. public List<WeekDto> GetTenderWeeks()
  310. {
  311. List<WeekDto> list = new List<WeekDto>();
  312. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(MyCommon.TenderUrl);
  313. request.Method = "GET";
  314. WebResponse response = request.GetResponse();
  315. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  316. {
  317. string result = reader.ReadToEnd();
  318. if (result.Length > 0)
  319. {
  320. string cleanLine;
  321. string[] lines = result.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
  322. foreach (string line in lines)
  323. {
  324. cleanLine = line.Replace("<br>", "");
  325. cleanLine = cleanLine.Replace("<br >", "");
  326. cleanLine = cleanLine.Replace("<br/>", "");
  327. cleanLine = cleanLine.Replace("<br />", "");
  328. list.Add(new WeekDto(GetTenderId(),cleanLine));
  329. }
  330. }
  331. }
  332. // Check that all regions are same as other
  333. list
  334. .Where(x => x.Region != null && x.Region.RegionCode == "FN")
  335. .ToList()
  336. .ForEach(x => x.Region.RegionCode = ChangeRegion(x.Region.RegionCode));
  337. list = GetRegion(list);
  338. return list;
  339. }
  340. private List<WeekDto> GetRegion(List<WeekDto> list)
  341. {
  342. ProvinceRepository province = new ProvinceRepository(_dbContext);
  343. Province prov = null;
  344. foreach(var item in list)
  345. {
  346. prov = province.GetDetailed(x => x.Code == item.Region.RegionCode);
  347. if(prov != null)
  348. {
  349. item.Region = new RegionDto(prov.Id, prov.Code, prov.Description);
  350. }
  351. }
  352. return list;
  353. }
  354. private string ChangeRegion(string value)
  355. {
  356. switch(value)
  357. {
  358. case "FN":
  359. return "KZN";
  360. case "L":
  361. return "LI";
  362. default:
  363. return value;
  364. }
  365. }
  366. public List<DetailedWeekDto> GetNeedApproval()
  367. {
  368. List<DetailedWeekDto> detList = new List<DetailedWeekDto>();
  369. var status = _dbContext.Status.FirstOrDefault(x => x.Code == "A1" && x.StatusType == StatusType.Timeshare);
  370. if(status != null)
  371. {
  372. var list = GetDetailedAll().Where(x => x.Status != null && x.Status.Id == status.Id).ToList();
  373. list.ForEach(x => detList.Add(new DetailedWeekDto(x)));
  374. }
  375. return detList;
  376. }
  377. public void VerifyWeek(int id)
  378. {
  379. var week = _dbContext.Weeks.FirstOrDefault(x => x.Id == id);
  380. var newStatus = _dbContext.Status.FirstOrDefault(x => x.Code.ToLower() == "a2" && x.StatusType == StatusType.Timeshare);
  381. if(week != null && newStatus != null)
  382. {
  383. week = GetDetailedWeek(week);
  384. week.Status = newStatus;
  385. Update(week);
  386. var template = _dbContext.Templates.Where(x => x.Name == "WeekAuthorized").FirstOrDefault();
  387. if(template != null)
  388. {
  389. TemplateRepository tempRepo = new TemplateRepository(_dbContext);
  390. tempRepo.SendEmailTemplate(template, week.Owner, new List<BaseEntity>() { week, week.Owner, week.Owner.User});
  391. }
  392. }
  393. }
  394. public void PublishWeek(int id)
  395. {
  396. var week = _dbContext.Weeks.FirstOrDefault(x => x.Id == id);
  397. var newStatus = _dbContext.Status.FirstOrDefault(x => x.Code.ToLower() == "b1" && x.StatusType == StatusType.Timeshare);
  398. if (week != null && newStatus != null)
  399. {
  400. week.Status = newStatus;
  401. Update(week);
  402. }
  403. }
  404. }
  405. }