@rytass/wms-module-core 0.1.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/LICENSE +21 -0
- package/README.md +80 -0
- package/index.cjs.js +6479 -0
- package/index.d.ts +5 -0
- package/index.js +74 -0
- package/lib/constants/index.d.ts +1 -0
- package/lib/constants/stock-status.d.ts +30 -0
- package/lib/constants/stock-status.js +51 -0
- package/lib/core.module.d.ts +7 -0
- package/lib/core.module.js +102 -0
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.js +40 -0
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.d.ts +20 -0
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js +103 -0
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order.entity.d.ts +8 -0
- package/lib/models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js +33 -0
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order-change.entity.js +40 -0
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order-item.entity.d.ts +19 -0
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js +97 -0
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order.entity.d.ts +10 -0
- package/lib/models/extensions/hold-inventory-order/hold-inventory-order.entity.js +43 -0
- package/lib/models/extensions/index.d.ts +37 -0
- package/lib/models/extensions/merge-batch-order/merge-batch-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/merge-batch-order/merge-batch-order-change.entity.js +40 -0
- package/lib/models/extensions/merge-batch-order/merge-batch-order-item.entity.d.ts +15 -0
- package/lib/models/extensions/merge-batch-order/merge-batch-order-item.entity.js +74 -0
- package/lib/models/extensions/merge-batch-order/merge-batch-order.entity.d.ts +8 -0
- package/lib/models/extensions/merge-batch-order/merge-batch-order.entity.js +33 -0
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order-change.entity.d.ts +11 -0
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order-change.entity.js +45 -0
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order-item.entity.d.ts +18 -0
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js +93 -0
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order.entity.d.ts +9 -0
- package/lib/models/extensions/quality-inspection-order/quality-inspection-order.entity.js +38 -0
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order-change.entity.d.ts +12 -0
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js +46 -0
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order-item.entity.d.ts +19 -0
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js +89 -0
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order.entity.d.ts +13 -0
- package/lib/models/extensions/receive-inventory-order/receive-inventory-order.entity.js +57 -0
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.js +40 -0
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.d.ts +15 -0
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js +74 -0
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.d.ts +8 -0
- package/lib/models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js +33 -0
- package/lib/models/extensions/scrape-order/scrape-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/scrape-order/scrape-order-change.entity.js +40 -0
- package/lib/models/extensions/scrape-order/scrape-order-item.entity.d.ts +18 -0
- package/lib/models/extensions/scrape-order/scrape-order-item.entity.js +93 -0
- package/lib/models/extensions/scrape-order/scrape-order.entity.d.ts +8 -0
- package/lib/models/extensions/scrape-order/scrape-order.entity.js +33 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-change.entity.d.ts +12 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-change.entity.js +46 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-item.entity.d.ts +19 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-item.entity.js +90 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.d.ts +13 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.js +65 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order.entity.d.ts +13 -0
- package/lib/models/extensions/ship-inventory-order/ship-inventory-order.entity.js +56 -0
- package/lib/models/extensions/split-batch-order/split-batch-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/split-batch-order/split-batch-order-change.entity.js +40 -0
- package/lib/models/extensions/split-batch-order/split-batch-order-item.entity.d.ts +15 -0
- package/lib/models/extensions/split-batch-order/split-batch-order-item.entity.js +74 -0
- package/lib/models/extensions/split-batch-order/split-batch-order.entity.d.ts +8 -0
- package/lib/models/extensions/split-batch-order/split-batch-order.entity.js +33 -0
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order-change.entity.js +40 -0
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order-item.entity.d.ts +19 -0
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js +98 -0
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order.entity.d.ts +8 -0
- package/lib/models/extensions/transfer-customer-order/transfer-customer-order.entity.js +33 -0
- package/lib/models/extensions/transfer-order/transfer-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/transfer-order/transfer-order-change.entity.js +40 -0
- package/lib/models/extensions/transfer-order/transfer-order-item.entity.d.ts +21 -0
- package/lib/models/extensions/transfer-order/transfer-order-item.entity.js +108 -0
- package/lib/models/extensions/transfer-order/transfer-order.entity.d.ts +12 -0
- package/lib/models/extensions/transfer-order/transfer-order.entity.js +53 -0
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.d.ts +10 -0
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.js +40 -0
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.d.ts +19 -0
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js +98 -0
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order.entity.d.ts +8 -0
- package/lib/models/extensions/transfer-vendor-order/transfer-vendor-order.entity.js +33 -0
- package/lib/models/index.d.ts +2 -0
- package/lib/models/metadata/batch-group.entity.d.ts +8 -0
- package/lib/models/metadata/batch-group.entity.js +31 -0
- package/lib/models/metadata/batch.entity.d.ts +15 -0
- package/lib/models/metadata/batch.entity.js +73 -0
- package/lib/models/metadata/index.d.ts +11 -0
- package/lib/models/metadata/loader-type.entity.d.ts +9 -0
- package/lib/models/metadata/loader-type.entity.js +43 -0
- package/lib/models/metadata/loader-unit.entity.d.ts +5 -0
- package/lib/models/metadata/loader-unit.entity.js +21 -0
- package/lib/models/metadata/loader.entity.d.ts +21 -0
- package/lib/models/metadata/loader.entity.js +98 -0
- package/lib/models/metadata/location.entity.d.ts +8 -0
- package/lib/models/metadata/location.entity.js +28 -0
- package/lib/models/metadata/material-unit.entity.d.ts +11 -0
- package/lib/models/metadata/material-unit.entity.js +51 -0
- package/lib/models/metadata/material.entity.d.ts +7 -0
- package/lib/models/metadata/material.entity.js +28 -0
- package/lib/models/metadata/sale-order.entity.d.ts +5 -0
- package/lib/models/metadata/sale-order.entity.js +21 -0
- package/lib/models/metadata/stock.entity.d.ts +8 -0
- package/lib/models/metadata/stock.entity.js +29 -0
- package/lib/models/metadata/unit.entity.d.ts +5 -0
- package/lib/models/metadata/unit.entity.js +21 -0
- package/lib/models/models.module.d.ts +6 -0
- package/lib/models/models.module.js +122 -0
- package/lib/services/allocate-inventory-order.service.d.ts +29 -0
- package/lib/services/allocate-inventory-order.service.js +299 -0
- package/lib/services/base-order.service.d.ts +41 -0
- package/lib/services/batch.service.d.ts +30 -0
- package/lib/services/batch.service.js +218 -0
- package/lib/services/helper.service.d.ts +6 -0
- package/lib/services/helper.service.js +28 -0
- package/lib/services/hold-inventory-order.service.d.ts +29 -0
- package/lib/services/hold-inventory-order.service.js +285 -0
- package/lib/services/index.d.ts +22 -0
- package/lib/services/loader.service.d.ts +43 -0
- package/lib/services/loader.service.js +282 -0
- package/lib/services/location.service.d.ts +13 -0
- package/lib/services/location.service.js +130 -0
- package/lib/services/material.service.d.ts +27 -0
- package/lib/services/material.service.js +172 -0
- package/lib/services/merge-batch-order.service.d.ts +19 -0
- package/lib/services/merge-batch-order.service.js +195 -0
- package/lib/services/quality-inspection-order.service.d.ts +19 -0
- package/lib/services/quality-inspection-order.service.js +135 -0
- package/lib/services/receive-inventory-order.service.d.ts +41 -0
- package/lib/services/receive-inventory-order.service.js +407 -0
- package/lib/services/reclassify-inventory-order.service.d.ts +19 -0
- package/lib/services/reclassify-inventory-order.service.js +181 -0
- package/lib/services/sale-order.service.d.ts +7 -0
- package/lib/services/sale-order.service.js +20 -0
- package/lib/services/scrape.service.d.ts +18 -0
- package/lib/services/scrape.service.js +139 -0
- package/lib/services/ship-inventory-order.service.d.ts +60 -0
- package/lib/services/ship-inventory-order.service.js +599 -0
- package/lib/services/split-batch-order.service.d.ts +19 -0
- package/lib/services/split-batch-order.service.js +161 -0
- package/lib/services/stock.service.d.ts +38 -0
- package/lib/services/stock.service.js +477 -0
- package/lib/services/transfer-customer-order.service.d.ts +35 -0
- package/lib/services/transfer-customer-order.service.js +279 -0
- package/lib/services/transfer-order.service.d.ts +44 -0
- package/lib/services/transfer-order.service.js +632 -0
- package/lib/services/transfer-vendor-order.service.d.ts +35 -0
- package/lib/services/transfer-vendor-order.service.js +279 -0
- package/lib/services/unit.service.d.ts +7 -0
- package/lib/services/unit.service.js +67 -0
- package/lib/services/warehouse-map.service.d.ts +10 -0
- package/lib/services/warehouse-map.service.js +38 -0
- package/lib/typings/aggregated-stock.d.ts +22 -0
- package/lib/typings/cancel-ship-inventory-order.input.d.ts +4 -0
- package/lib/typings/cancel-transfer-order.input.d.ts +4 -0
- package/lib/typings/core-module-options.d.ts +8 -0
- package/lib/typings/core-module-options.js +3 -0
- package/lib/typings/create-allocate-inventory-order.input.d.ts +25 -0
- package/lib/typings/create-hold-inventory-order.input.d.ts +21 -0
- package/lib/typings/create-inspection-order.input.d.ts +14 -0
- package/lib/typings/create-loader-type.input.d.ts +9 -0
- package/lib/typings/create-loader.input.d.ts +15 -0
- package/lib/typings/create-location.input.d.ts +12 -0
- package/lib/typings/create-material-unit.input.d.ts +5 -0
- package/lib/typings/create-material.input.d.ts +11 -0
- package/lib/typings/create-receive-inventory-order.input.d.ts +26 -0
- package/lib/typings/create-ship-inventory-order.input.d.ts +46 -0
- package/lib/typings/create-transfer-customer-order.input.d.ts +25 -0
- package/lib/typings/create-transfer-order.input.d.ts +25 -0
- package/lib/typings/create-transfer-vendor-order.input.d.ts +25 -0
- package/lib/typings/deallocate-inventory-order.d.ts +8 -0
- package/lib/typings/find-allocate-inventory-order.input.d.ts +13 -0
- package/lib/typings/find-batch.input.d.ts +8 -0
- package/lib/typings/find-hold-inventory-order.input.d.ts +13 -0
- package/lib/typings/find-loader.input.d.ts +33 -0
- package/lib/typings/find-location.input.d.ts +10 -0
- package/lib/typings/find-material.input.d.ts +10 -0
- package/lib/typings/find-receive-inventory-order.input.d.ts +26 -0
- package/lib/typings/find-receive-inventory-order.input.js +9 -0
- package/lib/typings/find-ship-inventory-order.input.d.ts +26 -0
- package/lib/typings/find-ship-inventory-order.input.js +9 -0
- package/lib/typings/find-stock.input.d.ts +21 -0
- package/lib/typings/find-transfer-order.input.d.ts +21 -0
- package/lib/typings/get-batch.input.d.ts +14 -0
- package/lib/typings/index.d.ts +18 -0
- package/lib/typings/merge-batch.input.d.ts +11 -0
- package/lib/typings/paginations.d.ts +4 -0
- package/lib/typings/receive-inventory-order.input.d.ts +8 -0
- package/lib/typings/reclassify-inventory-order.input.d.ts +11 -0
- package/lib/typings/release-inventory-order.input.d.ts +8 -0
- package/lib/typings/scrape.input.d.ts +8 -0
- package/lib/typings/ship-inventory-order.input.d.ts +4 -0
- package/lib/typings/split-batch.input.d.ts +10 -0
- package/lib/typings/transfer-order.input.d.ts +4 -0
- package/package.json +52 -0
- package/src/lib/models/extensions/README.md +219 -0
|
@@ -0,0 +1,135 @@
|
|
|
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 '../models/metadata/sale-order.entity.js';
|
|
12
|
+
import { StockEntityClass } from '../models/metadata/stock.entity.js';
|
|
13
|
+
import '../models/metadata/unit.entity.js';
|
|
14
|
+
import '../models/extensions/receive-inventory-order/receive-inventory-order-item.entity.js';
|
|
15
|
+
import '../models/extensions/receive-inventory-order/receive-inventory-order.entity.js';
|
|
16
|
+
import '../models/extensions/receive-inventory-order/receive-inventory-order-change.entity.js';
|
|
17
|
+
import '../models/extensions/ship-inventory-order/ship-inventory-order-item.entity.js';
|
|
18
|
+
import '../models/extensions/ship-inventory-order/ship-inventory-order.entity.js';
|
|
19
|
+
import '../models/extensions/ship-inventory-order/ship-inventory-order-reference.entity.js';
|
|
20
|
+
import '../models/extensions/ship-inventory-order/ship-inventory-order-change.entity.js';
|
|
21
|
+
import '../models/extensions/transfer-order/transfer-order.entity.js';
|
|
22
|
+
import '../models/extensions/transfer-order/transfer-order-item.entity.js';
|
|
23
|
+
import '../models/extensions/transfer-order/transfer-order-change.entity.js';
|
|
24
|
+
import '../models/extensions/hold-inventory-order/hold-inventory-order.entity.js';
|
|
25
|
+
import '../models/extensions/hold-inventory-order/hold-inventory-order-item.entity.js';
|
|
26
|
+
import '../models/extensions/hold-inventory-order/hold-inventory-order-change.entity.js';
|
|
27
|
+
import '../models/extensions/allocate-inventory-order/allocate-inventory-order.entity.js';
|
|
28
|
+
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-item.entity.js';
|
|
29
|
+
import '../models/extensions/allocate-inventory-order/allocate-inventory-order-change.entity.js';
|
|
30
|
+
import '../models/extensions/split-batch-order/split-batch-order.entity.js';
|
|
31
|
+
import '../models/extensions/split-batch-order/split-batch-order-item.entity.js';
|
|
32
|
+
import '../models/extensions/split-batch-order/split-batch-order-change.entity.js';
|
|
33
|
+
import '../models/extensions/merge-batch-order/merge-batch-order.entity.js';
|
|
34
|
+
import '../models/extensions/merge-batch-order/merge-batch-order-item.entity.js';
|
|
35
|
+
import '../models/extensions/merge-batch-order/merge-batch-order-change.entity.js';
|
|
36
|
+
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order.entity.js';
|
|
37
|
+
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-item.entity.js';
|
|
38
|
+
import '../models/extensions/reclassify-inventory-order/reclassify-inventory-order-change.entity.js';
|
|
39
|
+
import '../models/extensions/transfer-vendor-order/transfer-vendor-order.entity.js';
|
|
40
|
+
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-item.entity.js';
|
|
41
|
+
import '../models/extensions/transfer-vendor-order/transfer-vendor-order-change.entity.js';
|
|
42
|
+
import { QualityInspectionOrderEntity } from '../models/extensions/quality-inspection-order/quality-inspection-order.entity.js';
|
|
43
|
+
import { QualityInspectionOrderItemEntity } from '../models/extensions/quality-inspection-order/quality-inspection-order-item.entity.js';
|
|
44
|
+
import { QualityInspectionOrderChangeEntityClass } from '../models/extensions/quality-inspection-order/quality-inspection-order-change.entity.js';
|
|
45
|
+
import '../models/extensions/transfer-customer-order/transfer-customer-order.entity.js';
|
|
46
|
+
import '../models/extensions/transfer-customer-order/transfer-customer-order-item.entity.js';
|
|
47
|
+
import '../models/extensions/transfer-customer-order/transfer-customer-order-change.entity.js';
|
|
48
|
+
import '../models/extensions/scrape-order/scrape-order.entity.js';
|
|
49
|
+
import '../models/extensions/scrape-order/scrape-order-item.entity.js';
|
|
50
|
+
import '../models/extensions/scrape-order/scrape-order-change.entity.js';
|
|
51
|
+
import { DataSource } from 'typeorm';
|
|
52
|
+
import { OrderEntity } from '@rytass/wms-base-nestjs-module';
|
|
53
|
+
import { BatchService } from './batch.service.js';
|
|
54
|
+
import { MaterialService } from './material.service.js';
|
|
55
|
+
import { StockService } from './stock.service.js';
|
|
56
|
+
|
|
57
|
+
let QualityInspectionOrderService = class QualityInspectionOrderService {
|
|
58
|
+
dataSource;
|
|
59
|
+
qualityInspectionOrderEntity;
|
|
60
|
+
qualityInspectionOrderItemEntity;
|
|
61
|
+
batchService;
|
|
62
|
+
materialService;
|
|
63
|
+
stockService;
|
|
64
|
+
constructor(dataSource, qualityInspectionOrderEntity, qualityInspectionOrderItemEntity, batchService, materialService, stockService) {
|
|
65
|
+
this.dataSource = dataSource;
|
|
66
|
+
this.qualityInspectionOrderEntity = qualityInspectionOrderEntity;
|
|
67
|
+
this.qualityInspectionOrderItemEntity = qualityInspectionOrderItemEntity;
|
|
68
|
+
this.batchService = batchService;
|
|
69
|
+
this.materialService = materialService;
|
|
70
|
+
this.stockService = stockService;
|
|
71
|
+
}
|
|
72
|
+
async inspect({ manager = this.dataSource.manager, options, }) {
|
|
73
|
+
const orderRepository = manager.getRepository(OrderEntity);
|
|
74
|
+
const qualityInspectionOrderRepository = manager.getRepository(this.qualityInspectionOrderEntity);
|
|
75
|
+
const qualityInspectionOrderItemRepository = manager.getRepository(this.qualityInspectionOrderItemEntity);
|
|
76
|
+
const qualityInspectionOrderChangeRepository = manager.getRepository(QualityInspectionOrderChangeEntityClass);
|
|
77
|
+
const stockRepository = manager.getRepository(StockEntityClass);
|
|
78
|
+
const order = await qualityInspectionOrderRepository.save(qualityInspectionOrderRepository.create({
|
|
79
|
+
key: options.key,
|
|
80
|
+
note: options.note,
|
|
81
|
+
}));
|
|
82
|
+
await Promise.all(options.items.map(async (data) => {
|
|
83
|
+
if (!data.batchId || !data.materialId)
|
|
84
|
+
return;
|
|
85
|
+
const item = await qualityInspectionOrderItemRepository.save(qualityInspectionOrderItemRepository.create({
|
|
86
|
+
...data,
|
|
87
|
+
orderId: order.id,
|
|
88
|
+
targetInspectedQuantity: data.quantity,
|
|
89
|
+
}));
|
|
90
|
+
// Create base order for the change
|
|
91
|
+
const baseOrder = await orderRepository.save({});
|
|
92
|
+
// Get conversion factor to convert to base unit
|
|
93
|
+
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
94
|
+
await qualityInspectionOrderChangeRepository.save(qualityInspectionOrderChangeRepository.create({
|
|
95
|
+
itemId: item.id,
|
|
96
|
+
quantityChanged: item.targetInspectedQuantity * conversionFactor,
|
|
97
|
+
order: baseOrder,
|
|
98
|
+
}));
|
|
99
|
+
const splittedBatch = await this.batchService.splitBatch({
|
|
100
|
+
manager,
|
|
101
|
+
id: data.batchId,
|
|
102
|
+
inspectionResult: data.inspectionResult,
|
|
103
|
+
});
|
|
104
|
+
await stockRepository.save([
|
|
105
|
+
stockRepository.create({
|
|
106
|
+
orderId: baseOrder.id,
|
|
107
|
+
materialId: data.materialId,
|
|
108
|
+
batchId: splittedBatch.id,
|
|
109
|
+
locationId: this.stockService.resolveLocationId(data.locationId),
|
|
110
|
+
loaderId: this.stockService.resolveLoaderId(data.loaderId),
|
|
111
|
+
quantity: data.quantity * conversionFactor,
|
|
112
|
+
}),
|
|
113
|
+
stockRepository.create({
|
|
114
|
+
orderId: baseOrder.id,
|
|
115
|
+
materialId: data.materialId,
|
|
116
|
+
batchId: data.batchId,
|
|
117
|
+
locationId: this.stockService.resolveLocationId(data.locationId),
|
|
118
|
+
loaderId: this.stockService.resolveLoaderId(data.loaderId),
|
|
119
|
+
quantity: -1 * data.quantity * conversionFactor,
|
|
120
|
+
}),
|
|
121
|
+
]);
|
|
122
|
+
}));
|
|
123
|
+
return order;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
QualityInspectionOrderService = __decorate([
|
|
127
|
+
Injectable(),
|
|
128
|
+
__param(1, Inject(QualityInspectionOrderEntity)),
|
|
129
|
+
__param(2, Inject(QualityInspectionOrderItemEntity)),
|
|
130
|
+
__metadata("design:paramtypes", [DataSource, Object, Object, BatchService,
|
|
131
|
+
MaterialService,
|
|
132
|
+
StockService])
|
|
133
|
+
], QualityInspectionOrderService);
|
|
134
|
+
|
|
135
|
+
export { QualityInspectionOrderService };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ReceiveInventoryOrderEntityClass } from '../models/extensions/receive-inventory-order/receive-inventory-order.entity';
|
|
2
|
+
import { ReceiveInventoryOrderItemEntityClass } from '../models/extensions/receive-inventory-order/receive-inventory-order-item.entity';
|
|
3
|
+
import { DataSource, EntityManager, SelectQueryBuilder, type EntityTarget } from 'typeorm';
|
|
4
|
+
import { CreateReceiveInventoryOrderInput, UpdateReceiveInventoryOrderInput } from '../typings/create-receive-inventory-order.input';
|
|
5
|
+
import { FindReceiveInventoryOrderInput } from '../typings/find-receive-inventory-order.input';
|
|
6
|
+
import { ReceiveInventoryOrderInput } from '../typings/receive-inventory-order.input';
|
|
7
|
+
import { MaterialService } from './material.service';
|
|
8
|
+
import { BatchService } from './batch.service';
|
|
9
|
+
import { HelperService } from './helper.service';
|
|
10
|
+
import { StockService } from './stock.service';
|
|
11
|
+
/**
|
|
12
|
+
* Service for managing receive inventory orders and their items
|
|
13
|
+
* Supports dynamic entity injection for customization
|
|
14
|
+
* T - Receive Inventory Order entity type
|
|
15
|
+
* K - Receive Inventory Order Item entity type
|
|
16
|
+
*/
|
|
17
|
+
export declare class ReceiveInventoryOrderService<T extends ReceiveInventoryOrderEntityClass = ReceiveInventoryOrderEntityClass, K extends ReceiveInventoryOrderItemEntityClass = ReceiveInventoryOrderItemEntityClass> {
|
|
18
|
+
private readonly dataSource;
|
|
19
|
+
private readonly materialService;
|
|
20
|
+
private readonly batchService;
|
|
21
|
+
private readonly helperService;
|
|
22
|
+
private readonly stockService;
|
|
23
|
+
readonly receiveInventoryOrderEntity: EntityTarget<T>;
|
|
24
|
+
readonly receiveInventoryOrderItemEntity: EntityTarget<K>;
|
|
25
|
+
readonly selectQueryBuilder: (alias: string) => SelectQueryBuilder<T>;
|
|
26
|
+
constructor(dataSource: DataSource, materialService: MaterialService, batchService: BatchService, helperService: HelperService, stockService: StockService, receiveInventoryOrderEntity: EntityTarget<T>, receiveInventoryOrderItemEntity: EntityTarget<K>);
|
|
27
|
+
private processReceiveInventoryOrderItems;
|
|
28
|
+
createReceiveInventoryOrder({ manager, ...options }: CreateReceiveInventoryOrderInput & {
|
|
29
|
+
manager?: EntityManager;
|
|
30
|
+
}): Promise<T>;
|
|
31
|
+
/**
|
|
32
|
+
* Update a receive inventory order and its items
|
|
33
|
+
* update existing item when id is provided, otherwise create new item, remove behaviour controlled by removedAt field
|
|
34
|
+
*/
|
|
35
|
+
updateReceiveInventoryOrder({ id, manager, ...options }: UpdateReceiveInventoryOrderInput & {
|
|
36
|
+
manager?: EntityManager;
|
|
37
|
+
}): Promise<T>;
|
|
38
|
+
removeReceiveInventoryOrder(id: string): Promise<boolean>;
|
|
39
|
+
findMany(options: FindReceiveInventoryOrderInput<T>): Promise<[T[], number]>;
|
|
40
|
+
receive(options: ReceiveInventoryOrderInput[] | ReceiveInventoryOrderInput): Promise<boolean>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,407 @@
|
|
|
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 { OrderEntity } from '@rytass/wms-base-nestjs-module';
|
|
7
|
+
import { ReceiveInventoryOrderStatus } from '../typings/find-receive-inventory-order.input.js';
|
|
8
|
+
import '../models/metadata/batch.entity.js';
|
|
9
|
+
import '../models/metadata/batch-group.entity.js';
|
|
10
|
+
import { LoadersEntityClass } from '../models/metadata/loader.entity.js';
|
|
11
|
+
import '../models/metadata/loader-type.entity.js';
|
|
12
|
+
import '../models/metadata/loader-unit.entity.js';
|
|
13
|
+
import { LocationEntityClass } from '../models/metadata/location.entity.js';
|
|
14
|
+
import { MaterialEntityClass } from '../models/metadata/material.entity.js';
|
|
15
|
+
import '../models/metadata/material-unit.entity.js';
|
|
16
|
+
import '../models/metadata/sale-order.entity.js';
|
|
17
|
+
import { StockEntityClass } from '../models/metadata/stock.entity.js';
|
|
18
|
+
import '../models/metadata/unit.entity.js';
|
|
19
|
+
import { ReceiveInventoryOrderChangeEntityClass } from '../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 { MaterialService } from './material.service.js';
|
|
55
|
+
import { BatchService } from './batch.service.js';
|
|
56
|
+
import { HelperService } from './helper.service.js';
|
|
57
|
+
import { StockService } from './stock.service.js';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Service for managing receive inventory orders and their items
|
|
61
|
+
* Supports dynamic entity injection for customization
|
|
62
|
+
* T - Receive Inventory Order entity type
|
|
63
|
+
* K - Receive Inventory Order Item entity type
|
|
64
|
+
*/
|
|
65
|
+
let ReceiveInventoryOrderService = class ReceiveInventoryOrderService {
|
|
66
|
+
dataSource;
|
|
67
|
+
materialService;
|
|
68
|
+
batchService;
|
|
69
|
+
helperService;
|
|
70
|
+
stockService;
|
|
71
|
+
receiveInventoryOrderEntity;
|
|
72
|
+
receiveInventoryOrderItemEntity;
|
|
73
|
+
selectQueryBuilder;
|
|
74
|
+
constructor(dataSource, materialService, batchService, helperService, stockService, receiveInventoryOrderEntity, receiveInventoryOrderItemEntity) {
|
|
75
|
+
this.dataSource = dataSource;
|
|
76
|
+
this.materialService = materialService;
|
|
77
|
+
this.batchService = batchService;
|
|
78
|
+
this.helperService = helperService;
|
|
79
|
+
this.stockService = stockService;
|
|
80
|
+
this.receiveInventoryOrderEntity = receiveInventoryOrderEntity;
|
|
81
|
+
this.receiveInventoryOrderItemEntity = receiveInventoryOrderItemEntity;
|
|
82
|
+
this.selectQueryBuilder = (alias) => {
|
|
83
|
+
return this.dataSource
|
|
84
|
+
.getRepository(this.receiveInventoryOrderEntity)
|
|
85
|
+
.createQueryBuilder(alias)
|
|
86
|
+
.leftJoin((subQuery) => subQuery
|
|
87
|
+
.select(`CASE
|
|
88
|
+
WHEN o."removedAt" IS NOT NULL THEN '${ReceiveInventoryOrderStatus.REMOVED}'
|
|
89
|
+
WHEN COALESCE((
|
|
90
|
+
SELECT SUM(CAST(c."quantityChanged" AS NUMERIC))
|
|
91
|
+
FROM receive_inventory_order_changes c
|
|
92
|
+
JOIN receive_inventory_order_items i ON c."itemId" = i.id
|
|
93
|
+
WHERE i."orderId" = o.id
|
|
94
|
+
), 0) = COALESCE(target_sum.targetsum, 0) AND COALESCE(target_sum.targetsum, 0) > 0 THEN '${ReceiveInventoryOrderStatus.RECEIVED}'
|
|
95
|
+
WHEN COALESCE((
|
|
96
|
+
SELECT SUM(CAST(c."quantityChanged" AS NUMERIC))
|
|
97
|
+
FROM receive_inventory_order_changes c
|
|
98
|
+
JOIN receive_inventory_order_items i ON c."itemId" = i.id
|
|
99
|
+
WHERE i."orderId" = o.id
|
|
100
|
+
), 0) > 0 AND COALESCE((
|
|
101
|
+
SELECT SUM(CAST(c."quantityChanged" AS NUMERIC))
|
|
102
|
+
FROM receive_inventory_order_changes c
|
|
103
|
+
JOIN receive_inventory_order_items i ON c."itemId" = i.id
|
|
104
|
+
WHERE i."orderId" = o.id
|
|
105
|
+
), 0) < COALESCE(target_sum.targetsum, 0) THEN '${ReceiveInventoryOrderStatus.PARTIAL_RECEIVED}'
|
|
106
|
+
ELSE '${ReceiveInventoryOrderStatus.PENDING}'
|
|
107
|
+
END`, 'status')
|
|
108
|
+
.addSelect('o.id', 'orderid')
|
|
109
|
+
.from('receive_inventory_orders', 'o')
|
|
110
|
+
.leftJoin((sumQuery) => sumQuery
|
|
111
|
+
.select('i."orderId"', 'orderid')
|
|
112
|
+
.addSelect('SUM(CAST(i."targetReceivedQuantity" AS NUMERIC))', 'targetsum')
|
|
113
|
+
.from('receive_inventory_order_items', 'i')
|
|
114
|
+
.groupBy('i."orderId"'), 'target_sum', 'target_sum.orderid = o.id'), 'order_status', `${alias}.id = order_status.orderid`)
|
|
115
|
+
.select('orders.id', 'id')
|
|
116
|
+
.addSelect('orders.key', 'key')
|
|
117
|
+
.addSelect('orders.description', 'description')
|
|
118
|
+
.addSelect('orders."createdBy"', 'createdBy')
|
|
119
|
+
.addSelect('orders."createdAt"', 'createdAt')
|
|
120
|
+
.addSelect('orders."removedAt"', 'removedAt')
|
|
121
|
+
.addSelect('order_status.status', `status`);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
processReceiveInventoryOrderItems(items, orderId, manager) {
|
|
125
|
+
const materialRepository = manager.getRepository(MaterialEntityClass);
|
|
126
|
+
const locationRepository = manager.getRepository(LocationEntityClass);
|
|
127
|
+
const loaderRepository = manager.getRepository(LoadersEntityClass);
|
|
128
|
+
return items.reduce(async (accPromise, item) => {
|
|
129
|
+
const acc = await accPromise;
|
|
130
|
+
try {
|
|
131
|
+
const material = await materialRepository.findOneBy({
|
|
132
|
+
key: item.materialKey,
|
|
133
|
+
});
|
|
134
|
+
if (!material)
|
|
135
|
+
return acc;
|
|
136
|
+
const batch = item.batchKey
|
|
137
|
+
? await this.batchService.getBatch({
|
|
138
|
+
manager,
|
|
139
|
+
materialId: material.id,
|
|
140
|
+
key: item.batchKey,
|
|
141
|
+
})
|
|
142
|
+
: null;
|
|
143
|
+
const location = item.locationKey
|
|
144
|
+
? await locationRepository.findOneBy({
|
|
145
|
+
key: item.locationKey,
|
|
146
|
+
})
|
|
147
|
+
: null;
|
|
148
|
+
const loader = item.loaderKey
|
|
149
|
+
? await loaderRepository.findOneBy({
|
|
150
|
+
serialId: item.loaderKey,
|
|
151
|
+
})
|
|
152
|
+
: null;
|
|
153
|
+
return [
|
|
154
|
+
...acc,
|
|
155
|
+
{
|
|
156
|
+
...item,
|
|
157
|
+
orderId,
|
|
158
|
+
materialId: material.id,
|
|
159
|
+
batchId: batch?.id || null,
|
|
160
|
+
locationId: this.stockService.resolveLocationId(location?.id),
|
|
161
|
+
loaderId: loader?.id || null,
|
|
162
|
+
},
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return acc;
|
|
167
|
+
}
|
|
168
|
+
}, Promise.resolve([]));
|
|
169
|
+
}
|
|
170
|
+
async createReceiveInventoryOrder({ manager = this.dataSource.manager, ...options }) {
|
|
171
|
+
const orderRepository = manager.getRepository(OrderEntity);
|
|
172
|
+
const receiveInventoryOrderRepository = manager.getRepository(this.receiveInventoryOrderEntity);
|
|
173
|
+
const receiveInventoryOrderItemRepository = manager.getRepository(this.receiveInventoryOrderItemEntity);
|
|
174
|
+
// create base order
|
|
175
|
+
const { id } = await orderRepository.save(orderRepository.create());
|
|
176
|
+
// create receive inventory order
|
|
177
|
+
const { items, ...orderOptions } = options;
|
|
178
|
+
const order = await receiveInventoryOrderRepository.save(receiveInventoryOrderRepository.create({ id, ...orderOptions }));
|
|
179
|
+
// create receive inventory order items
|
|
180
|
+
if (items && items.length > 0) {
|
|
181
|
+
const processedItems = receiveInventoryOrderItemRepository.create(await this.processReceiveInventoryOrderItems(items, id, manager));
|
|
182
|
+
await receiveInventoryOrderItemRepository.save(processedItems);
|
|
183
|
+
}
|
|
184
|
+
return order;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Update a receive inventory order and its items
|
|
188
|
+
* update existing item when id is provided, otherwise create new item, remove behaviour controlled by removedAt field
|
|
189
|
+
*/
|
|
190
|
+
async updateReceiveInventoryOrder({ id, manager = this.dataSource.manager, ...options }) {
|
|
191
|
+
const receiveInventoryOrderRepository = manager.getRepository(this.receiveInventoryOrderEntity);
|
|
192
|
+
const receiveInventoryOrderItemRepository = manager.getRepository(this.receiveInventoryOrderItemEntity);
|
|
193
|
+
const order = await receiveInventoryOrderRepository.findOneByOrFail({
|
|
194
|
+
id,
|
|
195
|
+
removedAt: IsNull(),
|
|
196
|
+
});
|
|
197
|
+
const { items, ...orderOptions } = options;
|
|
198
|
+
const updated = await receiveInventoryOrderRepository.save(receiveInventoryOrderRepository.create({
|
|
199
|
+
...order,
|
|
200
|
+
...orderOptions,
|
|
201
|
+
}));
|
|
202
|
+
if (items?.length) {
|
|
203
|
+
const newItems = [];
|
|
204
|
+
const existingItems = [];
|
|
205
|
+
await receiveInventoryOrderItemRepository
|
|
206
|
+
.find({ where: { orderId: id } })
|
|
207
|
+
.then((items) => {
|
|
208
|
+
const itemSet = new Set(items.map((i) => i.id));
|
|
209
|
+
options.items?.forEach((item) => {
|
|
210
|
+
if (!item.id)
|
|
211
|
+
newItems.push(item);
|
|
212
|
+
else if (itemSet.has(item.id))
|
|
213
|
+
existingItems.push(item);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
if (newItems.length)
|
|
217
|
+
await receiveInventoryOrderItemRepository.save(receiveInventoryOrderItemRepository.create(await this.processReceiveInventoryOrderItems(newItems, id, manager)));
|
|
218
|
+
if (existingItems.length)
|
|
219
|
+
await receiveInventoryOrderItemRepository.save(existingItems.map((item) => receiveInventoryOrderItemRepository.create({
|
|
220
|
+
...item,
|
|
221
|
+
id: item.id,
|
|
222
|
+
})));
|
|
223
|
+
}
|
|
224
|
+
return updated;
|
|
225
|
+
}
|
|
226
|
+
async removeReceiveInventoryOrder(id) {
|
|
227
|
+
return this.dataSource
|
|
228
|
+
.getRepository(this.receiveInventoryOrderEntity)
|
|
229
|
+
.update({ id }, { removedAt: new Date() })
|
|
230
|
+
.then(({ affected }) => !!affected && affected > 0);
|
|
231
|
+
}
|
|
232
|
+
async findMany(options) {
|
|
233
|
+
const queryBuilder = this.selectQueryBuilder('orders');
|
|
234
|
+
if (options.id)
|
|
235
|
+
queryBuilder.andWhere('orders.id = :id', { id: options.id });
|
|
236
|
+
if (options.createdBy)
|
|
237
|
+
queryBuilder.andWhere('orders."createdBy" ILIKE :createdBy', {
|
|
238
|
+
createdBy: `%${options.createdBy}%`,
|
|
239
|
+
});
|
|
240
|
+
if (options.description)
|
|
241
|
+
queryBuilder.andWhere('orders.description ILIKE :description', {
|
|
242
|
+
description: `%${options.description}%`,
|
|
243
|
+
});
|
|
244
|
+
if (options.dateRange)
|
|
245
|
+
queryBuilder.andWhere('orders."createdAt" BETWEEN :from AND :to', {
|
|
246
|
+
from: options.dateRange.from,
|
|
247
|
+
to: options.dateRange.to,
|
|
248
|
+
});
|
|
249
|
+
if (options.status)
|
|
250
|
+
queryBuilder.andWhere(`status = :status`, {
|
|
251
|
+
status: options.status,
|
|
252
|
+
});
|
|
253
|
+
if (options.statuses && options.statuses.length > 0)
|
|
254
|
+
queryBuilder.andWhere(`status IN (:...statuses)`, {
|
|
255
|
+
statuses: options.statuses,
|
|
256
|
+
});
|
|
257
|
+
if (options.materialKey)
|
|
258
|
+
queryBuilder.andWhere(`EXISTS (
|
|
259
|
+
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
260
|
+
INNER JOIN materials ri_mat ON ri_mat.id = ri_items."materialId"
|
|
261
|
+
WHERE ri_items."orderId" = orders.id
|
|
262
|
+
AND ri_mat.key ILIKE :materialKey
|
|
263
|
+
)`, { materialKey: `%${options.materialKey}%` });
|
|
264
|
+
if (options.batchKey)
|
|
265
|
+
queryBuilder.andWhere(`EXISTS (
|
|
266
|
+
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
267
|
+
INNER JOIN batches ri_batch ON ri_batch.id = ri_items."batchId"
|
|
268
|
+
WHERE ri_items."orderId" = orders.id
|
|
269
|
+
AND ri_batch.key ILIKE :batchKey
|
|
270
|
+
)`, { batchKey: `%${options.batchKey}%` });
|
|
271
|
+
if (options.locationKey)
|
|
272
|
+
queryBuilder.andWhere(`EXISTS (
|
|
273
|
+
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
274
|
+
INNER JOIN locations ri_loc ON ri_loc.id = ri_items."locationId"
|
|
275
|
+
WHERE ri_items."orderId" = orders.id
|
|
276
|
+
AND ri_loc.key ILIKE :locationKey
|
|
277
|
+
)`, { locationKey: `%${options.locationKey}%` });
|
|
278
|
+
if (options.loaderKey)
|
|
279
|
+
queryBuilder.andWhere(`EXISTS (
|
|
280
|
+
SELECT 1 FROM receive_inventory_order_items ri_items
|
|
281
|
+
INNER JOIN loaders ri_ldr ON ri_ldr.id = ri_items."loaderId"
|
|
282
|
+
WHERE ri_items."orderId" = orders.id
|
|
283
|
+
AND ri_ldr."serialId" ILIKE :loaderKey
|
|
284
|
+
)`, { loaderKey: `%${options.loaderKey}%` });
|
|
285
|
+
if (options.wildcard) {
|
|
286
|
+
const { key, value } = options.wildcard;
|
|
287
|
+
queryBuilder.andWhere(`orders."${String(key)}" ILIKE :value`, {
|
|
288
|
+
value: `%${value}%`,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const total = await queryBuilder.getCount();
|
|
292
|
+
if (options.offset !== undefined && options.offset >= 0)
|
|
293
|
+
queryBuilder.skip(options.offset);
|
|
294
|
+
if (options.limit !== undefined && options.limit > 0)
|
|
295
|
+
queryBuilder.take(options.limit);
|
|
296
|
+
const items = await queryBuilder.getRawMany();
|
|
297
|
+
return [items, total];
|
|
298
|
+
}
|
|
299
|
+
receive(options) {
|
|
300
|
+
if (!Array.isArray(options))
|
|
301
|
+
options = [options];
|
|
302
|
+
return this.dataSource.transaction(async (manager) => {
|
|
303
|
+
const changeRepository = manager.getRepository(ReceiveInventoryOrderChangeEntityClass);
|
|
304
|
+
const itemRepository = manager.getRepository(this.receiveInventoryOrderItemEntity);
|
|
305
|
+
const orderRepository = manager.getRepository(OrderEntity);
|
|
306
|
+
const locationRepository = manager.getRepository(LocationEntityClass);
|
|
307
|
+
const loaderRepository = manager.getRepository(LoadersEntityClass);
|
|
308
|
+
const stockRepository = manager.getRepository(StockEntityClass);
|
|
309
|
+
const items = await itemRepository.find({
|
|
310
|
+
where: { id: In(options.map((o) => o.itemId)) },
|
|
311
|
+
relations: ['changes'],
|
|
312
|
+
});
|
|
313
|
+
const itemMap = new Map(items.map((i) => [i.id, i]));
|
|
314
|
+
await Promise.all(options.map(async (option) => {
|
|
315
|
+
const item = itemMap.get(option.itemId);
|
|
316
|
+
if (!item)
|
|
317
|
+
return;
|
|
318
|
+
if (!item.batchId && option.batchKey) {
|
|
319
|
+
// if batchKey is provided but item doesn't have batchId, attempt to find or create batch and update item with batchId
|
|
320
|
+
item.batchId = await this.batchService
|
|
321
|
+
.getBatch({
|
|
322
|
+
manager,
|
|
323
|
+
key: option.batchKey,
|
|
324
|
+
materialId: item.materialId,
|
|
325
|
+
})
|
|
326
|
+
.then(async (b) => {
|
|
327
|
+
await itemRepository.update({ id: item.id }, { batchId: b.id });
|
|
328
|
+
return b.id;
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
const conversionFactor = await this.materialService.getConversionFactor(item.materialId, item.unitId || undefined);
|
|
332
|
+
const totalChanged = item.changes.reduce((sum, change) => sum + Number(change.quantityChanged), 0);
|
|
333
|
+
const remainingQuantity = conversionFactor * item.targetReceivedQuantity - totalChanged;
|
|
334
|
+
if (remainingQuantity - option.quantity * conversionFactor < 0) {
|
|
335
|
+
throw new Error(`Receiving quantity exceeds target for item ${option.itemId}`);
|
|
336
|
+
}
|
|
337
|
+
const newOrder = await orderRepository.save(orderRepository.create());
|
|
338
|
+
let batchId = item.batchId;
|
|
339
|
+
if (option.batchKey)
|
|
340
|
+
batchId = await this.batchService
|
|
341
|
+
.getBatch({
|
|
342
|
+
manager,
|
|
343
|
+
key: option.batchKey,
|
|
344
|
+
materialId: item.materialId,
|
|
345
|
+
})
|
|
346
|
+
.then(({ id }) => id);
|
|
347
|
+
if (!batchId)
|
|
348
|
+
batchId = await this.batchService
|
|
349
|
+
.getBatch({
|
|
350
|
+
manager,
|
|
351
|
+
key: await this.helperService.getSeq('BATCH'),
|
|
352
|
+
materialId: item.materialId,
|
|
353
|
+
})
|
|
354
|
+
.then(({ id }) => id);
|
|
355
|
+
const inspectionResult = item.requireInspection === false
|
|
356
|
+
? true
|
|
357
|
+
: (option.inspectionResult ?? null);
|
|
358
|
+
if (inspectionResult != null)
|
|
359
|
+
batchId = await this.batchService
|
|
360
|
+
.splitBatch({
|
|
361
|
+
manager,
|
|
362
|
+
id: batchId,
|
|
363
|
+
inspectionResult,
|
|
364
|
+
})
|
|
365
|
+
.then(({ id }) => id);
|
|
366
|
+
let locationId = item.locationId;
|
|
367
|
+
if (option.locationKey && !locationId)
|
|
368
|
+
locationId = await locationRepository
|
|
369
|
+
.findOneByOrFail({ key: option.locationKey })
|
|
370
|
+
.then(({ id }) => id);
|
|
371
|
+
let loaderId = item.loaderId;
|
|
372
|
+
if (option.loaderKey && !loaderId)
|
|
373
|
+
loaderId = await loaderRepository
|
|
374
|
+
.findOneByOrFail({ serialId: option.loaderKey })
|
|
375
|
+
.then(({ id }) => id);
|
|
376
|
+
await Promise.all([
|
|
377
|
+
changeRepository.save(changeRepository.create({
|
|
378
|
+
itemId: option.itemId,
|
|
379
|
+
quantityChanged: option.quantity * conversionFactor,
|
|
380
|
+
id: newOrder.id,
|
|
381
|
+
})),
|
|
382
|
+
stockRepository.save(stockRepository.create({
|
|
383
|
+
batchId: batchId || undefined,
|
|
384
|
+
locationId: this.stockService.resolveLocationId(locationId),
|
|
385
|
+
loaderId: this.stockService.resolveLoaderId(loaderId),
|
|
386
|
+
materialId: item.materialId,
|
|
387
|
+
quantity: option.quantity * conversionFactor,
|
|
388
|
+
orderId: newOrder.id,
|
|
389
|
+
})),
|
|
390
|
+
]);
|
|
391
|
+
}));
|
|
392
|
+
return true;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
ReceiveInventoryOrderService = __decorate([
|
|
397
|
+
Injectable(),
|
|
398
|
+
__param(5, Inject(ReceiveInventoryOrderEntity)),
|
|
399
|
+
__param(6, Inject(ReceiveInventoryOrderItemEntity)),
|
|
400
|
+
__metadata("design:paramtypes", [DataSource,
|
|
401
|
+
MaterialService,
|
|
402
|
+
BatchService,
|
|
403
|
+
HelperService,
|
|
404
|
+
StockService, Object, Object])
|
|
405
|
+
], ReceiveInventoryOrderService);
|
|
406
|
+
|
|
407
|
+
export { ReceiveInventoryOrderService };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ReclassifyInventoryOrderEntityClass, ReclassifyInventoryOrderItemEntityClass } from '../models';
|
|
2
|
+
import { DataSource, EntityTarget } from 'typeorm';
|
|
3
|
+
import { ReclassifyInventoryOrderInput } from '../typings/reclassify-inventory-order.input';
|
|
4
|
+
import { MaterialService } from './material.service';
|
|
5
|
+
import { BatchService } from './batch.service';
|
|
6
|
+
import { HelperService } from './helper.service';
|
|
7
|
+
import { StockService } from './stock.service';
|
|
8
|
+
export declare class ReclassifyInventoryOrderService<T extends ReclassifyInventoryOrderEntityClass = ReclassifyInventoryOrderEntityClass, K extends ReclassifyInventoryOrderItemEntityClass = ReclassifyInventoryOrderItemEntityClass> {
|
|
9
|
+
private readonly dataSource;
|
|
10
|
+
private readonly materialService;
|
|
11
|
+
private readonly batchService;
|
|
12
|
+
private readonly helperService;
|
|
13
|
+
private readonly stockService;
|
|
14
|
+
readonly reclassifyInventoryOrderEntity: EntityTarget<T>;
|
|
15
|
+
readonly reclassifyInventoryOrderItemEntity: EntityTarget<K>;
|
|
16
|
+
private readonly seqPrefix;
|
|
17
|
+
constructor(dataSource: DataSource, materialService: MaterialService, batchService: BatchService, helperService: HelperService, stockService: StockService, reclassifyInventoryOrderEntity: EntityTarget<T>, reclassifyInventoryOrderItemEntity: EntityTarget<K>);
|
|
18
|
+
reclassify(options: ReclassifyInventoryOrderInput): Promise<T>;
|
|
19
|
+
}
|