Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (NSDictionary *)checkShift:(NSDate *)date specials:(NSArray *)specials {
  2.     if ([[Account getSingleEntity] isELD]) {
  3.         NSDate *endDate = [[TimeManager sharedInstance] currentTimeWithCorrectTimeZone];
  4.         [[self.statuses lastObject] setEndDate:endDate];
  5.     } else {
  6.         [[self.statuses lastObject] setEndDate:[Utils getQutterDateFromDate:[[TimeManager sharedInstance] currentTimeWithCorrectTimeZone]]];
  7.     }
  8.    
  9.     int hour = 60*60;
  10.     NSNumber *cycle = [[CycleStatusHelper new] getCycleAt:date];
  11.     NSDictionary *rules = [RulesSettings getDriverRulesForCycleId:cycle];
  12.     int totCycleHours = [rules[@"cycleHours"] intValue]*hour;//hours in seconds
  13.     int totShiftHours = [rules[@"shiftHours"] intValue]*hour;//hours in seconds
  14.     int totShiftWorkHours = [rules[@"shiftWorkHours"] intValue]*hour;
  15.     int totDriveHours = [rules[@"driveHours"] intValue]*hour;
  16.     int totBreakHours = [rules[@"breakHours"] intValue]*hour;
  17.     int totBreakHoursBreak = hour/2;
  18.     if ([[RulesSettings getSingleEntity].restBreakId integerValue] == 0) {
  19.         totBreakHours = 999*hour;
  20.     }
  21.     int restartHours = [rules[@"restartHours"] intValue]*hour;
  22.     int restartHoursCurrent = [rules[@"restartHours"] intValue]*hour;
  23.     int restartShiftHours = [rules[@"shiftRestartHours"] intValue]*hour;
  24.     int tillRestartHours = [rules[@"shiftRestartHours"] intValue]*hour;
  25.     BOOL needCheckTillRestart = true;
  26.     int maxBreak;
  27.     NSDate *newTime;
  28.     if ([RulesSettings getSingleEntity].cycleId.integerValue == 4 || [RulesSettings getSingleEntity].cycleId.integerValue == 5) {
  29.         newTime = [Utils getDateBeforeDate:date forHoursCount:14*24];
  30.         maxBreak = 0;
  31.     } else {
  32.         newTime = [Utils getDateBeforeDate:date forHoursCount:[rules[@"days"] integerValue]*24];
  33.         maxBreak = 24*hour;
  34.     }
  35.    
  36.     NSInteger cycleId = [RulesSettings getSingleEntity].cycleId.integerValue;
  37.     for (StatusSpecials *special in specials){
  38.         if (special.specId == 1) {//adverse driving +2 hours driving in usa
  39.             totDriveHours += 2*hour;
  40.         } else if (special.specId == 2) {//16 hours haul exception +2 hours shift in usa cycles
  41.             if (cycleId == 0 || cycleId == 1 || cycleId == 2 || cycleId == 3 || cycleId == 6 || cycleId == 7) {
  42.                 totShiftHours += 2*hour;
  43.             }
  44.         }
  45.     }
  46.    
  47.    
  48.     int sb8Hours = 8*hour;
  49.    
  50.     //$t78 = strtotime($newTime);
  51.     //$t24 = strtotime(Date::GetLastHoursFromTime($time, 24));//last 24 hours
  52.    
  53.     NSArray *statuses = [self getStatusesFrom:newTime to:date includeFake:false];
  54.     int length = (int)statuses.count;
  55.     NSArray *statusesReverse = [[[statuses reverseObjectEnumerator] allObjects] mutableCopy];
  56.    
  57.     BOOL needLastSt78 = YES;
  58.     BOOL needLastSt14 = YES;
  59.     BOOL needLastSt8 = YES;
  60.     if ([[RulesSettings getSingleEntity].restBreakId integerValue] == 0) {
  61.         needLastSt8 = NO;
  62.     }
  63.    
  64.     BOOL needRestart34HoursCurrnet = YES;
  65.     long newStatusTime = [date timeIntervalSince1970];
  66.     long statusLength = 0;
  67.     long last8SbTime = 0;
  68.     long last2MoreOffTime = 0;
  69.     int cycleDays = [rules[@"days"] intValue]-1;
  70.     long checkDate = [[Utils getDateWithTime:@0.0 fromDate:date] timeIntervalSince1970];
  71.    
  72.     BOOL splitted = false;
  73.     int sbSplitted = 0;
  74.     NSCalendar *calendar = [[TimeManager sharedInstance] getCalendar];
  75.     //loop through the statuses
  76.     for (int x = 0; x < length; x++) {
  77.         //get the latest in time status - the oldest one;
  78.         LogStatus *status = statusesReverse[x];
  79.         //status time in ms
  80.         long stTime = [status.startDate timeIntervalSince1970];
  81.        
  82.         long stDate = [[Utils fastConvertDateFromString:status.time zeroTime:true calendar:calendar] timeIntervalSince1970];
  83.        
  84.         long diffTime = checkDate - stDate;
  85.         long diffDays = diffTime/(24*60*60);
  86.         //if its equal the start time - go to next status(difference in time equal 0)
  87.         if(newStatusTime == stTime){
  88.             continue;
  89.         }
  90.         //get status type
  91.         NSInteger stType = status.statusTypeId.integerValue;
  92.         if (stType == 2 && x != length-1 && ((LogStatus *)statusesReverse[x+1]).statusTypeId.integerValue == 2) {
  93.             sbSplitted++;
  94.             continue;
  95.         }
  96.        
  97.         //get status length
  98.         NSDate *nextStatusTime;
  99.         if(x == 0){//status length = input minus this status time
  100.             //if its the first status(the closes to checking time - then status length == inout time minus status time
  101.             nextStatusTime = [NSDate dateWithTimeIntervalSince1970:newStatusTime];
  102.         }else if(sbSplitted > 0){
  103.             if (x-(sbSplitted+1) == -1) {
  104.                 nextStatusTime = [NSDate dateWithTimeIntervalSince1970:newStatusTime];
  105.             } else {
  106.                 nextStatusTime = [Utils fastConvertDateFromString:((LogStatus *)statusesReverse[x-(sbSplitted+1)]).time zeroTime:false calendar:calendar];
  107.             }
  108.             sbSplitted = 0;
  109.         } else {
  110.             nextStatusTime = [Utils fastConvertDateFromString:((LogStatus *)statusesReverse[x-1]).time zeroTime:false calendar:calendar];
  111.         }
  112.         statusLength = nextStatusTime.timeIntervalSince1970 - stTime;
  113.         if (needCheckTillRestart) {
  114.             if (stType == statusDr || stType == statusOn) {
  115.                 needCheckTillRestart = false;
  116.             } else {
  117.                 tillRestartHours -= statusLength;
  118.             }
  119.             if (tillRestartHours <= 0) {
  120.                 tillRestartHours = 0;
  121.                 needCheckTillRestart = false;
  122.             }
  123.         }
  124.         /*cycle */
  125.         if(needLastSt78){//if need to check current status for cycle check
  126.             if (diffDays > cycleDays) {
  127.                 needLastSt78 = false;
  128.                
  129.                 long lastDayTime = checkDate - cycleDays*24*60*60;
  130.                 long diffLength = nextStatusTime.timeIntervalSince1970 - lastDayTime;
  131.                 if(stType == 1 || stType == 0) {//driving or on duty
  132.                     totCycleHours -= diffLength;
  133.                 } else {
  134.                     if (x == 0)
  135.                         restartHoursCurrent = (int)(status.restart.integerValue - diffLength);
  136.                 }
  137.             }else if(stType == 1 || stType == 0){//driving or on duty
  138.                 //subtract status legth
  139.                 totCycleHours -= statusLength;
  140.                 //reset $restartHours value
  141.                 restartHours = [rules[@"restartHours"] intValue]*hour;
  142.                 needRestart34HoursCurrnet = NO;
  143.                 if(maxBreak < 24*hour){
  144.                     maxBreak = 0;
  145.                 }
  146.             }else{
  147.                 //subtract from restart
  148.                 restartHours -= statusLength;
  149.                 if(needRestart34HoursCurrnet){
  150.                     restartHoursCurrent-= statusLength;
  151.                 }
  152.                 maxBreak += statusLength;
  153.             }
  154.             if(restartHours <= 0){
  155.                 //if restart 34 hours less than 0 - mean that driver wasn't working for 34+ ours in a line - stop checking cycle
  156.                 needLastSt78 = NO;
  157.             }
  158.         }
  159.        
  160.         /* 8 hours violation */
  161.         if(needLastSt8){
  162.             int prev8hoursBreak = totBreakHoursBreak;
  163.             if(stType == 0 || stType == 1){//driving or on duty
  164.                 //subtract stuiatus legth
  165.                 totBreakHours -= statusLength;
  166.                 //reset $totBreakHoursBreak value
  167.                 totBreakHoursBreak = hour/2;
  168.                 if(prev8hoursBreak < hour){//if there was less than 30 minues break(15 minutes) it have to be added to total time
  169.                     //get length of small break
  170.                     int addDiff = totBreakHoursBreak - prev8hoursBreak;
  171.                     //add to total 8 hours
  172.                     totBreakHours -= addDiff;
  173.                 }
  174.             }else{
  175.                 //subtract from break
  176.                 totBreakHoursBreak -= statusLength;
  177.                 //if break less/equal 0
  178.                 if(totBreakHoursBreak <= 0){
  179.                     //stop checking 8 hours vioaltion
  180.                     needLastSt8 = NO;
  181.                 }
  182.             }
  183.         }
  184.         /* 11/14 hours violation, 8 hours sleeping berth, 10 hours restart */
  185.         if(needLastSt14){//if need to check 14 hours violation
  186.             int prev10 = restartShiftHours;//save previos 10 hours restart time to add if its changed
  187.             int prev8 = sb8Hours;//save previos 8 hours sleeping berth value
  188.             if(stType == 0 || stType == 1){//driving or on duty
  189.                 //reset $restartShiftHours and $sb8Hours values
  190.                 splitted = true;
  191.                 restartShiftHours = [rules[@"shiftRestartHours"] intValue]*60*60;
  192.                 sb8Hours = 8*hour;
  193.             }else{
  194.                 if(stType == 2){//slleping berth
  195.                     //subtract status legth from $sb8Hours value
  196.                     sb8Hours-=statusLength;
  197.                 }else{
  198.                     //reset $sb8Hours value if off duty
  199.                     sb8Hours = 8*hour;
  200.                 }
  201.                 //subtract status legth from $restartShiftHours value
  202.                 restartShiftHours -=statusLength;
  203.             }
  204.             //if $restartShiftHours == base value
  205.             if(restartShiftHours == [rules[@"shiftRestartHours"] intValue]*60*60){
  206.                 if(stType == 1){//if driving
  207.                     //subtract status legth from 11 hours variable
  208.                     totDriveHours -= statusLength;
  209.                 }
  210.                 if(stType == 1 || stType == 0){//if working
  211.                     totShiftWorkHours -= statusLength;
  212.                 }
  213.                 //subtract status legth from 14 hours variable
  214.                 totShiftHours -= statusLength;
  215.                
  216.             }
  217.             //if $restartShiftHours == base value and $prev10(previous status 10 hours restart value) != $restartShiftHours and $prev8 > 0(wasn't an 8 hours sleeping berth
  218.             if(restartShiftHours ==  [rules[@"shiftRestartHours"] intValue]*hour && prev10 != restartShiftHours && prev8 > 0){
  219.                 //get the stop length
  220.                 int addDiff = restartShiftHours - prev10;
  221.                 //subtract stop value from 14 hours
  222.                 totShiftHours -= addDiff;
  223.                 totShiftWorkHours -= addDiff;
  224.             }
  225.             //if 14 hours less than 11 hours
  226.             if(totShiftHours < totDriveHours){
  227.                 //11 hours drive = 14 hours shift
  228.                 totDriveHours = totShiftHours;
  229.             }
  230.             if(restartShiftHours <= 0){
  231.                 //if $restartShiftHours less than 0 (means more than 10 not working hours in a line) - stop checking 11/14
  232.                 needLastSt14 = NO;
  233.                
  234.                 last8SbTime = 0;
  235.                 last2MoreOffTime = 0;
  236.             } else if (stType == 2 && (statusLength+(8*hour-prev8)) >= 8*hour) {
  237.                 last8SbTime = stTime;
  238.                 if (last2MoreOffTime != 0 && (splitted)) {
  239.                     last2MoreOffTime = 0;
  240.                     needLastSt14 = NO;
  241.                 } else {
  242.                     last2MoreOffTime = stTime;
  243.                 }
  244.                 splitted = false;
  245.             } else if ((stType == 2 || stType == 3) && (statusLength+(8*hour-prev8)) >= 2*hour && ([rules[@"shiftRestartHours"] integerValue]*60*60 - restartShiftHours) >= 2*hour) {
  246.                 last2MoreOffTime = stTime;
  247.                 if (last8SbTime != 0 && splitted) {
  248.                     last8SbTime = 0;
  249.                     needLastSt14 = NO;
  250.                 }
  251.                 splitted = false;
  252.             }
  253.         }
  254.     }
  255.     //    if(restartHoursCurrent < 0){
  256.     //        restartHoursCurrent = 0;
  257.     //    }
  258.     //return values
  259.     BOOL hadCan24Break = YES;
  260.     if(maxBreak < 24*hour){
  261.         hadCan24Break = NO;
  262.     }
  263.    
  264.     NSMutableDictionary *dict = [NSMutableDictionary new];
  265.     dict[key_date] = date;
  266.     dict[key_hadCan24Break] = [NSNumber numberWithBool:hadCan24Break];
  267.     dict[key_cycle] = [[NSNumber numberWithInt:totCycleHours] stringValue];
  268.     dict[key_drive] = [[NSNumber numberWithInt:totDriveHours] stringValue];
  269.     dict[key_shift] = [[NSNumber numberWithInt:totShiftHours] stringValue];
  270.     dict[@"shiftWork"] = [[NSNumber numberWithInt:totShiftWorkHours] stringValue];
  271.     dict[key_restart34Hours] = [[NSNumber numberWithInt:restartHoursCurrent] stringValue];
  272.     dict[key_tot8Hours] = [[NSNumber numberWithInt:totBreakHours] stringValue];
  273.     dict[@"tillRestartHours"] = [[NSNumber numberWithInt:tillRestartHours] stringValue];
  274.     return dict;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement