@things-factory/sales-base 4.0.12 → 4.0.17

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 (40) hide show
  1. package/dist-server/service/arrival-notice/arrival-notice-query.js +0 -1
  2. package/dist-server/service/arrival-notice/arrival-notice-query.js.map +1 -1
  3. package/dist-server/service/manifest/manifest-mutation.js +63 -72
  4. package/dist-server/service/manifest/manifest-mutation.js.map +1 -1
  5. package/dist-server/service/manifest/manifest-query.js +49 -8
  6. package/dist-server/service/manifest/manifest-query.js.map +1 -1
  7. package/dist-server/service/manifest/manifest-type.js +67 -27
  8. package/dist-server/service/manifest/manifest-type.js.map +1 -1
  9. package/dist-server/service/manifest/manifest.js +5 -0
  10. package/dist-server/service/manifest/manifest.js.map +1 -1
  11. package/dist-server/service/others/other-types.js +4 -0
  12. package/dist-server/service/others/other-types.js.map +1 -1
  13. package/dist-server/service/release-good/release-good-mutation.js +360 -1
  14. package/dist-server/service/release-good/release-good-mutation.js.map +1 -1
  15. package/dist-server/service/release-good/release-good-query.js +160 -0
  16. package/dist-server/service/release-good/release-good-query.js.map +1 -1
  17. package/dist-server/service/release-good/release-good-types.js +34 -2
  18. package/dist-server/service/release-good/release-good-types.js.map +1 -1
  19. package/dist-server/service/reverse-kitting-order/reverse-kitting-order-mutation.js +66 -19
  20. package/dist-server/service/reverse-kitting-order/reverse-kitting-order-mutation.js.map +1 -1
  21. package/dist-server/service/reverse-kitting-order/reverse-kitting-order-query.js +84 -72
  22. package/dist-server/service/reverse-kitting-order/reverse-kitting-order-query.js.map +1 -1
  23. package/dist-server/service/reverse-kitting-order/reverse-kitting-order-type.js +144 -1
  24. package/dist-server/service/reverse-kitting-order/reverse-kitting-order-type.js.map +1 -1
  25. package/dist-server/service/reverse-kitting-order-inventory/reverse-kitting-order-inventory-mutation.js +17 -7
  26. package/dist-server/service/reverse-kitting-order-inventory/reverse-kitting-order-inventory-mutation.js.map +1 -1
  27. package/package.json +12 -12
  28. package/server/service/arrival-notice/arrival-notice-query.ts +0 -2
  29. package/server/service/manifest/manifest-mutation.ts +78 -88
  30. package/server/service/manifest/manifest-query.ts +64 -8
  31. package/server/service/manifest/manifest-type.ts +47 -20
  32. package/server/service/manifest/manifest.ts +5 -0
  33. package/server/service/others/other-types.ts +3 -0
  34. package/server/service/release-good/release-good-mutation.ts +512 -0
  35. package/server/service/release-good/release-good-query.ts +197 -2
  36. package/server/service/release-good/release-good-types.ts +31 -7
  37. package/server/service/reverse-kitting-order/reverse-kitting-order-mutation.ts +82 -21
  38. package/server/service/reverse-kitting-order/reverse-kitting-order-query.ts +95 -79
  39. package/server/service/reverse-kitting-order/reverse-kitting-order-type.ts +106 -1
  40. package/server/service/reverse-kitting-order-inventory/reverse-kitting-order-inventory-mutation.ts +31 -14
@@ -4,12 +4,19 @@ import { EntityManager, getRepository, In, Repository, SelectQueryBuilder } from
4
4
 
5
5
  import { Attachment } from '@things-factory/attachment-base'
6
6
  import { User } from '@things-factory/auth-base'
7
- import { Bizplace, getMyBizplace, getPermittedBizplaceIds } from '@things-factory/biz-base'
7
+ import { Bizplace, getCompanyBizplace, getMyBizplace, getPermittedBizplaceIds } from '@things-factory/biz-base'
8
8
  import { Product } from '@things-factory/product-base'
9
9
  import { buildQuery, Domain, Filter, ListParam, Pagination, Sorting } from '@things-factory/shell'
10
10
  import { Inventory } from '@things-factory/warehouse-base'
11
11
 
