Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: support to sync pos return netsuite internal id in OMS. (175-sync-pos-return-from-ns-to-oms) #176

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions src/FileCabinet/SuiteScripts/POSReturn/HC_MR_ExportedPOSReturnCSV.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/file', 'N/record', 'N/search', 'N/sftp', 'N/format', 'N/error'],
(file, record, search, sftp, format, error) => {
function uplaodPOSReturnCSVFileOnSFTP (posReturnFileObj, fileName) {
//Get Custom Record Type SFTP details
var customRecordSFTPSearch = search.create({
type: 'customrecord_ns_sftp_configuration',
columns: [
'custrecord_ns_sftp_server',
'custrecord_ns_sftp_userid',
'custrecord_ns_sftp_port_no',
'custrecord_ns_sftp_host_key',
'custrecord_ns_sftp_guid',
'custrecord_ns_sftp_default_file_dir'
]

});
var sftpSearchResults = customRecordSFTPSearch.run().getRange({
start: 0,
end: 1
});

var sftpSearchResult = sftpSearchResults[0];

var sftpUrl = sftpSearchResult.getValue({
name: 'custrecord_ns_sftp_server'
});

var sftpUserName = sftpSearchResult.getValue({
name: 'custrecord_ns_sftp_userid'
});

var sftpPort = sftpSearchResult.getValue({
name: 'custrecord_ns_sftp_port_no'
});

var hostKey = sftpSearchResult.getValue({
name: 'custrecord_ns_sftp_host_key'
});

var sftpKeyId = sftpSearchResult.getValue({
name: 'custrecord_ns_sftp_guid'
});

var sftpDirectory = sftpSearchResult.getValue({
name: 'custrecord_ns_sftp_default_file_dir'
});

sftpDirectory = sftpDirectory + 'pos-return';
sftpPort = parseInt(sftpPort);

var connection = sftp.createConnection({
username: sftpUserName,
secret: sftpKeyId,
url: sftpUrl,
port: sftpPort,
directory: sftpDirectory,
hostKey: hostKey
});
log.debug("Connection established successfully with SFTP server!");

if (posReturnFileObj.size > connection.MAX_FILE_SIZE) {
throw error.create({
name:"FILE_IS_TOO_BIG",
message:"The file you are trying to upload is too big"
});
}
connection.upload({
directory: '/import/returnidentification/',
file: posReturnFileObj
});
log.debug("POS Return CSV File Uploaded Successfully to SFTP server with file" + fileName);

}

const getInputData = (inputContext) => {
var now = format.format({value : new Date(), type: format.Type.DATETIME});

var nowDateParts = now.split(' ');

var datePart = nowDateParts[0];
var timePart = nowDateParts[1];
var ampmPart = nowDateParts[2];

// Remove the seconds from the time part
var timeWithoutSeconds = timePart.split(':').slice(0, 2).join(':');

var dateStringWithoutSeconds = datePart + ' ' + timeWithoutSeconds + ' ' + ampmPart;

// get last pos return export runtime
var customRecordSearch = search.create({
type: 'customrecord_hc_last_runtime_export',
columns: ['custrecord_pos_return_ex_date']
});

var searchResults = customRecordSearch.run().getRange({
start: 0,
end: 1
});

var searchResult = searchResults[0];
var lastExportDate = searchResult.getValue({
name: 'custrecord_pos_return_ex_date'
});

var lastExportDateParts = lastExportDate.split(' ');
var lastExportDatePart = lastExportDateParts[0];
var lastExportTimePart = lastExportDateParts[1];
var ampmExportPart = lastExportDateParts[2];

// Remove the seconds from the time part
var lastExportTimeWithoutSeconds = lastExportTimePart.split(':').slice(0, 2).join(':');

var lastExportDateString = lastExportDatePart + ' ' + lastExportTimeWithoutSeconds + ' ' + ampmExportPart;

// Get pos return search query
var posReturnSearch = search.load({ id: 'customsearch_hc_exp_pos_return' });

// Copy the filters from posReturnSearch into defaultFilters.
var defaultFilters = posReturnSearch.filters;

// Push the customFilters into defaultFilters.

defaultFilters.push(search.createFilter({
name: "datecreated",
operator: search.Operator.WITHIN,
values: lastExportDateString, dateStringWithoutSeconds
}));
// Copy the modified defaultFilters
posReturnSearch.filters = defaultFilters;

return posReturnSearch;
}

const map = (mapContext) => {
var contextValues = JSON.parse(mapContext.value);

var hcReturnId = contextValues.values.custbody_hc_pos_return_id;
var internalId = contextValues.values.internalid.value;

var posReturnData = {
"returnId": hcReturnId,
'returnIdentificationTypeId': "NETSUITE_RTN_ID",
'idValue': internalId
};

mapContext.write({
key: contextValues.id,
value: posReturnData
});

}

const reduce = (reduceContext) => {
var contextValues = JSON.parse(reduceContext.values);
var soId = reduceContext.key;
var content = contextValues.returnId + ',' + contextValues.returnIdentificationTypeId + ',' + contextValues.idValue + '\n';

reduceContext.write(soId, content);
}

const summarize = (summaryContext) => {
try {
var fileLines = 'returnId,returnIdentificationTypeId,idValue\n';
var totalRecordsExported = 0;

summaryContext.output.iterator().each(function(key, value) {
fileLines += value;
totalRecordsExported = totalRecordsExported + 1;
return true;
});

log.debug("====totalRecordsExported=="+totalRecordsExported);

if (totalRecordsExported > 0) {
var fileName = summaryContext.dateCreated + '-POSReturnExport.csv';
var posReturnFileObj = file.create({
name: fileName,
fileType: file.Type.CSV,
contents: fileLines
});

// call function to upload csv file on SFTP server
uplaodPOSReturnCSVFileOnSFTP(posReturnFileObj, fileName);

var currentDate = format.format({value : summaryContext.dateCreated, type: format.Type.DATETIME});

//Get Custom Record Type internal id
var customRecordHCExSearch = search.create({
type: 'customrecord_hc_last_runtime_export',
columns: ['internalid']
});
var searchResults = customRecordHCExSearch.run().getRange({
start: 0,
end: 1
});

var searchResult = searchResults[0];
var lastRuntimeExportInternalId = searchResult.getValue({
name: 'internalid'
});

// save last pos return export date
record.submitFields({
type: 'customrecord_hc_last_runtime_export',
id: lastRuntimeExportInternalId,
values: {
custrecord_pos_return_ex_date : currentDate
}
});
}
} catch (e) {
log.error({
title: 'Error in exporting and uploading pos return csv files',
details: e,
});
throw error.create({
name:"Error in exporting and uploading pos return csv files",
message: e
});
}
}
return {getInputData, map, reduce, summarize}
});
10 changes: 10 additions & 0 deletions src/FileCabinet/SuiteScripts/SFTP/HC_SC_CreateSFTPDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,16 @@ define(['N/sftp', 'N/error', 'N/search'], function (sftp, error, search) {
connection.makeDirectory({
path: 'salesorder/import/oms-fulfillment-nifi/archive'
});

connection.makeDirectory({
path: 'pos-return/import'
});
connection.makeDirectory({
path: 'pos-return/import/returnidentification'
});
connection.makeDirectory({
path: 'pos-return/import/returnidentification/archive'
});

} catch (e) {
log.error({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,12 @@
<label>Customer Deposit Export Date</label>
<storevalue>T</storevalue>
</customrecordcustomfield>
<customrecordcustomfield scriptid="custrecord_pos_return_ex_date">
<accesslevel>2</accesslevel>
<displaytype>NORMAL</displaytype>
<fieldtype>DATETIMETZ</fieldtype>
<label>POS Return Export Date</label>
<storevalue>T</storevalue>
</customrecordcustomfield>
</customrecordcustomfields>
</customrecordtype>
29 changes: 29 additions & 0 deletions src/Objects/POSReturn/customscript_exp_pos_return.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<mapreducescript scriptid="customscript_exp_pos_return">
<description></description>
<isinactive>F</isinactive>
<name>HC_MR_ExportedPOSReturnCSV</name>
<notifyadmins>F</notifyadmins>
<notifyemails></notifyemails>
<notifyowner>T</notifyowner>
<scriptfile>[/SuiteScripts/SalesOrder/HC_MR_ExportedPOSReturnCSV.js]</scriptfile>
<scriptdeployments>
<scriptdeployment scriptid="customdeploy_exp_pos_return">
<buffersize>1</buffersize>
<concurrencylimit>1</concurrencylimit>
<isdeployed>T</isdeployed>
<loglevel>DEBUG</loglevel>
<queueallstagesatonce>T</queueallstagesatonce>
<status>SCHEDULED</status>
<title>HC_MR_ExportedPOSReturnCSV</title>
<yieldaftermins>60</yieldaftermins>
<recurrence>
<daily>
<everyxdays>1</everyxdays>
<repeat>PT15M</repeat>
<startdate>2023-08-22</startdate>
<starttime>05:00:00Z</starttime>
</daily>
</recurrence>
</scriptdeployment>
</scriptdeployments>
</mapreducescript>
4 changes: 4 additions & 0 deletions src/Objects/POSReturn/customsearch_hc_exp_pos_return.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- This XML must be system-generated. Do not edit it manually. !-->
<savedsearch scriptid="customsearch_hc_exp_pos_return">
<definition>6961fec0aba62754ee610912dffdfa8693faf3b1306af6f555017fe0b4cc2f43@[email protected]@H4sIAAAAAAAA/+1dbW+jOhb+fqX7H6L08zblPa3SXBFwGrYEIiAz0/2CMglzJ3fTpErozHR+/RpjmlfSYAzJIksjTQPm2D74Oefx8cFu/fXreVb7ESxX08X8vs5d39RrwXy8mEznf9/Xh173X836X+0//2jNV6vF+M4NltPRbPo7mNhf/wnGobaYh6PpPFjWoJj56g6Vuq9/D8OXu0bj58+f1/MgXL1Ow+B6vHhurMbfg+fRqoGK1Wuv08l9XbyRRFm58d0Od+eOfgQTNxgtx9/vxq+rcPG8Qj/872M/+PXivyxW/jIIX5fzO04RZF6SeOlGbkr1dQ9urrl6LZw+B6tw9PwCe7RVro378Tx6eYGN3nqqDVsYNXcWTP4OlrC9y+A6rh7+h7u9vI6fvI4b2Uc/Wo1NoUkVk+DbdD4Nofh2Ky6tb1yZBKvxcvoSLmD5ZQDrmnhvL0FtPButVvf1uLzzfr3edoHqaD0fWJ7z1Gqsn2i3foxmr8Gq3foU/V/7Ng1m6EYipJtcqMf37uuP4MkfqNqj+gDqtUbGB13NMQaeb+j1qK2/f9/XV+ESjhWovaPvq9VA1WSorWsAU/d10FWHpudbah+2dhK8BPMJHJ9vj8Hbfd1cjEezQH9X5R3Wkmd4Jri62WtiT6uBXy+LZVgb2G7NydWwpKqnAdirx1uO5qvROHrTBOJN9cEfDDumob0L/rpYzILRvN7+NpqtAkKZpuF6dCUObAcqmrLQvqr1DAvQFeqAvmHpwKEr1e3Zn6FarUfKanVAFzgO0It4Ze+yC3l5a/GFvMa1+K7t9IuS7QAXmhyXrvhPBvjsqy4UHim+GNkPjjroFSV6w+RTkTy0oJ49x9A8QE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D5XQ0m/4OJvbXf4JxqC3m4Wg6D5btP//4H9p1EPI99wAA</definition>
</savedsearch>
3 changes: 3 additions & 0 deletions src/deploy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

<path>~/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateCustomerDepositAndRefund.js</path>
<path>~/FileCabinet/SuiteScripts/SalesOrder/HC_MR_ExportedOMSFulfilledBackOrdCSV.js</path>
<path>~/FileCabinet/SuiteScripts/SalesOrder/HC_MR_ExportedPOSReturnCSV.js</path>
</files>
<objects>
<!--Custom Record-->
Expand Down Expand Up @@ -173,6 +174,7 @@
<path>~/Objects/SalesOrder/customscript_create_cd_and_cr.xml</path>
<path>~/Objects/SalesOrder/customscript_export_cust_deposit.xml</path>
<path>~/Objects/SalesOrder/customscript_exp_oms_fulfilled_so.xml</path>
<path>~/Objects/SalesOrder/customscript_exp_pos_return.xml</path>

<!--Saved Search-->
<path>~/Objects/CashSale/customsearch_hc_export_cashsales.xml</path>
Expand All @@ -195,6 +197,7 @@
<path>~/Objects/Product/customsearch_hc_exp_kit_component.xml</path>
<path>~/Objects/SalesOrder/customsearch_hc_exp_customer_deposit.xml</path>
<path>~/Objects/SalesOrder/customsearch_hc_exp_oms_shipped_orders.xml</path>
<path>~/Objects/SalesOrder/customsearch_hc_exp_pos_return.xml</path>
</objects>
<translationimports>
<path>~/Translations/*</path>
Expand Down