neues JournalAccounting

This commit is contained in:
Ralph Soika 2025-03-26 11:20:43 +01:00
parent 56a60a990d
commit f52e76374e
2 changed files with 94 additions and 13 deletions

View file

@ -27,6 +27,18 @@ public class JournalAccounting {
@Column(precision = 19, scale = 4, nullable = false)
private BigDecimal amount;
// base amount (optional)
@Column(precision = 19, scale = 4)
private BigDecimal baseAmount;
// ISO currency code (e.g. "USD", "GBP")
@Column(length = 3)
private String currency;
// exchange rate (foreign currency -> base currency)
@Column(precision = 19, scale = 6)
private BigDecimal exchangeRate;
@Column()
private String reference;
@ -94,6 +106,30 @@ public class JournalAccounting {
this.amount = amount;
}
public BigDecimal getBaseAmount() {
return baseAmount;
}
public void setBaseAmount(BigDecimal baseAmount) {
this.baseAmount = baseAmount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public BigDecimal getExchangeRate() {
return exchangeRate;
}
public void setExchangeRate(BigDecimal exchangeRate) {
this.exchangeRate = exchangeRate;
}
public String getReference() {
return reference;
}

View file

@ -28,9 +28,11 @@
package org.imixs.workflow.office.journal;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.faces.data.WorkflowEvent;
@ -41,6 +43,7 @@ import jakarta.annotation.security.RolesAllowed;
import jakarta.annotation.security.RunAs;
import jakarta.ejb.Stateless;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
@ -65,6 +68,9 @@ public class JournalAccountingService {
@PersistenceContext(unitName = "org.imixs.workflow.jpa")
private EntityManager entityManager;
@Inject
DocumentService documentService;
/**
* WorkflowEvent listener to store an accounting entry
*
@ -116,16 +122,37 @@ public class JournalAccountingService {
}
// Update/set values
accounting.setAmount(new BigDecimal(workitem.getItemValueDouble("invoice.total")));
accounting.setDate(workitem.getItemValueDate("invoice.date"));
BigDecimal amount = BigDecimal.valueOf(workitem.getItemValueDouble("invoice.total"))
.setScale(2, RoundingMode.HALF_UP);
accounting.setAmount(amount);
accounting.setCurrency(workitem.getItemValueString("invoice.currency"));
// foreign currency
if (workitem.getItemValueDouble("invoice.rate") != 0.0) {
// YES
BigDecimal exchangeRate = BigDecimal.valueOf(workitem.getItemValueDouble("invoice.rate"))
.setScale(6, RoundingMode.HALF_UP);
// Hauswährung = Fremdwährung * Wechselkurs (auf 2 Stellen gerundet)
BigDecimal baseAmount = amount.divide(exchangeRate, 6, RoundingMode.HALF_UP).setScale(2,
RoundingMode.HALF_UP);
accounting.setExchangeRate(exchangeRate);
accounting.setBaseAmount(baseAmount);
} else {
// NO
accounting.setExchangeRate(null);
accounting.setBaseAmount(null);
}
// Update attributes
// Clear and add attributes (or use selective update if needed)
accounting.getAttributes().clear();
accounting.addAttribute("dbtr.name", workitem.getItemValueString("dbtr.name"));
accounting.addAttribute("dbtr.number", workitem.getItemValueString("dbtr.number"));
accounting.addAttribute("space.name", workitem.getItemValueString("space.name"));
// Add more attributes as needed
accounting.addAttribute("country", workitem.getItemValueString("invoice.country"));
// Persist or merge
if (accounting.getId() == null) {
@ -142,11 +169,12 @@ public class JournalAccountingService {
List<ItemCollection> paymentDetails = InvoiceUtil.explodeChildList(workitem, "payment.details");
for (ItemCollection payment : paymentDetails) {
String workitemRef = payment.getItemValueString("$uniqueid");
Double amount = payment.getItemValueDouble("payment.amount");
String invoiceRef = payment.getItemValueString("$uniqueid");
BigDecimal amount = BigDecimal.valueOf(payment.getItemValueDouble("payment.amount"))
.setScale(2, RoundingMode.HALF_UP);
// Find existing payment entry or create new one
String paymentBusinessRef = workitem.getUniqueID() + "-" + workitemRef;
String paymentBusinessRef = workitem.getUniqueID() + "-" + invoiceRef;
JournalAccounting accounting = findAccountingEntryByBusinessRef(paymentBusinessRef,
workitem.getWorkflowGroup());
@ -157,22 +185,39 @@ public class JournalAccountingService {
}
// Update payment values
accounting.setAmount(new BigDecimal(amount));
accounting.setDate(workitem.getItemValueDate("payment.date"));
// Find the related invoice entry
JournalAccounting invoiceEntry = findAccountingEntryByBusinessRef(invoiceRef, "Rechnungsausgang");
if (invoiceEntry != null) {
accounting.addAttribute("Invoice-Ref", invoiceEntry.getReference());
}
ItemCollection invoice = documentService.load(invoiceRef);
accounting.setCurrency(invoice.getItemValueString("invoice.currency"));
accounting.setAmount(amount);
// foreign currency
if (invoice.getItemValueDouble("invoice.rate") != 0.0) {
// YES
BigDecimal exchangeRate = BigDecimal.valueOf(invoice.getItemValueDouble("invoice.rate"))
.setScale(6, RoundingMode.HALF_UP);
// Hauswährung = Fremdwährung * Wechselkurs (auf 2 Stellen gerundet)
BigDecimal baseAmount = amount.divide(exchangeRate, 6, RoundingMode.HALF_UP).setScale(2,
RoundingMode.HALF_UP);
accounting.setExchangeRate(exchangeRate);
accounting.setBaseAmount(baseAmount);
} else {
// NO
accounting.setExchangeRate(null);
accounting.setBaseAmount(null);
}
// Update attributes
// Clear and add attributes (or use selective update if needed)
accounting.getAttributes().clear();
accounting.addAttribute("dbtr.name", workitem.getItemValueString("dbtr.name"));
accounting.addAttribute("dbtr.number", workitem.getItemValueString("dbtr.number"));
accounting.addAttribute("space.name", workitem.getItemValueString("space.name"));
// Add more attributes as needed
// Find the related invoice entry
JournalAccounting invoiceEntry = findAccountingEntryByBusinessRef(workitemRef, "Rechnungsausgang");
if (invoiceEntry != null) {
accounting.addAttribute("Invoice-Ref", invoiceEntry.getReference());
}
// Persist or merge
if (accounting.getId() == null) {