From 7caad48b08d95e01b88070e613998a7719413680 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Mon, 1 Apr 2024 14:22:50 +0530 Subject: [PATCH 1/8] Added: support to create return authorization and customer refund in NetSuite. --- project.json | 2 +- .../SalesOrder/HC_SC_CreateReturnRefund.js | 373 ++++++++++++++++++ .../customscript_create_return_refund.xml | 25 ++ src/deploy.xml | 6 +- 4 files changed, 404 insertions(+), 2 deletions(-) create mode 100644 src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js create mode 100644 src/Objects/SalesOrder/customscript_create_return_refund.xml diff --git a/project.json b/project.json index 2ba8310..853ce54 100644 --- a/project.json +++ b/project.json @@ -1,3 +1,3 @@ { - "defaultAuthId": "" + "defaultAuthId": "HotWax-NetSuite" } \ No newline at end of file diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js new file mode 100644 index 0000000..47af25f --- /dev/null +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js @@ -0,0 +1,373 @@ +/** + * @NApiVersion 2.1 + * @NScriptType ScheduledScript + */ + +define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, record, error, search, file) { + + function execute(context) { + try { + // Establish a connection to a remote FTP server + 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 + 'salesorder/return'; + sftpPort = parseInt(sftpPort); + + var connection = sftp.createConnection({ + username: sftpUserName, + keyId: sftpKeyId, + url: sftpUrl, + port: sftpPort, + directory: sftpDirectory, + hostKey: hostKey + }); + + log.debug("Connection established successfully with SFTP server!"); + + var list = connection.list({ + path: '/' + }); + + for (var i = 0; i < list.length; i++) { + if (!list[i].directory) { + var fileName = list[i].name; + + // Download the file from the remote server + var downloadedFile = connection.download({ + directory: '/', + filename: fileName + }); + + if (downloadedFile.size > 0) { + log.debug("File downloaded successfully !" + fileName); + var contents = downloadedFile.getContents(); + + // Parse the Return Authorization JSON file + var returnAuthorizationDataList = JSON.parse(contents); + var errorList = []; + + for (var dataIndex = 0; dataIndex < returnAuthorizationDataList.length; dataIndex++) { + var orderId = returnAuthorizationDataList[dataIndex].order_id; + var itemList = returnAuthorizationDataList[dataIndex].items; + var returnReason = returnAuthorizationDataList[dataIndex].return_reason; + var paymentmethodid = returnAuthorizationDataList[dataIndex].payment_method_id; + + try { + if (orderId) { + + // Initialize Return Authorization from Sales Order + var returnAuthorizationRecord = record.transform({ + fromType: record.Type.SALES_ORDER, + fromId: orderId, + toType: record.Type.RETURN_AUTHORIZATION, + isDynamic: true + }); + + // get customer ID + var customerID = returnAuthorizationRecord.getValue({ + fieldId: 'entity', + }); + + + // Set return reason + returnAuthorizationRecord.setValue({ + fieldId: 'returnreason', + value: returnReason + }); + + // Set order status + returnAuthorizationRecord.setValue({ + fieldId: 'orderstatus', + value: "B" + }); + + var lineCount = returnAuthorizationRecord.getLineCount({ + sublistId: 'item' + }); + + var removeListline = []; + + for (var j = 0; j < lineCount; j++) { + var matchFound = false; + + var itemid = returnAuthorizationRecord.getSublistValue({ + sublistId: 'item', + fieldId: 'item', + line: j + }); + + var externallineid = returnAuthorizationRecord.getSublistValue({ + sublistId: 'item', + fieldId: 'custcol_hc_order_line_id', + line: j + }); + + for (var itemIndex = 0; itemIndex < itemList.length; itemIndex++) { + var productId = itemList[itemIndex].product_id; + var returnquantity = itemList[itemIndex].quantity; + var returnamount = itemList[itemIndex].amount; + var returnlineid = itemList[itemIndex].external_order_line_id; + var locationid = itemList[itemIndex].location_id; + + // If return item match with sales order item + if (productId === itemid && returnlineid === externallineid) { + matchFound = true; + + returnAuthorizationRecord.selectLine({ + sublistId: 'item', + line: j + }); + + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'quantity', + value: returnquantity + }); + + // Custom price level + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'price', + value: "-1" + }); + + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'amount', + value: returnamount + }); + + if(locationid !== "") { + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'location', + value: locationid + }); + } + + returnAuthorizationRecord.commitLine({ + sublistId: 'item' + }); + } + } + + if (!matchFound) { + removeListline.push(j); + } + } + + + // Remove line item are not in return + if (removeListline.length > 0) { + for (var k = removeListline.length - 1; k >= 0; k--) { + var removeitem = removeListline[k] + returnAuthorizationRecord.removeLine({ + sublistId: 'item', + line: removeitem + }); + + } + } + + // Save the Return Authorization + var returnAuthorizationId = returnAuthorizationRecord.save(); + + log.debug("Return Authorization created for sales order with ID: " + orderId + ", RMA ID: " + returnAuthorizationId); + + // Create item receipt + + if(returnAuthorizationId) { + var itemReceipt = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: returnAuthorizationId, + toType: record.Type.ITEM_RECEIPT, + isDynamic: true + }); + + + var itemReceiptId = itemReceipt.save(); + + log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); + + } + + + // Create Credit Memo + if(itemReceiptId) { + var creditMemo = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: returnAuthorizationId, + toType: record.Type.CREDIT_MEMO, + isDynamic: true + }); + + var creditMemoId = creditMemo.save(); + + log.debug("Credit Memo created for return authorization with ID: " + returnAuthorizationId + ", Credit Memo ID: " + creditMemoId); + + } + + + if(creditMemoId) { + var customerRefund = record.create({ + type: record.Type.CUSTOMER_REFUND, + isDynamic: true, + defaultValues: { + entity: customerID, + } + }); + + customerRefund.setValue({ + fieldId: 'customer', + value: customerID + }); + + + + // Set Payment Method + customerRefund.setValue({ + fieldId: 'paymentmethod', + value: paymentmethodid + }); + + + + var lineCountMemo = customerRefund.getLineCount({ + sublistId: 'apply' + }); + + + for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ + + customerRefund.selectLine({ + sublistId: 'apply', + line: countMemo + }); + + + var creditid = customerRefund.getCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'internalid', + }); + + + if(creditMemoId == creditid) { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: true + }); + } else { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: false + }); + } + + } + + var customerRefundId = customerRefund.save(); + + log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); + + } + + + } + } catch (e) { + log.error({ + title: 'Error in processing sales order ' + orderId, + details: e + }); + var errorInfo = orderId + ',' + e.message + '\n'; + errorList.push(errorInfo); + } + } + + // // Archive the file + connection.move({ + from: '/' + fileName, + to: '/archive/' + fileName + }); + + log.debug('File moved! ' + fileName); + + if (errorList.length !== 0) { + var fileLines = 'orderId,errorMessage\n'; + fileLines = fileLines + errorList; + + var date = new Date(); + var errorFileName = date + '-ErrorReturnAuthorizations.csv'; + var fileObj = file.create({ + name: errorFileName, + fileType: file.Type.CSV, + contents: fileLines + }); + + connection.upload({ + directory: '/error/', + file: fileObj + }); + } + + + } + } + } + } catch (e) { + log.error({ + title: 'Error in creating Return Authorizations', + details: e + }); + throw error.create({ + name: "Error in creating Return Authorizations", + message: e + }); + } + } + + return { + execute: execute + }; +}); \ No newline at end of file diff --git a/src/Objects/SalesOrder/customscript_create_return_refund.xml b/src/Objects/SalesOrder/customscript_create_return_refund.xml new file mode 100644 index 0000000..fae1ace --- /dev/null +++ b/src/Objects/SalesOrder/customscript_create_return_refund.xml @@ -0,0 +1,25 @@ + + + F + HC_SC_CreateReturnRefund + F + + T + [/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js] + + + T + DEBUG + SCHEDULED + HC_SC_CreateReturnRefund + + + 1 + PT15M + 2023-08-22 + 05:00:00Z + + + + + \ No newline at end of file diff --git a/src/deploy.xml b/src/deploy.xml index e85aecc..81f2f7f 100644 --- a/src/deploy.xml +++ b/src/deploy.xml @@ -56,9 +56,11 @@ ~/FileCabinet/SuiteScripts/TransferOrder/HC_SC_ImportTOFulfillmentReceipts.js ~/FileCabinet/SuiteScripts/TransferOrder/HC_SC_ImportTOItemFulfillment.js ~/FileCabinet/SuiteScripts/TransferOrder/HC_MR_ExportedStoreTOFulfillmentCSV.js + + ~/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js - + Custom Record ~/Objects/CustomRecord/customrecord_hc_last_runtime_export.xml ~/Objects/CustomRecord/customrecord_ns_sftp_configuration.xml @@ -146,6 +148,8 @@ ~/Objects/TransferOrder/customscript_imp_to_itemfulfillment.xml ~/Objects/TransferOrder/customscript_exp_store_to_fulfillment.xml + ~/Objects/SalesOrder/customscript_create_return_refund.xml + ~/Objects/CashSale/customsearch_hc_export_cashsales.xml ~/Objects/Customer/customsearch_hc_export_customer.xml From 115323d00432b281efb312eaa5739300b37bed9c Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Mon, 1 Apr 2024 14:22:50 +0530 Subject: [PATCH 2/8] Added: support to create return authorization and customer refund in NetSuite. --- .../SalesOrder/HC_SC_CreateReturnRefund.js | 373 ++++++++++++++++++ .../customscript_create_return_refund.xml | 25 ++ src/deploy.xml | 6 +- 3 files changed, 403 insertions(+), 1 deletion(-) create mode 100644 src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js create mode 100644 src/Objects/SalesOrder/customscript_create_return_refund.xml diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js new file mode 100644 index 0000000..47af25f --- /dev/null +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js @@ -0,0 +1,373 @@ +/** + * @NApiVersion 2.1 + * @NScriptType ScheduledScript + */ + +define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, record, error, search, file) { + + function execute(context) { + try { + // Establish a connection to a remote FTP server + 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 + 'salesorder/return'; + sftpPort = parseInt(sftpPort); + + var connection = sftp.createConnection({ + username: sftpUserName, + keyId: sftpKeyId, + url: sftpUrl, + port: sftpPort, + directory: sftpDirectory, + hostKey: hostKey + }); + + log.debug("Connection established successfully with SFTP server!"); + + var list = connection.list({ + path: '/' + }); + + for (var i = 0; i < list.length; i++) { + if (!list[i].directory) { + var fileName = list[i].name; + + // Download the file from the remote server + var downloadedFile = connection.download({ + directory: '/', + filename: fileName + }); + + if (downloadedFile.size > 0) { + log.debug("File downloaded successfully !" + fileName); + var contents = downloadedFile.getContents(); + + // Parse the Return Authorization JSON file + var returnAuthorizationDataList = JSON.parse(contents); + var errorList = []; + + for (var dataIndex = 0; dataIndex < returnAuthorizationDataList.length; dataIndex++) { + var orderId = returnAuthorizationDataList[dataIndex].order_id; + var itemList = returnAuthorizationDataList[dataIndex].items; + var returnReason = returnAuthorizationDataList[dataIndex].return_reason; + var paymentmethodid = returnAuthorizationDataList[dataIndex].payment_method_id; + + try { + if (orderId) { + + // Initialize Return Authorization from Sales Order + var returnAuthorizationRecord = record.transform({ + fromType: record.Type.SALES_ORDER, + fromId: orderId, + toType: record.Type.RETURN_AUTHORIZATION, + isDynamic: true + }); + + // get customer ID + var customerID = returnAuthorizationRecord.getValue({ + fieldId: 'entity', + }); + + + // Set return reason + returnAuthorizationRecord.setValue({ + fieldId: 'returnreason', + value: returnReason + }); + + // Set order status + returnAuthorizationRecord.setValue({ + fieldId: 'orderstatus', + value: "B" + }); + + var lineCount = returnAuthorizationRecord.getLineCount({ + sublistId: 'item' + }); + + var removeListline = []; + + for (var j = 0; j < lineCount; j++) { + var matchFound = false; + + var itemid = returnAuthorizationRecord.getSublistValue({ + sublistId: 'item', + fieldId: 'item', + line: j + }); + + var externallineid = returnAuthorizationRecord.getSublistValue({ + sublistId: 'item', + fieldId: 'custcol_hc_order_line_id', + line: j + }); + + for (var itemIndex = 0; itemIndex < itemList.length; itemIndex++) { + var productId = itemList[itemIndex].product_id; + var returnquantity = itemList[itemIndex].quantity; + var returnamount = itemList[itemIndex].amount; + var returnlineid = itemList[itemIndex].external_order_line_id; + var locationid = itemList[itemIndex].location_id; + + // If return item match with sales order item + if (productId === itemid && returnlineid === externallineid) { + matchFound = true; + + returnAuthorizationRecord.selectLine({ + sublistId: 'item', + line: j + }); + + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'quantity', + value: returnquantity + }); + + // Custom price level + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'price', + value: "-1" + }); + + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'amount', + value: returnamount + }); + + if(locationid !== "") { + returnAuthorizationRecord.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'location', + value: locationid + }); + } + + returnAuthorizationRecord.commitLine({ + sublistId: 'item' + }); + } + } + + if (!matchFound) { + removeListline.push(j); + } + } + + + // Remove line item are not in return + if (removeListline.length > 0) { + for (var k = removeListline.length - 1; k >= 0; k--) { + var removeitem = removeListline[k] + returnAuthorizationRecord.removeLine({ + sublistId: 'item', + line: removeitem + }); + + } + } + + // Save the Return Authorization + var returnAuthorizationId = returnAuthorizationRecord.save(); + + log.debug("Return Authorization created for sales order with ID: " + orderId + ", RMA ID: " + returnAuthorizationId); + + // Create item receipt + + if(returnAuthorizationId) { + var itemReceipt = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: returnAuthorizationId, + toType: record.Type.ITEM_RECEIPT, + isDynamic: true + }); + + + var itemReceiptId = itemReceipt.save(); + + log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); + + } + + + // Create Credit Memo + if(itemReceiptId) { + var creditMemo = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: returnAuthorizationId, + toType: record.Type.CREDIT_MEMO, + isDynamic: true + }); + + var creditMemoId = creditMemo.save(); + + log.debug("Credit Memo created for return authorization with ID: " + returnAuthorizationId + ", Credit Memo ID: " + creditMemoId); + + } + + + if(creditMemoId) { + var customerRefund = record.create({ + type: record.Type.CUSTOMER_REFUND, + isDynamic: true, + defaultValues: { + entity: customerID, + } + }); + + customerRefund.setValue({ + fieldId: 'customer', + value: customerID + }); + + + + // Set Payment Method + customerRefund.setValue({ + fieldId: 'paymentmethod', + value: paymentmethodid + }); + + + + var lineCountMemo = customerRefund.getLineCount({ + sublistId: 'apply' + }); + + + for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ + + customerRefund.selectLine({ + sublistId: 'apply', + line: countMemo + }); + + + var creditid = customerRefund.getCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'internalid', + }); + + + if(creditMemoId == creditid) { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: true + }); + } else { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: false + }); + } + + } + + var customerRefundId = customerRefund.save(); + + log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); + + } + + + } + } catch (e) { + log.error({ + title: 'Error in processing sales order ' + orderId, + details: e + }); + var errorInfo = orderId + ',' + e.message + '\n'; + errorList.push(errorInfo); + } + } + + // // Archive the file + connection.move({ + from: '/' + fileName, + to: '/archive/' + fileName + }); + + log.debug('File moved! ' + fileName); + + if (errorList.length !== 0) { + var fileLines = 'orderId,errorMessage\n'; + fileLines = fileLines + errorList; + + var date = new Date(); + var errorFileName = date + '-ErrorReturnAuthorizations.csv'; + var fileObj = file.create({ + name: errorFileName, + fileType: file.Type.CSV, + contents: fileLines + }); + + connection.upload({ + directory: '/error/', + file: fileObj + }); + } + + + } + } + } + } catch (e) { + log.error({ + title: 'Error in creating Return Authorizations', + details: e + }); + throw error.create({ + name: "Error in creating Return Authorizations", + message: e + }); + } + } + + return { + execute: execute + }; +}); \ No newline at end of file diff --git a/src/Objects/SalesOrder/customscript_create_return_refund.xml b/src/Objects/SalesOrder/customscript_create_return_refund.xml new file mode 100644 index 0000000..fae1ace --- /dev/null +++ b/src/Objects/SalesOrder/customscript_create_return_refund.xml @@ -0,0 +1,25 @@ + + + F + HC_SC_CreateReturnRefund + F + + T + [/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js] + + + T + DEBUG + SCHEDULED + HC_SC_CreateReturnRefund + + + 1 + PT15M + 2023-08-22 + 05:00:00Z + + + + + \ No newline at end of file diff --git a/src/deploy.xml b/src/deploy.xml index e85aecc..4b826ac 100644 --- a/src/deploy.xml +++ b/src/deploy.xml @@ -56,9 +56,11 @@ ~/FileCabinet/SuiteScripts/TransferOrder/HC_SC_ImportTOFulfillmentReceipts.js ~/FileCabinet/SuiteScripts/TransferOrder/HC_SC_ImportTOItemFulfillment.js ~/FileCabinet/SuiteScripts/TransferOrder/HC_MR_ExportedStoreTOFulfillmentCSV.js + + ~/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js - + ~/Objects/CustomRecord/customrecord_hc_last_runtime_export.xml ~/Objects/CustomRecord/customrecord_ns_sftp_configuration.xml @@ -146,6 +148,8 @@ ~/Objects/TransferOrder/customscript_imp_to_itemfulfillment.xml ~/Objects/TransferOrder/customscript_exp_store_to_fulfillment.xml + ~/Objects/SalesOrder/customscript_create_return_refund.xml + ~/Objects/CashSale/customsearch_hc_export_cashsales.xml ~/Objects/Customer/customsearch_hc_export_customer.xml From 325af08393d8dfc995ad6a27bea4ce23d40a9202 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Thu, 4 Apr 2024 11:27:28 +0530 Subject: [PATCH 3/8] Implemented manual return handling for sales orders via user event script --- .../SFTP/HC_SC_CreateSFTPDirectory.js | 10 + .../SalesOrder/HC_SC_CreateReturnRefund.js | 48 +++- .../SalesOrder/HC_UE_ReturnRefund.js | 209 ++++++++++++++++++ .../customscript_hc_ue_returnrefund.xml | 26 +++ src/deploy.xml | 4 +- 5 files changed, 290 insertions(+), 7 deletions(-) create mode 100644 src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js create mode 100644 src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml diff --git a/src/FileCabinet/SuiteScripts/SFTP/HC_SC_CreateSFTPDirectory.js b/src/FileCabinet/SuiteScripts/SFTP/HC_SC_CreateSFTPDirectory.js index bbcd78c..4862ce6 100644 --- a/src/FileCabinet/SuiteScripts/SFTP/HC_SC_CreateSFTPDirectory.js +++ b/src/FileCabinet/SuiteScripts/SFTP/HC_SC_CreateSFTPDirectory.js @@ -197,6 +197,16 @@ define(['N/sftp', 'N/error', 'N/search'], function (sftp, error, search) { path: 'salesorder/giftcard-fulfillment/archive' }); + connection.makeDirectory({ + path: 'salesorder/return' + }); + connection.makeDirectory({ + path: 'salesorder/return/archive' + }); + connection.makeDirectory({ + path: 'salesorder/return/error' + }); + connection.makeDirectory({ path: 'discountitem' }); diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js index 47af25f..32caecf 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js @@ -120,6 +120,11 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, fieldId: 'orderstatus', value: "B" }); + // Set Return Payment method + returnAuthorizationRecord.setValue({ + fieldId: 'custbody_hc_payment_method', + value: paymentmethodid + }); var lineCount = returnAuthorizationRecord.getLineCount({ sublistId: 'item' @@ -127,6 +132,8 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, var removeListline = []; + var cust_ExternalLineID = [] + for (var j = 0; j < lineCount; j++) { var matchFound = false; @@ -183,6 +190,9 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, fieldId: 'location', value: locationid }); + + cust_ExternalLineID.push(externallineid) + } returnAuthorizationRecord.commitLine({ @@ -223,12 +233,38 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, toType: record.Type.ITEM_RECEIPT, isDynamic: true }); - - - var itemReceiptId = itemReceipt.save(); - - log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); - + var itemReceiptlineCount = itemReceipt.getLineCount({ + sublistId: 'item' + }); + + for (var itemlineid = 0; itemlineid < itemReceiptlineCount; itemlineid++) { + var itemReceiptExternalLineID = itemReceipt.getSublistValue({ + sublistId: 'item', + fieldId: 'custcol_hc_order_line_id', + line: itemlineid + }); + + itemReceipt.selectLine({ + sublistId: 'item', + line: itemlineid + }); + if (cust_ExternalLineID.includes(itemReceiptExternalLineID)) { + itemReceipt.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'itemreceive', + value: true + }); + } else { + itemReceipt.setCurrentSublistValue({ + sublistId: 'item', + fieldId: 'itemreceive', + value: false + }); + } + } + var itemReceiptId = itemReceipt.save(); + log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); + } diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js new file mode 100644 index 0000000..49cf5ff --- /dev/null +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js @@ -0,0 +1,209 @@ +/** + * @NApiVersion 2.1 + * @NScriptType UserEventScript + */ +define(['N/record', 'N/search', 'N/sftp', 'N/file'], + +( record, search, sftp, file) => { + + const afterSubmit = (context) => { + + log.debug("User event Script start" ) + + try { + if (context.type === context.UserEventType.CREATE) { + + var errorList = []; + + var newRecord = context.newRecord; + + var createdFrom = newRecord.getValue({ fieldId: 'createdfrom' }); + + var returnFieldid = search.lookupFields({ + type: search.Type.RETURN_AUTHORIZATION, + id: createdFrom, + columns: ["custbody_hc_payment_method"] + }) + + var paymentMethodId = returnFieldid.custbody_hc_payment_method + + if (paymentMethodId) { + + var creditMemo = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: createdFrom, + toType: record.Type.CREDIT_MEMO, + isDynamic: true + }); + + // get customer ID from Credit meme + var customerID = creditMemo.getValue({ + fieldId: 'entity', + }); + + var creditMemoId = creditMemo.save(); + + log.debug("Credit Memo created for return authorization with ID: " + createdFrom + ", Credit Memo ID: " + creditMemoId); + + if(creditMemoId) { + var customerRefund = record.create({ + type: record.Type.CUSTOMER_REFUND, + isDynamic: true, + defaultValues: { + entity: customerID, + } + }); + + customerRefund.setValue({ + fieldId: 'customer', + value: customerID + }); + + // Set Payment Method + customerRefund.setValue({ + fieldId: 'paymentmethod', + value: paymentMethodId + }); + + var lineCountMemo = customerRefund.getLineCount({ + sublistId: 'apply' + }); + + for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ + + customerRefund.selectLine({ + sublistId: 'apply', + line: countMemo + }); + + var creditid = customerRefund.getCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'internalid', + }); + + if(creditMemoId == creditid) { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: true + }); + } else { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: false + }); + } + + } + + var customerRefundId = customerRefund.save(); + + log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); + + } + + } else { + + log.debug("Payment Method is not Found for return authorization ID: " + createdFrom ); + + } + + } + + } catch (e) { + log.error({ + title: 'Error in creating credit memo and customer refund return authorization ID:' + createdFrom , + details: e + }); + + var errorInfo = createdFrom + ',' + e.message + '\n'; + errorList.push(errorInfo); + + if (errorList.length !== 0) { + + // Establish a connection to a remote FTP server + 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 + 'salesorder/return'; + sftpPort = parseInt(sftpPort); + + var connection = sftp.createConnection({ + username: sftpUserName, + keyId: sftpKeyId, + url: sftpUrl, + port: sftpPort, + directory: sftpDirectory, + hostKey: hostKey + }); + + log.debug("Connection established successfully with SFTP server!"); + + + var fileLines = 'ReturnID,errorMessage\n'; + fileLines = fileLines + errorList; + + var date = new Date(); + var errorFileName = date + '-ErrorReturnRefund.csv'; + var fileObj = file.create({ + name: errorFileName, + fileType: file.Type.CSV, + contents: fileLines + }); + + connection.upload({ + directory: '/error/', + file: fileObj + }); + log.debug('Error File moved! ' + errorFileName); + + } + + } + log.debug("User Event Script End") + + } + + return {afterSubmit} + + }); diff --git a/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml b/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml new file mode 100644 index 0000000..baa6e62 --- /dev/null +++ b/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml @@ -0,0 +1,26 @@ + + +F +HC_UE_ReturnRefund +F + +T +F +[/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js] + + +F +T +F +T + + +USERINTERFACE +T +DEBUG +ITEMRECEIPT +ADMINISTRATOR +RELEASED + + + \ No newline at end of file diff --git a/src/deploy.xml b/src/deploy.xml index 4b826ac..6cf681b 100644 --- a/src/deploy.xml +++ b/src/deploy.xml @@ -58,9 +58,10 @@ ~/FileCabinet/SuiteScripts/TransferOrder/HC_MR_ExportedStoreTOFulfillmentCSV.js ~/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js + ~/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js - + ~/Objects/CustomRecord/customrecord_hc_last_runtime_export.xml ~/Objects/CustomRecord/customrecord_ns_sftp_configuration.xml @@ -149,6 +150,7 @@ ~/Objects/TransferOrder/customscript_exp_store_to_fulfillment.xml ~/Objects/SalesOrder/customscript_create_return_refund.xml + ~/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml ~/Objects/CashSale/customsearch_hc_export_cashsales.xml From 597456b1887eba9839b8f729e95adb3ac076bd65 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Wed, 17 Apr 2024 16:58:41 +0530 Subject: [PATCH 4/8] Added item received location validation to HC_SC_CreateSalesReturn.js --- ...rnRefund.js => HC_SC_CreateSalesReturn.js} | 127 +------ .../SalesOrder/HC_UE_ReturnRefund.js | 320 ++++++++---------- ...l => customscript_create_sales_return.xml} | 10 +- .../customscript_hc_ue_returnrefund.xml | 2 +- src/deploy.xml | 4 +- 5 files changed, 170 insertions(+), 293 deletions(-) rename src/FileCabinet/SuiteScripts/SalesOrder/{HC_SC_CreateReturnRefund.js => HC_SC_CreateSalesReturn.js} (74%) rename src/Objects/SalesOrder/{customscript_create_return_refund.xml => customscript_create_sales_return.xml} (65%) diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js similarity index 74% rename from src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js rename to src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js index 32caecf..db8c2da 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js @@ -25,7 +25,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, }); var sftpSearchResult = sftpSearchResults[0]; - var sftpUrl = sftpSearchResult.getValue({ name: 'custrecord_ns_sftp_server' }); @@ -94,7 +93,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, try { if (orderId) { - // Initialize Return Authorization from Sales Order var returnAuthorizationRecord = record.transform({ fromType: record.Type.SALES_ORDER, @@ -103,40 +101,35 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, isDynamic: true }); - // get customer ID - var customerID = returnAuthorizationRecord.getValue({ - fieldId: 'entity', - }); - - // Set return reason returnAuthorizationRecord.setValue({ fieldId: 'returnreason', value: returnReason }); - + + // Set order status returnAuthorizationRecord.setValue({ fieldId: 'orderstatus', value: "B" }); + + // Set Return Payment method returnAuthorizationRecord.setValue({ fieldId: 'custbody_hc_payment_method', value: paymentmethodid - }); + }); var lineCount = returnAuthorizationRecord.getLineCount({ sublistId: 'item' - }); + }); var removeListline = []; - var cust_ExternalLineID = [] for (var j = 0; j < lineCount; j++) { var matchFound = false; - var itemid = returnAuthorizationRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', @@ -159,7 +152,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, // If return item match with sales order item if (productId === itemid && returnlineid === externallineid) { matchFound = true; - returnAuthorizationRecord.selectLine({ sublistId: 'item', line: j @@ -184,15 +176,13 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, value: returnamount }); - if(locationid !== "") { + if (locationid !== "") { returnAuthorizationRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'location', value: locationid }); - cust_ExternalLineID.push(externallineid) - } returnAuthorizationRecord.commitLine({ @@ -205,8 +195,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, removeListline.push(j); } } - - // Remove line item are not in return if (removeListline.length > 0) { for (var k = removeListline.length - 1; k >= 0; k--) { @@ -215,7 +203,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, sublistId: 'item', line: removeitem }); - } } @@ -225,8 +212,7 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, log.debug("Return Authorization created for sales order with ID: " + orderId + ", RMA ID: " + returnAuthorizationId); // Create item receipt - - if(returnAuthorizationId) { + if (returnAuthorizationId && cust_ExternalLineID.length > 0) { var itemReceipt = record.transform({ fromType: record.Type.RETURN_AUTHORIZATION, fromId: returnAuthorizationId, @@ -253,103 +239,18 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, sublistId: 'item', fieldId: 'itemreceive', value: true - }); + }); } else { itemReceipt.setCurrentSublistValue({ sublistId: 'item', fieldId: 'itemreceive', value: false - }); + }); } } - var itemReceiptId = itemReceipt.save(); + var itemReceiptId = itemReceipt.save(); log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); - - } - - - // Create Credit Memo - if(itemReceiptId) { - var creditMemo = record.transform({ - fromType: record.Type.RETURN_AUTHORIZATION, - fromId: returnAuthorizationId, - toType: record.Type.CREDIT_MEMO, - isDynamic: true - }); - - var creditMemoId = creditMemo.save(); - - log.debug("Credit Memo created for return authorization with ID: " + returnAuthorizationId + ", Credit Memo ID: " + creditMemoId); - } - - - if(creditMemoId) { - var customerRefund = record.create({ - type: record.Type.CUSTOMER_REFUND, - isDynamic: true, - defaultValues: { - entity: customerID, - } - }); - - customerRefund.setValue({ - fieldId: 'customer', - value: customerID - }); - - - - // Set Payment Method - customerRefund.setValue({ - fieldId: 'paymentmethod', - value: paymentmethodid - }); - - - - var lineCountMemo = customerRefund.getLineCount({ - sublistId: 'apply' - }); - - - for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ - - customerRefund.selectLine({ - sublistId: 'apply', - line: countMemo - }); - - - var creditid = customerRefund.getCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'internalid', - }); - - - if(creditMemoId == creditid) { - customerRefund.setCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'apply', - value: true - }); - } else { - customerRefund.setCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'apply', - value: false - }); - } - - } - - var customerRefundId = customerRefund.save(); - - log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); - - } - - } } catch (e) { log.error({ @@ -360,14 +261,13 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, errorList.push(errorInfo); } } - // // Archive the file connection.move({ from: '/' + fileName, to: '/archive/' + fileName }); - log.debug('File moved! ' + fileName); + log.debug('File moved!' + fileName); if (errorList.length !== 0) { var fileLines = 'orderId,errorMessage\n'; @@ -386,8 +286,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, file: fileObj }); } - - } } } @@ -402,7 +300,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, }); } } - return { execute: execute }; diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js index 49cf5ff..e2de1f9 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js @@ -3,117 +3,100 @@ * @NScriptType UserEventScript */ define(['N/record', 'N/search', 'N/sftp', 'N/file'], - ( record, search, sftp, file) => { - - const afterSubmit = (context) => { - - log.debug("User event Script start" ) - - try { - if (context.type === context.UserEventType.CREATE) { - - var errorList = []; - - var newRecord = context.newRecord; - - var createdFrom = newRecord.getValue({ fieldId: 'createdfrom' }); - - var returnFieldid = search.lookupFields({ - type: search.Type.RETURN_AUTHORIZATION, - id: createdFrom, - columns: ["custbody_hc_payment_method"] - }) - - var paymentMethodId = returnFieldid.custbody_hc_payment_method - - if (paymentMethodId) { - - var creditMemo = record.transform({ - fromType: record.Type.RETURN_AUTHORIZATION, - fromId: createdFrom, - toType: record.Type.CREDIT_MEMO, - isDynamic: true + const afterSubmit = (context) => { + log.debug("User event Script start") + try { + if (context.type === context.UserEventType.CREATE) { + var errorList = []; + + var newRecord = context.newRecord; + var createdFrom = newRecord.getValue({ fieldId: 'createdfrom' }); + + var returnFieldid = search.lookupFields({ + type: search.Type.RETURN_AUTHORIZATION, + id: createdFrom, + columns: ["custbody_hc_payment_method"] + }) + + var paymentMethodId = returnFieldid.custbody_hc_payment_method + if (paymentMethodId) { + var creditMemo = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: createdFrom, + toType: record.Type.CREDIT_MEMO, + isDynamic: true + }); + + // get customer ID from Credit meme + var customerID = creditMemo.getValue({ + fieldId: 'entity', + }); + + var creditMemoId = creditMemo.save(); + + log.debug("Credit Memo created for return authorization with ID: " + createdFrom + ", Credit Memo ID: " + creditMemoId); + + if (creditMemoId) { + var customerRefund = record.create({ + type: record.Type.CUSTOMER_REFUND, + isDynamic: true, + defaultValues: { + entity: customerID, + } + }); + + customerRefund.setValue({ + fieldId: 'customer', + value: customerID + }); + + // Set Payment Method + customerRefund.setValue({ + fieldId: 'paymentmethod', + value: paymentMethodId + }); + + var lineCountMemo = customerRefund.getLineCount({ + sublistId: 'apply' + }); + + for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ + customerRefund.selectLine({ + sublistId: 'apply', + line: countMemo + }); + + var creditid = customerRefund.getCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'internalid', + }); + + if (creditMemoId == creditid) { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: true }); + } else { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: false + }); + } + } + var customerRefundId = customerRefund.save(); - // get customer ID from Credit meme - var customerID = creditMemo.getValue({ - fieldId: 'entity', - }); - - var creditMemoId = creditMemo.save(); - - log.debug("Credit Memo created for return authorization with ID: " + createdFrom + ", Credit Memo ID: " + creditMemoId); - - if(creditMemoId) { - var customerRefund = record.create({ - type: record.Type.CUSTOMER_REFUND, - isDynamic: true, - defaultValues: { - entity: customerID, - } - }); - - customerRefund.setValue({ - fieldId: 'customer', - value: customerID - }); - - // Set Payment Method - customerRefund.setValue({ - fieldId: 'paymentmethod', - value: paymentMethodId - }); - - var lineCountMemo = customerRefund.getLineCount({ - sublistId: 'apply' - }); - - for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ - - customerRefund.selectLine({ - sublistId: 'apply', - line: countMemo - }); - - var creditid = customerRefund.getCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'internalid', - }); - - if(creditMemoId == creditid) { - customerRefund.setCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'apply', - value: true - }); - } else { - customerRefund.setCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'apply', - value: false - }); - } - - } - - var customerRefundId = customerRefund.save(); - - log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); - - } - - } else { - - log.debug("Payment Method is not Found for return authorization ID: " + createdFrom ); - + log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); } - + } else { + log.debug("Payment Method is not Found for return authorization ID: " + createdFrom); + } } - - } catch (e) { + } catch (e) { log.error({ - title: 'Error in creating credit memo and customer refund return authorization ID:' + createdFrom , + title: 'Error in creating credit memo and customer refund return authorization ID:' + createdFrom, details: e }); @@ -121,7 +104,6 @@ define(['N/record', 'N/search', 'N/sftp', 'N/file'], errorList.push(errorInfo); if (errorList.length !== 0) { - // Establish a connection to a remote FTP server var customRecordSFTPSearch = search.create({ type: 'customrecord_ns_sftp_configuration', @@ -133,77 +115,75 @@ define(['N/record', 'N/search', 'N/sftp', 'N/file'], '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 + 'salesorder/return'; - sftpPort = parseInt(sftpPort); + }); + + var sftpSearchResults = customRecordSFTPSearch.run().getRange({ + start: 0, + end: 1 + }); - var connection = sftp.createConnection({ - username: sftpUserName, - keyId: sftpKeyId, - url: sftpUrl, - port: sftpPort, - directory: sftpDirectory, - hostKey: hostKey - }); + var sftpSearchResult = sftpSearchResults[0]; + var sftpUrl = sftpSearchResult.getValue({ + name: 'custrecord_ns_sftp_server' + }); + + var sftpUserName = sftpSearchResult.getValue({ + name: 'custrecord_ns_sftp_userid' + }); - log.debug("Connection established successfully with SFTP server!"); + 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' + }); - var fileLines = 'ReturnID,errorMessage\n'; - fileLines = fileLines + errorList; + sftpDirectory = sftpDirectory + 'salesorder/return'; + sftpPort = parseInt(sftpPort); - var date = new Date(); - var errorFileName = date + '-ErrorReturnRefund.csv'; - var fileObj = file.create({ - name: errorFileName, - fileType: file.Type.CSV, - contents: fileLines - }); + var connection = sftp.createConnection({ + username: sftpUserName, + keyId: sftpKeyId, + url: sftpUrl, + port: sftpPort, + directory: sftpDirectory, + hostKey: hostKey + }); + + log.debug("Connection established successfully with SFTP server!"); + + var fileLines = 'ReturnID,errorMessage\n'; + fileLines = fileLines + errorList; - connection.upload({ - directory: '/error/', - file: fileObj - }); - log.debug('Error File moved! ' + errorFileName); - - } - - } - log.debug("User Event Script End") + var date = new Date(); + var errorFileName = date + '-ErrorReturnRefund.csv'; + + var fileObj = file.create({ + name: errorFileName, + fileType: file.Type.CSV, + contents: fileLines + }); + + connection.upload({ + directory: '/error/', + file: fileObj + }); - } + log.debug('Error File moved! ' + errorFileName); + } + } + log.debug("User Event Script End") + } - return {afterSubmit} + return { afterSubmit } }); diff --git a/src/Objects/SalesOrder/customscript_create_return_refund.xml b/src/Objects/SalesOrder/customscript_create_sales_return.xml similarity index 65% rename from src/Objects/SalesOrder/customscript_create_return_refund.xml rename to src/Objects/SalesOrder/customscript_create_sales_return.xml index fae1ace..8a2dd6c 100644 --- a/src/Objects/SalesOrder/customscript_create_return_refund.xml +++ b/src/Objects/SalesOrder/customscript_create_sales_return.xml @@ -1,17 +1,17 @@ - + F - HC_SC_CreateReturnRefund + HC_SC_CreateSalesReturn F T - [/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js] + [/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js] - + T DEBUG SCHEDULED - HC_SC_CreateReturnRefund + HC_SC_CreateSalesReturn 1 diff --git a/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml b/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml index baa6e62..77f0a25 100644 --- a/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml +++ b/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml @@ -15,7 +15,7 @@ T -USERINTERFACE +SCHEDULED|USERINTERFACE T DEBUG ITEMRECEIPT diff --git a/src/deploy.xml b/src/deploy.xml index 6cf681b..b13e7b7 100644 --- a/src/deploy.xml +++ b/src/deploy.xml @@ -57,7 +57,7 @@ ~/FileCabinet/SuiteScripts/TransferOrder/HC_SC_ImportTOItemFulfillment.js ~/FileCabinet/SuiteScripts/TransferOrder/HC_MR_ExportedStoreTOFulfillmentCSV.js - ~/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateReturnRefund.js + ~/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js ~/FileCabinet/SuiteScripts/SalesOrder/HC_UE_ReturnRefund.js @@ -149,7 +149,7 @@ ~/Objects/TransferOrder/customscript_imp_to_itemfulfillment.xml ~/Objects/TransferOrder/customscript_exp_store_to_fulfillment.xml - ~/Objects/SalesOrder/customscript_create_return_refund.xml + ~/Objects/SalesOrder/customscript_create_sales_return.xml ~/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml From bed231c92ddda17df098993c44dd8a5dfb240d61 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Wed, 17 Apr 2024 18:17:08 +0530 Subject: [PATCH 5/8] Commit reverted --- .../SalesOrder/HC_SC_CreateSalesReturn.js | 69 +++++++++++++++++++ .../customscript_hc_ue_returnrefund.xml | 2 +- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js index db8c2da..18055f8 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js @@ -251,6 +251,75 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, var itemReceiptId = itemReceipt.save(); log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); } + + // Create Credit Memo + if (itemReceiptId) { + var creditMemo = record.transform({ + fromType: record.Type.RETURN_AUTHORIZATION, + fromId: returnAuthorizationId, + toType: record.Type.CREDIT_MEMO, + isDynamic: true + }); + + var creditMemoId = creditMemo.save(); + + log.debug("Credit Memo created for return authorization with ID: " + returnAuthorizationId + ", Credit Memo ID: " + creditMemoId); + } + + if (creditMemoId) { + var customerRefund = record.create({ + type: record.Type.CUSTOMER_REFUND, + isDynamic: true, + defaultValues: { + entity: customerID, + } + }); + + customerRefund.setValue({ + fieldId: 'customer', + value: customerID + }); + + // Set Payment Method + customerRefund.setValue({ + fieldId: 'paymentmethod', + value: paymentmethodid + }); + + var lineCountMemo = customerRefund.getLineCount({ + sublistId: 'apply' + }); + + for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ + customerRefund.selectLine({ + sublistId: 'apply', + line: countMemo + }); + + var creditid = customerRefund.getCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'internalid', + }); + + if (creditMemoId == creditid) { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: true + }); + } else { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: false + }); + } + } + + var customerRefundId = customerRefund.save(); + + log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); + } } } catch (e) { log.error({ diff --git a/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml b/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml index 77f0a25..baa6e62 100644 --- a/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml +++ b/src/Objects/SalesOrder/customscript_hc_ue_returnrefund.xml @@ -15,7 +15,7 @@ T -SCHEDULED|USERINTERFACE +USERINTERFACE T DEBUG ITEMRECEIPT From 77d1055ab305f4e8da0e6cc9a4d8b4c83935c1a5 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Wed, 17 Apr 2024 18:24:32 +0530 Subject: [PATCH 6/8] Remove line space --- .../SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js index 18055f8..f07927c 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js @@ -106,7 +106,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, fieldId: 'returnreason', value: returnReason }); - // Set order status returnAuthorizationRecord.setValue({ @@ -114,7 +113,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, value: "B" }); - // Set Return Payment method returnAuthorizationRecord.setValue({ fieldId: 'custbody_hc_payment_method', From 892d21649ac33874e40e4cd4174367bdd9aa5188 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Wed, 17 Apr 2024 18:30:02 +0530 Subject: [PATCH 7/8] customer id added --- .../SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js index f07927c..ea7a8f4 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js @@ -101,6 +101,11 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, isDynamic: true }); + // get customer ID + var customerID = returnAuthorizationRecord.getValue({ + fieldId: 'entity', + }); + // Set return reason returnAuthorizationRecord.setValue({ fieldId: 'returnreason', From 2f3e9b0f99347eba19af15982927ccdaa591fc75 Mon Sep 17 00:00:00 2001 From: Aniket Chouhan Date: Thu, 18 Apr 2024 14:12:19 +0530 Subject: [PATCH 8/8] Add support for multiple payment methods in return feature --- .../SalesOrder/HC_SC_CreateSalesReturn.js | 113 ++++++++++-------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js index ea7a8f4..db1c12a 100644 --- a/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js +++ b/src/FileCabinet/SuiteScripts/SalesOrder/HC_SC_CreateSalesReturn.js @@ -89,8 +89,8 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, var orderId = returnAuthorizationDataList[dataIndex].order_id; var itemList = returnAuthorizationDataList[dataIndex].items; var returnReason = returnAuthorizationDataList[dataIndex].return_reason; - var paymentmethodid = returnAuthorizationDataList[dataIndex].payment_method_id; - + var paymentlist = returnAuthorizationDataList[dataIndex].payment_list; + try { if (orderId) { // Initialize Return Authorization from Sales Order @@ -118,12 +118,6 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, value: "B" }); - // Set Return Payment method - returnAuthorizationRecord.setValue({ - fieldId: 'custbody_hc_payment_method', - value: paymentmethodid - }); - var lineCount = returnAuthorizationRecord.getLineCount({ sublistId: 'item' }); @@ -201,20 +195,19 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, // Remove line item are not in return if (removeListline.length > 0) { for (var k = removeListline.length - 1; k >= 0; k--) { - var removeitem = removeListline[k] + var removeitem = removeListline[k] returnAuthorizationRecord.removeLine({ sublistId: 'item', line: removeitem }); } } - // Save the Return Authorization var returnAuthorizationId = returnAuthorizationRecord.save(); log.debug("Return Authorization created for sales order with ID: " + orderId + ", RMA ID: " + returnAuthorizationId); - - // Create item receipt + + // Create item receipt if (returnAuthorizationId && cust_ExternalLineID.length > 0) { var itemReceipt = record.transform({ fromType: record.Type.RETURN_AUTHORIZATION, @@ -251,8 +244,10 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, }); } } - var itemReceiptId = itemReceipt.save(); - log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); + + var itemReceiptId = itemReceipt.save(); + + log.debug("Item Receipt created for return authorization with ID: " + returnAuthorizationId + ", Item Receipt ID: " + itemReceiptId); } // Create Credit Memo @@ -270,58 +265,70 @@ define(['N/sftp', 'N/record', 'N/error', 'N/search', 'N/file'], function (sftp, } if (creditMemoId) { - var customerRefund = record.create({ - type: record.Type.CUSTOMER_REFUND, - isDynamic: true, - defaultValues: { + for (let list = 0; list < paymentlist.length; list++) { + var paymentID = paymentlist[list].payment_method_id + var refundAmount = paymentlist[list].refund_amount + + var customerRefund = record.create({ + type: record.Type.CUSTOMER_REFUND, + isDynamic: true, + defaultValues: { entity: customerID, - } - }); - - customerRefund.setValue({ - fieldId: 'customer', - value: customerID - }); - - // Set Payment Method - customerRefund.setValue({ - fieldId: 'paymentmethod', - value: paymentmethodid - }); - - var lineCountMemo = customerRefund.getLineCount({ - sublistId: 'apply' - }); + } + }); + + customerRefund.setValue({ + fieldId: 'customer', + value: customerID + }); - for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ - customerRefund.selectLine({ - sublistId: 'apply', - line: countMemo + // Set Payment Method + customerRefund.setValue({ + fieldId: 'paymentmethod', + value: paymentID }); - var creditid = customerRefund.getCurrentSublistValue({ - sublistId: 'apply', - fieldId: 'internalid', + var lineCountMemo = customerRefund.getLineCount({ + sublistId: 'apply' }); - if (creditMemoId == creditid) { - customerRefund.setCurrentSublistValue({ + for (var countMemo = 0; countMemo < lineCountMemo; countMemo++){ + customerRefund.selectLine({ sublistId: 'apply', - fieldId: 'apply', - value: true + line: countMemo }); - } else { - customerRefund.setCurrentSublistValue({ + + var creditid = customerRefund.getCurrentSublistValue({ sublistId: 'apply', - fieldId: 'apply', - value: false + fieldId: 'internalid', }); + + if (creditMemoId == creditid) { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: true + }); + + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'amount', + value: refundAmount + }); + + } else { + customerRefund.setCurrentSublistValue({ + sublistId: 'apply', + fieldId: 'apply', + value: false + }); + } } - } - var customerRefundId = customerRefund.save(); + var customerRefundId = customerRefund.save(); - log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); + log.debug("Customer Refund created for credit memo with ID: " + creditMemoId + ", Customer Refund ID: " + customerRefundId); + } } } } catch (e) {