-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_fulfillment_client.ts
556 lines (518 loc) · 16.9 KB
/
item_fulfillment_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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
import { EntryPoints } from 'N/types';
import * as currentRecord from 'N/currentRecord';
import * as dialog from 'N/ui/dialog';
import * as log from 'N/log';
/**
* A client script to automate the process of selecting shipping methods
*/
// Shipping Methods - Production
const fedEx2Day = '30611';
const uspsPriority = '22001';
const uspsFirstClass = '22000';
const uspsPriorityEnvelope = '31089';
const uspsPriorityLegalEnvelope = '31094';
const uspsPriorityMdFlatRateBox = '31136';
export let pageInit: EntryPoints.Client.pageInit = () => {
console.log('FedEx Client Script Loaded');
onLoad();
};
/**
* On page load it checks the item fulfillment ship method
* if method is FedEx 2 Day Express then it checks the status,
* depending on the status it automatically selects the
* appropriate fields.
*/
const onLoad = () => {
// on page load
const itemFulfill = currentRecord.get();
const status = itemFulfill.getValue('shipstatus');
// check if fedex express 2day
if (itemFulfill.getValue('shipmethod') === fedEx2Day) {
let msg;
let isShipNextDay = '';
let shipDay;
// check day
const date = new Date();
const day = date.getDay();
const hour = date.getHours();
const minutes = date.getMinutes();
console.log('Day: ' + day + ' | Hour: ' + hour);
try {
// if status picked or packed
if (status === 'A' || status === 'B') {
itemFulfill.setValue('shipstatus', 'C');
itemFulfill.setValue('generateintegratedshipperlabel', true);
// if thursday set saturday delivery
if (day === 4) {
itemFulfill.setValue('saturdaydeliveryfedex', true);
msg =
'FedEx 2Day Express with Saturday Delivery has been selected and the status has changed to SHIPPED.';
if (hour === 16) {
if (minutes >= 45) {
itemFulfill.setValue('saturdaydeliveryfedex', false);
shipDay = shipNextDay(true);
msg =
'FedEx 2Day Express has been selected and the status has changed to SHIPPED.';
isShipNextDay =
" It's after 4:45 PM the ship date has been changed automatically to: " +
shipDay +
" Make sure it's correct.";
}
}
if (hour > 16) {
itemFulfill.setValue('saturdaydeliveryfedex', false);
shipDay = shipNextDay(true);
msg =
'FedEx 2Day Express has been selected and the status has changed to SHIPPED.';
isShipNextDay =
" It's after 4:45 PM the ship date has been changed automatically to: " +
shipDay +
" Make sure it's correct.";
}
} else if (day === 6) {
shipDay = shipNextDay(true);
isShipNextDay =
" Since it's a Saturday, the ship date has been changed automatically to: " +
shipDay +
" Make sure it's correct.";
} else {
if (hour === 16) {
if (minutes >= 45) {
shipDay = shipNextDay(true);
isShipNextDay =
" It's after 4:45 PM the ship date has been changed automatically to: " +
shipDay +
" Make sure it's correct.";
if (day === 3) {
itemFulfill.setValue('saturdaydeliveryfedex', true);
isShipNextDay =
isShipNextDay +
' Since shipping will be on a Thursday, Saturday shipping has also been selected.';
}
}
}
if (hour > 16) {
shipDay = shipNextDay(true);
isShipNextDay =
" It's after 4:45 PM the ship date has been changed automatically to: " +
shipDay +
" Make sure it's correct.";
if (day === 3) {
itemFulfill.setValue('saturdaydeliveryfedex', true);
isShipNextDay =
isShipNextDay +
' Since shipping will be on a Thursday, Saturday shipping has also been selected.';
}
}
msg =
'FedEx 2Day Express has been selected and status has changed to SHIPPED.';
}
updateTranDate();
dialog.alert({
title: 'Ship Status Changed',
message: msg + isShipNextDay + ' Please save and print the label.',
});
}
} catch (e) {
console.log(e.message);
log.debug({
title: 'FAILED TO CHANGE SHIP STATUS',
details: e.message,
});
}
}
// USPS Priority
if (itemFulfill.getValue('shipmethod') == uspsPriority) {
try {
// if status picked or packed
if (status == 'A' || status == 'B') {
itemFulfill.setValue('shipstatus', 'C');
itemFulfill.setValue('generateintegratedshipperlabel', true);
// set package size
itemFulfill.selectLine({ sublistId: 'packageusps', line: 0 });
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagewidthusps',
value: '6',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagelengthusps',
value: '12',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packageheightusps',
value: '3',
});
// Commit line
itemFulfill.commitLine({ sublistId: 'packageusps' });
updateTranDate();
dialog.alert({
title: 'Ship Status Changed',
message:
'Ship status has been changed to USPS Priority, please save and print label.',
});
}
} catch (e) {
console.log(e.message);
}
}
// USPS First Class
if (itemFulfill.getValue('shipmethod') == uspsFirstClass) {
try {
// if status picked or packed
if (status == 'A' || status == 'B') {
itemFulfill.setValue('shipstatus', 'C');
itemFulfill.setValue('generateintegratedshipperlabel', true);
// set package size
itemFulfill.selectLine({ sublistId: 'packageusps', line: 0 });
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagewidthusps',
value: '6',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagelengthusps',
value: '9',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packageheightusps',
value: '3',
});
// Commit line
itemFulfill.commitLine({ sublistId: 'packageusps' });
updateTranDate();
dialog.alert({
title: 'Ship Status Changed',
message:
'Ship status has been changed to USPS First Class, please save and print label.',
});
}
} catch (e) {
console.log(e.message);
}
}
// USPS Priority - Flat Rate Envelope
if (itemFulfill.getValue('shipmethod') == uspsPriorityEnvelope) {
try {
// if status picked or packed
if (status == 'A' || status == 'B') {
itemFulfill.setValue('shipstatus', 'C');
itemFulfill.setValue('generateintegratedshipperlabel', true);
// // Add a new line item to package sublist
itemFulfill.selectLine({ sublistId: 'packageusps', line: 0 });
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagingusps',
value: 16,
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagewidthusps',
value: '6',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagelengthusps',
value: '9',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packageheightusps',
value: '3',
});
// Commit Line
itemFulfill.commitLine({ sublistId: 'packageusps' });
updateTranDate();
dialog.alert({
title: 'Ship Status Changed',
message:
'Ship status has been changed to USPS Priority and Packaging has been set to Flat Rate Envelope, please save and print label.',
});
}
} catch (e) {
console.log(e.message);
}
}
// USPS Priority - Flat Rate Legal Envelope
if (itemFulfill.getValue('shipmethod') == uspsPriorityLegalEnvelope) {
try {
// if status picked or packed
if (status == 'A' || status == 'B') {
itemFulfill.setValue('shipstatus', 'C');
itemFulfill.setValue('generateintegratedshipperlabel', true);
// // Add a new line item to package sublist
itemFulfill.selectLine({ sublistId: 'packageusps', line: 0 });
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagingusps',
value: 25,
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagewidthusps',
value: '12',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagelengthusps',
value: '6',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packageheightusps',
value: '3',
});
// Commit Line
itemFulfill.commitLine({ sublistId: 'packageusps' });
updateTranDate();
dialog.alert({
title: 'Ship Status Changed',
message:
'Ship status has been changed to USPS Priority and Packaging has been set to Flat Rate Legal Envelope, please save and print label.',
});
}
} catch (e) {
console.log(e.message);
}
}
// USPS Priority - Medium Flat Rate Box
if (itemFulfill.getValue('shipmethod') == uspsPriorityMdFlatRateBox) {
try {
// if status picked or packed
if (status == 'A' || status == 'B') {
itemFulfill.setValue('shipstatus', 'C');
itemFulfill.setValue('generateintegratedshipperlabel', true);
// // Add a new line item to package sublist
itemFulfill.selectLine({ sublistId: 'packageusps', line: 0 });
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagingusps',
value: 23,
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagewidthusps',
value: '8',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packagelengthusps',
value: '11',
});
itemFulfill.setCurrentSublistValue({
sublistId: 'packageusps',
fieldId: 'packageheightusps',
value: '6',
});
// Commit Line
itemFulfill.commitLine({ sublistId: 'packageusps' });
updateTranDate();
dialog.alert({
title: 'Ship Status Changed',
message:
'Ship status has been changed to USPS Priority and Packaging has been set to Medium Flat Rate Box, please save and print label.',
});
}
} catch (e) {
console.log(e.message);
}
}
};
/**
* Automates the manual process of selecting FedEx 2 Day Express
*/
export const setFedexExpress = () => {
console.log('FedEx Express button selected');
const itemFulfill = currentRecord.get();
console.log(itemFulfill);
// check day
const date = new Date();
const day = date.getDay();
console.log('Day: ' + day);
try {
// change shipment method -- reloads
const shipmethod = itemFulfill.getValue('shipmethod');
console.log('Ship method');
console.log(shipmethod);
if (shipmethod != '30611') {
itemFulfill.setValue('shipmethod', fedEx2Day);
}
} catch (e) {
console.log(e.message);
}
};
/**
* Changes the carrier settings for next day shipping, based on the next business day.
*/
export const shipNextDay = (hideDialog: boolean) => {
console.log('Next Day Shipping has been selected');
console.log('FedEx Client Script Loaded');
const itemFulfill = currentRecord.get();
// check if fedex express 2day
if (itemFulfill.getValue('shipmethod') === fedEx2Day) {
try {
const today = new Date();
const shipDay = new Date(today);
let daysToAdd: number;
const day = shipDay.getDay();
if (day === 5) {
daysToAdd = 3;
} else if (day === 6) {
daysToAdd = 2;
} else {
daysToAdd = 1;
}
// if thursday and shipping next day remove saturday delivery
if (day === 4) {
itemFulfill.setValue('saturdaydeliveryfedex', false);
}
shipDay.setDate(shipDay.getDate() + daysToAdd);
shipDay.setHours(0);
shipDay.setMinutes(0);
shipDay.setSeconds(0);
itemFulfill.setValue('shipdatefedex', shipDay);
if (!hideDialog) {
dialog.alert({
title: 'Ship Date Changed',
message:
'The ship date has been changed to: ' +
shipDay +
' Please make sure it is correct and save.',
});
}
return shipDay;
} catch (e) {
console.log(e.message);
}
} else {
dialog.alert({
title: 'Error:',
message:
'In order to change the ship date you must first select FedEx 2Day Express as the ship method.',
});
}
};
/**
* Sets shipping method as USPS Priority.
*/
export const setUspsPriority = () => {
console.log('USPS Priority has been selected.');
const itemFulfill = currentRecord.get();
try {
// change shipment method -- reloads
const shipmethod = itemFulfill.getValue('shipmethod');
console.log('Ship method');
console.log(shipmethod);
if (shipmethod != uspsPriority) {
itemFulfill.setValue('shipmethod', uspsPriority);
}
} catch (e) {
console.log(e.message);
}
};
/**
* Sets shipping method as USPS First-Class.
*/
const setUspsFirstClass = () => {
console.log('USPS First-Class has been selected.');
const itemFulfill = currentRecord.get();
try {
// change shipment method -- reloads
const shipmethod = itemFulfill.getValue('shipmethod');
console.log('Ship method');
console.log(shipmethod);
if (shipmethod != uspsFirstClass) {
itemFulfill.setValue('shipmethod', uspsFirstClass);
}
} catch (e) {
console.log(e.message);
}
};
/**
* Sets shipping method as USPS Priority.
* Has to be different shipping method so paginit func can
* set the package to Flat Rate Envelope.
*/
export const setUspsPriorityEnvelope = () => {
console.log('USPS Priority - Flat Rate Envelope has been selected.');
const itemFulfill = currentRecord.get();
try {
// change shipment method -- reloads
const shipmethod = itemFulfill.getValue('shipmethod');
console.log('Ship method');
console.log(shipmethod);
if (shipmethod != uspsPriorityEnvelope) {
itemFulfill.setValue('shipmethod', uspsPriorityEnvelope);
}
} catch (e) {
console.log(e.message);
}
};
/**
* Sets shipping method as USPS Priority.
* Has to be different shipping method so paginit func can
* set the package to Flat Rate Legal Envelope.
*/
export const setUspsPriorityLegalEnvelope = () => {
console.log('USPS Priority - Flat Rate Legal Envelope has been selected.');
const itemFulfill = currentRecord.get();
try {
// change shipment method -- reloads
const shipmethod = itemFulfill.getValue('shipmethod');
console.log('Ship method');
console.log(shipmethod);
if (shipmethod != uspsPriorityLegalEnvelope) {
itemFulfill.setValue('shipmethod', uspsPriorityLegalEnvelope);
}
} catch (e) {
console.log(e.message);
}
};
/**
* Sets shipping method as USPS Priority.
* Has to be different shipping method so paginit func can
* set the package to Medium Flat Rate Box.
*/
export const setUspsPriorityMediumBox = () => {
console.log('USPS Priority - Medium Flat Rate Box has been selected.');
const itemFulfill = currentRecord.get();
try {
// change shipment method -- reloads
const shipmethod = itemFulfill.getValue('shipmethod');
console.log('Ship method');
console.log(shipmethod);
if (shipmethod != uspsPriorityMdFlatRateBox) {
itemFulfill.setValue('shipmethod', uspsPriorityMdFlatRateBox);
}
} catch (e) {
console.log(e.message);
}
};
/**
* Gets triggered on field changed, mostly to get field names that don't
* exist in the docs.
*/
export const fieldChanged: EntryPoints.Client.fieldChanged = (
context: EntryPoints.Client.fieldChangedContext
) => {
console.log('field changed');
console.log(context.fieldId);
if (context.fieldId == 'shipmethod') {
onLoad();
}
};
const updateTranDate = () => {
const itemFulfillment = currentRecord.get();
const date = new Date();
// Set Tran Date
console.log('Setting Transaction Date.');
itemFulfillment.setValue('trandate', date);
};