Skip to content

Commit a382a9f

Browse files
committed
Suggest ex-date from DividendEvents in transaction dialog
Issue: portfolio-performance#5439
1 parent 64566a4 commit a382a9f

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/dialogs/transactions/AccountTransactionDialog.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.math.RoundingMode;
1010
import java.text.MessageFormat;
1111
import java.time.LocalDate;
12+
import java.time.temporal.ChronoUnit;
1213
import java.util.ArrayList;
1314
import java.util.List;
1415

@@ -41,6 +42,7 @@
4142
import name.abuchen.portfolio.model.Client;
4243
import name.abuchen.portfolio.model.Portfolio;
4344
import name.abuchen.portfolio.model.Security;
45+
import name.abuchen.portfolio.model.SecurityEvent;
4446
import name.abuchen.portfolio.money.CurrencyConverter;
4547
import name.abuchen.portfolio.money.CurrencyConverterImpl;
4648
import name.abuchen.portfolio.money.ExchangeRateProviderFactory;
@@ -126,7 +128,8 @@ protected void createFormElements(Composite editArea) // NOSONAR
126128

127129
var exDate = new ExDateInput(editArea);
128130
exDate.bindDate(Properties.exDate.name());
129-
exDate.setVisible(model().supportsSecurity());
131+
exDate.setVisible(model().supportsSecurity() && model().getSecurity() != null
132+
&& !AccountTransactionModel.EMPTY_SECURITY.equals(model().getSecurity()));
130133

131134
// shares
132135

@@ -262,14 +265,36 @@ public void widgetSelected(SelectionEvent e)
262265
{
263266
startingWith(dateTime.button).thenRight(exDate.checkBox).thenRight(exDate.date.getControl());
264267

265-
var hasExDate = model().getExDate() != null;
268+
var hasRealSecurity = model().getSecurity() != null
269+
&& !AccountTransactionModel.EMPTY_SECURITY.equals(model().getSecurity());
270+
exDate.setVisible(hasRealSecurity);
271+
272+
var hasExDate = hasRealSecurity && model().getExDate() != null;
266273
toggleExDatePicker(exDate, hasExDate);
267274

268275
exDate.checkBox.addSelectionListener(SelectionListener.widgetSelectedAdapter(event -> {
269276
var button = (Button) event.widget;
270277
toggleExDatePicker(exDate, button.getSelection());
278+
if (button.getSelection())
279+
suggestExDate(exDate);
271280
editArea.layout();
272281
}));
282+
283+
model.addPropertyChangeListener(Properties.security.name(), event -> {
284+
var newSecurity = event.getNewValue();
285+
var isRealSecurity = newSecurity != null && !AccountTransactionModel.EMPTY_SECURITY.equals(newSecurity);
286+
exDate.setVisible(isRealSecurity);
287+
toggleExDatePicker(exDate, false);
288+
editArea.layout();
289+
});
290+
291+
model.addPropertyChangeListener(Properties.date.name(), event -> {
292+
if (exDate.checkBox.isVisible() && exDate.checkBox.getSelection())
293+
{
294+
suggestExDate(exDate);
295+
toggleExDatePicker(exDate, true);
296+
}
297+
});
273298
}
274299

275300
// shares [- amount per share]
@@ -380,7 +405,17 @@ private void toggleExDatePicker(ExDateInput exDate, boolean enable)
380405

381406
if (enable)
382407
{
383-
model().setExDate(exDate.date.getSelection().atStartOfDay());
408+
var exDateValue = model().getExDate();
409+
if (exDateValue == null)
410+
{
411+
var txDate = model.getDate();
412+
exDate.date.setSelection(txDate);
413+
model().setExDate(txDate.atStartOfDay());
414+
}
415+
else
416+
{
417+
model().setExDate(exDate.date.getSelection().atStartOfDay());
418+
}
384419
}
385420
else
386421
{
@@ -389,6 +424,36 @@ private void toggleExDatePicker(ExDateInput exDate, boolean enable)
389424
}
390425
}
391426

427+
private void suggestExDate(ExDateInput exDate)
428+
{
429+
var security = model().getSecurity();
430+
if (security == null || AccountTransactionModel.EMPTY_SECURITY.equals(security))
431+
return;
432+
433+
var txDate = model().getDate();
434+
435+
for (var event : security.getEvents())
436+
{
437+
if (!(event instanceof SecurityEvent.DividendEvent dividend))
438+
continue;
439+
440+
var paymentDate = dividend.getPaymentDate();
441+
if (paymentDate == null)
442+
continue;
443+
444+
if (Math.abs(ChronoUnit.DAYS.between(txDate, paymentDate)) <= 5)
445+
{
446+
var eventExDate = dividend.getDate();
447+
if (eventExDate == null)
448+
continue;
449+
450+
exDate.date.setSelection(eventExDate);
451+
model().setExDate(eventExDate.atStartOfDay());
452+
return;
453+
}
454+
}
455+
}
456+
392457
private ComboInput setupSecurities(Composite editArea)
393458
{
394459
List<Security> activeSecurities = new ArrayList<>();

0 commit comments

Comments
 (0)