@rytass/wms-module-core 0.9.0 → 0.10.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/README.md +15 -2
- package/docs/README.md +2 -2
- package/docs/api-reference.md +129 -10
- package/docs/host-integration.md +172 -0
- package/docs/integration-guide.md +84 -43
- package/index.cjs.js +2454 -1658
- package/index.d.ts +2 -0
- package/lib/events/inventory-count-lock.subscriber.d.ts +17 -5
- package/lib/models/metadata/index.d.ts +1 -0
- package/lib/models/metadata/sequence.entity.d.ts +9 -0
- package/lib/models/models.module.d.ts +7 -0
- package/lib/services/allocate-inventory-order.service.d.ts +3 -3
- package/lib/services/batch.service.d.ts +13 -1
- package/lib/services/consumption-order.service.d.ts +6 -6
- package/lib/services/helper.service.d.ts +10 -4
- package/lib/services/hold-inventory-order.service.d.ts +3 -3
- package/lib/services/index.d.ts +1 -0
- package/lib/services/inventory-count-order.service.d.ts +28 -9
- package/lib/services/loader.service.d.ts +11 -3
- package/lib/services/material.service.d.ts +1 -1
- package/lib/services/merge-batch-order.service.d.ts +2 -2
- package/lib/services/quality-inspection-order.service.d.ts +1 -1
- package/lib/services/receive-inventory-order.service.d.ts +4 -4
- package/lib/services/reclassify-inventory-order.service.d.ts +2 -2
- package/lib/services/scrape.service.d.ts +1 -1
- package/lib/services/sequence.service.d.ts +58 -0
- package/lib/services/ship-inventory-order.service.d.ts +6 -6
- package/lib/services/split-batch-order.service.d.ts +2 -2
- package/lib/services/stock.service.d.ts +48 -2
- package/lib/services/transfer-customer-order.service.d.ts +3 -3
- package/lib/services/transfer-order.service.d.ts +7 -5
- package/lib/services/transfer-vendor-order.service.d.ts +3 -3
- package/lib/table-prefix/logical-table-name.d.ts +21 -0
- package/lib/table-prefix/table-prefix.d.ts +30 -0
- package/lib/typings/core-module-options.d.ts +31 -0
- package/lib/typings/create-transfer-order.input.d.ts +2 -0
- package/lib/typings/index.d.ts +20 -0
- package/lib/utils/run-in-transaction.d.ts +23 -0
- package/lib/utils/table-path.d.ts +13 -0
- package/package.json +9 -6
- package/index.js +0 -98
- package/lib/constants/consumption-change-kind.js +0 -20
- package/lib/constants/inventory-count-status.js +0 -22
- package/lib/constants/operations.js +0 -76
- package/lib/constants/order-type.js +0 -33
- package/lib/constants/stock-status.js +0 -61
- package/lib/core.module.js +0 -287
- package/lib/events/dispatch-scope.js +0 -29
- package/lib/events/inventory-count-lock.subscriber.js +0 -98
- package/lib/events/stock-change.subscriber.js +0 -252
- package/lib/events/wms-event.collector.js +0 -22
- package/lib/events/wms-event.dispatcher.js +0 -63
- package/lib/events/wms-event.tokens.js +0 -12
- package/lib/events/wms-operation-runner.js +0 -101
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.js +0 -40
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js +0 -104
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js +0 -33
- package/lib/models/extensions/consumption-order/consumption-order-change.entity.js +0 -59
- package/lib/models/extensions/consumption-order/consumption-order-item.entity.js +0 -114
- package/lib/models/extensions/consumption-order/consumption-order.entity.js +0 -69
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order-change.entity.js +0 -40
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js +0 -98
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order.entity.js +0 -43
- package/lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.js +0 -50
- package/lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.js +0 -191
- package/lib/models/extensions/inventory-count-order/inventory-count-order.entity.js +0 -155
- package/lib/models/extensions/merge-batch-order/merge-batch-order-change.entity.js +0 -40
- package/lib/models/extensions/merge-batch-order/merge-batch-order-item.entity.js +0 -75
- package/lib/models/extensions/merge-batch-order/merge-batch-order.entity.js +0 -33
- package/lib/models/extensions/order-reference/order-reference.entity.js +0 -95
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order-change.entity.js +0 -45
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js +0 -94
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order.entity.js +0 -38
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js +0 -46
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js +0 -89
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order.entity.js +0 -57
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.js +0 -40
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js +0 -75
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js +0 -33
- package/lib/models/extensions/scrape-order/scrape-order-change.entity.js +0 -40
- package/lib/models/extensions/scrape-order/scrape-order-item.entity.js +0 -94
- package/lib/models/extensions/scrape-order/scrape-order.entity.js +0 -33
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-change.entity.js +0 -46
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-item.entity.js +0 -90
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.js +0 -65
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order.entity.js +0 -56
- package/lib/models/extensions/split-batch-order/split-batch-order-change.entity.js +0 -40
- package/lib/models/extensions/split-batch-order/split-batch-order-item.entity.js +0 -75
- package/lib/models/extensions/split-batch-order/split-batch-order.entity.js +0 -33
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order-change.entity.js +0 -40
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js +0 -99
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order.entity.js +0 -33
- package/lib/models/extensions/transfer-order/transfer-order-change.entity.js +0 -40
- package/lib/models/extensions/transfer-order/transfer-order-item.entity.js +0 -109
- package/lib/models/extensions/transfer-order/transfer-order.entity.js +0 -53
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.js +0 -40
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js +0 -99
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order.entity.js +0 -33
- package/lib/models/metadata/batch-group.entity.js +0 -31
- package/lib/models/metadata/batch.entity.js +0 -131
- package/lib/models/metadata/loader-type.entity.js +0 -43
- package/lib/models/metadata/loader-unit.entity.js +0 -21
- package/lib/models/metadata/loader.entity.js +0 -98
- package/lib/models/metadata/location.entity.js +0 -28
- package/lib/models/metadata/material-unit.entity.js +0 -51
- package/lib/models/metadata/material.entity.js +0 -28
- package/lib/models/metadata/order.entity.js +0 -5
- package/lib/models/metadata/sale-order.entity.js +0 -21
- package/lib/models/metadata/stock.entity.js +0 -29
- package/lib/models/metadata/unit.entity.js +0 -21
- package/lib/models/models.module.js +0 -158
- package/lib/models/wms-entities.js +0 -82
- package/lib/services/allocate-inventory-order.service.js +0 -310
- package/lib/services/base-order.service.js +0 -76
- package/lib/services/batch.service.js +0 -272
- package/lib/services/consumption-order.service.js +0 -504
- package/lib/services/helper.service.js +0 -28
- package/lib/services/hold-inventory-order.service.js +0 -296
- package/lib/services/inventory-count-order.service.js +0 -648
- package/lib/services/loader.service.js +0 -291
- package/lib/services/location.service.js +0 -139
- package/lib/services/material.service.js +0 -181
- package/lib/services/merge-batch-order.service.js +0 -206
- package/lib/services/order-reference.service.js +0 -136
- package/lib/services/quality-inspection-order.service.js +0 -146
- package/lib/services/receive-inventory-order.service.js +0 -468
- package/lib/services/reclassify-inventory-order.service.js +0 -192
- package/lib/services/sale-order.service.js +0 -20
- package/lib/services/scrape.service.js +0 -150
- package/lib/services/ship-inventory-order.service.js +0 -610
- package/lib/services/split-batch-order.service.js +0 -191
- package/lib/services/stock.service.js +0 -559
- package/lib/services/transfer-customer-order.service.js +0 -290
- package/lib/services/transfer-order.service.js +0 -641
- package/lib/services/transfer-vendor-order.service.js +0 -290
- package/lib/services/unit.service.js +0 -76
- package/lib/services/warehouse-map.service.js +0 -50
- package/lib/typings/core-module-options.js +0 -3
- package/lib/typings/entity-overrides.d.ts +0 -34
- package/lib/typings/entity-overrides.js +0 -7
- package/lib/typings/find-receive-inventory-order.input.js +0 -9
- package/lib/typings/find-ship-inventory-order.input.js +0 -9
- package/src/lib/models/extensions/README.md +0 -219
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
import { __decorate, __param, __metadata } from 'tslib';
|
|
2
|
-
import { Injectable, Inject } from '@nestjs/common';
|
|
3
|
-
import '../models/metadata/batch.entity.js';
|
|
4
|
-
import '../models/metadata/batch-group.entity.js';
|
|
5
|
-
import '../models/metadata/loader.entity.js';
|
|
6
|
-
import '../models/metadata/loader-type.entity.js';
|
|
7
|
-
import '../models/metadata/loader-unit.entity.js';
|
|
8
|
-
import '../models/metadata/location.entity.js';
|
|
9
|
-
import '../models/metadata/material.entity.js';
|
|
10
|
-
import '../models/metadata/material-unit.entity.js';
|
|
11
|
-
import '@rytass/wms-base-nestjs-module';
|
|
12
|
-
import '../models/metadata/sale-order.entity.js';
|
|
13
|
-
import { StockEntity } from '../models/metadata/stock.entity.js';
|
|
14
|
-
import '../models/metadata/unit.entity.js';
|
|
15
|
-
import { WmsEntities } from '../models/wms-entities.js';
|
|
16
|
-
import '../models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js';
|
|
17
|
-
import '../models/extensions/receive-inventory-order/receive-inventory-order.entity.js';
|
|
18
|
-
import '../models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js';
|
|
19
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-item.entity.js';
|
|
20
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order.entity.js';
|
|
21
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.js';
|
|
22
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-change.entity.js';
|
|
23
|
-
import '../models/extensions/transfer-order/transfer-order.entity.js';
|
|
24
|
-
import '../models/extensions/transfer-order/transfer-order-item.entity.js';
|
|
25
|
-
import '../models/extensions/transfer-order/transfer-order-change.entity.js';
|
|
26
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order.entity.js';
|
|
27
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js';
|
|
28
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order-change.entity.js';
|
|
29
|
-
import { AllocateInventoryOrderEntity } from '../models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js';
|
|
30
|
-
import { AllocateInventoryOrderItemEntity } from '../models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js';
|
|
31
|
-
import { AllocateInventoryOrderChangeEntityClass } from '../models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.js';
|
|
32
|
-
import '../models/extensions/split-batch-order/split-batch-order.entity.js';
|
|
33
|
-
import '../models/extensions/split-batch-order/split-batch-order-item.entity.js';
|
|
34
|
-
import '../models/extensions/split-batch-order/split-batch-order-change.entity.js';
|
|
35
|
-
import '../models/extensions/merge-batch-order/merge-batch-order.entity.js';
|
|
36
|
-
import '../models/extensions/merge-batch-order/merge-batch-order-item.entity.js';
|
|
37
|
-
import '../models/extensions/merge-batch-order/merge-batch-order-change.entity.js';
|
|
38
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js';
|
|
39
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js';
|
|
40
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.js';
|
|
41
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order.entity.js';
|
|
42
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js';
|
|
43
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.js';
|
|
44
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order.entity.js';
|
|
45
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js';
|
|
46
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order-change.entity.js';
|
|
47
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order.entity.js';
|
|
48
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js';
|
|
49
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order-change.entity.js';
|
|
50
|
-
import '../models/extensions/scrape-order/scrape-order.entity.js';
|
|
51
|
-
import '../models/extensions/scrape-order/scrape-order-item.entity.js';
|
|
52
|
-
import '../models/extensions/scrape-order/scrape-order-change.entity.js';
|
|
53
|
-
import '../models/extensions/inventory-count-order/inventory-count-order.entity.js';
|
|
54
|
-
import '../models/extensions/inventory-count-order/inventory-count-order-item.entity.js';
|
|
55
|
-
import '../models/extensions/inventory-count-order/inventory-count-order-change.entity.js';
|
|
56
|
-
import '../models/extensions/consumption-order/consumption-order.entity.js';
|
|
57
|
-
import '../models/extensions/consumption-order/consumption-order-item.entity.js';
|
|
58
|
-
import '../models/extensions/consumption-order/consumption-order-change.entity.js';
|
|
59
|
-
import '../models/extensions/order-reference/order-reference.entity.js';
|
|
60
|
-
import { DataSource, Not, IsNull } from 'typeorm';
|
|
61
|
-
import { HelperService } from './helper.service.js';
|
|
62
|
-
import { MaterialService } from './material.service.js';
|
|
63
|
-
import { BatchService } from './batch.service.js';
|
|
64
|
-
import { StockService } from './stock.service.js';
|
|
65
|
-
|
|
66
|
-
let AllocateInventoryOrderService = class AllocateInventoryOrderService {
|
|
67
|
-
dataSource;
|
|
68
|
-
entities;
|
|
69
|
-
helperService;
|
|
70
|
-
materialService;
|
|
71
|
-
batchService;
|
|
72
|
-
stockService;
|
|
73
|
-
allocateInventoryOrderEntity;
|
|
74
|
-
allocateInventoryOrderItemEntity;
|
|
75
|
-
stockEntity;
|
|
76
|
-
seqPrefix = 'AIO';
|
|
77
|
-
constructor(dataSource, entities, helperService, materialService, batchService, stockService, allocateInventoryOrderEntity, allocateInventoryOrderItemEntity, stockEntity) {
|
|
78
|
-
this.dataSource = dataSource;
|
|
79
|
-
this.entities = entities;
|
|
80
|
-
this.helperService = helperService;
|
|
81
|
-
this.materialService = materialService;
|
|
82
|
-
this.batchService = batchService;
|
|
83
|
-
this.stockService = stockService;
|
|
84
|
-
this.allocateInventoryOrderEntity = allocateInventoryOrderEntity;
|
|
85
|
-
this.allocateInventoryOrderItemEntity = allocateInventoryOrderItemEntity;
|
|
86
|
-
this.stockEntity = stockEntity;
|
|
87
|
-
}
|
|
88
|
-
getRepositories(manager) {
|
|
89
|
-
return {
|
|
90
|
-
allocateInventoryOrder: manager.getRepository(this.allocateInventoryOrderEntity),
|
|
91
|
-
allocateInventoryOrderItem: manager.getRepository(this.allocateInventoryOrderItemEntity),
|
|
92
|
-
allocateInventoryOrderChange: manager.getRepository(AllocateInventoryOrderChangeEntityClass),
|
|
93
|
-
order: manager.getRepository(this.entities.order),
|
|
94
|
-
stock: manager.getRepository(this.stockEntity),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
async processAllocateInventoryOrderItems(items, manager) {
|
|
98
|
-
const repos = this.getRepositories(manager);
|
|
99
|
-
await Promise.all(items.map(async (data) => {
|
|
100
|
-
// Ensure required fields are present
|
|
101
|
-
if (!data.batchId || !data.materialId)
|
|
102
|
-
return;
|
|
103
|
-
// Split batch first with saleId/saleSerialId to track allocation state
|
|
104
|
-
const splittedBatch = await this.batchService.splitBatch({
|
|
105
|
-
id: data.batchId,
|
|
106
|
-
saleId: data.saleId,
|
|
107
|
-
saleSerialId: data.saleSerialId,
|
|
108
|
-
manager,
|
|
109
|
-
});
|
|
110
|
-
// Save item with ORIGINAL batch id to maintain source state
|
|
111
|
-
const item = await repos.allocateInventoryOrderItem.save(repos.allocateInventoryOrderItem.create(data));
|
|
112
|
-
const base = await repos.order.save({});
|
|
113
|
-
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
114
|
-
await repos.allocateInventoryOrderChange.save(repos.allocateInventoryOrderChange.create({
|
|
115
|
-
item,
|
|
116
|
-
order: base,
|
|
117
|
-
quantityChanged: item.targetAllocatedQuantity * conversionFactor,
|
|
118
|
-
}));
|
|
119
|
-
// Stock movements track the allocation through splitted batch
|
|
120
|
-
await this.stockService.validateStockAvailability({
|
|
121
|
-
materialId: item.materialId,
|
|
122
|
-
batchId: item.batchId,
|
|
123
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
124
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
125
|
-
quantity: item.targetAllocatedQuantity * conversionFactor,
|
|
126
|
-
}, manager);
|
|
127
|
-
await repos.stock.save(repos.stock.create([
|
|
128
|
-
// deduct from original batch available stock
|
|
129
|
-
{
|
|
130
|
-
orderId: base.id,
|
|
131
|
-
materialId: item.materialId,
|
|
132
|
-
batchId: item.batchId, // original batch
|
|
133
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
134
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
135
|
-
quantity: -1 * item.targetAllocatedQuantity * conversionFactor,
|
|
136
|
-
},
|
|
137
|
-
// add to splitted batch in allocated state
|
|
138
|
-
{
|
|
139
|
-
orderId: base.id,
|
|
140
|
-
materialId: item.materialId,
|
|
141
|
-
batchId: splittedBatch.id, // splitted batch marked with saleId
|
|
142
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
143
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
144
|
-
quantity: item.targetAllocatedQuantity * conversionFactor,
|
|
145
|
-
},
|
|
146
|
-
]));
|
|
147
|
-
}));
|
|
148
|
-
}
|
|
149
|
-
async createAllocateInventoryOrder(options, manager = this.dataSource.manager) {
|
|
150
|
-
const repos = this.getRepositories(manager);
|
|
151
|
-
const key = await this.helperService.getSeq(this.seqPrefix);
|
|
152
|
-
const { items, ...orderOptions } = options;
|
|
153
|
-
const order = await repos.allocateInventoryOrder.save(repos.allocateInventoryOrder.create({
|
|
154
|
-
key,
|
|
155
|
-
...orderOptions,
|
|
156
|
-
}));
|
|
157
|
-
if (items?.length) {
|
|
158
|
-
const processedItems = items.map((item) => ({
|
|
159
|
-
...item,
|
|
160
|
-
orderId: order.id,
|
|
161
|
-
}));
|
|
162
|
-
await this.processAllocateInventoryOrderItems(processedItems, manager);
|
|
163
|
-
}
|
|
164
|
-
return order;
|
|
165
|
-
}
|
|
166
|
-
async updateAllocateInventoryOrder({ id, manager = this.dataSource.manager, ...options }) {
|
|
167
|
-
const repos = this.getRepositories(manager);
|
|
168
|
-
const { items, ...orderOptions } = options;
|
|
169
|
-
const order = await repos.allocateInventoryOrder.findOneBy({ id });
|
|
170
|
-
const updated = await repos.allocateInventoryOrder.save({
|
|
171
|
-
...order,
|
|
172
|
-
...orderOptions,
|
|
173
|
-
});
|
|
174
|
-
if (items?.length) {
|
|
175
|
-
const existingItemIds = new Set((await repos.allocateInventoryOrderItem.find({
|
|
176
|
-
where: { orderId: id },
|
|
177
|
-
relations: { changes: true },
|
|
178
|
-
})).map((item) => item.id));
|
|
179
|
-
const newItems = [];
|
|
180
|
-
const existingItems = [];
|
|
181
|
-
items.forEach((item) => {
|
|
182
|
-
if (!item.id)
|
|
183
|
-
newItems.push({ ...item, orderId: id });
|
|
184
|
-
else if (existingItemIds.has(item.id))
|
|
185
|
-
existingItems.push({ ...item, orderId: id });
|
|
186
|
-
});
|
|
187
|
-
if (newItems.length)
|
|
188
|
-
await this.processAllocateInventoryOrderItems(newItems, manager);
|
|
189
|
-
if (existingItems.length)
|
|
190
|
-
await repos.allocateInventoryOrderItem.save(await Promise.all(existingItems.map((item) => repos.allocateInventoryOrderItem.create(item))));
|
|
191
|
-
}
|
|
192
|
-
return updated;
|
|
193
|
-
}
|
|
194
|
-
async deallocate(options, manager = this.dataSource.manager) {
|
|
195
|
-
if (!Array.isArray(options))
|
|
196
|
-
options = [options];
|
|
197
|
-
const repos = this.getRepositories(manager);
|
|
198
|
-
const key = await this.helperService.getSeq(this.seqPrefix, manager);
|
|
199
|
-
const order = await repos.allocateInventoryOrder.save(repos.allocateInventoryOrder.create({ key }));
|
|
200
|
-
if (options.length > 0) {
|
|
201
|
-
await Promise.all(options.map(async (data) => {
|
|
202
|
-
// Ensure required fields are present
|
|
203
|
-
if (!data.batchId || !data.materialId)
|
|
204
|
-
return;
|
|
205
|
-
const originalBatch = await this.batchService.batchRepository.findOneByOrFail({
|
|
206
|
-
id: data.batchId,
|
|
207
|
-
saleId: Not(IsNull()),
|
|
208
|
-
saleSerialId: Not(IsNull()),
|
|
209
|
-
});
|
|
210
|
-
const splittedBatch = await this.batchService.splitBatch({
|
|
211
|
-
id: data.batchId,
|
|
212
|
-
saleId: null, // mark splitted batch with null saleId to indicate deallocation
|
|
213
|
-
saleSerialId: null,
|
|
214
|
-
manager,
|
|
215
|
-
});
|
|
216
|
-
// Save item with ORIGINAL batch id to maintain source state
|
|
217
|
-
const item = await repos.allocateInventoryOrderItem.save(repos.allocateInventoryOrderItem.create({
|
|
218
|
-
...data,
|
|
219
|
-
saleId: originalBatch.saleId,
|
|
220
|
-
saleSerialId: originalBatch.saleSerialId,
|
|
221
|
-
targetAllocatedQuantity: -1 * data.quantity,
|
|
222
|
-
orderId: order.id,
|
|
223
|
-
}));
|
|
224
|
-
const base = await repos.order.save({});
|
|
225
|
-
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
226
|
-
const change = await repos.allocateInventoryOrderChange.save(repos.allocateInventoryOrderChange.create({
|
|
227
|
-
itemId: item.id,
|
|
228
|
-
id: base.id,
|
|
229
|
-
quantityChanged: -1 * item.targetAllocatedQuantity * conversionFactor,
|
|
230
|
-
}));
|
|
231
|
-
// Stock movements track the hold through splitted batch
|
|
232
|
-
await this.stockService.validateStockAvailability({
|
|
233
|
-
materialId: item.materialId,
|
|
234
|
-
batchId: item.batchId,
|
|
235
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
236
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
237
|
-
quantity: change.quantityChanged,
|
|
238
|
-
}, manager);
|
|
239
|
-
await repos.stock.save(repos.stock.create([
|
|
240
|
-
// deduct from original batch available stock
|
|
241
|
-
{
|
|
242
|
-
orderId: base.id,
|
|
243
|
-
materialId: item.materialId,
|
|
244
|
-
batchId: item.batchId, // original batch
|
|
245
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
246
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
247
|
-
quantity: -1 * change.quantityChanged,
|
|
248
|
-
},
|
|
249
|
-
// add to splitted batch in released state
|
|
250
|
-
{
|
|
251
|
-
orderId: base.id,
|
|
252
|
-
materialId: item.materialId,
|
|
253
|
-
batchId: splittedBatch.id, // splitted batch marked deallocated with null saleId
|
|
254
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
255
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
256
|
-
quantity: change.quantityChanged,
|
|
257
|
-
},
|
|
258
|
-
]));
|
|
259
|
-
}));
|
|
260
|
-
}
|
|
261
|
-
return order;
|
|
262
|
-
}
|
|
263
|
-
findMany(options) {
|
|
264
|
-
const queryBuilder = this.dataSource
|
|
265
|
-
.getRepository(this.allocateInventoryOrderEntity)
|
|
266
|
-
.createQueryBuilder('to');
|
|
267
|
-
if (options.key)
|
|
268
|
-
queryBuilder.andWhere('to.key ILIKE :key', { key: `%${options.key}%` });
|
|
269
|
-
if (options.dateRange)
|
|
270
|
-
queryBuilder.andWhere('to.createdAt BETWEEN :start AND :end', {
|
|
271
|
-
start: options.dateRange.from,
|
|
272
|
-
end: options.dateRange.to,
|
|
273
|
-
});
|
|
274
|
-
if (options.materialId)
|
|
275
|
-
queryBuilder.innerJoin('to.items', 'm_items', 'm_items.materialId = :materialId', {
|
|
276
|
-
materialId: options.materialId,
|
|
277
|
-
});
|
|
278
|
-
if (options.batchId)
|
|
279
|
-
queryBuilder.innerJoin('to.items', 'b_items', 'b_items.batchId = :batchId', {
|
|
280
|
-
batchId: options.batchId,
|
|
281
|
-
});
|
|
282
|
-
if (options.locationId)
|
|
283
|
-
queryBuilder.innerJoin('to.items', 'l_items', 'l_items.locationId = :locationId', {
|
|
284
|
-
locationId: options.locationId,
|
|
285
|
-
});
|
|
286
|
-
if (options.loaderId)
|
|
287
|
-
queryBuilder.innerJoin('to.items', 'ld_items', 'ld_items.loaderId = :loaderId', {
|
|
288
|
-
loaderId: options.loaderId,
|
|
289
|
-
});
|
|
290
|
-
if (options.offset !== undefined && options.offset >= 0)
|
|
291
|
-
queryBuilder.skip(options.offset);
|
|
292
|
-
if (options.limit !== undefined && options.limit > 0)
|
|
293
|
-
queryBuilder.take(options.limit);
|
|
294
|
-
return queryBuilder.getManyAndCount();
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
AllocateInventoryOrderService = __decorate([
|
|
298
|
-
Injectable(),
|
|
299
|
-
__param(6, Inject(AllocateInventoryOrderEntity)),
|
|
300
|
-
__param(7, Inject(AllocateInventoryOrderItemEntity)),
|
|
301
|
-
__param(8, Inject(StockEntity)),
|
|
302
|
-
__metadata("design:paramtypes", [DataSource,
|
|
303
|
-
WmsEntities,
|
|
304
|
-
HelperService,
|
|
305
|
-
MaterialService,
|
|
306
|
-
BatchService,
|
|
307
|
-
StockService, Object, Object, Object])
|
|
308
|
-
], AllocateInventoryOrderService);
|
|
309
|
-
|
|
310
|
-
export { AllocateInventoryOrderService };
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base service for order operations that create/deduct stock entries.
|
|
3
|
-
* Provides common methods for adding and deducting inventory across different
|
|
4
|
-
* order types.
|
|
5
|
-
*
|
|
6
|
-
* Public API, with one precondition that is easy to miss.
|
|
7
|
-
*
|
|
8
|
-
* `add` and `deduct` write an `orders` row and `stocks` rows — and nothing else.
|
|
9
|
-
* They do **not** write a `*_order_changes` row, and both `StockChangeSubscriber`
|
|
10
|
-
* and `InventoryCountLockSubscriber` hook the insert of that row. So a host that
|
|
11
|
-
* extends this and registers `customChangeTables`, but leaves its own change row
|
|
12
|
-
* unwritten, gets neither the `stock.*` event nor the inventory count lock: its
|
|
13
|
-
* movements pass a running count in silence. Writing the change row is the
|
|
14
|
-
* host's job, exactly as it is for every service in this package.
|
|
15
|
-
*
|
|
16
|
-
* What extending it does buy is unit conversion and
|
|
17
|
-
* `StockService.validateStockAvailability` around the stock write. See
|
|
18
|
-
* {@link WmsCustomChangeTable} for the rest of what a custom family inherits.
|
|
19
|
-
*/
|
|
20
|
-
class BaseOrderService {
|
|
21
|
-
/**
|
|
22
|
-
* Creates a new order and saves a positive quantity stock entry
|
|
23
|
-
* @param repos Repository map containing order and stock repositories
|
|
24
|
-
* @param params Stock entry parameters (materialId, batchId, quantity, locationId?, loaderId?, unitId?)
|
|
25
|
-
* @returns Created order ID
|
|
26
|
-
*/
|
|
27
|
-
async add(repos, params) {
|
|
28
|
-
const { materialId, batchId, quantity, locationId, loaderId, unitId } = params;
|
|
29
|
-
// Create new order
|
|
30
|
-
const order = await repos.order.save({});
|
|
31
|
-
// Get conversion factor if unitId is provided
|
|
32
|
-
const conversionFactor = await this.materialService.getConversionFactor(materialId, unitId);
|
|
33
|
-
// Create positive stock entry linked to this order
|
|
34
|
-
await repos.stock.save(repos.stock.create({
|
|
35
|
-
orderId: order.id,
|
|
36
|
-
materialId,
|
|
37
|
-
batchId,
|
|
38
|
-
locationId: this.stockService.resolveLocationId(locationId),
|
|
39
|
-
loaderId: this.stockService.resolveLoaderId(loaderId),
|
|
40
|
-
quantity: quantity * conversionFactor,
|
|
41
|
-
}));
|
|
42
|
-
return order.id;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Creates a negative quantity stock entry linked to a source order
|
|
46
|
-
* Used to deduct stock from an existing status (FREE, ALLOCATED, IN_TRANSFER, etc.)
|
|
47
|
-
* @param repos Repository map containing order and stock repositories
|
|
48
|
-
* @param sourceOrderId The order ID to link the deduction to (typically an existing allocated/transferred order)
|
|
49
|
-
* @param params Stock entry parameters (materialId, batchId, quantity, locationId?, loaderId?, unitId?)
|
|
50
|
-
*/
|
|
51
|
-
async deduct(repos, params) {
|
|
52
|
-
const { materialId, batchId, quantity, locationId, loaderId, unitId } = params;
|
|
53
|
-
const order = await repos.order.save({});
|
|
54
|
-
// Get conversion factor if unitId is provided
|
|
55
|
-
const conversionFactor = await this.materialService.getConversionFactor(materialId, unitId);
|
|
56
|
-
// Create negative stock entry linked to source order
|
|
57
|
-
// This allows the status calculation to determine what status is being deducted from
|
|
58
|
-
await this.stockService.validateStockAvailability({
|
|
59
|
-
materialId,
|
|
60
|
-
batchId,
|
|
61
|
-
locationId: this.stockService.resolveLocationId(locationId),
|
|
62
|
-
loaderId: this.stockService.resolveLoaderId(loaderId),
|
|
63
|
-
quantity: quantity * conversionFactor,
|
|
64
|
-
}, repos.stock.manager);
|
|
65
|
-
await repos.stock.save(repos.stock.create({
|
|
66
|
-
orderId: order.id,
|
|
67
|
-
materialId,
|
|
68
|
-
batchId,
|
|
69
|
-
locationId: this.stockService.resolveLocationId(locationId),
|
|
70
|
-
loaderId: this.stockService.resolveLoaderId(loaderId),
|
|
71
|
-
quantity: -1 * quantity * conversionFactor,
|
|
72
|
-
}));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export { BaseOrderService };
|
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import { __decorate, __param, __metadata } from 'tslib';
|
|
2
|
-
import { Injectable, Inject } from '@nestjs/common';
|
|
3
|
-
import { DataSource, IsNull, Raw } from 'typeorm';
|
|
4
|
-
import { BatchEntity } from '../models/metadata/batch.entity.js';
|
|
5
|
-
import { BatchGroupEntity } from '../models/metadata/batch-group.entity.js';
|
|
6
|
-
import '../models/metadata/loader.entity.js';
|
|
7
|
-
import '../models/metadata/loader-type.entity.js';
|
|
8
|
-
import '../models/metadata/loader-unit.entity.js';
|
|
9
|
-
import '../models/metadata/location.entity.js';
|
|
10
|
-
import '../models/metadata/material.entity.js';
|
|
11
|
-
import '../models/metadata/material-unit.entity.js';
|
|
12
|
-
import '@rytass/wms-base-nestjs-module';
|
|
13
|
-
import '../models/metadata/sale-order.entity.js';
|
|
14
|
-
import '../models/metadata/stock.entity.js';
|
|
15
|
-
import '../models/metadata/unit.entity.js';
|
|
16
|
-
import '../models/wms-entities.js';
|
|
17
|
-
import '../models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js';
|
|
18
|
-
import '../models/extensions/receive-inventory-order/receive-inventory-order.entity.js';
|
|
19
|
-
import '../models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js';
|
|
20
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-item.entity.js';
|
|
21
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order.entity.js';
|
|
22
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.js';
|
|
23
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-change.entity.js';
|
|
24
|
-
import '../models/extensions/transfer-order/transfer-order.entity.js';
|
|
25
|
-
import '../models/extensions/transfer-order/transfer-order-item.entity.js';
|
|
26
|
-
import '../models/extensions/transfer-order/transfer-order-change.entity.js';
|
|
27
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order.entity.js';
|
|
28
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js';
|
|
29
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order-change.entity.js';
|
|
30
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js';
|
|
31
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js';
|
|
32
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.js';
|
|
33
|
-
import '../models/extensions/split-batch-order/split-batch-order.entity.js';
|
|
34
|
-
import '../models/extensions/split-batch-order/split-batch-order-item.entity.js';
|
|
35
|
-
import '../models/extensions/split-batch-order/split-batch-order-change.entity.js';
|
|
36
|
-
import '../models/extensions/merge-batch-order/merge-batch-order.entity.js';
|
|
37
|
-
import '../models/extensions/merge-batch-order/merge-batch-order-item.entity.js';
|
|
38
|
-
import '../models/extensions/merge-batch-order/merge-batch-order-change.entity.js';
|
|
39
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js';
|
|
40
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js';
|
|
41
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.js';
|
|
42
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order.entity.js';
|
|
43
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js';
|
|
44
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.js';
|
|
45
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order.entity.js';
|
|
46
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js';
|
|
47
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order-change.entity.js';
|
|
48
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order.entity.js';
|
|
49
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js';
|
|
50
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order-change.entity.js';
|
|
51
|
-
import '../models/extensions/scrape-order/scrape-order.entity.js';
|
|
52
|
-
import '../models/extensions/scrape-order/scrape-order-item.entity.js';
|
|
53
|
-
import '../models/extensions/scrape-order/scrape-order-change.entity.js';
|
|
54
|
-
import '../models/extensions/inventory-count-order/inventory-count-order.entity.js';
|
|
55
|
-
import '../models/extensions/inventory-count-order/inventory-count-order-item.entity.js';
|
|
56
|
-
import '../models/extensions/inventory-count-order/inventory-count-order-change.entity.js';
|
|
57
|
-
import '../models/extensions/consumption-order/consumption-order.entity.js';
|
|
58
|
-
import '../models/extensions/consumption-order/consumption-order-item.entity.js';
|
|
59
|
-
import '../models/extensions/consumption-order/consumption-order-change.entity.js';
|
|
60
|
-
import '../models/extensions/order-reference/order-reference.entity.js';
|
|
61
|
-
|
|
62
|
-
let BatchService = class BatchService {
|
|
63
|
-
dataSource;
|
|
64
|
-
batchEntity;
|
|
65
|
-
batchGroupEntity;
|
|
66
|
-
batchRepository;
|
|
67
|
-
batchGroupRepository;
|
|
68
|
-
constructor(dataSource, batchEntity, batchGroupEntity) {
|
|
69
|
-
this.dataSource = dataSource;
|
|
70
|
-
this.batchEntity = batchEntity;
|
|
71
|
-
this.batchGroupEntity = batchGroupEntity;
|
|
72
|
-
this.batchRepository = this.dataSource.getRepository(this.batchEntity);
|
|
73
|
-
this.batchGroupRepository =
|
|
74
|
-
this.dataSource.getRepository(this.batchGroupEntity);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Validates batch state transition based on hierarchy:
|
|
78
|
-
* inShipment = held = transferId = vendorId = customerId > saleId+saleSerialId > inspectionResult
|
|
79
|
-
*
|
|
80
|
-
* - Top tier (terminal): inShipment, held, transferId, vendorId, customerId → no state change allowed
|
|
81
|
-
* - Middle tier: saleId+saleSerialId → can only go UP to top tier
|
|
82
|
-
* - Bottom tier: inspectionResult → can go UP to middle or top tier, but cannot change inspectionResult
|
|
83
|
-
*/
|
|
84
|
-
validateStateTransition(batch, options) {
|
|
85
|
-
// Check if trying to clear/release a top-tier state
|
|
86
|
-
const isReleasingInShipment = batch.inShipment === true && options.inShipment === false;
|
|
87
|
-
const isReleasingHeld = batch.held === true && options.held === false;
|
|
88
|
-
const isReleasingTransfer = batch.transferId != null && options.transferId === null;
|
|
89
|
-
const isReleasingVendor = batch.vendorId != null && options.vendorId === null;
|
|
90
|
-
const isReleasingCustomer = batch.customerId != null && options.customerId === null;
|
|
91
|
-
const isReleasing = isReleasingInShipment ||
|
|
92
|
-
isReleasingHeld ||
|
|
93
|
-
isReleasingTransfer ||
|
|
94
|
-
isReleasingVendor ||
|
|
95
|
-
isReleasingCustomer;
|
|
96
|
-
// Determine current state tier
|
|
97
|
-
const isTopTier = batch.inShipment === true ||
|
|
98
|
-
batch.held === true ||
|
|
99
|
-
batch.transferId != null ||
|
|
100
|
-
batch.vendorId != null ||
|
|
101
|
-
batch.customerId != null;
|
|
102
|
-
const isMiddleTier = batch.saleId != null || batch.saleSerialId != null;
|
|
103
|
-
// What is being changed (explicitly set, not undefined)
|
|
104
|
-
const changingMiddleTier = options.saleId !== undefined || options.saleSerialId !== undefined;
|
|
105
|
-
const changingInspection = options.inspectionResult !== undefined;
|
|
106
|
-
// Rule 1: Top tier is terminal unless explicitly releasing that state
|
|
107
|
-
if (isTopTier && !isReleasing) {
|
|
108
|
-
const state = batch.inShipment
|
|
109
|
-
? 'in shipment'
|
|
110
|
-
: batch.held
|
|
111
|
-
? 'held'
|
|
112
|
-
: batch.transferId
|
|
113
|
-
? 'in transfer'
|
|
114
|
-
: batch.vendorId
|
|
115
|
-
? 'vendor inventory'
|
|
116
|
-
: 'customer inventory';
|
|
117
|
-
throw new Error(`Batch is ${state}, cannot transition to other states. Only release is allowed.`);
|
|
118
|
-
}
|
|
119
|
-
// Rule 2: Middle tier cannot change to same tier
|
|
120
|
-
if (isMiddleTier && changingMiddleTier) {
|
|
121
|
-
throw new Error('Cannot modify allocation (saleId/saleSerialId) for already allocated batch');
|
|
122
|
-
}
|
|
123
|
-
// Rule 3: Cannot change inspection result after it's set (unless releasing to higher tier)
|
|
124
|
-
if (batch.inspectionResult != null &&
|
|
125
|
-
!isMiddleTier &&
|
|
126
|
-
!isTopTier &&
|
|
127
|
-
changingInspection) {
|
|
128
|
-
throw new Error('Inspection result already set, cannot modify');
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Find the unsplit batch carrying `key` for this material, or create it.
|
|
133
|
-
*
|
|
134
|
-
* `parentBatchId` and `attributes` are write-on-create only: the lookup still
|
|
135
|
-
* matches on key, material and the unsplit condition alone, so a caller that
|
|
136
|
-
* passes them for a key that already exists gets the existing batch back
|
|
137
|
-
* untouched. `key` is the batch's identity in this model — `batches` carries
|
|
138
|
-
* `UNIQUE (key, materialId)`, so there is exactly one row per key — and
|
|
139
|
-
* attributes describe a batch rather than distinguish one. A host that needs
|
|
140
|
-
* two distinct batches gives them two keys; asking for two here fails on the
|
|
141
|
-
* constraint.
|
|
142
|
-
*
|
|
143
|
-
* Deliberately **not** filtered on `requiresInspection`: a receipt awaiting
|
|
144
|
-
* inspection may flag the batch in place, and the flag must not then hide the
|
|
145
|
-
* key from the next lookup — which, given the unique constraint, would leave
|
|
146
|
-
* it trying to create a duplicate.
|
|
147
|
-
*/
|
|
148
|
-
async getBatch({ manager = this.dataSource.manager, ...options }) {
|
|
149
|
-
const batchRepository = manager.getRepository(this.batchEntity);
|
|
150
|
-
const batchGroupRepository = manager.getRepository(this.batchGroupEntity);
|
|
151
|
-
const unsplittedCondition = {
|
|
152
|
-
saleId: IsNull(),
|
|
153
|
-
saleSerialId: IsNull(),
|
|
154
|
-
customerId: IsNull(),
|
|
155
|
-
vendorId: IsNull(),
|
|
156
|
-
transferId: IsNull(),
|
|
157
|
-
inspectionResult: IsNull(),
|
|
158
|
-
held: false,
|
|
159
|
-
inShipment: false,
|
|
160
|
-
};
|
|
161
|
-
const existed = await batchRepository.findOneBy({
|
|
162
|
-
...unsplittedCondition,
|
|
163
|
-
key: options.key,
|
|
164
|
-
materialId: options.materialId,
|
|
165
|
-
});
|
|
166
|
-
if (existed)
|
|
167
|
-
return existed;
|
|
168
|
-
const batchGroup = await batchGroupRepository.save(batchGroupRepository.create({
|
|
169
|
-
key: options.key,
|
|
170
|
-
materialId: options.materialId,
|
|
171
|
-
}));
|
|
172
|
-
const batch = await batchRepository.save(batchRepository.create({
|
|
173
|
-
...options,
|
|
174
|
-
groupId: batchGroup.id,
|
|
175
|
-
}));
|
|
176
|
-
return batch;
|
|
177
|
-
}
|
|
178
|
-
async splitBatch({ manager = this.dataSource.manager, ...options }) {
|
|
179
|
-
const batchRepository = manager.getRepository(this.batchEntity);
|
|
180
|
-
const batch = await batchRepository.findOneOrFail({
|
|
181
|
-
where: { id: options.id },
|
|
182
|
-
relations: { batchGroup: true },
|
|
183
|
-
});
|
|
184
|
-
// Validate state transition before proceeding
|
|
185
|
-
this.validateStateTransition(batch, options);
|
|
186
|
-
// Create merged batch object with new metrics
|
|
187
|
-
const mergedBatch = {
|
|
188
|
-
...batch,
|
|
189
|
-
...options,
|
|
190
|
-
groupId: batch.batchGroup.id,
|
|
191
|
-
};
|
|
192
|
-
/*
|
|
193
|
-
* Absent attributes have to be matched with `IsNull()`, not with a bare
|
|
194
|
-
* `null`. A `null` on the right of a find condition compiles to `= NULL`,
|
|
195
|
-
* which is never true in SQL — so this lookup used to miss every batch whose
|
|
196
|
-
* attributes were unset and split off a duplicate instead of reusing the one
|
|
197
|
-
* already there. `getBatch` above has always done it this way; this call
|
|
198
|
-
* site did not. TypeORM 1.x refuses a bare `null` here outright, which is
|
|
199
|
-
* how the bug surfaced.
|
|
200
|
-
*/
|
|
201
|
-
const orNull = (value) => value === null || value === undefined ? IsNull() : value;
|
|
202
|
-
const whereCondition = {
|
|
203
|
-
groupId: mergedBatch.groupId,
|
|
204
|
-
saleId: orNull(mergedBatch.saleId),
|
|
205
|
-
saleSerialId: orNull(mergedBatch.saleSerialId),
|
|
206
|
-
customerId: orNull(mergedBatch.customerId),
|
|
207
|
-
vendorId: orNull(mergedBatch.vendorId),
|
|
208
|
-
inspectionResult: orNull(mergedBatch.inspectionResult),
|
|
209
|
-
transferId: orNull(mergedBatch.transferId),
|
|
210
|
-
held: mergedBatch.held ?? false,
|
|
211
|
-
inShipment: mergedBatch.inShipment ?? false,
|
|
212
|
-
// Part of the batch's identity: material waiting on an inspection must
|
|
213
|
-
// not be merged back into the free stock it was split away from.
|
|
214
|
-
requiresInspection: mergedBatch.requiresInspection ?? false,
|
|
215
|
-
/*
|
|
216
|
-
* Attributes take part in the identity check, so a split that asks for
|
|
217
|
-
* `{ bin: 'B1' }` never lands on the sibling holding `{ bin: 'B2' }`.
|
|
218
|
-
* Compared as `jsonb`, whose equality is by content rather than by the
|
|
219
|
-
* text of the document, so key order and whitespace do not matter — which
|
|
220
|
-
* a plain string comparison on the serialized column would get wrong.
|
|
221
|
-
*/
|
|
222
|
-
attributes: mergedBatch.attributes === null || mergedBatch.attributes === undefined
|
|
223
|
-
? IsNull()
|
|
224
|
-
: Raw((alias) => `${alias} = cast(:batchAttributes as jsonb)`, {
|
|
225
|
-
batchAttributes: JSON.stringify(mergedBatch.attributes),
|
|
226
|
-
}),
|
|
227
|
-
};
|
|
228
|
-
// Check if a batch with these exact metrics already exists in the same group
|
|
229
|
-
const existingBatch = await batchRepository.findOne({
|
|
230
|
-
where: whereCondition,
|
|
231
|
-
});
|
|
232
|
-
if (existingBatch) {
|
|
233
|
-
return existingBatch;
|
|
234
|
-
}
|
|
235
|
-
// If no matching batch exists, create a new one
|
|
236
|
-
return batchRepository.save(batchRepository.create({
|
|
237
|
-
...mergedBatch,
|
|
238
|
-
key: `${batch.batchGroup.key}_${Date.now().toString()}`,
|
|
239
|
-
// Not inherited from `mergedBatch`: spreading the source batch would
|
|
240
|
-
// copy *its* parent and flatten the chain by one link. The parent of
|
|
241
|
-
// the batch being created is the batch it was split out of.
|
|
242
|
-
parentBatchId: batch.id,
|
|
243
|
-
id: undefined,
|
|
244
|
-
}));
|
|
245
|
-
}
|
|
246
|
-
async findMany(options) {
|
|
247
|
-
const queryBuilder = this.batchGroupRepository.createQueryBuilder('batch');
|
|
248
|
-
if (options.wildcard) {
|
|
249
|
-
const { key, value } = options.wildcard;
|
|
250
|
-
queryBuilder.andWhere(`batch."${String(key)}" ILIKE :value`, {
|
|
251
|
-
value: `%${value}%`,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
if (options.materialId)
|
|
255
|
-
queryBuilder.andWhere('batch."materialId" = :materialId', {
|
|
256
|
-
materialId: options.materialId,
|
|
257
|
-
});
|
|
258
|
-
if (options.offset !== undefined && options.offset >= 0)
|
|
259
|
-
queryBuilder.skip(options.offset);
|
|
260
|
-
if (options.limit !== undefined && options.limit > 0)
|
|
261
|
-
queryBuilder.take(options.limit);
|
|
262
|
-
return queryBuilder.getManyAndCount();
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
BatchService = __decorate([
|
|
266
|
-
Injectable(),
|
|
267
|
-
__param(1, Inject(BatchEntity)),
|
|
268
|
-
__param(2, Inject(BatchGroupEntity)),
|
|
269
|
-
__metadata("design:paramtypes", [DataSource, Object, Object])
|
|
270
|
-
], BatchService);
|
|
271
|
-
|
|
272
|
-
export { BatchService };
|