Praveen Kumar

Praveen Kumar

  • NA
  • 235
  • 20.3k

calendar perio in dropdown for current month only appearing two times?

Jul 28 2020 3:29 AM
Hi,
 
In my dropdown the current month generates two times.How to avoid- help me to fix this. "Jul 2020" duplicate.
 
 
  1. //Controller  
  2. public ActionResult Billing(BillingManagerQueryParameters query)  
  3. {  
  4. if (_logger.IsInfoEnabled)  
  5. {  
  6. MethodBase m = MethodBase.GetCurrentMethod();  
  7. _logger.LogInfo(string.Format("Entering {0} with calendarPeriodId:{1} workflowGroupId:{2} companyId:{3}", m.Name, query.CalendarPeriodId, query.WorkflowGroupId, query.CompanyId));  
  8. }  
  9. CalendarPeriod period;  
  10. bool locked = false;  
  11. User user = null;  
  12. using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())  
  13. {  
  14. user = _Login.GetCurrentUser();  
  15. var calendarRepo = new CalendarPeriodRepository(_ambientDbContextLocator);  
  16. Customer customer = _wfRepo.FindBy<UserCustomer>(uc => uc.UserId == user.UserId, new[] { "Customer" }).FirstOrDefault().Customer;  
  17. if (query.CalendarPeriodId == null || query.CalendarPeriodId == 0)  
  18. {  
  19. period = calendarRepo.GetCalendarPeriod(PeriodType.M, DateTime.Now, customer.Id);  
  20. //get last month available if there are calendar periods but just not one for the current month  
  21. if (period == null)  
  22. {  
  23. period = calendarRepo.GetLastMonthAvailablePeriod(PeriodType.M, DateTime.Now, customer.Id);  
  24. }  
  25. locked = false// TODO: Delete CustCalendarPeriod period.CustomerCalendarPeriods.Where(x => x.CustomerCUSTOMER_ID == customer.CUSTOMER_ID).FirstOrDefault().CLOSED_FLAG == "Y" ? true : false;  
  26. query.CalendarPeriodId = period.Id;  
  27. }  
  28. else  
  29. {  
  30. period = calendarRepo.GetCalendarPeriod((int)query.CalendarPeriodId);  
  31. Debug.WriteLine("Checked period lock " + period.Id);  
  32. locked = false// TODO: Delete CustCalendarPeriod period.CustomerCalendarPeriods.Where(x => x.CustomerCUSTOMER_ID == customer.CustomerId).FirstOrDefault().CLOSED_FLAG == "Y" ? true : false;  
  33. //Debug.WriteLine("Locked " + period.CustomerCalendarPeriods.Where(x => x.CustomerCUSTOMER_ID == customer.CustomerId).FirstOrDefault().CLOSED_FLAG);  
  34. //currentPeriod = calendarRepo.GetCalendarPeriod(PeriodType.M, DateTime.Now);  
  35. }  
  36. }  
  37. //Check if there are not any calendar periods in database, Show off a useful message to user to add Calendar Period for Monthly  
  38. if (period == null)  
  39. {  
  40. if (_logger.IsInfoEnabled)  
  41. _logger.LogInfo("Exiting Billing");  
  42. return View("~/Areas/Billing/Views/Manage/Modals/AddCalendarPeriodMonthlyModal.cshtml");  
  43. }  
  44. SelectList periods = CreateBillingPeriodSelectList(period);  
  45. var customerId = GetCustomerId();  
  46. SelectListItemHelper selectListItemHelper = new SelectListItemHelper();  
  47. SelectList companyList = selectListItemHelper.CompaniesByUser(customerId,user,true);  
  48. SelectList wgList = selectListItemHelper.WorkflowGroupsByUser(customerId,user,true);  
  49. // Build the model  
  50. BillingViewModel model = new BillingViewModel()  
  51. {  
  52. Query = query,  
  53. CurrentPeriod = period,  
  54. WorkflowGroups = selectListItemHelper.WorkflowGroupsByUser(customerId,user,true),  
  55. Companies = selectListItemHelper.CompaniesByUser(customerId,user,true),  
  56. BillingPeriods = periods,  
  57. CurrentPeriodLocked = locked,  
  58. };  
  59. return View(model);  
  60. }  
  1. public CalendarPeriod GetCalendarPeriod(PeriodType type, DateTime date, int customerId)  
  2. {  
  3. return DbContext.CalendarPeriods  
  4. .Where(x => x.TypeCode == type.SmartCode &  
  5. x.StartDate <= date & x.EndDate >= date.Date &&  
  6. x.CalendarPeriodType.CustomerId == customerId  
  7. )  
  8. .FirstOrDefault();  
  9. }  
  10. public CalendarPeriod GetLastMonthAvailablePeriod(PeriodType type, DateTime date, int customerId)  
  11. {  
  12. int prevMonth = date.AddMonths(-1).Month;  
  13. return DbContext.CalendarPeriods  
  14. .Where(x => x.TypeCode == type.SmartCode &  
  15. x.StartDate.Month <= prevMonth & x.EndDate.Month <= prevMonth &&  
  16. x.CalendarPeriodType.CustomerId == customerId)  
  17. .FirstOrDefault();  
  18. }  
  19. public CalendarPeriod GetCalendarPeriod(int id)  
  20. {  
  21. return DbContext.CalendarPeriods.Find(id);  
  22. }  
  23.   
  24. private SelectList CreateBillingPeriodSelectList(CalendarPeriod period)  
  25. {  
  26. var calendarRepo = new Repositories.CalendarPeriodRepository(_ambientDbContextLocator);  
  27. var billingRepo = new Repositories.BillingRepository(_ambientDbContextLocator);  
  28. int customerId = GetCustomerId();  
  29. using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())  
  30. {  
  31. List<CalendarPeriod> periodsAvailable = new List<CalendarPeriod>();  
  32. periodsAvailable.AddRange(calendarRepo.GetBillingPeriods(customerId));  
  33. if (!periodsAvailable.Contains(period)) { periodsAvailable.Insert(0, period); }  
  34. SelectList monthList = new SelectList(periodsAvailable.Select(x => new { x.Id, x.Name }), "Id""Name");  
  35. return monthList;  
  36. }  
  37. }  
  38. public List<CalendarPeriod> GetBillingPeriods(int customerId)  
  39. {  
  40. DateTime endDate = DateTime.Now.AddDays(15);  
  41. return DbContext.CalendarPeriods  
  42. .Where(x => x.TypeCode == PeriodType.M.SmartCode &&  
  43. //x.StartDate >= startDate &&  
  44. x.StartDate <= endDate &&  
  45. x.VisibleToBilling &&  
  46. x.CalendarPeriodType.CustomerId == customerId)  
  47. .OrderByDescending(x => x.EndDate)  
  48. .ToList();  
  49. }  

Answers (1)