Correct export of Delivery Unit EON_EMOBILITY_VIRTA (42 files, replaces incomplete initial export)

This commit is contained in:
Thorsten Schmitz
2026-07-09 02:05:30 +02:00
parent dafd956626
commit 3348c5d03b
42 changed files with 6303 additions and 157 deletions
+492
View File
@@ -0,0 +1,492 @@
// Prod API Documentation Url: https://virta-api-admin.api-docs.io/
var ParDebug = false;
var Billingrunmode = "EMP";
var DateFrom = "";
var DateTo = "";
var output = {};
var outMessages = [];
var outErrorMessages = [];
var outMsg = {};
var outStatus = "success";
var myDebug = {};
var destn_pkg = "uniorg.eon.virta.v2";
var dest_name = "VirtaAPI";
var api_key = "qk2OLoXL3Y2Bqre9IQBdjaTqFUDvOJk888usKRiV";
var serviceVersion = '1.8';
var pageSize = 100;
/*if ($.request.parameters.get("pageSize")) {
pageSize = $.request.parameters.get("pageSize");
}*/
var debugLevel = 0;
switch ($.request.parameters.get("debug")) {
case "true":
case "yes":
case "1":
debugLevel = 1;
break;
case "full":
case "2":
debugLevel = 2;
break;
}
if (debugLevel > 0) {
output.requestParams = $.request.parameters;
}
function setOutputMessage() {
output.status = outStatus;
if (debugLevel > 0) {
output.debugInfos = myDebug;
}
output.messages = outMessages;
$.response.contentType = "application/json";
$.response.setBody(JSON.stringify(output));
//$.response.status = $.net.http.OK;
}
function exceptionHandler(ex) {
outMsg = {};
outMsg.method = "exceptionHandler";
outMsg.status = "error";
outMsg.message = ex.toString();
if (debugLevel > 0) {
outMsg.exceptionInfo = {};
outMsg.exceptionInfo.name = ex.name;
outMsg.exceptionInfo.message = ex.message;
outMsg.exceptionInfo.fileName = ex.fileName;
outMsg.exceptionInfo.lineNumber = ex.lineNumber;
outMsg.exceptionInfo.columnNumber = ex.columnNumber;
outMsg.exceptionInfo.stack = ex.stack;
//outMsg.exceptionInfo.source = ex.toSource();
}
outMessages.push(outMsg);
outStatus = "error";
output.status = outStatus;
if (debugLevel > 0) {
output.debugInfos = myDebug;
}
output.messages = outMessages;
var errbody = JSON.stringify(output);
$.response.contentType = 'application/json';
$.response.setBody(errbody);
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
}
function getVirtaToken(ifUsername, ifPassword) {
try
{
var dest = $.net.http.readDestination(destn_pkg, dest_name);
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.POST, "/auth");
req.headers.set("x-api-key", api_key);
req.contentType = "application/json";
outMsg = {};
outMsg.username = ifUsername;
outMsg.password = ifPassword;
req.setBody(JSON.stringify(outMsg));
client.request(req, dest);
var response = client.getResponse();
var b1iResp = response.body.asString();
var retObj = JSON.parse(b1iResp);
return retObj.token.toString();
} catch (e) {
$.response.contentType = "text/plain";
$.response.setBody(e.message);
}
}
function sleep(seconds) {
var currentTime = new Date().getTime();
while (currentTime + seconds * 1000 >= new Date().getTime()) {}
}
function getEMPList() {
var mytoken = getVirtaToken("b1_api_user", "E3qq$§U,Q3hD#K2}");
var dest = $.net.http.readDestination(destn_pkg, dest_name);
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.GET, "/emp/transactions");
req.contentType = "application/json";
req.headers.set("Authorization", "Bearer" + mytoken);
req.headers.set("x-api-key", api_key);
req.parameters.set("perPage", pageSize.toString());
req.parameters.set("from", DateFrom);
req.parameters.set("to", DateTo);
var totalCount = 1;
myDebug.Calls = [];
for (var p = 1; p <= Math.ceil(totalCount / pageSize); ++p) {
var debugCall = {};
debugCall.request = {
"page": p
};
req.parameters.set("page", p.toString());
client.request(req, dest);
var response = client.getResponse();
// get all the cookies and headers from the response
var he = [];
for (var c in response.headers) {
he.push(response.headers[c]);
}
debugCall.response = {
"headers": he
};
if (response.status === 429) {
// HTTP Status 429 "Too many requests"
var retryAfter = parseInt(response.headers.get('retry-after'), 10) + 1;
if (!(retryAfter > 0)) {
retryAfter = 10;
}
debugCall.waitSeconds = retryAfter;
sleep(retryAfter);
--p;
myDebug.Calls.push(debugCall);
} else if (response.status >= 200 && response.status < 300) {
var virtaBody = response.body.asString();
totalCount = response.headers.get('X-Total-Count');
var maxPageSize = Math.ceil(totalCount / pageSize);
debugCall.totalCount = totalCount;
debugCall.pagesCount = maxPageSize;
debugCall.link = response.headers.get('link');
debugCall.responseLineCount = JSON.parse(virtaBody).length;
myDebug.Calls.push(debugCall);
writeObj2Table(virtaBody);
} else {
debugCall.responseStatus = response.status;
myDebug.Calls.push(debugCall);
break;
}
//debugCall.virtaBody = virtaBody;
// debugCall.header = response.headers.toSting();
}
//$.response.status = $.net.http.OK;
//$.response.contentType = "application/json";
// $.response.setBody(JSON.stringify(b1iResp));
//$.response.setBody(JSON.stringify(virtaBody));
}
function getnextval() {
var conn = $.db.getConnection();
var pstmt;
var query = "select \"NICEPORTAL\".\"SEQ_CRMBP_ID\".NEXTVAL from dummy";
pstmt = conn.prepareStatement(query);
var rs = pstmt.executeQuery();
if (!rs.next()) {
outMsg = {};
outMsg.method = "getnexval from sequence error";
outMsg.status = "error";
// outMsg.message = e.message;
outMessages.push(outMsg);
} else {
return rs.getString(1);
}
rs.close();
pstmt.close();
conn.close();
}
function writeObj2Table(myOBJ) {
//var pdcontent = $.request.body.asString();
// pdcontent = pdcontent.replace(/\"([^(\")"]+)\":/g,"$1:");
var transOBJ = JSON.parse(myOBJ);
var conn = $.db.getConnection();
var pstmt;
var query;
var vals = {};
var data = [];
// var query2;
// var output = {results: [] };
//var mynextval = getnextval();
//myDebug.mynextval = mynextval;
try {
// query = "INSERT INTO \"NICEPORTAL\".\"UO_CRM_DATA_BP\" (ID,ID_MSD,TYPE,GROUP,SALUTATION,TITLE,NAME,SURNAME,PHONE,MOBILE_PHONE,EMAIL,ADDRESS_JSON,IBAN,BIC,SEPA_MANDATE_ID,SIGNATURE_DATE,ACCOUNT_OWNER,STREET,STREET_NO,ZIP_CODE,CITY,JSONRAW,CREATE_TS,UPDATE_TS) values (\"NICEPORTAL\".\"SEQ_CRMBP_ID\".NEXTVAL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
query =
"UPSERT \"VIRTAPORTAL\".\"EMP_TRANSACTIONS\" (UUID,CDRID,STATIONUUID,CUSTOMERID,STARTTIME,STOPTIME,DURATION,WATTHOUR,AMOUNT,CURRENCY,TRANSACTIONTYPE,CREATED,RFID,STATION,STATION_ID,STATION_NAME,LOCATION_ID,LOCATION_ADDRESS,LOCATION_CITY,LOCATION_TYPE,CUSTOMERGROUPID,CUSTOMERGROUPNAME,ORGANIZATIONID,PAYMENTTYPE,EVSEID,PLUGTYPE,EMPSHARE,SIGNEDMETERVALUEBEGIN,SIGNEDMETERVALUEEND,OPERATORPRICE,OPERATORCURRENCY,OPERATORID,FREECHARGING,EMPTARIFFID,EMPTARIFFNAME,FULLEVSEID,METERSTART,METERSTOP,NOTES,MILEAGE,JSONRAW,UPDATE_TS,EICHRECHTTYPE,KWHNETPRICE,MINUTENETPRICE,SESSIONNETPRICE,CPOOPERATORID,KWHGROSSPRICE,MINUTEGROSSPRICE,SESSIONGROSSPRICE,LOCATION_COUNTRY) values (" +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?,?,?,?,?,?,?,?,?) WITH PRIMARY KEY";
// "INSERT INTO \"NICEPORTAL\".\"UO_CRM_DATA_BP\" (ID,ID_MSD,ID_CRM,TYPE,BPGROUP,SALUTATION,TITLE,NAME,SURNAME,PHONE,MOBILE_PHONE,EMAIL,ADDRESS_JSON,IBAN,BIC,SEPA_MANDATE_ID,SIGNATURE_DATE,ACCOUNT_OWNER,STREET,STREET_NO,ZIP_CODE,CITY,JSONRAW,CREATE_TS,UPDATE_TS) values (\"NICEPORTAL\".\"SEQ_CRMBP_ID\".NEXTVAL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
pstmt = conn.prepareStatement(query);
pstmt.setBatchSize(transOBJ.length);
//$.trace.debug("prepareBatchStatement (" + transOBJ.length.toString() + " rows): " + query);
transOBJ.forEach(function(transRow) {
pstmt.setNString(1, transRow.uuid);
/*
if (!isNaN(parseInt(transRow.cdrId))) {
pstmt.setInteger(2, transRow.cdrId);
} else {
pstmt.setNull(2);
}
*/
if (transRow.cdrId) {
pstmt.setNString(2, transRow.cdrId.toString());
} else {
pstmt.setNull(2);
}
pstmt.setNString(3, transRow.stationUuid);
pstmt.setInteger(4, transRow.customerId);
pstmt.setNString(5, transRow.startTime);
pstmt.setNString(6, transRow.stopTime);
if (!isNaN(parseInt(transRow.duration))) {
pstmt.setInteger(7, transRow.duration);
} else {
pstmt.setNull(7);
}
if (!isNaN(parseInt(transRow.wattHour))) {
pstmt.setInteger(8, transRow.wattHour);
} else {
pstmt.setNull(8);
}
if (!isNaN(parseInt(transRow.amount))) {
pstmt.setInteger(9, transRow.amount);
} else {
pstmt.setNull(9);
}
pstmt.setNString(10, transRow.currency);
pstmt.setNString(11, transRow.transactionType);
pstmt.setNString(12, transRow.created);
if (transRow.rfid) {
pstmt.setNString(13, transRow.rfid);
} else {
pstmt.setNull(13);
}
if (transRow.station) {
pstmt.setNClob(14, JSON.stringify(transRow.station));
pstmt.setNString(15, transRow.station.identity);
if (transRow.station.name) {
pstmt.setNString(16, transRow.station.name);
} else {
pstmt.setNull(16);
}
if (typeof transRow.station.location === "undefined") {
pstmt.setNull(17);
pstmt.setNull(18);
pstmt.setNull(19);
pstmt.setNull(20);
pstmt.setNull(50);
} else {
pstmt.setInteger(17, transRow.station.location.id);
pstmt.setNString(18, transRow.station.location.address);
pstmt.setNString(19, transRow.station.location.city);
pstmt.setNString(20, transRow.station.location.locationType);
pstmt.setNString(50, transRow.station.location.country);
}
} else {
pstmt.setNull(14);
pstmt.setNull(15);
pstmt.setNull(16);
pstmt.setNull(17);
pstmt.setNull(18);
pstmt.setNull(19);
pstmt.setNull(20);
pstmt.setNull(50);
}
pstmt.setInteger(21, transRow.customerGroupId);
pstmt.setNString(22, transRow.customerGroupName);
pstmt.setInteger(23, transRow.organizationId);
pstmt.setNString(24, transRow.paymentType);
if (transRow.evseId) {
pstmt.setNString(25, transRow.evseId.toString());
} else {
pstmt.setNull(25);
}
if (transRow.plugType) {
pstmt.setNString(26, transRow.plugType);
} else {
pstmt.setNull(26);
}
if (!isNaN(parseInt(transRow.empShare))) {
pstmt.setInteger(27, transRow.empShare);
} else {
pstmt.setNull(27);
}
if (transRow.signedMeterValueBegin) {
pstmt.setNString(28, transRow.signedMeterValueBegin.substring(0, 50));
} else {
pstmt.setNull(28);
}
if (transRow.signedMeterValueEnd) {
pstmt.setNString(29, transRow.signedMeterValueEnd.substring(0, 50));
} else {
pstmt.setNull(29);
}
if (!isNaN(parseInt(transRow.operatorPrice))) {
pstmt.setInteger(30, transRow.operatorPrice);
} else {
pstmt.setNull(30);
}
pstmt.setNString(31, transRow.operatorCurrency);
if (!isNaN(parseInt(transRow.operatorId))) {
pstmt.setInteger(32, transRow.operatorId);
} else {
pstmt.setNull(32);
}
pstmt.setNString(33, transRow.freeCharging);
if (!isNaN(parseInt(transRow.empTariffId))) {
pstmt.setInteger(34, transRow.empTariffId);
} else {
pstmt.setNull(34);
}
if (transRow.empTariffName) {
pstmt.setNString(35, transRow.empTariffName);
} else {
pstmt.setNull(35);
}
if (transRow.fullEvseId) {
pstmt.setNString(36, transRow.fullEvseId);
} else {
pstmt.setNull(36);
}
if (!isNaN(parseInt(transRow.meterStart))) {
pstmt.setInteger(37, transRow.meterStart);
} else {
pstmt.setNull(37);
}
if (!isNaN(parseInt(transRow.meterStop))) {
pstmt.setInteger(38, transRow.meterStop);
} else {
pstmt.setNull(38);
}
pstmt.setNString(39, JSON.stringify(transRow.notes));
if (transRow.notes && transRow.notes.length > 0) {
pstmt.setInteger(40, transRow.notes[0].value);
} else {
pstmt.setNull(40);
}
//pstmt.setNString(34, "1234");
pstmt.setNString(41, JSON.stringify(transRow));
pstmt.setNString(42, transRow.eichrechtType);
if (!isNaN(parseInt(transRow.empKwhNetPrice))) {
pstmt.setInteger(43, transRow.empKwhNetPrice);
} else {
pstmt.setNull(43);
}
if (!isNaN(parseInt(transRow.empMinuteNetPrice))) {
pstmt.setInteger(44, transRow.empMinuteNetPrice);
} else {
pstmt.setNull(44);
}
if (!isNaN(parseInt(transRow.empSessionNetPrice))) {
pstmt.setInteger(45, transRow.empSessionNetPrice);
} else {
pstmt.setNull(45);
}
if (transRow.cpoOperatorId) {
pstmt.setNString(46, transRow.cpoOperatorId);
} else {
pstmt.setNull(46);
}
if (!isNaN(parseInt(transRow.empKwhGrossPrice))) {
pstmt.setInteger(47, transRow.empKwhGrossPrice);
} else {
pstmt.setNull(47);
}
if (!isNaN(parseInt(transRow.empMinuteGrossPrice))) {
pstmt.setInteger(48, transRow.empMinuteGrossPrice);
} else {
pstmt.setNull(48);
}
if (!isNaN(parseInt(transRow.empSessionGrossPrice))) {
pstmt.setInteger(49, transRow.empSessionGrossPrice);
} else {
pstmt.setNull(49);
}
pstmt.addBatch();
});
var batchResult = pstmt.executeBatch();
//$.trace.debug("BatchUpsert Result: " + batchResult.toString());
pstmt.close();
conn.commit();
conn.close();
outMsg = {};
outMsg.method = "insertDB";
outMsg.status = "success";
outMessages.push(outMsg);
} catch (e) {
$.trace.error("InsertDB ExceptionHandler: " + e.toString());
outMsg = {};
outMsg.method = "exceptionHandler";
outMsg.status = "error";
outMsg.message = e.toString();
if (ParDebug) {
outMsg.exceptionInfo = {};
outMsg.exceptionInfo.name = e.name;
outMsg.exceptionInfo.message = e.message;
outMsg.exceptionInfo.fileName = e.fileName;
outMsg.exceptionInfo.lineNumber = e.lineNumber;
outMsg.exceptionInfo.columnNumber = e.columnNumber;
outMsg.exceptionInfo.stack = e.stack;
//outMsg.exceptionInfo.source = e.toSource();
}
outMessages.push(outMsg);
vals.status = 'error';
//vals.description = e.message;
vals.messages = outMessages;
if (ParDebug) {
vals.debug = {};
vals.debug.statement = query;
}
}
}
var content = '';
if ($.request.body) {
content = $.request.body.asString();
}
ParDebug = $.request.parameters.get('debug') && $.request.parameters.get('debug').toLowerCase() === "true";
if ($.request.parameters.get('Billingrunmode') > 0) {
Billingrunmode = $.request.parameters.get('Billingrunmode');
}
if ($.request.parameters.get('DateFrom')) {
DateFrom = $.request.parameters.get('DateFrom');
}
if ($.request.parameters.get('DateTo')) {
DateTo = $.request.parameters.get('DateTo');
}
//doHTTPCallstartBillingrun("FEE","20180101","20180131");
//getVirtaToken("b1_api_user","E3qq$§U,Q3hD#K2}");
getEMPList();
setOutputMessage();