@rytass/wms-module-core 0.5.0 → 0.6.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.
Files changed (49) hide show
  1. package/README.md +43 -1
  2. package/docs/api-reference.md +32 -1
  3. package/index.cjs.js +963 -5
  4. package/index.js +7 -0
  5. package/lib/constants/index.d.ts +2 -0
  6. package/lib/constants/inventory-count-status.d.ts +19 -0
  7. package/lib/constants/inventory-count-status.js +22 -0
  8. package/lib/constants/operations.d.ts +34 -0
  9. package/lib/constants/operations.js +66 -0
  10. package/lib/constants/stock-status.d.ts +2 -0
  11. package/lib/constants/stock-status.js +4 -0
  12. package/lib/core.module.js +9 -1
  13. package/lib/events/index.d.ts +1 -0
  14. package/lib/events/inventory-count-lock.subscriber.d.ts +38 -0
  15. package/lib/events/inventory-count-lock.subscriber.js +98 -0
  16. package/lib/events/stock-change.subscriber.js +6 -0
  17. package/lib/events/wms-event.types.d.ts +2 -2
  18. package/lib/models/extensions/index.d.ts +3 -0
  19. package/lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.d.ts +20 -0
  20. package/lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.js +50 -0
  21. package/lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.d.ts +39 -0
  22. package/lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.js +123 -0
  23. package/lib/models/extensions/inventory-count-order/inventory-count-order.entity.d.ts +50 -0
  24. package/lib/models/extensions/inventory-count-order/inventory-count-order.entity.js +155 -0
  25. package/lib/models/models.module.js +7 -0
  26. package/lib/services/allocate-inventory-order.service.js +3 -0
  27. package/lib/services/batch.service.js +3 -0
  28. package/lib/services/hold-inventory-order.service.js +3 -0
  29. package/lib/services/index.d.ts +1 -0
  30. package/lib/services/inventory-count-order.service.d.ts +143 -0
  31. package/lib/services/inventory-count-order.service.js +521 -0
  32. package/lib/services/loader.service.js +3 -0
  33. package/lib/services/location.service.js +3 -0
  34. package/lib/services/material.service.js +3 -0
  35. package/lib/services/merge-batch-order.service.js +3 -0
  36. package/lib/services/quality-inspection-order.service.js +3 -0
  37. package/lib/services/receive-inventory-order.service.js +3 -0
  38. package/lib/services/reclassify-inventory-order.service.js +3 -0
  39. package/lib/services/scrape.service.js +3 -0
  40. package/lib/services/ship-inventory-order.service.js +3 -0
  41. package/lib/services/split-batch-order.service.js +3 -0
  42. package/lib/services/stock.service.js +3 -0
  43. package/lib/services/transfer-customer-order.service.js +3 -0
  44. package/lib/services/transfer-order.service.js +3 -0
  45. package/lib/services/transfer-vendor-order.service.js +3 -0
  46. package/lib/services/unit.service.js +3 -0
  47. package/lib/services/warehouse-map.service.d.ts +11 -1
  48. package/lib/services/warehouse-map.service.js +13 -4
  49. package/package.json +1 -1
