Correct export of Delivery Unit EON_CRM_INVOICE_STATUS (9 files, replaces incomplete initial export)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
CREATE COLUMN TABLE "INTEGRATION_DATA"."CRM_INV_PROC" ("Company" VARCHAR(50) NOT NULL ,
|
CREATE COLUMN TABLE "INTEGRATION_DATA"."CRM_INV_PROC" ("Company" VARCHAR(50) NOT NULL ,
|
||||||
"DocEntry" INTEGER CS_INT NOT NULL,
|
"DocEntry" INTEGER CS_INT NOT NULL,
|
||||||
"DocNum" INTEGER CS_INT NOT NULL,
|
"DocNum" INTEGER CS_INT NOT NULL,
|
||||||
"CRM_GUID" NVARCHAR(36) NOT NULL,
|
"CRM_GUID" NVARCHAR(36) NOT NULL,
|
||||||
|
|||||||
@@ -1 +1,48 @@
|
|||||||
-- Create views for CRM invoice status
|
SET SCHEMA "A19753_EON_P01";
|
||||||
|
/* UO_CRM_INV_PROC V1.1 */
|
||||||
|
ALTER VIEW UO_CRM_INV_PROC ("Company",
|
||||||
|
"DocEntry",
|
||||||
|
"DocNum",
|
||||||
|
"CRM_GUID",
|
||||||
|
"INVOICE_STATUS",
|
||||||
|
"ARCHIVE_LINK",
|
||||||
|
"EMAIL_SENT_AT",
|
||||||
|
"PAID_AT")
|
||||||
|
as
|
||||||
|
with Spool as (
|
||||||
|
select max("DocEntry") as "DocEntry", U_UO_DOCENTRY, U_UO_OBJTYPE
|
||||||
|
from "@UO_SPOOL"
|
||||||
|
group by U_UO_DOCENTRY, U_UO_OBJTYPE
|
||||||
|
),
|
||||||
|
ArchiveTask as (
|
||||||
|
select "DocEntry", last_value(U_UO_URL order by "LineId") as "ArchiveLink"
|
||||||
|
from "@UO_SPOOL_TASKS"
|
||||||
|
where U_UO_SPOOLTYPE='A' and U_UO_STATUS='S'
|
||||||
|
group by "DocEntry"
|
||||||
|
),
|
||||||
|
MailTask as (
|
||||||
|
select "DocEntry", last_value(TO_TIMESTAMP(U_UO_TIMESTAMP) order by "LineId") as "EmailSentAt"
|
||||||
|
from "@UO_SPOOL_TASKS"
|
||||||
|
where U_UO_SPOOLTYPE='M' and U_UO_STATUS='S'
|
||||||
|
group by "DocEntry"
|
||||||
|
),
|
||||||
|
LastReconciliation as (
|
||||||
|
select OINV."DocEntry", max(OITR."ReconDate") as "ReconciliationDate"
|
||||||
|
from OITR
|
||||||
|
inner join ITR1 on OITR."ReconNum"=ITR1."ReconNum"
|
||||||
|
inner join OINV on ITR1."SrcObjAbs"=OINV."DocEntry" and ITR1."SrcObjTyp"=OINV."ObjType"
|
||||||
|
group by OINV."DocEntry"
|
||||||
|
)
|
||||||
|
select CURRENT_OBJECT_SCHEMA() as "Company", T0."DocEntry", T0."DocNum", T0.U_EON_SRC_GUID as "CRM_GUID",
|
||||||
|
case
|
||||||
|
when T0."DocStatus"='C' then 'INVOICE_PAID'
|
||||||
|
else 'INVOICE_SENT'
|
||||||
|
end "INVOICE_STATUS",
|
||||||
|
T2."ArchiveLink" as "ARCHIVE_LINK", T3."EmailSentAt" as "EMAIL_SENT_AT", T5."ReconciliationDate" as "PAID_AT"
|
||||||
|
from OINV T0
|
||||||
|
left join Spool T1 on T1.U_UO_DOCENTRY=TO_NVARCHAR(T0."DocEntry") and T1.U_UO_OBJTYPE=T0."ObjType"
|
||||||
|
left join ArchiveTask T2 on T1."DocEntry"= T2."DocEntry"
|
||||||
|
left join MailTask T3 on T1."DocEntry"= T3."DocEntry"
|
||||||
|
left join LastReconciliation T5 on T0."DocEntry"=T5."DocEntry"
|
||||||
|
where T0."CANCELED"='N' and T0.U_EON_SRC_SYSTEM='crm' and T0.U_EON_SRC_GUID is not null
|
||||||
|
order by T0."DocEntry" desc
|
||||||
|
|||||||
@@ -1 +1,63 @@
|
|||||||
-- Data maintenance for CRM invoice processing
|
-- insert new entries
|
||||||
|
insert into "INTEGRATION_DATA"."CRM_INV_PROC" (
|
||||||
|
"Company",
|
||||||
|
"DocEntry",
|
||||||
|
"DocNum",
|
||||||
|
"CRM_GUID",
|
||||||
|
"INVOICE_STATUS",
|
||||||
|
"ARCHIVE_LINK",
|
||||||
|
"EMAIL_SENT_AT",
|
||||||
|
"PAID_AT"
|
||||||
|
)
|
||||||
|
select
|
||||||
|
"Company",
|
||||||
|
"DocEntry",
|
||||||
|
"DocNum",
|
||||||
|
"CRM_GUID",
|
||||||
|
"INVOICE_STATUS",
|
||||||
|
"ARCHIVE_LINK",
|
||||||
|
"EMAIL_SENT_AT",
|
||||||
|
"PAID_AT"
|
||||||
|
from "A19753_EON_P01"."UO_CRM_INV_PROC"
|
||||||
|
where not exists (
|
||||||
|
select 1 from "INTEGRATION_DATA"."CRM_INV_PROC" T0
|
||||||
|
where T0."Company"="A19753_EON_P01"."UO_CRM_INV_PROC"."Company" and T0."DocEntry"="A19753_EON_P01"."UO_CRM_INV_PROC"."DocEntry"
|
||||||
|
);
|
||||||
|
|
||||||
|
-- update existing entries
|
||||||
|
update T0
|
||||||
|
set STATUS='N',
|
||||||
|
UPDATE_TS=current_timestamp,
|
||||||
|
CRM_GUID=T1.CRM_GUID,
|
||||||
|
INVOICE_STATUS=T1.INVOICE_STATUS,
|
||||||
|
ARCHIVE_LINK=T1.ARCHIVE_LINK,
|
||||||
|
EMAIL_SENT_AT=T1.EMAIL_SENT_AT,
|
||||||
|
PAID_AT=T1.PAID_AT
|
||||||
|
from "INTEGRATION_DATA"."CRM_INV_PROC" T0
|
||||||
|
inner join (select
|
||||||
|
"Company",
|
||||||
|
"DocEntry",
|
||||||
|
"DocNum",
|
||||||
|
"CRM_GUID",
|
||||||
|
"INVOICE_STATUS",
|
||||||
|
"ARCHIVE_LINK",
|
||||||
|
"EMAIL_SENT_AT",
|
||||||
|
"PAID_AT"
|
||||||
|
from "A19753_EON_P01"."UO_CRM_INV_PROC"
|
||||||
|
except
|
||||||
|
select
|
||||||
|
"Company",
|
||||||
|
"DocEntry",
|
||||||
|
"DocNum",
|
||||||
|
"CRM_GUID",
|
||||||
|
"INVOICE_STATUS",
|
||||||
|
"ARCHIVE_LINK",
|
||||||
|
"EMAIL_SENT_AT",
|
||||||
|
"PAID_AT"
|
||||||
|
from "INTEGRATION_DATA"."CRM_INV_PROC") T1 on T0."Company"=T1."Company" and T0."DocEntry"=T1."DocEntry";
|
||||||
|
|
||||||
|
-- resend failed entries
|
||||||
|
update "INTEGRATION_DATA"."CRM_INV_PROC"
|
||||||
|
set STATUS='N',
|
||||||
|
UPDATE_TS=current_timestamp
|
||||||
|
where STATUS='E';
|
||||||
|
|||||||
@@ -1 +1,496 @@
|
|||||||
// NAVLib - Navigation library for invoice status
|
//var logginglib = $.import("uniorg.libs", "uologging");
|
||||||
|
var destLib = $.import("uniorg.destinations", "uodestlib");
|
||||||
|
var datetimeLib = $.import("uniorg.libs", "datetime");
|
||||||
|
var httpUtils = $.import("uniorg.libs", "httpUtils");
|
||||||
|
|
||||||
|
var libName = "uniorg.eon.invoice.status.v1.NAVLib.xsjslib";
|
||||||
|
var libVersion = '1.4';
|
||||||
|
|
||||||
|
var debugInfo = {};
|
||||||
|
var exContext = {};
|
||||||
|
var throwException = false;
|
||||||
|
var logLevel = "error";
|
||||||
|
|
||||||
|
function getCompanyData(id) {
|
||||||
|
var conn = $.hdb.getConnection();
|
||||||
|
var query = "SELECT \"SYS_ID\", \"SYS_UUID\", \"SYS_NAME\" FROM \"INTEGRATION_DATA\".\"UO_USER_PAR\" WHERE \"ID\"=?";
|
||||||
|
var rs = conn.executeQuery(query, id);
|
||||||
|
var companyData = rs[0];
|
||||||
|
conn.close();
|
||||||
|
return companyData;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getToken() {
|
||||||
|
var retObj = {};
|
||||||
|
// retObj.debug = {};
|
||||||
|
var resultStatus = "failure";
|
||||||
|
var sessionStore = new $.security.Store("session.xssecurestore");
|
||||||
|
var accessToken = "";
|
||||||
|
try {
|
||||||
|
var storedSession = JSON.parse(sessionStore.read({
|
||||||
|
name: "session"
|
||||||
|
}));
|
||||||
|
// retObj.debug.storedSession = storedSession;
|
||||||
|
var currentTS = Math.floor(Date.now() / 1000);
|
||||||
|
//retObj.debug.currentTS = currentTS;
|
||||||
|
// get new token if expiring in less than 5 minutes
|
||||||
|
if (currentTS < storedSession.expireTS - 300) {
|
||||||
|
accessToken = storedSession.access_token;
|
||||||
|
retObj.data = storedSession;
|
||||||
|
resultStatus = "success";
|
||||||
|
retObj.providedBy = "sessionStore";
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
$.trace.error(e.message);
|
||||||
|
}
|
||||||
|
if (accessToken === "") {
|
||||||
|
try {
|
||||||
|
var dest = destLib.getDestination("MicrosoftOAuth");
|
||||||
|
var client = new $.net.http.Client();
|
||||||
|
var authData = destLib.getEONCRMApiLogin();
|
||||||
|
var req = new $.web.WebRequest($.net.http.POST, "/" + authData.tenantId + "/oauth2/v2.0/token");
|
||||||
|
//retObj.authData = authData;
|
||||||
|
//req.headers.set("Content-Type", "application/json");
|
||||||
|
//req.setBody(JSON.stringify(authData));
|
||||||
|
req.parameters.set("client_id", authData.clientId);
|
||||||
|
req.parameters.set("scope", authData.scope);
|
||||||
|
req.parameters.set("grant_type", authData.grantType);
|
||||||
|
req.parameters.set("client_secret", authData.clientSecret);
|
||||||
|
client.request(req, dest);
|
||||||
|
var resp = client.getResponse();
|
||||||
|
//retObj.bodyString = resp.body.asString();
|
||||||
|
var body = JSON.parse(resp.body.asString());
|
||||||
|
retObj.data = body;
|
||||||
|
if (body.access_token) {
|
||||||
|
resultStatus = "success";
|
||||||
|
retObj.providedBy = "API";
|
||||||
|
// Workaround: store with own expire timestamp
|
||||||
|
var sessionData = {
|
||||||
|
access_token: body.access_token,
|
||||||
|
expireTS: currentTS + body.expires_in //3600
|
||||||
|
};
|
||||||
|
sessionStore.store({
|
||||||
|
name: "session",
|
||||||
|
value: JSON.stringify(sessionData)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
client.close();
|
||||||
|
} catch (e) {
|
||||||
|
retObj.error = "API call failed";
|
||||||
|
retObj.exception = e.message;
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
retObj.status = resultStatus;
|
||||||
|
return retObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*function logout(token) {
|
||||||
|
var retObj = {};
|
||||||
|
var resultStatus = "failure";
|
||||||
|
try {
|
||||||
|
var dest = destLib.getDestination("EONCRM");
|
||||||
|
var client = new $.net.http.Client();
|
||||||
|
var req = new $.web.WebRequest($.net.http.POST, "logout");
|
||||||
|
req.headers.set("Authorization", "Bearer " + token);
|
||||||
|
req.headers.set("Content-Type", "application/json");
|
||||||
|
client.request(req, dest);
|
||||||
|
var resp = client.getResponse();
|
||||||
|
//retObj.bodyString = resp.body.asString();
|
||||||
|
if (resp.status >= 200 && resp.status < 300) {
|
||||||
|
resultStatus = "success";
|
||||||
|
}
|
||||||
|
client.close();
|
||||||
|
} catch (e) {
|
||||||
|
retObj.error = "API call logout failed";
|
||||||
|
retObj.exception = e.message;
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
retObj.status = resultStatus;
|
||||||
|
return retObj;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
function mockTest(debugLevel) {
|
||||||
|
var retObj = {};
|
||||||
|
var resultStatus = "failure";
|
||||||
|
try {
|
||||||
|
if (debugLevel > 0) {
|
||||||
|
retObj.debugInfos = {};
|
||||||
|
}
|
||||||
|
var authResp = getToken();
|
||||||
|
if (debugLevel > 1) {
|
||||||
|
retObj.debugInfos.authResponse = authResp;
|
||||||
|
}
|
||||||
|
//var dest = destLib.getDestination("EONCRM");
|
||||||
|
var dest = destLib.getDestination("EONDriveAPI");
|
||||||
|
var client = new $.net.http.Client();
|
||||||
|
//var req = new $.web.WebRequest($.net.http.PATCH, "/invoices/v1/0eff936c-cdeb-ec11-bb3d-000d3a672caf");
|
||||||
|
var req = new $.web.WebRequest($.net.http.PATCH, "/external-service/v1/de/invoices/v1/0eff936c-cdeb-ec11-bb3d-000d3a672caf");
|
||||||
|
req.setBody(JSON.stringify({
|
||||||
|
"archiveLink": "DMS_IY_Retail$NOTSET$5089039$2$NOTSET",
|
||||||
|
"invoiceEmailSentAt": "2022-06-14T06:28:37.427Z",
|
||||||
|
"invoicePaidAt": "2022-06-20T06:28:37.427Z",
|
||||||
|
"receiptNumberSap": "440110203",
|
||||||
|
"status": "INVOICE_SENT"
|
||||||
|
}));
|
||||||
|
req.headers.set("Content-Type", "application/json");
|
||||||
|
req.headers.set("Authorization", "Bearer " + authResp.data.access_token);
|
||||||
|
//req.setBody(JSON.stringify(payload));
|
||||||
|
client.request(req, dest);
|
||||||
|
var response = client.getResponse();
|
||||||
|
var body;
|
||||||
|
retObj.httpStatus = response.status;
|
||||||
|
retObj.httpStatusName = httpUtils.getHttpStatusName(response.status);
|
||||||
|
var he = [];
|
||||||
|
for (var c in response.headers) {
|
||||||
|
he.push(response.headers[c]);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
body = response.body ? response.body.asString() : {};
|
||||||
|
|
||||||
|
if (debugLevel > 0) {
|
||||||
|
retObj.debugInfos.responseBody = body;
|
||||||
|
retObj.debugInfos.responseHeaders = he;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
retObj.response = JSON.parse(body);
|
||||||
|
} catch (e) {
|
||||||
|
retObj.response = {
|
||||||
|
body: body
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (response.status >= 200 && response.status < 300) {
|
||||||
|
if (Array.isArray(retObj.response)) {
|
||||||
|
resultStatus = "success";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
retObj.error = "API answered with http status " + response.status + " - " + httpUtils.getHttpStatusName(response.status);
|
||||||
|
if (body && body.charAt) {
|
||||||
|
retObj.error = retObj.error + " / Response Body: " + body;
|
||||||
|
}
|
||||||
|
if (retObj.response.replyText) {
|
||||||
|
retObj.error = retObj.error + " / API Response Code " + retObj.response.replyCode.toString() + ": " + retObj.response.replyText;
|
||||||
|
}
|
||||||
|
$.trace.error(retObj.error);
|
||||||
|
if (throwException) {
|
||||||
|
throw new Error(retObj.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
retObj.error = "failed parsing API response / Exception: " + e.message;
|
||||||
|
retObj.exception = e.message;
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.close();
|
||||||
|
} catch (e) {
|
||||||
|
retObj.error = "API call failed / Exception: " + e.message;
|
||||||
|
retObj.exception = e.message;
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*if (retObj.response && retObj.response.Response && retObj.response.Response.status === "failure") {
|
||||||
|
retObj.error = "(" + retObj.response.Response.recordNumber + ") " + retObj.response.Response.statusMessage;
|
||||||
|
$.trace.error(retObj.error);
|
||||||
|
if (throwException) {
|
||||||
|
throw new Error(retObj.error);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
retObj.status = resultStatus;
|
||||||
|
return retObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadData(crmGuid, payload, debugLevel) {
|
||||||
|
var retObj = {};
|
||||||
|
var resultStatus = "failure";
|
||||||
|
try {
|
||||||
|
if (debugLevel > 0) {
|
||||||
|
retObj.debugInfos = {};
|
||||||
|
}
|
||||||
|
var authResp = getToken();
|
||||||
|
if (debugLevel > 1) {
|
||||||
|
retObj.debugInfos.authResponse = authResp;
|
||||||
|
}
|
||||||
|
//var dest = destLib.getDestination("EONCRM");
|
||||||
|
var dest = destLib.getDestination("EONDriveAPI");
|
||||||
|
var client = new $.net.http.Client();
|
||||||
|
//var req = new $.web.WebRequest($.net.http.PATCH, "/invoices/v1/" + crmGuid);
|
||||||
|
var req = new $.web.WebRequest($.net.http.PATCH, "/external-service/v1/de/invoices/v1/" + crmGuid);
|
||||||
|
req.setBody(JSON.stringify(payload));
|
||||||
|
req.headers.set("Content-Type", "application/json");
|
||||||
|
req.headers.set("Authorization", "Bearer " + authResp.data.access_token);
|
||||||
|
//req.setBody(JSON.stringify(payload));
|
||||||
|
client.request(req, dest);
|
||||||
|
var response = client.getResponse();
|
||||||
|
var body;
|
||||||
|
retObj.httpStatus = response.status;
|
||||||
|
retObj.httpStatusName = httpUtils.getHttpStatusName(response.status);
|
||||||
|
var he = [];
|
||||||
|
for (var c in response.headers) {
|
||||||
|
he.push(response.headers[c]);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
body = response.body ? response.body.asString() : {};
|
||||||
|
|
||||||
|
if (debugLevel > 0) {
|
||||||
|
retObj.debugInfos.responseBody = body;
|
||||||
|
retObj.debugInfos.responseHeaders = he;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
retObj.response = JSON.parse(body);
|
||||||
|
} catch (e) {
|
||||||
|
retObj.response = {
|
||||||
|
body: body
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (response.status >= 200 && response.status < 300) {
|
||||||
|
//if (Array.isArray(retObj.response)) {
|
||||||
|
resultStatus = "success";
|
||||||
|
//}
|
||||||
|
} else {
|
||||||
|
if (response.status === 401) {
|
||||||
|
// if unauthorized, delete old session token
|
||||||
|
try {
|
||||||
|
var sessionStore = new $.security.Store("session.xssecurestore");
|
||||||
|
sessionStore.remove({
|
||||||
|
name: "session"
|
||||||
|
});
|
||||||
|
} catch (ex) {
|
||||||
|
$.trace.error(ex.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
retObj.error = "API answered with http status " + response.status + " - " + httpUtils.getHttpStatusName(response.status);
|
||||||
|
if (body && body.charAt) {
|
||||||
|
retObj.error = retObj.error + " / Response Body: " + body;
|
||||||
|
}
|
||||||
|
if (retObj.response.replyText) {
|
||||||
|
retObj.error = retObj.error + " / API Response Code " + retObj.response.replyCode.toString() + ": " + retObj.response.replyText;
|
||||||
|
}
|
||||||
|
$.trace.error(retObj.error);
|
||||||
|
if (throwException) {
|
||||||
|
throw new Error(retObj.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
retObj.error = "failed parsing API response / Exception: " + e.message;
|
||||||
|
retObj.exception = e.message;
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.close();
|
||||||
|
} catch (e) {
|
||||||
|
retObj.error = "API call failed / Exception: " + e.message;
|
||||||
|
retObj.exception = e.message;
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*if (retObj.response && retObj.response.Response && retObj.response.Response.status === "failure") {
|
||||||
|
retObj.error = "(" + retObj.response.Response.recordNumber + ") " + retObj.response.Response.statusMessage;
|
||||||
|
$.trace.error(retObj.error);
|
||||||
|
if (throwException) {
|
||||||
|
throw new Error(retObj.error);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
retObj.status = resultStatus;
|
||||||
|
return retObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
function procDoc(docData, debugLevel) {
|
||||||
|
var company = docData.Company;
|
||||||
|
var docEntry = docData.DocEntry;
|
||||||
|
var result = {
|
||||||
|
Company: company,
|
||||||
|
DocEntry: docEntry
|
||||||
|
};
|
||||||
|
var conn = $.hdb.getConnection();
|
||||||
|
exContext = {
|
||||||
|
company: company,
|
||||||
|
docEntry: docEntry
|
||||||
|
};
|
||||||
|
var payload = {
|
||||||
|
archiveLink: docData.ARCHIVE_LINK,
|
||||||
|
invoiceEmailSentAt: docData.EMAIL_SENT_AT === null ? null : datetimeLib.convertToISODate(docData.EMAIL_SENT_AT) + 'T00:00:00Z',
|
||||||
|
invoicePaidAt: docData.PAID_AT === null ? null : datetimeLib.convertToISODate(docData.PAID_AT) + 'T00:00:00Z',
|
||||||
|
receiptNumberSap: docData.DocNum,
|
||||||
|
status: docData.INVOICE_STATUS
|
||||||
|
};
|
||||||
|
exContext.action = 'uploadData';
|
||||||
|
var uploadResult = uploadData(docData.CRM_GUID, payload, debugLevel);
|
||||||
|
result.status = uploadResult.status;
|
||||||
|
if (debugLevel > 1 || uploadResult.status !== "success") {
|
||||||
|
result.docData = docData;
|
||||||
|
result.payload = payload;
|
||||||
|
result.uploadResult = uploadResult;
|
||||||
|
}
|
||||||
|
if (uploadResult.status !== "success") {
|
||||||
|
result.error = uploadResult.error;
|
||||||
|
exContext.uploadResult = uploadResult;
|
||||||
|
}
|
||||||
|
exContext.action = 'ProcTable';
|
||||||
|
if (result.status === "success") {
|
||||||
|
conn.executeUpdate(
|
||||||
|
"UPDATE \"INTEGRATION_DATA\".\"CRM_INV_PROC\" SET \"STATUS\"='S', \"UPDATE_TS\"=CURRENT_TIMESTAMP, \"LAST_ERROR\"='' \
|
||||||
|
WHERE \"Company\"=? AND \"DocEntry\"=?",
|
||||||
|
company, docEntry);
|
||||||
|
conn.commit();
|
||||||
|
} else {
|
||||||
|
conn.executeUpdate(
|
||||||
|
"UPDATE \"INTEGRATION_DATA\".\"CRM_INV_PROC\" SET \"STATUS\"='E', \"UPDATE_TS\"=CURRENT_TIMESTAMP, \"LAST_ERROR\"=? \
|
||||||
|
WHERE \"Company\"=? AND \"DocEntry\"=?",
|
||||||
|
result.error, company, docEntry);
|
||||||
|
conn.commit();
|
||||||
|
}
|
||||||
|
exContext = {};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function processDocs(parameter, triggerInfo) {
|
||||||
|
if (parameter === undefined || typeof parameter !== 'object') {
|
||||||
|
throw new Error("Parameter object invalid");
|
||||||
|
}
|
||||||
|
if (parameter.company === undefined) {
|
||||||
|
throw new Error("parameter.company invalid");
|
||||||
|
}
|
||||||
|
if ((parameter.batchsize === undefined || (!Number.isInteger(parameter.batchsize)) || parameter.batchsize <=
|
||||||
|
0)) {
|
||||||
|
throw new Error("parameter.batchsize invalid");
|
||||||
|
}
|
||||||
|
if (parameter.throwException === true) {
|
||||||
|
throwException = true;
|
||||||
|
}
|
||||||
|
if (parameter.logLevel) {
|
||||||
|
logLevel = parameter.logLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = {};
|
||||||
|
var resultStatus = "failure";
|
||||||
|
var startTS = new Date();
|
||||||
|
var procEntries = [];
|
||||||
|
var procResults = [];
|
||||||
|
var conn = $.hdb.getConnection();
|
||||||
|
|
||||||
|
try {
|
||||||
|
var companyCfg = destLib.getCompanyCfg(parameter.company, 'SYS_NAME');
|
||||||
|
if (parameter.debugLevel > 1) {
|
||||||
|
debugInfo.companyCfg = companyCfg;
|
||||||
|
}
|
||||||
|
if (!companyCfg.ID) {
|
||||||
|
throw new Error("invalid company");
|
||||||
|
}
|
||||||
|
|
||||||
|
var companyData = getCompanyData(companyCfg.ID);
|
||||||
|
debugInfo.companyData = companyData;
|
||||||
|
var companyCode = companyData.SYS_NAME;
|
||||||
|
debugInfo.company = companyCode;
|
||||||
|
|
||||||
|
var query =
|
||||||
|
"select top " + parameter.batchsize.toString() +
|
||||||
|
' "Company", "DocEntry", "DocNum", \
|
||||||
|
"CRM_GUID", "INVOICE_STATUS", "ARCHIVE_LINK", "EMAIL_SENT_AT", "PAID_AT" \
|
||||||
|
from "INTEGRATION_DATA"."CRM_INV_PROC" where "STATUS"=\'N\' order by "Company", "DocEntry"';
|
||||||
|
//debugInfo.query = query;
|
||||||
|
var rs = conn.executeQuery(query);
|
||||||
|
var iterator = rs.getIterator();
|
||||||
|
while (iterator.next()) {
|
||||||
|
procEntries.push(iterator.value());
|
||||||
|
}
|
||||||
|
//debugInfo.procEntries = procEntries;
|
||||||
|
if (procEntries.length > 0) {
|
||||||
|
conn.executeUpdate(
|
||||||
|
"UPDATE \"INTEGRATION_DATA\".\"CRM_INV_PROC\" SET \"STATUS\"='P', \"UPDATE_TS\"=CURRENT_TIMESTAMP WHERE \"Company\"=? AND \"DocEntry\"=?",
|
||||||
|
procEntries.map(
|
||||||
|
function(x) {
|
||||||
|
return [x.Company, x.DocEntry];
|
||||||
|
}));
|
||||||
|
conn.commit();
|
||||||
|
}
|
||||||
|
procEntries.forEach(function(x, idx) {
|
||||||
|
try {
|
||||||
|
procResults.push(procDoc(x, parameter.debugLevel));
|
||||||
|
} catch (procEx) {
|
||||||
|
conn.executeUpdate(
|
||||||
|
"UPDATE \"INTEGRATION_DATA\".\"CRM_INV_PROC\" SET \"STATUS\"='E', \"UPDATE_TS\"=CURRENT_TIMESTAMP, \"LAST_ERROR\"=? \
|
||||||
|
WHERE \"Company\"=? AND \"DocEntry\"=? AND \"STATUS\"='P'",
|
||||||
|
procEx.name + " Exception: " + procEx.message, x.Company, x.DocEntry);
|
||||||
|
conn.commit();
|
||||||
|
if (throwException) {
|
||||||
|
if (procEntries.length > idx + 1) {
|
||||||
|
// reset status for unprocessed entries before exiting the forEach loop with exception
|
||||||
|
try {
|
||||||
|
conn.executeUpdate(
|
||||||
|
"UPDATE \"INTEGRATION_DATA\".\"CRM_INV_PROC\" SET \"STATUS\"='N', \"UPDATE_TS\"=CURRENT_TIMESTAMP \
|
||||||
|
WHERE \"Company\"=? AND \"DocEntry\"=? AND \"STATUS\"='P'",
|
||||||
|
procEntries.slice(idx + 1).map(
|
||||||
|
function(y) {
|
||||||
|
return [y.Company, y.DocEntry];
|
||||||
|
}));
|
||||||
|
conn.commit();
|
||||||
|
} catch (ex) {
|
||||||
|
$.trace.error(ex.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw (procEx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
result.procResults = procResults;
|
||||||
|
if (procResults.filter(function(x) {
|
||||||
|
return x.status === 'success';
|
||||||
|
}).length === procEntries.length) {
|
||||||
|
resultStatus = "success";
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
result.exception = e.name + " Exception: " + e.message;
|
||||||
|
if (parameter.debugLevel > 0) {
|
||||||
|
result.exception += "\nDetails:\nFilename: " + e.fileName + "\nLine " + e.lineNumber + ", Column " + e.columnNumber + "\nStack Trace:\n" +
|
||||||
|
e.stack;
|
||||||
|
}
|
||||||
|
if (procEntries.length > 0) {
|
||||||
|
try {
|
||||||
|
conn.executeUpdate(
|
||||||
|
"UPDATE \"INTEGRATION_DATA\".\"CRM_INV_PROC\" SET \"STATUS\"='E', \"UPDATE_TS\"=CURRENT_TIMESTAMP, \"LAST_ERROR\"=? \
|
||||||
|
WHERE \"Company\"=? AND \"DocEntry\"=? AND \"STATUS\"='P'",
|
||||||
|
procEntries.map(
|
||||||
|
function(x) {
|
||||||
|
return [result.exception, x.Company, x.DocEntry];
|
||||||
|
}));
|
||||||
|
conn.commit();
|
||||||
|
} catch (ex) {
|
||||||
|
$.trace.error(ex.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$.trace.error(e.message);
|
||||||
|
if (throwException) {
|
||||||
|
result.durationMilliseconds = new Date() - startTS;
|
||||||
|
if (parameter.debugLevel > 0) {
|
||||||
|
result.debugInfo = debugInfo;
|
||||||
|
}
|
||||||
|
throw (e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*if (oAuthData.refresh_token) {
|
||||||
|
debugInfo.logout = logout(oAuthData.refresh_token);
|
||||||
|
}*/
|
||||||
|
if (parameter.debugLevel > 0) {
|
||||||
|
result.debugInfo = debugInfo;
|
||||||
|
}
|
||||||
|
result.durationMilliseconds = new Date() - startTS;
|
||||||
|
result.status = resultStatus;
|
||||||
|
|
||||||
|
conn.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -1 +1,84 @@
|
|||||||
// Connection test for invoice status service
|
var destLib = $.import("uniorg.destinations", "uodestlib");
|
||||||
|
var NAVLib = $.import("uniorg.eon.crm.invoice.status.v1", "NAVLib");
|
||||||
|
var httpUtils = $.import("uniorg.libs", "httpUtils");
|
||||||
|
|
||||||
|
var output = {};
|
||||||
|
var outMessages = [];
|
||||||
|
var outMsg = {};
|
||||||
|
var outStatus = "success";
|
||||||
|
var myDebug = {};
|
||||||
|
|
||||||
|
var debugLevel = 1;
|
||||||
|
var throwException = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
var response = {};
|
||||||
|
|
||||||
|
// remove stored session
|
||||||
|
/*try {
|
||||||
|
var sessionStore = new $.security.Store("session.xssecurestore");
|
||||||
|
sessionStore.remove({
|
||||||
|
name: "session"
|
||||||
|
});
|
||||||
|
} catch (ex) {
|
||||||
|
$.trace.error(ex.message);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// get token
|
||||||
|
response.token = NAVLib.getToken();
|
||||||
|
|
||||||
|
//response.mockTest = NAVLib.mockTest(debugLevel);
|
||||||
|
|
||||||
|
/*response.processDocs = NAVLib.processDocs({
|
||||||
|
batchsize: 1,
|
||||||
|
company: "A19753_EON_P01",
|
||||||
|
debugLevel: debugLevel,
|
||||||
|
throwException: throwException
|
||||||
|
}, "test");*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
response.companyCfg = destLib.getCompanyCfg('A19753_EON_P01', 'SYS_NAME');
|
||||||
|
response.company = NAVLib.getCompanyData(response.companyCfg.ID);
|
||||||
|
*/
|
||||||
|
|
||||||
|
$.response.contentType = "application/json";
|
||||||
|
$.response.setBody(JSON.stringify(response));
|
||||||
|
$.response.status = $.net.http.OK;
|
||||||
|
} catch (ex) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
} catch (e) {}
|
||||||
|
myDebug.response = response;
|
||||||
|
exceptionHandler(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
function exceptionHandler(ex) {
|
||||||
|
outMsg = {};
|
||||||
|
outMsg.method = "exceptionHandler";
|
||||||
|
outMsg.status = "error";
|
||||||
|
outMsg.message = ex.toString();
|
||||||
|
if (debugLevel > 0) {
|
||||||
|
outMsg.exceptionContext = NAVLib.exContext;
|
||||||
|
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;
|
||||||
|
output.messages = outMessages;
|
||||||
|
if (debugLevel > 0) {
|
||||||
|
output.debugInfos = myDebug;
|
||||||
|
}
|
||||||
|
|
||||||
|
var errbody = JSON.stringify(output);
|
||||||
|
$.response.contentType = 'application/json';
|
||||||
|
$.response.setBody(errbody);
|
||||||
|
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"description": "CRM Invoice Processing Tables Delta Load",
|
"description": "CRM Invoice Processing Tables Delta Load",
|
||||||
"action": "uniorg.eon.crm.invoice.status.v1:processJob.xsjs::deltaLoad",
|
"action": "uniorg.eon.crm.invoice.status.v1:processJob.xsjs::deltaLoad",
|
||||||
"schedules": []
|
"schedules": [
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,74 @@
|
|||||||
// Process job for invoice status updates
|
var logginglib = $.import("uniorg.libs", "uologging");
|
||||||
|
var NAVLib = $.import("uniorg.eon.crm.invoice.status.v1", "NAVLib");
|
||||||
|
|
||||||
|
var serviceName = "uniorg.eon.crm.invoice.status.v1.processJob.xsjs";
|
||||||
|
var serviceVersion = '1.0';
|
||||||
|
|
||||||
|
function executeJob(params) {
|
||||||
|
var result = {};
|
||||||
|
NAVLib.throwException = false;
|
||||||
|
result = NAVLib.processDocs(params, "scheduled job");
|
||||||
|
}
|
||||||
|
|
||||||
|
function deltaLoad() {
|
||||||
|
var conn = $.hdb.getConnection();
|
||||||
|
//insert new entries
|
||||||
|
conn.executeUpdate('insert into "INTEGRATION_DATA"."CRM_INV_PROC" ( \
|
||||||
|
"Company", \
|
||||||
|
"DocEntry", \
|
||||||
|
"DocNum", \
|
||||||
|
"CRM_GUID", \
|
||||||
|
"INVOICE_STATUS", \
|
||||||
|
"ARCHIVE_LINK", \
|
||||||
|
"EMAIL_SENT_AT", \
|
||||||
|
"PAID_AT" \
|
||||||
|
) \
|
||||||
|
select \
|
||||||
|
"Company", \
|
||||||
|
"DocEntry", \
|
||||||
|
"DocNum", \
|
||||||
|
"CRM_GUID", \
|
||||||
|
"INVOICE_STATUS", \
|
||||||
|
"ARCHIVE_LINK", \
|
||||||
|
"EMAIL_SENT_AT", \
|
||||||
|
"PAID_AT" \
|
||||||
|
from "A19753_EON_P01"."UO_CRM_INV_PROC" \
|
||||||
|
where not exists ( \
|
||||||
|
select 1 from "INTEGRATION_DATA"."CRM_INV_PROC" T0 \
|
||||||
|
where T0."Company"="A19753_EON_P01"."UO_CRM_INV_PROC"."Company" and T0."DocEntry"="A19753_EON_P01"."UO_CRM_INV_PROC"."DocEntry" \
|
||||||
|
)');
|
||||||
|
conn.commit();
|
||||||
|
//update existing entries
|
||||||
|
conn.executeUpdate('update T0 \
|
||||||
|
set STATUS=\'N\', \
|
||||||
|
UPDATE_TS=current_timestamp, \
|
||||||
|
CRM_GUID=T1.CRM_GUID, \
|
||||||
|
INVOICE_STATUS=T1.INVOICE_STATUS, \
|
||||||
|
ARCHIVE_LINK=T1.ARCHIVE_LINK, \
|
||||||
|
EMAIL_SENT_AT=T1.EMAIL_SENT_AT, \
|
||||||
|
PAID_AT=T1.PAID_AT \
|
||||||
|
from "INTEGRATION_DATA"."CRM_INV_PROC" T0 \
|
||||||
|
inner join (select \
|
||||||
|
"Company", \
|
||||||
|
"DocEntry", \
|
||||||
|
"DocNum", \
|
||||||
|
"CRM_GUID", \
|
||||||
|
"INVOICE_STATUS", \
|
||||||
|
"ARCHIVE_LINK", \
|
||||||
|
"EMAIL_SENT_AT", \
|
||||||
|
"PAID_AT" \
|
||||||
|
from "A19753_EON_P01"."UO_CRM_INV_PROC" \
|
||||||
|
except \
|
||||||
|
select \
|
||||||
|
"Company", \
|
||||||
|
"DocEntry", \
|
||||||
|
"DocNum", \
|
||||||
|
"CRM_GUID", \
|
||||||
|
"INVOICE_STATUS", \
|
||||||
|
"ARCHIVE_LINK", \
|
||||||
|
"EMAIL_SENT_AT", \
|
||||||
|
"PAID_AT" \
|
||||||
|
from "INTEGRATION_DATA"."CRM_INV_PROC") T1 on T0."Company"=T1."Company" and T0."DocEntry"=T1."DocEntry"');
|
||||||
|
conn.commit();
|
||||||
|
conn.close();
|
||||||
|
}
|
||||||
@@ -1 +1 @@
|
|||||||
{}
|
{}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"description": "CRM Invoice Update",
|
"description": "CRM Invoice Update",
|
||||||
"action": "uniorg.eon.crm.invoice.status.v1:processJob.xsjs::executeJob",
|
"action": "uniorg.eon.crm.invoice.status.v1:processJob.xsjs::executeJob",
|
||||||
"schedules": []
|
"schedules": [
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user