-
Notifications
You must be signed in to change notification settings - Fork 5
/
Report.cs
217 lines (183 loc) · 6.07 KB
/
Report.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace CCTime
{
class Report
{
public Report( DateTime currentDate)
{
var d = currentDate;
var currentWeek = Utilities.GetIso8601WeekOfYear( currentDate );
var dayNames = new string[7];
// Find start of week
while( currentWeek == Utilities.GetIso8601WeekOfYear( d.AddDays( -1 ) ) )
{
d = d.AddDays( -1 );
}
var chart = new Dictionary<string, int[]>();
// Collect data from all days
var satTotals = 0;
var sunTotals = 0;
var foundUnallocatedHours = false;
for( int i = 0; i < 7; i++ )
{
dayNames[i] = d.ToString( "ddd" ); // Gets the localized day name
var data = TaskManager.GetDay( d );
if( data != null )
{
foreach( var task in data )
{
if(task.Title == Settings.UnaccountedString && task.Minutes != 0)
{
foundUnallocatedHours = true;
}
if( !chart.ContainsKey( task.Title ) )
{
chart.Add( task.Title, new int[7] );
}
chart[task.Title][i] += task.Minutes;
if( i == 5 ) satTotals += task.Minutes;
if( i == 6 ) sunTotals += task.Minutes;
}
}
d = d.AddDays( 1 );
}
if( chart.Count <= 2 )
{
MessageBox.Show( "Nothing to show yet...", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name );
return;
}
if( foundUnallocatedHours && Settings.ReportWarnIfUnaccountedHours )
{
MessageBox.Show( "Warning: There are still hours left unaccounted for this week...", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name, MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
}
var totals = chart[Settings.TotalsString];
chart.Remove( Settings.TotalsString );
var unaccounted = chart[Settings.UnaccountedString];
chart.Remove( Settings.UnaccountedString );
var onlyTitles = chart.Select( x => x.Key.Split(';')[0] );
var longestTaskName = onlyTitles.Max( s => s.Length );
var startIndent = Math.Max( longestTaskName, 16 );
// Remove sat/sun if empty
var lastDayIndex = 7;
if( sunTotals == 0 )
{
lastDayIndex = 6;
if( satTotals == 0 )
{
lastDayIndex = 5;
}
}
// Generate table
// Header: Week + Day names
var report = ( Settings.ReportWeekString + " " + currentWeek.ToString() ).PadRight( startIndent );
for( int i = 0; i < lastDayIndex; i++ )
{
report += dayNames[i].PadLeft( Settings.ReportTabSize );
}
report += System.Environment.NewLine + System.Environment.NewLine;
// Each task
foreach( var task in chart )
{
if( task.Value.Sum() > 0 ) // skip task with no hours whole week
{
string[] tmp = task.Key.Split( ';' );
var title = tmp[0];
var comment = tmp.Length > 1 ? tmp[1] : "";
report += title.PadRight( startIndent );
for( int i = 0; i < lastDayIndex; i++ )
{
var hours = FormatTime( task.Value[i] );
if( hours == "0" )
report += Settings.ReportZeroIndicator.PadLeft( Settings.ReportTabSize );
else
report += hours.PadLeft( Settings.ReportTabSize );
}
if( Settings.ReportShowTotalsColumn )
{
var sum = FormatTime( task.Value.Sum() );
sum = sum.PadRight( 4 ); // helps aligning comment below correctly
report += ( "".PadLeft( Settings.ReportTabSize ) + "= " + sum );
}
if( comment != "" )
{
report += ( "".PadLeft( Settings.ReportTabSize ) + comment );
}
report += System.Environment.NewLine;
}
}
// Add totals below all tasks
if( Settings.ReportShowTotalsRow )
{
report += System.Environment.NewLine;
report += Settings.ReportTotalsString.PadRight( startIndent );
for( int i = 0; i < lastDayIndex; i++ )
{
var hours = FormatTime( totals[i] );
report += hours.PadLeft( Settings.ReportTabSize );
}
if( Settings.ReportShowTotalsColumn )
{
report += ( "".PadLeft( Settings.ReportTabSize ) + "= " + FormatTime( totals.Sum() ) );
}
}
report += System.Environment.NewLine;
// Finally unaccounted minutes
if( Settings.ReportShowUnaccountedRow && foundUnallocatedHours )
{
report += Settings.ReportUnaccountedString.PadRight( startIndent );
for( int i = 0; i < lastDayIndex; i++ )
{
var hours = FormatTime( unaccounted[i] );
report += hours.PadLeft( Settings.ReportTabSize );
}
}
report += System.Environment.NewLine;
// Show the report
ShowReportForm( "Report for week " + currentWeek, report );
if( Settings.ReportAutoCopyToClipboard )
{
Clipboard.SetText( report );
}
}
private void ShowReportForm( string title, string data)
{
Form form = new Form();
TextBox textBox = new TextBox();
textBox.Multiline = true;
textBox.WordWrap = false;
textBox.ScrollBars = ScrollBars.Both;
textBox.Font = new Font( "Consolas", 12, FontStyle.Regular );
textBox.Anchor = textBox.Anchor | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom;
form.ClientSize = new System.Drawing.Size( Settings.ReportWindowWidth, Settings.ReportWindowHeight );
textBox.SetBounds( 0, 0, Settings.ReportWindowWidth, Settings.ReportWindowHeight );
form.Controls.AddRange( new Control[] { textBox } );
form.StartPosition = FormStartPosition.CenterScreen;
textBox.Text = data;
form.Text = title;
textBox.Focus();
form.KeyPreview = true;
form.KeyUp += new KeyEventHandler( delegate( object sender, KeyEventArgs e ) { if( e.KeyCode == Keys.Escape ) ( sender as Form ).Close(); });
textBox.KeyDown += new KeyEventHandler( delegate( object sender, KeyEventArgs e ) { if( e.Control && e.KeyCode == Keys.A ) { e.SuppressKeyPress = true; textBox.SelectAll(); } } );
form.Show();
textBox.SelectionLength = 0;
}
private string FormatTime(int minutes)
{
switch( Settings.ReportTimeFormat.ToLower() )
{
case "minutes":
return minutes.ToString();
case "hours":
return Utilities.MinutesToHours( minutes );
case "fractional":
default:
return Utilities.MinutesToFractionalHours( minutes );
}
}
}
}