Hi,
In my dropdown the current month generates two times.How to avoid- help me to fix this. "Jul 2020" duplicate.
-
- public ActionResult Billing(BillingManagerQueryParameters query)
- {
- if (_logger.IsInfoEnabled)
- {
- MethodBase m = MethodBase.GetCurrentMethod();
- _logger.LogInfo(string.Format("Entering {0} with calendarPeriodId:{1} workflowGroupId:{2} companyId:{3}", m.Name, query.CalendarPeriodId, query.WorkflowGroupId, query.CompanyId));
- }
- CalendarPeriod period;
- bool locked = false;
- User user = null;
- using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())
- {
- user = _Login.GetCurrentUser();
- var calendarRepo = new CalendarPeriodRepository(_ambientDbContextLocator);
- Customer customer = _wfRepo.FindBy<UserCustomer>(uc => uc.UserId == user.UserId, new[] { "Customer" }).FirstOrDefault().Customer;
- if (query.CalendarPeriodId == null || query.CalendarPeriodId == 0)
- {
- period = calendarRepo.GetCalendarPeriod(PeriodType.M, DateTime.Now, customer.Id);
-
- if (period == null)
- {
- period = calendarRepo.GetLastMonthAvailablePeriod(PeriodType.M, DateTime.Now, customer.Id);
- }
- locked = false;
- query.CalendarPeriodId = period.Id;
- }
- else
- {
- period = calendarRepo.GetCalendarPeriod((int)query.CalendarPeriodId);
- Debug.WriteLine("Checked period lock " + period.Id);
- locked = false;
-
-
- }
- }
-
- if (period == null)
- {
- if (_logger.IsInfoEnabled)
- _logger.LogInfo("Exiting Billing");
- return View("~/Areas/Billing/Views/Manage/Modals/AddCalendarPeriodMonthlyModal.cshtml");
- }
- SelectList periods = CreateBillingPeriodSelectList(period);
- var customerId = GetCustomerId();
- SelectListItemHelper selectListItemHelper = new SelectListItemHelper();
- SelectList companyList = selectListItemHelper.CompaniesByUser(customerId,user,true);
- SelectList wgList = selectListItemHelper.WorkflowGroupsByUser(customerId,user,true);
-
- BillingViewModel model = new BillingViewModel()
- {
- Query = query,
- CurrentPeriod = period,
- WorkflowGroups = selectListItemHelper.WorkflowGroupsByUser(customerId,user,true),
- Companies = selectListItemHelper.CompaniesByUser(customerId,user,true),
- BillingPeriods = periods,
- CurrentPeriodLocked = locked,
- };
- return View(model);
- }
- public CalendarPeriod GetCalendarPeriod(PeriodType type, DateTime date, int customerId)
- {
- return DbContext.CalendarPeriods
- .Where(x => x.TypeCode == type.SmartCode &
- x.StartDate <= date & x.EndDate >= date.Date &&
- x.CalendarPeriodType.CustomerId == customerId
- )
- .FirstOrDefault();
- }
- public CalendarPeriod GetLastMonthAvailablePeriod(PeriodType type, DateTime date, int customerId)
- {
- int prevMonth = date.AddMonths(-1).Month;
- return DbContext.CalendarPeriods
- .Where(x => x.TypeCode == type.SmartCode &
- x.StartDate.Month <= prevMonth & x.EndDate.Month <= prevMonth &&
- x.CalendarPeriodType.CustomerId == customerId)
- .FirstOrDefault();
- }
- public CalendarPeriod GetCalendarPeriod(int id)
- {
- return DbContext.CalendarPeriods.Find(id);
- }
-
- private SelectList CreateBillingPeriodSelectList(CalendarPeriod period)
- {
- var calendarRepo = new Repositories.CalendarPeriodRepository(_ambientDbContextLocator);
- var billingRepo = new Repositories.BillingRepository(_ambientDbContextLocator);
- int customerId = GetCustomerId();
- using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())
- {
- List<CalendarPeriod> periodsAvailable = new List<CalendarPeriod>();
- periodsAvailable.AddRange(calendarRepo.GetBillingPeriods(customerId));
- if (!periodsAvailable.Contains(period)) { periodsAvailable.Insert(0, period); }
- SelectList monthList = new SelectList(periodsAvailable.Select(x => new { x.Id, x.Name }), "Id", "Name");
- return monthList;
- }
- }
- public List<CalendarPeriod> GetBillingPeriods(int customerId)
- {
- DateTime endDate = DateTime.Now.AddDays(15);
- return DbContext.CalendarPeriods
- .Where(x => x.TypeCode == PeriodType.M.SmartCode &&
-
- x.StartDate <= endDate &&
- x.VisibleToBilling &&
- x.CalendarPeriodType.CustomerId == customerId)
- .OrderByDescending(x => x.EndDate)
- .ToList();
- }