Skip to content

Commit

Permalink
[26602] Fehler der doppelten Übernahme korrigiert (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksic28 authored Nov 12, 2024
1 parent 5b1a38d commit 2d61b50
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import ch.elexis.core.services.holder.ContextServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.services.holder.StoreToStringServiceHolder;
import ch.elexis.core.ui.processor.BillingProcessor;

@Component
public class PrescriptionBilledAdjuster implements IBilledAdjuster {

private BillingProcessor billingProcessor;
private ExecutorService executor = Executors.newSingleThreadExecutor();

@Override
Expand All @@ -39,6 +40,7 @@ public void adjust(IBilled billed) {
@Override
public void run() {
IBillable billable = billed.getBillable();
billingProcessor = new BillingProcessor(billed.getEncounter());
if (billable instanceof IArticle) {
IArticle article = (IArticle) billable;
Optional<IPatient> patientOpt = getPatient(billed);
Expand All @@ -48,6 +50,7 @@ public void run() {
ContextServiceHolder.get().postEvent(ElexisEventTopics.EVENT_RELOAD, IPrescription.class);
}
}
billingProcessor.updatePrescriptionsWithDosage(billed);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,50 @@ private void handleArticleBillingDialog(IArticle selectedArticle) {
PrescriptionSignatureTitleAreaDialog dialog = new PrescriptionSignatureTitleAreaDialog(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), selectedArticle);
dialog.lookup();
dialog.setFromBillingDialog(true);
if (dialog.open() == Dialog.OK) {
IArticleDefaultSignature signature = dialog.getSignature();
List<IPrescription> prescriptions = getRecentPatientPrescriptions(actEncounter.getPatient(),
actEncounter.getDate().atStartOfDay());
boolean medicationExists = checkIfMedicationExists(prescriptions, selectedArticle, signature);
Result<IBilled> billResult = BillingServiceHolder.get().bill(selectedArticle, actEncounter, 1.0);
if (billResult.isOK()) {
IBilled billed = billResult.get();
updatePrescriptionWithBilledId(billed, signature);
CoreModelServiceHolder.get().save(actEncounter);
if (!medicationExists) {
dialog.setFromBillingDialog(true);
if (dialog.open() != Dialog.OK) {
return;
}
IArticleDefaultSignature signature = dialog.getSignature();
List<IPrescription> prescriptions = getRecentPatientPrescriptions(actEncounter.getPatient(),
actEncounter.getDate().atStartOfDay());
boolean medicationExists = checkIfMedicationExists(prescriptions, selectedArticle, signature);
if (medicationExists) {
return;
}
EntryType disposalType = signature.getDisposalType();
IPrescription prescription = null;
if (disposalType != EntryType.SELF_DISPENSED) {
Result<IBilled> billResult = BillingServiceHolder.get().bill(selectedArticle, actEncounter, 1.0);
if (billResult.isOK()) {
IBilled billed = billResult.get();
CreatePrescriptionHelper prescriptionHelper = new CreatePrescriptionHelper(selectedArticle,
Display.getDefault().getActiveShell());
prescription = prescriptionHelper.createPrescriptionFromSignature(signature);
if (prescription != null) {
prescription.setExtInfo(ch.elexis.core.model.prescription.Constants.FLD_EXT_VERRECHNET_ID,
billed.getId().toString());
prescription.setDosageInstruction(signature.getSignatureAsDosisString());
prescription.setRemark(signature.getComment());
CoreModelServiceHolder.get().save(prescription);
updatePrescriptionWithBilledId(billed, signature);
CreatePrescriptionHelper prescriptionHelper = new CreatePrescriptionHelper(selectedArticle,
Display.getDefault().getActiveShell());
IPrescription prescription = prescriptionHelper.createPrescriptionFromSignature(signature);
if (prescription != null && billed != null) {
prescription.setExtInfo(ch.elexis.core.model.prescription.Constants.FLD_EXT_VERRECHNET_ID,
billed.getId().toString());
CoreModelServiceHolder.get().save(prescription);
postRefreshMedicationEvent();
}
}
} else {
ResultDialog.show(billResult);
} else {
ResultDialog.show(billResult);
return;
}
} else {
CreatePrescriptionHelper prescriptionHelper = new CreatePrescriptionHelper(selectedArticle,
Display.getDefault().getActiveShell());
prescription = prescriptionHelper.createPrescriptionFromSignature(signature);
if (prescription != null) {
prescription.setDosageInstruction(signature.getSignatureAsDosisString());
prescription.setRemark(signature.getComment());
CoreModelServiceHolder.get().save(prescription);
}
}
CoreModelServiceHolder.get().save(actEncounter);
postRefreshMedicationEvent();
}

private boolean checkIfMedicationExists(List<IPrescription> prescriptions, IArticle selectedArticle,
Expand Down Expand Up @@ -168,7 +185,7 @@ public void updatePrescriptionsWithDosage(IBilled billed) {
|| !dosageInstruction.equals(lastPrescription.getDosageInstruction());
})
.forEach(prescription -> {
prescription.setDosageInstruction(lastPrescription.getDosageInstruction());
prescription.setDosageInstruction(lastPrescription.getDosageInstruction());
prescription.setRemark(lastPrescription.getRemark());
prescription.setDateFrom(actEncounter.getDate().atStartOfDay());
CoreModelServiceHolder.get().save(prescription);
Expand All @@ -183,7 +200,7 @@ private boolean isMatchingPrescription(IPrescription prescription, IBilled bille
}

public static List<IPrescription> getRecentPatientPrescriptions(IPatient patient, LocalDateTime referenceDate) {
return getMedicationAll(patient, Arrays.asList(EntryType.FIXED_MEDICATION, EntryType.RESERVE_MEDICATION,
return getMedicationRecent(patient, Arrays.asList(EntryType.FIXED_MEDICATION, EntryType.RESERVE_MEDICATION,
EntryType.SYMPTOMATIC_MEDICATION, EntryType.SELF_DISPENSED), referenceDate);
}

Expand Down Expand Up @@ -258,7 +275,7 @@ private void postRefreshMedicationEvent() {
ContextServiceHolder.get().postEvent(ElexisEventTopics.EVENT_RELOAD, IPrescription.class);
}

public static List<IPrescription> getMedicationAll(IPatient patient, List<EntryType> filterType,
public static List<IPrescription> getMedicationRecent(IPatient patient, List<EntryType> filterType,
LocalDateTime referenceDate) {
LocalDateTime startDate = referenceDate.minus(3, ChronoUnit.MONTHS);
IQuery<IPrescription> query = CoreModelServiceHolder.get().getQuery(IPrescription.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,21 @@ public void selfDispense(IPrescription prescription) {
dispensationArticle = item.get();
}
}
Result<IBilled> result = BillingServiceHolder.get().bill(dispensationArticle, encounter.get(), 1);
if (result.isOK()) {
IBilled billed = result.get();
billingProcessor.updatePrescriptionsWithDosage(billed);
ContextServiceHolder.get().postEvent(ElexisEventTopics.EVENT_UPDATE, encounter.get());
// work is done

if (!isArticleAlreadyBilled(dispensationArticle, encounter.get())) {
Result<IBilled> result = BillingServiceHolder.get().bill(dispensationArticle, encounter.get(), 1);
if (result.isOK()) {
IBilled billed = result.get();
prescription.setExtInfo(ch.elexis.core.model.prescription.Constants.FLD_EXT_VERRECHNET_ID,
billed.getId().toString());
CoreModelServiceHolder.get().save(prescription);
billingProcessor.updatePrescriptionsWithDosage(billed);
ContextServiceHolder.get().postEvent(ElexisEventTopics.EVENT_UPDATE, encounter.get());
return;
} else {
MessageDialog.openError(parentShell, "Fehler bei der Verrechnung", result.toString());
}
} else {
return;
}
}
Expand All @@ -156,6 +165,11 @@ public void selfDispense(IPrescription prescription) {
Messages.CreatePrescriptionHelper_WarninigNoConsText);
}

private boolean isArticleAlreadyBilled(IArticle article, IEncounter encounter) {
return encounter.getBilled().stream().anyMatch(billed -> billed.getBillable() instanceof IArticle
&& ((IArticle) billed.getBillable()).getId().equals(article.getId()));
}

private boolean shouldUpdateToArtikelstamm() {
return ConfigServiceHolder.getUser(MEDICATION_SETTINGS_DISPENSE_ARTIKELSTAMM_CONVERT, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.inject.Inject;

Expand Down Expand Up @@ -1155,11 +1156,20 @@ private void changeQuantityDialog(String p, IBilled billed) {
} else {
changeAnzahl = Integer.parseInt(dlg.getValue());
}

double oldAnzahl = billed.getAmount();
double difference = changeAnzahl - oldAnzahl;
IStatus status = BillingServiceHolder.get().changeAmountValidated(billed, changeAnzahl);
if (!status.isOK()) {
StatusDialog.show(status);
}
if (difference < 0) {
int itemsToRemove = (int) (-difference);
List<IPrescription> prescriptions = BillingProcessor.getMedicationRecent(
actEncounter.getPatient(), Collections.singletonList(EntryType.SELF_DISPENSED),
actEncounter.getDate().atStartOfDay());
handleLinkedPrescriptionRemover(billed, prescriptions, itemsToRemove);
}

// refresh with changed amount
CoreModelServiceHolder.get().refresh(billed);
billed.setText(text);
Expand All @@ -1170,6 +1180,7 @@ private void changeQuantityDialog(String p, IBilled billed) {
SWTHelper.showError(Messages.VerrechnungsDisplay_invalidEntryCaption, // $NON-NLS-1$
Messages.VerrechnungsDisplay_invalidEntryBody); // $NON-NLS-1$
}
ContextServiceHolder.get().postEvent(ElexisEventTopics.EVENT_RELOAD, IPrescription.class);
}
}

Expand Down Expand Up @@ -1238,13 +1249,36 @@ private void handleLinkedMedication(IBilled billed, List<IPrescription> prescrip
}).forEach(action);
}

/**
* Removes up to a specified number of prescriptions linked to a billed item.
*
* @param billed The billed item whose linked prescriptions are to be
* removed.
* @param prescriptions The list of prescriptions to search.
* @param itemsToRemove The maximum number of prescriptions to remove.
*/
private void handleLinkedPrescriptionRemover(IBilled billed, List<IPrescription> prescriptions, int itemsToRemove) {
prescriptions.stream()
// Filter prescriptions linked to the billed item
.filter(prescription -> {
Object extInfoObj = prescription
.getExtInfo(ch.elexis.core.model.prescription.Constants.FLD_EXT_VERRECHNET_ID);
// Check if extInfo exists and IDs match
return extInfoObj != null && billed.getId().equals(extInfoObj.toString());
})
// Limit to the specified number of prescriptions to remove
.limit(itemsToRemove)
// Delete each filtered prescription
.forEach(prescription -> {
CoreModelServiceHolder.get().delete(prescription);
});
}

private boolean isMedicationLinked(IBilled billed, List<IPrescription> prescriptions) {
return prescriptions.stream().anyMatch(prescription -> {
Object extInfoObj = prescription
.getExtInfo(ch.elexis.core.model.prescription.Constants.FLD_EXT_VERRECHNET_ID);
return extInfoObj != null && billed.getId().equals(extInfoObj.toString());
});
}


}

0 comments on commit 2d61b50

Please sign in to comment.