12
- import { InventoryInfos, OrderInventory, ReleasableInventoryList, ReleaseGoodList, ShippingOrder } from '../'
12
+ import {
13
+ InventoryInfos,
14
+ NewReleaseGood,
15
+ OrderInventory,
16
+ ReleasableInventoryList,
17
+ ReleaseGoodList,
18
+ ShippingOrder
19
+ } from '../'
13
20
  import { ATTACHMENT_TYPE, ORDER_INVENTORY_STATUS, ORDER_STATUS } from '../../constants'
14
21
  import { ReleaseGood } from './release-good'
15
22
 
@@ -463,6 +470,129 @@ export class ReleaseGoodQuery {
463
470
  }
464
471
  }
465
472
 
473
+ @Directive('@transaction')
474
+ @Query(returns => [ReleaseGood])
475
+ async bulkReleaseGoodsAvailableItems(
476
+ @Ctx() context: any,
477
+ @Arg('rawReleaseGoods', type => [NewReleaseGood], { nullable: true }) rawReleaseGoods: NewReleaseGood[],
478
+ @Arg('bizplaceId', type => String) bizplaceId: string
479
+ ): Promise<ReleaseGood[]> {
480
+ const { domain, tx } = context.state
481
+ const companyBizplaceId: Bizplace = await getCompanyBizplace(null, null, bizplaceId)
482
+
483
+ if (!rawReleaseGoods) return
484
+
485
+ const json_oi = JSON.stringify(
486
+ rawReleaseGoods.map(raw => {
487
+ return {
488
+ sku: raw.sku,
489
+ packing_type: raw.packingType,
490
+ packing_size: raw.packingSize,
491
+ uom: raw.uom,
492
+ release_qty: raw.releaseQty
493
+ }
494
+ })
495
+ )
496
+
497
+ await tx.query(
498
+ `
499
+ CREATE TEMP TABLE temp_order_products(
500
+ product_id VARCHAR(50),
501
+ sku VARCHAR(150),
502
+ packing_type VARCHAR(50),
503
+ packing_size INT,
504
+ uom VARCHAR(10),
505
+ release_qty INT
506
+ );
507
+ `
508
+ )
509
+
510
+ await tx.query(
511
+ `
512
+ INSERT INTO temp_order_products
513
+ SELECT p.id AS product_id, js.sku,
514
+ CASE WHEN js.packing_type NOTNULL AND js.packing_size NOTNULL THEN js.packing_type ELSE pd.packing_type END,
515
+ CASE WHEN js.packing_type NOTNULL AND js.packing_size NOTNULL THEN js.packing_size ELSE pd.packing_size END,
516
+ CASE WHEN js.uom NOTNULL THEN js.uom ELSE pd.uom END,
517
+ js.release_qty
518
+ FROM JSON_POPULATE_RECORDSET(NULL::temp_order_products,'${json_oi}') js
519
+ LEFT JOIN products p ON js.sku = p.sku AND p.bizplace_id = $1
520
+ LEFT JOIN product_details pd ON p.id = pd.product_id AND pd.is_default = TRUE;
521
+ `,
522
+ [companyBizplaceId.id]
523
+ )
524
+
525
+ let availableItems = await tx.query(
526
+ `
527
+ SELECT i.product_id, foo.sku, i.batch_id, i.packing_type, i.packing_size, i.uom,
528
+ SUM(i.qty - COALESCE(i.locked_qty, 0)) - COALESCE(
529
+ (
530
+ SELECT SUM(oi.release_qty) FROM order_inventories oi
531
+ WHERE (
532
+ oi.status = 'PENDING'
533
+ OR oi.status = 'PENDING_RECEIVE'
534
+ OR oi.status = 'PENDING_WORKSHEET'
535
+ OR oi.status = 'PENDING_SPLIT'
536
+ )
537
+ AND oi.inventory_id IS null
538
+ AND oi.product_id = i.product_id::uuid
539
+ AND oi.packing_type = i.packing_type
540
+ AND oi.uom = i.uom
541
+ AND oi.domain_id = $1
542
+ AND oi.bizplace_id = $2
543
+ ), 0) as "remain_qty",
544
+ SUM(i.uom_value - COALESCE(i.locked_uom_value, 0)) - COALESCE(
545
+ (
546
+ SELECT SUM(oi.release_uom_value) FROM order_inventories oi
547
+ WHERE (
548
+ oi.status = 'PENDING'
549
+ OR oi.status = 'PENDING_RECEIVE'
550
+ OR oi.status = 'PENDING_WORKSHEET'
551
+ OR oi.status = 'PENDING_SPLIT'
552
+ )
553
+ AND oi.inventory_id IS null
554
+ AND oi.product_id = i.product_id::uuid
555
+ AND oi.packing_type = i.packing_type
556
+ AND oi.uom = i.uom
557
+ AND oi.domain_id = $1
558
+ AND oi.bizplace_id = $2
559
+ ), 0) as "remain_uom_value"
560
+ FROM inventories i
561
+ INNER JOIN (
562
+ SELECT top.product_id, top.sku, top.packing_type, top.packing_size, top.uom
563
+ FROM temp_order_products top
564
+ GROUP BY top.product_id, top.sku, top.packing_type, top.packing_size, top.uom
565
+ ) AS foo
566
+ ON i.product_id = foo.product_id::uuid
567
+ AND i.packing_type = foo.packing_type
568
+ AND i.packing_size = foo.packing_size
569
+ AND i.uom = foo.uom
570
+ AND i.domain_id = $1
571
+ AND i.bizplace_id = $2
572
+ GROUP BY i.product_id, foo.sku, i.batch_id, i.packing_type, i.packing_size, i.uom
573
+ ORDER BY foo.sku, remain_qty
574
+ `,
575
+ [domain.id, bizplaceId]
576
+ )
577
+
578
+ await tx.query('DROP TABLE temp_order_products')
579
+
580
+ availableItems = availableItems.map(item => {
581
+ return {
582
+ productId: item.product_id,
583
+ sku: item.sku,
584
+ batchId: item.batch_id,
585
+ packingType: item.packing_type,
586
+ packingSize: item.packing_size,
587
+ uom: item.uom,
588
+ remainQty: item.remain_qty,
589
+ remainUomValue: item.remain_uom_value
590
+ }
591
+ })
592
+
593
+ return _extractData(rawReleaseGoods, availableItems)
594
+ }
595
+
466
596
  @FieldResolver(type => Domain)