package/index.js CHANGED
@@ -49,6 +49,9 @@ export { TransferCustomerOrderChangeEntity, TransferCustomerOrderChangeEntityCla
49
49
  export { ScrapeOrderEntity, ScrapeOrderEntityClass } from './lib/models/extensions/scrape-order/scrape-order.entity.js';
50
50
  export { ScrapeOrderItemEntity, ScrapeOrderItemEntityClass } from './lib/models/extensions/scrape-order/scrape-order-item.entity.js';
51
51
  export { ScrapeOrderChangeEntity, ScrapeOrderChangeEntityClass } from './lib/models/extensions/scrape-order/scrape-order-change.entity.js';
52
+ export { InventoryCountOrderEntity, InventoryCountOrderEntityClass, InventoryCountOrderLocationEntity, InventoryCountOrderLocationEntityClass } from './lib/models/extensions/inventory-count-order/inventory-count-order.entity.js';
53
+ export { InventoryCountOrderItemEntity, InventoryCountOrderItemEntityClass } from './lib/models/extensions/inventory-count-order/inventory-count-order-item.entity.js';
54
+ export { InventoryCountOrderChangeEntity, InventoryCountOrderChangeEntityClass } from './lib/models/extensions/inventory-count-order/inventory-count-order-change.entity.js';
52
55
  export { StockService } from './lib/services/stock.service.js';
53
56
  export { LoaderService } from './lib/services/loader.service.js';
54
57
  export { MaterialService } from './lib/services/material.service.js';
@@ -67,15 +70,19 @@ export { TransferVendorOrderService } from './lib/services/transfer-vendor-order
67
70
  export { QualityInspectionOrderService } from './lib/services/quality-inspection-order.service.js';
68
71
  export { TransferCustomerOrderService } from './lib/services/transfer-customer-order.service.js';
69
72
  export { SaleOrderService } from './lib/services/sale-order.service.js';
73
+ export { InventoryCountOrderService } from './lib/services/inventory-count-order.service.js';
70
74
  export { ScrapeService } from './lib/services/scrape.service.js';
71
75
  export { WarehouseMapDataService } from './lib/services/warehouse-map.service.js';
72
76
  export { HelperService } from './lib/services/helper.service.js';
73
77
  export { WMS_CORE_MODULE_OPTIONS } from './lib/typings/core-module-options.js';
74
78
  export { ReceiveInventoryOrderStatus } from './lib/typings/find-receive-inventory-order.input.js';
75
79
  export { ShipInventoryOrderStatus } from './lib/typings/find-ship-inventory-order.input.js';
80
+ export { InventoryCountStatus } from './lib/constants/inventory-count-status.js';
81
+ export { WMS_OPERATION_NAMES } from './lib/constants/operations.js';
76
82
  export { ChangeType, StockStatus } from './lib/constants/stock-status.js';
77
83
  export { WMS_ACTOR_EXTRACTOR, WMS_EVENT_EMITTER_BRIDGE, WMS_EVENT_OPTIONS, WMS_STOCK_ENRICH } from './lib/events/wms-event.tokens.js';
78
84
  export { WmsEventCollector } from './lib/events/wms-event.collector.js';
79
85
  export { WmsEventDispatcher } from './lib/events/wms-event.dispatcher.js';
80
86
  export { collectToScope, runInDispatchScope } from './lib/events/dispatch-scope.js';
87
+ export { InventoryCountLockSubscriber } from './lib/events/inventory-count-lock.subscriber.js';
81
88
  export { OrderEntity as OrderEntityClass } from '@rytass/wms-base-nestjs-module';
@@ -1 +1,3 @@
1
+ export * from './inventory-count-status';
2
+ export * from './operations';
1
3
  export * from './stock-status';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The lifecycle of an inventory count order, matching the four states the
3
+ * design shows on the list page.
4
+ *
5
+ * Only `COUNTING` accepts counted quantities, and only `COUNTING` locks the
6
+ * scoped locations; `COMPLETED` is reached exclusively by settling the variance,
7
+ * so an order that was counted but never settled stays `COUNTING` for as long as
8
+ * it takes.
9
+ */
10
+ export declare enum InventoryCountStatus {
11
+ /** 尚未執行 — created, scope fixed, nothing counted, nothing locked. */
12
+ PENDING = "PENDING",
13
+ /** 盤點中 — book quantities snapshotted and the scoped locations locked. */
14
+ COUNTING = "COUNTING",
15
+ /** 已完成 — the variance was settled and stock adjusted. */
16
+ COMPLETED = "COMPLETED",
17
+ /** 已取消 — abandoned from either PENDING or COUNTING; nothing adjusted. */
18
+ CANCELLED = "CANCELLED"
19
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * The lifecycle of an inventory count order, matching the four states the
3
+ * design shows on the list page.
4
+ *
5
+ * Only `COUNTING` accepts counted quantities, and only `COUNTING` locks the
6
+ * scoped locations; `COMPLETED` is reached exclusively by settling the variance,
7
+ * so an order that was counted but never settled stays `COUNTING` for as long as
8
+ * it takes.
9
+ */
10
+ var InventoryCountStatus;
11
+ (function (InventoryCountStatus) {
12
+ /** 尚未執行 — created, scope fixed, nothing counted, nothing locked. */
13
+ InventoryCountStatus["PENDING"] = "PENDING";
14
+ /** 盤點中 — book quantities snapshotted and the scoped locations locked. */
15
+ InventoryCountStatus["COUNTING"] = "COUNTING";
16
+ /** 已完成 — the variance was settled and stock adjusted. */
17
+ InventoryCountStatus["COMPLETED"] = "COMPLETED";
18
+ /** 已取消 — abandoned from either PENDING or COUNTING; nothing adjusted. */
19
+ InventoryCountStatus["CANCELLED"] = "CANCELLED";
20
+ })(InventoryCountStatus || (InventoryCountStatus = {}));
21
+
22
+ export { InventoryCountStatus };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @file operations.ts
3
+ * The canonical list of every root operation this package exposes, named the
4
+ * same way `@WmsOperation` declares it: `` `${resource}:${action}` ``.
5
+ *
6
+ * One name, three uses
7
+ * --------------------
8
+ * 1. The `requirement` a consumer's `authChecker` receives.
9
+ * 2. The key for `enabledOperations` / `disabledOperations` — the deployment
10
+ * switch that decides whether a resolver is registered in the schema at all.
11
+ * 3. The value `@rytass/wms-module-react`'s `INVENTORY_ACTION_OPERATIONS` maps
12
+ * each UI action to, so a host can derive its frontend and backend switches
13
+ * from one constant.
14
+ *
15
+ * These strings are internal metadata — they never appear in the GraphQL schema
16
+ * — so refining one is not a GraphQL breaking change, but it *is* a breaking
17
+ * change for consumers keying off it. Treat the union as public API.
18
+ *
19
+ * **Invariant:** this union must list exactly the requirements declared by
20
+ * `@WmsOperation` across `libs/graphql`, no more and no less.
21
+ * `libs/graphql/src/lib/operation-names.invariant.spec.ts` fails otherwise, so a
22
+ * new resolver that forgets to add its name here cannot merge.
23
+ *
24
+ * Note that a name is **not** unique per schema field: several queries share one
25
+ * requirement (`loader:read` covers 6 fields, `stock:read` 5, `material:read` and
26
+ * `loader:create` 3 each, and 7 more cover 2). Disabling a name disables every
27
+ * field carrying it — use `disabledFields` when you need finer granularity.
28
+ */
29
+ export type WmsOperationName = 'allocate-inventory-order:create' | 'allocate-inventory-order:deallocate' | 'allocate-inventory-order:read' | 'allocate-inventory-order:update' | 'batch:merge' | 'batch:read' | 'batch:split' | 'hold-inventory-order:create' | 'hold-inventory-order:read' | 'hold-inventory-order:release' | 'hold-inventory-order:update' | 'inventory-count-order:add-item' | 'inventory-count-order:cancel' | 'inventory-count-order:count' | 'inventory-count-order:create' | 'inventory-count-order:read' | 'inventory-count-order:remove-item' | 'inventory-count-order:reset' | 'inventory-count-order:settle' | 'inventory-count-order:start' | 'inventory-count-order:update' | 'loader:create' | 'loader:read' | 'loader:update' | 'location:create' | 'location:read' | 'location:update' | 'material:create' | 'material:read' | 'material:update' | 'quality-inspection-order:inspect' | 'receive-inventory-order:create' | 'receive-inventory-order:read' | 'receive-inventory-order:receive' | 'receive-inventory-order:update' | 'reclassify-inventory-order:reclassify' | 'sale-order:read' | 'scrape-order:scrape' | 'ship-inventory-order:cancel' | 'ship-inventory-order:create' | 'ship-inventory-order:read' | 'ship-inventory-order:ship' | 'ship-inventory-order:update' | 'stock:read' | 'transfer-customer-order:create' | 'transfer-customer-order:return' | 'transfer-customer-order:update' | 'transfer-order:cancel' | 'transfer-order:create' | 'transfer-order:read' | 'transfer-order:transfer' | 'transfer-order:update' | 'transfer-vendor-order:create' | 'transfer-vendor-order:return' | 'transfer-vendor-order:update' | 'unit:read' | 'warehouse-map:read' | 'warehouse-map:update';
30
+ /**
31
+ * Runtime counterpart of {@link WmsOperationName}. Iterate it to build a UI, to
32
+ * diff against a host's own allow-list, or to assert the invariant above.
33
+ */
34
+ export declare const WMS_OPERATION_NAMES: readonly WmsOperationName[];
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Runtime counterpart of {@link WmsOperationName}. Iterate it to build a UI, to
3
+ * diff against a host's own allow-list, or to assert the invariant above.
4
+ */
5
+ const WMS_OPERATION_NAMES = [
6
+ 'allocate-inventory-order:create',
7
+ 'allocate-inventory-order:deallocate',
8
+ 'allocate-inventory-order:read',
9
+ 'allocate-inventory-order:update',
10
+ 'batch:merge',
11
+ 'batch:read',
12
+ 'batch:split',
13
+ 'hold-inventory-order:create',
14
+ 'hold-inventory-order:read',
15
+ 'hold-inventory-order:release',
16
+ 'hold-inventory-order:update',
17
+ 'inventory-count-order:add-item',
18
+ 'inventory-count-order:cancel',
19
+ 'inventory-count-order:count',
20
+ 'inventory-count-order:create',
21
+ 'inventory-count-order:read',
22
+ 'inventory-count-order:remove-item',
23
+ 'inventory-count-order:reset',
24
+ 'inventory-count-order:settle',
25
+ 'inventory-count-order:start',
26
+ 'inventory-count-order:update',
27
+ 'loader:create',
28
+ 'loader:read',
29
+ 'loader:update',
30
+ 'location:create',
31
+ 'location:read',
32
+ 'location:update',
33
+ 'material:create',
34
+ 'material:read',
35
+ 'material:update',
36
+ 'quality-inspection-order:inspect',
37
+ 'receive-inventory-order:create',
38
+ 'receive-inventory-order:read',
39
+ 'receive-inventory-order:receive',
40
+ 'receive-inventory-order:update',
41
+ 'reclassify-inventory-order:reclassify',
42
+ 'sale-order:read',
43
+ 'scrape-order:scrape',
44
+ 'ship-inventory-order:cancel',
45
+ 'ship-inventory-order:create',
46
+ 'ship-inventory-order:read',
47
+ 'ship-inventory-order:ship',
48
+ 'ship-inventory-order:update',
49
+ 'stock:read',
50
+ 'transfer-customer-order:create',
51
+ 'transfer-customer-order:return',
52
+ 'transfer-customer-order:update',
53
+ 'transfer-order:cancel',
54
+ 'transfer-order:create',
55
+ 'transfer-order:read',
56
+ 'transfer-order:transfer',
57
+ 'transfer-order:update',
58
+ 'transfer-vendor-order:create',
59
+ 'transfer-vendor-order:return',
60
+ 'transfer-vendor-order:update',
61
+ 'unit:read',
62
+ 'warehouse-map:read',
63
+ 'warehouse-map:update',
64
+ ];
65
+
66
+ export { WMS_OPERATION_NAMES };
@@ -26,5 +26,7 @@ export declare enum ChangeType {
26
26
  ALLOCATED = "ALLOCATED",
27
27
  DEALLOCATED = "DEALLOCATED",
28
28
  RECLASSIFIED = "RECLASSIFIED",
29
+ COUNT_GAINED = "COUNT_GAINED",
30
+ COUNT_LOST = "COUNT_LOST",
29
31
  SCRAPED = "SCRAPED"
30
32
  }
@@ -44,6 +44,10 @@ var ChangeType;
44
44
  ChangeType["DEALLOCATED"] = "DEALLOCATED";
45
45
  // reclassify-inventory-order-change.entity.ts
46
46
  ChangeType["RECLASSIFIED"] = "RECLASSIFIED";
47
+ // inventory-count-order-change.entity.ts, change > 0
48
+ ChangeType["COUNT_GAINED"] = "COUNT_GAINED";
49
+ // inventory-count-order-change.entity.ts, change < 0
50
+ ChangeType["COUNT_LOST"] = "COUNT_LOST";
47
51
  // scrape-order-change.entity.ts
48
52
  ChangeType["SCRAPED"] = "SCRAPED";
49
53
  })(ChangeType || (ChangeType = {}));
