added statistik data object

This commit is contained in:
Ralph Soika 2023-07-18 18:01:23 +02:00
parent ef8833dd59
commit ff6ff80506
2 changed files with 202 additions and 30 deletions

View file

@ -1,12 +1,21 @@
package com.alexanderlogistics; package com.alexanderlogistics;
import java.io.Serializable; 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 java.util.logging.Logger;
import javax.enterprise.context.ConversationScoped; import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService; import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.exceptions.QueryException; import org.imixs.workflow.exceptions.QueryException;
import org.imixs.workflow.faces.data.WorkflowController; import org.imixs.workflow.faces.data.WorkflowController;
@ -38,13 +47,10 @@ public class DebitorStatistikController implements Serializable {
public int getRechnungen() { public int getRechnungen() {
int result = 0; int result = 0;
String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number");
if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) {
dbtNr = dbtNr.substring(1);
}
try { try {
result = documentService 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 $workflowgroup:\"Rechnungsausgang\"");
} catch (QueryException e) { } catch (QueryException e) {
logger.severe("Failed to get statistic: " + e.getMessage()); logger.severe("Failed to get statistic: " + e.getMessage());
@ -54,13 +60,10 @@ public class DebitorStatistikController implements Serializable {
public int getRechnungenBezahlt() { public int getRechnungenBezahlt() {
int result = 0; int result = 0;
String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number");
if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) {
dbtNr = dbtNr.substring(1);
}
try { try {
result = documentService 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])"); + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5900 TO 5999])");
} catch (QueryException e) { } catch (QueryException e) {
logger.severe("Failed to get statistic: " + e.getMessage()); logger.severe("Failed to get statistic: " + e.getMessage());
@ -70,13 +73,10 @@ public class DebitorStatistikController implements Serializable {
public int getRechnungenOffen() { public int getRechnungenOffen() {
int result = 0; int result = 0;
String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number");
if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) {
dbtNr = dbtNr.substring(1);
}
try { try {
result = documentService result = documentService
.count("(type:workitem) AND dbtr.number:" + dbtNr .count("(type:workitem) AND dbtr.number:" + getDbtNr()
+ " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5000 TO 5099])"); + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5000 TO 5099])");
} catch (QueryException e) { } catch (QueryException e) {
logger.severe("Failed to get statistic: " + e.getMessage()); logger.severe("Failed to get statistic: " + e.getMessage());
@ -86,13 +86,10 @@ public class DebitorStatistikController implements Serializable {
public int getRechnungenFaellig() { public int getRechnungenFaellig() {
int result = 0; int result = 0;
String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number");
if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) {
dbtNr = dbtNr.substring(1);
}
try { try {
result = documentService result = documentService
.count("(type:workitem) AND dbtr.number:" + dbtNr .count("(type:workitem) AND dbtr.number:" + getDbtNr()
+ " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5100 TO 5199])"); + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5100 TO 5199])");
} catch (QueryException e) { } catch (QueryException e) {
logger.severe("Failed to get statistic: " + e.getMessage()); logger.severe("Failed to get statistic: " + e.getMessage());
@ -102,13 +99,10 @@ public class DebitorStatistikController implements Serializable {
public int getRechnungenGemahnt() { public int getRechnungenGemahnt() {
int result = 0; int result = 0;
String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number");
if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) {
dbtNr = dbtNr.substring(1);
}
try { try {
result = documentService result = documentService
.count("(type:workitem ) AND dbtr.number:" + dbtNr .count("(type:workitem ) AND dbtr.number:" + getDbtNr()
+ " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5200 TO 5299])"); + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5200 TO 5299])");
} catch (QueryException e) { } catch (QueryException e) {
logger.severe("Failed to get statistic: " + e.getMessage()); logger.severe("Failed to get statistic: " + e.getMessage());
@ -118,13 +112,10 @@ public class DebitorStatistikController implements Serializable {
public int getRechnungenInkasso() { public int getRechnungenInkasso() {
int result = 0; int result = 0;
String dbtNr = workflowController.getWorkitem().getItemValueString("dbtr.number");
if (dbtNr.startsWith("D") || dbtNr.startsWith("K")) {
dbtNr = dbtNr.substring(1);
}
try { try {
result = documentService 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])"); + " AND $workflowgroup:\"Rechnungsausgang\" AND ($taskid:[5300 TO 5399])");
} catch (QueryException e) { } catch (QueryException e) {
logger.severe("Failed to get statistic: " + e.getMessage()); logger.severe("Failed to get statistic: " + e.getMessage());
@ -132,4 +123,129 @@ public class DebitorStatistikController implements Serializable {
return result; 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<String, DebitorStatistikData> stats = new HashMap<String, DebitorStatistikData>();
// 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<ItemCollection> 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<String> 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;
}
} }

View file

@ -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);
}
}