Untitled
unknown
plain_text
2 years ago
22 kB
3
Indexable
Never
/** * @NApiVersion 2.0 * @NScriptType Suitelet * @NModuleScope Public * @Author TC025 Gino * @Description Suilet UI for computing penalty of utility * Version Date Author Remarks * 1.00 Jun 15 2022 TC025 Gino Initial Version */ define([ 'N/search', 'N/record', 'N/ui/serverWidget', 'N/redirect', 'N/runtime', 'N/config', 'N/format', 'N/redirect', 'N/file', 'N/task', './moment' ], function (search, record, ui, redirect, runtime, config, format, redirect, file, task, moment){ /** * @Description Suitelet entry point * @SavedSearch n/a * @Parameters {context} context user event script context object * @Returns n/a */ function onRequest (context) { var request = context.request; var response = context.response; var userObj = runtime.getCurrentUser(); var scriptObj = runtime.getCurrentScript(); var params = request.parameters; var form = ui.createForm({ title: 'Compute Penalty' }); if (request.method === 'GET') { //Setting up of Filter Fields form.clientScriptModulePath = './025CS_utilityPenaltyUITriggers.js'; form.addFieldGroup({id: 'filtersgroup',label: 'Filters'}); form.addButton({id: 'custpage_btn_applyfilters', label: 'Apply Filters', functionName: 'applyFilters'}) form.addButton({id: 'custpage_btn_resetfilters', label: 'Reset Filters', functionName: 'resetFilters'}); form.addSubmitButton({label: 'Compute and Create Penalty Invoice'}) var casField = form.addField({id: 'custpage_cas',label: 'CAS Reference',type: 'SELECT',container: 'filtersgroup'}); var busField = form.addField({id: 'custpage_class',label: 'Business Unit',type: 'SELECT',container: 'filtersgroup', source: 'classification'}); casField.isMandatory = true; busField.isMandatory = true; if(!!params.cas || !!params.class){ casField.defaultValue = params.cas; busField.defaultValue = params.class; }; var casList = getCasList(); casList.forEach( function(v, i) { casField.addSelectOption({ value: v.internalId, text: v.docname }); }); //End of Setting Filter Fields //Setting up of Sublist var utilDetailSublist = form.addSublist({id: 'utilitydetailssublist', label: 'Utility Details', type: 'LIST'}); utilDetailSublist.addField({id: 'custpage_sub_checked',label: 'Compute',type: 'CHECKBOX'}); utilDetailSublist.addField({id: 'custpage_sub_invintid',label: 'Invoice Internal Id',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_casref',label: 'Customer Account Reference',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_busunit',label: 'Business Unit',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_meter',label: 'Meter',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_customer',label: 'Customer',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_startdate',label: 'Start Date',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_enddate',label: 'End Date',type: 'TEXT'}); utilDetailSublist.addField({id: 'custpage_sub_amtrem',label: 'Amount Remaining',type: 'CURRENCY'}); utilDetailSublist.addField({id: 'custpage_sub_penaltytoday',label: 'Penalty as of Today',type: 'CURRENCY'}); utilDetailSublist.addField({id: 'custpage_sub_location',label: 'Loc ID',type: 'TEXT'}).updateDisplayType({displayType: ui.FieldDisplayType.HIDDEN}); utilDetailSublist.addField({id: 'custpage_sub_casrefid',label: 'CAS Ref Id',type: 'TEXT'}).updateDisplayType({displayType: ui.FieldDisplayType.HIDDEN}); utilDetailSublist.addField({id: 'custpage_sub_busunitid',label: 'Business Unit Id',type: 'TEXT'}).updateDisplayType({displayType: ui.FieldDisplayType.HIDDEN}); utilDetailSublist.addField({id: 'custpage_sub_meterid',label: 'Meter Id',type: 'TEXT'}).updateDisplayType({displayType: ui.FieldDisplayType.HIDDEN}); utilDetailSublist.addField({id: 'custpage_sub_customerid',label: 'Customer Id',type: 'TEXT'}).updateDisplayType({displayType: ui.FieldDisplayType.HIDDEN}); if(!!params.cas && !!params.class){ var relatedInvoicesArr = getRelatedInvoices(params.cas,params.class); relatedInvoicesArr.forEach( function(v, i) { log.error('v.locationId',v.locationId) utilDetailSublist.setSublistValue({id: 'custpage_sub_invintid', line: i, value: v.internalId}); utilDetailSublist.setSublistValue({id: 'custpage_sub_casref', line: i, value: v.casRefName}); utilDetailSublist.setSublistValue({id: 'custpage_sub_busunit', line: i, value: v.className}); utilDetailSublist.setSublistValue({id: 'custpage_sub_meter', line: i, value: v.meterTypeInvName}); utilDetailSublist.setSublistValue({id: 'custpage_sub_customer', line: i, value: v.customerName}); utilDetailSublist.setSublistValue({id: 'custpage_sub_startdate', line: i, value: v.startDate}); utilDetailSublist.setSublistValue({id: 'custpage_sub_enddate', line: i, value: v.endDate}); utilDetailSublist.setSublistValue({id: 'custpage_sub_amtrem', line: i, value: v.amountInv}); utilDetailSublist.setSublistValue({id: 'custpage_sub_penaltytoday', line: i, value: v.penaltyAmt}); //Hidden Fields utilDetailSublist.setSublistValue({id: 'custpage_sub_location', line: i, value: v.locationId}); utilDetailSublist.setSublistValue({id: 'custpage_sub_casrefid', line: i, value: params.cas}); utilDetailSublist.setSublistValue({id: 'custpage_sub_busunitid', line: i, value: v.classId}); utilDetailSublist.setSublistValue({id: 'custpage_sub_meterid', line: i, value: v.meterTypeInv}); utilDetailSublist.setSublistValue({id: 'custpage_sub_customerid', line: i, value: v.customerId}); }); utilDetailSublist.addMarkAllButtons(); }; //End of Setting up sublist response.writePage(form); }else{ //POST Request Start log.error('post request.parameters',params) var bulkObj = {}; var dataToConvert = []; var casRefList = []; var userObj = runtime.getCurrentUser(); var sublistCount = request.getLineCount({ group: 'utilitydetailssublist' }); var companyInfRec = config.load({ type: config.Type.COMPANY_INFORMATION }); var busUnitRec = record.load({ type: 'classification', id: params.custpage_class }) var elecItemId = busUnitRec.getValue('custrecord_sol_bu_electricitem'); var waterItemId = busUnitRec.getValue('custrecord_sol_bu_wateritem'); var penaltyItemId = companyInfRec.getValue('custrecord_company_penaltyitempref'); for(var i = 0; sublistCount > i; i++){ var convertLine = request.getSublistValue({ group: 'utilitydetailssublist', name: 'custpage_sub_checked', line: i }); if(convertLine == true || convertLine == 'T'){ var meterType = request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_meterid', line: i }) if(meterType == 1){ itemId = elecItemId; }else{ itemId = waterItemId; }; log.error('loc',request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_location', line: i })) var invObj = { mainLine: { class: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_busunitid', line: i }), startdate: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_startdate', line: i }), enddate: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_enddate', line: i }), duedate: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_enddate', line: i }), customer: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_customerid', line: i }), location: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_location', line: i }), casref: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_casrefid', line: i }), custbody_sol_inv_casref: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_casrefid', line: i }), custbody_sol_inv_metertype : meterType, }, itemLine: { item: penaltyItemId, amount: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_penaltytoday', line: i }), }, recToUpdate: { casRec: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_casrefid', line: i }), mainInvoice: request.getSublistValue({group: 'utilitydetailssublist', name: 'custpage_sub_invintid', line: i }), } }; bulkObj.proj = invObj.mainLine.location; bulkObj.class = invObj.mainLine.class; bulkObj.sdate = invObj.mainLine.startdate; bulkObj.edate = invObj.mainLine.enddate; casRefList.push(invObj.recToUpdate.casRec); dataToConvert.push(invObj); }; }; log.error('dataToConvert',dataToConvert); if(dataToConvert.length < 1){ throw 'Choose at least one line to convert.' }else{ //Arrange Data to Convert into same CAS Ref // var groupPerCasRef = {}; // dataToConvert.forEach( function(v, i) { // if(groupPerCasRef.hasOwnProperty(v.mainLine.casref)){ // groupPerCasRef[v.mainLine.casref].itemLine.amount = parseFloat(groupPerCasRef[v.mainLine.casref].itemLine.amount) + parseFloat(v.itemLine.amount); // groupPerCasRef[v.mainLine.casref].recToUpdate.mainInvoice.push(v.recToUpdate.mainInvoice); // }else{ // groupPerCasRef[v.mainLine.casref] = v // var mainInvoiceArr = []; // mainInvoiceArr.push(groupPerCasRef[v.mainLine.casref].recToUpdate.mainInvoice); // groupPerCasRef[v.mainLine.casref].recToUpdate.mainInvoice = mainInvoiceArr; // } // }); // log.error('groupPerCasRef',groupPerCasRef) // //Re-assemble them to dataToConvert Array // dataToConvert = []; // Object.keys(groupPerCasRef).forEach( function(element, index) { // dataToConvert.push(groupPerCasRef[element]); // }); // log.error('dataToConvert grouped per CAS',dataToConvert); // log.error('dataToConvert[0].mainLine.class',dataToConvert[0].mainLine.class) var bulkInvoiceRec = record.create({ type: 'customrecord_solar_bulkinvoicesummary', isDynamic: false, }); var today = new Date(); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_businessunit', value: dataToConvert[0].mainLine.class}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_project', value: dataToConvert[0].mainLine.location}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_startdate', value: format.parse({ value : dataToConvert[0].mainLine.startdate, type : format.Type.DATE})}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_enddate', value: format.parse({ value : dataToConvert[0].mainLine.enddate, type : format.Type.DATE})}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_scriptremarks', value: 'Bulk Invoice Generation is now processing. System will notify User via Email once backend script is done processing.'}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_generatedby', value: 8}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_generatedas', value: userObj.role}); bulkInvoiceRec.setValue({fieldId: 'custrecord_solar_bbs_generateddate', value: today}) var bulkId = bulkInvoiceRec.save(); log.error('userObj',userObj); dataToConvert[0].bulkRef = bulkId; dataToConvert[0].userName = userObj.name; dataToConvert[0].userRoleName = userObj.roleId.charAt(0).toUpperCase() + userObj.roleId.slice(1); dataToConvert[0].userId = userObj.id; dataToConvert[0].userRole = userObj.role; redirect.toRecord({ id: bulkId, type: 'customrecord_solar_bulkinvoicesummary', }); log.error('after redirect'); var txtFileToCreateDetails = file.create({ name: 'utilityToInoiveDetails', fileType: file.Type.JSON, contents: JSON.stringify(dataToConvert), }); // txtFileToCreateDetails.encoding = file.Encoding.MAC_ROMAN txtFileToCreateDetails.folder = 345; txtFileToCreateDetails.save(); var scriptTask = task.create({taskType: task.TaskType.SCHEDULED_SCRIPT}); scriptTask.scriptId = 'customscript_025_ss_createinvoices'; scriptTask.deploymentId = 'customdeploy_025_ss_createinvoices'; var scriptTaskId = scriptTask.submit(); }; }; }; // /** // * DONOT ALTER THIS FUNCTION // * Retrieves all(even if data is more than 2000) // * search results of an nlobjSearchResultSet // * // * @param {resultSet} set search result set to retrieve results // * @return {Array} array containing search results // */ var getResults = function(set) { var holder = []; var i = 0; while (true) { var result = set.getRange({ start: i, end: i + 1000 }); if (!result) break; holder = holder.concat(result); if (result.length < 1000) break; i += 1000; } return holder; }; function getCasList() { var query = search.load({ //[SCRIPT] CAS List id: 'customsearch_sol_caslist' }); var results = getResults(query.run()) results = results.map(function (x) { return { 'internalId': x.getValue(x.columns[0]), 'docname': x.getValue(x.columns[1]), } }) return results; }; function getUtilityDetails(params,periodCoverage) { var query = search.load({ //[SCRIPT] Utility Record for Invoice Conversion id: 'customsearch_025_utilityrecordforinvoice' }); query.filters.push( search.createFilter({ name: 'custrecord_sol_meterreading_businessunit', operator: search.Operator.ANYOF, join: 'CUSTRECORD_SOL_UTILITY_MRREF', values: params.class }) ); query.filters.push( search.createFilter({ name: 'custrecord_sol_utility_project', operator: search.Operator.ANYOF, values: params.project }) ); query.filters.push( search.createFilter({ name: 'custrecord_sol_meterreading_startdate', operator: search.Operator.ONORAFTER, join: 'custrecord_sol_utility_mrref', values: periodCoverage.start }) ); query.filters.push( search.createFilter({ name: 'custrecord_sol_meterreading_startdate', operator: search.Operator.ONORBEFORE, join: 'custrecord_sol_utility_mrref', values: periodCoverage.end }) ); query.filters.push( search.createFilter({ name: 'custrecord_sol_meterreading_startdate', operator: search.Operator.ONORBEFORE, join: 'custrecord_sol_utility_mrref', values: periodCoverage.end }) ); var results = getResults(query.run()) results = results.map(function (x) { return { 'internalId': x.getValue(x.columns[0]), 'mrRef': x.getValue(x.columns[1]) || ' ', 'mrRefName': x.getText(x.columns[1]) || ' ', 'casRef': x.getValue(x.columns[2]) || ' ', 'casRefName': x.getText(x.columns[2]) || ' ', 'meter': x.getValue(x.columns[3]) || ' ', 'meterName': x.getText(x.columns[3]) || ' ', 'meterType': x.getValue(x.columns[4]) || ' ', 'meterTypeName': x.getText(x.columns[4]) || ' ', 'customer': x.getValue(x.columns[5]) || ' ', 'customerName': x.getText(x.columns[5]) || ' ', 'businessUnit': x.getValue(x.columns[6]) || ' ', 'businessUnitName': x.getText(x.columns[6]) || ' ', 'startDate': x.getValue(x.columns[7]) || ' ', 'endDate': x.getValue(x.columns[8]) || ' ', 'dueDate': x.getValue(x.columns[9]) || ' ', 'billableAmt': x.getValue(x.columns[10]) || 0.00, 'invoiceRef': x.getValue(x.columns[11]) || ' ', 'unitTypeText': x.getText(x.columns[12]) || ' ', 'previousReading': x.getValue(x.columns[13]) || 0.00, 'currentReading': x.getValue(x.columns[14]) || 0.00, 'consumption': x.getValue(x.columns[15]) || 0.00, 'billableRate': x.getValue(x.columns[16]) || 0.00, 'totalConsumption': x.getValue(x.columns[17]) || 0.00, 'systemLossAmt': x.getValue(x.columns[18]) || 0.00, 'serviceCharge': x.getValue(x.columns[19]) || 0.00, 'billableAmt': x.getValue(x.columns[20]) || 0.00, 'vat': x.getValue(x.columns[21]) || 0.00, 'reconnectionFee': x.getValue(x.columns[22]) || 0.00, 'ttlAmount': x.getValue(x.columns[23]) || 0.00, 'systemLosskwh': x.getValue(x.columns[24]) || 0.00, 'penaltyAmt': x.getValue(x.columns[25]) || 0.00, 'projectText': x.getText(x.columns[26]) || 0.00, }; }) return results; }; function getRelatedInvoices(casRef,paramClass) { var query = search.load({ //[SCRIPT] Open Utility Invoices for Penalty id: 'customsearch_025_openutilityinvoicespen' }); query.filters.push( search.createFilter({ name: 'custbody_sol_inv_casref', operator: search.Operator.ANYOF, values: casRef }) ); query.filters.push( search.createFilter({ name: 'class', operator: search.Operator.ANYOF, values: paramClass }) ); var results = getResults(query.run()) results = results.map(function (x) { return { 'internalId' : x.getValue(x.columns[0]), 'classId': x.getValue(x.columns[1]) || ' ', 'className': x.getText(x.columns[1]) || ' ', 'locationId': x.getValue(x.columns[2]) || ' ', 'locationName': x.getText(x.columns[2]) || ' ', 'meterTypeInv': x.getValue(x.columns[3]) || ' ', 'meterTypeInvName': x.getText(x.columns[3]) || ' ', 'customerId': x.getValue(x.columns[4]) || ' ', 'customerName': x.getValue(x.columns[5]) || ' ', 'startDate': x.getValue(x.columns[6]) || ' ', 'endDate': x.getValue(x.columns[9]) || 0.00, 'amountInv': x.getValue(x.columns[8]) || 0.00, 'casRefId': x.getValue(x.columns[10]) || 0.00, 'casRefName': x.getValue(x.columns[10]) || 0.00, 'penaltyAmt': x.getValue(x.columns[11]) || 0.00, }; }) return results; }; function toSystemDate(date) { return format.format({ type: format.Type.DATETIME, value: date }); }; return { onRequest : onRequest }; });