public static ExigoService.CalendarEvent CopyCalendarEvent(Guid calendarEventID, int customerID, bool copyRecurrenceSettings = true) { // Get the event var context = Exigo.ODataCalendars(); var calendarEvent = context.CalendarEvents .Where(c => c.CalendarEventID == calendarEventID) .FirstOrDefault(); if (calendarEvent == null) { return(null); } // Create a clone of the event var copiedCalendarEvent = GlobalUtilities.Extend(calendarEvent, new CalendarContext.CalendarEvent()); // Fetch the calendars var calendar = GetCalendars(new GetCalendarsRequest { CustomerID = customerID, IncludeCalendarSubscriptions = false }).FirstOrDefault(); if (calendar == null) { return(null); } // Change some settings to give the event new ownership copiedCalendarEvent.CalendarID = calendar.CalendarID; copiedCalendarEvent.CalendarEventID = Guid.Empty; copiedCalendarEvent.IsPrivateCopy = true; // Clear out the recurring settings if desired if (!copyRecurrenceSettings) { copiedCalendarEvent.CalendarEventRecurrenceTypeID = null; copiedCalendarEvent.CalendarEventRecurrenceInterval = null; copiedCalendarEvent.CalendarEventRecurrenceMaxInstances = null; copiedCalendarEvent.CalendarEventRecurrenceEndDate = null; copiedCalendarEvent.WeeklyCalendarEventRecurrenceDays = null; copiedCalendarEvent.MonthlyCalendarEventRecurrenceTypeID = null; } return((ExigoService.CalendarEvent)copiedCalendarEvent); }
public JsonNetResult GetRealTimeBonusDetails(KendoGridRequest request) { var results = new List <RealTimeCommissionBonusDetail>(); // Get the commission record(s) var context = Exigo.WebService(); var realtimeresponse = context.GetRealTimeCommissions(new Common.Api.ExigoWebService.GetRealTimeCommissionsRequest { CustomerID = Identity.Current.CustomerID }); if (realtimeresponse.Commissions.Length == 0) { return(new JsonNetResult()); } // Get the bonuses (I know, this is brutal, but our UI depends on it) foreach (var commission in realtimeresponse.Commissions) { var bonuses = commission.Bonuses .Select(c => new CommissionBonus() { BonusID = c.BonusID, BonusDescription = c.Description }).Distinct(); foreach (var bonusID in commission.Bonuses.Select(c => c.BonusID).Distinct()) { var bonus = bonuses.Where(c => c.BonusID == bonusID).FirstOrDefault(); // Get the details for this bonus var details = context.GetRealTimeCommissionDetail(new Common.Api.ExigoWebService.GetRealTimeCommissionDetailRequest { CustomerID = commission.CustomerID, PeriodType = commission.PeriodType, PeriodID = commission.PeriodID, BonusID = bonusID }).CommissionDetails; // Get the period details for this period var period = Exigo.GetPeriods(new GetPeriodsRequest { PeriodTypeID = commission.PeriodType, PeriodIDs = new int[] { commission.PeriodID } }).FirstOrDefault(); // Format and save each bonus foreach (var detail in details) { var typedDetail = (CommissionBonusDetail)detail; var result = GlobalUtilities.Extend(typedDetail, new RealTimeCommissionBonusDetail()); result.BonusID = bonus.BonusID; result.BonusDescription = bonus.BonusDescription; result.PeriodDescription = period.PeriodDescription; results.Add(result); } } } // Filtering foreach (var filter in request.FilterObjectWrapper.FilterObjects) { results = results.AsQueryable().Where(filter.Field1, filter.Operator1, filter.Value1).ToList(); } // Sorting foreach (var sort in request.SortObjects) { results = results.AsQueryable().OrderBy(sort.Field, sort.Direction).ToList(); } // Return the data return(new JsonNetResult(new { data = results })); }
private static IEnumerable <ExigoService.CalendarEvent> GetCalendarRecurringEventInstances(List <ExigoService.CalendarEvent> instances, ExigoService.CalendarEvent recurringEvent, GetCalendarEventsRequest request, int instanceAttempts = 0) { // Set some variables for easier access if (recurringEvent.CalendarEventRecurrenceTypeID == null) { return(instances); } var recurrenceTypeID = (int)recurringEvent.CalendarEventRecurrenceTypeID; var recurrenceInterval = (int)recurringEvent.CalendarEventRecurrenceInterval; // Before we do anything, validate that our start date is valid based on its recurrence settings // Weekly recurrence validations if (recurrenceTypeID == 2) { var indexInList = recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.IndexOf(recurringEvent.StartDate.DayOfWeek); // If the start date's day of week doesn't match any of the available days in the week, // we need to make an adjustment to the start date and restart the method. if (indexInList == -1) { instanceAttempts--; var daysToAdjust = 0; foreach (var day in recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek) { if ((int)day > (int)recurringEvent.StartDate.DayOfWeek) { daysToAdjust = (int)day - (int)recurringEvent.StartDate.DayOfWeek; recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdjust); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdjust); break; } } // If we didn't find a day in the week that follows the current day, advance to the first available day in the next week if (daysToAdjust == 0) { daysToAdjust = 7 - ((int)recurringEvent.StartDate.DayOfWeek - (int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.First()); recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdjust); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdjust); } return(GetCalendarRecurringEventInstances(instances, recurringEvent, request, instanceAttempts)); } } // Add an instance of this recurring event if applicable if (recurringEvent.StartDate >= request.StartDate && recurringEvent.StartDate < request.EndDate) { // Create and add the instance var instance = GlobalUtilities.Extend(recurringEvent, new ExigoService.CalendarEvent()); instance.IsRecurringEventInstance = true; instances.Add(instance); } // Increment the amount of instances attempted instanceAttempts++; // If we have the maximum number of instances, stop creating instances and return the collection if (recurringEvent.CalendarEventRecurrenceMaxInstances != null && instanceAttempts >= (int)recurringEvent.CalendarEventRecurrenceMaxInstances) { return(instances); } // Increment the period of time based on the recurrence type. switch (recurrenceTypeID) { // Daily case 1: recurringEvent.StartDate = recurringEvent.StartDate.AddDays(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(1 * recurrenceInterval); break; // Weekly case 2: // Determine if we are repeating this event more than once per week. // If this event only occurs once per week, just add another week. if (recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.Count == 1) { recurringEvent.StartDate = recurringEvent.StartDate.AddDays(7 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(7 * recurrenceInterval); } // If this event occurs more than once in a week, let's ensure that we advance the dates appropriately. else { var indexInList = recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.IndexOf(recurringEvent.UtcStartDate.DayOfWeek); // If we are on the last day in the list, skip ahead to the front of the list var daysToAdvance = 0; if (indexInList == (recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.Count - 1)) { daysToAdvance = (7 - ((int)recurringEvent.StartDate.DayOfWeek - (int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek.First())) + (7 * (recurrenceInterval - 1)); } // Otherwise, add the number of days between the current start date and the next day in the week. else { daysToAdvance = ((int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek[indexInList + 1]) - ((int)recurringEvent.WeeklyCalendarEventRecurrenceDaysInWeek[indexInList]); } recurringEvent.StartDate = recurringEvent.StartDate.AddDays(daysToAdvance); recurringEvent.EndDate = recurringEvent.EndDate.AddDays(daysToAdvance); } break; // Monthly case 3: var monthlyRecurringTypeID = (int)recurringEvent.MonthlyCalendarEventRecurrenceTypeID; switch (monthlyRecurringTypeID) { // Day of the month case 1: recurringEvent.StartDate = recurringEvent.StartDate.AddMonths(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddMonths(1 * recurrenceInterval); break; // Day of the week (ie. second Tuesday of the month) case 2: // Determine which week and day the current start date is in var dayOfWeek = recurringEvent.StartDate.DayOfWeek; var nthWeek = 1; var checkingDate = recurringEvent.StartDate.BeginningOfMonth().Next(dayOfWeek); while (recurringEvent.StartDate != checkingDate) { checkingDate.AddDays(7); nthWeek++; } var nextMonth = recurringEvent.StartDate.AddMonths(1 * recurrenceInterval).BeginningOfMonth(); var timeDifference = recurringEvent.EndDate.Subtract(recurringEvent.StartDate); recurringEvent.StartDate = GlobalUtilities.GetNthWeekofMonth(nextMonth, nthWeek, dayOfWeek); recurringEvent.EndDate = recurringEvent.StartDate.Add(timeDifference); break; } break; // Yearly case 4: recurringEvent.StartDate = recurringEvent.StartDate.AddYears(1 * recurrenceInterval); recurringEvent.EndDate = recurringEvent.EndDate.AddYears(1 * recurrenceInterval); break; } // If we have exceeded the maximum recurrence end date, stop creating instances and return the collection if (recurringEvent.CalendarEventRecurrenceEndDate != null && (DateTime)recurringEvent.CalendarEventRecurrenceEndDate < recurringEvent.StartDate) { return(instances); } // If our new start date has exceeded the range of the original request, stop creating instances and return the collection if (recurringEvent.StartDate > request.EndDate) { return(instances); } return(GetCalendarRecurringEventInstances(instances, recurringEvent, request, instanceAttempts)); }
public JsonNetResult GetRealTimeBonusDetails(KendoGridRequest request) { try { // Have to include this as part of the url via a query string, to avoid hacking the kendo request object that is Http POSTed - Mike M. var periodID = Request.QueryString["periodID"] != null?Convert.ToInt32(Request.QueryString["periodID"]) : 0; var results = new List <RealTimeCommissionBonusDetail>(); // Get the commission record(s) var context = ExigoDAL.WebService(); var realtimeresponse = context.GetRealTimeCommissions(new Common.Api.ExigoWebService.GetRealTimeCommissionsRequest { CustomerID = Identity.Current.CustomerID }); if (realtimeresponse.Commissions.Length == 0) { return(new JsonNetResult()); } // Get the bonuses (I know, this is brutal, but our UI depends on it) var commissionsList = periodID > 0 ? realtimeresponse.Commissions.Where(c => c.PeriodID == periodID).ToList() : realtimeresponse.Commissions.ToList(); foreach (var commission in commissionsList) { var bonuses = commission.Bonuses .Select(c => new CommissionBonus() { BonusID = c.BonusID, BonusDescription = c.Description }).Distinct(); foreach (var bonusID in commission.Bonuses.Select(c => c.BonusID).Distinct()) { var bonus = bonuses.Where(c => c.BonusID == bonusID).FirstOrDefault(); // Get the details for this bonus var details = context.GetRealTimeCommissionDetail(new Common.Api.ExigoWebService.GetRealTimeCommissionDetailRequest { CustomerID = commission.CustomerID, PeriodType = commission.PeriodType, PeriodID = commission.PeriodID, BonusID = bonusID }).CommissionDetails; // Get the period details for this period var period = ExigoDAL.GetPeriods(new GetPeriodsRequest { PeriodTypeID = commission.PeriodType, PeriodIDs = new List <int> { commission.PeriodID } }).FirstOrDefault(); // Format and save each bonus foreach (var detail in details) { var typedDetail = (CommissionBonusDetail)detail; var result = GlobalUtilities.Extend(typedDetail, new RealTimeCommissionBonusDetail()); result.BonusID = bonus.BonusID; result.BonusDescription = bonus.BonusDescription; result.PeriodDescription = period.PeriodDescription; results.Add(result); } } } // Filtering foreach (var filter in request.FilterObjectWrapper.FilterObjects) { results = results.AsQueryable().Where(filter.Field1, filter.Operator1, filter.Value1).ToList(); } // Sorting foreach (var sort in request.SortObjects) { results = results.AsQueryable().OrderBy(sort.Field, sort.Direction).ToList(); } // Return the data return(new JsonNetResult(new { data = results })); } catch (Exception ex) { return(new JsonNetResult(new { success = false, message = ex.Message })); } }