forked from portfolio-performance/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuySellModel.java
142 lines (117 loc) · 4.47 KB
/
BuySellModel.java
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
package name.abuchen.portfolio.ui.dialogs.transactions;
import java.time.LocalDateTime;
import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.BuySellEntry;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.Transaction;
import name.abuchen.portfolio.model.TransactionOwner;
import name.abuchen.portfolio.ui.Messages;
/* package */class BuySellModel extends AbstractSecurityTransactionModel
{
private BuySellEntry source;
protected Account account;
public BuySellModel(Client client, PortfolioTransaction.Type type)
{
super(client, type);
if (!accepts(type))
throw new IllegalArgumentException();
}
@Override
public boolean accepts(PortfolioTransaction.Type type)
{
return type == PortfolioTransaction.Type.BUY
|| type == PortfolioTransaction.Type.SELL
|| type == PortfolioTransaction.Type.COVER
|| type == PortfolioTransaction.Type.SHORT;
}
@Override
public void setSource(Object entry)
{
this.source = (BuySellEntry) entry;
this.type = source.getPortfolioTransaction().getType();
this.portfolio = (Portfolio) source.getOwner(source.getPortfolioTransaction());
this.account = (Account) source.getOwner(source.getAccountTransaction());
fillFromTransaction(source.getPortfolioTransaction());
}
@Override
public boolean hasSource()
{
return source != null;
}
@Override
public void applyChanges()
{
if (security == null)
throw new UnsupportedOperationException(Messages.MsgMissingSecurity);
if (account == null)
throw new UnsupportedOperationException(Messages.MsgMissingReferenceAccount);
BuySellEntry entry;
if (source != null && source.getOwner(source.getPortfolioTransaction()).equals(portfolio)
&& source.getOwner(source.getAccountTransaction()).equals(account))
{
entry = source;
}
else
{
if (source != null)
{
@SuppressWarnings("unchecked")
TransactionOwner<Transaction> owner = (TransactionOwner<Transaction>) source
.getOwner(source.getPortfolioTransaction());
owner.deleteTransaction(source.getPortfolioTransaction(), client);
source = null;
}
entry = new BuySellEntry(portfolio, account);
entry.setCurrencyCode(account.getCurrencyCode());
entry.insert();
}
entry.setDate(LocalDateTime.of(date, time));
entry.setCurrencyCode(account.getCurrencyCode());
entry.setSecurity(security);
entry.setShares(shares);
entry.setAmount(total);
entry.setType(type);
entry.setNote(note);
writeToTransaction(entry.getPortfolioTransaction());
}
@Override
public void resetToNewTransaction()
{
this.source = null;
super.resetToNewTransaction();
}
@Override
public void setPortfolio(Portfolio portfolio)
{
setAccount(portfolio.getReferenceAccount());
super.setPortfolio(portfolio);
}
public Account getAccount()
{
return account;
}
public void setAccount(Account account)
{
String oldAccountCurrency = getTransactionCurrencyCode();
String oldExchangeRateCurrencies = getExchangeRateCurrencies();
String oldInverseExchangeRateCurrencies = getInverseExchangeRateCurrencies();
firePropertyChange(Properties.account.name(), this.account, this.account = account); // NOSONAR
firePropertyChange(Properties.transactionCurrencyCode.name(), oldAccountCurrency, getTransactionCurrencyCode());
firePropertyChange(Properties.exchangeRateCurrencies.name(), oldExchangeRateCurrencies,
getExchangeRateCurrencies());
firePropertyChange(Properties.inverseExchangeRateCurrencies.name(), oldInverseExchangeRateCurrencies,
getInverseExchangeRateCurrencies());
if (getSecurity() != null)
{
updateSharesAndQuote();
updateExchangeRate();
}
}
@Override
public String getTransactionCurrencyCode()
{
return account != null ? account.getCurrencyCode() : ""; //$NON-NLS-1$
}
}