-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.m
58 lines (50 loc) · 1.5 KB
/
Account.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
//
// Account.m
// Allowance
//
// Created by Pablo Collins on 6/13/10.
//
#import "Account.h"
#import "Kid.h"
@implementation Account
@dynamic isExpenseType;
@dynamic name;
@dynamic createdAt;
@dynamic debitTransactions;
@dynamic owner;
@dynamic creditTransactions;
@dynamic balance;
+ (void)findOrCreateMasterAccount {
Account *mst = [Account masterAccount];
if (mst != nil) {
return;
}
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
mst = (Account *)[NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:ctx];
mst.name = @"master";
mst.isExpenseType = [NSNumber numberWithBool:YES];
mst.createdAt = [NSDate date];
[mm save];
}
+ (Account *)masterAccount {
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:ctx];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:entity];
NSPredicate *p = [NSPredicate predicateWithFormat:@"name == 'master'"];
[req setPredicate:p];
[req setFetchLimit:1];
NSError *err;
NSArray *accounts = [ctx executeFetchRequest:req error:&err];
if ([accounts count] > 0) {
return [accounts objectAtIndex:0];
} else {
return nil;
}
}
- (NSArray *)transactionHistory {
return [Transaction historyForAccount:self];
}
@end