@things-factory/warehouse-base 8.0.0-beta.8 → 8.0.0
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/service/inventory/inventory-mutation.js +15 -4
- package/dist-server/service/inventory/inventory-mutation.js.map +1 -1
- package/dist-server/service/inventory/inventory-query.d.ts +2 -0
- package/dist-server/service/inventory/inventory-types.d.ts +4 -0
- package/dist-server/service/inventory/inventory-types.js +16 -0
- package/dist-server/service/inventory/inventory-types.js.map +1 -1
- package/dist-server/service/inventory/inventory.d.ts +2 -0
- package/dist-server/service/inventory/inventory.js +12 -0
- package/dist-server/service/inventory/inventory.js.map +1 -1
- package/dist-server/service/warehouse/warehouse-mutation.js +24 -0
- package/dist-server/service/warehouse/warehouse-mutation.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -8
- package/server/constants/index.ts +5 -0
- package/server/constants/inventory.ts +67 -0
- package/server/constants/location.ts +14 -0
- package/server/constants/pallet.ts +10 -0
- package/server/constants/rule-type.ts +5 -0
- package/server/constants/tote.ts +5 -0
- package/server/controllers/ecommerce/ecommerce-controller.ts +108 -0
- package/server/controllers/ecommerce/index.ts +2 -0
- package/server/controllers/ecommerce/sellercraft-controller.ts +100 -0
- package/server/controllers/index.ts +2 -0
- package/server/controllers/warehouse-controller.ts +181 -0
- package/server/index.ts +9 -0
- package/server/middlewares/index.ts +0 -0
- package/server/migrations/index.ts +9 -0
- package/server/service/index.ts +80 -0
- package/server/service/inventory/index.ts +6 -0
- package/server/service/inventory/inventory-mutation.ts +530 -0
- package/server/service/inventory/inventory-query.ts +1259 -0
- package/server/service/inventory/inventory-types.ts +367 -0
- package/server/service/inventory/inventory.ts +408 -0
- package/server/service/inventory-change/index.ts +6 -0
- package/server/service/inventory-change/inventory-change-mutation.ts +969 -0
- package/server/service/inventory-change/inventory-change-query.ts +93 -0
- package/server/service/inventory-change/inventory-change-types.ts +36 -0
- package/server/service/inventory-change/inventory-change.ts +164 -0
- package/server/service/inventory-history/index.ts +6 -0
- package/server/service/inventory-history/inventory-history-mutation.ts +116 -0
- package/server/service/inventory-history/inventory-history-query.ts +1845 -0
- package/server/service/inventory-history/inventory-history-types.ts +444 -0
- package/server/service/inventory-history/inventory-history.ts +203 -0
- package/server/service/inventory-item/index.ts +6 -0
- package/server/service/inventory-item/inventory-item-mutation.ts +217 -0
- package/server/service/inventory-item/inventory-item-query.ts +226 -0
- package/server/service/inventory-item/inventory-item-type.ts +74 -0
- package/server/service/inventory-item/inventory-item.ts +105 -0
- package/server/service/inventory-item-change/index.ts +6 -0
- package/server/service/inventory-item-change/inventory-item-change-mutation.ts +119 -0
- package/server/service/inventory-item-change/inventory-item-change-query.ts +47 -0
- package/server/service/inventory-item-change/inventory-item-change-type.ts +68 -0
- package/server/service/inventory-item-change/inventory-item-change.ts +92 -0
- package/server/service/inventory-product/index.ts +6 -0
- package/server/service/inventory-product/inventory-product-mutation.ts +116 -0
- package/server/service/inventory-product/inventory-product-query.ts +47 -0
- package/server/service/inventory-product/inventory-product-type.ts +59 -0
- package/server/service/inventory-product/inventory-product.ts +88 -0
- package/server/service/location/index.ts +6 -0
- package/server/service/location/location-mutation.ts +134 -0
- package/server/service/location/location-query.ts +244 -0
- package/server/service/location/location-types.ts +173 -0
- package/server/service/location/location.ts +121 -0
- package/server/service/movement/index.ts +6 -0
- package/server/service/movement/movement-mutation.ts +60 -0
- package/server/service/movement/movement-query.ts +263 -0
- package/server/service/movement/movement-types.ts +74 -0
- package/server/service/movement/movement.ts +81 -0
- package/server/service/pallet/index.ts +6 -0
- package/server/service/pallet/pallet-mutation.ts +242 -0
- package/server/service/pallet/pallet-query.ts +106 -0
- package/server/service/pallet/pallet-types.ts +80 -0
- package/server/service/pallet/pallet.ts +92 -0
- package/server/service/pallet-count/index.ts +6 -0
- package/server/service/pallet-count/pallet-count-mutation.ts +151 -0
- package/server/service/pallet-count/pallet-count-query.ts +45 -0
- package/server/service/pallet-count/pallet-count-types.ts +36 -0
- package/server/service/pallet-count/pallet-count.ts +70 -0
- package/server/service/pallet-history/index.ts +6 -0
- package/server/service/pallet-history/pallet-history-mutation.ts +114 -0
- package/server/service/pallet-history/pallet-history-query.ts +48 -0
- package/server/service/pallet-history/pallet-history-types.ts +36 -0
- package/server/service/pallet-history/pallet-history.ts +89 -0
- package/server/service/reduced-inventory-history/index.ts +3 -0
- package/server/service/reduced-inventory-history/reduced-inventory-history.ts +92 -0
- package/server/service/tote/index.ts +6 -0
- package/server/service/tote/tote-mutation.ts +201 -0
- package/server/service/tote/tote-query.ts +106 -0
- package/server/service/tote/tote-types.ts +44 -0
- package/server/service/tote/tote.ts +77 -0
- package/server/service/warehouse/index.ts +6 -0
- package/server/service/warehouse/warehouse-mutation.ts +152 -0
- package/server/service/warehouse/warehouse-query.ts +58 -0
- package/server/service/warehouse/warehouse-types.ts +50 -0
- package/server/service/warehouse/warehouse.ts +95 -0
- package/server/utils/datetime-util.ts +54 -0
- package/server/utils/index.ts +3 -0
- package/server/utils/inventory-no-generator.ts +15 -0
- package/server/utils/inventory-util.ts +490 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
|
|
2
|
+
import { In, MoreThan, SelectQueryBuilder, Equal } from 'typeorm'
|
|
3
|
+
|
|
4
|
+
import { Bizplace } from '@things-factory/biz-base'
|
|
5
|
+
import { generateId } from '@things-factory/id-rule-base'
|
|
6
|
+
import { Sellercraft, SellercraftStatus } from '@things-factory/integration-sellercraft'
|
|
7
|
+
import { Product } from '@things-factory/product-base'
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
import { ArrivalNotice } from '@things-factory/sales-base'
|
|
10
|
+
|
|
11
|
+
import { INVENTORY_STATUS, INVENTORY_TRANSACTION_TYPE, LOCATION_STATUS, RULE_TYPE } from '../../constants'
|
|
12
|
+
import { SellercraftController } from '../../controllers'
|
|
13
|
+
import { DateGenerator, InventoryNoGenerator, InventoryUtil } from '../../utils'
|
|
14
|
+
import { InventoryHistory } from '../inventory-history/inventory-history'
|
|
15
|
+
import { Location } from '../location/location'
|
|
16
|
+
import { Inventory } from './inventory'
|
|
17
|
+
import { InventoryPatch, NewInventory } from './inventory-types'
|
|
18
|
+
|
|
19
|
+
@Resolver(Inventory)
|
|
20
|
+
export class InventoryMutation {
|
|
21
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
22
|
+
@Directive('@transaction')
|
|
23
|
+
@Mutation(returns => Inventory)
|
|
24
|
+
async createInventory(
|
|
25
|
+
@Arg('inventory') inventory: NewInventory,
|
|
26
|
+
@Ctx() context: ResolverContext
|
|
27
|
+
): Promise<Inventory> {
|
|
28
|
+
const { tx } = context.state
|
|
29
|
+
|
|
30
|
+
return await tx.getRepository(Inventory).save({
|
|
31
|
+
domain: context.state.domain,
|
|
32
|
+
creator: context.state.user,
|
|
33
|
+
updater: context.state.user,
|
|
34
|
+
...inventory
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
39
|
+
@Directive('@transaction')
|
|
40
|
+
@Mutation(returns => Inventory)
|
|
41
|
+
async updateInventory(@Arg('patch') patch: InventoryPatch, @Ctx() context: ResolverContext): Promise<Inventory> {
|
|
42
|
+
const { domain, user, tx } = context.state
|
|
43
|
+
|
|
44
|
+
const invQb: SelectQueryBuilder<Inventory> = await tx
|
|
45
|
+
.getRepository(Inventory)
|
|
46
|
+
.createQueryBuilder('INV')
|
|
47
|
+
.where('INV.domain_id = :domainId', { domainId: domain.id })
|
|
48
|
+
.andWhere('INV.bizplace_id = :bizplaceId', { bizplaceId: patch.bizplace.id })
|
|
49
|
+
.andWhere('INV.batch_id = :batchId', { batchId: patch.batchId })
|
|
50
|
+
.andWhere('INV.product_id = :productId', { productId: patch.product.id })
|
|
51
|
+
.andWhere('INV.packing_type = :packingType', { packingType: patch.packingType })
|
|
52
|
+
.andWhere('INV.packing_size = :packingSize', { packingSize: patch.packingSize })
|
|
53
|
+
|
|
54
|
+
if (patch?.cartonId) invQb.andWhere('INV.carton_id = :cartonId', { cartonId: patch.cartonId })
|
|
55
|
+
else invQb.andWhere('INV.pallet_id = :palletId', { palletId: patch.palletId })
|
|
56
|
+
|
|
57
|
+
const inventory = await invQb.getOne()
|
|
58
|
+
|
|
59
|
+
return await tx.getRepository(Inventory).save({
|
|
60
|
+
...inventory,
|
|
61
|
+
...patch,
|
|
62
|
+
updater: user
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
67
|
+
@Directive('@transaction')
|
|
68
|
+
@Mutation(returns => Inventory)
|
|
69
|
+
async updateInventoryRemark(
|
|
70
|
+
@Arg('id') id: string,
|
|
71
|
+
@Arg('remark') remark: string,
|
|
72
|
+
@Ctx() context: ResolverContext
|
|
73
|
+
): Promise<Inventory> {
|
|
74
|
+
const { domain, user, tx } = context.state
|
|
75
|
+
|
|
76
|
+
const repository = tx.getRepository(Inventory)
|
|
77
|
+
const inventory = await repository.findOne({ where: { domain: { id: domain.id }, id } })
|
|
78
|
+
|
|
79
|
+
return await repository.save({
|
|
80
|
+
...inventory,
|
|
81
|
+
remark,
|
|
82
|
+
updater: user
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
87
|
+
@Directive('@transaction')
|
|
88
|
+
@Mutation(returns => Boolean)
|
|
89
|
+
async deleteInventory(@Arg('name') name: string, @Ctx() context: ResolverContext): Promise<Boolean> {
|
|
90
|
+
const { tx } = context.state
|
|
91
|
+
|
|
92
|
+
await tx.getRepository(Inventory).delete({ domain: { id: context.state.domain.id }, name })
|
|
93
|
+
return true
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
97
|
+
@Directive('@transaction')
|
|
98
|
+
@Mutation(returns => [Inventory])
|
|
99
|
+
async updateMultipleInventory(
|
|
100
|
+
@Arg('patches', type => [InventoryPatch]) patches: InventoryPatch[],
|
|
101
|
+
@Ctx() context: ResolverContext
|
|
102
|
+
): Promise<Inventory[]> {
|
|
103
|
+
return await updateMultipleInventory(patches, context)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
107
|
+
@Directive('@transaction')
|
|
108
|
+
@Mutation(returns => Boolean)
|
|
109
|
+
async deleteInventories(
|
|
110
|
+
@Arg('ids', type => [String]) ids: string[],
|
|
111
|
+
@Ctx() context: ResolverContext
|
|
112
|
+
): Promise<Boolean> {
|
|
113
|
+
const { tx } = context.state
|
|
114
|
+
|
|
115
|
+
await tx.getRepository(Inventory).delete({
|
|
116
|
+
id: In(ids)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@Directive('@privilege(category: "inventory", privilege: "mutation")')
|
|
123
|
+
@Directive('@transaction')
|
|
124
|
+
@Mutation(returns => Boolean)
|
|
125
|
+
async inventoryTransfer(
|
|
126
|
+
@Arg('originPalletId') originPalletId: string,
|
|
127
|
+
@Arg('palletId', { nullable: true }) palletId: string,
|
|
128
|
+
@Arg('cartonId', { nullable: true }) cartonId: string,
|
|
129
|
+
@Arg('fromLocationName') fromLocationName: string,
|
|
130
|
+
@Arg('toLocationName') toLocationName: string,
|
|
131
|
+
@Arg('qty') qty: number,
|
|
132
|
+
@Arg('reason', { nullable: true }) reason: string,
|
|
133
|
+
@Ctx() context: ResolverContext
|
|
134
|
+
): Promise<Boolean> {
|
|
135
|
+
const { domain, user, tx } = context.state
|
|
136
|
+
|
|
137
|
+
const fromLocation: Location = await tx.getRepository(Location).findOne({
|
|
138
|
+
where: { domain: { id: domain.id }, name: fromLocationName },
|
|
139
|
+
relations: ['domain', 'warehouse']
|
|
140
|
+
})
|
|
141
|
+
// 1. check toLocation exist
|
|
142
|
+
const toLocation: Location = await tx.getRepository(Location).findOne({
|
|
143
|
+
where: { domain: { id: domain.id }, name: toLocationName },
|
|
144
|
+
relations: ['domain', 'warehouse']
|
|
145
|
+
})
|
|
146
|
+
if (!toLocation) throw new Error(context.t(`error.To_location_does_not_exists`))
|
|
147
|
+
|
|
148
|
+
if (qty < 0) throw new Error(`Invalid quantity`)
|
|
149
|
+
|
|
150
|
+
// 2. search for related inventory
|
|
151
|
+
const invQb: SelectQueryBuilder<Inventory> = await tx
|
|
152
|
+
.getRepository(Inventory)
|
|
153
|
+
.createQueryBuilder('INV')
|
|
154
|
+
.innerJoinAndSelect('INV.location', 'loc')
|
|
155
|
+
.innerJoinAndSelect('INV.product', 'product')
|
|
156
|
+
.innerJoinAndSelect('INV.bizplace', 'bizplace')
|
|
157
|
+
.leftJoinAndSelect('INV.refInventory', 'refInventory')
|
|
158
|
+
.leftJoinAndSelect('INV.reusablePallet', 'reusablePallet')
|
|
159
|
+
.leftJoinAndSelect('INV.productDetail', 'productDetail')
|
|
160
|
+
.leftJoinAndSelect('bizplace.domain', 'domain')
|
|
161
|
+
.leftJoinAndSelect('product.productDetails', 'productDetails')
|
|
162
|
+
.leftJoinAndSelect('INV.originInventory', 'originInventory')
|
|
163
|
+
.where('INV.domain_id = :domainId', { domainId: domain.id })
|
|
164
|
+
.andWhere('INV.pallet_id = :palletId', { palletId: originPalletId })
|
|
165
|
+
|
|
166
|
+
let inventory: Inventory = await invQb.getOne()
|
|
167
|
+
if (!inventory) throw new Error('Inventory not found')
|
|
168
|
+
const arrivalNotice: ArrivalNotice = await tx.getRepository(ArrivalNotice).findOne({
|
|
169
|
+
where: {
|
|
170
|
+
domain: { id: domain.id },
|
|
171
|
+
id: Equal(inventory.refOrderId)
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
if (qty > inventory.qty - (inventory?.lockedQty > 0 ? inventory.lockedQty : 0))
|
|
176
|
+
throw new Error('Transfer quantity exceeded inventory quantity')
|
|
177
|
+
|
|
178
|
+
if (toLocation.name == inventory.location.name) throw new Error('Inventory is in current location')
|
|
179
|
+
|
|
180
|
+
let transactionInventory = new InventoryUtil(tx, domain, user)
|
|
181
|
+
|
|
182
|
+
const sellercraft: Sellercraft = await tx.getRepository(Sellercraft).findOne({
|
|
183
|
+
where: { domain: { id: inventory.bizplace.domain.id }, status: SellercraftStatus.ACTIVE },
|
|
184
|
+
relations: ['domain']
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
const partialTransferFunc = async function (inventory, qty, fromLocation, toLocation, cartonId, palletId, context) {
|
|
188
|
+
const { user, tx } = context.state
|
|
189
|
+
|
|
190
|
+
if (qty < inventory.qty - (inventory?.lockedQty > 0 ? inventory.lockedQty : 0)) {
|
|
191
|
+
if (!palletId || palletId.trim() == '') throw new Error('Require new lot id for partial transfer')
|
|
192
|
+
|
|
193
|
+
let transferQty = qty
|
|
194
|
+
let transferUomValue = (inventory.uomValue / inventory.qty) * qty
|
|
195
|
+
let newInventory = {
|
|
196
|
+
...inventory,
|
|
197
|
+
palletId,
|
|
198
|
+
location: toLocation,
|
|
199
|
+
warehouse: toLocation.warehouse,
|
|
200
|
+
zone: toLocation.zone,
|
|
201
|
+
creator: user,
|
|
202
|
+
updater: user
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
delete newInventory.id
|
|
206
|
+
delete newInventory.updatedAt
|
|
207
|
+
|
|
208
|
+
newInventory.cartonId = cartonId
|
|
209
|
+
newInventory.qty = transferQty
|
|
210
|
+
newInventory.lockedQty = 0
|
|
211
|
+
newInventory.uomValue = transferUomValue
|
|
212
|
+
newInventory.lockedUomValue = 0
|
|
213
|
+
if (inventory.originInventory?.id) {
|
|
214
|
+
newInventory.originInventory = { id: inventory.originInventory.id }
|
|
215
|
+
} else {
|
|
216
|
+
newInventory.originInventory = { id: inventory.id }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
await transactionInventory.transactionInventory(
|
|
220
|
+
{ ...inventory, qty: inventory.qty - transferQty, uomValue: inventory.uomValue - transferUomValue },
|
|
221
|
+
arrivalNotice,
|
|
222
|
+
-transferQty,
|
|
223
|
+
-transferUomValue,
|
|
224
|
+
INVENTORY_TRANSACTION_TYPE.RELOCATE,
|
|
225
|
+
reason || null
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
await transactionInventory.transactionInventory(
|
|
229
|
+
newInventory,
|
|
230
|
+
arrivalNotice,
|
|
231
|
+
transferQty,
|
|
232
|
+
transferUomValue,
|
|
233
|
+
INVENTORY_TRANSACTION_TYPE.RELOCATE,
|
|
234
|
+
reason || null
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
if (sellercraft) {
|
|
238
|
+
const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
|
|
239
|
+
await sellercraftCtrl.updateSellercraftStock(sellercraft, inventory)
|
|
240
|
+
await sellercraftCtrl.updateSellercraftStock(sellercraft, newInventory)
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
inventory.warehouse = toLocation.warehouse
|
|
244
|
+
inventory.location = toLocation
|
|
245
|
+
inventory.zone = toLocation.zone
|
|
246
|
+
inventory.updater = user
|
|
247
|
+
inventory.cartonId = cartonId
|
|
248
|
+
|
|
249
|
+
let isEmptyLocation = await tx.getRepository(Inventory).find({
|
|
250
|
+
where: { location: { id: fromLocation.id } }
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
if ((isEmptyLocation.length = 1)) {
|
|
254
|
+
await tx.getRepository(Location).save({
|
|
255
|
+
...fromLocation,
|
|
256
|
+
status: LOCATION_STATUS.EMPTY
|
|
257
|
+
})
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
await transactionInventory.transactionInventory(
|
|
261
|
+
inventory,
|
|
262
|
+
arrivalNotice,
|
|
263
|
+
0,
|
|
264
|
+
0,
|
|
265
|
+
INVENTORY_TRANSACTION_TYPE.RELOCATE,
|
|
266
|
+
reason || null
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
if (sellercraft) {
|
|
270
|
+
const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
|
|
271
|
+
await sellercraftCtrl.updateSellercraftStock(sellercraft, inventory)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (inventory.cartonId && inventory.cartonId != '') {
|
|
277
|
+
if (!cartonId) throw new Error('Require carton id')
|
|
278
|
+
|
|
279
|
+
if (qty < inventory.qty - (inventory?.lockedQty > 0 ? inventory.lockedQty : 0)) {
|
|
280
|
+
palletId = await generateId({
|
|
281
|
+
domain: domain,
|
|
282
|
+
type: RULE_TYPE.LOT_NUMBER_ID,
|
|
283
|
+
seed: { date: DateGenerator.generateDate() }
|
|
284
|
+
})
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
await partialTransferFunc(inventory, qty, fromLocation, toLocation, cartonId, palletId, context)
|
|
288
|
+
} else {
|
|
289
|
+
if (!palletId) {
|
|
290
|
+
palletId = await generateId({
|
|
291
|
+
domain: domain,
|
|
292
|
+
type: RULE_TYPE.LOT_NUMBER_ID,
|
|
293
|
+
seed: { date: DateGenerator.generateDate() }
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
await partialTransferFunc(inventory, qty, fromLocation, toLocation, null, palletId, context)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return true
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export async function updateMultipleInventory(
|
|
304
|
+
patches: InventoryPatch[],
|
|
305
|
+
context: ResolverContext
|
|
306
|
+
): Promise<Inventory[]> {
|
|
307
|
+
const { domain, user, tx } = context.state
|
|
308
|
+
|
|
309
|
+
let results = []
|
|
310
|
+
const _createRecords = patches.filter((patch: any) => !patch.id)
|
|
311
|
+
const _updateRecords = patches.filter((patch: any) => patch.id)
|
|
312
|
+
|
|
313
|
+
const inventoryRepo = tx.getRepository(Inventory)
|
|
314
|
+
if (_createRecords.length > 0) {
|
|
315
|
+
let today = new Date()
|
|
316
|
+
let year = today.getFullYear()
|
|
317
|
+
let month = today.getMonth()
|
|
318
|
+
let date = today.getDate()
|
|
319
|
+
|
|
320
|
+
for (let i = 0; i < _createRecords.length; i++) {
|
|
321
|
+
const total = await tx.getRepository(Inventory).countBy({
|
|
322
|
+
createdAt: MoreThan(new Date(year, month, date))
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
const newRecord = _createRecords[i]
|
|
326
|
+
|
|
327
|
+
if (newRecord.location && newRecord.location.id) {
|
|
328
|
+
var location = await tx.getRepository(Location).findOne({
|
|
329
|
+
where: { id: newRecord.location.id },
|
|
330
|
+
relations: ['warehouse']
|
|
331
|
+
})
|
|
332
|
+
newRecord.location = location
|
|
333
|
+
newRecord.zone = location.zone
|
|
334
|
+
newRecord.warehouse = location.warehouse
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (newRecord.bizplace && newRecord.bizplace.id) {
|
|
338
|
+
newRecord.bizplace = (await tx.getRepository(Bizplace).findOneBy({ id: newRecord.bizplace.id })) as any
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (newRecord.product && newRecord.product.id) {
|
|
342
|
+
var product = (await tx.getRepository(Product).findOneBy({ id: newRecord.product.id })) as any
|
|
343
|
+
newRecord.product = product
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let palletId =
|
|
347
|
+
'P' +
|
|
348
|
+
year.toString().substr(year.toString().length - 2) +
|
|
349
|
+
('0' + (month + 1).toString()).substr(('0' + (month + 1).toString()).toString().length - 2) +
|
|
350
|
+
('0' + date.toString()).substr(('0' + date.toString()).length - 2) +
|
|
351
|
+
('0000' + (total + 1).toString()).substr(('0000' + (total + 1).toString()).length - 4)
|
|
352
|
+
|
|
353
|
+
newRecord.name = palletId
|
|
354
|
+
newRecord.status = newRecord.qty < 1 ? 'TERMINATED' : 'STORED'
|
|
355
|
+
newRecord.palletId = palletId
|
|
356
|
+
|
|
357
|
+
const result = await inventoryRepo.save({
|
|
358
|
+
domain: domain,
|
|
359
|
+
creator: user,
|
|
360
|
+
updater: user,
|
|
361
|
+
lastSeq: 0,
|
|
362
|
+
...newRecord
|
|
363
|
+
} as any)
|
|
364
|
+
|
|
365
|
+
await tx.getRepository(InventoryHistory).save({
|
|
366
|
+
...newRecord,
|
|
367
|
+
domain: domain,
|
|
368
|
+
creator: user,
|
|
369
|
+
updater: user,
|
|
370
|
+
name: InventoryNoGenerator.inventoryHistoryName(),
|
|
371
|
+
seq: 0,
|
|
372
|
+
transactionType: 'NEW',
|
|
373
|
+
productId: newRecord.product.id,
|
|
374
|
+
warehouseId: newRecord.warehouse.id,
|
|
375
|
+
locationId: newRecord.location.id
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
results.push({ ...result, cuFlag: '+' })
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (_updateRecords.length > 0) {
|
|
383
|
+
for (let i = 0; i < _updateRecords.length; i++) {
|
|
384
|
+
const newRecord = _updateRecords[i]
|
|
385
|
+
const newHistoryRecord = JSON.parse(JSON.stringify(_updateRecords[i]))
|
|
386
|
+
let transactionType = ''
|
|
387
|
+
|
|
388
|
+
let inventory = await inventoryRepo.findOne({
|
|
389
|
+
where: { id: newRecord.id },
|
|
390
|
+
relations: ['warehouse', 'location', 'product', 'bizplace']
|
|
391
|
+
})
|
|
392
|
+
newHistoryRecord.openingQty = inventory.qty
|
|
393
|
+
newHistoryRecord.openingUomValue = inventory.uomValue
|
|
394
|
+
|
|
395
|
+
// Get last sequence from InventoryHistory
|
|
396
|
+
let latestEntry = await tx.getRepository(InventoryHistory).find({
|
|
397
|
+
where: { palletId: inventory.palletId },
|
|
398
|
+
order: { seq: 'DESC' },
|
|
399
|
+
take: 1
|
|
400
|
+
})
|
|
401
|
+
let lastSeq = latestEntry[0].seq
|
|
402
|
+
|
|
403
|
+
// Condition 1: Change location (RELOCATE)
|
|
404
|
+
if (newRecord.location && newRecord.location.id) {
|
|
405
|
+
var location = await tx.getRepository(Location).findOne({
|
|
406
|
+
where: { id: newRecord.location.id },
|
|
407
|
+
relations: ['warehouse']
|
|
408
|
+
})
|
|
409
|
+
newRecord.location = location
|
|
410
|
+
newRecord.zone = location.zone
|
|
411
|
+
newRecord.warehouse = location.warehouse
|
|
412
|
+
|
|
413
|
+
newHistoryRecord.location = location
|
|
414
|
+
newHistoryRecord.zone = location.zone
|
|
415
|
+
newHistoryRecord.warehouse = location.warehouse
|
|
416
|
+
|
|
417
|
+
transactionType = 'RELOCATE'
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Condition 2: Change of qty or uomValue.
|
|
421
|
+
// Set qty movement for inventory history
|
|
422
|
+
if (typeof newRecord.qty != 'undefined') {
|
|
423
|
+
transactionType = 'ADJUSTMENT'
|
|
424
|
+
newHistoryRecord.qty = newRecord.qty - inventory.qty
|
|
425
|
+
if (newRecord.qty < 1) {
|
|
426
|
+
newRecord.status = 'TERMINATED'
|
|
427
|
+
newRecord.qty = 0
|
|
428
|
+
newRecord.uomValue = 0
|
|
429
|
+
}
|
|
430
|
+
} else {
|
|
431
|
+
newHistoryRecord.qty = 0
|
|
432
|
+
}
|
|
433
|
+
// Set uomValue movement for inventory history
|
|
434
|
+
if (typeof newRecord.uomValue != 'undefined') {
|
|
435
|
+
transactionType = 'ADJUSTMENT'
|
|
436
|
+
newHistoryRecord.uomValue = newRecord.uomValue - inventory.uomValue
|
|
437
|
+
if (newRecord.uomValue < 1) {
|
|
438
|
+
newRecord.uomValue = 0
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
newHistoryRecord.uomValue = 0
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Condition 3: Change of bizplace or product or batch id or packing type
|
|
445
|
+
if (newRecord.bizplace && newRecord.bizplace.id) {
|
|
446
|
+
newRecord.bizplace = (await tx.getRepository(Bizplace).findOneBy({ id: newRecord.bizplace.id })) as any
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (newRecord.product && newRecord.product.id) {
|
|
450
|
+
newRecord.product = (await tx.getRepository(Product).findOneBy({ id: newRecord.product.id })) as any
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (
|
|
454
|
+
(newRecord.product && newRecord.product.id) ||
|
|
455
|
+
(newRecord.bizplace && newRecord.bizplace.id) ||
|
|
456
|
+
newRecord.batchId ||
|
|
457
|
+
newRecord.packingType
|
|
458
|
+
) {
|
|
459
|
+
if (
|
|
460
|
+
inventory.bizplace.id !== (newRecord.bizplace ? newRecord.bizplace.id : '') ||
|
|
461
|
+
inventory.product.id !== (newRecord.product ? newRecord.product.id : '') ||
|
|
462
|
+
inventory.batchId !== newRecord.batchId ||
|
|
463
|
+
inventory.packingType !== newRecord.packingType
|
|
464
|
+
) {
|
|
465
|
+
transactionType = 'ADJUSTMENT'
|
|
466
|
+
lastSeq = lastSeq + 1
|
|
467
|
+
let inventoryHistory: any = {
|
|
468
|
+
...inventory,
|
|
469
|
+
domain: domain,
|
|
470
|
+
bizplace: inventory.bizplace.id,
|
|
471
|
+
openingQty: inventory.qty,
|
|
472
|
+
openingUomValue: inventory.uomValue,
|
|
473
|
+
qty: -inventory.qty || 0,
|
|
474
|
+
uomValue: -inventory.uomValue || 0,
|
|
475
|
+
name: InventoryNoGenerator.inventoryHistoryName(),
|
|
476
|
+
seq: lastSeq,
|
|
477
|
+
inventory,
|
|
478
|
+
transactionType: transactionType,
|
|
479
|
+
status: INVENTORY_STATUS.TERMINATED,
|
|
480
|
+
productId: inventory.product.id,
|
|
481
|
+
warehouseId: inventory.warehouse.id,
|
|
482
|
+
locationId: inventory.location.id,
|
|
483
|
+
creator: user,
|
|
484
|
+
updater: user
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
delete inventoryHistory.id
|
|
488
|
+
await tx.getRepository(InventoryHistory).save(inventoryHistory as any)
|
|
489
|
+
|
|
490
|
+
newHistoryRecord.qty = newRecord.qty || inventory.qty
|
|
491
|
+
newHistoryRecord.uomValue = newRecord.uomValue || inventory.uomValue || 0
|
|
492
|
+
newHistoryRecord.openingQty = 0
|
|
493
|
+
newHistoryRecord.openingUomValue = 0
|
|
494
|
+
newHistoryRecord.batchId = newRecord.batchId || inventory.batchId || '-'
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
//Transaction type will be RELOCATE if there is only location change. Any other changes will be considered Adjustment.
|
|
499
|
+
lastSeq = lastSeq + 1
|
|
500
|
+
let inventoryHistory = {
|
|
501
|
+
...inventory,
|
|
502
|
+
...newHistoryRecord,
|
|
503
|
+
domain: domain,
|
|
504
|
+
creator: user,
|
|
505
|
+
updater: user,
|
|
506
|
+
name: InventoryNoGenerator.inventoryHistoryName(),
|
|
507
|
+
seq: lastSeq,
|
|
508
|
+
inventory,
|
|
509
|
+
transactionType: transactionType == '' ? 'ADJUSTMENT' : transactionType,
|
|
510
|
+
productId: newHistoryRecord.product ? newHistoryRecord.product.id : inventory.product.id,
|
|
511
|
+
warehouseId: newHistoryRecord.warehouse ? newHistoryRecord.warehouse.id : inventory.warehouse.id,
|
|
512
|
+
locationId: newHistoryRecord.location ? newHistoryRecord.location.id : inventory.location.id
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
delete inventoryHistory.id
|
|
516
|
+
await tx.getRepository(InventoryHistory).save(inventoryHistory)
|
|
517
|
+
|
|
518
|
+
const result = await inventoryRepo.save({
|
|
519
|
+
...inventory,
|
|
520
|
+
...newRecord,
|
|
521
|
+
updater: user,
|
|
522
|
+
lastSeq: lastSeq
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
results.push({ ...result, cuFlag: 'M' })
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return results
|
|
530
|
+
}
|