-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kid.m
127 lines (113 loc) · 3.99 KB
/
Kid.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Kid.m
// Allowance
//
// Created by Pablo Collins on 6/13/10.
//
#import "Kid.h"
@implementation Kid
@dynamic name;
@dynamic payAmount;
@dynamic accounts;
@dynamic lastWeekPaid;
+ (void)createWithName:(NSString *)name andAllowance:(int)amount payToday:(bool)payToday {
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
Kid *kid = (Kid *)[NSEntityDescription
insertNewObjectForEntityForName:@"Kid"
inManagedObjectContext:ctx];
kid.name = name;
kid.payAmount = [NSNumber numberWithInt:amount];
int appPayDay = [AllowanceAppDelegate paymentDay];
int currentWeek = [Cal currentWeek];
int currentDay = [Cal currentDay];
int lastWeekPaid;
if ((currentDay < appPayDay) || payToday) {
lastWeekPaid = currentWeek - 1;
} else {
lastWeekPaid = currentWeek;
}
kid.lastWeekPaid = [NSNumber numberWithInt:lastWeekPaid];
Account *acct = (Account *)[NSEntityDescription
insertNewObjectForEntityForName:@"Account"
inManagedObjectContext:ctx];
acct.name = @"savings";
acct.owner = kid;
acct.createdAt = [NSDate date];
[mm save];
if (payToday) {
[kid updateAccount];
}
}
- (Account *)savingsAccount {
return [self.accounts anyObject]; //fixme
}
- (NSNumber *)savingsBalance {
return [self savingsAccount].balance;
}
- (NSInteger)updateAccount {
int out = 0;
if (self.payAmount == nil) {
return out;
}
int lastWeekPaid = [self.lastWeekPaid intValue];
int currentWeek = [Cal currentWeek];
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
Account *savings = [self savingsAccount];
Account *masterAccount = [Account masterAccount];
int lastWeek = currentWeek - 1;
int currentDay = [Cal currentDay];
int paymentDay = [AllowanceAppDelegate paymentDay];
while ((lastWeekPaid < lastWeek) ||
(lastWeekPaid == lastWeek && currentDay >= paymentDay)) {
lastWeekPaid += 1;
Transaction *tx = (Transaction *)[NSEntityDescription
insertNewObjectForEntityForName:@"Transaction"
inManagedObjectContext:ctx];
tx.amount = self.payAmount;
tx.sourceAccount = masterAccount;
tx.targetAccount = savings;
tx.tag = @"allowance";
tx.transactionDate = [Cal dateForWeeknum:lastWeekPaid andWeekday:paymentDay];
//tx.notes = [NSString stringWithFormat:@"Week %i",lastWeekPaid];
[tx updateAccounts];
out += 1;
}
self.lastWeekPaid = [NSNumber numberWithInt:lastWeekPaid];
[mm save];
return out;
}
- (void)updateAccountBy:(int)amount withNotes:(NSString *)notes {
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
Account *savings = [self savingsAccount];
Account *masterAccount = [Account masterAccount];
Transaction *tx = (Transaction *)[NSEntityDescription
insertNewObjectForEntityForName:@"Transaction"
inManagedObjectContext:ctx];
if (amount < 0) {
tx.sourceAccount = savings;
tx.targetAccount = masterAccount;
amount = -amount;
} else {
tx.sourceAccount = masterAccount;
tx.targetAccount = savings;
}
tx.amount = [NSNumber numberWithInt:amount];
tx.tag = @"update";
tx.transactionDate = [NSDate date];
tx.notes = notes;
[tx updateAccounts];
[mm save];
}
- (double)balanceCents {
return [[self savingsAccount].balance intValue]/(double)[AllowanceAppDelegate priceDenominator];
}
- (NSString *)balanceString {
return [AllowanceAppDelegate priceFromDouble:[self balanceCents]];
}
- (NSArray *)transactionHistory {
return [[self savingsAccount] transactionHistory];
}
@end