-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transaction.m
93 lines (82 loc) · 3.08 KB
/
Transaction.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
//
// Transaction.m
// Allowance
//
// Created by Pablo Collins on 6/13/10.
//
#import "Transaction.h"
#import "Account.h"
@implementation Transaction
@dynamic amount;
@dynamic notes;
@dynamic tag;
@dynamic sourceAccountBalance;
@dynamic targetAccountBalance;
@dynamic transactionDate;
@dynamic sourceAccount;
@dynamic targetAccount;
+ (Transaction *)lastAllowancePaymentForAccount:(Account *)acct {
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
NSEntityDescription *d = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:ctx];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:d];
NSPredicate *p = [NSPredicate predicateWithFormat:@"(targetAccount == %@) AND (tag == 'allowance')",acct];
[req setPredicate:p];
NSSortDescriptor *ordering = [[NSSortDescriptor alloc] initWithKey:@"transactionDate" ascending:NO];
[req setSortDescriptors:[NSArray arrayWithObject:ordering]];
[req setFetchLimit:1];
NSError *e;
NSArray *txs = [ctx executeFetchRequest:req error:&e];
if ([txs count] > 0) {
return [txs objectAtIndex:0];
} else {
return nil;
}
}
+ (NSArray *)historyForAccount:(Account *)acct {
ModelManager *mm = [ModelManager sharedInstance];
NSManagedObjectContext *ctx = [mm managedObjectContext];
NSEntityDescription *d = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:ctx];
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:d];
NSPredicate *p = [NSPredicate predicateWithFormat:@"(targetAccount == %@) OR (sourceAccount == %@)",acct,acct];
[req setPredicate:p];
NSSortDescriptor *ordering = [[NSSortDescriptor alloc] initWithKey:@"transactionDate" ascending:NO];
[req setSortDescriptors:[NSArray arrayWithObject:ordering]];
NSError *e;
NSArray *out = [ctx executeFetchRequest:req error:&e];
return out;
}
- (void)updateAccounts {
int amt = [self.amount intValue];
int sourceBalance = [self.sourceAccount.balance intValue];
self.sourceAccountBalance = [NSNumber numberWithInt:sourceBalance];
sourceBalance -= amt;
self.sourceAccount.balance = [NSNumber numberWithInt:sourceBalance];
int targetBalance = [self.targetAccount.balance intValue];
self.targetAccountBalance = [NSNumber numberWithInt:targetBalance];
targetBalance += amt;
self.targetAccount.balance = [NSNumber numberWithInt:targetBalance];
}
+ (NSString *)formatAmount:(NSInteger)i
{
return [AllowanceAppDelegate priceFromDouble:i/(double)[AllowanceAppDelegate priceDenominator]];
}
- (NSString *)formattedAmount
{
return [Transaction formatAmount:[self.amount intValue]];
}
- (NSInteger)currentBalance
{
if (self.sourceAccount == [AllowanceAppDelegate masterAccount]) {
return [self.targetAccountBalance intValue] + [self.amount intValue];
} else {
return [self.sourceAccountBalance intValue] - [self.amount intValue];
}
}
- (NSString *)currentBalanceFormatted
{
return [Transaction formatAmount:[self currentBalance]];
}
@end