fixed decimal format germany for cargosoft export

This commit is contained in:
Ralph Soika 2023-01-24 13:31:07 +01:00
parent 9edadfed88
commit c2213ff656

View file

@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -67,204 +68,206 @@ import org.imixs.workflow.exceptions.QueryException;
*/ */
public class CargosoftExportScheduler implements Scheduler { public class CargosoftExportScheduler implements Scheduler {
public static final int MAX_COUNT = 999; public static final int MAX_COUNT = 999;
public static final String FTP_ERROR = "FTP_ERROR"; public static final String FTP_ERROR = "FTP_ERROR";
public static DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy"); public static DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
public static DecimalFormat decimalFormat = new DecimalFormat("0.00"); public static DecimalFormat decimalFormat = new DecimalFormat("0.00",
public static DecimalFormat rateFormat = new DecimalFormat("0.000000"); new DecimalFormatSymbols(java.util.Locale.GERMANY));
public static DecimalFormat rateFormat = new DecimalFormat("0.000000",
new DecimalFormatSymbols(java.util.Locale.GERMANY));
@EJB @EJB
DocumentService documentService; DocumentService documentService;
@EJB @EJB
WorkflowService workflowService; WorkflowService workflowService;
@EJB @EJB
ModelService modelService; ModelService modelService;
@EJB @EJB
ReportService reportService; ReportService reportService;
private static Logger logger = Logger.getLogger(CargosoftExportScheduler.class.getName()); private static Logger logger = Logger.getLogger(CargosoftExportScheduler.class.getName());
/** /**
* This is the method which processes the timeout event depending on the running * This is the method which processes the timeout event depending on the running
* timer settings. * timer settings.
* *
* *
* *
* @param timer * @param timer
* @throws QueryException * @throws QueryException
*/ */
public ItemCollection run(ItemCollection configuration) throws SchedulerException { public ItemCollection run(ItemCollection configuration) throws SchedulerException {
List<ItemCollection> invoices = null; List<ItemCollection> invoices = null;
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
int lines = 0; int lines = 0;
configuration.removeItem("_scheduler_logmessage"); configuration.removeItem("_scheduler_logmessage");
logMessage(configuration, "....export to Cargosoft..."); logMessage(configuration, "....export to Cargosoft...");
// $taskID<5900 // $taskID<5900
String sQuery = "(type:workitem AND $modelversion:rechnungsausgang* AND $taskid:[5000 TO 5899])"; String sQuery = "(type:workitem AND $modelversion:rechnungsausgang* AND $taskid:[5000 TO 5899])";
try { try {
// find the textblock... // find the textblock...
invoices = documentService.find(sQuery, 9999, 0); invoices = documentService.find(sQuery, 9999, 0);
} catch (QueryException e) { } catch (QueryException e) {
logMessage(configuration, "failed to search invoices by query: " + e.getMessage()); logMessage(configuration, "failed to search invoices by query: " + e.getMessage());
} }
if (invoices != null && invoices.size() > 0) { if (invoices != null && invoices.size() > 0) {
// File csvOutputFile = new File(CSV_FILE_NAME); // File csvOutputFile = new File(CSV_FILE_NAME);
for (ItemCollection doc : invoices) { for (ItemCollection doc : invoices) {
String line = ""; String line = "";
line = line + doc.getItemValueString("dbtr.number") + ";"; line = line + doc.getItemValueString("dbtr.number") + ";";
line = line + "\"" + doc.getItemValueString("dbtr.name") + "\";"; line = line + "\"" + doc.getItemValueString("dbtr.name") + "\";";
line = line + "\"" + doc.getItemValueString("invoice.number") + "\";"; line = line + "\"" + doc.getItemValueString("invoice.number") + "\";";
line = line + dateFormat.format(doc.getItemValueDate("invoice.date")) + ";"; line = line + dateFormat.format(doc.getItemValueDate("invoice.date")) + ";";
line = line + dateFormat.format(doc.getItemValueDate("invoice.duedate")) + ";"; line = line + dateFormat.format(doc.getItemValueDate("invoice.duedate")) + ";";
// Betrag soll; Betrag Haben; // Betrag soll; Betrag Haben;
double betrag = doc.getItemValueDouble("invoice.total"); double betrag = doc.getItemValueDouble("invoice.total");
if (betrag >= 0) { if (betrag >= 0) {
// Soll // Soll
line = line + decimalFormat.format(betrag) + ";0,00;"; line = line + decimalFormat.format(betrag) + ";0,00;";
} else { } else {
// Haben // Haben
line = line + "0,00;" + decimalFormat.format(Math.abs(betrag)) + ";"; line = line + "0,00;" + decimalFormat.format(Math.abs(betrag)) + ";";
} }
// Saldo; S/H Saldo // Saldo; S/H Saldo
betrag = doc.getItemValueDouble("invoice.saldo"); betrag = doc.getItemValueDouble("invoice.saldo");
// convert to hauswahrung // convert to hauswahrung
double rate = doc.getItemValueDouble("invoice.rate"); double rate = doc.getItemValueDouble("invoice.rate");
double hausWaehrung = betrag; double hausWaehrung = betrag;
if (rate != 0.0) { if (rate != 0.0) {
hausWaehrung = betrag / rate; hausWaehrung = betrag / rate;
} }
if (hausWaehrung >= 0) { if (hausWaehrung >= 0) {
// Soll // Soll
line = line + decimalFormat.format(hausWaehrung) + ";S;"; line = line + decimalFormat.format(hausWaehrung) + ";S;";
} else { } else {
// Haben // Haben
line = line + decimalFormat.format(Math.abs(hausWaehrung)) + ";H;"; line = line + decimalFormat.format(Math.abs(hausWaehrung)) + ";H;";
} }
// WKZ // WKZ
String wkz = doc.getItemValueString("invoice.currency"); String wkz = doc.getItemValueString("invoice.currency");
if (!"EUR".equals(wkz)) { if (!"EUR".equals(wkz)) {
line = line + // line = line + //
wkz + ";" + // wkz + ";" + //
decimalFormat.format(doc.getItemValueDouble("invoice.base.amount")) + ";" + // decimalFormat.format(doc.getItemValueDouble("invoice.base.amount")) + ";" + //
rateFormat.format(doc.getItemValueDouble("invoice.rate")) + ";"; rateFormat.format(doc.getItemValueDouble("invoice.rate")) + ";";
} else { } else {
line = line + ";0,00;0,000000;"; line = line + ";0,00;0,000000;";
} }
line = line + ";;;;;;" +doc.getItemValueString("invoice.text"); line = line + ";;;;;;" + doc.getItemValueString("invoice.text");
// auffüllen mit 77 mal ';' // auffüllen mit 77 mal ';'
int i = 69; int i = 69;
while (i > 0) { while (i > 0) {
line = line + ";"; line = line + ";";
i--; i--;
} }
buffer.append(line + "\n"); buffer.append(line + "\n");
lines++; lines++;
} }
} }
// transfer file via FTP... // transfer file via FTP...
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm"); DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
// MMM d, yyyy HH:mm a // MMM d, yyyy HH:mm a
FileData fileData = new FileData("OPD_" + formatter.format(new Date()) + ".txt", FileData fileData = new FileData("OPD_" + formatter.format(new Date()) + ".txt",
String.valueOf(buffer).getBytes(), null, null); String.valueOf(buffer).getBytes(), null, null);
put(configuration, fileData); put(configuration, fileData);
logMessage(configuration, "...Export completed: " + lines + " lines"); logMessage(configuration, "...Export completed: " + lines + " lines");
return configuration; return configuration;
} }
private void logMessage(ItemCollection configuration, String message) { private void logMessage(ItemCollection configuration, String message) {
configuration.appendItemValue("_scheduler_logmessage", message); configuration.appendItemValue("_scheduler_logmessage", message);
logger.info(message); logger.info(message);
} }
/** /**
* Diese Hilfsmehtode überträgt eine Datei an einen FTP Server. Die * Diese Hilfsmehtode überträgt eine Datei an einen FTP Server. Die
* Verbindugnsdaten stehen im Configuraiton Workitem. * Verbindugnsdaten stehen im Configuraiton Workitem.
* *
* @param fileData object * @param fileData object
* @throws PluginException * @throws PluginException
*/ */
public void put(ItemCollection configuration, FileData fileData) { public void put(ItemCollection configuration, FileData fileData) {
String ftpServer = configuration.getItemValueString("_datev_ftp_host"); String ftpServer = configuration.getItemValueString("_datev_ftp_host");
String ftpUser = configuration.getItemValueString("_datev_ftp_userid"); String ftpUser = configuration.getItemValueString("_datev_ftp_userid");
String ftpPassword = configuration.getItemValueString("_datev_ftp_password"); String ftpPassword = configuration.getItemValueString("_datev_ftp_password");
String ftpWorkingPath = configuration.getItemValueString("_datev_ftp_path_buchungsstapel"); String ftpWorkingPath = configuration.getItemValueString("_datev_ftp_path_buchungsstapel");
int ftpPort = configuration.getItemValueInteger("_datev_ftp_port"); int ftpPort = configuration.getItemValueInteger("_datev_ftp_port");
if (ftpPort <= 0) { if (ftpPort <= 0) {
// set default port // set default port
ftpPort = 21; ftpPort = 21;
} }
String fileName = fileData.getName(); String fileName = fileData.getName();
// Compute file path // Compute file path
if (!ftpWorkingPath.startsWith("/")) { if (!ftpWorkingPath.startsWith("/")) {
ftpWorkingPath = "/" + ftpWorkingPath; ftpWorkingPath = "/" + ftpWorkingPath;
} }
if (!ftpWorkingPath.endsWith("/")) { if (!ftpWorkingPath.endsWith("/")) {
ftpWorkingPath = ftpWorkingPath + "/"; ftpWorkingPath = ftpWorkingPath + "/";
} }
InputStream writer = null; InputStream writer = null;
FTPClient ftpClient = null; FTPClient ftpClient = null;
try { try {
logMessage(configuration, "......put " + fileName + " to FTP server: " + ftpServer + "..."); logMessage(configuration, "......put " + fileName + " to FTP server: " + ftpServer + "...");
ftpClient = new FTPSClient("TLS", false); ftpClient = new FTPSClient("TLS", false);
ftpClient.setBufferSize(8192); ftpClient.setBufferSize(8192);
ftpClient.connect(ftpServer, ftpPort); ftpClient.connect(ftpServer, ftpPort);
if (ftpClient.login(ftpUser, ftpPassword) == false) { if (ftpClient.login(ftpUser, ftpPassword) == false) {
logMessage(configuration, "FTP file transfer failed: login failed!"); logMessage(configuration, "FTP file transfer failed: login failed!");
} }
ftpClient.enterLocalPassiveMode(); ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.ASCII_FILE_TYPE); ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
ftpClient.setControlEncoding("UTF-8"); ftpClient.setControlEncoding("UTF-8");
// verify directories // verify directories
if (!ftpClient.changeWorkingDirectory(ftpWorkingPath)) { if (!ftpClient.changeWorkingDirectory(ftpWorkingPath)) {
logMessage(configuration, "FTP file transfer failed: missing working directory '" + ftpWorkingPath logMessage(configuration, "FTP file transfer failed: missing working directory '" + ftpWorkingPath
+ "' : " + ftpClient.getReplyString()); + "' : " + ftpClient.getReplyString());
} }
// upload file to FTP server. // upload file to FTP server.
writer = new ByteArrayInputStream(fileData.getContent()); writer = new ByteArrayInputStream(fileData.getContent());
if (!ftpClient.storeFile(fileName, writer)) { if (!ftpClient.storeFile(fileName, writer)) {
logMessage(configuration, "FTP file transfer failed: unable to write '" + ftpWorkingPath + fileName logMessage(configuration, "FTP file transfer failed: unable to write '" + ftpWorkingPath + fileName
+ "' : " + ftpClient.getReplyString()); + "' : " + ftpClient.getReplyString());
} }
logMessage(configuration, "...." + ftpWorkingPath + fileName + " transfered successfull to " + ftpServer); logMessage(configuration, "...." + ftpWorkingPath + fileName + " transfered successfull to " + ftpServer);
} catch (IOException e) { } catch (IOException e) {
logMessage(configuration, "FTP file transfer failed: " + e.getMessage()); logMessage(configuration, "FTP file transfer failed: " + e.getMessage());
} finally { } finally {
// do logout.... // do logout....
try { try {
if (writer != null) { if (writer != null) {
writer.close(); writer.close();
} }
ftpClient.logout(); ftpClient.logout();
ftpClient.disconnect(); ftpClient.disconnect();
} catch (IOException e) { } catch (IOException e) {
logMessage(configuration, "FTP file transfer failed: " + e.getMessage()); logMessage(configuration, "FTP file transfer failed: " + e.getMessage());
} }
} }
} }
} }