@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,468 +0,0 @@
|
|
|
1
|
-
import { __decorate, __param, __metadata } from 'tslib';
|
|
2
|
-
import { Injectable, Inject } from '@nestjs/common';
|
|
3
|
-
import { ReceiveInventoryOrderEntity } from '../models/extensions/receive-inventory-order/receive-inventory-order.entity.js';
|
|
4
|
-
import { ReceiveInventoryOrderItemEntity } from '../models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js';
|
|
5
|
-
import { DataSource, IsNull, In } from 'typeorm';
|
|
6
|
-
import { ReceiveInventoryOrderStatus } from '../typings/find-receive-inventory-order.input.js';
|
|
7
|
-
import '../models/metadata/batch.entity.js';
|
|
8
|
-
import '../models/metadata/batch-group.entity.js';
|
|
9
|
-
import { LoadersEntityClass } from '../models/metadata/loader.entity.js';
|
|
10
|
-
import '../models/metadata/loader-type.entity.js';
|
|
11
|
-
import '../models/metadata/loader-unit.entity.js';
|
|
12
|
-
import '../models/metadata/location.entity.js';
|
|
13
|
-
import '../models/metadata/material.entity.js';
|
|
14
|
-
import '../models/metadata/material-unit.entity.js';
|
|
15
|
-
import '@rytass/wms-base-nestjs-module';
|
|
16
|
-
import '../models/metadata/sale-order.entity.js';
|
|
17
|
-
import '../models/metadata/stock.entity.js';
|
|
18
|
-
import '../models/metadata/unit.entity.js';
|
|
19
|
-
import { WmsEntities } from '../models/wms-entities.js';
|
|
20
|
-
import { ReceiveInventoryOrderChangeEntityClass } from '../models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js';
|
|
21
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-item.entity.js';
|
|
22
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order.entity.js';
|
|
23
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.js';
|
|
24
|
-
import '../models/extensions/ship-inventory-order/ship-inventory-order-change.entity.js';
|
|
25
|
-
import '../models/extensions/transfer-order/transfer-order.entity.js';
|
|
26
|
-
import '../models/extensions/transfer-order/transfer-order-item.entity.js';
|
|
27
|
-
import '../models/extensions/transfer-order/transfer-order-change.entity.js';
|
|
28
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order.entity.js';
|
|
29
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js';
|
|
30
|
-
import '../models/extensions/hold-inventory-order/hold-inventory-order-change.entity.js';
|
|
31
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js';
|
|
32
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js';
|
|
33
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.js';
|
|
34
|
-
import '../models/extensions/split-batch-order/split-batch-order.entity.js';
|
|
35
|
-
import '../models/extensions/split-batch-order/split-batch-order-item.entity.js';
|
|
36
|
-
import '../models/extensions/split-batch-order/split-batch-order-change.entity.js';
|
|
37
|
-
import '../models/extensions/merge-batch-order/merge-batch-order.entity.js';
|
|
38
|
-
import '../models/extensions/merge-batch-order/merge-batch-order-item.entity.js';
|
|
39
|
-
import '../models/extensions/merge-batch-order/merge-batch-order-change.entity.js';
|
|
40
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js';
|
|
41
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js';
|
|
42
|
-
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.js';
|
|
43
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order.entity.js';
|
|
44
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js';
|
|
45
|
-
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.js';
|
|
46
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order.entity.js';
|
|
47
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js';
|
|
48
|
-
import '../models/extensions/quality-inspection-order/quality-inspection-order-change.entity.js';
|
|
49
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order.entity.js';
|
|
50
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js';
|
|
51
|
-
import '../models/extensions/transfer-customer-order/transfer-customer-order-change.entity.js';
|
|
52
|
-
import '../models/extensions/scrape-order/scrape-order.entity.js';
|
|
53
|
-
import '../models/extensions/scrape-order/scrape-order-item.entity.js';
|
|
54
|
-
import '../models/extensions/scrape-order/scrape-order-change.entity.js';
|
|
55
|
-
import '../models/extensions/inventory-count-order/inventory-count-order.entity.js';
|
|
56
|
-
import '../models/extensions/inventory-count-order/inventory-count-order-item.entity.js';
|
|
57
|
-
import '../models/extensions/inventory-count-order/inventory-count-order-change.entity.js';
|
|
58
|
-
import '../models/extensions/consumption-order/consumption-order.entity.js';
|
|
59
|
-
import '../models/extensions/consumption-order/consumption-order-item.entity.js';
|
|
60
|
-
import '../models/extensions/consumption-order/consumption-order-change.entity.js';
|
|
61
|
-
import '../models/extensions/order-reference/order-reference.entity.js';
|
|
62
|
-
import { MaterialService } from './material.service.js';
|
|
63
|
-
import { BatchService } from './batch.service.js';
|
|
64
|
-
import { HelperService } from './helper.service.js';
|
|
65
|
-
import { StockService } from './stock.service.js';
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Service for managing receive inventory orders and their items
|
|
69
|
-
* Supports dynamic entity injection for customization
|
|
70
|
-
* T - Receive Inventory Order entity type
|
|
71
|
-
* K - Receive Inventory Order Item entity type
|
|
72
|
-
*/
|
|
73
|
-
let ReceiveInventoryOrderService = class ReceiveInventoryOrderService {
|
|
74
|
-
dataSource;
|
|
75
|
-
entities;
|
|
76
|
-
materialService;
|
|
77
|
-
batchService;
|
|
78
|
-
helperService;
|
|
79
|
-
stockService;
|
|
80
|
-
receiveInventoryOrderEntity;
|
|
81
|
-
receiveInventoryOrderItemEntity;
|
|
82
|
-
selectQueryBuilder;
|
|
83
|
-
constructor(dataSource, entities, materialService, batchService, helperService, stockService, receiveInventoryOrderEntity, receiveInventoryOrderItemEntity) {
|
|
84
|
-
this.dataSource = dataSource;
|
|
85
|
-
this.entities = entities;
|
|
86
|
-
this.materialService = materialService;
|
|
87
|
-
this.batchService = batchService;
|
|
88
|
-
this.helperService = helperService;
|
|
89
|
-
this.stockService = stockService;
|
|
90
|
-
this.receiveInventoryOrderEntity = receiveInventoryOrderEntity;
|
|
91
|
-
this.receiveInventoryOrderItemEntity = receiveInventoryOrderItemEntity;
|
|
92
|
-
this.selectQueryBuilder = (alias) => {
|
|
93
|
-
return this.dataSource
|
|
94
|
-
.getRepository(this.receiveInventoryOrderEntity)
|
|
95
|
-
.createQueryBuilder(alias)
|
|
96
|
-
.leftJoin((subQuery) => subQuery
|
|
97
|
-
.select(`CASE
|
|
98
|
-
WHEN o."removedAt" IS NOT NULL THEN '${ReceiveInventoryOrderStatus.REMOVED}'
|
|
99
|
-
WHEN COALESCE((
|
|
100
|
-
SELECT SUM(CAST(c."quantityChanged" AS NUMERIC))
|
|
101
|
-
FROM receive_inventory_order_changes c
|
|
102
|
-
JOIN receive_inventory_order_items i ON c."itemId" = i.id
|
|
103
|
-
WHERE i."orderId" = o.id
|
|
104
|
-
), 0) = COALESCE(target_sum.targetsum, 0) AND COALESCE(target_sum.targetsum, 0) > 0 THEN '${ReceiveInventoryOrderStatus.RECEIVED}'
|
|
105
|
-
WHEN COALESCE((
|
|
106
|
-
SELECT SUM(CAST(c."quantityChanged" AS NUMERIC))
|
|
107
|
-
FROM receive_inventory_order_changes c
|
|
108
|
-
JOIN receive_inventory_order_items i ON c."itemId" = i.id
|
|
109
|
-
WHERE i."orderId" = o.id
|
|
110
|
-
), 0) > 0 AND COALESCE((
|
|
111
|
-
SELECT SUM(CAST(c."quantityChanged" AS NUMERIC))
|
|
112
|
-
FROM receive_inventory_order_changes c
|
|
113
|
-
JOIN receive_inventory_order_items i ON c."itemId" = i.id
|
|
114
|
-
WHERE i."orderId" = o.id
|
|
115
|
-
), 0) < COALESCE(target_sum.targetsum, 0) THEN '${ReceiveInventoryOrderStatus.PARTIAL_RECEIVED}'
|
|
116
|
-
ELSE '${ReceiveInventoryOrderStatus.PENDING}'
|
|
117
|
-
END`, 'status')
|
|
118
|
-
.addSelect('o.id', 'orderid')
|
|
119
|
-
.from('receive_inventory_orders', 'o')
|
|
120
|
-
.leftJoin((sumQuery) => sumQuery
|
|
121
|
-
.select('i."orderId"', 'orderid')
|
|
122
|
-
.addSelect('SUM(CAST(i."targetReceivedQuantity" AS NUMERIC))', 'targetsum')
|
|
123
|
-
.from('receive_inventory_order_items', 'i')
|
|
124
|
-
.groupBy('i."orderId"'), 'target_sum', 'target_sum.orderid = o.id'), 'order_status', `${alias}.id = order_status.orderid`)
|
|
125
|
-
.select('orders.id', 'id')
|
|
126
|
-
.addSelect('orders.key', 'key')
|
|
127
|
-
.addSelect('orders.description', 'description')
|
|
128
|
-
.addSelect('orders."createdBy"', 'createdBy')
|
|
129
|
-
.addSelect('orders."createdAt"', 'createdAt')
|
|
130
|
-
.addSelect('orders."removedAt"', 'removedAt')
|
|
131
|
-
.addSelect('order_status.status', `status`);
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
processReceiveInventoryOrderItems(items, orderId, manager) {
|
|
135
|
-
const materialRepository = manager.getRepository(this.materialService.materialEntity);
|
|
136
|
-
const locationRepository = manager.getRepository(this.entities.location);
|
|
137
|
-
const loaderRepository = manager.getRepository(LoadersEntityClass);
|
|
138
|
-
return items.reduce(async (accPromise, item) => {
|
|
139
|
-
const acc = await accPromise;
|
|
140
|
-
try {
|
|
141
|
-
const material = await materialRepository.findOneBy({
|
|
142
|
-
key: item.materialKey,
|
|
143
|
-
});
|
|
144
|
-
if (!material)
|
|
145
|
-
return acc;
|
|
146
|
-
const batch = item.batchKey
|
|
147
|
-
? await this.batchService.getBatch({
|
|
148
|
-
manager,
|
|
149
|
-
materialId: material.id,
|
|
150
|
-
key: item.batchKey,
|
|
151
|
-
})
|
|
152
|
-
: null;
|
|
153
|
-
const location = item.locationKey
|
|
154
|
-
? await locationRepository.findOneBy({
|
|
155
|
-
key: item.locationKey,
|
|
156
|
-
})
|
|
157
|
-
: null;
|
|
158
|
-
const loader = item.loaderKey
|
|
159
|
-
? await loaderRepository.findOneBy({
|
|
160
|
-
serialId: item.loaderKey,
|
|
161
|
-
})
|
|
162
|
-
: null;
|
|
163
|
-
return [
|
|
164
|
-
...acc,
|
|
165
|
-
{
|
|
166
|
-
...item,
|
|
167
|
-
orderId,
|
|
168
|
-
materialId: material.id,
|
|
169
|
-
batchId: batch?.id || null,
|
|
170
|
-
locationId: this.stockService.resolveLocationId(location?.id),
|
|
171
|
-
loaderId: loader?.id || null,
|
|
172
|
-
},
|
|
173
|
-
];
|
|
174
|
-
}
|
|
175
|
-
catch {
|
|
176
|
-
return acc;
|
|
177
|
-
}
|
|
178
|
-
}, Promise.resolve([]));
|
|
179
|
-
}
|
|
180
|
-
async createReceiveInventoryOrder({ manager = this.dataSource.manager, ...options }) {
|
|
181
|
-
const orderRepository = manager.getRepository(this.entities.order);
|
|
182
|
-
const receiveInventoryOrderRepository = manager.getRepository(this.receiveInventoryOrderEntity);
|
|
183
|
-
const receiveInventoryOrderItemRepository = manager.getRepository(this.receiveInventoryOrderItemEntity);
|
|
184
|
-
// create base order
|
|
185
|
-
const { id } = await orderRepository.save(orderRepository.create());
|
|
186
|
-
// create receive inventory order
|
|
187
|
-
const { items, ...orderOptions } = options;
|
|
188
|
-
const order = await receiveInventoryOrderRepository.save(receiveInventoryOrderRepository.create({ id, ...orderOptions }));
|
|
189
|
-
// create receive inventory order items
|
|
190
|
-
if (items && items.length > 0) {
|
|
191
|
-
const processedItems = receiveInventoryOrderItemRepository.create(await this.processReceiveInventoryOrderItems(items, id, manager));
|
|
192
|
-
await receiveInventoryOrderItemRepository.save(processedItems);
|
|
193
|
-
}
|
|
194
|
-
return order;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Update a receive inventory order and its items
|
|
198
|
-
* update existing item when id is provided, otherwise create new item, remove behaviour controlled by removedAt field
|
|
199
|
-
*/
|
|
200
|
-
async updateReceiveInventoryOrder({ id, manager = this.dataSource.manager, ...options }) {
|
|
201
|
-
const receiveInventoryOrderRepository = manager.getRepository(this.receiveInventoryOrderEntity);
|
|
202
|
-
const receiveInventoryOrderItemRepository = manager.getRepository(this.receiveInventoryOrderItemEntity);
|
|
203
|
-
const order = await receiveInventoryOrderRepository.findOneByOrFail({
|
|
204
|
-
id,
|
|
205
|
-
removedAt: IsNull(),
|
|
206
|
-
});
|
|
207
|
-
const { items, ...orderOptions } = options;
|
|
208
|
-
const updated = await receiveInventoryOrderRepository.save(receiveInventoryOrderRepository.create({
|
|
209
|
-
...order,
|
|
210
|
-
...orderOptions,
|
|
211
|
-
}));
|
|
212
|
-
if (items?.length) {
|
|
213
|
-
const newItems = [];
|
|
214
|
-
const existingItems = [];
|
|
215
|
-
await receiveInventoryOrderItemRepository
|
|
216
|
-
.find({ where: { orderId: id } })
|
|
217
|
-
.then((items) => {
|
|
218
|
-
const itemSet = new Set(items.map((i) => i.id));
|
|
219
|
-
options.items?.forEach((item) => {
|
|
220
|
-
if (!item.id)
|
|
221
|
-
newItems.push(item);
|
|
222
|
-
else if (itemSet.has(item.id))
|
|
223
|
-
existingItems.push(item);
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
if (newItems.length)
|
|
227
|
-
await receiveInventoryOrderItemRepository.save(receiveInventoryOrderItemRepository.create(await this.processReceiveInventoryOrderItems(newItems, id, manager)));
|
|
228
|
-
if (existingItems.length)
|
|
229
|
-
await receiveInventoryOrderItemRepository.save(existingItems.map((item) => receiveInventoryOrderItemRepository.create({
|
|
230
|
-
...item,
|
|
231
|
-
id: item.id,
|
|
232
|
-
})));
|
|
233
|
-
}
|
|
234
|
-
return updated;
|
|
235
|
-
}
|
|
236
|
-
async removeReceiveInventoryOrder(id) {
|
|
237
|
-
return this.dataSource
|
|
238
|
-
.getRepository(this.receiveInventoryOrderEntity)
|
|
239
|
-
.update({ id }, { removedAt: new Date() })
|
|
240
|
-
.then(({ affected }) => !!affected && affected > 0);
|
|
241
|
-
}
|
|
242
|
-
async findMany(options) {
|
|
243
|
-
const queryBuilder = this.selectQueryBuilder('orders');
|
|
244
|
-
if (options.id)
|
|
245
|
-
queryBuilder.andWhere('orders.id = :id', { id: options.id });
|
|
246
|
-
if (options.createdBy)
|
|
247
|
-
queryBuilder.andWhere('orders."createdBy" ILIKE :createdBy', {
|
|
248
|
-
createdBy: `%${options.createdBy}%`,
|
|
249
|
-
});
|
|
250
|
-
if (options.description)
|
|
251
|
-
queryBuilder.andWhere('orders.description ILIKE :description', {
|
|
252
|
-
description: `%${options.description}%`,
|
|
253
|
-
});
|
|
254
|
-
if (options.dateRange)
|
|
255
|
-
queryBuilder.andWhere('orders."createdAt" BETWEEN :from AND :to', {
|
|
256
|
-
from: options.dateRange.from,
|
|
257
|
-
to: options.dateRange.to,
|
|
258
|
-
});
|
|
259
|
-
if (options.status)
|
|
260
|
-
queryBuilder.andWhere(`status = :status`, {
|
|
261
|
-
status: options.status,
|
|
262
|
-
});
|
|
263
|
-
if (options.statuses && options.statuses.length > 0)
|
|
264
|
-
queryBuilder.andWhere(`status IN (:...statuses)`, {
|
|
265
|
-
statuses: options.statuses,
|
|
266
|
-
});
|
|
267
|
-
if (options.materialKey)
|
|
268
|
-
queryBuilder.andWhere(`EXISTS (
|
|
269
|
-
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
270
|
-
INNER JOIN materials ri_mat ON ri_mat.id = ri_items."materialId"
|
|
271
|
-
WHERE ri_items."orderId" = orders.id
|
|
272
|
-
AND ri_mat.key ILIKE :materialKey
|
|
273
|
-
)`, { materialKey: `%${options.materialKey}%` });
|
|
274
|
-
if (options.batchKey)
|
|
275
|
-
queryBuilder.andWhere(`EXISTS (
|
|
276
|
-
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
277
|
-
INNER JOIN batches ri_batch ON ri_batch.id = ri_items."batchId"
|
|
278
|
-
WHERE ri_items."orderId" = orders.id
|
|
279
|
-
AND ri_batch.key ILIKE :batchKey
|
|
280
|
-
)`, { batchKey: `%${options.batchKey}%` });
|
|
281
|
-
if (options.locationKey)
|
|
282
|
-
queryBuilder.andWhere(`EXISTS (
|
|
283
|
-
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
284
|
-
INNER JOIN locations ri_loc ON ri_loc.id = ri_items."locationId"
|
|
285
|
-
WHERE ri_items."orderId" = orders.id
|
|
286
|
-
AND ri_loc.key ILIKE :locationKey
|
|
287
|
-
)`, { locationKey: `%${options.locationKey}%` });
|
|
288
|
-
if (options.loaderKey)
|
|
289
|
-
queryBuilder.andWhere(`EXISTS (
|
|
290
|
-
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
291
|
-
INNER JOIN loaders ri_ldr ON ri_ldr.id = ri_items."loaderId"
|
|
292
|
-
WHERE ri_items."orderId" = orders.id
|
|
293
|
-
AND ri_ldr."serialId" ILIKE :loaderKey
|
|
294
|
-
)`, { loaderKey: `%${options.loaderKey}%` });
|
|
295
|
-
if (options.wildcard) {
|
|
296
|
-
const { key, value } = options.wildcard;
|
|
297
|
-
queryBuilder.andWhere(`orders."${String(key)}" ILIKE :value`, {
|
|
298
|
-
value: `%${value}%`,
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
const total = await queryBuilder.getCount();
|
|
302
|
-
if (options.offset !== undefined && options.offset >= 0)
|
|
303
|
-
queryBuilder.skip(options.offset);
|
|
304
|
-
if (options.limit !== undefined && options.limit > 0)
|
|
305
|
-
queryBuilder.take(options.limit);
|
|
306
|
-
const items = await queryBuilder.getRawMany();
|
|
307
|
-
return [items, total];
|
|
308
|
-
}
|
|
309
|
-
receive(options) {
|
|
310
|
-
if (!Array.isArray(options))
|
|
311
|
-
options = [options];
|
|
312
|
-
return this.dataSource.transaction(async (manager) => {
|
|
313
|
-
const changeRepository = manager.getRepository(ReceiveInventoryOrderChangeEntityClass);
|
|
314
|
-
const itemRepository = manager.getRepository(this.receiveInventoryOrderItemEntity);
|
|
315
|
-
const orderRepository = manager.getRepository(this.entities.order);
|
|
316
|
-
const locationRepository = manager.getRepository(this.entities.location);
|
|
317
|
-
const loaderRepository = manager.getRepository(LoadersEntityClass);
|
|
318
|
-
const stockRepository = manager.getRepository(this.entities.stock);
|
|
319
|
-
const batchRepository = manager.getRepository(this.entities.batch);
|
|
320
|
-
const items = await itemRepository.find({
|
|
321
|
-
where: { id: In(options.map((o) => o.itemId)) },
|
|
322
|
-
relations: { changes: true },
|
|
323
|
-
});
|
|
324
|
-
const itemMap = new Map(items.map((i) => [i.id, i]));
|
|
325
|
-
/*
|
|
326
|
-
* Sequential, not `Promise.all`. Two options for one item both reach the
|
|
327
|
-
* batch lookups below before either has written, so `splitBatch`'s
|
|
328
|
-
* read-then-create misses twice and one receipt ends up fragmented across
|
|
329
|
-
* two batch identities. A receive order has a handful of lines; ordering
|
|
330
|
-
* them costs nothing worth measuring.
|
|
331
|
-
*/
|
|
332
|
-
for (const option of options) {
|
|
333
|
-
await (async () => {
|
|
334
|
-
const item = itemMap.get(option.itemId);
|
|
335
|
-
if (!item)
|
|
336
|
-
return;
|
|
337
|
-
if (!item.batchId && option.batchKey) {
|
|
338
|
-
// if batchKey is provided but item doesn't have batchId, attempt to find or create batch and update item with batchId
|
|
339
|
-
item.batchId = await this.batchService
|
|
340
|
-
.getBatch({
|
|
341
|
-
manager,
|
|
342
|
-
key: option.batchKey,
|
|
343
|
-
materialId: item.materialId,
|
|
344
|
-
})
|
|
345
|
-
.then(async (b) => {
|
|
346
|
-
await itemRepository.update({ id: item.id }, { batchId: b.id });
|
|
347
|
-
return b.id;
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
351
|
-
const totalChanged = item.changes.reduce((sum, change) => sum + Number(change.quantityChanged), 0);
|
|
352
|
-
const remainingQuantity = conversionFactor * item.targetReceivedQuantity - totalChanged;
|
|
353
|
-
if (remainingQuantity - option.quantity * conversionFactor < 0) {
|
|
354
|
-
throw new Error(`Receiving quantity exceeds target for item ${option.itemId}`);
|
|
355
|
-
}
|
|
356
|
-
const newOrder = await orderRepository.save(orderRepository.create());
|
|
357
|
-
let batchId = item.batchId;
|
|
358
|
-
if (option.batchKey)
|
|
359
|
-
batchId = await this.batchService
|
|
360
|
-
.getBatch({
|
|
361
|
-
manager,
|
|
362
|
-
key: option.batchKey,
|
|
363
|
-
materialId: item.materialId,
|
|
364
|
-
})
|
|
365
|
-
.then(({ id }) => id);
|
|
366
|
-
if (!batchId)
|
|
367
|
-
batchId = await this.batchService
|
|
368
|
-
.getBatch({
|
|
369
|
-
manager,
|
|
370
|
-
key: await this.helperService.getSeq('BATCH'),
|
|
371
|
-
materialId: item.materialId,
|
|
372
|
-
})
|
|
373
|
-
.then(({ id }) => id);
|
|
374
|
-
const inspectionResult = item.requireInspection === false
|
|
375
|
-
? true
|
|
376
|
-
: (option.inspectionResult ?? null);
|
|
377
|
-
if (inspectionResult != null) {
|
|
378
|
-
batchId = await this.batchService
|
|
379
|
-
.splitBatch({
|
|
380
|
-
manager,
|
|
381
|
-
id: batchId,
|
|
382
|
-
inspectionResult,
|
|
383
|
-
})
|
|
384
|
-
.then(({ id }) => id);
|
|
385
|
-
}
|
|
386
|
-
else if (item.requireInspection) {
|
|
387
|
-
/*
|
|
388
|
-
* Somebody asked for this to be inspected and it has not been yet.
|
|
389
|
-
* Recording that on the batch is what lets `PENDING_INSPECTION`
|
|
390
|
-
* mean "awaiting inspection" rather than "never inspected" -- the
|
|
391
|
-
* latter being true of almost every batch in the warehouse.
|
|
392
|
-
*
|
|
393
|
-
* Split only when the batch already holds something. `require-
|
|
394
|
-
* Inspection` defaults to **true**, so this is the ordinary receive
|
|
395
|
-
* path, not an edge case: splitting unconditionally moved every
|
|
396
|
-
* default receipt off the batch key the caller asked for and onto a
|
|
397
|
-
* synthetic child, which an exact-id lookup then could not find.
|
|
398
|
-
*
|
|
399
|
-
* A batch with nothing on it yet has nothing to commingle with, so
|
|
400
|
-
* flagging it in place is both sufficient and invisible. Only a
|
|
401
|
-
* receipt joining stock that is already there needs its own batch,
|
|
402
|
-
* which is the same thing holding or shipping part of a batch does.
|
|
403
|
-
*/
|
|
404
|
-
const onHand = await stockRepository
|
|
405
|
-
.createQueryBuilder('stock')
|
|
406
|
-
.select('COALESCE(SUM(stock.quantity), 0)', 'total')
|
|
407
|
-
.where('stock."batchId" = :batchId', { batchId })
|
|
408
|
-
.getRawOne()
|
|
409
|
-
.then((row) => parseFloat(row?.total ?? '0'));
|
|
410
|
-
if (onHand > 0) {
|
|
411
|
-
batchId = await this.batchService
|
|
412
|
-
.splitBatch({
|
|
413
|
-
manager,
|
|
414
|
-
id: batchId,
|
|
415
|
-
requiresInspection: true,
|
|
416
|
-
})
|
|
417
|
-
.then(({ id }) => id);
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
await batchRepository.update({ id: batchId }, {
|
|
421
|
-
requiresInspection: true,
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
let locationId = item.locationId;
|
|
426
|
-
if (option.locationKey && !locationId)
|
|
427
|
-
locationId = await locationRepository
|
|
428
|
-
.findOneByOrFail({ key: option.locationKey })
|
|
429
|
-
.then(({ id }) => id);
|
|
430
|
-
let loaderId = item.loaderId;
|
|
431
|
-
if (option.loaderKey && !loaderId)
|
|
432
|
-
loaderId = await loaderRepository
|
|
433
|
-
.findOneByOrFail({ serialId: option.loaderKey })
|
|
434
|
-
.then(({ id }) => id);
|
|
435
|
-
await Promise.all([
|
|
436
|
-
changeRepository.save(changeRepository.create({
|
|
437
|
-
itemId: option.itemId,
|
|
438
|
-
quantityChanged: option.quantity * conversionFactor,
|
|
439
|
-
id: newOrder.id,
|
|
440
|
-
})),
|
|
441
|
-
stockRepository.save(stockRepository.create({
|
|
442
|
-
batchId: batchId || undefined,
|
|
443
|
-
locationId: this.stockService.resolveLocationId(locationId),
|
|
444
|
-
loaderId: this.stockService.resolveLoaderId(loaderId),
|
|
445
|
-
materialId: item.materialId,
|
|
446
|
-
quantity: option.quantity * conversionFactor,
|
|
447
|
-
orderId: newOrder.id,
|
|
448
|
-
})),
|
|
449
|
-
]);
|
|
450
|
-
})();
|
|
451
|
-
}
|
|
452
|
-
return true;
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
ReceiveInventoryOrderService = __decorate([
|
|
457
|
-
Injectable(),
|
|
458
|
-
__param(6, Inject(ReceiveInventoryOrderEntity)),
|
|
459
|
-
__param(7, Inject(ReceiveInventoryOrderItemEntity)),
|
|
460
|
-
__metadata("design:paramtypes", [DataSource,
|
|
461
|
-
WmsEntities,
|
|
462
|
-
MaterialService,
|
|
463
|
-
BatchService,
|
|
464
|
-
HelperService,
|
|
465
|
-
StockService, Object, Object])
|
|
466
|
-
], ReceiveInventoryOrderService);
|
|
467
|
-
|
|
468
|
-
export { ReceiveInventoryOrderService };
|
|
@@ -1,192 +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 '../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 '../models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js';
|
|
30
|
-
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js';
|
|
31
|
-
import '../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 { ReclassifyInventoryOrderEntity } from '../models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js';
|
|
39
|
-
import { ReclassifyInventoryOrderItemEntity } from '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js';
|
|
40
|
-
import { ReclassifyInventoryOrderChangeEntityClass } from '../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 } from 'typeorm';
|
|
61
|
-
import { MaterialService } from './material.service.js';
|
|
62
|
-
import { BatchService } from './batch.service.js';
|
|
63
|
-
import { HelperService } from './helper.service.js';
|
|
64
|
-
import { StockService } from './stock.service.js';
|
|
65
|
-
|
|
66
|
-
let ReclassifyInventoryOrderService = class ReclassifyInventoryOrderService {
|
|
67
|
-
dataSource;
|
|
68
|
-
entities;
|
|
69
|
-
materialService;
|
|
70
|
-
batchService;
|
|
71
|
-
helperService;
|
|
72
|
-
stockService;
|
|
73
|
-
reclassifyInventoryOrderEntity;
|
|
74
|
-
reclassifyInventoryOrderItemEntity;
|
|
75
|
-
seqPrefix = 'RCO';
|
|
76
|
-
constructor(dataSource, entities, materialService, batchService, helperService, stockService, reclassifyInventoryOrderEntity, reclassifyInventoryOrderItemEntity) {
|
|
77
|
-
this.dataSource = dataSource;
|
|
78
|
-
this.entities = entities;
|
|
79
|
-
this.materialService = materialService;
|
|
80
|
-
this.batchService = batchService;
|
|
81
|
-
this.helperService = helperService;
|
|
82
|
-
this.stockService = stockService;
|
|
83
|
-
this.reclassifyInventoryOrderEntity = reclassifyInventoryOrderEntity;
|
|
84
|
-
this.reclassifyInventoryOrderItemEntity = reclassifyInventoryOrderItemEntity;
|
|
85
|
-
}
|
|
86
|
-
reclassify(options) {
|
|
87
|
-
return this.dataSource.transaction(async (manager) => {
|
|
88
|
-
const reclassifyOrderRepository = manager.getRepository(this.reclassifyInventoryOrderEntity);
|
|
89
|
-
const reclassifyOrderItemRepository = manager.getRepository(this.reclassifyInventoryOrderItemEntity);
|
|
90
|
-
const reclassifyOrderChangeRepository = manager.getRepository(ReclassifyInventoryOrderChangeEntityClass);
|
|
91
|
-
const orderRepository = manager.getRepository(this.entities.order);
|
|
92
|
-
const stockRepository = manager.getRepository(this.entities.stock);
|
|
93
|
-
const batchRepository = manager.getRepository(this.entities.batch);
|
|
94
|
-
const materialRepository = manager.getRepository(this.materialService.materialEntity);
|
|
95
|
-
// Ensure new material exists
|
|
96
|
-
const material = await materialRepository.findOneOrFail({
|
|
97
|
-
where: { key: options.materialKey },
|
|
98
|
-
});
|
|
99
|
-
const key = await this.helperService.getSeq(this.seqPrefix, manager);
|
|
100
|
-
const order = await reclassifyOrderRepository.save(reclassifyOrderRepository.create({
|
|
101
|
-
key,
|
|
102
|
-
}));
|
|
103
|
-
// Process each item reclassification
|
|
104
|
-
await Promise.all(options.items.map(async (item) => {
|
|
105
|
-
if (item.materialId === material.id) {
|
|
106
|
-
throw new Error(`Item with batchId ${item.batchId} is already classified under the target material`);
|
|
107
|
-
}
|
|
108
|
-
// Get conversion factor for unit conversion
|
|
109
|
-
const conversionFactor = await this.materialService.getConversionFactor(material.id, item.unitId || undefined);
|
|
110
|
-
const { key, ...batch } = await batchRepository.findOneOrFail({
|
|
111
|
-
where: { id: item.batchId },
|
|
112
|
-
});
|
|
113
|
-
// Get or create a batch for the new material
|
|
114
|
-
const targetBatch = await this.batchService
|
|
115
|
-
.getBatch({
|
|
116
|
-
materialId: material.id,
|
|
117
|
-
key,
|
|
118
|
-
manager,
|
|
119
|
-
})
|
|
120
|
-
.then(({ id }) => this.batchService.splitBatch({
|
|
121
|
-
saleId: batch.saleId ?? null,
|
|
122
|
-
saleSerialId: batch.saleSerialId ?? null,
|
|
123
|
-
customerId: batch.customerId ?? null,
|
|
124
|
-
vendorId: batch.vendorId ?? null,
|
|
125
|
-
inspectionResult: batch.inspectionResult ?? null,
|
|
126
|
-
transferId: batch.transferId ?? null,
|
|
127
|
-
held: batch.held ?? false,
|
|
128
|
-
inShipment: batch.inShipment ?? false,
|
|
129
|
-
manager,
|
|
130
|
-
id,
|
|
131
|
-
}));
|
|
132
|
-
// Create base order record
|
|
133
|
-
const baseOrder = await orderRepository.save({});
|
|
134
|
-
// Create reclassify order item
|
|
135
|
-
const reclassifyItem = await reclassifyOrderItemRepository.save(reclassifyOrderItemRepository.create({
|
|
136
|
-
targetReclassifiedQuantity: item.quantity,
|
|
137
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
138
|
-
loaderId: item.loaderId,
|
|
139
|
-
orderId: order.id,
|
|
140
|
-
}));
|
|
141
|
-
// Record the change
|
|
142
|
-
await reclassifyOrderChangeRepository.save(reclassifyOrderChangeRepository.create({
|
|
143
|
-
itemId: reclassifyItem.id,
|
|
144
|
-
id: baseOrder.id,
|
|
145
|
-
quantityChanged: item.quantity * conversionFactor,
|
|
146
|
-
}));
|
|
147
|
-
// Update stock: deduct from original material and add to new material
|
|
148
|
-
await this.stockService.validateStockAvailability({
|
|
149
|
-
materialId: item.materialId,
|
|
150
|
-
batchId: item.batchId,
|
|
151
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
152
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
153
|
-
quantity: item.quantity * conversionFactor,
|
|
154
|
-
}, manager);
|
|
155
|
-
await stockRepository.save(stockRepository.create([
|
|
156
|
-
// Deduct from original material batch
|
|
157
|
-
{
|
|
158
|
-
orderId: baseOrder.id,
|
|
159
|
-
materialId: item.materialId,
|
|
160
|
-
batchId: item.batchId,
|
|
161
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
162
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
163
|
-
quantity: -1 * item.quantity * conversionFactor,
|
|
164
|
-
},
|
|
165
|
-
// Add to new material batch
|
|
166
|
-
{
|
|
167
|
-
orderId: baseOrder.id,
|
|
168
|
-
materialId: material.id,
|
|
169
|
-
batchId: targetBatch.id,
|
|
170
|
-
locationId: this.stockService.resolveLocationId(item.locationId),
|
|
171
|
-
loaderId: this.stockService.resolveLoaderId(item.loaderId),
|
|
172
|
-
quantity: item.quantity * conversionFactor,
|
|
173
|
-
},
|
|
174
|
-
]));
|
|
175
|
-
}));
|
|
176
|
-
return order;
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
ReclassifyInventoryOrderService = __decorate([
|
|
181
|
-
Injectable(),
|
|
182
|
-
__param(6, Inject(ReclassifyInventoryOrderEntity)),
|
|
183
|
-
__param(7, Inject(ReclassifyInventoryOrderItemEntity)),
|
|
184
|
-
__metadata("design:paramtypes", [DataSource,
|
|
185
|
-
WmsEntities,
|
|
186
|
-
MaterialService,
|
|
187
|
-
BatchService,
|
|
188
|
-
HelperService,
|
|
189
|
-
StockService, Object, Object])
|
|
190
|
-
], ReclassifyInventoryOrderService);
|
|
191
|
-
|
|
192
|
-
export { ReclassifyInventoryOrderService };
|