Hmmm...
But I run my script for every file which means It would handle the PDF and TXT file separately. So I will end up with 2 new entries in my database.
Code: Select all
function jobArrived( s : Switch, job : Job )
{
var pdf = File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
function getSuppliers(){
var theHTTP = new HTTP( HTTP.SSL );
theHTTP.authScheme = HTTP.ProprietaryAuth;
theHTTP.addHeader( "Authorization", "Bearer asdasdasdasd");
theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdadsasd/databases/asdasd/tables/ASDDDrecords?perPage=9999";
theHTTP.get();
while (!theHTTP.waitForFinished(3)) {
job.log(1, 'API CALL IN PROGRESS!');
}
if (theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200) {
job.log(1, 'getSuppliers - API CALL successfull RESPONSE: ' +theHTTP.statusCode ); // theHTTP.getServerResponse().toString('UTF-8')
job.log(1,"FULL RESPONSE GET SUPPLIERS: "+theHTTP.getServerResponse().toString('UTF-8'))
supplierJson = JSON.parse(theHTTP.getServerResponse().toString('UTF-8'));
// job.log( 1, JSON.stringify( jsonReturn[0]['fields']['Bestellungen'] ));
// job.log(1,jsonReturn[0]['fields']['Name oder Firma']);
} else {
job.log(
3,
'API CALL Failed: ' +
theHTTP.getServerResponse().toString('UTF-8') +
' Status Code: ' +
theHTTP.statusCode.toString('UTF-8')
);
}
return supplierJson
}
function getCompany( supplierJson, fileAsText ){
var newSupplierJson = [];
for ( var j=0; j< supplierJson.length; j++ ){
if ( "UST ID" in supplierJson[j]['fields'] ){
newSupplierJson.push(supplierJson[j]);
}
}
//File.write("C:\\Users\\Administrator.BECHTELDRUCK\\Nextcloud\\Documents\\coding\\SwitchScripts\\RECHNUNGSEINGANG\\RECHNUNGEN AUSLESEN TEST\\TXT LOWER ARCHIV\\"+job.getName(), pdfLower, 'UTF-8'); // AUCH ALS LOWER TEXT ABSPEICHERN!
// job.log(1,"länge json: "+supplierJson.length);
supplierId = 0;
for (var i=0; i< newSupplierJson.length; i++){
var supplierUSTID = newSupplierJson[i]['fields']['UST ID'];
var regex = "("+supplierUSTID+")";
var pattern = new RegExp( regex );
pattern.global = true;
pattern.ignoreCase = true;
//job.log(1,i);
var matched = pdf.match(pattern);
if (matched){
job.log(1,"Pattern found: "+supplierUSTID+" Name: "+ newSupplierJson[i]['fields']['Name oder Firma'] );
job.setPrivateData("pdSupplierUSTID", supplierUSTID );
job.log(1,"matches: "+matched );
job.log(1, JSON.stringify( newSupplierJson[i] ) );
supplierId = newSupplierJson[i]['id'];
}
}
job.sendToSingle( job.getPath() );
return supplierId
};
function upload2Ninox(supplierId){
name = job.getName();
prefix = job.getUniqueNamePrefix();
filename = name.replace(prefix,"");
var pdf = File.read( job.getPath(), "UTF-8" );// PDF Text einlesen
var encodedText = HTTP.encodeURI( pdf );
var nummer = "asd";
var date="2020-07-16";
job.log(1,date);
var doktyp = "Rechnung";
var theHTTP = new HTTP (HTTP.SSL);
theHTTP.authScheme = HTTP.ProprietaryAuth;
theHTTP.authorization = "Bearer asdasd";
theHTTP.url = "https://api.ninoxdb.de/v1/teams/asdasd/databases/gasasd/tables/ASD/records";
var json = new ByteArray(
'{"fields":{"Lieferanten":"' +
supplierId+
'","Nummer":"' +
nummer +
'","Datum":"' +
date +
'","Dokumenttyp":"' +
doktyp +
'","pdf als text":"' +
encodedText +
'"}}',
'UTF-8'
);
theHTTP.setPostData(json, 'application/json');
theHTTP.post();
while( !theHTTP.waitForFinished( 3 ) )
{
job.log( 1, "API CALL IN PROGRESS!" );
}
if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
{
job.log(1," upload2Ninox - API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
// UPLOAD VIA API TO NINOX DATABASE
try{
var response = JSON.parse(theHTTP.getServerResponse().toString( "UTF-8" ));
var ninoxId =response.id;
job.setPrivateData("pdNinoxId", ninoxId );
var theHTTP = new HTTP( HTTP.SSL );
theHTTP.authScheme = HTTP.ProprietaryAuth;
theHTTP.authorization = "Bearer asdasd";
theHTTP.setAttachedFile( job.getPath() , filename );
theHTTP.url = "https://api.ninoxdb.de/v1/teams/uasdasdQ/databases/gasdadv/tables/ASD/records/"+id+"/files";
theHTTP.enableMime = true;
theHTTP.post();
while( !theHTTP.waitForFinished( 3 ) )
{
job.log( 1, "API CALL IN PROGRESS!" );
}
if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
{
job.log(1,"API CALL successfull RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
}
else
{
job.log(3,"API CALL Failed: "+theHTTP.getServerResponse().toString( "UTF-8" )+ " Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
}
}
catch(theError) {
job.log(3,'json parse error: ' +theError);
}
}
else
{
job.log(3,"API CALL Failed: "+theHTTP.getServerResponse().toString( "UTF-8" )+ " Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
}
}
// Ausführung
getSuppliers();
getCompany(supplierJson,pdf);
upload2Ninox(supplierId);
}