Commit bc9c83f4 authored by Thomas Huster's avatar Thomas Huster

[20705] use InvoiceService instead of Rechnung build to create invoice

parent 4ca6dada
Pipeline #16149 passed with stages
in 14 minutes and 47 seconds
......@@ -32,10 +32,12 @@ import ch.elexis.core.exceptions.ElexisException;
import ch.elexis.core.model.IBilled;
import ch.elexis.core.model.ICoverage;
import ch.elexis.core.model.IEncounter;
import ch.elexis.core.model.IInvoice;
import ch.elexis.core.services.holder.BillingServiceHolder;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.services.holder.EncounterServiceHolder;
import ch.elexis.core.services.holder.InvoiceServiceHolder;
import ch.elexis.data.Fall;
import ch.elexis.data.Konsultation;
import ch.elexis.data.Mandant;
......@@ -383,14 +385,16 @@ public class BillingUtil {
* @param toBillMap
* @return
*/
public static List<Result<Rechnung>> createBills(
public static List<Result<IInvoice>> createBills(
Map<Rechnungssteller, Map<Fall, List<Konsultation>>> toBillMap){
List<Result<Rechnung>> ret = new ArrayList<>();
List<Result<IInvoice>> ret = new ArrayList<>();
Set<Rechnungssteller> invoicers = toBillMap.keySet();
for (Rechnungssteller invoicer : invoicers) {
Set<Fall> faelle = toBillMap.get(invoicer).keySet();
for (Fall fall : faelle) {
ret.add(Rechnung.build(toBillMap.get(invoicer).get(fall)));
List<IEncounter> encounters = NoPoUtil
.loadAsIdentifiable(toBillMap.get(invoicer).get(fall), IEncounter.class);
ret.add(InvoiceServiceHolder.get().invoice(encounters));
}
}
return ret;
......@@ -934,7 +938,8 @@ public class BillingUtil {
historyEntryDTO.setIgnored(true);
} else {
Result<Rechnung> rechnungResult = Rechnung.build(releasedKonsultations);
Result<IInvoice> rechnungResult = InvoiceServiceHolder.get()
.invoice(NoPoUtil.loadAsIdentifiable(releasedKonsultations, IEncounter.class));
if (!rechnungResult.isOK()) {
for (@SuppressWarnings("rawtypes")
......@@ -951,7 +956,7 @@ public class BillingUtil {
+ (rechnungResult.get() != null ? rechnungResult.get().getId() : "null"));
log.error("invoice correction: error details: " + output.toString());
} else {
Rechnung newRechnung = rechnungResult.get();
Rechnung newRechnung = Rechnung.load(rechnungResult.get().getId());
invoiceCorrectionDTO.setNewInvoiceNumber(newRechnung.getNr());
log.debug(
"invoice correction: create new invoice with number [{}] old invoice number [{}] ",
......
......@@ -99,7 +99,7 @@ public class NoPoUtil {
* @param type
* @return
*/
public static <T> List<T> loadAsIdentifiable(List<PersistentObject> persistentObjects,
public static <T> List<T> loadAsIdentifiable(List<? extends PersistentObject> persistentObjects,
Class<T> type){
if (persistentObjects != null && !persistentObjects.isEmpty()) {
List<T> ret = new ArrayList<>();
......
......@@ -33,6 +33,7 @@ import ch.elexis.core.data.interfaces.IDiagnose;
import ch.elexis.core.data.interfaces.events.MessageEvent;
import ch.elexis.core.data.service.LocalLockServiceHolder;
import ch.elexis.core.model.InvoiceState;
import ch.elexis.core.services.IInvoiceService;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.rgw.io.Settings;
import ch.rgw.tools.JdbcLink;
......@@ -101,6 +102,8 @@ public class Rechnung extends PersistentObject {
* einen Rechnungssteller hat, werden alle Konsultationen ungeachtet des Mandanten verrechnet
*
* @return Ein Result mit ggf. der erstellten Rechnung als Inhalt
*
* @deprecated use {@link IInvoiceService} to create invoices instead
*/
public static Result<Rechnung> build(final List<Konsultation> behandlungen){
System.out.println("js Rechnung: build() begin");
......@@ -389,6 +392,7 @@ public class Rechnung extends PersistentObject {
* freigegeben, andernfalls bleiben sie abgeschlossen.
* @return if reopen is true the released konsultations from the bill will be returned
* @since 3.3
* @deprecated {@link IInvoiceService} to cancel invoices instead
*/
public List<Konsultation> stornoBill(final boolean reopen){
InvoiceState invoiceState = InvoiceState.fromState(getStatus());
......
......@@ -3,6 +3,7 @@ package ch.elexis.core.services;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
......@@ -27,6 +28,7 @@ import ch.elexis.core.model.Identifiable;
import ch.elexis.core.model.InvoiceState;
import ch.elexis.core.model.builder.IAccountTransactionBuilder;
import ch.elexis.core.model.builder.IInvoiceBilledBuilder;
import ch.elexis.core.model.builder.IPaymentBuilder;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.ContextServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
......@@ -257,8 +259,36 @@ public class InvoiceService implements IInvoiceService {
@Override
public List<IEncounter> cancel(IInvoice invoice, boolean reopen){
// TODO Auto-generated method stub
return null;
InvoiceState invoiceState = invoice.getState();
List<IEncounter> ret = Collections.emptyList();
if (!InvoiceState.CANCELLED.equals(invoiceState)
&& !InvoiceState.DEPRECIATED.equals(invoiceState)) {
Money amount = invoice.getTotalAmount();
new IPaymentBuilder(CoreModelServiceHolder.get(), invoice, amount, "Storno")
.buildAndSave();
if (reopen) {
ret = removeEncounters(invoice);
invoice.setState(InvoiceState.CANCELLED);
CoreModelServiceHolder.get().save(invoice);
} else {
invoice.setState(InvoiceState.DEPRECIATED);
CoreModelServiceHolder.get().save(invoice);
}
} else if (reopen && InvoiceState.CANCELLED.equals(invoiceState)) {
// if bill is canceled ensure that all kons are opened
ret = removeEncounters(invoice);
}
return ret;
}
private List<IEncounter> removeEncounters(IInvoice invoice){
List<IEncounter> encounters = invoice.getEncounters();
for (IEncounter iEncounter : encounters) {
iEncounter.setInvoice(null);
}
CoreModelServiceHolder.get().save(encounters);
return encounters;
}
@Override
......
......@@ -12,16 +12,17 @@ import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.widgets.Display;
import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.service.LocalLockServiceHolder;
import ch.elexis.core.data.util.NoPoUtil;
import ch.elexis.core.lock.types.LockResponse;
import ch.elexis.core.model.IEncounter;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.InvoiceServiceHolder;
import ch.elexis.core.ui.dbcheck.contributions.dialogs.SelectBillingStrategyDialog;
import ch.elexis.core.ui.dbcheck.external.ExternalMaintenance;
import ch.elexis.data.Fall;
import ch.elexis.data.Konsultation;
import ch.elexis.data.Query;
import ch.elexis.data.Rechnung;
import ch.elexis.data.Rechnungssteller;
import ch.rgw.tools.TimeTool;
......@@ -41,7 +42,8 @@ public class BillAllOpenCons extends ExternalMaintenance {
TimeTool now = new TimeTool();
for (Fall fall : qre) {
if (fall.isOpen()) {
Rechnung.build(Arrays.asList(fall.getBehandlungen(false)));
InvoiceServiceHolder.get().invoice(NoPoUtil.loadAsIdentifiable(
Arrays.asList(fall.getBehandlungen(false)), IEncounter.class));
fall.setEndDatum(now.toString(TimeTool.DATE_GER));
count++;
}
......@@ -124,7 +126,8 @@ public class BillAllOpenCons extends ExternalMaintenance {
getConsultationByRechnungssteller(fall);
Set<Rechnungssteller> keys = consByRechnungssteller.keySet();
for (Rechnungssteller key : keys) {
Rechnung.build(consByRechnungssteller.get(key));
InvoiceServiceHolder.get().invoice(NoPoUtil.loadAsIdentifiable(
consByRechnungssteller.get(key), IEncounter.class));
}
fall.setEndDatum(new TimeTool().toString(TimeTool.DATE_GER));
LocalLockServiceHolder.get().releaseLock((fall));
......
......@@ -14,6 +14,7 @@ import org.eclipse.ui.handlers.HandlerUtil;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.data.util.BillingUtil;
import ch.elexis.core.model.IInvoice;
import ch.elexis.core.ui.UiDesk;
import ch.elexis.core.ui.views.rechnung.RnOutputDialog;
import ch.elexis.data.Fall;
......@@ -33,10 +34,10 @@ public class BillActiveEncounterHandler extends AbstractHandler implements IHand
if (selectedEncounter != null) {
Result<Konsultation> result = BillingUtil.getBillableResult(selectedEncounter);
if (result.isOK()) {
List<Result<Rechnung>> results =
List<Result<IInvoice>> results =
BillingUtil.createBills(getBillMap(selectedEncounter));
if (!results.isEmpty() && results.get(0).isOK()) {
Rechnung invoice = results.get(0).get();
Rechnung invoice = Rechnung.load(results.get(0).get().getId());
new RnOutputDialog(UiDesk.getTopShell(), Collections.singletonList(invoice))
.open();
}
......
......@@ -37,10 +37,10 @@ import org.eclipse.ui.handlers.HandlerUtil;
import org.slf4j.LoggerFactory;
import ch.elexis.core.data.util.BillingUtil;
import ch.elexis.core.model.IInvoice;
import ch.elexis.core.ui.views.rechnung.BillingProposalView;
import ch.elexis.data.Fall;
import ch.elexis.data.Konsultation;
import ch.elexis.data.Rechnung;
import ch.elexis.data.Rechnungssteller;
import ch.rgw.tools.Result;
......@@ -88,14 +88,14 @@ public class BillingProposalViewCreateBillsHandler extends AbstractHandler imple
BillingUtil.getGroupedBillable(billable);
monitor.worked(1);
// create all bills
List<Result<Rechnung>> results = BillingUtil.createBills(toBillMap);
List<Result<IInvoice>> results = BillingUtil.createBills(toBillMap);
// build information to show
for (Result<Rechnung> result : results) {
for (Result<IInvoice> result : results) {
if (result.isOK()) {
successful++;
} else {
errorneousInfo.append(result.getSeverity()).append(" -> ");
List<Result<Rechnung>.msg> messages = result.getMessages();
List<Result<IInvoice>.msg> messages = result.getMessages();
for (int i = 0; i < messages.size(); i++) {
if (i > 0) {
errorneousInfo.append(" / ");
......
......@@ -36,13 +36,16 @@ import org.eclipse.ui.handlers.IHandlerService;
import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.util.BillingUtil;
import ch.elexis.core.data.util.NoPoUtil;
import ch.elexis.core.data.util.ResultAdapter;
import ch.elexis.core.model.IEncounter;
import ch.elexis.core.model.IInvoice;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.InvoiceServiceHolder;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.core.ui.views.rechnung.Messages;
import ch.elexis.data.Fall;
import ch.elexis.data.Konsultation;
import ch.elexis.data.Rechnung;
import ch.rgw.tools.Result;
import ch.rgw.tools.Tree;
......@@ -67,7 +70,7 @@ public class ErstelleRnnCommand extends AbstractHandler {
throw new ExecutionException("Bad parameter " + pe.getMessage()); //$NON-NLS-1$
}
IProgressMonitor monitor = Handler.getMonitor(eev);
Result<Rechnung> res = null;
Result<IInvoice> res = null;
for (Tree tPat = tSelection.getFirstChild(); tPat != null; tPat = tPat.getNextSibling()) {
int rejected = 0;
for (Tree tFall = tPat.getFirstChild(); tFall != null; tFall = tFall.getNextSibling()) {
......@@ -85,8 +88,9 @@ public class ErstelleRnnCommand extends AbstractHandler {
toBill.add((Konsultation) t.contents);
}
Map<Integer, List<Konsultation>> sortedByYears =
BillingUtil.getSortedByYear(toBill);
Map<Integer, List<IEncounter>> sortedByYears =
BillingUtil.getSortedEncountersByYear(
NoPoUtil.loadAsIdentifiable(toBill, IEncounter.class));
if (!BillingUtil.canBillYears(new ArrayList<>(sortedByYears.keySet()))) {
StringJoiner sj = new StringJoiner(", ");
sortedByYears.keySet().forEach(i -> sj.add(Integer.toString(i)));
......@@ -96,7 +100,7 @@ public class ErstelleRnnCommand extends AbstractHandler {
+ sj.toString() + " erstellen?")) {
// bill each year separately
for (Integer year : sortedByYears.keySet()) {
res = Rechnung.build(sortedByYears.get(year));
res = InvoiceServiceHolder.get().invoice(sortedByYears.get(year));
if (monitor != null) {
monitor.worked(1);
}
......@@ -114,7 +118,8 @@ public class ErstelleRnnCommand extends AbstractHandler {
}
}
} else {
res = Rechnung.build(toBill);
res = InvoiceServiceHolder.get()
.invoice(NoPoUtil.loadAsIdentifiable(toBill, IEncounter.class));
if (monitor != null) {
monitor.worked(1);
}
......
package ch.elexis.core.model.builder;
import java.time.LocalDate;
import ch.elexis.core.model.IInvoice;
import ch.elexis.core.model.IPayment;
import ch.elexis.core.services.IModelService;
import ch.rgw.tools.Money;
public class IPaymentBuilder extends AbstractBuilder<IPayment> {
public IPaymentBuilder(IModelService modelService, IInvoice invoice, Money amount,
String remark){
super(modelService);
object = modelService.create(IPayment.class);
object.setInvoice(invoice);
object.setAmount(amount);
object.setDate(LocalDate.now());
object.setRemark(remark);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment