/** * Copyright (c) 1998-2017 NetSuite, Inc. * 2955 Campus Drive, Suite 100, San Mateo, CA, USA 94403-2511 * All Rights Reserved. * * This software is the confidential and proprietary information of * NetSuite, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with NetSuite. **/ /** * Module Description: * * * * Module Name * * * Version Date Author Remarks 1.00 Feb 20, 2019 lnekula * */ /** * @NApiVersion 2.x * @NScriptType Suitelet * @NModuleScope Public */ define( ['N/record', 'N/ui/serverWidget', 'N/search', 'N/runtime', 'N/http', 'SuiteScripts/NSUtilvSS2.js'], /** * @param {record} * record * @param {serverWidget} * serverWidget */ function (record, ui, search, runtime, http, NSUtil) { /** * Evaluate if the given string or object value is empty, null or * undefined. * * @param {String} * stValue - string or object to evaluate * @returns {Boolean} - true if empty/null/undefined, false if not * @author mmeremilla * @memberOf NSUtil */ var isEmpty = function (stValue) { return ((stValue === '' || stValue == null || stValue == undefined) || (stValue.constructor === Array && stValue.length == 0) || (stValue.constructor === Object && (function ( v) { for (var k in v) return false; return true; })(stValue))); }; var Helper = {}; Helper.getResultsColumns = function (searchSearch, resultSetList, key) { var stLogTitle = 'getResultsColumns'; var index = ''; var myPagedData = searchSearch.runPaged(); myPagedData.pageRanges.forEach(function (pageRange) { var myPage = myPagedData.fetch({index: pageRange.index}); myPage.data.forEach(function (objSearchResult) { var resultSet = {}; for (var ii = 0; ii < searchSearch.columns.length; ii++) { var value = objSearchResult.getValue( { name: searchSearch.columns[ii] }); resultSet[searchSearch.columns[ii].label] = value; if (searchSearch.columns[ii].label == key) { index = value } } if (typeof resultSetList[index] === 'undefined') resultSetList[index] = []; resultSetList[index].push(resultSet) }); }); //log.debug(stLogTitle + ' resultSetList : ', JSON.stringify(resultSetList)); return resultSetList; } var LOG_TITLE = 'Promotional Sales Order.'; var COL_PREFIX = 'custpage_mnf_col_'; /** * Definition of the Suitelet script trigger point. * * @param {Object} * context * @param {ServerRequest} * context.request - Encapsulation of the incoming * request * @param {ServerResponse} * context.response - Encapsulation of the Suitelet * response * @Since 2015.2 */ function onRequest(context) { // Process request as such var request = context.request; var strFunctionName = 'onRequest'; var stLogTitle = LOG_TITLE + 'onRequest'; var theForm = undefined; // Get and validate input project search var scriptObj = runtime.getCurrentScript(); var objParams = {}; // Get parameters var stInvoiceSearch = scriptObj.getParameter({ name: 'custscript_purchrep_salesorder_search' }); var stItemBOMSearch = scriptObj.getParameter({ name: 'custscript_purchrep_item_bom_search' }); var stBomRevSearch = scriptObj.getParameter({ name: 'custscript_purchrep_bomrev_search' }); var custscript_purchase_report_get_component = scriptObj.getParameter({ name: 'custscript_purchase_report_get_component' }); //custscript_purchrep_inboundshipment var stInboundShipmentSearch = scriptObj.getParameter({ name: 'custscript_purchrep_inboundshipment' }); //custscript_purchrep_purchaseorder var stPurchaseOrderSearch = scriptObj.getParameter({ name: 'custscript_purchrep_purchaseorder' }); var stClient = scriptObj.getParameter({ name: 'custscript_purchase_report_client' }); objParams = request.parameters; objParams.client = stClient; objParams.search = stInvoiceSearch; objParams.stBomRevSearch = stBomRevSearch; objParams.stItemBOMSearch = stItemBOMSearch; objParams.stInboundShipmentSearch = stInboundShipmentSearch; objParams.stPurchaseOrderSearch = stPurchaseOrderSearch; objParams.custscript_purchase_report_get_component = custscript_purchase_report_get_component; log.debug(stLogTitle, 'objParams: ' + JSON.stringify(objParams)); // If request method is post if (request.method == http.Method.POST) { // processSubmit if loadtype is empty if (isEmpty(objParams.loadtype)) { try { processSubmit(request); objParams.status = 'OK'; } catch (e) { objParams.status = e.name + ': ' + e.message; } } } // Generate suitelet form based on parameters theForm = generateForm(objParams); // Write into response context.response.writePage(theForm); } /** * Function generating main suitelet form on GET request. * @function generateForm * * @param {Object} * objParams - script parameters * @return {Object} * objForm - Suitelet form object */ function generateForm(objParams) { var stLogTitle = LOG_TITLE + 'generateForm'; var intCounter = 0; log.debug(stLogTitle, '-- Entry --'); var stTitle = 'Component Purchase Report Script'; // Create form with respective title var objForm = ui.createForm({ title: stTitle }); // Set client file id objForm.clientScriptFileId = objParams.client; // Add main field group var objFilterFieldGroup = objForm.addFieldGroup({ id: 'custpage_mnf_fg_filters', label: 'Filters' }); var obj3rdPartyPayerField = ''; // Add 3rd party payer field var obj3rdPartyPayerField = objForm.addField({ id: 'custpage_mnf_fld_3pp', type: ui.FieldType.TEXT, label: 'Vendor', container: 'custpage_mnf_fg_filters'//, //source: record.Type.SALES_ORDER }); if (!isEmpty(objParams.custpage_mnf_fld_3pp)) { obj3rdPartyPayerField.defaultValue = objParams.custpage_mnf_fld_3pp; } obj3rdPartyPayerField.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN }); // Add customer field var objCustomerField = objForm.addField({ id: 'custpage_mnf_fld_customer', type: ui.FieldType.TEXT, label: 'Vendor', container: 'custpage_mnf_fg_filters'//, //source: record.Type.CUSTOMER }); if (!isEmpty(objParams.custpage_mnf_fld_customer)) { objCustomerField.defaultValue = objParams.custpage_mnf_fld_customer; } /*objCustomerField.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN });*/ // Add tran date field var objDateFilter = objForm.addField({ id: 'custpage_mnf_fld_trandate', type: ui.FieldType.DATE, label: 'Sales Order Date', container: 'custpage_mnf_fg_filters' }); if (!isEmpty(objParams.custpage_mnf_fld_trandate)) { objDateFilter.defaultValue = objParams.custpage_mnf_fld_trandate; } objDateFilter.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN }); // Add due date field var objDueDateField = objForm.addField({ id: 'custpage_mnf_fld_due_date', type: ui.FieldType.DATE, label: 'Due Date', container: 'custpage_mnf_fg_filters' }); if (!isEmpty(objParams.custpage_mnf_fld_due_date)) { objDueDateField.defaultValue = objParams.custpage_mnf_fld_due_date; } objDueDateField.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN }); // Add Message hidden fiedl var objMessage = objForm.addField({ id: 'custpage_mnf_fld_message', type: ui.FieldType.LONGTEXT, label: 'Message', container: 'custpage_mnf_fg_filters' }); objMessage.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN }); objMessage.defaultValue = objParams.status; // Add search and submit button /*objForm.addSubmitButton({ label: 'Submit' });*/ objForm.addButton({ id: 'custpage_mnf_search', label: 'Search', functionName: 'performSearch' }); // Construct objSublist var objSublist = objForm.addSublist({ id: 'custpage_mnf_sublist_invoices', type: ui.SublistType.LIST, label: 'Sales Order Items' }); objSublist.addMarkAllButtons(); var bomsearchTranslation = {}; // If load type not empty if (!isEmpty(objParams.loadtype)) { // and invoice search param provided if (!isEmpty(objParams.search)) { // Load the search var objSearch = search.load({ id: objParams.search }); //log.debug(stLogTitle, 'objSearch: ' + JSON.stringify(objSearch)); // invoke search invoices to get applicable invoices var arrInvoices = searchInvoices(objSearch, objParams); //log.debug(stLogTitle, 'arrInvoices: ' + JSON.stringify(arrInvoices)); // Search for Item and Bill of Materials Search //var objComponentQtySearch = search.load({ // id: objParams.custscript_purchase_report_get_component //}); //var arrComponentItemQtySearch = Helper.getResultsColumns(objComponentQtySearch, {}, 'internalid') //log.debug(stLogTitle, 'arrComponentItemQtySearch: ' + JSON.stringify(arrComponentItemQtySearch)); //var objItemBOMSearch = search.load({ // id: objParams.stItemBOMSearch //}); //log.debug(stLogTitle, 'objItemBOMSearch: ' + JSON.stringify(objItemBOMSearch)); // invoke search invoices to get applicable invoices //var arrItemBOMSearch = searchInvoices(objItemBOMSearch, objParams); //var arrItemBOMSearch = Helper.getResultsColumns(objItemBOMSearch, {}, 'internalid') //log.debug(stLogTitle, 'arrItemBOMSearch : ' + JSON.stringify(arrItemBOMSearch)); //for (var index in arrItemBOMSearch) { // log.debug(stLogTitle, 'arrItemBOMSearch keys : ' + JSON.stringify(Object.keys(arrItemBOMSearch[index][0]))); // break; //} // End of Item and Bill of Materials Search // Search for Bill of Materials Revision Search //var objBOMRevSearch = search.load({ // id: objParams.stBomRevSearch //}); //var arrBOMRevSearch = Helper.getResultsColumns(objBOMRevSearch, {}, 'billofmaterials') //log.debug(stLogTitle, 'arrBOMRevSearch : ' + JSON.stringify(arrBOMRevSearch)); // End of Bill of Materials Revision Search // Search for Inbound Shipment Revision Search var objInboundShipmentSearch = search.load({ id: objParams.stInboundShipmentSearch }); var arrInboundShipmentSearch = Helper.getResultsColumns(objInboundShipmentSearch, {}, 'internalid') log.debug(stLogTitle, 'arrInboundShipmentSearch : ' + JSON.stringify(arrInboundShipmentSearch)); // End of Inbound Shipment Search // Search for Purchase Order Search //var objPurchaseOrderSearch = search.load({ // id: objParams.stPurchaseOrderSearch //}); //var arrPurchaseOrderSearch = Helper.getResultsColumns(objPurchaseOrderSearch, {}, 'internalid') //log.debug(stLogTitle, 'arrPurchaseOrderSearch : ' + JSON.stringify(arrPurchaseOrderSearch)); // End of Purchase Order Search var resultItemBOMRev = {}; /* for (var index in arrItemBOMSearch) { for (var index1 in arrItemBOMSearch[index]) { //log.debug(stLogTitle, 'arrItemBOMSearch : ' + JSON.stringify(arrItemBOMSearch[index][index1])); var indexarrItemBOMSearch = arrItemBOMSearch[index][index1].billofmaterialsid; var assemblyIndex = index; //arrItemBOMSearch[index][index1].internalid; //log.debug(stLogTitle, 'arrBOMRevSearch[index] : ' + JSON.stringify(arrBOMRevSearch[index])); for (var indexarrBOMRevSearch in arrBOMRevSearch[indexarrItemBOMSearch]) { log.debug(stLogTitle, 'arrBOMRevSearch : ' + JSON.stringify(arrBOMRevSearch[indexarrItemBOMSearch][indexarrBOMRevSearch])); if (typeof resultItemBOMRev[assemblyIndex] === 'undefined') resultItemBOMRev[assemblyIndex] = []; var element = {}; element = { billofmaterialid: indexarrItemBOMSearch, componentItem: arrBOMRevSearch[indexarrItemBOMSearch][indexarrBOMRevSearch].componentitem, componentItemId: arrBOMRevSearch[indexarrItemBOMSearch][indexarrBOMRevSearch].componentitemid, componentQuantity: arrBOMRevSearch[indexarrItemBOMSearch][indexarrBOMRevSearch].componentquantity, componentitemdesc: arrBOMRevSearch[indexarrItemBOMSearch][indexarrBOMRevSearch].componentdescription || null, }; resultItemBOMRev[assemblyIndex].push(element) } } } */ //log.debug(stLogTitle, 'resultItemBOMRev : ' + JSON.stringify(resultItemBOMRev)); // If invoices are found if (!isEmpty(arrInvoices)) { // Add sublist columns // select checkbox var fldSelect = objSublist.addField({ id: 'custpage_mnf_col_invoice_select', type: ui.FieldType.CHECKBOX, label: 'SELECT' }); fldSelect.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN }); // Invoice id - hidden var fldId = objSublist.addField({ id: 'custpage_mnf_col_id', type: ui.FieldType.TEXT, label: 'ID' }); fldId.updateDisplayType({ displayType: ui.FieldDisplayType.HIDDEN }); // For each search column, add column to the suitelet objSearch.columns.forEach(function (column) { //log.debug(stLogTitle, 'column: ' + JSON.stringify(column)); column = JSON.parse(JSON.stringify(column)); //log.debug(stLogTitle, 'column.name: ' + column.name); // log.debug(stLogTitle, 'column.type: ' + column.type); // log.debug(stLogTitle, 'column.label: ' + column.label); if (column.type.toUpperCase() == ui.FieldType.SELECT) { column.type = ui.FieldType.TEXT; } else if (column.type.toUpperCase() == ui.FieldType.CHECKBOX) { column.type = ui.FieldType.TEXT; } if (column.type == 'currency2') { var stType = 'text'; } else { var stType = column.type; } if (column.label != 'internalid-internalid') { var name = column.label.split('-')[0] var label = column.label.split('-')[1] if (label.indexOf('INV') != -1) { stType = 'INTEGER'; } //log.debug(stLogTitle, 'column.label: ' + name + 'Label : ' + label); var objColumn = objSublist.addField({ id: COL_PREFIX + name, //column.name, type: stType, label: label //column.label }); } }); //log.debug(stLogTitle, JSON.stringify(objSublist)) /* var objColumn = objSublist.addField({ id: COL_PREFIX + 'componentitemid', type: ui.FieldType.TEXT, label: 'Component Item' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'componentitemdesc', type: ui.FieldType.TEXT, label: 'Component Item Description' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'componentquantity', type: ui.FieldType.FLOAT, label: 'Component Quantity' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'totalcomponentqty', type: ui.FieldType.FLOAT, label: 'Total Component Quantity' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'componentohnand', type: ui.FieldType.FLOAT, label: 'Component On Hand' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'componentavailable', type: ui.FieldType.FLOAT, label: 'Component Available' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'poquantity', type: ui.FieldType.FLOAT, label: 'Work Order Quantity' });*/ var objColumn = objSublist.addField({ id: COL_PREFIX + 'quantityremaining', type: ui.FieldType.FLOAT, label: 'Inbound Shipment Quantity' }); var objColumn = objSublist.addField({ id: COL_PREFIX + 'internalid', type: 'text', label: 'Internal ID' }); var intQuantity = 1; var objSaveLineValues = {}; var intItemId = ''; var insertLine = true; var objFinalSummarized = {} // for each search result - invoice, insert line into the sublist arrInvoices.forEach(function (result) { var element = {}; var quantityavailable = 0; var soquantity = 0; var backorderquantity = 0; var quantityonorder = 0; var blankinventoryonhand =0; var totalblankinventoryonhand=0; //log.audit(stLogTitle, 'Object.keys(result) : ' + JSON.stringify(Object.keys(result))); //log.audit(stLogTitle, 'Object.keys(result) : ' + JSON.stringify(result)); Object.keys(result).forEach(function (key) { //log.debug(stLogTitle, 'key : ' + key + ' result[key] : ' + result[key]) if (isEmpty(result[key])) { result[key] = null; return; } if (key == 'quantityavailable') { quantityavailable = NSUtil.forceInt(result[key]) } if (key == 'quantity') { soquantity = NSUtil.forceInt(result[key]) } if (key == 'backordered') { backorderquantity = NSUtil.forceInt(result[key]) } if (key == 'quantityonorder') { quantityonorder = NSUtil.forceInt(result[key]) } if (key == 'blankinvonhand') { blankinventoryonhand = NSUtil.forceInt(NSUtil.forceInt(quantityavailable) - NSUtil.forceInt(soquantity) - NSUtil.forceInt(backorderquantity)).toFixed(0) objSublist.setSublistValue({ id: COL_PREFIX + key, line: intCounter, value: blankinventoryonhand }); } else if (key == 'totalblankinv') { objSublist.setSublistValue({ id: COL_PREFIX + key, line: intCounter, value: NSUtil.forceInt(NSUtil.forceInt(blankinventoryonhand) + NSUtil.forceInt(quantityonorder)).toFixed(0) }); } else { objSublist.setSublistValue({ id: COL_PREFIX + key, line: intCounter, value: result[key] }); } element[COL_PREFIX + key] = result[key]; objSaveLineValues[key] = result[key] //log.debug(stLogTitle, ' key :' + key) if (key == 'quantity') { intQuantity = NSUtil.forceInt(result[key]) } if (key == 'internalid' && !isEmpty(result[key])) { intItemId = NSUtil.forceInt(result[key]); //insertLine = true; } if (key == 'quantity') { /* log.debug(stLogTitle, 'intItemId : ' + intItemId + ' result[key] : ' + intItemId + JSON.stringify(arrPurchaseOrderSearch[intItemId]) + JSON.stringify(arrInboundShipmentSearch[intItemId]));*/ //arrPurchaseOrderSearch /* objSublist.setSublistValue({ id: COL_PREFIX + 'poquantity', line: intCounter, value: typeof arrPurchaseOrderSearch[intItemId] == 'undefined' ? '0' : (arrPurchaseOrderSearch[intItemId][0].poquantity || 0) }); element[COL_PREFIX + 'poquantity'] = typeof arrPurchaseOrderSearch[intItemId] == 'undefined' ? '0' : (arrPurchaseOrderSearch[intItemId][0].poquantity || 0) */ //arrInboundShipmentSearch objSublist.setSublistValue({ id: COL_PREFIX + 'quantityremaining', line: intCounter, value: typeof arrInboundShipmentSearch[intItemId] == 'undefined' ? '0' : (arrInboundShipmentSearch[intItemId][0].quantityremaining || 0) }); element[COL_PREFIX + 'quantityremaining'] = typeof arrInboundShipmentSearch[intItemId] == 'undefined' ? '0' : (arrInboundShipmentSearch[intItemId][0].quantityremaining || 0) objSublist.setSublistValue({ id: COL_PREFIX + key, line: intCounter, value: result[key] }); //log.debug(stLogTitle, 'resultItemBOMRev[intItemId] : ' // + JSON.stringify(resultItemBOMRev[intItemId])); var componentindex = intCounter; /* for (var indexresultItemBOMRev in resultItemBOMRev[intItemId]) { componentindex = componentindex + 1; var elementComponent = {}; elementComponent[COL_PREFIX + 'componentitemid'] = resultItemBOMRev[intItemId][indexresultItemBOMRev].componentItem; var componentItemId = resultItemBOMRev[intItemId][indexresultItemBOMRev].componentItemId; elementComponent[COL_PREFIX + 'componentquantity'] = resultItemBOMRev[intItemId][indexresultItemBOMRev].componentQuantity; elementComponent[COL_PREFIX + 'totalcomponentqty'] = NSUtil.forceInt(resultItemBOMRev[intItemId][indexresultItemBOMRev].componentQuantity) * intQuantity; elementComponent[COL_PREFIX + 'componentitemdesc'] = resultItemBOMRev[intItemId][indexresultItemBOMRev].componentitemdesc; elementComponent[COL_PREFIX + 'componentohnand'] = typeof arrComponentItemQtySearch[componentItemId] != 'undefined' ? arrComponentItemQtySearch[componentItemId][0].onhand || 0 : 0 elementComponent[COL_PREFIX + 'componentonorder'] = typeof arrComponentItemQtySearch[componentItemId] != 'undefined' ? arrComponentItemQtySearch[componentItemId][0].onorder || 0 : 0 elementComponent[COL_PREFIX + 'componentavailable'] = typeof arrComponentItemQtySearch[componentItemId] != 'undefined' ? arrComponentItemQtySearch[componentItemId][0].available || 0 : 0 elementComponent[COL_PREFIX + 'poquantity'] = typeof arrPurchaseOrderSearch[componentItemId] == 'undefined' ? '0' : arrPurchaseOrderSearch[componentItemId][0].poquantity elementComponent[COL_PREFIX + 'quantityexpected'] = typeof arrInboundShipmentSearch[componentItemId] == 'undefined' ? '0' : arrInboundShipmentSearch[componentItemId][0].quantityexpected //intCounter++; insertLine = false; if (typeof objFinalSummarized[componentindex] === 'undefined') objFinalSummarized[componentindex] = []; objFinalSummarized[componentindex].push(elementComponent) log.debug(stLogTitle, 'objFinalSummarized[intCounter] : ' + JSON.stringify(objFinalSummarized[componentindex])); //var element = {}; } */ } }); if (insertLine == true) { intCounter++; //log.audit("intCounter", intCounter) // RR log } /*if (insertLine == true) { intCounter++; insertLine = true; if (typeof objFinalSummarized[intCounter] === 'undefined') objFinalSummarized[intCounter] = []; objFinalSummarized[intCounter].push(element) log.debug(stLogTitle, 'objFinalSummarized[intCounter] : ' + JSON.stringify(objFinalSummarized[intCounter])); }*/ }); /* log.debug(stLogTitle, 'intCounter : ' + JSON.stringify(intCounter)); log.debug(stLogTitle, 'objFinalSummarized : ' + JSON.stringify(objFinalSummarized)); var objForDisplay = {} var element = {} for (var i in objFinalSummarized) { var componentItemId = objFinalSummarized[i][0].custpage_mnf_col_componentitemid ? objFinalSummarized[i][0].custpage_mnf_col_componentitemid : '' //log.debug(stLogTitle, 'componentItemId : ' + componentItemId); if (typeof objForDisplay[componentItemId] === 'undefined') objForDisplay[componentItemId] = []; if (!NSUtil.isEmpty(componentItemId)) { var element = {} element.custpage_mnf_col_componentquantity = objFinalSummarized[i][0].custpage_mnf_col_componentquantity; element.custpage_mnf_col_totalcomponentqty = objFinalSummarized[i][0].custpage_mnf_col_totalcomponentqty; element.custpage_mnf_col_componentitemdesc = objFinalSummarized[i][0].custpage_mnf_col_componentitemdesc; element.custpage_mnf_col_componentohnand = objFinalSummarized[i][0].custpage_mnf_col_componentohnand; element.custpage_mnf_col_componentavailable = objFinalSummarized[i][0].custpage_mnf_col_componentavailable; element.custpage_mnf_col_poquantity = objFinalSummarized[i][0].custpage_mnf_col_poquantity; element.custpage_mnf_col_quantityexpected = objFinalSummarized[i][0].custpage_mnf_col_quantityexpected; objForDisplay[componentItemId].push(element); } } log.debug(stLogTitle, 'objForDisplay : ' + JSON.stringify(objForDisplay)); var objForDisplayComponentTotals = {}; for (var i in objForDisplay) { if (!NSUtil.isEmpty(objForDisplay[i])) { var element = {} element.componentItemId = i; element.custpage_mnf_col_componentquantity = NSUtil.forceFloat(objForDisplay[i][0].custpage_mnf_col_componentquantity) + NSUtil.forceFloat(element.custpage_mnf_col_componentquantity); element.custpage_mnf_col_totalcomponentqty = NSUtil.forceFloat(objForDisplay[i][0].custpage_mnf_col_totalcomponentqty) + NSUtil.forceFloat(element.custpage_mnf_col_totalcomponentqty); element.custpage_mnf_col_componentitemdesc = objForDisplay[i][0].custpage_mnf_col_componentitemdesc; element.custpage_mnf_col_componentohnand = objForDisplay[i][0].custpage_mnf_col_componentohnand; element.custpage_mnf_col_componentavailable = objForDisplay[i][0].custpage_mnf_col_componentavailable; element.custpage_mnf_col_poquantity = objForDisplay[i][0].custpage_mnf_col_poquantity; element.custpage_mnf_col_quantityexpected = objForDisplay[i][0].custpage_mnf_col_quantityexpected; if (typeof objForDisplayComponentTotals[i] === 'undefined') objForDisplayComponentTotals[i] = []; objForDisplayComponentTotals[i].push(element); } } log.debug(stLogTitle, 'objForDisplayComponentTotals : ' + JSON.stringify(objForDisplayComponentTotals)); log.debug(stLogTitle, 'intCounter : ' + JSON.stringify(intCounter)); for (var i in objForDisplayComponentTotals) { objSublist.setSublistValue({ id: COL_PREFIX + 'componentitemid', line: intCounter, value: i }); objSublist.setSublistValue({ id: COL_PREFIX + 'componentquantity', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_componentquantity || 0 }); objSublist.setSublistValue({ id: COL_PREFIX + 'totalcomponentqty', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_totalcomponentqty || 0 }); objSublist.setSublistValue({ id: COL_PREFIX + 'componentitemdesc', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_componentitemdesc || '' }); objSublist.setSublistValue({ id: COL_PREFIX + 'componentohnand', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_componentohnand || 0 }); objSublist.setSublistValue({ id: COL_PREFIX + 'componentavailable', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_componentavailable || 0 }); objSublist.setSublistValue({ id: COL_PREFIX + 'poquantity', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_poquantity || 0 }); objSublist.setSublistValue({ id: COL_PREFIX + 'quantityexpected', line: intCounter, value: objForDisplayComponentTotals[i][0].custpage_mnf_col_quantityexpected || 0 }); intCounter++; } */ } } } log.debug(stLogTitle, '-- Exit --'); return objForm; } /** * Function performing actions connected with suitelet submission. * @function processSubmit * * @param {Object} * objParams - script parameters * @return {Object} * objForm - Suitelet form object */ function processSubmit(request) { var stLogTitle = LOG_TITLE + 'processSubmit'; log.debug(stLogTitle, '-- Entry --'); var objParams = request.parameters; var arrInvoices = []; var stId = ''; var blChecked = undefined; log.debug(stLogTitle, 'request: ' + JSON.stringify(request)); // Count lines var intLines = request.getLineCount({ group: 'custpage_mnf_sublist_invoices' }); // For each line for (var i = 0; i < intLines; i++) { // Get value of checkbox blChecked = request.getSublistValue({ group: 'custpage_mnf_sublist_invoices', name: 'custpage_mnf_col_invoice_select', line: i }); log.debug(stLogTitle, 'Line: ' + i + ' | checked: ' + blChecked); // If checked, puhs into array if (blChecked == 'T') { stId = request.getSublistValue({ group: 'custpage_mnf_sublist_invoices', name: 'custpage_mnf_col_id', line: i }); arrInvoices.push(stId); } } log.debug(stLogTitle, 'arrInvoices: ' + JSON.stringify(arrInvoices)); // Process each invoice from array arrInvoices.forEach(function (element) { processSingleInvoice(element); }); log.debug(stLogTitle, '-- Exit --'); } /** * Function performing operation on single invoice - offsetting with credit memo, creating new 3rd party invoice, updating fields on transactions * @function processSingleInvoice * * @param {string} * stId - invoice id * @return {Void} */ function processSingleInvoice(stId) { var stLogTitle = LOG_TITLE + 'processSingleInvoice'; log.debug(stLogTitle, '-- Entry --'); log.audit(stLogTitle, 'Processing invoice ' + stId); var recCreditMemo = undefined; var recNewInvoice = undefined; var stCreditMemo = ''; var stNewInvoice = ''; var st3rdParty = ''; var stSalesOrder = ''; var objLinks = undefined; // var recInvoic // Transform invoice into credit memo recCreditMemo = record.transform({ fromType: record.Type.INVOICE, fromId: stId, toType: record.Type.CREDIT_MEMO, isDynamic: true, }); // Get value of 3rd party st3rdParty = recCreditMemo.getValue({ fieldId: 'custbody_mnf_3rd_party_payer' }); // Get value of sales order stSalesOrder = recCreditMemo.getValue({ fieldId: 'custbody_mnf_inv_src_tran' }); log.debug(stLogTitle, 'st3rdParty: ' + st3rdParty); // Save credit memo stCreditMemo = recCreditMemo.save(); log.audit(stLogTitle, 'Saving Credit Memo: ' + stCreditMemo + ' | From invoice ' + stId); // Create copy of original invoice recNewInvoice = record.copy({ type: record.Type.INVOICE, id: stId, isDynamic: false, ignoreFieldChange: true //Added by Manoj to resolve reset of line amounts. }); // Change the entity recNewInvoice.setValue({ fieldId: 'entity', value: st3rdParty }); // Save the new invoice stNewInvoice = recNewInvoice.save(); log.audit(stLogTitle, 'Saving 3rd Party Invoice: ' + stNewInvoice + ' | From invoice ' + stId); // Init object holding custom field keys and transactions to be updated objLinks = { links: { custbody_mnf_org_invoice_number: stId, custbody_mnf_sales_order_number: stSalesOrder, custbody_mnf_credit_memo_invoice: stCreditMemo, custbody_mnf_3rd_party_invoice: stNewInvoice }, transactions: [ {type: record.Type.INVOICE, id: stId}, {type: record.Type.INVOICE, id: stNewInvoice}, {type: record.Type.SALES_ORDER, id: stSalesOrder}, {type: record.Type.CREDIT_MEMO, id: stCreditMemo}, ] } // Invoke updateTransactionLinks updateTransactionLinks(objLinks); log.debug(stLogTitle, '-- Exit --'); } /** * Function updating reference fields on processed transactions * @function updateTransactionLinks * * @param {Object} * objLinks - holding values to be updated and transactions to be updated * @return {Void} */ function updateTransactionLinks(objLinks) { var stLogTitle = LOG_TITLE + 'updateTransactionLinks'; log.debug(stLogTitle, '-- Entry --'); var recCreditMemo = undefined; var recNewInvoice = undefined; var stCreditMemo = ''; var stNewInvoice = ''; var st3rdParty = ''; var stSalesOrder = ''; log.debug(stLogTitle, 'objLinks: ' + JSON.stringify(objLinks)); objLinks.transactions.forEach(function (element) { record.submitFields({ type: element.type, id: element.id, values: objLinks.links, }); }); log.debug(stLogTitle, '-- Exit --'); } /** * Function searching for invoices based on provided parameter * @function searchInvoices * * @param {Object} * objParams - script parameters * @param {Object} * objSearch - search object * @return {Array} * arrResult - invoices array */ function searchInvoices(objSearch, objParams) { var stLogTitle = LOG_TITLE + 'searchInvoices'; log.debug(stLogTitle, '-- Entry --'); var arrResult = []; if (!isEmpty(objParams.custpage_mnf_fld_customer)) { var objFilter = search.createFilter({ name: 'manufacturer', join: 'item', operator: search.Operator.CONTAINS, values: objParams.custpage_mnf_fld_customer }); objSearch.filters.push(objFilter); } if (!isEmpty(objParams.custpage_mnf_fld_trandate)) { var objFilter = search.createFilter({ name: 'trandate', operator: search.Operator.ONORBEFORE, values: objParams.custpage_mnf_fld_trandate }); objSearch.filters.push(objFilter); } if (!isEmpty(objParams.custpage_mnf_fld_due_date)) { var objFilter = search.createFilter({ name: 'duedate', operator: search.Operator.ONORBEFORE, values: objParams.custpage_mnf_fld_due_date }); objSearch.filters.push(objFilter); } var arrItemList = []; // Run search objSearch.run().each(function (element) { //log.debug(stLogTitle, 'element: ' + JSON.stringify(element)); var objResultItem = {}; objResultItem.id = element.id; objSearch.columns.forEach(function (column) { column = JSON.parse(JSON.stringify(column)); //log.audit(stLogTitle, 'column: ' + JSON.stringify(column) + ' ' + '| ui.FieldType.SELECT: ' + ui.FieldType.SELECT + " column.type " + column.type); var name = column.label.split('-')[0] //log.audit("column label", column.label) if (column.type.toUpperCase() == ui.FieldType.SELECT) { objResultItem[name] = element.getText(column); } else if (column.type.toUpperCase() == ui.FieldType.CHECKBOX) { objResultItem[name] = element.getValue(column) || ''; } else { objResultItem[name] = element.getValue(column); } }); arrItemList.push(parseInt(element.getText(objSearch.columns[0]))); //log.audit("objResultItem", JSON.stringify(objResultItem)) arrResult.push(objResultItem); return true; }); //dp: load item search var itemSe = search.load('customsearch_nsacs_lf_cprs'); if (!isEmpty(objParams.custpage_mnf_fld_customer)) { var objFilter = search.createFilter({ name: 'manufacturer', operator: search.Operator.CONTAINS, values: objParams.custpage_mnf_fld_customer }); itemSe.filters.push(objFilter); } //log.audit('arrItemList',arrItemList); if(arrItemList.length > 0) { //log.audit('ctArr_true',arrItemList.length); itemSe.filters.push(search.createFilter({ name: 'internalid', operator: search.Operator.NONEOF, values: arrItemList })); } itemSe.run().each(function (element) { //log.debug(stLogTitle, 'element: ' + JSON.stringify(element)); var objResultItem = {}; objResultItem.id = element.id; itemSe.columns.forEach(function (column) { column = JSON.parse(JSON.stringify(column)); // console.log('test', 'column: ' + JSON.stringify(column)) var name = column.label.split('-')[0] //objResultItem[name] = element.getValue(column); log.audit(stLogTitle, 'column: ' + JSON.stringify(column) + ' ' + '| ui.FieldType.SELECT: ' + ui.FieldType.SELECT + " column.type " + column.type); if (column.type) { if (column.type.toUpperCase() == ui.FieldType.SELECT) { objResultItem[name] = element.getText(column); } else if (column.type.toUpperCase() == ui.FieldType.CHECKBOX) { objResultItem[name] = element.getValue(column) || ''; } else { objResultItem[name] = element.getValue(column); } } }); if(arrItemList.indexOf(element.getText(objSearch.columns[0])) == -1) arrResult.push(objResultItem); return true; }); log.audit("arrResult : ", JSON.stringify(arrResult)) arrResult.sort(function(a, b) { var keyA = (a['item']), keyB = (b['item']); // Compare the 2 dates if (keyA < keyB) return -1; if (keyA > keyB) return 1; return 0; }); log.debug(stLogTitle, '-- Exit --'); return arrResult; } return { onRequest: onRequest }; });