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

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