|
/** |
|
* Scripts to automate printing of sales orders to warehouse |
|
* |
|
* Dependencies: |
|
* |
|
* |
|
* * Custom Fields |
|
* |
|
* |------------------------|---------------------------|-----------------|----------------| |
|
* | Custom Field Type | ID | Type | Description | |
|
* |------------------------|---------------------------|-----------------|----------------| |
|
* | Transaction Body Field | custbody_mos_printcount | Integer Number | Print Count | |
|
* |------------------------|---------------------------|-----------------|----------------| |
|
* |
|
* * Custom Record |
|
* |
|
* ID = customrecord_mos_printlog |
|
* |
|
* |--------------------------------------|---------------------------|--------------|--------------------------------------| |
|
* | ID | Type | Description | Notes | |
|
* |--------------------------------------|---------------------------|--------------|--------------------------------------| |
|
* | custrecord_mos_printlog_user | List/Record (Employee) | User | Dynamic Default - Current User | |
|
* | custrecord_mos_printlog_timedate | Date/Time | Date/Time | Dynamic Default - Current Date/Time | |
|
* | custrecord_mos_printlog_transaction | List/Record (Transaction) | Transaction | | |
|
* | custrecord_mos_printlog_file | Document | File | | |
|
* |--------------------------------------|---------------------------|--------------|--------------------------------------| |
|
*/ |
|
|
|
/** |
|
* Add 'Print to Warehouse' button to Sales Orders and Transfer Orders |
|
* |
|
* Script ID: customscript_mos_salesorderprint_ue |
|
* Script Type: User Event |
|
* Deployed to: Sales Order, Transfer Order |
|
* |
|
*/ |
|
|
|
function beforeload(type, form) { |
|
var status = nlapiGetFieldValue('status'); |
|
|
|
var isopen = ( |
|
status !== 'Pending Billing' && |
|
status !== 'Billed' && |
|
status !== 'Closed' && |
|
status !== 'Rejected' && |
|
status !== 'Pending Approval' && |
|
status !== 'Received' |
|
); |
|
|
|
if (type == 'view' && isopen) { |
|
form.setScript('customscript_mos_salesorderprint_client'); |
|
form.addButton('custpage_printshippinglabel', 'Print to Warehouse', 'printToWarehouse()'); |
|
} |
|
} |
|
|
|
/** |
|
* Client script to print Sales Order directly to warehouse printer |
|
* |
|
* Script ID: customscript_mos_salesorderprint_client |
|
* Script Type: Client Script |
|
* Deployed to: <not deployed> |
|
* |
|
*/ |
|
|
|
function checkForMOSLocation() { |
|
return nlapiSearchRecord('transaction', null, [['internalid', 'is', nlapiGetRecordId()], 'and', ['location', 'anyof', 1]]) !== null; |
|
} |
|
|
|
function printToWarehouse() { |
|
|
|
if(nlapiGetRecordType() == 'salesorder' && ! nlapiLookupField('salesorder', nlapiGetRecordId(), 'custbody_reviewed_by')) { |
|
Ext.MessageBox.show({ title: 'Order not reviewed yet!', msg: 'This Sales Order has not been reviewed yet, and cannot be printed.', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR }); |
|
return; |
|
} |
|
|
|
if(! checkForMOSLocation()) { |
|
Ext.MessageBox.show({ title: 'Order location not MOS!', msg: 'This Sales Order does not have stock location of MOS and cannot be printed to MOS Warehouse.', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR }); |
|
return; |
|
} |
|
|
|
Ext.MessageBox.show({title: 'Printing sales order', msg: 'Please wait..', wait: true, width: 250 }); |
|
var url = nlapiResolveURL('SUITELET', 'customscript_mos_salesorderprint_sl', 'customdeploy_mos_salesorderprint_sl'); |
|
nlapiRequestURL(url, {custom_id: nlapiGetRecordId(), custom_type: nlapiGetRecordType()}, null, function(response) { |
|
var result = response.getBody(); |
|
if(result === 'SUCCESS') { |
|
Ext.MessageBox.show({ title: 'Sales Order printed', msg: 'Sales Order printed to warehouse', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.INFO }); |
|
} else { |
|
Ext.MessageBox.show({ title: 'Error Printing!', msg: result, buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR }); |
|
} |
|
}); |
|
} |
|
|
|
/** |
|
* Suitelet called by Client script that actually does the printing |
|
* |
|
* Script ID: customscript_mos_salesorderprint_sl |
|
* Script Type: Suitelet |
|
* |
|
*/ |
|
|
|
// Returns the internal id of a folder to save the packing slip to based on the (UTC) date, creating it if it doesn't exist |
|
|
|
function getFolderID() { |
|
var PARENT_FOLDER_ID = 58130; |
|
|
|
var name = new Date().toISOString().substring(0, 10); |
|
|
|
var filters = [['parent', 'anyOf', PARENT_FOLDER_ID], 'and', ['name', 'is', name]]; |
|
var results = nlapiSearchRecord('folder', null, filters); |
|
|
|
if(results) { |
|
return results[0].getId(); |
|
} else { |
|
var record = nlapiCreateRecord('folder'); |
|
record.setFieldValue('name', name); |
|
record.setFieldValue('parent', PARENT_FOLDER_ID); |
|
return nlapiSubmitRecord(record); |
|
} |
|
} |
|
|
|
function generatePDF(id) { |
|
// retry generating pdf up to three times in case of timeouts |
|
var count = 0; |
|
var maxTries = 3; |
|
|
|
while(true) { |
|
try { |
|
var pdf = nlapiPrintRecord('PICKINGTICKET', id, 'PDF'); |
|
return pdf; |
|
} catch (err) { |
|
nlapiLogExecution('ERROR', 'Error printing', err.message); |
|
if (++count == maxTries) throw err; |
|
} |
|
} |
|
} |
|
|
|
function suitelet(request, response) { |
|
|
|
try { |
|
var id = request.getParameter('custom_id'); |
|
var type = request.getParameter('custom_type'); |
|
|
|
if(! id || ! type) { |
|
response.write('invalid parameters'); |
|
} else { |
|
var pdf = generatePDF(id); |
|
pdf.setName(id + '~' + new Date().toISOString() + '.pdf'); |
|
pdf.setFolder(getFolderID()); |
|
var fileid = nlapiSubmitFile(pdf); |
|
|
|
var record = nlapiCreateRecord('customrecord_mos_printlog'); |
|
record.setFieldValue('custrecord_mos_printlog_file', fileid); |
|
record.setFieldValue('custrecord_mos_printlog_transaction', id); |
|
nlapiSubmitRecord(record); |
|
|
|
var results = nlapiSearchRecord('customrecord_mos_printlog', null, ['custrecord_mos_printlog_transaction', 'anyOf', id]); |
|
var count = (results || []).length; |
|
nlapiSubmitField(type, id, 'custbody_mos_printcount', count); |
|
|
|
nlapiSendEmail(nlapiGetUser(), 'XXXXX', 'HP4350-20:' + id, id, null, null, null, pdf, true); |
|
response.write('SUCCESS'); |
|
} |
|
} catch(err) { |
|
response.write(err + ' (line number: ' + err.lineno + ')'); |
|
} |
|
} |