-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeEntry.m
80 lines (69 loc) · 1.85 KB
/
TimeEntry.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// TimeEntry.m
// TimeClock
//
// Created by Matthew Baker on 10/8/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TimeEntry.h"
#import "Break.h"
#import "Client.h"
@implementation TimeEntry
@dynamic notes;
@dynamic outTime;
@dynamic inTime;
@dynamic client;
@dynamic breaks;
@dynamic isOvertime;
- (NSNumber *)totalTime {
NSDate *endTime = self.outTime == nil ? [NSDate date] : self.outTime;
return [NSNumber numberWithDouble:[endTime timeIntervalSinceDate:self.inTime]/3600];
}
- (void)punchDay {
switch (self.currentState) {
case TimeEntryStateNewEntry:
self.inTime = [NSDate date];
break;
case TimeEntryStateShiftBegun:
case TimeEntryStateBreakBegun:
case TimeEntryStateBreakEnded:
self.outTime = [NSDate date];
break;
default:
//change no state since this entry is closed
break;
}
}
- (void)punchBreak:(NSManagedObjectContext *)managedObjectContext {
Break *breakEntry = nil;
switch (self.currentState) {
case TimeEntryStateShiftBegun:
case TimeEntryStateBreakEnded:
//start new break entry
breakEntry = (Break *)[NSEntityDescription insertNewObjectForEntityForName:@"Break" inManagedObjectContext:managedObjectContext];
[breakEntry startBreak];
[self addBreaksObject:breakEntry];
break;
case TimeEntryStateBreakBegun:
breakEntry = (Break *)[[self.breaks allObjects] objectAtIndex:0];
[breakEntry endBreak];
break;
default:
//do nothing
return;
}
NSError *error;
if (![managedObjectContext save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
- (TimeEntryState)currentState {
if (self.inTime == nil) return TimeEntryStateNewEntry;
if ([self.breaks count] > 0) {
}
if (self.outTime == nil) return TimeEntryStateShiftBegun;
return TimeEntryStateShiftEnded;
}
@end