diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikController.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikController.java index 9c222ad..933502b 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikController.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikController.java @@ -1,12 +1,21 @@ package com.alexanderlogistics; import java.io.Serializable; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.logging.Logger; import javax.enterprise.context.ConversationScoped; import javax.inject.Inject; import javax.inject.Named; +import org.imixs.workflow.ItemCollection; import org.imixs.workflow.engine.DocumentService; import org.imixs.workflow.exceptions.QueryException; import org.imixs.workflow.faces.data.WorkflowController; @@ -38,13 +47,10 @@ public class DebitorStatistikController implements Serializable { public int getRechnungen() { int result = 0; - String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); - if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { - dbtNr = dbtNr.substring(1); - } + try { result = documentService - .count("(type:workitem OR type:workitemarchive) AND dbtr.number:" + dbtNr + .count("(type:workitem OR type:workitemarchive) AND dbtr.number:" + getDbtNr() + " AND $workflowgroup:\"Rechnungsausgang\""); } catch (QueryException e) { logger.severe("Failed to get statistic: " + e.getMessage()); @@ -54,13 +60,10 @@ public class DebitorStatistikController implements Serializable { public int getRechnungenBezahlt() { int result = 0; - String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); - if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { - dbtNr = dbtNr.substring(1); - } + try { result = documentService - .count("(type:workitem OR type:workitemarchive) AND dbtr.number:" + dbtNr + .count("(type:workitem OR type:workitemarchive) AND dbtr.number:" + getDbtNr() + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5900 TO 5999])"); } catch (QueryException e) { logger.severe("Failed to get statistic: " + e.getMessage()); @@ -70,13 +73,10 @@ public class DebitorStatistikController implements Serializable { public int getRechnungenOffen() { int result = 0; - String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); - if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { - dbtNr = dbtNr.substring(1); - } + try { result = documentService - .count("(type:workitem) AND dbtr.number:" + dbtNr + .count("(type:workitem) AND dbtr.number:" + getDbtNr() + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5000 TO 5099])"); } catch (QueryException e) { logger.severe("Failed to get statistic: " + e.getMessage()); @@ -86,13 +86,10 @@ public class DebitorStatistikController implements Serializable { public int getRechnungenFaellig() { int result = 0; - String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); - if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { - dbtNr = dbtNr.substring(1); - } + try { result = documentService - .count("(type:workitem) AND dbtr.number:" + dbtNr + .count("(type:workitem) AND dbtr.number:" + getDbtNr() + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5100 TO 5199])"); } catch (QueryException e) { logger.severe("Failed to get statistic: " + e.getMessage()); @@ -102,13 +99,10 @@ public class DebitorStatistikController implements Serializable { public int getRechnungenGemahnt() { int result = 0; - String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); - if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { - dbtNr = dbtNr.substring(1); - } + try { result = documentService - .count("(type:workitem ) AND dbtr.number:" + dbtNr + .count("(type:workitem ) AND dbtr.number:" + getDbtNr() + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5200 TO 5299])"); } catch (QueryException e) { logger.severe("Failed to get statistic: " + e.getMessage()); @@ -118,13 +112,10 @@ public class DebitorStatistikController implements Serializable { public int getRechnungenInkasso() { int result = 0; - String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); - if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { - dbtNr = dbtNr.substring(1); - } + try { result = documentService - .count("(type:workitem OR type:workitemarchive) AND dbtr.number:" + dbtNr + .count("(type:workitem OR type:workitemarchive) AND dbtr.number:" + getDbtNr() + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5300 TO 5399])"); } catch (QueryException e) { logger.severe("Failed to get statistic: " + e.getMessage()); @@ -132,4 +123,129 @@ public class DebitorStatistikController implements Serializable { return result; } + /** + * Hilfsmethode die das fuehrende K/D aus der Debitorennummer entfernt + * + * @return + */ + private String getDbtNr() { + String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number"); + if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) { + dbtNr = dbtNr.substring(1); + } + return dbtNr; + } + + /** + * Sucht alle Rechnugnen aus einem Zeitraum und sammelt Zahlungsziel und + * Zahlungszeitspanne gruppiert nach monaten + * + */ + public void collect(Date from, Date to) { + + Map stats = new HashMap(); + + // invoice.date:[20210101 TO 20210201] + DateFormat df = new SimpleDateFormat("yyyymmdd"); + + String query = "(type:workitemarchive) AND dbtr.number:" + getDbtNr() + + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5300 TO 5399])"; + + query = query + " AND (invoice.date:[" + df.format(from) + " TO " + df.format(to) + "])"; + logger.info("query = " + query); + try { + List invoices = documentService + .find(query, 9999, 0); + + for (ItemCollection invoice : invoices) { + try { + + Date invoiceDate = invoice.getItemValueDate("invoice.date"); + Date paymentDate = findLastEventDate(invoice); + + if (invoiceDate != null && paymentDate != null) { + // do we have a stats object? + DebitorStatistikData statData = stats.get(getYearMonth(invoiceDate)); + if (statData == null) { + // create a new one + statData = new DebitorStatistikData(invoiceDate); + } + + // jetzt irgendwas ausrechnen + + // .. und wieder speichern + stats.put(statData.toString(), statData); + + } else { + logger.warning("No payment Date found for invoice " + invoice.getUniqueID()); + } + + } catch (ParseException e) { + logger.warning("Unable to parse payment Date for invoice " + invoice.getUniqueID()); + } + } + + } catch (QueryException e) { + logger.severe("Failed to get statistic: " + e.getMessage()); + } + + } + + /** + * returns + * + * 202304 from a given date + */ + public String getYearMonth(Date date) { + + // Create a Calendar instance and set the date + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + + // Get the year from the Calendar object + int year = calendar.get(Calendar.YEAR); + // Get the month from the Calendar object + int month = calendar.get(Calendar.MONTH); + // Increment the month by 1 since Calendar months are zero-based + month++; + // Convert the month to a String with leading "0" if necessary + return "" + year + (month < 10 ? "0" + month : "" + month); + + } + + /** + * Finds the date when a workitem last reached the current task + * + * 2023-06-28T12:40:17.437|rechnungsausgang-de-1.0|5001.50|5900| + * + * @param invoice + * @return + * @throws ParseException + */ + public Date findLastEventDate(ItemCollection invoice) throws ParseException { + + // Create a SimpleDateFormat instance with the desired format + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); + List logEntries = invoice.getItemValue("$eventlog"); + + // Iterate through the List from the last element to the first + for (int i = logEntries.size() - 1; i >= 0; i--) { + String entry = logEntries.get(i); + + if (entry.endsWith(invoice.getTaskID() + "|")) { + // found! + String dateString = entry.substring(0, entry.indexOf("|") - 1); + // Parse the date string into a Date object + Date date = sdf.parse(dateString); + // Print the Date object + System.out.println("Date: " + date); + return date; + } + } + + // no date found! + return null; + + } + } diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikData.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikData.java new file mode 100644 index 0000000..7bf11d2 --- /dev/null +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/DebitorStatistikData.java @@ -0,0 +1,56 @@ +package com.alexanderlogistics; + +import java.util.Calendar; +import java.util.Date; + +/** + * Speichert Payment statistik + */ +public class DebitorStatistikData { + + int month; + int year; + + int count = 0; + int paymentTime; // Zahlungsziel + int paymentDays; // Tatsächliche Zahlung + + public DebitorStatistikData(Date date) { + // Create a Calendar instance and set the date + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + + // Get the year from the Calendar object + this.year = calendar.get(Calendar.YEAR); + // Get the month from the Calendar object + this.month = calendar.get(Calendar.MONTH); + // Increment the month by 1 since Calendar months are zero-based + this.month++; + + } + + public DebitorStatistikData(int year, int month) { + this.month = month; + this.year = year; + } + + /** + * Aktualisiert die statistischen werte + * + * @param invoiceDate + * @param paymentDate + */ + public void compute(Date invoiceDate, Date paymentDate) { + // und was nun? + } + + /** + * returns + * + * 202304 + */ + public String toString() { + return "" + year + (month < 10 ? "0" + month : "" + month); + } + +}