@@ -27,10 +27,12 @@ import { TransferVendorOrderService } from './services/transfer-vendor-order.ser
27
27
  import { QualityInspectionOrderService } from './services/quality-inspection-order.service.js';
28
28
  import { TransferCustomerOrderService } from './services/transfer-customer-order.service.js';
29
29
  import { SaleOrderService } from './services/sale-order.service.js';
30
+ import { InventoryCountOrderService } from './services/inventory-count-order.service.js';
30
31
  import { ScrapeService } from './services/scrape.service.js';
31
32
  import { WarehouseMapDataService } from './services/warehouse-map.service.js';
32
33
  import { HelperService } from './services/helper.service.js';
33
34
  import { StockChangeSubscriber } from './events/stock-change.subscriber.js';
35
+ import { InventoryCountLockSubscriber } from './events/inventory-count-lock.subscriber.js';
34
36
  import { WMS_STOCK_ENRICH } from './events/wms-event.tokens.js';
35
37
  import { WMS_CORE_MODULE_OPTIONS } from './typings/core-module-options.js';
36
38
 
@@ -131,6 +133,7 @@ const exportDependencies = [
131
133
  MergeBatchOrderService,
132
134
  ReclassifyInventoryOrderService,
133
135
  ScrapeService,
136
+ InventoryCountOrderService,
134
137
  QualityInspectionOrderService,
135
138
  WarehouseMapDataService,
136
139
  ];
