Hi,
In my dropdown the current month generates two times.How to avoid- help me to fix this. "Jul 2020" duplicate.
- //Controller
- 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
(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);
- //get last month available if there are calendar periods but just not one for the current month
- if (period == null)
- {
- period = calendarRepo.GetLastMonthAvailablePeriod(PeriodType.M, DateTime.Now, customer.Id);
- }
- locked = false; // TODO: Delete CustCalendarPeriod period.CustomerCalendarPeriods.Where(x => x.CustomerCUSTOMER_ID == customer.CUSTOMER_ID).FirstOrDefault().CLOSED_FLAG == "Y" ? true : false;
- query.CalendarPeriodId = period.Id;
- }
- else
- {
- period = calendarRepo.GetCalendarPeriod((int)query.CalendarPeriodId);
- Debug.WriteLine("Checked period lock " + period.Id);
- locked = false; // TODO: Delete CustCalendarPeriod period.CustomerCalendarPeriods.Where(x => x.CustomerCUSTOMER_ID == customer.CustomerId).FirstOrDefault().CLOSED_FLAG == "Y" ? true : false;
- //Debug.WriteLine("Locked " + period.CustomerCalendarPeriods.Where(x => x.CustomerCUSTOMER_ID == customer.CustomerId).FirstOrDefault().CLOSED_FLAG);
- //currentPeriod = calendarRepo.GetCalendarPeriod(PeriodType.M, DateTime.Now);
- }
- }
- //Check if there are not any calendar periods in database, Show off a useful message to user to add Calendar Period for Monthly
- 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);
- // Build the model
- 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
periodsAvailable = new List (); - 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
GetBillingPeriods( int customerId) - {
- DateTime endDate = DateTime.Now.AddDays(15);
- return DbContext.CalendarPeriods
- .Where(x => x.TypeCode == PeriodType.M.SmartCode &&
- //x.StartDate >= startDate &&
- x.StartDate <= endDate &&
- x.VisibleToBilling &&
- x.CalendarPeriodType.CustomerId == customerId)
- .OrderByDescending(x => x.EndDate)
- .ToList();
- }

Varun SetiaPosted Jul 28, 2020, 3:49 AM