-
-
Save jjmartin/112daec3f928eb367253aaf896688dae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @NApiVersion 2.0 | |
* @NScriptType MapReduceScript | |
*/ | |
/************************************************************* | |
* File Header | |
* Script Name: render-pdf-invoices.js | |
* Description: Map Reduce Script for generating Invoices in PDF format, sending to S3 | |
*********************************************************** */ | |
define(['N/render', 'N/email', 'N/file', 'N/error', 'N/log', 'N/search', 'N/record', '/SuiteScripts/ns-aws-s3', 'N/runtime', 'N/encode'], | |
function(render, email, file, error, log, search, record, AWS, runtime, encode) { | |
var strKeyId = runtime.getCurrentScript().getParameter({ | |
name: 'custscript_accesskeyid_pdf' | |
}); | |
var strAccessKey = runtime.getCurrentScript().getParameter({ | |
name: 'custscript_secretaccesskey_pdf' | |
}); | |
function getInputData() { | |
// Stamp invoice as sent | |
// Should we send by whole month now? 2 scripts one for individual invoice? On demand. All invoices created within the month | |
// Resubmit to s3 checkbox that sends individual pdf to s3 | |
// we need to build a paging system to pull all new invoices so that the script scales | |
// Contract id is not on invoice | |
// Okay with naming convention? CustomerId_ContractId_InvoiceDate_invoiceid.pdf | |
// instead of AWS should we just email it? no | |
return search.create({ | |
type: record.Type.INVOICE, | |
filters: [ | |
['internalid', search.Operator.IS, 14373] | |
], | |
columns: ['entity'], | |
title: 'Open Invoice Search' | |
}); | |
} | |
function map(context) { | |
var searchResult = JSON.parse(context.value); | |
var invoiceId = searchResult.id; | |
var entityId = searchResult.values.entity.value; | |
pdfConvert(invoiceId, entityId); | |
} | |
function summarize(context) { | |
} | |
function pdfConvert(invoiceId, entityId) { | |
try { | |
log.debug({ | |
title: 'entityId', | |
details: entityId | |
}); | |
log.debug({ | |
title: 'invoiceId', | |
details: invoiceId | |
}); | |
// TODO: template param? | |
var xmlTemplateFile = file.load('Templates/PDF Templates/correctedXmlTemplateNow.xml'); | |
var renderer = render.create(); | |
renderer.templateContent = xmlTemplateFile.getContents(); | |
var invoice = record.load({ | |
type: record.Type.INVOICE, | |
id: invoiceId | |
}); | |
renderer.addRecord('record', invoice); | |
var customer = record.load({ | |
type: record.Type.CUSTOMER, | |
id: entityId | |
}); | |
var invoicePdf = renderer.renderAsPdf(); | |
invoicePdf.folder = -15; | |
invoicePdf.name = 'TEMP' + invoiceId + '.pdf'; | |
var fileId = invoicePdf.save(); | |
invoicePdf = file.load({ | |
id: fileId | |
}); | |
var base64Contents = invoicePdf.getContents(); | |
log.debug({ | |
title: 'base64Contents', | |
details: base64Contents | |
}); | |
var rawString = encode.convert({ | |
string: base64Contents, | |
inputEncoding: encode.Encoding.BASE_64, | |
outputEncoding: encode.Encoding.UTF_8 | |
}); | |
log.debug({ | |
title: 'rawString', | |
details: rawString | |
}); | |
var sfdcCustomerId = customer.getText({ | |
fieldId: 'custentity_cdn_sfdc_customer_id' | |
}); | |
var invNumber = invoice.getText({ | |
fieldId: 'tranid' | |
}); | |
var filename = sfdcCustomerId + '_' + invNumber + '.pdf'; | |
log.debug({ | |
title: 'fileName', | |
details: filename | |
}); | |
var options = { | |
region: 'us-east-2', | |
accessKeyId: strKeyId, | |
secretAccessKey: strAccessKey, | |
}; | |
var s3 = new AWS.S3(options); | |
var putParam = { | |
Bucket: 'cleardata-netsuite-dev', | |
ACL: 'bucket-owner-full-control', | |
ContentType: 'application/pdf', | |
Key: filename, | |
Body: rawString | |
}; | |
s3.putObject(putParam, function(err, data) { | |
if (err) { | |
log.error({ | |
title: 'Put Error', | |
details: { | |
err: err, | |
stack: err.stack | |
} | |
}); | |
} else { | |
log.debug({ | |
title: 'put data response', | |
details: data | |
}); | |
} | |
}); | |
} catch (err) { | |
log.debug({ | |
title: 'PDF Failed', | |
details: err | |
}); | |
throw err; | |
} | |
} | |
// Link each entry point to the appropriate function. | |
return { | |
getInputData: getInputData, | |
map: map, | |
summarize: summarize | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment