-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcostedBOMrevision.suitelet.js
292 lines (233 loc) · 8.58 KB
/
costedBOMrevision.suitelet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope Public
*/
/*
Script Info
---Costed BOM Revision Inquiry---
A Suitelet utility for comparing Set/Average/LPP cost of Assembly Items
Tim Dietrich
queryModule and SuiteQL
timdietrich@me.com
https://timdietrich.me
Scott Danesi
Costed BOM SuiteQL
https://www.scottdanesi.com/?p=5019
Compiled by crimes-johnson
https://github.com/crimes-johnson
11-05-2020: Initial Suitelet by Tim Dietrich
09-27-2024: Compiled by Crimes Johnson
11-05-2024: Updated to fix a change by NetSuite to BOM records
*/
var
log,
query,
serverWidget,
historyRows = 100;
define( [ 'N/log', 'N/query', 'N/ui/serverWidget' ], main );
function main( logModule, queryModule, serverWidgetModule ) {
// Set module references.
log = logModule;
query= queryModule;
serverWidget = serverWidgetModule;
return {
onRequest: function( context ) {
// Create a form.
var form = serverWidget.createForm(
{
title: 'Costed BOM Revision Inquiry',
hideNavBar: false
}
);
// Add a submit button.
form.addSubmitButton( { label: 'Get Cost' } );
// Add a "BOM Revision ID" field.
var revField = form.addField(
{
id: 'custpage_field_revid',
type: serverWidget.FieldType.TEXT,
label: 'Revision Name'
}
);
// Make the search field mandatory.
revField.isMandatory = true;
// If the form has been submitted...
if ( context.request.method == 'POST' ) {
// Set defaults for the item and date field values.
revField.defaultValue = context.request.parameters.custpage_field_revid;
// Process the form.
formProcess( context, form );
}
// Display the form.
context.response.writePage( form );
}
}
}
// SuiteQL query to submit
function formProcess( context, form ) {
var theQuery = '';
theQuery += 'SELECT ';
theQuery += 't2.fullname as "assembly name", ';
theQuery += 't2.displayName as "assembly description", ';
theQuery += 'bomRevision.name as "bom revision", ';
theQuery += 'item.itemId as "component Name", ';
theQuery += 'BomRevisionComponent.description as "description", ';
theQuery += 'BomRevisionComponent.bomQuantity as "quantity", ';
theQuery += 'to_char( item.cost, \'$9,999.99\' ) as "set cost", ';
theQuery += 'to_char( CASE WHEN item.averageCost = 0 THEN item.cost ELSE item.averageCost END, \'$9,999.99\' ) as "average cost", ';
theQuery += 'to_char( BomRevisionComponent.bomQuantity * CASE WHEN item.averageCost = 0 THEN item.cost ELSE item.averageCost END, \'$9,999.99\' ) as "avg cost total", ';
theQuery += 'to_char( CASE WHEN item.lastPurchasePrice = 0 THEN item.cost ELSE item.averageCost END, \'$9,999.99\' ) as "last purchase price", ';
theQuery += 'to_char( BomRevisionComponent.bomQuantity * CASE WHEN item.lastPurchasePrice = 0 THEN item.cost ELSE item.averageCost END, \'$9,999.99\' ) as "last purchase total", ';
theQuery += 't1.altName as "preferred vendor" ';
theQuery += 'FROM ';
theQuery += 'BomRevisionComponent ';
theQuery += 'LEFT JOIN item ON BomRevisionComponent.item = item.id ';
theQuery += 'LEFT JOIN bomRevision ON BomRevisionComponent.bomrevision = bomRevision.id ';
theQuery += 'LEFT JOIN ';
theQuery += '( ';
theQuery += 'SELECT ';
theQuery += 'itemVendor.item, ';
theQuery += 'itemVendor.preferredVendor, ';
theQuery += 'itemVendor.vendor, ';
theQuery += 'Vendor.altname ';
theQuery += 'FROM ';
theQuery += 'itemVendor ';
theQuery += 'LEFT JOIN Vendor ON itemVendor.vendor = Vendor.id ';
theQuery += 'WHERE ';
theQuery += 'itemVendor.preferredVendor = \'T\' ';
theQuery += ') as t1 ';
theQuery += 'ON t1.item = item.id ';
theQuery += 'LEFT JOIN ';
theQuery += '( ';
theQuery += 'SELECT ';
theQuery += 'item.fullName, ';
theQuery += 'item.displayName, ';
theQuery += 'item.id, ';
theQuery += 'bomRevision.name ';
theQuery += 'FROM ';
theQuery += 'item ';
theQuery += 'JOIN itemAssemblyItemBom ON item.id = itemAssemblyItemBom.assembly '; //NetSuite changed the default record from bomAssembly to itemAssemblyItemBom in 2024.2 release
theQuery += 'JOIN bom ON itemAssemblyItemBom.billofmaterials = bom.id ';
theQuery += 'JOIN bomRevision ON bomRevision.billofmaterials = bom.id ';
theQuery += ') as t2 ';
theQuery += 'ON t2.name = bomRevision.name ';
theQuery += 'WHERE bomRevision .name = \'' + context.request.parameters.custpage_field_revid + '\' ';
theQuery += 'UNION ';
theQuery += 'SELECT ';
theQuery += 'NULL as "assembly name", ';
theQuery += 'NULL as "assembly description", ';
theQuery += 'NULL as "bom revision", ';
theQuery += 'NULL as "component name", ';
theQuery += '\'Total Cost\' as "description", ';
theQuery += 'NULL as "quantity", ';
theQuery += 'to_char( SUM(BomRevisionComponent.bomQuantity * item.cost), \'$9,999.99\' ) as "set cost total", ';
theQuery += 'NULL as "average cost", ';
theQuery += 'to_char( SUM(BomRevisionComponent.bomQuantity * CASE WHEN item.averageCost = 0 THEN item.cost ELSE item.averageCost END), \'$9,999.99\' ) as "avg cost total", ';
theQuery += 'NULL as "last Purchase Price", ';
theQuery += 'to_char( SUM(BomRevisionComponent.bomQuantity * CASE WHEN item.lastPurchasePrice = 0 THEN item.cost ELSE item.averageCost END), \'$9,999.99\' ) as "last purchase total", ';
theQuery += 'NULL as "preferred vendor" ';
theQuery += 'FROM ';
theQuery += 'BomRevisionComponent ';
theQuery += 'LEFT JOIN item ON BomRevisionComponent.item = item.id ';
theQuery += 'LEFT JOIN bomRevision ON BomRevisionComponent.bomrevision = bomRevision.id ';
theQuery += 'WHERE bomRevision .name = \'' + context.request.parameters.custpage_field_revid + '\' ';
try {
// Run the query.
var queryResults = query.runSuiteQL(
{
query: theQuery
}
);
// Get the mapped results.
var records = queryResults.asMappedResults();
// If records were returned...
if ( records.length > 0 ) {
// Create a sublist for the results.
var resultsSublist = form.addSublist(
{
id : 'results_sublist',
label : 'Revision Components List',
type : serverWidget.SublistType.LIST
}
);
// Get the column names.
var columnNames = Object.keys( records[0] );
// Loop over the column names...
for ( i = 0; i < columnNames.length; i++ ) {
// Add the column to the sublist as a field.
resultsSublist.addField(
{
id: 'custpage_results_sublist_col_' + i,
type: serverWidget.FieldType.TEXT,
label: columnNames[i]
}
);
}
// Add the records to the sublist...
for ( r = 0; r < records.length; r++ ) {
// Get the record.
var record = records[r];
// Loop over the columns...
for ( c = 0; c < columnNames.length; c++ ) {
// Get the column name.
var column = columnNames[c];
// Get the column value.
var value = record[column];
// If the column has a value...
if ( value != null ) {
// Get the value as a string.
value = value.toString();
// If the value is too long to be displayed in the sublist...
if ( value.length > 300 ) {
// Truncate the value.
value = value.substring( 0, 297 ) + '...';
}
// Add the column value.
resultsSublist.setSublistValue(
{
id : 'custpage_results_sublist_col_' + c,
line : r,
value : value
}
);
}
}
}
} else {
// Add an "Error" field.
var errorField = form.addField(
{
id: 'custpage_field_error',
type: serverWidget.FieldType.TEXT,
label: 'Error'
}
);
errorField.defaultValue = 'No revision found for: ' + context.request.parameters.custpage_field_revid;
// Add an inline HTML field so that JavaScript can be injected.
var jsField = form.addField(
{
id: 'custpage_field_js',
type: serverWidget.FieldType.INLINEHTML,
label: 'Javascript'
}
);
// Add Javascript to make the error field red.
jsField.defaultValue = '<script>\r\n';
jsField.defaultValue += 'document.addEventListener(\'DOMContentLoaded\', function() {';
jsField.defaultValue += 'document.getElementById("custpage_field_error").style.background="red";\r\n';
jsField.defaultValue += 'document.getElementById("custpage_field_error").style.color="white";\r\n';
jsField.defaultValue += '}, false);';
jsField.defaultValue += '</script>';
}
} catch( e ) {
var errorField = form.addField(
{
id: 'custpage_field_error',
type: serverWidget.FieldType.LONGTEXT,
label: 'Error'
}
);
errorField.defaultValue = e.message;
}
}