@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,504 +0,0 @@
|
|
|
1
|
-
import { __decorate, __param, __metadata } from 'tslib';
|
|
2
|
-
import { Injectable, Inject } from '@nestjs/common';
|
|
3
|
-
import { DataSource, IsNull, In } from 'typeorm';
|
|
4
|
-
import '../models/metadata/batch.entity.js';
|
|
5
|
-
import '../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 { StockEntity } from '../models/metadata/stock.entity.js';
|
|
15
|
-
import '../models/metadata/unit.entity.js';
|
|
16
|
-
import { WmsEntities } from '../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 { ConsumptionOrderEntity } from '../models/extensions/consumption-order/consumption-order.entity.js';
|
|
58
|
-
import { ConsumptionOrderItemEntity } from '../models/extensions/consumption-order/consumption-order-item.entity.js';
|
|
59
|
-
import { ConsumptionOrderChangeEntityClass } from '../models/extensions/consumption-order/consumption-order-change.entity.js';
|
|
60
|
-
import '../models/extensions/order-reference/order-reference.entity.js';
|
|
61
|
-
import { ConsumptionChangeKind } from '../constants/consumption-change-kind.js';
|
|
62
|
-
import { HelperService } from './helper.service.js';
|
|
63
|
-
import { MaterialService } from './material.service.js';
|
|
64
|
-
import { StockService } from './stock.service.js';
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Material handed to a process that consumes it: issue it out, take back what
|
|
68
|
-
* came home, settle what was used.
|
|
69
|
-
*
|
|
70
|
-
* The three actions share one order because they are one story, and because
|
|
71
|
-
* that is the grammar the rest of this package already speaks — hold/release,
|
|
72
|
-
* allocate/deallocate and transfer/cancel are each a single order too.
|
|
73
|
-
*
|
|
74
|
-
* No new stock status. An issue is a location move and nothing else: status
|
|
75
|
-
* here is a flag on the *batch*, so a "being consumed" status would force every
|
|
76
|
-
* partial issue to split a child batch off it, and one batch key per issue is a
|
|
77
|
-
* worse answer than one location. What the process actually is — a work order,
|
|
78
|
-
* a repair job, a subcontract — belongs in `order_references`, which can say
|
|
79
|
-
* *which* one; a status could only ever say "some".
|
|
80
|
-
*/
|
|
81
|
-
let ConsumptionOrderService = class ConsumptionOrderService {
|
|
82
|
-
dataSource;
|
|
83
|
-
entities;
|
|
84
|
-
helperService;
|
|
85
|
-
materialService;
|
|
86
|
-
stockService;
|
|
87
|
-
consumptionOrderEntity;
|
|
88
|
-
consumptionOrderItemEntity;
|
|
89
|
-
stockEntity;
|
|
90
|
-
seqPrefix = 'CSO';
|
|
91
|
-
consumptionOrderRepository;
|
|
92
|
-
constructor(dataSource, entities, helperService, materialService, stockService, consumptionOrderEntity, consumptionOrderItemEntity, stockEntity) {
|
|
93
|
-
this.dataSource = dataSource;
|
|
94
|
-
this.entities = entities;
|
|
95
|
-
this.helperService = helperService;
|
|
96
|
-
this.materialService = materialService;
|
|
97
|
-
this.stockService = stockService;
|
|
98
|
-
this.consumptionOrderEntity = consumptionOrderEntity;
|
|
99
|
-
this.consumptionOrderItemEntity = consumptionOrderItemEntity;
|
|
100
|
-
this.stockEntity = stockEntity;
|
|
101
|
-
this.consumptionOrderRepository = this.dataSource.getRepository(this.consumptionOrderEntity);
|
|
102
|
-
}
|
|
103
|
-
getRepositories(manager) {
|
|
104
|
-
return {
|
|
105
|
-
consumptionOrder: manager.getRepository(this.consumptionOrderEntity),
|
|
106
|
-
consumptionOrderItem: manager.getRepository(this.consumptionOrderItemEntity),
|
|
107
|
-
consumptionOrderChange: manager.getRepository(ConsumptionOrderChangeEntityClass),
|
|
108
|
-
order: manager.getRepository(this.entities.order),
|
|
109
|
-
stock: manager.getRepository(this.stockEntity),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
itemRow(input, orderId) {
|
|
113
|
-
const locationId = this.stockService.resolveLocationId(input.locationId);
|
|
114
|
-
return {
|
|
115
|
-
orderId,
|
|
116
|
-
materialId: input.materialId,
|
|
117
|
-
batchId: input.batchId,
|
|
118
|
-
locationId,
|
|
119
|
-
loaderId: this.stockService.resolveLoaderId(input.loaderId),
|
|
120
|
-
unitId: input.unitId ?? null,
|
|
121
|
-
targetConsumedQuantity: input.quantity,
|
|
122
|
-
// Defaulting the destination to the source keeps a host that does not
|
|
123
|
-
// separate a consuming location from the shelf working: the stock rows
|
|
124
|
-
// net out and only the change rows record what happened.
|
|
125
|
-
targetLocationId: this.stockService.resolveLocationId(input.targetLocationId ?? locationId),
|
|
126
|
-
targetLoaderId: this.stockService.resolveLoaderId(input.targetLoaderId ?? input.loaderId),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
async createConsumptionOrder(options) {
|
|
130
|
-
if (!options.items.length) {
|
|
131
|
-
throw new Error('A consumption order needs at least one item');
|
|
132
|
-
}
|
|
133
|
-
return this.dataSource.transaction(async (manager) => {
|
|
134
|
-
const repos = this.getRepositories(manager);
|
|
135
|
-
const key = await this.helperService.getSeq(this.seqPrefix, manager);
|
|
136
|
-
const order = await repos.consumptionOrder.save(repos.consumptionOrder.create({
|
|
137
|
-
key,
|
|
138
|
-
description: options.description ?? null,
|
|
139
|
-
createdBy: options.createdBy ?? null,
|
|
140
|
-
}));
|
|
141
|
-
await repos.consumptionOrderItem.save(options.items.map((item) => repos.consumptionOrderItem.create(this.itemRow(item, order.id))));
|
|
142
|
-
return order;
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Replace the order's lines. Refused once anything has moved: the changes
|
|
147
|
-
* point at items, so swapping the items out from under them would leave the
|
|
148
|
-
* ledger describing lines that no longer exist.
|
|
149
|
-
*/
|
|
150
|
-
async updateConsumptionOrder(options) {
|
|
151
|
-
return this.dataSource.transaction(async (manager) => {
|
|
152
|
-
const repos = this.getRepositories(manager);
|
|
153
|
-
const order = await repos.consumptionOrder.findOneOrFail({
|
|
154
|
-
where: { id: options.id },
|
|
155
|
-
});
|
|
156
|
-
if (order.removedAt) {
|
|
157
|
-
// The "has anything moved" check below only looks at live items, and a
|
|
158
|
-
// removed order has none — so without this it would happily attach a
|
|
159
|
-
// fresh set of lines to an order that is supposed to be gone.
|
|
160
|
-
throw new Error(`Consumption order ${order.key} has been removed and cannot be updated`);
|
|
161
|
-
}
|
|
162
|
-
if (options.description !== undefined) {
|
|
163
|
-
await repos.consumptionOrder.update(order.id, {
|
|
164
|
-
description: options.description,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
if (options.items) {
|
|
168
|
-
const existing = await repos.consumptionOrderItem.find({
|
|
169
|
-
where: { orderId: order.id, removedAt: IsNull() },
|
|
170
|
-
});
|
|
171
|
-
const moved = existing.length
|
|
172
|
-
? await repos.consumptionOrderChange.count({
|
|
173
|
-
where: { itemId: In(existing.map(({ id }) => id)) },
|
|
174
|
-
})
|
|
175
|
-
: 0;
|
|
176
|
-
if (moved > 0) {
|
|
177
|
-
throw new Error(`Consumption order ${order.key} has already moved stock; its items can no longer be replaced`);
|
|
178
|
-
}
|
|
179
|
-
await repos.consumptionOrderItem.remove(existing);
|
|
180
|
-
await repos.consumptionOrderItem.save(options.items.map((item) => repos.consumptionOrderItem.create(this.itemRow(item, order.id))));
|
|
181
|
-
}
|
|
182
|
-
return repos.consumptionOrder.findOneOrFail({
|
|
183
|
-
where: { id: order.id },
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
async removeConsumptionOrder(id) {
|
|
188
|
-
return this.dataSource.transaction(async (manager) => {
|
|
189
|
-
const repos = this.getRepositories(manager);
|
|
190
|
-
const items = await repos.consumptionOrderItem.find({
|
|
191
|
-
where: { orderId: id, removedAt: IsNull() },
|
|
192
|
-
});
|
|
193
|
-
const moved = items.length
|
|
194
|
-
? await repos.consumptionOrderChange.count({
|
|
195
|
-
where: { itemId: In(items.map(({ id: itemId }) => itemId)) },
|
|
196
|
-
})
|
|
197
|
-
: 0;
|
|
198
|
-
if (moved > 0) {
|
|
199
|
-
throw new Error(`Consumption order ${id} has already moved stock and cannot be removed`);
|
|
200
|
-
}
|
|
201
|
-
const removedAt = new Date();
|
|
202
|
-
await repos.consumptionOrderItem.update({ orderId: id }, {
|
|
203
|
-
removedAt,
|
|
204
|
-
});
|
|
205
|
-
await repos.consumptionOrder.update(id, { removedAt });
|
|
206
|
-
return true;
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* How much of each line has moved, **in base units**.
|
|
211
|
-
*
|
|
212
|
-
* The changes are the only record of that — nothing is denormalised onto the
|
|
213
|
-
* item — so `issue`, `return` and `settle` all read it back before they write.
|
|
214
|
-
*
|
|
215
|
-
* Base units on both sides is the point. `quantityChanged` is written in base
|
|
216
|
-
* units (the stock ledger's own unit), while `targetConsumedQuantity` is
|
|
217
|
-
* stored in whatever unit the line was written in. Reporting the two side by
|
|
218
|
-
* side without converting made every comparison in `act` wrong by the
|
|
219
|
-
* conversion factor. `ReceiveInventoryOrderService` resolves the same tension
|
|
220
|
-
* the same way.
|
|
221
|
-
*/
|
|
222
|
-
async getProgress(orderId, manager = this.dataSource.manager) {
|
|
223
|
-
const repos = this.getRepositories(manager);
|
|
224
|
-
const items = await repos.consumptionOrderItem.find({
|
|
225
|
-
where: { orderId, removedAt: IsNull() },
|
|
226
|
-
});
|
|
227
|
-
if (!items.length)
|
|
228
|
-
return [];
|
|
229
|
-
const changes = await repos.consumptionOrderChange.find({
|
|
230
|
-
where: { itemId: In(items.map(({ id }) => id)) },
|
|
231
|
-
});
|
|
232
|
-
return Promise.all(items.map(async (item) => {
|
|
233
|
-
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
234
|
-
const own = changes.filter((change) => change.itemId === item.id);
|
|
235
|
-
const sum = (kind) => own
|
|
236
|
-
.filter((change) => change.kind === kind)
|
|
237
|
-
.reduce((total, change) => total + Math.abs(Number(change.quantityChanged)), 0);
|
|
238
|
-
const issued = sum(ConsumptionChangeKind.ISSUE);
|
|
239
|
-
const returned = sum(ConsumptionChangeKind.RETURN);
|
|
240
|
-
const settled = sum(ConsumptionChangeKind.SETTLE);
|
|
241
|
-
return {
|
|
242
|
-
itemId: item.id,
|
|
243
|
-
target: Number(item.targetConsumedQuantity) * conversionFactor,
|
|
244
|
-
issued,
|
|
245
|
-
returned,
|
|
246
|
-
settled,
|
|
247
|
-
outstanding: issued - returned - settled,
|
|
248
|
-
};
|
|
249
|
-
}));
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Hand material to the process: it leaves the source location and appears at
|
|
253
|
-
* the consuming one. Nothing about the batch changes.
|
|
254
|
-
*/
|
|
255
|
-
issue(options) {
|
|
256
|
-
return this.act(options, ConsumptionChangeKind.ISSUE);
|
|
257
|
-
}
|
|
258
|
-
/** Take back material the process did not use. */
|
|
259
|
-
return(options) {
|
|
260
|
-
return this.act(options, ConsumptionChangeKind.RETURN);
|
|
261
|
-
}
|
|
262
|
-
/** Close out material the process used up: it leaves inventory. */
|
|
263
|
-
settle(options) {
|
|
264
|
-
return this.act(options, ConsumptionChangeKind.SETTLE);
|
|
265
|
-
}
|
|
266
|
-
async act(options, kind) {
|
|
267
|
-
return this.dataSource.transaction(async (manager) => {
|
|
268
|
-
const repos = this.getRepositories(manager);
|
|
269
|
-
/*
|
|
270
|
-
* Serialise concurrent actions on one order before reading anything.
|
|
271
|
-
*
|
|
272
|
-
* The whole guard rests on a progress snapshot, and two transactions that
|
|
273
|
-
* both read it before either writes both believe the line is untouched --
|
|
274
|
-
* so a ten-unit line goes out twice. `validateStockAvailability` does not
|
|
275
|
-
* save us: it checks that the shelf has the stock, not that the order
|
|
276
|
-
* asked for it, so the over-draw only surfaces when the shelf happens to
|
|
277
|
-
* be short.
|
|
278
|
-
*/
|
|
279
|
-
const order = await repos.consumptionOrder.findOne({
|
|
280
|
-
where: { id: options.orderId },
|
|
281
|
-
lock: { mode: 'pessimistic_write' },
|
|
282
|
-
});
|
|
283
|
-
if (!order) {
|
|
284
|
-
throw new Error(`Consumption order ${options.orderId} does not exist`);
|
|
285
|
-
}
|
|
286
|
-
if (order.removedAt) {
|
|
287
|
-
// Nothing else reads the order's own `removedAt`, so without this a
|
|
288
|
-
// removed order still moves stock while staying out of every listing.
|
|
289
|
-
throw new Error(`Consumption order ${order.key} has been removed and cannot move stock`);
|
|
290
|
-
}
|
|
291
|
-
const items = await repos.consumptionOrderItem.find({
|
|
292
|
-
where: { orderId: options.orderId, removedAt: IsNull() },
|
|
293
|
-
});
|
|
294
|
-
if (!items.length) {
|
|
295
|
-
throw new Error(`Consumption order ${options.orderId} has no items`);
|
|
296
|
-
}
|
|
297
|
-
const progress = await this.getProgress(options.orderId, manager);
|
|
298
|
-
const progressById = new Map(progress.map((entry) => [entry.itemId, entry]));
|
|
299
|
-
if (options.items?.length === 0) {
|
|
300
|
-
// Omitting `items` already means "every line". An empty array is
|
|
301
|
-
// therefore a caller that meant to name lines and computed none —
|
|
302
|
-
// succeeding silently would hide that.
|
|
303
|
-
throw new Error(`Consumption order ${order.key} was asked to ${kind.toLowerCase()} an empty line list; omit "items" to act on every line`);
|
|
304
|
-
}
|
|
305
|
-
const lines = options.items ??
|
|
306
|
-
items.map((item) => ({ itemId: item.id, quantity: undefined }));
|
|
307
|
-
/*
|
|
308
|
-
* What this call has already moved, per line. The snapshot above is read
|
|
309
|
-
* once, so without this two lines naming the same item would each see
|
|
310
|
-
* "nothing moved yet" and both go through.
|
|
311
|
-
*/
|
|
312
|
-
const movedInThisCall = new Map();
|
|
313
|
-
for (const line of lines) {
|
|
314
|
-
const item = items.find(({ id }) => id === line.itemId);
|
|
315
|
-
if (!item) {
|
|
316
|
-
throw new Error(`Item ${line.itemId} does not belong to consumption order ${options.orderId}`);
|
|
317
|
-
}
|
|
318
|
-
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
319
|
-
const state = progressById.get(item.id);
|
|
320
|
-
const alreadyMoved = movedInThisCall.get(item.id) ?? 0;
|
|
321
|
-
// Everything here is in base units: `getProgress` reports base units and
|
|
322
|
-
// converts the target, and a caller's `quantity` -- which is in the
|
|
323
|
-
// line's own unit -- is converted on the way in. Mixing the two is what
|
|
324
|
-
// made a whole-order settle ask for twelve times what was there.
|
|
325
|
-
//
|
|
326
|
-
// `??` rather than `||`: a line with nothing left must stay 0 and be
|
|
327
|
-
// rejected below, not fall through to the whole target.
|
|
328
|
-
const remaining = (kind === ConsumptionChangeKind.ISSUE
|
|
329
|
-
? (state?.target ?? 0) - (state?.issued ?? 0)
|
|
330
|
-
: (state?.outstanding ?? 0)) - alreadyMoved;
|
|
331
|
-
const amount = line.quantity === undefined
|
|
332
|
-
? remaining
|
|
333
|
-
: line.quantity * conversionFactor;
|
|
334
|
-
if (amount <= 0) {
|
|
335
|
-
// Acting on the whole order is the common call, and an order where
|
|
336
|
-
// some lines are already done is the common state; skipping them
|
|
337
|
-
// beats making the caller compute the list.
|
|
338
|
-
if (line.quantity === undefined)
|
|
339
|
-
continue;
|
|
340
|
-
throw new Error(`Consumption order line ${item.id} cannot move ${amount} (base units)`);
|
|
341
|
-
}
|
|
342
|
-
if (amount > remaining) {
|
|
343
|
-
throw new Error(kind === ConsumptionChangeKind.ISSUE
|
|
344
|
-
? `Consumption order line ${item.id} has only ${remaining} left to issue, asked for ${amount} (base units)`
|
|
345
|
-
: `Consumption order line ${item.id} has only ${remaining} outstanding, asked to ${kind.toLowerCase()} ${amount} (base units)`);
|
|
346
|
-
}
|
|
347
|
-
await this.writeMovement(repos, manager, item, kind, amount);
|
|
348
|
-
movedInThisCall.set(item.id, alreadyMoved + amount);
|
|
349
|
-
}
|
|
350
|
-
return true;
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
/** `amount` is in base units; `act` has already converted. */
|
|
354
|
-
async writeMovement(repos, manager, item, kind, amount) {
|
|
355
|
-
const source = {
|
|
356
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
357
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
358
|
-
};
|
|
359
|
-
const target = {
|
|
360
|
-
locationId: this.stockService.resolveLocationId(item.targetLocationId),
|
|
361
|
-
loaderId: this.stockService.resolveLoaderId(item.targetLoaderId),
|
|
362
|
-
};
|
|
363
|
-
const movement = kind === ConsumptionChangeKind.ISSUE
|
|
364
|
-
? { from: source, to: target }
|
|
365
|
-
: kind === ConsumptionChangeKind.RETURN
|
|
366
|
-
? { from: target, to: source }
|
|
367
|
-
: { from: target, to: null };
|
|
368
|
-
await this.stockService.validateStockAvailability({
|
|
369
|
-
materialId: item.materialId,
|
|
370
|
-
batchId: item.batchId,
|
|
371
|
-
locationId: movement.from.locationId,
|
|
372
|
-
loaderId: movement.from.loaderId,
|
|
373
|
-
quantity: amount,
|
|
374
|
-
}, manager);
|
|
375
|
-
const base = await repos.order.save({});
|
|
376
|
-
await repos.consumptionOrderChange.save(repos.consumptionOrderChange.create({
|
|
377
|
-
id: base.id,
|
|
378
|
-
itemId: item.id,
|
|
379
|
-
// The sign follows the stock the order controls: an issue puts material
|
|
380
|
-
// into the process's hands, a return and a settlement take it back out.
|
|
381
|
-
quantityChanged: kind === ConsumptionChangeKind.ISSUE ? amount : -1 * amount,
|
|
382
|
-
kind,
|
|
383
|
-
}));
|
|
384
|
-
const rows = [
|
|
385
|
-
{
|
|
386
|
-
orderId: base.id,
|
|
387
|
-
materialId: item.materialId,
|
|
388
|
-
batchId: item.batchId,
|
|
389
|
-
locationId: movement.from.locationId,
|
|
390
|
-
loaderId: movement.from.loaderId,
|
|
391
|
-
quantity: -1 * amount,
|
|
392
|
-
},
|
|
393
|
-
];
|
|
394
|
-
if (movement.to) {
|
|
395
|
-
rows.push({
|
|
396
|
-
orderId: base.id,
|
|
397
|
-
materialId: item.materialId,
|
|
398
|
-
batchId: item.batchId,
|
|
399
|
-
locationId: movement.to.locationId,
|
|
400
|
-
loaderId: movement.to.loaderId,
|
|
401
|
-
quantity: amount,
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
await repos.stock.save(repos.stock.create(rows));
|
|
405
|
-
}
|
|
406
|
-
async findMany(options) {
|
|
407
|
-
const queryBuilder = this.consumptionOrderRepository
|
|
408
|
-
.createQueryBuilder('consumption')
|
|
409
|
-
.leftJoinAndSelect('consumption.items', 'item');
|
|
410
|
-
if (!options.includeRemoved) {
|
|
411
|
-
queryBuilder.andWhere('consumption."removedAt" IS NULL');
|
|
412
|
-
}
|
|
413
|
-
if (options.ids?.length) {
|
|
414
|
-
queryBuilder.andWhere('consumption.id IN (:...ids)', {
|
|
415
|
-
ids: options.ids,
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
if (options.key) {
|
|
419
|
-
queryBuilder.andWhere('consumption.key ILIKE :key', {
|
|
420
|
-
key: `%${options.key}%`,
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
if (options.description) {
|
|
424
|
-
queryBuilder.andWhere('consumption.description ILIKE :description', {
|
|
425
|
-
description: `%${options.description}%`,
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
if (options.createdBy) {
|
|
429
|
-
queryBuilder.andWhere('consumption."createdBy" = :createdBy', {
|
|
430
|
-
createdBy: options.createdBy,
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
if (options.materialId) {
|
|
434
|
-
queryBuilder.andWhere('item."materialId" = :materialId', {
|
|
435
|
-
materialId: options.materialId,
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
if (options.batchId) {
|
|
439
|
-
queryBuilder.andWhere('item."batchId" = :batchId', {
|
|
440
|
-
batchId: options.batchId,
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
if (options.locationId) {
|
|
444
|
-
queryBuilder.andWhere('(item."locationId" = :locationId OR item."targetLocationId" = :locationId)', { locationId: options.locationId });
|
|
445
|
-
}
|
|
446
|
-
if (options.createdAtFrom) {
|
|
447
|
-
queryBuilder.andWhere('consumption."createdAt" >= :createdAtFrom', {
|
|
448
|
-
createdAtFrom: options.createdAtFrom,
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
if (options.createdAtTo) {
|
|
452
|
-
queryBuilder.andWhere('consumption."createdAt" <= :createdAtTo', {
|
|
453
|
-
createdAtTo: options.createdAtTo,
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
/*
|
|
457
|
-
* The property path, not a quoted column. `getManyAndCount` with a joined
|
|
458
|
-
* relation and a limit wraps the query in a `SELECT DISTINCT` over the root
|
|
459
|
-
* ids, and it can only carry an ordering across that wrapper if it
|
|
460
|
-
* recognises the expression as a column of the aliased entity. Given raw
|
|
461
|
-
* SQL it copies the string through verbatim and Postgres rejects
|
|
462
|
-
* `distinctAlias.consumption_"createdAt"`. Unpaginated calls never hit it,
|
|
463
|
-
* which is why it took a page-sized query to surface.
|
|
464
|
-
*/
|
|
465
|
-
queryBuilder.orderBy('consumption.createdAt', 'DESC');
|
|
466
|
-
if (options.offset !== undefined && options.offset >= 0) {
|
|
467
|
-
queryBuilder.skip(options.offset);
|
|
468
|
-
}
|
|
469
|
-
if (options.limit !== undefined && options.limit > 0) {
|
|
470
|
-
queryBuilder.take(options.limit);
|
|
471
|
-
}
|
|
472
|
-
return queryBuilder.getManyAndCount();
|
|
473
|
-
}
|
|
474
|
-
findOne(id) {
|
|
475
|
-
return this.consumptionOrderRepository.findOneOrFail({
|
|
476
|
-
where: { id },
|
|
477
|
-
relations: { items: true },
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
/** Every line of the given orders, for a DataLoader batching one round trip. */
|
|
481
|
-
async findItemsByOrders(orderIds) {
|
|
482
|
-
if (!orderIds.length)
|
|
483
|
-
return [];
|
|
484
|
-
return this.dataSource
|
|
485
|
-
.getRepository(this.consumptionOrderItemEntity)
|
|
486
|
-
.find({
|
|
487
|
-
where: { orderId: In([...orderIds]), removedAt: IsNull() },
|
|
488
|
-
order: { id: 'ASC' },
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
ConsumptionOrderService = __decorate([
|
|
493
|
-
Injectable(),
|
|
494
|
-
__param(5, Inject(ConsumptionOrderEntity)),
|
|
495
|
-
__param(6, Inject(ConsumptionOrderItemEntity)),
|
|
496
|
-
__param(7, Inject(StockEntity)),
|
|
497
|
-
__metadata("design:paramtypes", [DataSource,
|
|
498
|
-
WmsEntities,
|
|
499
|
-
HelperService,
|
|
500
|
-
MaterialService,
|
|
501
|
-
StockService, Object, Object, Object])
|
|
502
|
-
], ConsumptionOrderService);
|
|
503
|
-
|
|
504
|
-
export { ConsumptionOrderService };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { Injectable } from '@nestjs/common';
|
|
3
|
-
import { DataSource } from 'typeorm';
|
|
4
|
-
import { DateTime } from 'luxon';
|
|
5
|
-
|
|
6
|
-
let HelperService = class HelperService {
|
|
7
|
-
dataSource;
|
|
8
|
-
constructor(dataSource) {
|
|
9
|
-
this.dataSource = dataSource;
|
|
10
|
-
}
|
|
11
|
-
async getSeq(prefix, manager = this.dataSource.manager) {
|
|
12
|
-
const dateStr = DateTime.now().toFormat('yyyyMMdd');
|
|
13
|
-
const seqName = `${prefix}_${dateStr}`;
|
|
14
|
-
const quotedSeqName = `"${seqName}"`;
|
|
15
|
-
await this.dataSource.query(`CREATE SEQUENCE IF NOT EXISTS ${quotedSeqName} START 1`);
|
|
16
|
-
const [{ seq_num }] = await manager.query(`SELECT nextval($1) as seq_num`, [
|
|
17
|
-
`"${seqName}"`,
|
|
18
|
-
]);
|
|
19
|
-
const serialNumber = String(seq_num).padStart(4, '0');
|
|
20
|
-
return `${dateStr}${serialNumber}`;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
HelperService = __decorate([
|
|
24
|
-
Injectable(),
|
|
25
|
-
__metadata("design:paramtypes", [DataSource])
|
|
26
|
-
], HelperService);
|
|
27
|
-
|
|
28
|
-
export { HelperService };
|