-
Notifications
You must be signed in to change notification settings - Fork 0
/
finance.linq
108 lines (96 loc) · 3.95 KB
/
finance.linq
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
<Query Kind="Program">
<NuGetReference>DDay.iCal</NuGetReference>
<Namespace>Transaction = UserQuery.Transaction</Namespace>
<Namespace>DDay.iCal</Namespace>
</Query>
void Main()
{
var startDate = DateTime.Now;//.AddDays(1);
//var startDate = new DateTime(2015,3,16);
var endDate = new DateTime(2015,3,16);
var transactions = new List<Transaction>
{
new Transaction(703, "jan", new DateTime(2015, 2, 15)),
new Transaction(3, "equifax", new DateTime(2015, 2, 16)),
new Transaction(740, "rent", new DateTime(2015, 2, 17)),
new Transaction(6, "netflix", new DateTime(2015, 2, 21)),
new Transaction(10, "spotify", new DateTime(2015, 2, 23)),
new Transaction(25, "tesco", new DateTime(2015, 2, 25)),
new Transaction(470, "polska wesele", new DateTime(2015, 02, 03),1),
new Transaction(200, "polska karta I pozyczka", new DateTime(2015, 02, 03),1),
new Transaction(1400, "santander", new DateTime(2015, 02, 03),1),
new Transaction(449, "sofa", new DateTime(2015, 02, 20), 1),
new Transaction(150, "saving", new DateTime(2015, 3, 01)),
new Transaction(6.5, "account", new DateTime(2015, 3, 02)),
new Transaction(12.12, "tv", new DateTime(2015, 3, 02)),
new Transaction(15.1, "insurance", new DateTime(2015, 3, 03)),
new Transaction(19.29, "loyds", new DateTime(2015, 03, 06)),
new Transaction(291.67, "zopa", new DateTime(2015, 03, 09)),
new Transaction(505, "santander", new DateTime(2015, 3, 09)),
new Transaction(3.99, "creditex", new DateTime(2015, 03, 11)),
new Transaction(905.55, "halifax", new DateTime(2015, 03, 12)),
new Transaction(14.21, "bos", new DateTime(2015, 03, 12)),
};
//transactions.OrderBy(t=>t.Date).Dump();
transactions.FutureTransactions(startDate, endDate.Date).OrderBy(t=>t.Date).Dump();
}
public static class TransactionFilters
{
public static IEnumerable<Transaction> FutureTransactions(this IEnumerable<Transaction> transactions, DateTime startDate,DateTime endDate)
{
return transactions.SelectMany(transaction=>
{
var evnt = new Event();
evnt.Start = new iCalDateTime(transaction.Date);
evnt.Duration = new TimeSpan(1,0,0);
var rule = new RecurrencePattern(FrequencyType.Monthly);
evnt.RecurrenceRules.Add(rule);
if (transaction.NumberOfTimes.HasValue)
rule.Count = transaction.NumberOfTimes.Value;
rule.SetToLastDayOfTheMonth(transaction.Date.Day);
return RecurrenceUtil.GetOccurrences(evnt,new iCalDateTime(startDate.Date.AddDays(-1)),new iCalDateTime(endDate.Date.AddDays(1)), true)
.Select(o=>
{
var t = transaction.Clone();
t.Date = o.Period.StartTime.Date;
return t;
});
});
}
}
public static class RecurrencePatterExtensions
{
public static RecurrencePattern SetToLastDayOfTheMonth(this RecurrencePattern pattern, int eventDay)
{
if (eventDay==31)
{
pattern.ByMonthDay.Add(-1);
}
else if (eventDay>=29)
{
pattern.ByMonth.Add(2);
pattern.ByMonthDay.Add(-1);
}
return pattern;
}
}
public class Transaction
{
public Transaction(double amount, string description, DateTime date, int? numberOfTimes=null)
{
Amount = amount;
Description = description;
Date = date;
NumberOfTimes = numberOfTimes;
}
public string Description { get; set; }
public double Amount { get; set; }
public DateTime Date { get; set; }
public int? NumberOfTimes { get; set; }
public DateTime? EndDate { get; set; }
public Transaction Clone()
{
return (Transaction)this.MemberwiseClone();
}
}
// Define other methods and classes here