Home pairing and calculating the hours of checkTypes
Reply: 0

pairing and calculating the hours of checkTypes

user6009
1#
user6009 Published in May 22, 2018, 4:34 am

Continuing from here, I have a table named Attendancelogs which has all the sorted logs CHeckTypes In and Out accordingly. (Thanks to @StephenMuecke the previous question was pretty much resolved) However, what I am trying to achieve next is to make a pairList of all the In and Out in a particular period (StartDate and EndDate) and then calculate the total hours of the employee.

Different Cases, already been taken care of in the previous question

I have two method, one called getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) which creates the pairList and the second method getTimeSpentEachDay(List<Pair> pairList) which calculates the total hours of each day.

Pair.cs

  public class Pair {
      public int id { get; set; }
      public int RegisterationId { get; set; }
      public int EmpID { get; set; }
      public DateTime InnDateTime { get; set; }
      public DateTime OutDateTime { get; set; }
  }

  public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) {
      DateTime today = DateTime.Now;
      List<Pair> pairList = new List<Pair>();
      var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
            x.Date <= EndDate && x.isIgnore != true && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList();

      int loopEnd = 0;
      bool oddLogs = false;

      if (logs.Count % 2 == 0) {
          loopEnd = logs.Count;
      } else {
          loopEnd = logs.Count - 1;
          oddLogs = true;
      }

      bool inn = true;

      if (loopEnd > 1) {
          Pair pair = new Pair();

          for (int v = 0; v < loopEnd; v++) {

              if (inn) {
                  pair.InnDateTime = logs[v].DateTime;
                  inn = false;
              } else {
                  pair.OutDateTime = logs[v].DateTime;
                  inn = true;
                  pairList.Add(pair);
                  pair = new Pair();
              }
          }
      }

      Bogus bogus = new Bogus();
      DateTime bogusDate = new DateTime();

      if (oddLogs) {
          bogus.MachineNum = logs[logs.Count - 1].DeviceID;
          bogus.RegisterationId = logs[logs.Count - 1].RegisterationId;
          bogus.DateTime = logs[logs.Count - 1].DateTime;
          bogusDate = logs[logs.Count - 1].DateTime;
      }

      return pairList;
  } 

^I changed the above method with different approach, since the above approach would mess up with Case 1 shown in the link above.

     public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate) {
         DateTime today = DateTime.Now;
         List<Pair> pairList = new List<Pair>();
         var logs = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
                x.Date <= EndDate && x.isIgnore != true && (x.CheckType == "In" || x.CheckType == "Out")).Distinct().ToList();

          bool isCheck = false;
          Pair pair = new Pair();
          DateTime previous = logs.FirstOrDefault().DateTime;

          foreach (var log in logs) {

              if (!isCheck) {
                  pair.InnDateTime = log.DateTime;
                  isCheck = true;
              } else {
                  pair.OutDateTime = log.DateTime;
                  isCheck = false;
              }
              pairList.Add(pair);
              pair = new Pair();
          }

              return pairList;
        }

^This approach again will fail at Case 1 as it is sequentially adding In and Out in the pairList.

public List<DateAndTime> getTimeSpentEachDay(List<Pair> pairList) {
    List<DateAndTime> list = new List<DateAndTime>();

    if (pairList.Count > 0) {

        for (int i = 0; i < pairList.Count; i++) {
            TimeSpan span = TimeSpan.Zero;
            // bool flag = false;
            int result = -1;

            do {
                span = span + (pairList[i].OutDateTime - pairList[i].InnDateTime);
                result = -1;

                if (i < pairList.Count - 1) {
                    DateTime p = (DateTime)pairList[i].InnDateTime;
                    DateTime q = (DateTime)pairList[i + 1].InnDateTime;

                    result = DateTime.Compare(p.Date, q.Date);
                }

                if (result == 0) {
                    i++;
                    //  flag = true;
                }
            } while (result == 0);

            //if (i == pairList.Count - 1)
            //{
            //    span = span + (pairList[i].OutDateTime - pairList[i].InnDateTime) ?? TimeSpan.Zero;
            //}


            DateAndTime dnt = new DateAndTime();
            dnt.date = ((DateTime)pairList[i].InnDateTime).ToString("yyyy-MM-dd");
            dnt.Time = span;
            list.Add(dnt);
        }
    }

    return list.ToList();
}

I am trying to get total hours for each pair as well as a way to take the odd In's together which I can display on the calendar.

Below is an image of the calendar view for an employee which displays only the paired hours, I even want to show a In that would indicate the employee that he either forgot to check out or a missing entry was made.

*Lets say he had a checkin for 9th April of around 08:00 PM that had no Check out on that day and also in the NightersLimit (12 AM - 7 AM) then there should be a single line displayed on the calendar which can only happen if I bring the bogus record for a day from previous method.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313012 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO