improve invoice plugin logic

This commit is contained in:
Ralph Soika 2025-12-01 12:00:58 +01:00
parent fecb9e8813
commit 78739d3517
3 changed files with 50 additions and 36 deletions

View file

@ -123,26 +123,34 @@ public class BusinessPartnerService {
* Diese Methode aktualisiert das business Partner object. Sie wird von den
* Invoice Plugins genutzt. Die Methode wirft eine PluginException falls das
* BusinessPartner Objekt gesperrt oder in verification ist!
*
* <p>
* Gleichzeitig wird bei Rechnungen in Abhängigkeit der Rechnungsart die
* dbtr./cdtr. items sowie mail und country aktualisiert.
* <p>
* Die Methode wirft eine Plugin Exception falls der Business Partner im Status
* Verification ist und updateWhenVerification=false.
* <p>
* Die Methode wirft eine Plugin Exception falls der Business Partner im Status
* Locked ist und updateWhenLocked=false.
*
* @param partnerID
* @param workitem
* @param invoice
* @return BusinessPartner Object
* @throws PluginException
*/
public ItemCollection updateBusinessPartnerData(ItemCollection workitem) throws PluginException {
String partnerID = workitem.getItemValueString("partner.id").trim();
// Migration wenn keine partner.id existier!
public ItemCollection updateBusinessPartnerData(ItemCollection invoice, boolean updateWhenVerification,
boolean updateWhenLocked)
throws PluginException {
String partnerID = invoice.getItemValueString("partner.id").trim();
// Migration wenn keine partner.id existiert!
if (partnerID.isEmpty()) {
if (InvoiceUtil.isCreditorInvoice(workitem)) {
partnerID = InvoiceUtil.buildBPID(workitem.getItemValueString("cdtr.number"));
if (InvoiceUtil.isCreditorInvoice(invoice)) {
partnerID = InvoiceUtil.buildBPID(invoice.getItemValueString("cdtr.number"));
}
if (InvoiceUtil.isDebitorInvoice(workitem)) {
partnerID = InvoiceUtil.buildBPID(workitem.getItemValueString("dbtr.number"));
if (InvoiceUtil.isDebitorInvoice(invoice)) {
partnerID = InvoiceUtil.buildBPID(invoice.getItemValueString("dbtr.number"));
}
workitem.setItemValue("partner.id", partnerID);
invoice.setItemValue("partner.id", partnerID);
}
if (partnerID == null || partnerID.isEmpty()) {
@ -164,40 +172,40 @@ public class BusinessPartnerService {
Integer.parseInt(partnerID);
// it is an int - get last 4 digits
partnerID = "BP" + partnerID.substring(Math.max(0, partnerID.length() - 4));
workitem.setItemValue("partner.id", partnerID);
invoice.setItemValue("partner.id", partnerID);
businessPartner = getBusinessPartnerByID(partnerID);
} catch (NumberFormatException e) {
/* not an int */
}
if (businessPartner == null) {
workitem.removeItem("partner.id");
invoice.removeItem("partner.id");
logger.warning("Business Partner for Partner ID " + partnerID + " not found!");
return null;
}
}
// because the method can be called multiple times within one worklfow
// processing cycle
// we need to load the object within the current transaction context
// because the method can be called multiple times within one workflow
// processing cycle we need to load the object within the current transaction
// context
businessPartner = documentService.load(businessPartner.getUniqueID());
if (businessPartner != null) {
// update partner id data
workitem.setItemValue("partner.id", partnerID);
workitem.setItemValue("partner.name", businessPartner.getItemValueString("partner.name"));
invoice.setItemValue("partner.id", partnerID);
invoice.setItemValue("partner.name", businessPartner.getItemValueString("partner.name"));
logger.info("check invoice type....");
// Update Invoice Partner Meta Data
if (InvoiceUtil.isDebitorInvoice(workitem)) {
if (InvoiceUtil.isDebitorInvoice(invoice)) {
logger.info("...is debitor invoice");
workitem.setItemValue("dbtr.number", businessPartner.getItemValueString("dbtr.number"));
workitem.setItemValue("dbtr.name", businessPartner.getItemValueString("partner.name"));
workitem.setItemValue("dbtr.mail", businessPartner.getItemValueString("dbtr.mail"));
workitem.setItemValue("invoice.country", businessPartner.getItemValueString("_vendor_country"));
invoice.setItemValue("dbtr.number", businessPartner.getItemValueString("dbtr.number"));
invoice.setItemValue("dbtr.name", businessPartner.getItemValueString("partner.name"));
invoice.setItemValue("dbtr.mail", businessPartner.getItemValueString("dbtr.mail"));
invoice.setItemValue("invoice.country", businessPartner.getItemValueString("_vendor_country"));
}
logger.info("...is creditor invoice");
if (InvoiceUtil.isCreditorInvoice(workitem)) {
workitem.setItemValue("cdtr.name", businessPartner.getItemValueString("partner.name"));
workitem.setItemValue("cdtr.mail", businessPartner.getItemValueString("cdtr.mail"));
workitem.setItemValue("invoice.country", businessPartner.getItemValueString("_vendor_country"));
if (InvoiceUtil.isCreditorInvoice(invoice)) {
invoice.setItemValue("cdtr.name", businessPartner.getItemValueString("partner.name"));
invoice.setItemValue("cdtr.mail", businessPartner.getItemValueString("cdtr.mail"));
invoice.setItemValue("invoice.country", businessPartner.getItemValueString("_vendor_country"));
/*
* Sonderfall - Frau Mahner 9.10.2025
@ -205,25 +213,25 @@ public class BusinessPartnerService {
* ein alternatives Konto gewählt hat muss dieses gewinnen!
* Ansonsten nehmen wir die cdtr.number
*/
if ("credit".equals(workitem.getItemValueString("payment.type"))) {
String bookingAccount = workitem.getItemValueString("payment.booking.account");
if ("credit".equals(invoice.getItemValueString("payment.type"))) {
String bookingAccount = invoice.getItemValueString("payment.booking.account");
if (!bookingAccount.isBlank()) {
workitem.setItemValue("cdtr.number", bookingAccount);
invoice.setItemValue("cdtr.number", bookingAccount);
} else {
workitem.setItemValue("cdtr.number", businessPartner.getItemValueString("cdtr.number"));
invoice.setItemValue("cdtr.number", businessPartner.getItemValueString("cdtr.number"));
}
} else {
// default - cdtr.number
workitem.setItemValue("cdtr.number", businessPartner.getItemValueString("cdtr.number"));
invoice.setItemValue("cdtr.number", businessPartner.getItemValueString("cdtr.number"));
}
}
// Validate Business Partner Status!
if (businessPartner.getTaskID() == TASK_VERIFICATION) {
if (businessPartner.getTaskID() == TASK_VERIFICATION && !updateWhenVerification) {
throw new PluginException(InvoicePlugin.class.getName(), ERROR_BUSINESSPARTNER_VERIFICATION,
"Businesspartner is in verification. Invoice can't be processed!");
}
if (businessPartner.getTaskID() == TASK_LOCKED) {
if (businessPartner.getTaskID() == TASK_LOCKED && updateWhenLocked) {
throw new PluginException(InvoicePlugin.class.getName(), ERROR_BUSINESSPARTNER_LOCKED,
"Businesspartner is locked. Invoice can't be processed!");
}

View file

@ -11,6 +11,8 @@ import jakarta.inject.Inject;
*
* Auch das feld _img wird in Abhängigkeit vom item 'invoice.protest' gesetzt
*
* Das Plugin ignoriert den Status des Business Partners.
*
* @author rsoika
* @version 1.0
*
@ -29,7 +31,7 @@ public class InvoiceOutgoingPlugin extends AbstractPlugin {
public ItemCollection run(ItemCollection workitem, ItemCollection event) throws PluginException {
// Update BUsiness Partner Data
businessPartnerService.updateBusinessPartnerData(workitem);
businessPartnerService.updateBusinessPartnerData(workitem, true, true);
// Update Invoice.positions
InvoiceUtil.updateInvoicePositions(workitem);

View file

@ -39,6 +39,10 @@ import jakarta.inject.Inject;
* 26.4.25 - Das Plugin setzt nun auch partner.name und partner.id zusätzlich
* processed es das BP Workitem
*
* <p>
* Das Plugin prüft den Status des Business Partners und wirft eine Exception
* wenn dieser in Verification oder Locked ist.
*
* @author rsoika
* @version 1.0
*
@ -83,7 +87,7 @@ public class InvoicePlugin extends AbstractPlugin {
public ItemCollection run(ItemCollection workitem, ItemCollection event) throws PluginException {
// Update BUsiness Partner Data
ItemCollection businessPartner = businessPartnerService.updateBusinessPartnerData(workitem);
ItemCollection businessPartner = businessPartnerService.updateBusinessPartnerData(workitem, false, false);
updateImg(workitem);