forked from portfolio-performance/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountTransactionModel.java
639 lines (524 loc) · 20.3 KB
/
AccountTransactionModel.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package name.abuchen.portfolio.ui.dialogs.transactions;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import com.ibm.icu.text.MessageFormat;
import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.AccountTransaction;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.Transaction;
import name.abuchen.portfolio.money.CurrencyConverter;
import name.abuchen.portfolio.money.CurrencyConverterImpl;
import name.abuchen.portfolio.money.ExchangeRate;
import name.abuchen.portfolio.money.ExchangeRateTimeSeries;
import name.abuchen.portfolio.money.Money;
import name.abuchen.portfolio.money.Values;
import name.abuchen.portfolio.snapshot.ClientSnapshot;
import name.abuchen.portfolio.snapshot.SecurityPosition;
import name.abuchen.portfolio.ui.Messages;
public class AccountTransactionModel extends AbstractModel
{
public enum Properties
{
security, account, date, time, shares, fxGrossAmount, dividendAmount, exchangeRate, inverseExchangeRate, grossAmount, // NOSONAR
fxTaxes, taxes, total, note, exchangeRateCurrencies, inverseExchangeRateCurrencies, // NOSONAR
accountCurrencyCode, securityCurrencyCode, fxCurrencyCode, calculationStatus; // NOSONAR
}
public static final Security EMPTY_SECURITY = new Security("-----", ""); //$NON-NLS-1$ //$NON-NLS-2$
private final Client client;
private AccountTransaction.Type type;
private Account sourceAccount;
private AccountTransaction sourceTransaction;
private Security security;
private Account account;
private LocalDate date = LocalDate.now();
private LocalTime time = LocalTime.MIDNIGHT;
private long shares;
private long fxGrossAmount;
private BigDecimal dividendAmount = BigDecimal.ZERO;
private BigDecimal exchangeRate = BigDecimal.ONE;
private long grossAmount;
private long fxTaxes;
private long taxes;
private long total;
private String note;
private IStatus calculationStatus = ValidationStatus.ok();
public AccountTransactionModel(Client client, AccountTransaction.Type type)
{
this.client = client;
this.type = type;
checkType();
}
@Override
public String getHeading()
{
return type.toString();
}
private void checkType()
{
switch (type)
{
case DEPOSIT:
case REMOVAL:
case FEES:
case FEES_REFUND:
case TAXES:
case TAX_REFUND:
case INTEREST:
case INTEREST_CHARGE:
case DIVIDENDS:
return;
case BUY:
case SELL:
case COVER:
case SHORT:
case TRANSFER_IN:
case TRANSFER_OUT:
default:
throw new UnsupportedOperationException();
}
}
@Override
public void applyChanges()
{
if (security == null && supportsSecurity() && !supportsOptionalSecurity())
throw new UnsupportedOperationException(Messages.MsgMissingSecurity);
if (account == null)
throw new UnsupportedOperationException(Messages.MsgMissingAccount);
AccountTransaction t;
if (sourceTransaction != null && sourceAccount.equals(account))
{
// transactions stays in same account
t = sourceTransaction;
}
else
{
if (sourceTransaction != null)
{
sourceAccount.deleteTransaction(sourceTransaction, client);
sourceTransaction = null;
sourceAccount = null;
}
t = new AccountTransaction();
t.setCurrencyCode(getAccountCurrencyCode());
account.addTransaction(t);
}
t.setDateTime(LocalDateTime.of(date, time));
t.setSecurity(!EMPTY_SECURITY.equals(security) ? security : null);
t.setShares(supportsShares() ? shares : 0);
t.setAmount(total);
t.setType(type);
t.setNote(note);
t.clearUnits();
if (taxes != 0)
t.addUnit(new Transaction.Unit(Transaction.Unit.Type.TAX, Money.of(getAccountCurrencyCode(), taxes)));
String fxCurrencyCode = getFxCurrencyCode();
if (!fxCurrencyCode.equals(account.getCurrencyCode()))
{
Transaction.Unit forex = new Transaction.Unit(Transaction.Unit.Type.GROSS_VALUE, //
Money.of(getAccountCurrencyCode(), grossAmount), //
Money.of(getSecurityCurrencyCode(), fxGrossAmount), //
getExchangeRate());
t.addUnit(forex);
if (fxTaxes != 0)
t.addUnit(new Transaction.Unit(Transaction.Unit.Type.TAX, //
Money.of(getAccountCurrencyCode(), Math.round(fxTaxes * exchangeRate.doubleValue())), //
Money.of(getSecurityCurrencyCode(), fxTaxes), //
exchangeRate));
}
}
@Override
public void resetToNewTransaction()
{
this.sourceAccount = null;
this.sourceTransaction = null;
setFxGrossAmount(0);
setDividendAmount(BigDecimal.ZERO);
setGrossAmount(0);
setTaxes(0);
setFxTaxes(0);
setNote(null);
}
public boolean supportsShares()
{
return type == AccountTransaction.Type.DIVIDENDS;
}
public boolean supportsSecurity()
{
switch (type)
{
case DIVIDENDS:
case TAXES:
case TAX_REFUND:
case FEES:
case FEES_REFUND:
return true;
default:
return false;
}
}
public boolean supportsOptionalSecurity()
{
switch (type)
{
case TAXES:
case TAX_REFUND:
case FEES:
case FEES_REFUND:
return true;
default:
return false;
}
}
public boolean supportsTaxUnits()
{
return type == AccountTransaction.Type.DIVIDENDS || type == AccountTransaction.Type.INTEREST;
}
public void setSource(Account account, AccountTransaction transaction)
{
this.sourceAccount = account;
this.sourceTransaction = transaction;
this.security = transaction.getSecurity();
if (this.security == null && supportsOptionalSecurity())
this.security = EMPTY_SECURITY;
this.account = account;
LocalDateTime transactionDate = transaction.getDateTime();
this.date = transactionDate.toLocalDate();
this.time = transactionDate.toLocalTime();
this.shares = transaction.getShares();
this.total = transaction.getAmount();
// both will be overwritten if forex data exists
this.exchangeRate = BigDecimal.ONE;
this.taxes = 0;
this.fxTaxes = 0;
transaction.getUnits().forEach(unit -> {
switch (unit.getType())
{
case GROSS_VALUE:
this.exchangeRate = unit.getExchangeRate();
this.grossAmount = unit.getAmount().getAmount();
this.fxGrossAmount = unit.getForex().getAmount();
break;
case TAX:
if (unit.getForex() != null)
this.fxTaxes += unit.getForex().getAmount();
else
this.taxes += unit.getAmount().getAmount();
break;
default:
throw new UnsupportedOperationException();
}
});
this.grossAmount = calculateGrossAmount4Total();
// in case units have to forex gross value
if (exchangeRate.equals(BigDecimal.ONE))
this.fxGrossAmount = grossAmount;
this.dividendAmount = calculateDividendAmount();
this.note = transaction.getNote();
}
@Override
public IStatus getCalculationStatus()
{
return calculationStatus;
}
/**
* Due to the limited precision of the exchange rate (4 digits), the amount
* is checked against a range.
*/
private IStatus calculateStatus()
{
// check whether converted amount is in range
long upper = Math.round(fxGrossAmount * exchangeRate.add(BigDecimal.valueOf(0.0001)).doubleValue());
long lower = Math.round(fxGrossAmount * exchangeRate.add(BigDecimal.valueOf(-0.0001)).doubleValue());
if (grossAmount < lower || grossAmount > upper)
return ValidationStatus.error(Messages.MsgErrorConvertedAmount);
if (grossAmount == 0L)
return ValidationStatus.error(MessageFormat.format(Messages.MsgDialogInputRequired, Messages.ColumnTotal));
return ValidationStatus.ok();
}
public Account getAccount()
{
return account;
}
public void setAccount(Account account)
{
String oldCurrencyCode = getAccountCurrencyCode();
String oldFxCurrencyCode = getFxCurrencyCode();
String oldExchangeRateCurrencies = getExchangeRateCurrencies();
String oldInverseExchangeRateCurrencies = getInverseExchangeRateCurrencies();
firePropertyChange(Properties.account.name(), this.account, this.account = account);
firePropertyChange(Properties.accountCurrencyCode.name(), oldCurrencyCode, getAccountCurrencyCode());
firePropertyChange(Properties.fxCurrencyCode.name(), oldFxCurrencyCode, getFxCurrencyCode());
firePropertyChange(Properties.exchangeRateCurrencies.name(), oldExchangeRateCurrencies,
getExchangeRateCurrencies());
firePropertyChange(Properties.inverseExchangeRateCurrencies.name(), oldInverseExchangeRateCurrencies,
getInverseExchangeRateCurrencies());
updateExchangeRate();
}
public Security getSecurity()
{
return security;
}
public void setSecurity(Security security)
{
if (!supportsSecurity())
return;
String oldCurrencyCode = getSecurityCurrencyCode();
String oldFxCurrencyCode = getFxCurrencyCode();
String oldExchangeRateCurrencies = getExchangeRateCurrencies();
String oldInverseExchangeRateCurrencies = getInverseExchangeRateCurrencies();
firePropertyChange(Properties.security.name(), this.security, this.security = security);
firePropertyChange(Properties.securityCurrencyCode.name(), oldCurrencyCode, getSecurityCurrencyCode());
firePropertyChange(Properties.fxCurrencyCode.name(), oldFxCurrencyCode, getFxCurrencyCode());
firePropertyChange(Properties.exchangeRateCurrencies.name(), oldExchangeRateCurrencies,
getExchangeRateCurrencies());
firePropertyChange(Properties.inverseExchangeRateCurrencies.name(), oldInverseExchangeRateCurrencies,
getInverseExchangeRateCurrencies());
updateExchangeRate();
updateShares();
}
private void updateExchangeRate()
{
// do not auto-suggest exchange rates when editing an existing
// transaction
if (sourceTransaction != null)
return;
if (getAccountCurrencyCode().equals(getSecurityCurrencyCode()))
{
setExchangeRate(BigDecimal.ONE);
}
else if (!getSecurityCurrencyCode().isEmpty())
{
ExchangeRateTimeSeries series = getExchangeRateProviderFactory() //
.getTimeSeries(getSecurityCurrencyCode(), getAccountCurrencyCode());
if (series != null)
setExchangeRate(series.lookupRate(date).orElse(new ExchangeRate(date, BigDecimal.ONE)).getValue());
else
setExchangeRate(BigDecimal.ONE);
}
}
private void updateShares()
{
// do not auto-suggest shares and quote when editing an existing
// transaction
if (sourceTransaction != null)
return;
if (!supportsShares() || security == null)
return;
CurrencyConverter converter = new CurrencyConverterImpl(getExchangeRateProviderFactory(),
client.getBaseCurrency());
ClientSnapshot snapshot = ClientSnapshot.create(client, converter, date);
SecurityPosition p = snapshot.getJointPortfolio().getPositionsBySecurity().get(security);
setShares(p != null ? p.getShares() : 0);
}
public LocalDate getDate()
{
return date;
}
public void setDate(LocalDate date)
{
firePropertyChange(Properties.date.name(), this.date, this.date = date);
updateShares();
updateExchangeRate();
}
public LocalTime getTime()
{
return time;
}
public void setTime(LocalTime time)
{
firePropertyChange(Properties.time.name(), this.time, this.time = time);
}
public long getShares()
{
return shares;
}
public void setShares(long shares)
{
firePropertyChange(Properties.shares.name(), this.shares, this.shares = shares);
firePropertyChange(Properties.dividendAmount.name(), this.dividendAmount,
this.dividendAmount = calculateDividendAmount());
}
public long getFxGrossAmount()
{
return fxGrossAmount;
}
public void setFxGrossAmount(long foreignCurrencyAmount)
{
firePropertyChange(Properties.fxGrossAmount.name(), this.fxGrossAmount,
this.fxGrossAmount = foreignCurrencyAmount);
triggerGrossAmount(Math.round(exchangeRate.doubleValue() * foreignCurrencyAmount));
firePropertyChange(Properties.dividendAmount.name(), this.dividendAmount,
this.dividendAmount = calculateDividendAmount());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public BigDecimal getDividendAmount()
{
return dividendAmount;
}
public void setDividendAmount(BigDecimal amount)
{
triggerDividendAmount(amount);
long myGrossAmount = calculateGrossAmount4Dividend();
setFxGrossAmount(myGrossAmount);
}
public void triggerDividendAmount(BigDecimal amount)
{
firePropertyChange(Properties.dividendAmount.name(), this.dividendAmount, this.dividendAmount = amount);
}
public BigDecimal getExchangeRate()
{
return exchangeRate;
}
public void setExchangeRate(BigDecimal exchangeRate)
{
BigDecimal newRate = exchangeRate == null ? BigDecimal.ZERO : exchangeRate;
BigDecimal oldInverseRate = getInverseExchangeRate();
firePropertyChange(Properties.exchangeRate.name(), this.exchangeRate, this.exchangeRate = newRate);
firePropertyChange(Properties.inverseExchangeRate.name(), oldInverseRate, getInverseExchangeRate());
triggerGrossAmount(Math.round(newRate.doubleValue() * fxGrossAmount));
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public BigDecimal getInverseExchangeRate()
{
return BigDecimal.ONE.divide(exchangeRate, 10, RoundingMode.HALF_DOWN);
}
public void setInverseExchangeRate(BigDecimal rate)
{
setExchangeRate(BigDecimal.ONE.divide(rate, 10, RoundingMode.HALF_DOWN));
}
public long getGrossAmount()
{
return grossAmount;
}
public void setGrossAmount(long amount)
{
triggerGrossAmount(amount);
if (fxGrossAmount != 0)
{
BigDecimal newExchangeRate = BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(fxGrossAmount), 10,
RoundingMode.HALF_UP);
BigDecimal oldInverseRate = getInverseExchangeRate();
firePropertyChange(Properties.exchangeRate.name(), this.exchangeRate, this.exchangeRate = newExchangeRate);
firePropertyChange(Properties.inverseExchangeRate.name(), oldInverseRate, getInverseExchangeRate());
}
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public void triggerGrossAmount(long amount)
{
firePropertyChange(Properties.grossAmount.name(), this.grossAmount, this.grossAmount = amount);
triggerTotal(calculateTotal());
}
public long getFxTaxes()
{
return fxTaxes;
}
public void setFxTaxes(long fxTaxes)
{
firePropertyChange(Properties.fxTaxes.name(), this.fxTaxes, this.fxTaxes = fxTaxes);
triggerTotal(calculateTotal());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getTaxes()
{
return taxes;
}
public void setTaxes(long taxes)
{
firePropertyChange(Properties.taxes.name(), this.taxes, this.taxes = taxes);
triggerTotal(calculateTotal());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getTotal()
{
return total;
}
public void setTotal(long total)
{
triggerTotal(total);
firePropertyChange(Properties.grossAmount.name(), this.grossAmount,
this.grossAmount = calculateGrossAmount4Total());
firePropertyChange(Properties.fxGrossAmount.name(), this.fxGrossAmount,
this.fxGrossAmount = Math.round(grossAmount / exchangeRate.doubleValue()));
firePropertyChange(Properties.dividendAmount.name(), this.dividendAmount,
this.dividendAmount = calculateDividendAmount());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public void triggerTotal(long total)
{
firePropertyChange(Properties.total.name(), this.total, this.total = total);
}
protected BigDecimal calculateDividendAmount()
{
if (shares > 0)
return BigDecimal.valueOf(
(fxGrossAmount * Values.Share.factor()) / (double) shares / Values.Amount.divider());
else
return BigDecimal.ZERO;
}
protected long calculateGrossAmount4Total()
{
long totalTaxes = taxes + Math.round(exchangeRate.doubleValue() * fxTaxes);
return total + totalTaxes;
}
protected long calculateGrossAmount4Dividend()
{
return Math.round((shares * dividendAmount.doubleValue() * Values.Amount.factor())
/ (double) Values.Share.factor());
}
private long calculateTotal()
{
long totalTaxes = taxes + Math.round(exchangeRate.doubleValue() * fxTaxes);
return Math.max(0, grossAmount - totalTaxes);
}
public String getNote()
{
return note;
}
public void setNote(String note)
{
firePropertyChange(Properties.note.name(), this.note, this.note = note);
}
public String getAccountCurrencyCode()
{
return account != null ? account.getCurrencyCode() : ""; //$NON-NLS-1$
}
public String getSecurityCurrencyCode()
{
return security != null ? security.getCurrencyCode() : ""; //$NON-NLS-1$
}
public String getFxCurrencyCode()
{
return security != null && !security.getCurrencyCode().isEmpty() ? security.getCurrencyCode()
: getAccountCurrencyCode();
}
/**
* Returns exchange rate label in direct (price) notation.
*/
public String getExchangeRateCurrencies()
{
return String.format("%s/%s", getSecurityCurrencyCode(), getAccountCurrencyCode()); //$NON-NLS-1$
}
/**
* Returns exchange rate label in indirect (quantity) notation.
*/
public String getInverseExchangeRateCurrencies()
{
return String.format("%s/%s", getAccountCurrencyCode(), getSecurityCurrencyCode()); //$NON-NLS-1$
}
public AccountTransaction.Type getType()
{
return type;
}
}