467
597
  async domain(@Root() releaseGood: ReleaseGood): Promise<Domain> {
468
598
  return await getRepository(Domain).findOne(releaseGood.domainId)
@@ -559,6 +689,71 @@ async function dropOITempTable(domain: Domain, tx: EntityManager) {
559
689
  await tx.query(`DROP TABLE oi`)
560
690
  }
561
691
 
692
+ function _extractData(rawData, validatedData) {
693
+ return rawData.map(raw => {
694
+ const idx = validatedData.findIndex(val => {
695
+ const comparison = ['packingType', 'packingSize', 'uom']
696
+
697
+ let a: any = {},
698
+ b: any = {}
699
+
700
+ comparison.forEach(cc => {
701
+ if (raw[cc]) {
702
+ a[cc] = raw[cc]
703
+ b[cc] = val[cc]
704
+ }
705
+ })
706
+
707
+ a = JSON.stringify(Object.fromEntries(Object.entries(a).sort()))
708
+ b = JSON.stringify(Object.fromEntries(Object.entries(b).sort()))
709
+
710
+ return val.sku == raw.sku && a === b
711
+ })
712
+
713
+ let releaseUomValue = 0
714
+
715
+ // if sku is matched, assign qty and product id
716
+ if (idx >= 0) {
717
+ // assign qty to rawData as much as possible
718
+ releaseUomValue =
719
+ (Math.round((validatedData[idx]?.remainUomValue / validatedData[idx]?.remainQty) * 100) / 100) * raw.releaseQty
720
+
721
+ raw.assignedQty =
722
+ validatedData[idx].remainQty >= raw.releaseQty
723
+ ? raw.releaseQty > 0
724
+ ? raw.releaseQty
725
+ : 0
726
+ : validatedData[idx].remainQty
727
+
728
+ raw.assignedUomValue =
729
+ validatedData[idx].remainUomValue >= releaseUomValue
730
+ ? releaseUomValue > 0
731
+ ? releaseUomValue
732
+ : 0
733
+ : validatedData[idx].remainUomValue
734
+
735
+ // deduct qty & uomValue from validateData
736
+ validatedData[idx].remainQty -= raw.assignedQty
737
+ validatedData[idx].remainUomValue -= raw.assignedUomValue
738
+
739
+ raw.productId = validatedData[idx].productId
740
+ raw.packingType = validatedData[idx].packingType
741
+ raw.packingSize = validatedData[idx].packingSize
742
+ raw.uom = validatedData[idx].uom
743
+ raw.batchId = validatedData[idx].batchId
744
+ } else {
745
+ raw.assignedQty = 0
746
+ raw.assignedUomValue = 0
747
+ raw.productId = null
748
+ }
749
+
750
+ return {
751
+ ...raw,
752
+ releaseUomValue
753
+ }
754
+ })
755
+ }
756
+
562
757
  function getConditionValues(
563
758
  conditions: Filter[]
564
759
  ): {
@@ -121,10 +121,19 @@ export class InventoryInfos {
121
121
 
122
122
  @Field({ nullable: true })
123
123
  remark?: string
124
+
125
+ @Field({ nullable: true })
126
+ lockedQty?: number
127
+
128
+ @Field({ nullable: true })
129
+ lockedUomValue?: number
124
130
  }
125
131
 
126
132
  @ObjectType()
127
133
  export class ShippingOrderInfo {
134
+ @Field({ nullable: true })
135
+ id?: string
136
+
128
137
  @Field({ nullable: true })
129
138
  containerNo?: string
130
139
 
@@ -272,11 +281,11 @@ export class NewReleaseGood {
272
281
  @Field(type => ObjectRef, { nullable: true })
273
282
  shippingOrder: ObjectRef
274
283
 
275
- @Field(type => NewOrderInventory, { nullable: true })
276
- orderInventories: [NewOrderInventory]
284
+ @Field(type => [NewOrderInventory], { nullable: true })
285
+ orderInventories: NewOrderInventory[]
277
286
 
278
- @Field(type => NewOrderVas, { nullable: true })
279
- orderVass: [NewOrderVas]
287
+ @Field(type => [NewOrderVas], { nullable: true })
288
+ orderVass: NewOrderVas[]
280
289
 
281
290
  @Field({ nullable: true })
282
291
  status: string
@@ -295,6 +304,21 @@ export class NewReleaseGood {
295
304
 
296
305
  @Field({ nullable: true })
297
306
  partnerBizplaceId: string
307
+
308
+ @Field({ nullable: true })
309
+ sku: string
310
+
311
+ @Field({ nullable: true })
312
+ packingType: string
313
+
314
+ @Field({ nullable: true })
315
+ packingSize: string
316
+
317
+ @Field({ nullable: true })
318
+ uom: string
319
+
320
+ @Field({ nullable: true })
321
+ releaseQty: string
298
322
  }
299
323
 
300
324
  @InputType()
@@ -372,13 +396,13 @@ export class ReleaseGoodPatch {
372
396
  shippingOrder: ObjectRef
373
397
 
374
398
  @Field(type => [ObjectRef], { nullable: true })
375
- deliveryOrders: [ObjectRef]
399
+ deliveryOrders: ObjectRef[]
376
400
 
377
401
  @Field(type => [ObjectRef], { nullable: true })
378
- orderInventories: [ObjectRef]
402
+ orderInventories: ObjectRef[]
379
403
 
380
404
  @Field(type => [ObjectRef], { nullable: true })
381
- orderVass: [ObjectRef]
405
+ orderVass: ObjectRef[]
382
406
 
383
407
  @Field({ nullable: true })
384
408
  status: string
@@ -7,6 +7,7 @@ import {
7
7
  Inventory,
8
8
  INVENTORY_STATUS,
9
9
  INVENTORY_TRANSACTION_TYPE,
10
+ InventoryHistory,
10
11
  InventoryNoGenerator,
11
12
  InventoryPatch,
12
13
  InventoryUtil,
@@ -51,19 +52,19 @@ export class ReverseKittingOrderMutation {
51
52
 
52
53
  // 2. Create Reverse Kitting Order Items
53
54
  reverseKittingOrderOIs = await Promise.all(
54
- reverseKittingOrderOIs.map(async (ork: any) => {
55
- if (ork?.inventory?.id) {
56
- const foundInv: Inventory = await tx.getRepository(Inventory).findOne(ork.inventory.id)
57
- ork.inventory = foundInv
55
+ reverseKittingOrderOIs.map(async (reverseKittingOI: any) => {
56
+ if (reverseKittingOI?.inventory?.id) {
57
+ const foundInv: Inventory = await tx.getRepository(Inventory).findOne(reverseKittingOI.inventory.id)
58
+ reverseKittingOI.inventory = foundInv
58
59
 
59
- foundInv.lockedQty = Number(foundInv.lockedQty) + ork.qty
60
- foundInv.lockedUomValue = Number(foundInv.lockedUomValue) + ork.uomValue
60
+ foundInv.lockedQty = Number(foundInv.lockedQty) + reverseKittingOI.qty
61
+ foundInv.lockedUomValue = Number(foundInv.lockedUomValue) + reverseKittingOI.uomValue
61
62
  foundInv.updater = user
62
63
 
63
64
  await tx.getRepository(Inventory).save(foundInv)
64
65
  }
65
66
  return {
66
- ...ork,
67
+ ...reverseKittingOI,
67
68
  domain,
68
69
  name: OrderNoGenerator.orderVas(),
69
70
  reverseKittingOrder: createdReverseKittingOrder,
@@ -130,8 +131,8 @@ export class ReverseKittingOrderMutation {
130
131
  updater: user
131
132
  })
132
133
 
133
- reverseKittingOIs = reverseKittingOIs.map((ork: ReverseKittingOrderInventory) => {
134
- return { ...ork, status: ORDER_VAS_STATUS.READY_TO_PROCESS }
134
+ reverseKittingOIs = reverseKittingOIs.map((reverseKittingOI: ReverseKittingOrderInventory) => {
135
+ return { ...reverseKittingOI, status: ORDER_VAS_STATUS.READY_TO_PROCESS }
135
136
  })
136
137
  await tx.getRepository(ReverseKittingOrderInventory).save(reverseKittingOIs)
137
138
 
@@ -244,6 +245,68 @@ export class ReverseKittingOrderMutation {
244
245
  return reverseKitting
245
246
  }
246
247
 
248
+ @Directive('@transaction')
249
+ @Mutation(returns => Boolean, { description: 'To undo the execution of reverse kitting order' })
250
+ async undoReverseKittingInventory(
251
+ @Arg('reverseKittingOIName') reverseKittingOIName: string,
252
+ @Arg('palletId') palletId: string,
253
+ @Ctx() context: any
254
+ ): Promise<boolean> {
255
+ const { tx, domain, user } = context.state
256
+
257
+ //@sumjoekin please add stricter validation. It is dangerous without strict validation. Unexpected behaviour will cause accidental deletion of inventory.
258
+
259
+ const reverseKittingOI: ReverseKittingOrderInventory = await tx
260
+ .getRepository(ReverseKittingOrderInventory)
261
+ .findOne({
262
+ where: { domain, name: reverseKittingOIName },
263
+ relations: ['bizplace', 'reverseKittingOrder']
264
+ })
265
+
266
+ const bizplace: Bizplace = reverseKittingOI.bizplace
267
+ const reverseKittingOrder: ReverseKittingOrder = reverseKittingOI.reverseKittingOrder
268
+ let foundInventory: Inventory = await tx.getRepository(Inventory).findOne({
269
+ where: { domain, bizplace, orderVasId: reverseKittingOI.id, palletId },
270
+ relations: ['bizplace', 'product']
271
+ })
272
+ if (!foundInventory) throw new Error(`There's no results matched with condition ${foundInventory.palletId}`)
273
+
274
+ let operationGuide: any = JSON.parse(reverseKittingOI.operationGuide)
275
+ let productCombinationSets: any[] = operationGuide.data.productCombinationSets
276
+ productCombinationSets = productCombinationSets.map(item => {
277
+ if (foundInventory.product.id == item.productId) {
278
+ item.actualQty -= foundInventory.qty
279
+ }
280
+ return item
281
+ })
282
+ operationGuide.data.productCombinationSets = productCombinationSets
283
+
284
+ await tx
285
+ .getRepository(ReverseKittingOrderInventory)
286
+ .update({ id: reverseKittingOI.id }, { operationGuide: operationGuide })
287
+
288
+ foundInventory.lastSeq++
289
+ foundInventory.status = INVENTORY_STATUS.DELETED
290
+ foundInventory = await InventoryUtil.transactionInventory(
291
+ foundInventory,
292
+ reverseKittingOrder,
293
+ -foundInventory.qty,
294
+ -foundInventory.uomValue,
295
+ INVENTORY_TRANSACTION_TYPE.UNDO_REVERSE_KITTING,
296
+ user,
297
+ tx
298
+ )
299
+ foundInventory.qty = 0
300
+ foundInventory.uomValue = 0
301
+ foundInventory.updater = user
302
+
303
+ await tx.getRepository(InventoryHistory).update({ inventory: foundInventory }, { inventory: null })
304
+
305
+ await tx.getRepository(Inventory).delete({ id: foundInventory.id })
306
+
307
+ return true
308
+ }
309
+
247
310
  @Directive('@transaction')
248
311
  @Mutation(returns => ReverseKittingOrder, { description: 'To activate reverse kitting order' })
249
312
  async activateReverseKittingOrder(@Arg('name') name: string, @Ctx() context: any): Promise<ReverseKittingOrder> {
@@ -265,8 +328,8 @@ export class ReverseKittingOrderMutation {
265
328
  updater: user
266
329
  })
267
330
 
268
- reverseKittingOIs = reverseKittingOIs.map((ork: ReverseKittingOrderInventory) => {
269
- return { ...ork, status: ORDER_VAS_STATUS.PROCESSING }
331
+ reverseKittingOIs = reverseKittingOIs.map((reverseKittingOI: ReverseKittingOrderInventory) => {
332
+ return { ...reverseKittingOI, status: ORDER_VAS_STATUS.PROCESSING }
270
333
  })
271
334
  await tx.getRepository(ReverseKittingOrderInventory).save(reverseKittingOIs)
272
335
 
@@ -297,9 +360,7 @@ export class ReverseKittingOrderMutation {
297
360
  const customerBizplace: Bizplace = foundReverseKittingOrder.bizplace
298
361
 
299
362
  const productDetail: ProductDetail = await tx.getRepository(ProductDetail).findOne({
300
- where: {
301
- id: inventory.productDetailId
302
- },
363
+ where: { id: inventory.productDetailId },
303
364
  relations: ['product']
304
365
  })
305
366
 
@@ -394,18 +455,18 @@ export class ReverseKittingOrderMutation {
394
455
 
395
456
  // Delete order reverse kittings by ids
396
457
  await Promise.all(
397
- reverseKittingOIs.map(async (ork: ReverseKittingOrderInventory) => {
398
- if (ork?.inventory?.id) {
399
- ork.inventory = await tx.getRepository(Inventory).findOne(ork.inventory.id)
458
+ reverseKittingOIs.map(async (reverseKittingOI: ReverseKittingOrderInventory) => {
459
+ if (reverseKittingOI?.inventory?.id) {
460
+ reverseKittingOI.inventory = await tx.getRepository(Inventory).findOne(reverseKittingOI.inventory.id)
400
461
 
401
462
  await tx.getRepository(Inventory).save({
402
- ...ork.inventory,
403
- lockedQty: ork.inventory.lockedQty - ork.qty,
404
- lockedUomValue: ork.inventory.lockedUomValue - ork.uomValue,
463
+ ...reverseKittingOI.inventory,
464
+ lockedQty: reverseKittingOI.inventory.lockedQty - reverseKittingOI.qty,
465
+ lockedUomValue: reverseKittingOI.inventory.lockedUomValue - reverseKittingOI.uomValue,
405
466
  updater: user
406
467
  })
407
468
  }
408
- return ork
469
+ return reverseKittingOI
409
470
  })
410
471
  )
411
472
 
@@ -3,13 +3,15 @@ import { getRepository, In } from 'typeorm'
3
3
 
4
4
  import { User } from '@things-factory/auth-base'
5
5
  import { Bizplace, getPermittedBizplaceIds } from '@things-factory/biz-base'
6
+ import { generateId } from '@things-factory/id-rule-base'
7
+ import { Product, ProductDetail } from '@things-factory/product-base'
6
8
  import { convertListParams, Domain, ListParam } from '@things-factory/shell'
7
9
  import { Inventory, INVENTORY_STATUS } from '@things-factory/warehouse-base'
8
10
 
9
11
  import { ORDER_VAS_STATUS } from '../../constants'
10
12
  import { ReverseKittingOrderInventory } from '../reverse-kitting-order-inventory/reverse-kitting-order-inventory'
11
13
  import { ReverseKittingOrder } from './reverse-kitting-order'
12
- import { ReverseKittingOrderList } from './reverse-kitting-order-type'
14
+ import { LotInfo, ReverseKittingOrderList, ReverseKittingInventoryDetail } from './reverse-kitting-order-type'
13
15
 
14
16
  @Resolver(ReverseKittingOrder)
15
17
  export class ReverseKittingOrderQuery {
@@ -41,35 +43,119 @@ export class ReverseKittingOrderQuery {
41
43
 
42
44
  @Query(returns => ReverseKittingOrder, { description: 'To fetch a ReverseKittingOrder' })
43
45
  async reverseKittingInventories(
44
- @Arg('orderReverseKittingName') orderReverseKittingName: string,
46
+ @Arg('reverseKittingOIName') reverseKittingOIName: string,
47
+ @Arg('productDetailId') productDetailId: string,
45
48
  @Ctx() context: any
46
49
  ): Promise<Inventory[]> {
47
50
  const { domain } = context.state
48
- const reverseKittingOI: ReverseKittingOrderInventory = await getRepository(ReverseKittingOrderInventory).findOne({
51
+
52
+ const productDetail: ProductDetail = await getRepository(ProductDetail).findOne({
53
+ where: { id: productDetailId },
54
+ relations: ['product']
55
+ })
56
+
57
+ const product: Product = productDetail.product
58
+
59
+ const foundReverseKittingOI: ReverseKittingOrderInventory = await getRepository(
60
+ ReverseKittingOrderInventory
61
+ ).findOne({
49
62
  where: {
50
63
  domain,
51
- name: orderReverseKittingName,
64
+ name: reverseKittingOIName,
52
65
  status: ORDER_VAS_STATUS.PROCESSING
53
66
  },
54
67
  relations: ['bizplace', 'reverseKittingOrder', 'inventory']
55
68
  })
56
69
 
57
- if (!reverseKittingOI) return
70
+ if (!foundReverseKittingOI) return
58
71
 
59
- const reverseKittingOrder: ReverseKittingOrder = reverseKittingOI.reverseKittingOrder
60
- const customerBizplace: Bizplace = reverseKittingOI.bizplace
72
+ const reverseKittingOrder: ReverseKittingOrder = foundReverseKittingOI.reverseKittingOrder
73
+ const customerBizplace: Bizplace = foundReverseKittingOI.bizplace
61
74
  return await getRepository(Inventory).find({
62
75
  where: {
63
76
  domain,
64
77
  bizplace: customerBizplace,
65
78
  refOrderId: reverseKittingOrder.id,
66
- orderVasId: reverseKittingOI.id,
79
+ orderVasId: foundReverseKittingOI.id,
80
+ product: product,
67
81
  status: INVENTORY_STATUS.STORED
68
82
  },
69
- relations: ['product', 'bizplace']
83
+ relations: ['product', 'bizplace', 'location']
70
84
  })
71
85
  }
72
86
 
87
+ @Query(returns => [ReverseKittingInventoryDetail], {
88
+ description: 'To generate lot id and label for reverse kitting'
89
+ })
90
+ async generateReverseKittingLotId(
91
+ @Arg('targets', type => [LotInfo]) targets: LotInfo[],
92
+ @Arg('type') type: string,
93
+ @Ctx() context: any
94
+ ): Promise<ReverseKittingInventoryDetail[]> {
95
+ const { tx, domain } = context.state
96
+ // 1. get and set the date
97
+ const today = new Date()
98
+ const year = today.getFullYear()
99
+ const month = today.getMonth()
100
+ const day = today.getDate()
101
+
102
+ const yy = String(year).substr(String(year).length - 2)
103
+ const mm = String(month + 1).padStart(2, '0')
104
+ const dd = String(day).padStart(2, '0')
105
+
106
+ const date = yy + mm + dd
107
+ let results = []
108
+
109
+ // 2. get worksheet detail
110
+ let ids = targets.map(target => target.id)
111
+
112
+ // - getRepository using In(array) to pass the value to defined variable
113
+ const foundOrderReverseKittings: ReverseKittingOrderInventory[] = await tx
114
+ .getRepository(ReverseKittingOrderInventory)
115
+ .find({
116
+ where: {
117
+ domain,
118
+ id: In(ids)
119
+ },
120
+ relations: ['domain', 'bizplace']
121
+ })
122
+
123
+ // 3. from worksheet detail get product name, product type, batchid, packing type, bizplace
124
+
125
+ if (foundOrderReverseKittings.length <= 0) throw new Error('Unable to find order reverse kittings')
126
+ else {
127
+ for (let i = 0; i < foundOrderReverseKittings.length; i++) {
128
+ let foundORK = foundOrderReverseKittings[i]
129
+ for (let idx = 0; idx < targets.length; idx++) {
130
+ if (foundORK.id === targets[idx].id) {
131
+ // 4. generate pallet id based on print qty > call generateId resolver
132
+ for (let i = 0; i < targets[idx].printQty; i++) {
133
+ const generatedPalletId = await generateId({
134
+ domain,
135
+ type,
136
+ seed: {
137
+ batchId: foundORK.inventory.batchId,
138
+ date: date
139
+ }
140
+ })
141
+
142
+ // 5. map all data to be returned
143
+ if (foundORK != null) {
144
+ results.push({
145
+ ...foundORK,
146
+ palletId: generatedPalletId,
147
+ bizplace: foundORK.bizplace
148
+ })
149
+ }
150
+ }
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ return results
157
+ }
158
+
73
159
  @Query(returns => ReverseKittingOrderList, { description: 'To fetch multiple ReverseKittingOrders' })
74
160
  async reverseKittingOrders(@Args() params: ListParam, @Ctx() context: any): Promise<ReverseKittingOrderList> {
75
161
  const { domain, user }: { domain: Domain; user: User } = context.state
@@ -92,76 +178,6 @@ export class ReverseKittingOrderQuery {
92
178
  return { items, total }
93
179
  }
94
180
 
95
- // @Query(returns => [InventoryDetail], { description: 'To generate reverse kitting order lot id' })
96
- // async generateReverseKittingPalletId(
97
- // @Arg('targets') targets: [PalletInfo],
98
- // @Arg('type') type: string,
99
- // @Ctx() context: any
100
- // ): Promise<InventoryDetail[]> {
101
- // const { tx, domain } = context.state
102
- // // 1. get and set the date
103
- // const today = new Date()
104
- // const year = today.getFullYear()
105
- // const month = today.getMonth()
106
- // const day = today.getDate()
107
-
108
- // const yy = String(year).substr(String(year).length - 2)
109
- // const mm = String(month + 1).padStart(2, '0')
110
- // const dd = String(day).padStart(2, '0')
111
-
112
- // const date = yy + mm + dd
113
- // let results = []
114
-
115
- // // 2. get worksheet detail
116
- // let ids = targets.map(target => target.id)
117
-
118
- // // - getRepository using In(array) to pass the value to defined variable
119
- // const reverseKittingOIs: ReverseKittingOrderInventory[] = await tx
120
- // .getRepository(ReverseKittingOrderInventory)
121
- // .find({
122
- // where: {
123
- // domain,
124
- // id: In(ids)
125
- // },
126
- // relations: ['domain', 'bizplace']
127
- // })
128
-
129
- // // 3. from worksheet detail get product name, product type, batchid, packing type, bizplace
130
-
131
- // if (reverseKittingOIs.length <= 0) throw new Error('Unable to find order reverse kittings')
132
- // else {
133
- // for (let i = 0; i < reverseKittingOIs.length; i++) {
134
- // let reverseKittingOI = reverseKittingOIs[i]
135
- // for (let idx = 0; idx < targets.length; idx++) {
136
- // if (reverseKittingOI.id === targets[idx].id) {
137
- // // 4. generate pallet id based on print qty > call generateId resolver
138
- // for (let i = 0; i < targets[idx].printQty; i++) {
139
- // const generatedPalletId = await generateId({
140
- // domain,
141
- // type,
142
- // seed: {
143
- // batchId: reverseKittingOI.inventory.batchId,
144
- // date: date
145
- // }
146
- // })
147
-
148
- // // 5. map all data to be returned
149
- // if (reverseKittingOI != null) {
150
- // results.push({
151
- // ...reverseKittingOI,
152
- // palletId: generatedPalletId,
153
- // bizplace: reverseKittingOI.bizplace
154
- // })
155
- // }
156
- // }
157
- // }
158
- // }
159
- // }
160
- // }
161
-
162
- // return results
163
- // }
164
-
165
181
  @FieldResolver(type => Domain)
166
182
  async domain(@Root() reverseKittingOrder: ReverseKittingOrder): Promise<Domain> {
167
183
  return await getRepository(Domain).findOne(reverseKittingOrder.domainId)