Beispiel #1
0
        public IEnumerable <GeotabInfoModel> GetGeotabInfo(List <DutyStatusLog> logList)
        {
            PTC.Log.MethodStart();

            List <GeotabInfoModel> listOfGeotabInfo = new List <GeotabInfoModel>();

            IEnumerable <Id> newList = logList.Select(l => l.Driver.Id).Distinct();

            PTC.Log.Info($"Found {newList.Count()} drivers found from logs...");

            foreach (Id driver in newList)
            {
                UserSearch driverSearch = new UserSearch
                {
                    IsDriver = true,
                    Id       = driver,
                };

                User user = geotabCalls.GetUser(driverSearch);

                if (user.ActiveTo < DateTime.UtcNow)
                {
                    continue;
                }

                GeotabInfoModel geotabInfo = new GeotabInfoModel
                {
                    FirstName      = user.FirstName,
                    LastName       = user.LastName,
                    EmployeeNumber = user.EmployeeNo,
                    Timezone       = user.TimeZoneId.TimeZoneStringSpliter(), // starting the time zone
                    Logs           = logList.Where(x => x.Driver.Id.Equals(user.Id)).ToList()
                };

                if (geotabInfo.EmployeeNumber != "" && geotabInfo.EmployeeNumber != null)
                {
                    listOfGeotabInfo.Add(geotabInfo);
                    PTC.Log.Info($"Adding employee {geotabInfo.EmployeeNumber} to upload list...");
                }
            }
            PTC.Log.Info($"Total of {listOfGeotabInfo.Count()} drivers that have employee numbers...");
            return(listOfGeotabInfo);
        }
