diff --git a/bundles/ch.elexis.core.data/src/ch/elexis/data/Rechnung.java b/bundles/ch.elexis.core.data/src/ch/elexis/data/Rechnung.java index 288eb96efd..5be5c08c20 100644 --- a/bundles/ch.elexis.core.data/src/ch/elexis/data/Rechnung.java +++ b/bundles/ch.elexis.core.data/src/ch/elexis/data/Rechnung.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.List; +import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -33,10 +34,12 @@ import ch.elexis.core.data.service.CoreModelServiceHolder; import ch.elexis.core.data.service.LocalLockServiceHolder; import ch.elexis.core.events.MessageEvent; +import ch.elexis.core.model.IContact; import ch.elexis.core.model.IInvoice; import ch.elexis.core.model.InvoiceState; import ch.elexis.core.services.IInvoiceService; import ch.elexis.core.services.holder.ConfigServiceHolder; +import ch.elexis.core.services.holder.ContextServiceHolder; import ch.rgw.tools.JdbcLink; import ch.rgw.tools.JdbcLink.Stm; import ch.rgw.tools.Money; @@ -83,6 +86,7 @@ public class Rechnung extends PersistentObject { public static final String OUTPUT = "Ausgegeben"; public static final String REMARKS = "Bemerkungen"; public static final String INVOICE_CORRECTION = "Rechnungskorrektur"; + public static final String MANDATOR = "Mandator"; static { addMapping(TABLENAME, BILL_NUMBER, CASE_ID, MANDATOR_ID, "RnDatum=S:D:RnDatum", BILL_STATE, @@ -597,9 +601,14 @@ public void setTemporaryState(final int temporaryState, TimeTool expiryDate) { * @param state as defined by {@link InvoiceState#numericValue()} */ public void setStatus(final InvoiceState state) { - set(BILL_STATE, Integer.toString(state.getState())); + set(BILL_STATE, String.valueOf(state.getState())); set(BILL_STATE_DATE, new TimeTool().toString(TimeTool.DATE_GER)); - addTrace(STATUS_CHANGED, Integer.toString(state.getState())); + addTrace(STATUS_CHANGED, String.valueOf(state.getState())); + ContextServiceHolder.get().getActiveUserContact().ifPresent(activeUserContact -> { + String userDescription = String.format("%s %s", activeUserContact.getDescription1(), //$NON-NLS-1$ + activeUserContact.getDescription2()); + addTrace(MANDATOR, userDescription); + }); } /** diff --git a/bundles/ch.elexis.core.model/src/ch/elexis/core/model/Invoice.java b/bundles/ch.elexis.core.model/src/ch/elexis/core/model/Invoice.java index ff15f46736..86dc4b3a11 100644 --- a/bundles/ch.elexis.core.model/src/ch/elexis/core/model/Invoice.java +++ b/bundles/ch.elexis.core.model/src/ch/elexis/core/model/Invoice.java @@ -18,6 +18,7 @@ import ch.elexis.core.model.service.holder.StoreToStringServiceHolder; import ch.elexis.core.model.util.internal.ModelUtil; import ch.elexis.core.services.INamedQuery; +import ch.elexis.core.services.holder.ContextServiceHolder; import ch.rgw.tools.Money; import ch.rgw.tools.StringTool; import ch.rgw.tools.TimeTool; @@ -54,6 +55,11 @@ public void setState(InvoiceState value) { setStateDate(LocalDate.now()); getEntityMarkDirty().setState(value); addTrace(InvoiceConstants.STATUS_CHANGED, Integer.toString(value.numericValue())); + ContextServiceHolder.get().getActiveUserContact().ifPresent(activeUserContact -> { + String userDescription = String.format("%s %s", activeUserContact.getDescription1(), //$NON-NLS-1$ + activeUserContact.getDescription2()); + addTrace(InvoiceConstants.MANDATOR, userDescription); + }); } @Override diff --git a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/views/rechnung/RechnungsBlatt.java b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/views/rechnung/RechnungsBlatt.java index 46cb39dcbd..f8eda1629b 100644 --- a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/views/rechnung/RechnungsBlatt.java +++ b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/views/rechnung/RechnungsBlatt.java @@ -20,8 +20,10 @@ import static ch.elexis.core.ui.constants.UiPreferenceConstants.USERSETTINGS2_EXPANDABLE_COMPOSITES; import static ch.elexis.core.ui.constants.UiPreferenceConstants.USERSETTINGS2_EXPANDABLE_COMPOSITES_STATES; +import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Objects; @@ -824,13 +826,10 @@ public void display() { + ((adressat != null) ? adressat.getLabel() : StringUtils.EMPTY)); form.setText(actRn.getLabel()); List trace = actRn.getTrace(Rechnung.STATUS_CHANGED); - for (String s : trace) { - String[] stm = s.split("\\s*:\\s"); //$NON-NLS-1$ - StringBuilder sb = new StringBuilder(); - sb.append(stm[0]).append(" : ").append( //$NON-NLS-1$ - InvoiceState.fromState(Integer.parseInt(stm[1])).getLocaleText()); - lbJournal.add(sb.toString()); - } + List mandatorTrace = actRn.getTrace(Rechnung.MANDATOR); + List combinedTrace = combineAndSortTrace(trace, mandatorTrace); + combinedTrace.forEach(lbJournal::add); + if (journalSelection != null && journalSelection.length > 0) { for (int i = 0; i < lbJournal.getItemCount(); i++) { for (String selection : journalSelection) { @@ -890,4 +889,39 @@ public void display() { } } + private List combineAndSortTrace(List trace, List mandatorTrace) { + SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss"); //$NON-NLS-1$ + return trace.stream().map(statusEntry -> { + // Split the status entry into a timestamp and a status code. + String[] stm = statusEntry.split("\\s*:\\s", 2); //$NON-NLS-1$ + String timestampString = stm[0]; // Extract the timestamp part. + String statusText = timestampString + " : " //$NON-NLS-1$ + + InvoiceState.fromState(Integer.parseInt(stm[1])).getLocaleText(); + // Find the corresponding mandator entry that matches the timestamp. + String mandatorLabel = mandatorTrace.stream().filter(m -> m.contains(timestampString)).findFirst() + .map(m -> { + // Remove the matched mandator entry from the list to avoid reuse. + mandatorTrace.remove(m); + // Extract the label text from the mandator entry if it contains a colon. + if (m.contains(":")) { //$NON-NLS-1$ + String[] parts = m.split(":"); //$NON-NLS-1$ + return parts.length > 1 ? parts[parts.length - 1].trim() : ""; //$NON-NLS-1$ + } + return ""; //$NON-NLS-1$ + }).orElse(""); //$NON-NLS-1$ + // Return the status text with or without the mandator label. + return mandatorLabel.isEmpty() ? statusText : statusText + " / " + mandatorLabel; //$NON-NLS-1$ + }).sorted((entry1, entry2) -> { + try { + // Parse the dates from the entries for sorting. + Date date1 = dateFormat.parse(entry1.split(" : ")[0]); //$NON-NLS-1$ + Date date2 = dateFormat.parse(entry2.split(" : ")[0]); //$NON-NLS-1$ + return date1.compareTo(date2); + } catch (Exception e) { + LoggerFactory.getLogger(getClass()).error("Error parsing dates for sorting: {}", e.getMessage(), e); //$NON-NLS-1$ + return 0; + } + }).collect(Collectors.toList()); + } + } diff --git a/bundles/ch.elexis.core/src/ch/elexis/core/model/InvoiceConstants.java b/bundles/ch.elexis.core/src/ch/elexis/core/model/InvoiceConstants.java index 805ff7aa84..1ff23471d9 100644 --- a/bundles/ch.elexis.core/src/ch/elexis/core/model/InvoiceConstants.java +++ b/bundles/ch.elexis.core/src/ch/elexis/core/model/InvoiceConstants.java @@ -9,4 +9,5 @@ public class InvoiceConstants { public static final String OUTPUT = "Ausgegeben"; public static final String REMARKS = "Bemerkungen"; public static final String INVOICE_CORRECTION = "Rechnungskorrektur"; + public static final String MANDATOR = "Mandator"; }