-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsales_order_client.ts
333 lines (313 loc) · 9.94 KB
/
sales_order_client.ts
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
import { EntryPoints } from 'N/types';
import * as currentRecord from 'N/currentRecord';
import * as record from 'N/record';
import * as dialog from 'N/ui/dialog';
import * as log from 'N/log';
/**
* A client script to calculate shipping and handling costs.
*/
export const pageInit: EntryPoints.Client.pageInit = () => {
console.log('Client Script Loaded');
};
export const fieldChanged: EntryPoints.Client.fieldChanged = (
context: EntryPoints.Client.fieldChangedContext
) => {
const salesRecord = currentRecord.get();
const channel = salesRecord.getValue('custbody_fa_channel');
const shipCountry = salesRecord.getValue('shipcountry');
if (channel == '' && shipCountry == 'US') {
const shipMethod: record.FieldValue = salesRecord.getValue('shipmethod');
// Amazon FBA (21719), In Store Pickup (22004), Will Call (21989), Freight (22022)
if (
shipMethod !== 21719 &&
shipMethod !== 22004 &&
shipMethod !== 21989 &&
shipMethod !== 22022
) {
// on shipping cost change
if (context.fieldId == 'shippingcost') {
// calculate handling
calculateHandling();
// get cost for total
const shippingCost: record.FieldValue =
salesRecord.getValue('shippingcost');
const handlingCost: record.FieldValue =
salesRecord.getValue('handlingcost');
// add total
let total =
parseFloat(String(shippingCost)) + parseFloat(String(handlingCost));
total = round(total, 2);
salesRecord.setValue('custbody_sp_total_shipping_cost', total);
}
// on handling cost change
if (context.fieldId == 'handlingcost') {
const shippingCost: record.FieldValue =
salesRecord.getValue('shippingcost');
const handlingCost: record.FieldValue =
salesRecord.getValue('handlingcost');
// add total
let total =
parseFloat(String(shippingCost)) + parseFloat(String(handlingCost));
total = round(total, 2);
salesRecord.setValue('custbody_sp_total_shipping_cost', total);
}
}
}
};
const round = (value: number, decimals: number) => {
return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals);
};
export const calculateHandling = () => {
const salesRecord = currentRecord.get();
const channel = salesRecord.getValue('custbody_fa_channel');
const shipCountry = salesRecord.getValue('shipcountry');
// check for marketplace
if (channel === '' && shipCountry === 'US') {
// check for ship method returns ID
const shipMethod = salesRecord.getValue('shipmethod');
if (shipMethod && shipMethod !== '') {
const shipState = salesRecord.getValue('shipstate');
if (shipState && shipState !== '') {
const UsRegion1 = ['AZ', 'CA', 'ID', 'NV', 'UT'];
const UsRegion2 = [
'AR',
'CO',
'KS',
'MO',
'MT',
'NE',
'NM',
'OK',
'OR',
'TX',
'WA',
'WY',
];
const UsRegion3 = [
'AL',
'DE',
'FL',
'GA',
'IL',
'IN',
'IA',
'KY',
'LA',
'MD',
'MA',
'MI',
'MN',
'MS',
'NC',
'ND',
'OH',
'SC',
'SD',
'TN',
'VA',
'WV',
'WI',
];
const UsRegion4 = [
'CT',
'DC',
'ME',
'NH',
'NJ',
'NY',
'RI',
'VT',
'GU',
'VI',
];
const UsRegion5 = ['AK', 'HI', 'PR', 'AS'];
const shippingCost: record.FieldValue =
salesRecord.getValue('shippingcost');
let handlingCost = 0;
if (UsRegion1.includes(String(shipState))) {
handlingCost = Number(shippingCost) * 0.8;
} else if (UsRegion2.includes(String(shipState))) {
handlingCost = Number(shippingCost) * 0.7;
} else if (UsRegion3.includes(String(shipState))) {
handlingCost = Number(shippingCost) * 0.6;
} else if (UsRegion4.includes(String(shipState))) {
handlingCost = Number(shippingCost) * 0.3;
} else if (UsRegion5.includes(String(shipState))) {
handlingCost = Number(shippingCost) * 0.3;
} else {
handlingCost = 0.0;
}
// round handling cost
handlingCost = round(handlingCost, 2);
// log
const logData = {
'Shipping Method': shipMethod,
'Ship State': shipState,
'Handling Cost': '$' + handlingCost,
};
log.debug({
title: 'Calculating and Setting Handling Cost',
details: logData,
});
salesRecord.setValue('handlingcost', handlingCost);
}
} else {
log.error({
title: 'Shipping Method is not selected',
details: 'Shipping Method is not selected',
});
dialog.alert({
title: 'Ship Method Error',
message: 'Error: Please select a Ship Method',
});
}
} else {
log.error({
title: 'Handling Cost Error',
details:
'Sales Channel ' +
salesRecord.getValue('custbody_fa_channel') +
', FarApp has already added the handling cost to the shipping cost. Please do not add it again.',
});
dialog.alert({
title: 'Handling Cost Error',
message:
'Error: Sales Channel ' +
salesRecord.getValue('custbody_fa_channel') +
', FarApp has already added the handling cost to the shipping cost. Please do not add it again.',
});
}
};
export const calculateTotalWeight = () => {
try {
console.log('starting calculation / conversion...');
// get record
const salesRecord = currentRecord.get();
// get line count
const lines = salesRecord.getLineCount({ sublistId: 'item' });
let totalWeight = 0;
let totalItems = 0;
for (let i = 0; i < lines; i++) {
console.log('checking line: ' + i);
// get weight unit (lb, oz, kg, g)
let quantity = salesRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i,
});
let weight = salesRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_sp_item_weight',
line: i,
});
// check if line item has quantity
// custom line items like discount and subtotal should not -- these will be skipped
if (quantity && weight) {
const unit = salesRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_sp_item_weight_units',
line: i,
});
console.log('converting ' + unit + ' to lb...');
if (unit === 'oz') {
// convert oz to lbs
weight = Number(weight) * 0.0625;
} else if (unit === 'kg') {
// convert oz to kg
weight = Number(weight) * 2.20462;
} else if (unit === 'g') {
// convert oz to g
weight = Number(weight) * 0.00220462;
} else {
weight = Number(weight) * 1;
}
// set line weight
console.log('setting converted item weight');
const convertedWeight = round(weight, 3);
const currentLine = salesRecord.selectLine({
sublistId: 'item',
line: i,
});
currentLine.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_sp_converted_item_weight',
value: convertedWeight,
ignoreFieldChange: false,
});
salesRecord.commitLine({ sublistId: 'item' });
// calculate line weight
const lineWeight = weight * Number(quantity);
// calculate total weight
totalWeight = totalWeight + lineWeight;
// totalWeight = round(totalWeight, 2);
// // set fields
// salesRecord.setValue({ fieldId: 'custbody_sp_total_items_weight', value: totalWeight });
const itemType = salesRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_sp_item_type',
line: i,
});
// load item record
if (itemType === 'Kit/Package') {
console.log(
'item is of type kit/package looking at components to get accurate item quantities...'
);
const itemId = salesRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_sp_item_id',
line: i,
});
const loadedItem = record.load({
type: record.Type.KIT_ITEM,
id: Number(itemId),
});
const components = loadedItem.getLineCount({ sublistId: 'member' });
let totalComponents = 0;
for (let j = 0; j < components; j++) {
console.log('checking component ' + j);
const componentQuantity = loadedItem.getSublistValue({
sublistId: 'member',
fieldId: 'quantity',
line: j,
});
totalComponents += Number(componentQuantity);
}
// calculate total item count for kit items
quantity = Number(quantity) * totalComponents;
}
// calculate total item count
totalItems = totalItems + Number(quantity);
}
}
// set total weight
totalWeight = round(totalWeight, 2);
// set fields
salesRecord.setValue({
fieldId: 'custbody_sp_total_items_weight',
value: totalWeight,
});
// set total item count
salesRecord.setValue({
fieldId: 'custbody_sp_total_items',
value: totalItems,
});
console.log(
'Total order weight: ' +
totalWeight +
' lb(s) | Total order item count: ' +
totalItems
);
console.log('DONE!');
dialog.alert({
title: 'Completed',
message:
'Calculation / Conversion has been completed. Any errors will be logged in console.',
});
} catch (e) {
console.log(e.message);
}
};