Beispiel #2
0
        public IEnumerable <GeotabInfoModel> GetGeotabInfo(List <User> driverList)
        {
            List <GeotabInfoModel> listOfGeotabInfo = new List <GeotabInfoModel>();

            List <DutyStatusLogType> bindLogTypes = LogTypesToUse();

            foreach (var driver in driverList)
            {
                PTC.Log.Info($"Associating {driver.Name} to the log list....");
                DutyStatusLogSearch statusSearch = new DutyStatusLogSearch
                {
                    UserSearch = new UserSearch {
                        Id = driver.Id
                    },
                    FromDate            = lastTime.ReadLastRunFile(), // last run datetime, store in a temp file to obtain this, a separate have window service create it. once ask finshes, update file with last run date (dattimenow). Create new class (potentially interface) for it, separate methods that grabs stores and update
                    ToDate              = DateTime.UtcNow,
                    Statuses            = bindLogTypes,
                    IncludeBoundaryLogs = true
                };

                List <DutyStatusLog> logRecordList = geotabCalls.GetLogList(statusSearch).ToList();

                GeotabInfoModel geotabInfo = new GeotabInfoModel
                {
                    FirstName      = driver.FirstName,
                    LastName       = driver.LastName,
                    EmployeeNumber = driver.EmployeeNo,
                    Logs           = logRecordList
                };

                if (geotabInfo.FirstName != "**<No User>")
                {
                    listOfGeotabInfo.Add(geotabInfo);
                }
            }
            return(listOfGeotabInfo);
        }
        public IEnumerable <TimeCardModel> LastRecordedPunches(GeotabInfoModel driver)
        {
            Log.MethodStart();

            List <TimeCardModel> driverTimePunches = new List <TimeCardModel>();
            LastRecordedTime     lastTime          = new LastRecordedTime();

            employeeNumber = driver.EmployeeNumber;

            DateTime tempDate = lastTime.ReadLastRunFile().AddDays(-14);
            //Log.Info($"Getting paycom punches from {tempDate} for {employeeNumber}...");
            long startDate = DateTimeExtensions.DateTimetoUnixTimeStamp(tempDate);

            string url = string.Format(punchAuditWithDate, employeeNumber, startDate);

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(baseUrl);
                    client.DefaultRequestHeaders.Add("Authorization", AUTH_HEADER);
                    HttpResponseMessage result = client.GetAsync(url).Result;

                    if (result.StatusCode != HttpStatusCode.OK && result.StatusCode != HttpStatusCode.PartialContent)
                    {
                        return(null);
                    }

                    string resultData = result.Content.ReadAsStringAsync().Result;

                    PunchAuditResponseModel data = JsonConvert.DeserializeObject <PunchAuditResponseModel>(resultData);
                    if (data.errors.FirstOrDefault() == "No Content Found")
                    {
                        Log.Info($"No recent punches for {employeeNumber} since {tempDate}...");
                        return(null);
                    }


                    ActiveResponseModel[] punchData = data.data.Active;

                    foreach (var activePunch in punchData)
                    {
                        TimeCardModel currentTimeCard = new TimeCardModel();
                        //var change = TimeSpan.Parse(activePunch.punchtime.TrimEnd(new char[] { ' ', 'A', 'M', 'P' }));
                        DateTime temporaryDate = DateTime.Parse($"{activePunch.punchdate} {activePunch.punchtime}");

                        currentTimeCard.FirstName           = driver.FirstName;
                        currentTimeCard.LastName            = driver.LastName;
                        currentTimeCard.EmployeeNumber      = driver.EmployeeNumber;
                        currentTimeCard.PunchDateTimeInUnix = TimeZoneConverter.ConvertToUtcFromTimeZone(driver.Timezone, temporaryDate);
                        //currentTimeCard.PunchDateTimeInUnix = DateTime.Parse($"{activePunch.punchdate} {activePunch.punchtime}").ConvertToUtcFromEastern().DateTimetoUnixTimeStamp();
                        currentTimeCard.PunchDate = $"{activePunch.punchdate} {activePunch.punchtime}";
                        currentTimeCard.PunchType = activePunch.punchtype;

                        driverTimePunches.Add(currentTimeCard);
                    }

                    //Log.Info($"Last punch for employee {employeeNumber} : {resultData}");

                    //driverTimePunches = punchData.Select(x => new TimeCardModel
                    //{
                    //    EmployeeNumber = driver.EmployeeNumber,
                    //    PunchTime = Convert.ToInt64(x.punchtime.TrimEnd(new char[] { ' ', 'A', 'M', 'P' })),
                    //    PunchDate = x.punchdate,
                    //    PunchType = x.punchtype
                    //}).ToList();
                }

                return(driverTimePunches.OrderBy(x => x.PunchDate).OrderBy(y => y.PunchTime));
            }

            catch (Exception ex)
            {
                Log.Error(ex);
                Log.Info(ex);
                throw;
            }
        }
        public GeotabInfoModel AuditIdentifyLastPunch(GeotabInfoModel driver)
        {
            employeeNumber = driver.EmployeeNumber;

            GeotabInfoModel lastEntry          = new GeotabInfoModel();
            var             convertedStartDate = startDate.ReadLastRunFile().DateTimetoUnixTimeStamp();

            var url = string.Format(punchAuditWithDate, employeeNumber, convertedStartDate);

            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(baseUrl);
                    client.DefaultRequestHeaders.Add("Authorization", AUTH_HEADER);
                    var result = client.GetAsync(url).Result;

                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        string resultData = result.Content.ReadAsStringAsync().Result;
                        var    data       = JsonConvert.DeserializeObject <PunchAuditResponseModel>(resultData);

                        string lastPunch = data.data.Active.OrderBy(x => x.punchid).LastOrDefault().punchtype;
                        lastEntry.PunchType = lastPunch;

                        return(lastEntry);
                    }
                    else if (result.StatusCode == HttpStatusCode.PartialContent)
                    {
                        HttpHeaders          headers = result.Headers;
                        IEnumerable <string> links;
                        headers.TryGetValues("Link", out links);

                        string   listToString = links.First();
                        string[] uriArray     = listToString.Split(',');
                        string   newUri       = uriArray.Take(uriArray.Length - 1).LastOrDefault();

                        int toRemove = newUri.IndexOf(">");
                        if (toRemove > 0)
                        {
                            newUri = newUri.Substring(0, toRemove);
                        }
                        newUri = newUri.Replace("<", "");
                        var newResult = client.GetAsync(newUri).Result;

                        string resultData = newResult.Content.ReadAsStringAsync().Result;
                        var    data       = JsonConvert.DeserializeObject <PunchHistoryResponseModel>(resultData);

                        var lastPunch = data.data.LastOrDefault().punchtype;
                        lastEntry.PunchType = lastPunch;

                        return(lastEntry);
                    }

                    return(lastEntry);
                }
            }

            catch (Exception ex)
            {
                Log.Error($"{ex}");
                throw ex;
            }
        }
        public GeotabInfoModel IdentifyLastPunch(GeotabInfoModel driver)
        {
            Log.MethodStart();

            employeeNumber = driver.EmployeeNumber;
            Log.Info($"Getting last punch for {employeeNumber}");

            GeotabInfoModel lastEntry = new GeotabInfoModel();
            DateTime        startDate = DateTime.Now.AddDays(-14).Date;

            string url = string.Format(punchAuditWithDate, employeeNumber, startDate.DateTimetoUnixTimeStamp());

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(baseUrl);
                    client.DefaultRequestHeaders.Add("Authorization", AUTH_HEADER);
                    HttpResponseMessage result = client.GetAsync(url).Result;

                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        string resultData            = result.Content.ReadAsStringAsync().Result;
                        PunchAuditResponseModel data = JsonConvert.DeserializeObject <PunchAuditResponseModel>(resultData);

                        if (data.errors.FirstOrDefault() == "No Content Found" || data.data.Active == null || data.data.Active.Length <= 0)
                        {
                            Log.Info($"{employeeNumber} has no punches in the last 14 days...");
                            string lastPunch = "OD";
                            lastEntry.PunchType = lastPunch;
                            return(lastEntry);
                        }
                        else
                        {
                            Log.Info($"Last punches for employee {employeeNumber}...");
                            string lastPunch = data.data.Active.OrderByDescending(x => x.punchid).FirstOrDefault().punchtype;
                            lastEntry.PunchType = lastPunch;

                            return(lastEntry);
                        }
                    }
                    else if (result.StatusCode == HttpStatusCode.PartialContent)
                    {
                        HttpHeaders          headers = result.Headers;
                        IEnumerable <string> links;
                        headers.TryGetValues("Link", out links);

                        string   listToString = links.First();
                        string[] uriArray     = listToString.Split(',');
                        string   newUri       = uriArray.Take(uriArray.Length - 1).LastOrDefault();

                        int toRemove = newUri.IndexOf(">");
                        if (toRemove > 0)
                        {
                            newUri = newUri.Substring(0, toRemove);
                        }
                        newUri = newUri.Replace("<", "");
                        HttpResponseMessage newResult = client.GetAsync(newUri).Result;

                        string resultData = newResult.Content.ReadAsStringAsync().Result;
                        PunchHistoryResponseModel data = JsonConvert.DeserializeObject <PunchHistoryResponseModel>(resultData);
                        //Log.Info($"Last punches for employee {employeeNumber} : {resultData}");

                        string lastPunch = data.data.LastOrDefault().punchtype;
                        lastEntry.PunchType = lastPunch;

                        return(lastEntry);
                    }

                    Log.MethodEnd();

                    return(lastEntry);
                }
            }

            catch (Exception ex)
            {
                Log.Error($"{ex}");
                throw ex;
            }
        }
        private List <TimeCardModel> CreateTimeCard(List <GeotabInfoModel> listOfGeotabInfo)
        {
            Log.MethodStart();
            List <TimeCardModel> timeCards = new List <TimeCardModel>();

            foreach (GeotabInfoModel record in listOfGeotabInfo)
            {
                bool previousLogIsOnDuty  = false;
                bool previousLogIsOffDuty = false;

                GeotabInfoModel oldPunch = paycomCalls.IdentifyLastPunch(record);

                //var auditLastPunch = paycomCalls.AuditIdentifyLastPunch(record); // testing with punchaudit url

                if (oldPunch.PunchType == "ID")
                {
                    previousLogIsOnDuty  = true;
                    previousLogIsOffDuty = false;
                }
                else if (oldPunch.PunchType == "OD")
                {
                    previousLogIsOnDuty  = false;
                    previousLogIsOffDuty = true;
                }

                record.Logs = record.Logs.OrderBy(l => l.DateTime).ToList();

                List <DutyStatusLog> logs = record.Logs;

                for (int i = 0; i < record.Logs.Count; i++)
                {
                    TimeCardModel timeCard = new TimeCardModel();

                    timeCard.FirstName      = record.FirstName;
                    timeCard.LastName       = record.LastName;
                    timeCard.EmployeeNumber = record.EmployeeNumber;
                    timeCard.Timezone       = record.Timezone;
                    timeCard.ClockType      = "S";
                    timeCard.EntryType      = 1;

                    switch (logs[i].Status)
                    {
                    case DutyStatusLogType.D:
                    case DutyStatusLogType.ON:
                    case DutyStatusLogType.YM:
                        if (previousLogIsOnDuty)
                        {
                            continue;
                        }
                        else
                        {
                            timeCard.PunchType = "ID";
                            timeCard.PunchTime = logs[i].DateTime.Value.DateTimetoUnixTimeStamp();
                            timeCards.Add(timeCard);
                        }

                        previousLogIsOnDuty  = true;
                        previousLogIsOffDuty = false;
                        break;

                    case DutyStatusLogType.OFF:
                    case DutyStatusLogType.PC:
                    case DutyStatusLogType.SB:
                    case DutyStatusLogType.WT:
                        if (previousLogIsOffDuty)
                        {
                            continue;
                        }
                        else
                        {
                            timeCard.PunchType = "OD";
                            timeCard.PunchTime = logs[i].DateTime.Value.DateTimetoUnixTimeStamp();
                            timeCards.Add(timeCard);
                        }

                        previousLogIsOffDuty = true;
                        previousLogIsOnDuty  = false;
                        break;
                    }
                }
            }
            ;

            Log.MethodEnd();

            return(timeCards);
        }