Dynamics 365





Monday, May 28, 2018

Calculate Duration between two times using business calendar in MS CRM Dynamics

Calculate Duration between two times using business calendar in MS CRM Dynamics

Guid calendarGuid = TimeManager.GetCalendarFromSLA(sla.SLAId);

                var ResolutionDate = DateTime.UtcNow;
                List<TimeSlot> timeSlots = TimeManager.GetAvailableTimeSlotsForCalendar(calendarGuid, CaseCreateTime, DateTime.UtcNow);
                double TotalSpanSeconds = 0;
                double Totalhours = 0;
                double TotalBusinesshours = 0;
                double Totaldays = 0;
                double TotalMinutes = 0;
                int TotalDaysTillDay = timeSlots.Count / 2;
                foreach (var slot in timeSlots)
                {
                    double slotSpan = 0;
                    double hours = 0;
                    if (CaseCreateTime > slot.End)
                        slotSpan = 0;
                    else if (CaseCreateTime > slot.Start && ResolutionDate < slot.End)
                    {
                        slotSpan = (ResolutionDate - CaseCreateTime).TotalSeconds;
                        hours = (ResolutionDate - CaseCreateTime).TotalHours;
                    }
                    else if (CaseCreateTime > slot.Start && CaseCreateTime < slot.End && ResolutionDate > slot.End)
                    {
                        slotSpan = (slot.End - CaseCreateTime).TotalSeconds;
                        hours = (slot.End - CaseCreateTime).TotalHours;
                    }
                    else if (ResolutionDate > slot.Start && ResolutionDate < slot.End)
                    {
                        slotSpan = (ResolutionDate - slot.Start).TotalSeconds;
                        hours = (ResolutionDate - slot.Start).TotalHours;
                    }
                    else
                    {
                        slotSpan = (slot.End - slot.Start).TotalSeconds;
                        hours = (slot.End - slot.Start).TotalHours;
                    }

                    TotalSpanSeconds += slotSpan;
                    Totalhours += hours;

                }
                TotalBusinesshours = Totalhours;

                double reminingHours = Totalhours % 8;
                Totaldays = Totalhours / 8;             
                TotalMinutes = reminingHours * 60;

                BusinessHoursDuration = new TimeSpan(Convert.ToInt32(Totaldays), Convert.ToInt32(reminingHours), Convert.ToInt32(TotalMinutes), 0);


                var resolutionDuration = string.Format("{0:%d} days, {0:%h} hours, {0:%m} minutes, {0:%s} seconds", BusinessHoursDuration);




//GetAvailableTimeSlotsForCalendar


public List<TimeSlot> GetAvailableTimeSlotsForCalendar(Guid calendarId, DateTime startTime, DateTime endTime)
        {
            try
            {
                //Convert time to UTC because calendar info returned by the system is in UTC
                if (startTime.Kind == DateTimeKind.Local)
                    startTime = startTime.ToUniversalTime();
                if (endTime.Kind == DateTimeKind.Local)
                    endTime = endTime.ToUniversalTime();

             
                TimeInfo[] timeInfos = GetTimeInfoForCalendar(calendarId, startTime, endTime);
           
                List<TimeSlot> timeSlots = GetAvailableTimeSlots(timeInfos);
                return timeSlots;

            }
            catch (Exception ex)
            {
               
                throw;
            }
        }


// to get time slot

internal Entity GetCalendarFromSLA(Guid slaId)
        {
            try
            {
                return Retrieve(SLAColumnNames.EntityName, slaId, new string[] { SLAColumnNames.SLAId, SLAColumnNames.BusinessHours });
            }
            catch (Exception ex)
            {
                ExceptionHelper.LogException(ex, user, BaseConstants.BaseModuleName, "Error getting calendar for SLA:" + slaId.ToString(), service);
                throw;
            }
        }

No comments:

Post a Comment