-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear_bin_available_inventory.ts
229 lines (215 loc) · 6.1 KB
/
clear_bin_available_inventory.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
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
import { EntryPoints } from 'N/types';
import * as runtime from 'N/runtime';
import * as serverWidget from 'N/ui/serverWidget';
import * as search from 'N/search';
import * as log from 'N/log';
import * as record from 'N/record';
/**
* A Suitelet to clear the available inventory in a bin.
*/
export const onRequest: EntryPoints.Suitelet.onRequest = (
context: EntryPoints.Suitelet.onRequestContext
) => {
const request = context.request;
const response = context.response;
if (request.method == 'GET') {
onGet(response);
} else if (request.method == 'POST') {
onPost(request, response);
}
};
/**
* Generates the product form.
*/
const onGet = response => {
const Form = serverWidget.createForm({
title: `Clear Bin's Available Inventory`,
});
const BinField = Form.addField({
id: 'bin_number',
label: 'Bin Number',
type: serverWidget.FieldType.TEXT,
});
const Button = Form.addSubmitButton({
label: 'Submit',
});
response.writePage(Form);
};
/**
* Returns the items in the bin
* and generates a form for deleting them.
*/
const onPost = (request, response) => {
/**
* Load the saved search
*/
const savedSearchID = runtime
.getCurrentScript()
.getParameter({ name: 'custscript_bin_available_items_search_id' });
const savedSearch = search.load({
id: String(savedSearchID),
});
/**
* Filter the saved search
*/
const searchFilter = {
name: 'formulanumeric',
operator: search.Operator.EQUALTO,
values: [1],
formula: `CASE WHEN {binonhand.binnumber} = '${request.parameters.bin_number}' THEN 1 ELSE 0 END`,
join: null,
summary: null,
};
const savedSearchFilters = savedSearch.filters;
savedSearchFilters.push(searchFilter);
savedSearch.filters = savedSearchFilters;
/**
* Get the results of the saved search
*/
const savedSearchResults = savedSearch
.run()
.getRange({ start: 0, end: 1000 });
const savedSearchJSON = JSON.parse(JSON.stringify(savedSearchResults));
log.debug({
title: 'Saved Search',
details: savedSearchResults,
});
/**
* Create Inventory Adjustment
*/
const adjustInventory = list => {
const adjustmentRecord = record.create({
type: record.Type.INVENTORY_ADJUSTMENT,
isDynamic: true,
});
adjustmentRecord.setValue({
fieldId: 'account',
value: 213,
});
for (const item of list) {
const availableQuantity = parseInt(
item.values['binOnHand.quantityavailable']
);
if (availableQuantity > 0) {
// log
log.debug({
title: 'ADDING ITEM',
details:
'ID: ' +
item.id +
' | AvailQty: ' +
availableQuantity +
' | Location: ' +
item.values['binOnHand.location'][0].value +
' | Bin Number: ' +
item.values['binOnHand.binnumber'][0].value +
' | InventoryAssignment: ' +
availableQuantity * -1,
});
adjustmentRecord.selectNewLine({
sublistId: 'inventory',
});
adjustmentRecord.setCurrentSublistValue({
sublistId: 'inventory',
fieldId: 'item',
value: parseInt(item.id),
});
adjustmentRecord.setCurrentSublistValue({
sublistId: 'inventory',
fieldId: 'adjustqtyby',
value: availableQuantity * -1,
});
adjustmentRecord.setCurrentSublistValue({
sublistId: 'inventory',
fieldId: 'location',
value: parseInt(item.values['binOnHand.location'][0].value),
});
const subRecord = adjustmentRecord.getCurrentSublistSubrecord({
sublistId: 'inventory',
fieldId: 'inventorydetail',
});
subRecord.selectNewLine({
sublistId: 'inventoryassignment',
});
subRecord.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'binnumber',
value: parseInt(item.values['binOnHand.binnumber'][0].value),
});
subRecord.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'status',
value: 1,
});
// set quantity
subRecord.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
value: availableQuantity * -1,
});
// commit line
subRecord.commitLine({
sublistId: 'inventoryassignment',
});
adjustmentRecord.commitLine({
sublistId: 'inventory',
});
}
}
const recordId = adjustmentRecord.save({
enableSourcing: false,
ignoreMandatoryFields: false,
});
log.debug({
title: 'Record ID',
details: recordId,
});
return { id: recordId };
};
const inventoryAdjustment = adjustInventory(savedSearchJSON);
/**
* Create the form
*/
const Form = serverWidget.createForm({
title: `Inventory Adjustment ${inventoryAdjustment.id} Complete`,
});
const DataFieldGroup = Form.addFieldGroup({
id: 'group_2',
label: `Adjusted Bin ${request.parameters.bin_number} Contents: On Hand - Available = New On Hand`,
});
const Table = Form.addField({
id: 'table',
label: 'The table data',
type: serverWidget.FieldType.INLINEHTML,
container: 'group_2',
});
Table.defaultValue = `
<table style="width: 100%">
<tr>
<td>SKU</td>
<td>On Hand</td>
<td>Available</td>
<td> New On Hand</td>
</tr>
${savedSearchJSON.map(
record =>
`<tr>
<td>${record.values.itemid}</td>
<td>${record.values['binOnHand.quantityonhand']}</td>
<td>${record.values['binOnHand.quantityavailable']}</td>
<td>${
parseInt(record.values['binOnHand.quantityonhand']) -
parseInt(record.values['binOnHand.quantityavailable'])
}</td>
</tr>
`
)}
</table>
`;
response.writePage(Form);
};