-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZXReportWindowController.m
More file actions
260 lines (228 loc) · 9.08 KB
/
ZXReportWindowController.m
File metadata and controls
260 lines (228 loc) · 9.08 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Name: ZXReportWindowController.m
* Project: Strongbox
* Created on: 2008-04-13
*
* Copyright (C) 2008 Pierre-Hans Corcoran
*
* --------------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (version 2) as published
* by the Free Software Foundation. This program is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have
* received a copy of the GNU General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* --------------------------------------------------------------------------
*/
#import "ZXReportWindowController.h"
#import "ZXDocument.h"
#import "ZXLabelController.h"
#import "ZXNotification.h"
#import "ZXReportGraphView.h"
#import "ZXReportHistView.h"
#import "ZXReportSection.h"
#import "ZXReportTextView.h"
@interface ZXReportWindowController (Private)
- (void)parseReportDates;
- (NSNumber *)parseLabelAmount:(id)label;
- (void)setupNotificationObserving;
- (void)resetViewsPositions;
@end
enum {
ZXAllAccountsDepositsReportType = 0,
ZXAllAccountsWithdrawalsReportType = 1,
ZXActiveAccountDepositsReportType = 2,
ZXActiveAccountWithdrawalsReportType = 3,
};
enum {
ZXAllReportTime = 0,
ZXThisMonthReportTime = 1,
ZXLastMonthReportTime = 2,
ZXThisYearReportTime = 3,
ZXLastYearReportTime = 4,
ZXCustomReportTime = 5,
};
@implementation ZXReportWindowController
@synthesize owner, reportStartDate, reportEndDate, detailBoxHidden;
- (id)initWithOwner:(id)newOwner;
{
self = [super init];
self.owner = newOwner;
self.reportStartDate = [NSDate distantPast];
self.reportEndDate = [NSDate date];
self.detailBoxHidden = [NSNumber numberWithBool:YES];
return self;
}
- (IBAction)toggleReportWindow:(id)sender
{
BOOL mustUpdate = NO;
if(!reportWindow) {
[NSBundle loadNibNamed:@"ReportWindow" owner:self];
mustUpdate = YES;
}
if([reportWindow isVisible] == NO || mustUpdate) {
[self setupNotificationObserving];
[self updateView:self];
[reportWindow makeKeyAndOrderFront:nil];
} else {
[reportWindow performClose:self];
}
}
- (void)setupNotificationObserving
{
id defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(updateView:)
name:ZXAccountTotalDidChangeNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(updateView:)
name:ZXTransactionLabelDidChangeNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(updateView:)
name:ZXActiveAccountDidChangeNotification
object:nil];
}
- (IBAction)updateView:(id)sender
{
[self resetViewsPositions];
[graphView removeAllSections];
[textView removeAllSections];
[self parseReportDates];
for(id label in [self.owner allLabels]) {
if([[label valueForKey:@"isImmutable"] boolValue]) continue;
id textColor = [label valueForKey:@"textColor"];
id labelAmount;
id labelName = [label valueForKey:@"name"];
labelAmount = [self parseLabelAmount:label];
double v = [labelAmount doubleValue];
if(-0.001 < v && v < 0.001) continue;
ZXReportSection *section = [ZXReportSection sectionWithColor:textColor
amount:labelAmount
name:labelName];
[graphView addSection:section];
//[histView addSection:section];
[textView addSection:section];
NSRect frame = [graphView frame];
// Substracting the width added to the textView to the graphView
frame.size.width -= [textView.lastWidthModification doubleValue];
textView.lastWidthModification = [NSNumber numberWithInt:0];
[graphView setFrame:frame];
}
[textView display];
[graphView display];
//[histView display];
}
- (NSNumber *)parseLabelAmount:(id)label
{
id labelAmount = [NSNumber numberWithInt:0];
id txDesc = [NSEntityDescription entityForName:@"Transaction"
inManagedObjectContext:self.owner.managedObjectContext];
NSPredicate *totalPredicate = nil;
NSArray *dateArray = [NSArray arrayWithObjects:self.reportStartDate, self.reportEndDate, nil];
int tag = [reportTypePopUpButton selectedTag];
id predString = @"(transactionLabel == %@) AND (date BETWEEN %@)";
id curAccount = [self.owner.accountController valueForKeyPath:@"selection.self"];
if(tag == ZXAllAccountsDepositsReportType) {
predString = [NSString stringWithFormat:@"%@ AND (amount > 0)", predString];
totalPredicate = [NSPredicate predicateWithFormat:predString, label, dateArray];
} else if(tag == ZXAllAccountsWithdrawalsReportType) {
predString = [NSString stringWithFormat:@"%@ AND (amount < 0)", predString];
totalPredicate = [NSPredicate predicateWithFormat:predString, label, dateArray];
} else if(tag == ZXActiveAccountDepositsReportType) {
predString = [NSString stringWithFormat:@"%@ AND (account == %%@) AND (amount > 0)", predString];
totalPredicate = [NSPredicate predicateWithFormat:predString, label, dateArray, curAccount];
} else if(tag == ZXActiveAccountWithdrawalsReportType) {
predString = [NSString stringWithFormat:@"%@ AND (account == %%@) AND (amount < 0)", predString];
totalPredicate = [NSPredicate predicateWithFormat:predString, label, dateArray, curAccount];
}
NSFetchRequest *fetchRequest;
NSError *error;
NSArray *array;
fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:txDesc];
[fetchRequest setPredicate:totalPredicate];
error = nil;
array = [self.owner.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if(array == nil) {
// FIXME: Exception management to be done
return nil;
}
double val = [[array valueForKeyPath:@"@sum.amount"] doubleValue];
if(val < 0) val *= -1;
return [NSNumber numberWithDouble:val];
}
- (void)parseReportDates
{
// Default interval is from a distant past to now
NSCalendarDate *calendarDate = [NSCalendarDate calendarDate];
if([reportTimePopUpButton selectedTag] != ZXCustomReportTime) {
self.reportStartDate = [NSDate distantPast];
self.reportEndDate = [NSDate date];
}
switch([reportTimePopUpButton selectedTag]) {
case ZXAllReportTime:
break;
case ZXThisMonthReportTime:
self.reportStartDate = [NSCalendarDate dateWithYear:[calendarDate yearOfCommonEra] month:[calendarDate monthOfYear] day:1 hour:0 minute:0 second:0 timeZone:[calendarDate timeZone]];
break;
case ZXLastMonthReportTime:
self.reportStartDate = [NSCalendarDate dateWithYear:[calendarDate yearOfCommonEra] month:[calendarDate monthOfYear] - 1 day:1 hour:0 minute:0 second:0 timeZone:[calendarDate timeZone]];
self.reportEndDate = [NSCalendarDate dateWithYear:[calendarDate yearOfCommonEra] month:[calendarDate monthOfYear] day:1 hour:0 minute:0 second:0 timeZone:[calendarDate timeZone]];
break;
case ZXThisYearReportTime:
self.reportStartDate = [NSCalendarDate dateWithYear:[calendarDate yearOfCommonEra] month:1 day:1 hour:0 minute:0 second:0 timeZone:[calendarDate timeZone]];
break;
case ZXLastYearReportTime:
self.reportStartDate = [NSCalendarDate dateWithYear:[calendarDate yearOfCommonEra] - 1 month:1 day:1 hour:0 minute:0 second:0 timeZone:[calendarDate timeZone]];
self.reportEndDate = [NSCalendarDate dateWithYear:[calendarDate yearOfCommonEra] month:1 day:1 hour:0 minute:0 second:0 timeZone:[calendarDate timeZone]];
break;
case ZXCustomReportTime:
default:
break;
}
}
// FIXME: Code from original Cashbox application. To be revised.
- (void) resetViewsPositions {
// wtf?
float difference = 1 - [textView frame].size.width;
// What is that?!
NSRect frame = [textView frame];
frame.size.width += difference; // is equal to frame.size.width = 1
frame.origin.x -= difference; // is equal to frame.origin.x += [textView frame].size.width - 1;
[textView setFrame:frame];
// Frame of text view is modified to have a width of 1 at the last pixel of previous frame
// Change size of graph, that may be useful
frame = [graphView frame];
frame.size.width -= difference; // frame.size.width += [textView frame].size.width - 1;
[graphView setFrame:frame];
// The graph fills the freed space
}
- (IBAction)toggleDetailBox:(id)sender
{
NSRect frame = [reportWindow frame];
frame.size.height += ([self.detailBoxHidden boolValue] ? 1: -1) * [detailBox frame].size.height;
frame.origin.y += ([self.detailBoxHidden boolValue] ? -1: 1) * [detailBox frame].size.height;
[[reportWindow animator] setFrame:frame display:YES];
self.detailBoxHidden = [NSNumber numberWithBool:([self.detailBoxHidden boolValue] ? NO: YES)];
}
- (void)setValue:(id)newValue forKey:(id)key
{
[super setValue:newValue forKey:key];
NSLog(@"Setting %@", key);
if([key isEqual:@"reportStartDate"] || [key isEqual:@"reportEndDate"]) {
[reportTimePopUpButton selectItemWithTag:ZXCustomReportTime];
[self updateView:self];
NSLog(@"Updating.");
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end