@@ -143,9 +146,13 @@ let WmsModuleCoreModule = WmsModuleCoreModule_1 = class WmsModuleCoreModule {
143
146
  providers: [
144
147
  ...exportDependencies,
145
148
  StockChangeSubscriber,
149
+ InventoryCountLockSubscriber,
146
150
  ...resolvedEntityProviders,
147
151
  { provide: WMS_CORE_MODULE_OPTIONS, useValue: coreOptions },
148
- { provide: WMS_STOCK_ENRICH, useValue: !!coreOptions.enrichStockEvents },
152
+ {
153
+ provide: WMS_STOCK_ENRICH,
154
+ useValue: !!coreOptions.enrichStockEvents,
155
+ },
149
156
  ],
150
157
  exports: [
151
158
  ...exportDependencies,
@@ -165,6 +172,7 @@ let WmsModuleCoreModule = WmsModuleCoreModule_1 = class WmsModuleCoreModule {
165
172
  providers: [
166
173
  ...exportDependencies,
167
174
  StockChangeSubscriber,
175
+ InventoryCountLockSubscriber,
168
176
  ...resolvedEntityProviders,
169
177
  {
170
178
  provide: WMS_CORE_MODULE_OPTIONS,
@@ -3,3 +3,4 @@ export { WmsEventCollector } from './wms-event.collector';
3
3
  export { WmsEventDispatcher } from './wms-event.dispatcher';
4
4
  export { collectToScope, runInDispatchScope, type DispatchScopeResult, } from './dispatch-scope';
5
5
  export type { WmsActor, WmsActorExtractor, WmsAfterEventContext, WmsAfterListener, WmsAfterListenerMap, WmsBeforeHook, WmsBeforeHookContext, WmsBeforeHookMap, WmsDomainEvent, WmsEventEmitterBridge, WmsEventModuleOptions, WmsEventName, WmsEventPayloadMap, WmsOperationEventName, WmsOperationEventPayload, WmsStockEventName, WmsStockEventPayload, } from './wms-event.types';
6
+ export * from './inventory-count-lock.subscriber';
@@ -0,0 +1,38 @@
1
+ import { DataSource, type EntitySubscriberInterface, type InsertEvent } from 'typeorm';
2
+ /**
3
+ * Refuse stock movements in locations a running inventory count has locked.
4
+ *
5
+ * Starting a count snapshots the ledger for its scope. That snapshot is what the
6
+ * variance is measured against, so it has to stay true: if a transfer or a
7
+ * shipment moved stock in the meantime, settlement would post the difference
8
+ * between "what we counted" and "what the books said an hour ago" — silently
9
+ * writing off someone else's legitimate movement.
10
+ *
11
+ * Why a subscriber rather than a check in each service
12
+ * ---------------------------------------------------
13
+ * Every stock movement in this package, without exception, ends in a row on some
14
+ * `*_order_change` table — that is the same property `StockChangeSubscriber`
15
+ * relies on to collect `stock.*` events without touching a single service. Doing
16
+ * the lock check at the same choke point means:
17
+ *
18
+ * - the twelve domain services stay untouched;
19
+ * - any future path that writes a change row is covered automatically;
20
+ * - a consumer calling a service **directly**, bypassing the resolvers, is
21
+ * covered too — which is exactly the case that matters, because a host with
22
+ * its own inbound flow does precisely that.
23
+ *
24
+ * The check costs one indexed lookup per change row. That is deliberate: caching
25
+ * the locked set would open a window right after `start()` in which a movement
26
+ * slips through, and a lock with a window is not a lock.
27
+ */
28
+ export declare class InventoryCountLockSubscriber implements EntitySubscriberInterface {
29
+ constructor(dataSource: DataSource);
30
+ beforeInsert(event: InsertEvent<unknown>): Promise<void>;
31
+ /**
32
+ * The location the change ultimately touches, taken from the change's order
33
+ * item. `beforeInsert` may see the relation either already flattened to
34
+ * `itemId` or still as the item object, depending on how the caller built the
35
+ * row, so both are accepted.
36
+ */
37
+ private resolveLocationId;
38
+ }
@@ -0,0 +1,98 @@
1
+ import { __decorate, __param, __metadata } from 'tslib';
2
+ import { Injectable, ConflictException } from '@nestjs/common';
3
+ import { InjectDataSource } from '@nestjs/typeorm';
4
+ import { DataSource } from 'typeorm';
5
+
6
+ /** The count's own settlement writes here; it must never lock itself out. */
7
+ const INVENTORY_COUNT_CHANGE_TABLE = 'inventory_count_order_changes';
8
+ /**
9
+ * Refuse stock movements in locations a running inventory count has locked.
10
+ *
11
+ * Starting a count snapshots the ledger for its scope. That snapshot is what the
12
+ * variance is measured against, so it has to stay true: if a transfer or a
13
+ * shipment moved stock in the meantime, settlement would post the difference
14
+ * between "what we counted" and "what the books said an hour ago" — silently
15
+ * writing off someone else's legitimate movement.
16
+ *
17
+ * Why a subscriber rather than a check in each service
18
+ * ---------------------------------------------------
19
+ * Every stock movement in this package, without exception, ends in a row on some
20
+ * `*_order_change` table — that is the same property `StockChangeSubscriber`
21
+ * relies on to collect `stock.*` events without touching a single service. Doing
22
+ * the lock check at the same choke point means:
23
+ *
24
+ * - the twelve domain services stay untouched;
25
+ * - any future path that writes a change row is covered automatically;
26
+ * - a consumer calling a service **directly**, bypassing the resolvers, is
27
+ * covered too — which is exactly the case that matters, because a host with
28
+ * its own inbound flow does precisely that.
29
+ *
30
+ * The check costs one indexed lookup per change row. That is deliberate: caching
31
+ * the locked set would open a window right after `start()` in which a movement
32
+ * slips through, and a lock with a window is not a lock.
33
+ */
34
+ let InventoryCountLockSubscriber = class InventoryCountLockSubscriber {
35
+ constructor(dataSource) {
36
+ dataSource.subscribers.push(this);
37
+ }
38
+ async beforeInsert(event) {
39
+ const tableName = event.metadata.tableName;
40
+ if (tableName === INVENTORY_COUNT_CHANGE_TABLE ||
41
+ !tableName.endsWith('_order_changes')) {
42
+ return;
43
+ }
44
+ const locationId = await this.resolveLocationId(event);
45
+ if (!locationId) {
46
+ // split / merge / reclassify items carry no location. They still move
47
+ // stock, but only within a position the caller already named elsewhere;
48
+ // there is nothing to match against the scope here.
49
+ return;
50
+ }
51
+ const locks = await event.manager.query(`SELECT o."key"
52
+ FROM inventory_count_order_locations l
53
+ JOIN inventory_count_orders o ON o.id = l."orderId"
54
+ WHERE l."locationId" = $1
55
+ AND o.status = 'COUNTING'
56
+ LIMIT 1`, [locationId]);
57
+ if (!locks.length) {
58
+ return;
59
+ }
60
+ // Naming the order is the difference between an operator who can go and
61
+ // settle the count and one who just sees "denied".
62
+ throw new ConflictException(`Location ${locationId} is locked by inventory count order ${locks[0].key}; finish or cancel that count before moving this stock.`);
63
+ }
64
+ /**
65
+ * The location the change ultimately touches, taken from the change's order
66
+ * item. `beforeInsert` may see the relation either already flattened to
67
+ * `itemId` or still as the item object, depending on how the caller built the
68
+ * row, so both are accepted.
69
+ */
70
+ async resolveLocationId(event) {
71
+ const entity = event.entity;
72
+ if (!entity) {
73
+ return null;
74
+ }
75
+ if (entity.item && entity.item.locationId !== undefined) {
76
+ return entity.item.locationId;
77
+ }
78
+ const itemId = entity.itemId ?? entity.item?.id;
79
+ if (!itemId) {
80
+ return null;
81
+ }
82
+ const itemRelation = event.metadata.relations.find((relation) => relation.propertyName === 'item');
83
+ if (!itemRelation) {
84
+ return null;
85
+ }
86
+ const item = await event.manager
87
+ .getRepository(itemRelation.inverseEntityMetadata.target)
88
+ .findOne({ where: { id: itemId } });
89
+ return item?.locationId ?? null;
90
+ }
91
+ };
92
+ InventoryCountLockSubscriber = __decorate([
93
+ Injectable(),
94
+ __param(0, InjectDataSource()),
95
+ __metadata("design:paramtypes", [DataSource])
96
+ ], InventoryCountLockSubscriber);
97
+
98
+ export { InventoryCountLockSubscriber };
@@ -41,6 +41,10 @@ const TABLE_TO_CHANGE_TYPE = new Map([
41
41
  ['merge_batch_order_changes', () => ChangeType.MERGED],
42
42
  ['reclassify_inventory_order_changes', () => ChangeType.RECLASSIFIED],
43
43
  ['scrape_order_changes', () => ChangeType.SCRAPED],
44
+ [
45
+ 'inventory_count_order_changes',
46
+ (q) => (q > 0 ? ChangeType.COUNT_GAINED : ChangeType.COUNT_LOST),
47
+ ],
44
48
  ]);
45
49
  const CHANGE_TYPE_TO_EVENT = {
46
50
  [ChangeType.RECEIVED]: 'stock.received',
@@ -60,6 +64,8 @@ const CHANGE_TYPE_TO_EVENT = {
60
64
  [ChangeType.DEALLOCATED]: 'stock.deallocated',
61
65
  [ChangeType.RECLASSIFIED]: 'stock.reclassified',
62
66
  [ChangeType.SCRAPED]: 'stock.scraped',
67
+ [ChangeType.COUNT_GAINED]: 'stock.count-gained',
68
+ [ChangeType.COUNT_LOST]: 'stock.count-lost',
63
69
  };
64
70
  const QUALITY_INSPECTION_TABLE = 'quality_inspection_order_changes';
65
71
  /**
@@ -15,8 +15,8 @@ import type { ChangeType } from '../constants/stock-status';
15
15
  * names and payloads. Wiring is opt-in via `forRootAsync({ beforeHooks,
16
16
  * afterListeners })`; when nothing is configured the whole chain is inert.
17
17
  */
18
- export type WmsOperationEventName = 'receive-inventory-order.created' | 'receive-inventory-order.updated' | 'receive-inventory-order.received' | 'ship-inventory-order.created' | 'ship-inventory-order.updated' | 'ship-inventory-order.shipped' | 'ship-inventory-order.ship-cancelled' | 'transfer-order.created' | 'transfer-order.updated' | 'transfer-order.transferred' | 'transfer-order.transfer-cancelled' | 'hold-inventory-order.created' | 'hold-inventory-order.updated' | 'hold-inventory-order.released' | 'allocate-inventory-order.created' | 'allocate-inventory-order.updated' | 'allocate-inventory-order.deallocated' | 'transfer-vendor-order.created' | 'transfer-vendor-order.updated' | 'transfer-vendor-order.returned' | 'transfer-customer-order.created' | 'transfer-customer-order.updated' | 'transfer-customer-order.returned' | 'quality-inspection-order.inspected' | 'scrape-order.scraped' | 'reclassify-inventory-order.reclassified' | 'batch.merged' | 'batch.splitted' | 'material.created' | 'material.updated' | 'loader.created' | 'loader.batch-created' | 'loader.updated' | 'loader.type-created' | 'loader.type-updated' | 'location.created' | 'location.updated' | 'warehouse-map.updated';
19
- export type WmsStockEventName = 'stock.received' | 'stock.in-shipment' | 'stock.shipped' | 'stock.in-transfer' | 'stock.transferred' | 'stock.vendor-transferred' | 'stock.vendor-returned' | 'stock.held' | 'stock.released' | 'stock.customer-held' | 'stock.customer-returned' | 'stock.splitted' | 'stock.merged' | 'stock.allocated' | 'stock.deallocated' | 'stock.reclassified' | 'stock.scraped' | 'stock.inspection-resolved';
18
+ export type WmsOperationEventName = 'receive-inventory-order.created' | 'receive-inventory-order.updated' | 'receive-inventory-order.received' | 'ship-inventory-order.created' | 'ship-inventory-order.updated' | 'ship-inventory-order.shipped' | 'ship-inventory-order.ship-cancelled' | 'transfer-order.created' | 'transfer-order.updated' | 'transfer-order.transferred' | 'transfer-order.transfer-cancelled' | 'hold-inventory-order.created' | 'hold-inventory-order.updated' | 'hold-inventory-order.released' | 'allocate-inventory-order.created' | 'allocate-inventory-order.updated' | 'allocate-inventory-order.deallocated' | 'transfer-vendor-order.created' | 'transfer-vendor-order.updated' | 'transfer-vendor-order.returned' | 'transfer-customer-order.created' | 'transfer-customer-order.updated' | 'transfer-customer-order.returned' | 'quality-inspection-order.inspected' | 'scrape-order.scraped' | 'reclassify-inventory-order.reclassified' | 'batch.merged' | 'batch.splitted' | 'material.created' | 'material.updated' | 'loader.created' | 'loader.batch-created' | 'loader.updated' | 'loader.type-created' | 'loader.type-updated' | 'location.created' | 'location.updated' | 'warehouse-map.updated' | 'inventory-count-order.created' | 'inventory-count-order.updated' | 'inventory-count-order.started' | 'inventory-count-order.counted' | 'inventory-count-order.reset' | 'inventory-count-order.cancelled' | 'inventory-count-order.settled';
19
+ export type WmsStockEventName = 'stock.received' | 'stock.in-shipment' | 'stock.shipped' | 'stock.in-transfer' | 'stock.transferred' | 'stock.vendor-transferred' | 'stock.vendor-returned' | 'stock.held' | 'stock.released' | 'stock.customer-held' | 'stock.customer-returned' | 'stock.splitted' | 'stock.merged' | 'stock.allocated' | 'stock.deallocated' | 'stock.reclassified' | 'stock.scraped' | 'stock.inspection-resolved' | 'stock.count-gained' | 'stock.count-lost';
20
20
  export type WmsEventName = WmsOperationEventName | WmsStockEventName;
21
21
  /**
22
22
  * The authenticated identity that performed a mutation, in a host-neutral shape.
@@ -35,3 +35,6 @@ export * from './transfer-customer-order/transfer-customer-order-change.entity';
35
35
  export * from './scrape-order/scrape-order.entity';
36
36
  export * from './scrape-order/scrape-order-item.entity';
37
37
  export * from './scrape-order/scrape-order-change.entity';
38
+ export * from './inventory-count-order/inventory-count-order.entity';
39
+ export * from './inventory-count-order/inventory-count-order-item.entity';
40
+ export * from './inventory-count-order/inventory-count-order-change.entity';
@@ -0,0 +1,20 @@
1
+ import { OrderEntity } from '@rytass/wms-base-nestjs-module';
2
+ import { InventoryCountOrderItemEntityClass } from './inventory-count-order-item.entity';
3
+ export declare const InventoryCountOrderChangeEntity: unique symbol;
4
+ /**
5
+ * The stock movement a settled count line produced.
6
+ *
7
+ * `quantityChanged` carries the sign, exactly like every other `*_order_change`
8
+ * table: positive is a surplus (盤盈), negative a shortage (盤虧).
9
+ * `StockChangeSubscriber` reads the sign to pick between `stock.count-gained`
10
+ * and `stock.count-lost`, so nothing here has to emit events itself.
11
+ *
12
+ * Lines whose count matched the books produce no row at all.
13
+ */
14
+ export declare class InventoryCountOrderChangeEntityClass {
15
+ id: string;
16
+ itemId: string;
17
+ quantityChanged: number;
18
+ order: OrderEntity;
19
+ item: InventoryCountOrderItemEntityClass;
20
+ }
@@ -0,0 +1,50 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { OrderEntity } from '@rytass/wms-base-nestjs-module';
3
+ import { PrimaryGeneratedColumn, Column, OneToOne, JoinColumn, ManyToOne, Entity } from 'typeorm';
4
+ import { InventoryCountOrderItemEntityClass } from './inventory-count-order-item.entity.js';
5
+
6
+ const InventoryCountOrderChangeEntity = Symbol('InventoryCountOrderChangeEntity');
7
+ /**
8
+ * The stock movement a settled count line produced.
9
+ *
10
+ * `quantityChanged` carries the sign, exactly like every other `*_order_change`
11
+ * table: positive is a surplus (盤盈), negative a shortage (盤虧).
12
+ * `StockChangeSubscriber` reads the sign to pick between `stock.count-gained`
13
+ * and `stock.count-lost`, so nothing here has to emit events itself.
14
+ *
15
+ * Lines whose count matched the books produce no row at all.
16
+ */
17
+ let InventoryCountOrderChangeEntityClass = class InventoryCountOrderChangeEntityClass {
18
+ id;
19
+ itemId;
20
+ quantityChanged;
21
+ order;
22
+ item;
23
+ };
24
+ __decorate([
25
+ PrimaryGeneratedColumn('uuid'),
26
+ __metadata("design:type", String)
27
+ ], InventoryCountOrderChangeEntityClass.prototype, "id", void 0);
28
+ __decorate([
29
+ Column({ type: 'uuid' }),
30
+ __metadata("design:type", String)
31
+ ], InventoryCountOrderChangeEntityClass.prototype, "itemId", void 0);
32
+ __decorate([
33
+ Column({ type: 'numeric' }),
34
+ __metadata("design:type", Number)
35
+ ], InventoryCountOrderChangeEntityClass.prototype, "quantityChanged", void 0);
36
+ __decorate([
37
+ OneToOne('orders'),
38
+ JoinColumn({ name: 'id', referencedColumnName: 'id' }),
39
+ __metadata("design:type", OrderEntity)
40
+ ], InventoryCountOrderChangeEntityClass.prototype, "order", void 0);
41
+ __decorate([
42
+ ManyToOne('inventory_count_order_items', (item) => item.changes),
43
+ JoinColumn({ name: 'itemId', referencedColumnName: 'id' }),
44
+ __metadata("design:type", InventoryCountOrderItemEntityClass)
45
+ ], InventoryCountOrderChangeEntityClass.prototype, "item", void 0);
46
+ InventoryCountOrderChangeEntityClass = __decorate([
47
+ Entity('inventory_count_order_changes')
48
+ ], InventoryCountOrderChangeEntityClass);
49
+
50
+ export { InventoryCountOrderChangeEntity, InventoryCountOrderChangeEntityClass };
@@ -0,0 +1,39 @@
1
+ import { LoadersEntityClass, LocationEntityClass } from '../../metadata';
2
+ import { InventoryCountOrderEntityClass } from './inventory-count-order.entity';
3
+ import { InventoryCountOrderChangeEntityClass } from './inventory-count-order-change.entity';
4
+ export declare const InventoryCountOrderItemEntity: unique symbol;
5
+ /**
6
+ * One line of the count sheet.
7
+ *
8
+ * Two flavours share the table, told apart by {@link isAddition}:
9
+ *
10
+ * - a **scoped line**, expanded from the order's locations when the order is
11
+ * created. `bookQuantity` is the snapshot taken at "開始盤點".
12
+ * - an **addition** (盤盈), added by hand during counting for stock the books do
13
+ * not know about at all. `bookQuantity` is `0` by definition, and it is the
14
+ * only flavour a user may delete.
15
+ */
16
+ export declare class InventoryCountOrderItemEntityClass {
17
+ id: string;
18
+ orderId: string;
19
+ materialId: string;
20
+ batchId: string;
21
+ locationId: string | null;
22
+ loaderId: string | null;
23
+ /**
24
+ * The unit the counted quantity is expressed in; `null` means the material's
25
+ * base unit. Settlement converts through the material's conversion factor
26
+ * before comparing with {@link bookQuantity}, which is always in base units.
27
+ */
28
+ unitId: string | null;
29
+ /** Ledger quantity in base units, frozen when counting started. */
30
+ bookQuantity: number;
31
+ /** What was actually found, in {@link unitId}. `null` until someone counts. */
32
+ countedQuantity: number | null;
33
+ /** `true` for a line added during counting for unbooked stock (盤盈). */
34
+ isAddition: boolean;
35
+ location: LocationEntityClass;
36
+ loader: LoadersEntityClass;
37
+ order: InventoryCountOrderEntityClass;
38
+ changes: InventoryCountOrderChangeEntityClass[];
39
+ }