@things-factory/operato-hub 4.3.382 → 4.3.384
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-server/routers/api/restful-apis/v1/company/upsert-product.js +55 -197
- package/dist-server/routers/api/restful-apis/v1/company/upsert-product.js.map +1 -1
- package/dist-server/routers/api/restful-apis/v1/utils/params.js +6 -2
- package/dist-server/routers/api/restful-apis/v1/utils/params.js.map +1 -1
- package/dist-server/routers/api/restful-apis/v1/warehouse/get-inventory-adjustment-list.js +214 -0
- package/dist-server/routers/api/restful-apis/v1/warehouse/get-inventory-adjustment-list.js.map +1 -0
- package/dist-server/routers/api/restful-apis/v1/warehouse/index.js +1 -0
- package/dist-server/routers/api/restful-apis/v1/warehouse/index.js.map +1 -1
- package/package.json +8 -8
- package/server/routers/api/restful-apis/v1/company/upsert-product.ts +63 -266
- package/server/routers/api/restful-apis/v1/utils/params.ts +6 -2
- package/server/routers/api/restful-apis/v1/warehouse/get-inventory-adjustment-list.ts +234 -0
- package/server/routers/api/restful-apis/v1/warehouse/index.ts +1 -0
|
@@ -2,16 +2,16 @@ import _ from 'lodash'
|
|
|
2
2
|
import {
|
|
3
3
|
EntityManager,
|
|
4
4
|
getConnection,
|
|
5
|
-
getRepository,
|
|
6
5
|
In
|
|
7
6
|
} from 'typeorm'
|
|
8
7
|
|
|
9
8
|
import { restfulApiRouter as router } from '@things-factory/api'
|
|
10
|
-
import { Bizplace } from '@things-factory/biz-base'
|
|
11
9
|
import {
|
|
10
|
+
createProduct,
|
|
11
|
+
NewProduct,
|
|
12
12
|
Product,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
ProductPatch,
|
|
14
|
+
updateProduct
|
|
15
15
|
} from '@things-factory/product-base'
|
|
16
16
|
|
|
17
17
|
import {
|
|
@@ -33,285 +33,82 @@ router.post(
|
|
|
33
33
|
async (context, next) => {
|
|
34
34
|
try {
|
|
35
35
|
await getConnection().transaction(async (tx: EntityManager) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const skus = productInfo.map(p => p.sku)
|
|
40
|
-
const { user, domain } = context.state
|
|
41
|
-
let response = []
|
|
42
|
-
let newSku = []
|
|
43
|
-
let existingSku = []
|
|
44
|
-
let checkSku
|
|
45
|
-
|
|
46
|
-
if (!skus) {
|
|
47
|
-
throw new ApiError('E03', 'sku')
|
|
36
|
+
const { domain } = context.state
|
|
37
|
+
if (!Array.isArray(context.request.body.data)) {
|
|
38
|
+
throw new ApiError('E01', 'data field is not an array')
|
|
48
39
|
}
|
|
49
40
|
|
|
50
|
-
let
|
|
41
|
+
let patches = context.request.body.data
|
|
42
|
+
const skus = patches.map(p => p.sku)
|
|
51
43
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
await Promise.all(
|
|
45
|
+
patches.map(async p => {
|
|
46
|
+
if (!p.productDetails || p.productDetails.length === 0) {
|
|
47
|
+
throw new ApiError('E01', 'missing product detail')
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
)
|
|
55
51
|
|
|
56
|
-
|
|
57
|
-
let
|
|
58
|
-
where: {
|
|
59
|
-
domain
|
|
60
|
-
}
|
|
61
|
-
})
|
|
52
|
+
context.state.tx = tx
|
|
53
|
+
let results = []
|
|
62
54
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
where: {
|
|
66
|
-
sku: In(skus),
|
|
67
|
-
bizplace
|
|
68
|
-
},
|
|
55
|
+
let _foundRecords: Product = await tx.getRepository(Product).find({
|
|
56
|
+
where: { sku: In(skus), domain },
|
|
69
57
|
relations: ['productDetails', 'productDetails.productBarcodes']
|
|
70
58
|
})
|
|
71
59
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
60
|
+
const _updateRecords = patches.filter((patch: any) =>
|
|
61
|
+
_foundRecords.some(foundRecord => foundRecord.sku === patch.sku)
|
|
62
|
+
)
|
|
63
|
+
const _createRecords = patches.filter(
|
|
64
|
+
(createPatch: any) => !_updateRecords.some(updateRecord => updateRecord.sku === createPatch.sku)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if (_createRecords.length > 0) {
|
|
68
|
+
for (let i = 0; i < _createRecords.length; i++) {
|
|
69
|
+
const patch: NewProduct = _createRecords[i]
|
|
70
|
+
const result = await createProduct(patch, context)
|
|
71
|
+
if (result.costPrice) {
|
|
72
|
+
result.costPrice = result.costPrice.toFixed(2)
|
|
73
|
+
}
|
|
74
|
+
results.push({ ...result })
|
|
84
75
|
}
|
|
85
76
|
}
|
|
86
77
|
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
let pds = await Promise.all(
|
|
103
|
-
productDetails.map(async pd => {
|
|
104
|
-
let productBarcodes = pd.productBarcodes
|
|
105
|
-
delete pd.productBarcodes
|
|
106
|
-
if (pd.packingSize < 0) throw new ApiError('E01', `${pd.gtin}: packing size cannot be negative`)
|
|
107
|
-
const productDetail = pd
|
|
108
|
-
const matchingPd = checkSku.map(itm => {
|
|
109
|
-
const matchingProductDetail = itm.productDetails.find(prodDetail => {
|
|
110
|
-
return (
|
|
111
|
-
prodDetail.packingSize === pd.packingSize &&
|
|
112
|
-
prodDetail.packingType === pd.packingType &&
|
|
113
|
-
prodDetail.uom === pd.uom
|
|
114
|
-
)
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
return { matchingProductDetail }
|
|
118
|
-
})
|
|
119
|
-
let pdResult: ProductDetail
|
|
120
|
-
|
|
121
|
-
const foundIndex = matchingPd.findIndex(itm => itm.matchingProductDetail)
|
|
122
|
-
if (foundIndex != -1) {
|
|
123
|
-
pdResult = await tx.getRepository(ProductDetail).save({
|
|
124
|
-
...productDetail,
|
|
125
|
-
id: matchingPd[foundIndex].matchingProductDetail.id,
|
|
126
|
-
name: productDetail.gtin,
|
|
127
|
-
product: result,
|
|
128
|
-
childProductDetail: null,
|
|
129
|
-
childQty: 0,
|
|
130
|
-
creatorId: matchingPd[foundIndex].matchingProductDetail.creatorId,
|
|
131
|
-
domain: domain,
|
|
132
|
-
updater: user
|
|
133
|
-
})
|
|
134
|
-
} else {
|
|
135
|
-
pdResult = await tx.getRepository(ProductDetail).save({
|
|
136
|
-
...productDetail,
|
|
137
|
-
name: productDetail.gtin,
|
|
138
|
-
product: result,
|
|
139
|
-
childProductDetail: null,
|
|
140
|
-
childQty: 0,
|
|
141
|
-
creator: user,
|
|
142
|
-
domain: domain,
|
|
143
|
-
updater: user
|
|
144
|
-
})
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
delete pdResult.creator
|
|
148
|
-
delete pdResult.updater
|
|
149
|
-
delete pdResult.product
|
|
150
|
-
delete pdResult.domain
|
|
151
|
-
|
|
152
|
-
let pbs: ProductBarcode[]
|
|
153
|
-
productBarcodes = productBarcodes.filter(barcode => Object.keys(barcode).length > 0)
|
|
154
|
-
|
|
155
|
-
if (productBarcodes.length > 0) {
|
|
156
|
-
pbs = await Promise.all(
|
|
157
|
-
productBarcodes.map(async pb => {
|
|
158
|
-
let existingPb: ProductBarcode
|
|
159
|
-
existingPb = await tx.getRepository(ProductBarcode).findOne({
|
|
160
|
-
where: {
|
|
161
|
-
gtin: pb.gtin,
|
|
162
|
-
productDetail: pdResult.id
|
|
163
|
-
}
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
if (existingPb) {
|
|
167
|
-
// Update existing productBarcode
|
|
168
|
-
pdResult.productBarcodes = pdResult.productBarcodes || []
|
|
169
|
-
pdResult.productBarcodes.push(pb.gtin)
|
|
170
|
-
return tx.getRepository(ProductBarcode).update(existingPb.id, {
|
|
171
|
-
...pb,
|
|
172
|
-
product: result,
|
|
173
|
-
productDetail: pdResult.id,
|
|
174
|
-
updater: user
|
|
175
|
-
})
|
|
176
|
-
} else {
|
|
177
|
-
// Create a new productBarcode
|
|
178
|
-
pdResult.productBarcodes = pdResult.productBarcodes || []
|
|
179
|
-
pdResult.productBarcodes.push(pb.gtin)
|
|
180
|
-
return tx.getRepository(ProductBarcode).save({
|
|
181
|
-
gtin: pb.gtin ? pb.gtin : result.sku,
|
|
182
|
-
product: result,
|
|
183
|
-
productDetail: pdResult,
|
|
184
|
-
domain,
|
|
185
|
-
creator: user,
|
|
186
|
-
updater: user
|
|
187
|
-
})
|
|
188
|
-
}
|
|
189
|
-
})
|
|
190
|
-
)
|
|
191
|
-
} else {
|
|
192
|
-
let existingPb: ProductBarcode
|
|
193
|
-
existingPb = await tx.getRepository(ProductBarcode).findOne({
|
|
194
|
-
where: {
|
|
195
|
-
gtin: result.sku,
|
|
196
|
-
productDetail: pdResult.id
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
if (!existingPb) {
|
|
200
|
-
pbs = [
|
|
201
|
-
{
|
|
202
|
-
gtin: result.sku,
|
|
203
|
-
product,
|
|
204
|
-
productDetail: pdResult,
|
|
205
|
-
domain,
|
|
206
|
-
creator: user,
|
|
207
|
-
updater: user
|
|
208
|
-
}
|
|
209
|
-
]
|
|
210
|
-
|
|
211
|
-
await tx.getRepository(ProductBarcode).save(pbs)
|
|
212
|
-
|
|
213
|
-
pdResult.productBarcodes = pbs.map(pb => pb.gtin)
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (existingPb) {
|
|
217
|
-
pdResult.productBarcodes = [existingPb.gtin]
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return pdResult
|
|
222
|
-
})
|
|
223
|
-
)
|
|
224
|
-
delete result.creator
|
|
225
|
-
delete result.updater
|
|
226
|
-
delete result.bizplace
|
|
227
|
-
delete result.domain
|
|
228
|
-
response.push({ ...result, productDetails: pds })
|
|
229
|
-
})
|
|
230
|
-
)
|
|
78
|
+
if (_updateRecords.length > 0) {
|
|
79
|
+
for (let i = 0; i < _updateRecords.length; i++) {
|
|
80
|
+
let patch: ProductPatch = _updateRecords[i]
|
|
81
|
+
let matchingSku = _foundRecords.find(itm => itm.sku === patch.sku)
|
|
82
|
+
patch = {
|
|
83
|
+
...patch,
|
|
84
|
+
id: matchingSku.id
|
|
85
|
+
}
|
|
86
|
+
const result = await updateProduct(patch.id, patch, context)
|
|
87
|
+
if (result.costPrice) {
|
|
88
|
+
result.costPrice = +result.costPrice.toFixed(2)
|
|
89
|
+
}
|
|
90
|
+
results.push({ ...result })
|
|
91
|
+
}
|
|
231
92
|
}
|
|
232
93
|
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
newSku.map(async p => {
|
|
237
|
-
let product = p
|
|
238
|
-
const productDetails = p.productDetails
|
|
239
|
-
|
|
240
|
-
product.domain = domain
|
|
241
|
-
product.bizplace = bizplace
|
|
242
|
-
product.creator = user
|
|
243
|
-
product.updater = user
|
|
244
|
-
delete product.productDetails
|
|
94
|
+
// Set up response body
|
|
95
|
+
const updatedResults = results.map(product => {
|
|
96
|
+
const { domain, updater, bizplace, creator, creatorId, updaterId, ...updatedProduct } = product
|
|
245
97
|
|
|
246
|
-
|
|
98
|
+
const updatedProductDetails = product.productDetails.map(detail => {
|
|
99
|
+
const { name, ...restDetail } = detail
|
|
100
|
+
return restDetail
|
|
101
|
+
})
|
|
247
102
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const productBarcodes = productDetail.productBarcodes
|
|
254
|
-
let pdResult = await tx.getRepository(ProductDetail).save({
|
|
255
|
-
...productDetail,
|
|
256
|
-
name: productDetail.gtin,
|
|
257
|
-
product: result,
|
|
258
|
-
childProductDetail: null,
|
|
259
|
-
childQty: 0,
|
|
260
|
-
domain: domain,
|
|
261
|
-
creator: user,
|
|
262
|
-
updater: user
|
|
263
|
-
})
|
|
264
|
-
delete pdResult.creator
|
|
265
|
-
delete pdResult.updater
|
|
266
|
-
delete pdResult.product
|
|
267
|
-
delete pdResult.domain
|
|
268
|
-
|
|
269
|
-
let pbs: ProductBarcode[]
|
|
270
|
-
|
|
271
|
-
if (productBarcodes) {
|
|
272
|
-
pbs = productBarcodes.map(pb => {
|
|
273
|
-
return {
|
|
274
|
-
...pb,
|
|
275
|
-
gtin: pb.gtin ? pb.gtin : result.sku,
|
|
276
|
-
product,
|
|
277
|
-
productDetail: pdResult,
|
|
278
|
-
domain,
|
|
279
|
-
creator: user,
|
|
280
|
-
updater: user
|
|
281
|
-
}
|
|
282
|
-
})
|
|
283
|
-
pdResult.productBarcodes = pbs.map(pb => pb.gtin)
|
|
284
|
-
} else {
|
|
285
|
-
pbs = [
|
|
286
|
-
{
|
|
287
|
-
gtin: result.sku,
|
|
288
|
-
product: result,
|
|
289
|
-
productDetail: pdResult,
|
|
290
|
-
domain,
|
|
291
|
-
creator: user,
|
|
292
|
-
updater: user
|
|
293
|
-
}
|
|
294
|
-
]
|
|
295
|
-
pdResult.productBarcodes = pbs.map(pb => pb.gtin)
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
await tx.getRepository(ProductBarcode).save(pbs)
|
|
299
|
-
|
|
300
|
-
return pdResult
|
|
301
|
-
})
|
|
302
|
-
)
|
|
303
|
-
delete result.creator
|
|
304
|
-
delete result.updater
|
|
305
|
-
delete result.bizplace
|
|
306
|
-
delete result.domain
|
|
307
|
-
response.push({ ...result, productDetails: pds })
|
|
308
|
-
})
|
|
309
|
-
)
|
|
310
|
-
}
|
|
103
|
+
return {
|
|
104
|
+
...updatedProduct,
|
|
105
|
+
productDetails: updatedProductDetails
|
|
106
|
+
}
|
|
107
|
+
})
|
|
311
108
|
|
|
312
|
-
//
|
|
109
|
+
//pass response
|
|
313
110
|
context.body = {
|
|
314
|
-
data:
|
|
111
|
+
data: updatedResults
|
|
315
112
|
}
|
|
316
113
|
})
|
|
317
114
|
} catch (e) {
|
|
@@ -25,6 +25,7 @@ export const params = {
|
|
|
25
25
|
'get-delivery-order-list': ['bizplaceId', 'doNo', 'vehicleNo', ...fromToDate, ...limitOffset],
|
|
26
26
|
'get-onhand-inventory-list': ['bizplaceId', 'sku', 'batchId', 'fromDate', 'toDate', ...limitOffset],
|
|
27
27
|
'get-inventory-product-group-list': ['bizplaceId', 'sku', 'batchId', ...fromToDate, ...limitOffset],
|
|
28
|
+
'get-inventory-adjustment-list': ['bizplaceId', ...fromToDate, ...limitOffset, 'sku', 'adjustmentStatus'],
|
|
28
29
|
'get-manifest-details': ['bizplaceId', 'name', 'id'],
|
|
29
30
|
'add-contact-points': [
|
|
30
31
|
'bizplaceId',
|
|
@@ -493,6 +494,7 @@ export const reqParams = {
|
|
|
493
494
|
'get-inventory-product-group-list': ['bizplaceId'],
|
|
494
495
|
'get-manifest-details': ['bizplaceId'],
|
|
495
496
|
'get-warehouse-bizplace-onhand-inventories': ['bizplaceId'],
|
|
497
|
+
'get-inventory-adjustment-list': ['bizplaceId', 'fromDate', 'toDate'],
|
|
496
498
|
'upsert-product': [
|
|
497
499
|
{
|
|
498
500
|
name: 'sku',
|
|
@@ -885,7 +887,8 @@ export const requireLimitOffsetValidation = [
|
|
|
885
887
|
'get-marketplace-order-shipping-list',
|
|
886
888
|
'get-contact-point-list',
|
|
887
889
|
'get-bizplace-list',
|
|
888
|
-
'get-draft-order-list'
|
|
890
|
+
'get-draft-order-list',
|
|
891
|
+
'get-inventory-adjustment-list'
|
|
889
892
|
]
|
|
890
893
|
|
|
891
894
|
// Require fromDate and toDate
|
|
@@ -895,5 +898,6 @@ export const requireFromToDateValidation = [
|
|
|
895
898
|
'get-goods-received-note-list',
|
|
896
899
|
'get-delivery-order-list',
|
|
897
900
|
'get-marketplace-order-shipping-list',
|
|
898
|
-
'get-draft-order-list'
|
|
901
|
+
'get-draft-order-list',
|
|
902
|
+
'get-inventory-adjustment-list'
|
|
899
903
|
]
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import _ from 'lodash'
|
|
2
|
+
import { Between, getRepository, LessThanOrEqual, SelectQueryBuilder } from 'typeorm'
|
|
3
|
+
|
|
4
|
+
import { restfulApiRouter as router } from '@things-factory/api'
|
|
5
|
+
import { InventoryChange } from '@things-factory/warehouse-base'
|
|
6
|
+
|
|
7
|
+
import { businessMiddleware, loggingMiddleware, validationMiddleware } from '../middlewares'
|
|
8
|
+
import { ApiError, ApiErrorHandler, throwInternalServerError } from '../utils/error-util'
|
|
9
|
+
|
|
10
|
+
router.get(
|
|
11
|
+
'/v1/warehouse/get-inventory-adjustment-list',
|
|
12
|
+
businessMiddleware,
|
|
13
|
+
validationMiddleware,
|
|
14
|
+
loggingMiddleware,
|
|
15
|
+
async (context, next) => {
|
|
16
|
+
try {
|
|
17
|
+
// optional query parameter filter
|
|
18
|
+
const domainId = context.state.domain.id
|
|
19
|
+
let filter = { ...context.query }
|
|
20
|
+
const { bizplaceId, limit, offset } = filter
|
|
21
|
+
let { fromDate, toDate } = filter
|
|
22
|
+
|
|
23
|
+
let page = 0
|
|
24
|
+
if (offset != 0) {
|
|
25
|
+
page = offset
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const fromDateTime = getDateTime(fromDate)
|
|
29
|
+
const toDateTime = getDateTime(toDate)
|
|
30
|
+
|
|
31
|
+
//change dateTime to GMT
|
|
32
|
+
function getDateTime(newDate) {
|
|
33
|
+
const DateString = new Date(newDate)
|
|
34
|
+
|
|
35
|
+
const timePart = `${DateString.getHours().toString().padStart(2, '0')}:${DateString.getUTCMinutes()
|
|
36
|
+
.toString()
|
|
37
|
+
.padStart(2, '0')}:${DateString.getSeconds().toString().padStart(2, '0')}.${DateString.getMilliseconds()
|
|
38
|
+
.toString()
|
|
39
|
+
.padStart(3, '0')}`
|
|
40
|
+
|
|
41
|
+
const datePart = `${DateString.getFullYear()}-${(DateString.getMonth() + 1)
|
|
42
|
+
.toString()
|
|
43
|
+
.padStart(2, '0')}-${DateString.getDate().toString().padStart(2, '0')}`
|
|
44
|
+
|
|
45
|
+
const dateTime = `${datePart} ${timePart}`
|
|
46
|
+
|
|
47
|
+
return dateTime
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const qb: SelectQueryBuilder<InventoryChange> = getRepository(InventoryChange).createQueryBuilder('ic')
|
|
51
|
+
|
|
52
|
+
qb.leftJoinAndSelect('ic.inventory', 'iv', 'ic.inventory_id = iv.id')
|
|
53
|
+
.leftJoinAndSelect('ic.product', 'p', 'ic.product_id = p.id')
|
|
54
|
+
.leftJoinAndSelect('iv.product', 'p2', 'iv.product_id = p2.id')
|
|
55
|
+
.leftJoinAndSelect('ic.lastInventoryHistory', 'ih', 'ic.last_inventory_history_id = ih.id')
|
|
56
|
+
.where(`ic.domain_id = :domainId`, { domainId: domainId })
|
|
57
|
+
.andWhere('ic."created_at" BETWEEN :fromDate AND :toDate', {
|
|
58
|
+
fromDate: fromDateTime,
|
|
59
|
+
toDate: toDateTime
|
|
60
|
+
})
|
|
61
|
+
.andWhere(`(ic.bizplace_id = :bizplaceId OR (ic.bizplace_id IS NULL AND iv.bizplace_id = :bizplaceId))`, {
|
|
62
|
+
bizplaceId: bizplaceId
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
if (filter?.sku) {
|
|
66
|
+
qb.andWhere('p2."sku"=:sku', { sku: filter.sku })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (filter?.adjustmentStatus) {
|
|
70
|
+
qb.andWhere('ic."status"=:status', { status: filter.adjustmentStatus.toUpperCase() })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
qb.orderBy('ic.createdAt', 'DESC')
|
|
74
|
+
|
|
75
|
+
// Fetch all row for exporting
|
|
76
|
+
if (page != 0 && limit) {
|
|
77
|
+
qb.offset((page - 1) * limit).limit(limit)
|
|
78
|
+
} else {
|
|
79
|
+
qb.offset(page * limit).limit(limit)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let [item, total] = await qb.getManyAndCount()
|
|
83
|
+
|
|
84
|
+
let data = []
|
|
85
|
+
for (const i of item) {
|
|
86
|
+
let diffQty = 0
|
|
87
|
+
if (i?.qty && i?.lastInventoryHistory?.qty) {
|
|
88
|
+
diffQty = (i?.qty ?? 0) - ((i.lastInventoryHistory.openingQty || 0) + i.lastInventoryHistory.qty ?? 0)
|
|
89
|
+
}
|
|
90
|
+
const datapoint = {
|
|
91
|
+
id: i.id,
|
|
92
|
+
original: {
|
|
93
|
+
productDetail: {
|
|
94
|
+
sku:
|
|
95
|
+
i?.transactionType !== 'NEW'
|
|
96
|
+
? i?.product?.sku
|
|
97
|
+
? i.product.sku
|
|
98
|
+
: i?.inventory?.product?.sku
|
|
99
|
+
? i.inventory.product.sku
|
|
100
|
+
: null
|
|
101
|
+
: null,
|
|
102
|
+
name:
|
|
103
|
+
i?.transactionType !== 'NEW'
|
|
104
|
+
? i?.product?.name
|
|
105
|
+
? i.product.name
|
|
106
|
+
: i?.inventory?.product?.name
|
|
107
|
+
? i.inventory.product.name
|
|
108
|
+
: null
|
|
109
|
+
: null
|
|
110
|
+
},
|
|
111
|
+
palletId: i?.lastInventoryHistory?.palletId
|
|
112
|
+
? i.lastInventoryHistory.palletId
|
|
113
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.palletId
|
|
114
|
+
? i.inventory.palletId
|
|
115
|
+
: null,
|
|
116
|
+
cartonId: i?.lastInventoryHistory?.cartonId
|
|
117
|
+
? i.lastInventoryHistory.cartonId
|
|
118
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.cartonId
|
|
119
|
+
? i.inventory.cartonId
|
|
120
|
+
: null,
|
|
121
|
+
batchId: i?.lastInventoryHistory?.batchId
|
|
122
|
+
? i.lastInventoryHistory.batchId
|
|
123
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.batchId
|
|
124
|
+
? i.inventory.batchId
|
|
125
|
+
: null,
|
|
126
|
+
batchNoRef: i?.lastInventoryHistory?.batchIdRef
|
|
127
|
+
? i.lastInventoryHistory.batchIdRef
|
|
128
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.batchIdRef
|
|
129
|
+
? i.inventory.batchIdRef
|
|
130
|
+
: null,
|
|
131
|
+
packingType: i?.lastInventoryHistory?.packingType
|
|
132
|
+
? i.lastInventoryHistory.packingType
|
|
133
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.packingType
|
|
134
|
+
? i.inventory.packingType
|
|
135
|
+
: null,
|
|
136
|
+
packingSize: i?.lastInventoryHistory?.packingSize
|
|
137
|
+
? i.lastInventoryHistory.packingSize
|
|
138
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.packingSize
|
|
139
|
+
? i.inventory.packingSize
|
|
140
|
+
: null,
|
|
141
|
+
qty: i?.lastInventoryHistory?.qty
|
|
142
|
+
? (i.lastInventoryHistory.openingQty || 0) + i.lastInventoryHistory.qty
|
|
143
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.qty
|
|
144
|
+
? i.inventory.qty
|
|
145
|
+
: null,
|
|
146
|
+
uom: i?.lastInventoryHistory?.uom
|
|
147
|
+
? i.lastInventoryHistory.uom
|
|
148
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.uom
|
|
149
|
+
? i.inventory.uom
|
|
150
|
+
: null,
|
|
151
|
+
uomValue: i?.lastInventoryHistory?.openingUomValue
|
|
152
|
+
? (i.lastInventoryHistory.openingUomValue || 0) + i.lastInventoryHistory.uomValue
|
|
153
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.uomValue
|
|
154
|
+
? i.inventory.uomValue
|
|
155
|
+
: null,
|
|
156
|
+
unitCost: i?.lastInventoryHistory?.unitCost
|
|
157
|
+
? i.lastInventoryHistory.unitCost
|
|
158
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.unitCost
|
|
159
|
+
? i.inventory.unitCost
|
|
160
|
+
: null,
|
|
161
|
+
expirationDate: i?.lastInventoryHistory?.expirationDate
|
|
162
|
+
? i.lastInventoryHistory.expirationDate
|
|
163
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.expirationDate
|
|
164
|
+
? i.inventory.expirationDate
|
|
165
|
+
: null,
|
|
166
|
+
manufactureDate: i?.lastInventoryHistory?.manufactureDate
|
|
167
|
+
? i.lastInventoryHistory.manufactureDate
|
|
168
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.manufactureDate
|
|
169
|
+
? i.inventory.manufactureDate
|
|
170
|
+
: null,
|
|
171
|
+
adjustmentCode: i?.lastInventoryHistory?.adjustmentCode
|
|
172
|
+
? i.lastInventoryHistory.adjustmentCode
|
|
173
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') &&
|
|
174
|
+
i?.status === 'PENDING' &&
|
|
175
|
+
i?.inventory?.adjustmentCode
|
|
176
|
+
? i.inventory.adjustmentCode
|
|
177
|
+
: null,
|
|
178
|
+
adjustmentNote: i?.lastInventoryHistory?.adjustmentNote
|
|
179
|
+
? i.lastInventoryHistory.adjustmentNote
|
|
180
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') &&
|
|
181
|
+
i?.status === 'PENDING' &&
|
|
182
|
+
i?.inventory?.adjustmentNote
|
|
183
|
+
? i.inventory.adjustmentNote
|
|
184
|
+
: null,
|
|
185
|
+
inventoryStatus: i?.lastInventoryHistory?.status
|
|
186
|
+
? i.lastInventoryHistory.status
|
|
187
|
+
: (i?.transactionType === 'CHANGES' || i?.transactionType === 'MISSING') && i?.inventory?.status
|
|
188
|
+
? i.inventory.status
|
|
189
|
+
: null
|
|
190
|
+
},
|
|
191
|
+
new: {
|
|
192
|
+
productDetail: {
|
|
193
|
+
sku: i?.product?.sku ? i.product.sku : null,
|
|
194
|
+
name: i?.product?.name ? i.product.name : null
|
|
195
|
+
},
|
|
196
|
+
palletId: i?.palletId ? i.palletId : null,
|
|
197
|
+
cartonId: i?.cartonId ? i.cartonId : null,
|
|
198
|
+
batchId: i?.batchId ? i.batchId : null,
|
|
199
|
+
batchNoRef: i?.batchIdRef ? i.batchIdRef : null,
|
|
200
|
+
packingType: i?.packingType ? i.packingType : null,
|
|
201
|
+
packingSize: i?.packingSize ? i.packingSize : null,
|
|
202
|
+
qty: i?.qty ? i.qty : null,
|
|
203
|
+
uom: i?.uom ? i.uom : null,
|
|
204
|
+
uomValue: i?.uomValue ? i.uomValue : null,
|
|
205
|
+
unitCost: i?.unitCost ? i.unitCost : null,
|
|
206
|
+
expirationDate: i?.expirationDate ? i.expirationDate : null,
|
|
207
|
+
manufactureDate: i?.manufactureDate ? i.manufactureDate : null,
|
|
208
|
+
adjustmentCode: i?.adjustmentCode ? i.adjustmentCode : null,
|
|
209
|
+
adjustmentNote: i?.adjustmentNote ? i.adjustmentNote : null,
|
|
210
|
+
inventoryStatus: i?.status !== 'PENDING' && i?.inventory?.status === 'MISSING' ? i.inventory.status : null,
|
|
211
|
+
adjustmentStatus: i?.status ? i.status : null,
|
|
212
|
+
type: i?.transactionType ? i.transactionType : null,
|
|
213
|
+
submittedAt: i?.createdAt ? i.createdAt : null,
|
|
214
|
+
updatedAt: i?.updatedAt ? i.updatedAt : null
|
|
215
|
+
},
|
|
216
|
+
adjusted: {
|
|
217
|
+
qty: diffQty === 0 ? 0 : diffQty < 0 ? diffQty : `+${diffQty}`
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
data.push(datapoint)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
context.body = {
|
|
224
|
+
data,
|
|
225
|
+
limit,
|
|
226
|
+
offset,
|
|
227
|
+
total
|
|
228
|
+
}
|
|
229
|
+
} catch (e) {
|
|
230
|
+
if (e instanceof ApiError) ApiErrorHandler(context, e)
|
|
231
|
+
else throwInternalServerError(context, e)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
)
|