126 lines
No EOL
4.7 KiB
Java
126 lines
No EOL
4.7 KiB
Java
package com.alexanderlogistics.mahnlauf;
|
|
|
|
import java.util.List;
|
|
import java.util.logging.Logger;
|
|
|
|
import org.imixs.workflow.ItemCollection;
|
|
import org.imixs.workflow.SignalAdapter;
|
|
import org.imixs.workflow.engine.WorkflowService;
|
|
import org.imixs.workflow.exceptions.AccessDeniedException;
|
|
import org.imixs.workflow.exceptions.AdapterException;
|
|
import org.imixs.workflow.exceptions.ModelException;
|
|
import org.imixs.workflow.exceptions.PluginException;
|
|
import org.imixs.workflow.exceptions.ProcessingErrorException;
|
|
import org.imixs.workflow.exceptions.QueryException;
|
|
|
|
import jakarta.inject.Inject;
|
|
|
|
/**
|
|
* Der InkassoAppendAdapter verknüpft eine Rechnung mit einem Inkasso Workflow
|
|
* zum aktuellen Debitor. Existiert aktuell kein Inkasso für diesen
|
|
* Debitor erzeugt der Adapter automatisch eine neue Prozessinstanz.
|
|
* <p>
|
|
* Wurde die Rechnung bereits einem Inkasso zugeordnet passiert nichts.
|
|
*
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author rsoika
|
|
*/
|
|
public class InkassoAppendAdapter implements SignalAdapter {
|
|
|
|
private static Logger logger = Logger.getLogger(InkassoAppendAdapter.class.getName());
|
|
|
|
public static final String ERROR_MISSING_DATA = "MISSING_DATA";
|
|
public static final String ERROR_CONFIG = "CONFIG_ERROR";
|
|
|
|
public static final String ITEM_DBTR_NUMBER = "dbtr.number";
|
|
public static final String ITEM_DBTR_NAME = "dbtr.name";
|
|
|
|
@Inject
|
|
WorkflowService workflowService;
|
|
|
|
/**
|
|
* This method finds or create the Inkasso and adds a reference
|
|
* ($workitemref) to the current invoice.
|
|
*
|
|
* @throws PluginException
|
|
*/
|
|
@Override
|
|
public ItemCollection execute(ItemCollection document, ItemCollection event)
|
|
throws AdapterException, PluginException {
|
|
|
|
ItemCollection inkassoWorkitem = appendInvoice(document);
|
|
|
|
document.setItemValue("debtcollection.ref", inkassoWorkitem.getUniqueID());
|
|
|
|
return document;
|
|
}
|
|
|
|
/**
|
|
* Diese method hängt eine referenz der aktuellen Rechnung an den Inkasso
|
|
* Workflow an. Gibt es noch keinen, wird einer erzeugt.
|
|
* Die method liefert das Inkasso Workitem zurück.
|
|
*
|
|
* @param document
|
|
* @throws PluginException
|
|
*/
|
|
private ItemCollection appendInvoice(ItemCollection document) throws PluginException {
|
|
String dbtrNumber = document.getItemValueString(ITEM_DBTR_NUMBER);
|
|
String currency = document.getItemValueString("invoice.currency");
|
|
|
|
if (dbtrNumber == null || dbtrNumber.isEmpty()) {
|
|
throw new PluginException(PluginException.class.getName(), ERROR_MISSING_DATA,
|
|
"Inkasso kann nicht erzeugt werden. Bitte wählen Sie zuerst einen Debitor aus.");
|
|
}
|
|
|
|
logger.info("......Search Inkasso '" + dbtrNumber + "'...");
|
|
ItemCollection inkasso;
|
|
try {
|
|
inkasso = findInkasso(dbtrNumber, currency);
|
|
if (inkasso == null) {
|
|
// create a new one
|
|
inkasso = new ItemCollection().workflowGroup("Inkasso").task(1000).event(100);
|
|
// add cdtr.name
|
|
inkasso.setItemValue(ITEM_DBTR_NAME, document.getItemValue(ITEM_DBTR_NAME));
|
|
inkasso.setItemValue(ITEM_DBTR_NUMBER, document.getItemValue(ITEM_DBTR_NUMBER));
|
|
inkasso.setItemValue("invoice.currency", currency);
|
|
} else {
|
|
// Inkasso speichern
|
|
inkasso.event(100);
|
|
}
|
|
// Invoice mit Inkasso verknüpften (falls noch nicht verknüpft)
|
|
inkasso.appendItemValueUnique("$workitemref", document.getUniqueID());
|
|
return workflowService.processWorkItem(inkasso);
|
|
|
|
} catch (QueryException | AccessDeniedException | ProcessingErrorException | ModelException e1) {
|
|
throw new PluginException(PluginException.class.getName(), ERROR_MISSING_DATA,
|
|
"Es konnte kein Inkasso zugewiesen werden: " + e1.getMessage());
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prüft alle offenen Inkasso Workflows und gibt den neuesten zur angegebenen
|
|
* dbtrNumber zurück, oder null falls es keinen Offenen gibt.
|
|
*
|
|
* @param dbtrNumber
|
|
* @return
|
|
* @throws QueryException
|
|
*/
|
|
private ItemCollection findInkasso(String dbtrNumber, String currency) throws QueryException {
|
|
String query = "(type:workitem) AND ($modelversion:inkasso*) ";
|
|
List<ItemCollection> resultList = workflowService.getDocumentService().find(query, 999, 0, "$modified", true);
|
|
|
|
for (ItemCollection inkasso : resultList) {
|
|
if (dbtrNumber.equals(inkasso.getItemValueString(ITEM_DBTR_NUMBER))
|
|
&& currency.equals(inkasso.getItemValueString("invoice.currency"))) {
|
|
return inkasso;
|
|
}
|
|
}
|
|
|
|
// no Inkasso found
|
|
return null;
|
|
}
|
|
|
|
} |