Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (NSDictionary *)checkShift:(NSDate *)date specials:(NSArray *)specials {
- if ([[Account getSingleEntity] isELD]) {
- NSDate *endDate = [[TimeManager sharedInstance] currentTimeWithCorrectTimeZone];
- [[self.statuses lastObject] setEndDate:endDate];
- } else {
- [[self.statuses lastObject] setEndDate:[Utils getQutterDateFromDate:[[TimeManager sharedInstance] currentTimeWithCorrectTimeZone]]];
- }
- int hour = 60*60;
- NSNumber *cycle = [[CycleStatusHelper new] getCycleAt:date];
- NSDictionary *rules = [RulesSettings getDriverRulesForCycleId:cycle];
- int totCycleHours = [rules[@"cycleHours"] intValue]*hour;//hours in seconds
- int totShiftHours = [rules[@"shiftHours"] intValue]*hour;//hours in seconds
- int totShiftWorkHours = [rules[@"shiftWorkHours"] intValue]*hour;
- int totDriveHours = [rules[@"driveHours"] intValue]*hour;
- int totBreakHours = [rules[@"breakHours"] intValue]*hour;
- int totBreakHoursBreak = hour/2;
- if ([[RulesSettings getSingleEntity].restBreakId integerValue] == 0) {
- totBreakHours = 999*hour;
- }
- int restartHours = [rules[@"restartHours"] intValue]*hour;
- int restartHoursCurrent = [rules[@"restartHours"] intValue]*hour;
- int restartShiftHours = [rules[@"shiftRestartHours"] intValue]*hour;
- int tillRestartHours = [rules[@"shiftRestartHours"] intValue]*hour;
- BOOL needCheckTillRestart = true;
- int maxBreak;
- NSDate *newTime;
- if ([RulesSettings getSingleEntity].cycleId.integerValue == 4 || [RulesSettings getSingleEntity].cycleId.integerValue == 5) {
- newTime = [Utils getDateBeforeDate:date forHoursCount:14*24];
- maxBreak = 0;
- } else {
- newTime = [Utils getDateBeforeDate:date forHoursCount:[rules[@"days"] integerValue]*24];
- maxBreak = 24*hour;
- }
- NSInteger cycleId = [RulesSettings getSingleEntity].cycleId.integerValue;
- for (StatusSpecials *special in specials){
- if (special.specId == 1) {//adverse driving +2 hours driving in usa
- totDriveHours += 2*hour;
- } else if (special.specId == 2) {//16 hours haul exception +2 hours shift in usa cycles
- if (cycleId == 0 || cycleId == 1 || cycleId == 2 || cycleId == 3 || cycleId == 6 || cycleId == 7) {
- totShiftHours += 2*hour;
- }
- }
- }
- int sb8Hours = 8*hour;
- //$t78 = strtotime($newTime);
- //$t24 = strtotime(Date::GetLastHoursFromTime($time, 24));//last 24 hours
- NSArray *statuses = [self getStatusesFrom:newTime to:date includeFake:false];
- int length = (int)statuses.count;
- NSArray *statusesReverse = [[[statuses reverseObjectEnumerator] allObjects] mutableCopy];
- BOOL needLastSt78 = YES;
- BOOL needLastSt14 = YES;
- BOOL needLastSt8 = YES;
- if ([[RulesSettings getSingleEntity].restBreakId integerValue] == 0) {
- needLastSt8 = NO;
- }
- BOOL needRestart34HoursCurrnet = YES;
- long newStatusTime = [date timeIntervalSince1970];
- long statusLength = 0;
- long last8SbTime = 0;
- long last2MoreOffTime = 0;
- int cycleDays = [rules[@"days"] intValue]-1;
- long checkDate = [[Utils getDateWithTime:@0.0 fromDate:date] timeIntervalSince1970];
- BOOL splitted = false;
- int sbSplitted = 0;
- NSCalendar *calendar = [[TimeManager sharedInstance] getCalendar];
- //loop through the statuses
- for (int x = 0; x < length; x++) {
- //get the latest in time status - the oldest one;
- LogStatus *status = statusesReverse[x];
- //status time in ms
- long stTime = [status.startDate timeIntervalSince1970];
- long stDate = [[Utils fastConvertDateFromString:status.time zeroTime:true calendar:calendar] timeIntervalSince1970];
- long diffTime = checkDate - stDate;
- long diffDays = diffTime/(24*60*60);
- //if its equal the start time - go to next status(difference in time equal 0)
- if(newStatusTime == stTime){
- continue;
- }
- //get status type
- NSInteger stType = status.statusTypeId.integerValue;
- if (stType == 2 && x != length-1 && ((LogStatus *)statusesReverse[x+1]).statusTypeId.integerValue == 2) {
- sbSplitted++;
- continue;
- }
- //get status length
- NSDate *nextStatusTime;
- if(x == 0){//status length = input minus this status time
- //if its the first status(the closes to checking time - then status length == inout time minus status time
- nextStatusTime = [NSDate dateWithTimeIntervalSince1970:newStatusTime];
- }else if(sbSplitted > 0){
- if (x-(sbSplitted+1) == -1) {
- nextStatusTime = [NSDate dateWithTimeIntervalSince1970:newStatusTime];
- } else {
- nextStatusTime = [Utils fastConvertDateFromString:((LogStatus *)statusesReverse[x-(sbSplitted+1)]).time zeroTime:false calendar:calendar];
- }
- sbSplitted = 0;
- } else {
- nextStatusTime = [Utils fastConvertDateFromString:((LogStatus *)statusesReverse[x-1]).time zeroTime:false calendar:calendar];
- }
- statusLength = nextStatusTime.timeIntervalSince1970 - stTime;
- if (needCheckTillRestart) {
- if (stType == statusDr || stType == statusOn) {
- needCheckTillRestart = false;
- } else {
- tillRestartHours -= statusLength;
- }
- if (tillRestartHours <= 0) {
- tillRestartHours = 0;
- needCheckTillRestart = false;
- }
- }
- /*cycle */
- if(needLastSt78){//if need to check current status for cycle check
- if (diffDays > cycleDays) {
- needLastSt78 = false;
- long lastDayTime = checkDate - cycleDays*24*60*60;
- long diffLength = nextStatusTime.timeIntervalSince1970 - lastDayTime;
- if(stType == 1 || stType == 0) {//driving or on duty
- totCycleHours -= diffLength;
- } else {
- if (x == 0)
- restartHoursCurrent = (int)(status.restart.integerValue - diffLength);
- }
- }else if(stType == 1 || stType == 0){//driving or on duty
- //subtract status legth
- totCycleHours -= statusLength;
- //reset $restartHours value
- restartHours = [rules[@"restartHours"] intValue]*hour;
- needRestart34HoursCurrnet = NO;
- if(maxBreak < 24*hour){
- maxBreak = 0;
- }
- }else{
- //subtract from restart
- restartHours -= statusLength;
- if(needRestart34HoursCurrnet){
- restartHoursCurrent-= statusLength;
- }
- maxBreak += statusLength;
- }
- if(restartHours <= 0){
- //if restart 34 hours less than 0 - mean that driver wasn't working for 34+ ours in a line - stop checking cycle
- needLastSt78 = NO;
- }
- }
- /* 8 hours violation */
- if(needLastSt8){
- int prev8hoursBreak = totBreakHoursBreak;
- if(stType == 0 || stType == 1){//driving or on duty
- //subtract stuiatus legth
- totBreakHours -= statusLength;
- //reset $totBreakHoursBreak value
- totBreakHoursBreak = hour/2;
- if(prev8hoursBreak < hour){//if there was less than 30 minues break(15 minutes) it have to be added to total time
- //get length of small break
- int addDiff = totBreakHoursBreak - prev8hoursBreak;
- //add to total 8 hours
- totBreakHours -= addDiff;
- }
- }else{
- //subtract from break
- totBreakHoursBreak -= statusLength;
- //if break less/equal 0
- if(totBreakHoursBreak <= 0){
- //stop checking 8 hours vioaltion
- needLastSt8 = NO;
- }
- }
- }
- /* 11/14 hours violation, 8 hours sleeping berth, 10 hours restart */
- if(needLastSt14){//if need to check 14 hours violation
- int prev10 = restartShiftHours;//save previos 10 hours restart time to add if its changed
- int prev8 = sb8Hours;//save previos 8 hours sleeping berth value
- if(stType == 0 || stType == 1){//driving or on duty
- //reset $restartShiftHours and $sb8Hours values
- splitted = true;
- restartShiftHours = [rules[@"shiftRestartHours"] intValue]*60*60;
- sb8Hours = 8*hour;
- }else{
- if(stType == 2){//slleping berth
- //subtract status legth from $sb8Hours value
- sb8Hours-=statusLength;
- }else{
- //reset $sb8Hours value if off duty
- sb8Hours = 8*hour;
- }
- //subtract status legth from $restartShiftHours value
- restartShiftHours -=statusLength;
- }
- //if $restartShiftHours == base value
- if(restartShiftHours == [rules[@"shiftRestartHours"] intValue]*60*60){
- if(stType == 1){//if driving
- //subtract status legth from 11 hours variable
- totDriveHours -= statusLength;
- }
- if(stType == 1 || stType == 0){//if working
- totShiftWorkHours -= statusLength;
- }
- //subtract status legth from 14 hours variable
- totShiftHours -= statusLength;
- }
- //if $restartShiftHours == base value and $prev10(previous status 10 hours restart value) != $restartShiftHours and $prev8 > 0(wasn't an 8 hours sleeping berth
- if(restartShiftHours == [rules[@"shiftRestartHours"] intValue]*hour && prev10 != restartShiftHours && prev8 > 0){
- //get the stop length
- int addDiff = restartShiftHours - prev10;
- //subtract stop value from 14 hours
- totShiftHours -= addDiff;
- totShiftWorkHours -= addDiff;
- }
- //if 14 hours less than 11 hours
- if(totShiftHours < totDriveHours){
- //11 hours drive = 14 hours shift
- totDriveHours = totShiftHours;
- }
- if(restartShiftHours <= 0){
- //if $restartShiftHours less than 0 (means more than 10 not working hours in a line) - stop checking 11/14
- needLastSt14 = NO;
- last8SbTime = 0;
- last2MoreOffTime = 0;
- } else if (stType == 2 && (statusLength+(8*hour-prev8)) >= 8*hour) {
- last8SbTime = stTime;
- if (last2MoreOffTime != 0 && (splitted)) {
- last2MoreOffTime = 0;
- needLastSt14 = NO;
- } else {
- last2MoreOffTime = stTime;
- }
- splitted = false;
- } else if ((stType == 2 || stType == 3) && (statusLength+(8*hour-prev8)) >= 2*hour && ([rules[@"shiftRestartHours"] integerValue]*60*60 - restartShiftHours) >= 2*hour) {
- last2MoreOffTime = stTime;
- if (last8SbTime != 0 && splitted) {
- last8SbTime = 0;
- needLastSt14 = NO;
- }
- splitted = false;
- }
- }
- }
- // if(restartHoursCurrent < 0){
- // restartHoursCurrent = 0;
- // }
- //return values
- BOOL hadCan24Break = YES;
- if(maxBreak < 24*hour){
- hadCan24Break = NO;
- }
- NSMutableDictionary *dict = [NSMutableDictionary new];
- dict[key_date] = date;
- dict[key_hadCan24Break] = [NSNumber numberWithBool:hadCan24Break];
- dict[key_cycle] = [[NSNumber numberWithInt:totCycleHours] stringValue];
- dict[key_drive] = [[NSNumber numberWithInt:totDriveHours] stringValue];
- dict[key_shift] = [[NSNumber numberWithInt:totShiftHours] stringValue];
- dict[@"shiftWork"] = [[NSNumber numberWithInt:totShiftWorkHours] stringValue];
- dict[key_restart34Hours] = [[NSNumber numberWithInt:restartHoursCurrent] stringValue];
- dict[key_tot8Hours] = [[NSNumber numberWithInt:totBreakHours] stringValue];
- dict[@"tillRestartHours"] = [[NSNumber numberWithInt:tillRestartHours] stringValue];
- return dict;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement