-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transaction.cs
143 lines (110 loc) · 4.4 KB
/
Transaction.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
using CoinStory.Models.Enumerations;
using CoinStory.Models.Interfaces;
namespace CoinStory.Models
{
/// <summary>
/// Contains all data regarding an operation that involves some amount of coins
/// </summary>
public class Transaction : ITransaction
{
private DateTime _date;
public int ID { get; set; }
public string? Identifier { get; set; }
public DateTime Date
{
get => _date;
set => _date = value.Kind != DateTimeKind.Utc
? DateTime.SpecifyKind(value, DateTimeKind.Utc)
: value;
}
public Platform Platform { get; set; }
public virtual decimal AmountIn { get; set; }
public Currency? CurrencyIn { get; set; }
public virtual decimal AmountOut { get; set; }
public Currency? CurrencyOut { get; set; }
public virtual decimal UsdValueOut { get; set; }
public virtual decimal Fee { get; set; }
public Currency? FeeCurrency { get; set; }
public virtual decimal UsdFeeValue { get; set; }
public virtual decimal? CapitalChange { get; set; }
public TransactionType Type { get; set; }
public string? Metadata { get; set; }
public string? Comments { get; set; }
public bool Ignored { get; set; }
public bool Merged { get; set; }
public bool Manual { get; set; }
public Transaction(string identifier, DateTime date, decimal amountIn, Currency? currencyIn, decimal amountOut, Currency? currencyOut, TransactionType type, Platform platform, decimal fee, Currency? feeCurrency, string comments) : this()
{
Identifier = identifier;
Date = date;
AmountIn = amountIn;
CurrencyIn = currencyIn;
AmountOut = amountOut;
CurrencyOut = currencyOut;
Type = type;
Platform = platform;
Fee = fee;
FeeCurrency = feeCurrency;
Metadata = comments;
}
public Transaction()
{
}
public string ToCSV() => string.Join(",", new string[]
{
Date.ToShortDateString() + " " + Date.ToShortTimeString(),
Platform.ToString(),
AmountIn.ToString(".################"),
CurrencyIn?.ToString() ?? "",
AmountOut.ToString(".################"),
CurrencyOut?.ToString() ?? "",
Fee.ToString(".################"),
FeeCurrency?.ToString() ?? "",
Identifier ?? "",
Type.ToString(),
Metadata ?? ""
});
public override bool Equals(object? obj)
{
if (obj is ITransaction txn)
{
if (Platform != txn.Platform || Type != txn.Type && Type != TransactionType.Transfer && txn.Type != TransactionType.Transfer)
return false;
if (!string.IsNullOrEmpty(Identifier) && Identifier == txn.Identifier)
return true;
return Date == txn.Date &&
CurrencyIn == txn.CurrencyIn &&
CurrencyOut == txn.CurrencyOut &&
AmountIn == txn.AmountIn &&
AmountOut == txn.AmountOut;
}
return base.Equals(obj);
}
public override int GetHashCode()
{
HashCode hash = new HashCode();
hash.Add(ID);
hash.Add(Identifier);
hash.Add(Date);
hash.Add(Platform);
hash.Add(AmountIn);
hash.Add(CurrencyIn);
hash.Add(AmountOut);
hash.Add(CurrencyOut);
hash.Add(UsdValueOut);
hash.Add(Fee);
hash.Add(FeeCurrency);
hash.Add(UsdFeeValue);
hash.Add(CapitalChange);
hash.Add(Type);
hash.Add(Metadata);
hash.Add(Comments);
hash.Add(Ignored);
hash.Add(Merged);
hash.Add(Manual);
return hash.ToHashCode();
}
public static bool operator ==(Transaction txn1, Transaction txn2) => txn1 is null ? txn2 is null : Equals(txn1, txn2);
public static bool operator !=(Transaction txn1, Transaction txn2) => !(txn1 == txn2);
}
}