@things-factory/sales-base 4.3.108 → 4.3.109

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 (70) hide show
  1. package/dist-server/service/arrival-notice/arrival-notice-mutation.js +5 -6
  2. package/dist-server/service/arrival-notice/arrival-notice-mutation.js.map +1 -1
  3. package/dist-server/service/arrival-notice/arrival-notice-query.js +0 -1
  4. package/dist-server/service/arrival-notice/arrival-notice-query.js.map +1 -1
  5. package/dist-server/service/draft-release-good/draft-release-good-mutation.js +4 -3
  6. package/dist-server/service/draft-release-good/draft-release-good-mutation.js.map +1 -1
  7. package/dist-server/service/draft-release-good/draft-release-good-query.js +15 -11
  8. package/dist-server/service/draft-release-good/draft-release-good-query.js.map +1 -1
  9. package/dist-server/service/invoice-product/invoice-product.js +3 -8
  10. package/dist-server/service/invoice-product/invoice-product.js.map +1 -1
  11. package/dist-server/service/order-inventory/order-inventory-query.js +1 -2
  12. package/dist-server/service/order-inventory/order-inventory-query.js.map +1 -1
  13. package/dist-server/service/order-inventory/order-inventory-types.js +10 -18
  14. package/dist-server/service/order-inventory/order-inventory-types.js.map +1 -1
  15. package/dist-server/service/order-inventory/order-inventory.js +3 -12
  16. package/dist-server/service/order-inventory/order-inventory.js.map +1 -1
  17. package/dist-server/service/order-product/order-product-query.js +0 -1
  18. package/dist-server/service/order-product/order-product-query.js.map +1 -1
  19. package/dist-server/service/order-product/order-product-types.js +10 -18
  20. package/dist-server/service/order-product/order-product-types.js.map +1 -1
  21. package/dist-server/service/order-vas/order-vas.js +3 -8
  22. package/dist-server/service/order-vas/order-vas.js.map +1 -1
  23. package/dist-server/service/others/other-query.js +177 -196
  24. package/dist-server/service/others/other-query.js.map +1 -1
  25. package/dist-server/service/others/other-types.js +1 -9
  26. package/dist-server/service/others/other-types.js.map +1 -1
  27. package/dist-server/service/purchase-order/purchase-order-mutation.js +2 -10
  28. package/dist-server/service/purchase-order/purchase-order-mutation.js.map +1 -1
  29. package/dist-server/service/purchase-order/purchase-order-query.js +0 -1
  30. package/dist-server/service/purchase-order/purchase-order-query.js.map +1 -1
  31. package/dist-server/service/release-good/release-good-mutation.js +8 -18
  32. package/dist-server/service/release-good/release-good-mutation.js.map +1 -1
  33. package/dist-server/service/release-good/release-good-query.js +2 -3
  34. package/dist-server/service/release-good/release-good-query.js.map +1 -1
  35. package/dist-server/service/release-good/release-good-types.js +4 -8
  36. package/dist-server/service/release-good/release-good-types.js.map +1 -1
  37. package/dist-server/service/return-order/return-order-mutation.js +0 -3
  38. package/dist-server/service/return-order/return-order-mutation.js.map +1 -1
  39. package/dist-server/service/return-order/return-order-types.js.map +1 -1
  40. package/dist-server/service/reverse-kitting-order-inventory/reverse-kitting-order-inventory.js +3 -8
  41. package/dist-server/service/reverse-kitting-order-inventory/reverse-kitting-order-inventory.js.map +1 -1
  42. package/dist-server/utils/inventory-util.js +104 -39
  43. package/dist-server/utils/inventory-util.js.map +1 -1
  44. package/package.json +13 -13
  45. package/server/service/arrival-notice/arrival-notice-mutation.ts +3 -6
  46. package/server/service/arrival-notice/arrival-notice-query.ts +0 -1
  47. package/server/service/draft-release-good/draft-release-good-mutation.ts +4 -3
  48. package/server/service/draft-release-good/draft-release-good-query.ts +21 -17
  49. package/server/service/invoice-product/invoice-product.ts +1 -5
  50. package/server/service/order-inventory/order-inventory-query.ts +0 -2
  51. package/server/service/order-inventory/order-inventory-types.ts +0 -6
  52. package/server/service/order-inventory/order-inventory.ts +1 -8
  53. package/server/service/order-product/order-product-query.ts +0 -1
  54. package/server/service/order-product/order-product-types.ts +0 -6
  55. package/server/service/order-vas/order-vas.ts +1 -6
  56. package/server/service/others/other-query.ts +219 -231
  57. package/server/service/others/other-types.ts +1 -7
  58. package/server/service/purchase-order/purchase-order-mutation.ts +1 -10
  59. package/server/service/purchase-order/purchase-order-query.ts +0 -1
  60. package/server/service/release-good/release-good-mutation.ts +8 -21
  61. package/server/service/release-good/release-good-query.ts +2 -3
  62. package/server/service/release-good/release-good-types.ts +3 -4
  63. package/server/service/return-order/return-order-mutation.ts +1 -4
  64. package/server/service/return-order/return-order-types.ts +0 -1
  65. package/server/service/reverse-kitting-order-inventory/reverse-kitting-order-inventory.ts +1 -5
  66. package/server/utils/inventory-util.ts +149 -75
  67. package/translations/en.json +0 -1
  68. package/translations/ko.json +1 -2
  69. package/translations/ms.json +1 -2
  70. package/translations/zh.json +0 -25
@@ -2,7 +2,7 @@ import { EntityManager, Equal, getRepository, In, Not, Raw, Repository, SelectQu
2
2
 
3
3
  import { User } from '@things-factory/auth-base'
4
4
  import { Bizplace } from '@things-factory/biz-base'
5
- import { Product, ProductBundle, ProductDetail } from '@things-factory/product-base'
5
+ import { Product, ProductBundle } from '@things-factory/product-base'
6
6
  import { Domain, ListParam } from '@things-factory/shell'
7
7
  import {
8
8
  Inventory,
@@ -12,8 +12,7 @@ import {
12
12
  Location,
13
13
  LOCATION_STATUS,
14
14
  LOCATION_TYPE,
15
- Pallet,
16
- generateInventoryHistory
15
+ Pallet
17
16
  } from '@things-factory/warehouse-base'
18
17
 
19
18
  import { ORDER_TYPES } from '../constants'
@@ -103,7 +102,6 @@ export const InventoryUtil = {
103
102
  coalesce(p.sku, '') AS "productSKU",
104
103
  coalesce(p.brand, '') AS "productBrand",
105
104
  p.id AS "productId",
106
- pd.id AS "productDetailId",
107
105
  COALESCE(SUM(COALESCE(i.qty, 0)) - SUM(COALESCE(i.locked_qty, 0)) - MAX(COALESCE(oi.release_qty, 0)) - MAX(COALESCE(bp.bundle_product_release_qty, 0)),0) AS "remainQty",
108
106
  COALESCE(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)),0) AS "remainUomValue",
109
107
  concat(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)), ' ', pd.uom) AS "remainUomValueWithUom",
@@ -116,7 +114,10 @@ export const InventoryUtil = {
116
114
  WHERE l2.type NOT IN ('${LOCATION_TYPE.QUARANTINE}', '${LOCATION_TYPE.RESERVE}')
117
115
  AND i.domain_id = $1 AND i.status = 'STORED'
118
116
  ${inventoryBizplaceFilter ? `AND i.bizplace_id = '${inventoryBizplaceFilter.value}'` : ``}
119
- ) i ON i.product_detail_id = pd.id
117
+ ) i ON i.product_id = pd.product_id
118
+ AND i.packing_type = pd.packing_type
119
+ and i.packing_size = pd.packing_size
120
+ and i.uom = pd.uom
120
121
  LEFT JOIN oi ON i.batch_id = oi.batch_id AND p.name = oi.product_name AND i.packing_type = oi.packing_type AND i.packing_size = oi.packing_size AND i.uom = oi.uom
121
122
  LEFT JOIN (
122
123
  SELECT pbs.product_id, SUM(pbs.bundle_qty * src.release_qty) AS bundle_product_release_qty, SUM(pbs.bundle_qty * src.release_uom_value) AS bundle_product_release_uom_value
@@ -124,26 +125,24 @@ export const InventoryUtil = {
124
125
  INNER JOIN json_populate_recordset(NULL::order_inventories, '${batchBundle}') src ON src.product_id = pbs.product_bundle_id
125
126
  GROUP BY pbs.product_id
126
127
  ) bp on i.product_id = bp.product_id
127
- WHERE i.bizplace_id IN (${bizplaceIds})
128
+ WHERE p.bizplace_id IN (${bizplaceIds})
128
129
  ${productDetailWhereClause}
129
- ${
130
- productFilter
131
- ? `AND (
130
+ ${productFilter
131
+ ? `AND (
132
132
  lower(p.sku) ilike '${productFilter.value}'
133
133
  OR lower(p.name) ilike '${productFilter.value}'
134
134
  OR lower(p.description) ilike '${productFilter.value}'
135
135
  )
136
136
  `
137
- : ``
138
- }
137
+ : ``
138
+ }
139
139
  GROUP BY
140
140
  p.id,
141
- pd.id,
142
141
  pd.packing_type,
143
142
  pd.packing_size,
144
143
  pd.uom
145
144
  UNION
146
- SELECT packing_type, packing_size,'UNIT' AS "uom", name AS "productName", sku AS "productSKU", '-' AS "productBrand", id AS "productId", pbs.product_detail_id AS "productDetailId",
145
+ SELECT packing_type, packing_size,'UNIT' AS "uom", name AS "productName", sku AS "productSKU", '-' AS "productBrand", id AS "productId",
147
146
  COALESCE(MIN(FLOOR(pbs."availableQty")),0) AS "remainQty",
148
147
  COALESCE(MIN(FLOOR(pbs."availableUomValue")),0) AS "remainUomValue",
149
148
  CONCAT(COALESCE(MIN(FLOOR(pbs."availableUomValue")),0),' UNIT') AS "remainUomValueWithUom",
@@ -152,8 +151,7 @@ export const InventoryUtil = {
152
151
  LEFT JOIN (
153
152
  SELECT pbs.product_id, pbs.product_bundle_id, min(pbs.bundle_qty),
154
153
  (SUM(COALESCE(i2.qty, 0)) - SUM(COALESCE(i2.locked_qty, 0)) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty) AS "availableQty",
155
- (SUM(COALESCE(i2.uom_value, 0)) - SUM(COALESCE(i2.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue",
156
- pbs.product_detail_id
154
+ (SUM(COALESCE(i2.uom_value, 0)) - SUM(COALESCE(i2.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue"
157
155
  FROM product_bundle_settings pbs
158
156
  LEFT JOIN inventories i2 ON i2.product_id = pbs.product_id AND i2.domain_id = $1 AND i2.status = 'STORED'
159
157
  INNER JOIN locations l2 ON i2.location_id = l2.id
@@ -162,28 +160,25 @@ export const InventoryUtil = {
162
160
  ${inventoryBizplaceFilter ? `AND i2.bizplace_id = '${inventoryBizplaceFilter.value}'` : ``}
163
161
  GROUP BY
164
162
  pbs.product_id,
165
- pbs.product_bundle_id,
166
- pbs.product_detail_id
163
+ pbs.product_bundle_id
167
164
  ) pbs ON pbs.product_bundle_id = pb.id
168
165
  ${bundleWhereClause}
169
166
  AND pb.bizplace_id IN (${bizplaceIds})
170
- ${
171
- productFilter
172
- ? `AND (
167
+ ${productFilter
168
+ ? `AND (
173
169
  lower(pb.sku) ilike '${productFilter.value}'
174
170
  OR lower(pb.name) ilike '${productFilter.value}'
175
171
  OR lower(pb.description) ilike '${productFilter.value}'
176
172
  )
177
173
  `
178
- : ``
179
- }
174
+ : ``
175
+ }
180
176
  GROUP BY
181
177
  pb.packing_type,
182
178
  pb.packing_size,
183
179
  pb.name,
184
180
  pb.sku,
185
- pb.id,
186
- pbs.product_detail_id
181
+ pb.id
187
182
  )
188
183
  AS inv_prod_grp
189
184
  ${whereClause}
@@ -278,7 +273,6 @@ export const InventoryUtil = {
278
273
  coalesce(p.sku, '') AS "productSKU",
279
274
  coalesce(p.brand, '') AS "productBrand",
280
275
  p.id AS "productId",
281
- i.product_detail_id AS "productDetailId",
282
276
  SUM(COALESCE(i.qty, 0)) - SUM(COALESCE(i.locked_qty, 0)) - MAX(COALESCE(oi.release_qty, 0)) - MAX(COALESCE(bp.bundle_product_release_qty, 0)) AS "remainQty",
283
277
  SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)) AS "remainUomValue",
284
278
  concat(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)), ' ', i.uom) AS "remainUomValueWithUom",
@@ -298,7 +292,6 @@ export const InventoryUtil = {
298
292
  AND l2.type NOT IN ('${LOCATION_TYPE.QUARANTINE}', '${LOCATION_TYPE.RESERVE}')
299
293
  ${productWhereClause}
300
294
  GROUP BY
301
- i.product_detail_id,
302
295
  i.batch_id,
303
296
  i.batch_id_ref,
304
297
  p.id,
@@ -306,7 +299,7 @@ export const InventoryUtil = {
306
299
  i.packing_size,
307
300
  i.uom
308
301
  UNION
309
- SELECT 'BUNDLE' AS "batchId", null as "batchIdRef", packing_type, packing_size,'UNIT' AS "uom", name AS "productName", sku AS "productSKU", 'brand' AS "productBrand", id AS "productId", pbs.product_detail_id AS "productDetailId",
302
+ SELECT 'BUNDLE' AS "batchId", null as "batchIdRef", packing_type, packing_size,'UNIT' AS "uom", name AS "productName", sku AS "productSKU", 'brand' AS "productBrand", id AS "productId",
310
303
  MIN(FLOOR(pbs."availableQty")) AS "remainQty",
311
304
  MIN(FLOOR(pbs."availableUomValue")) AS "remainUomValue",
312
305
  CONCAT(MIN(FLOOR(pbs."availableUomValue")),' UNIT') AS "remainUomValueWithUom",
@@ -315,17 +308,16 @@ export const InventoryUtil = {
315
308
  INNER JOIN (
316
309
  SELECT pbs.product_id, pbs.product_bundle_id, min(pbs.bundle_qty),
317
310
  (SUM(COALESCE(i2.qty, 0)) - SUM(COALESCE(i2.locked_qty, 0)) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty) AS "availableQty",
318
- (SUM(COALESCE(i2.uom_value, 0)) - SUM(COALESCE(i2.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue",
319
- pbs.product_detail_id
311
+ (SUM(COALESCE(i2.uom_value, 0)) - SUM(COALESCE(i2.locked_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue"
320
312
  FROM product_bundle_settings pbs
321
313
  LEFT JOIN inventories i2 ON i2.product_id = pbs.product_id AND i2.domain_id = $1
322
314
  AND i2.bizplace_id IN (${bizplaceIds})
323
315
  AND i2.status = 'STORED'
324
316
  LEFT JOIN oi ON oi.product_id = i2.product_id
325
- GROUP BY pbs.product_id, pbs.product_bundle_id, pbs.product_detail_id
317
+ GROUP BY pbs.product_id, pbs.product_bundle_id
326
318
  ) pbs ON pbs.product_bundle_id = pb.id
327
319
  ${bundleWhereClause}
328
- GROUP BY pb.packing_type, pb.packing_size, pb.name, pb.sku, pb.id, pbs.product_detail_id
320
+ GROUP BY pb.packing_type, pb.packing_size, pb.name, pb.sku, pb.id
329
321
  )
330
322
  AS inv_prod_grp
331
323
  ${whereClause}
@@ -365,15 +357,13 @@ export const InventoryUtil = {
365
357
 
366
358
  if (params?.pagination) {
367
359
  items = await trxMgr.query(
368
- `select * from temp_inventory_product_group ${filterGroupTypeQuery ? filterGroupTypeQuery : ''} ${
369
- sortingArr.length > 0 ? ' Order by ' + sortedBy.toString() : ''
360
+ `select * from temp_inventory_product_group ${filterGroupTypeQuery ? filterGroupTypeQuery : ''} ${sortingArr.length > 0 ? ' Order by ' + sortedBy.toString() : ''
370
361
  } OFFSET $1 LIMIT $2`,
371
362
  [(params.pagination.page - 1) * params.pagination.limit, params.pagination.limit]
372
363
  )
373
364
  } else {
374
365
  items = await trxMgr.query(
375
- `select * from temp_inventory_product_group ${filterGroupTypeQuery ? filterGroupTypeQuery : ''} ${
376
- sortingArr.length > 0 ? ' Order by ' + sortedBy.toString() : ''
366
+ `select * from temp_inventory_product_group ${filterGroupTypeQuery ? filterGroupTypeQuery : ''} ${sortingArr.length > 0 ? ' Order by ' + sortedBy.toString() : ''
377
367
  }`
378
368
  )
379
369
  }
@@ -493,17 +483,12 @@ export const InventoryUtil = {
493
483
  context: any,
494
484
  trxMgr?: EntityManager
495
485
  ): Promise<Inventory> {
496
- let validOrderInventories = orderInventories.filter(oi => oi.isError == false)
497
-
498
- if (!validOrderInventories.length) {
499
- return
500
- }
501
-
502
486
  let json_oi = JSON.stringify(
503
- validOrderInventories.map(x => {
487
+ orderInventories.map(x => {
504
488
  return {
505
- product_detail_id: x.productDetail.id,
489
+ product_id: x.product.id,
506
490
  batch_id: x.batchId,
491
+ packing_type: x.packingType,
507
492
  release_qty: x.releaseQty,
508
493
  uom: x.uom
509
494
  }
@@ -512,14 +497,15 @@ export const InventoryUtil = {
512
497
 
513
498
  let resultQb = await trxMgr.query(
514
499
  `
515
- select joi.product_detail_id as "productDetailId", joi.batch_id as "batchId", joi.uom as "uom", joi.release_qty as "releaseQty",
500
+ select joi.product_id as "productId", joi.batch_id as "batchId", joi.packing_type as "packingType", joi.uom as "uom", joi.release_qty as "releaseQty",
516
501
  sum(i.qty - coalesce(i.locked_qty,0)) - coalesce(
517
502
  (
518
503
  select sum(oi.release_qty) from order_inventories oi
519
504
  where (oi.status = 'PENDING' or oi.status = 'PENDING_RECEIVE' or oi.status = 'PENDING_WORKSHEET' or oi.status = 'PENDING_SPLIT')
520
505
  and oi.inventory_id IS null
521
- and oi.product_detail_id = joi.product_detail_id
506
+ and oi.product_id = joi.product_id
522
507
  and oi.batch_id = joi.batch_id
508
+ and oi.packing_type = joi.packing_type
523
509
  and oi.uom = joi.uom
524
510
  and oi.domain_id = $1
525
511
  and oi.bizplace_id = $2
@@ -529,20 +515,21 @@ export const InventoryUtil = {
529
515
  select sum(oi.release_uom_value) from order_inventories oi
530
516
  where (oi.status = 'PENDING' or oi.status = 'PENDING_RECEIVE' or oi.status = 'PENDING_WORKSHEET' or oi.status = 'PENDING_SPLIT')
531
517
  and oi.inventory_id IS null
532
- and oi.product_detail_id = joi.product_detail_id
518
+ and oi.product_id = joi.product_id
533
519
  and oi.batch_id = joi.batch_id
520
+ and oi.packing_type = joi.packing_type
534
521
  and oi.uom = joi.uom
535
522
  and oi.domain_id = $1
536
523
  and oi.bizplace_id = $2
537
524
  ),0) as "availableUomValue"
538
525
  from json_populate_recordset(NULL::order_inventories,'${json_oi}') joi
539
- left join inventories i on joi.product_detail_id = i.product_detail_id
526
+ left join inventories i on joi.product_id = i.product_id
540
527
  and joi.batch_id = i.batch_id
541
- and i.status ='STORED'
528
+ and joi.packing_type = i.packing_type and i.status ='STORED'
542
529
  and joi.uom = i.uom
543
530
  and i.domain_id = $1
544
531
  and i.bizplace_id = $2
545
- group by joi.product_detail_id, joi.batch_id, joi.packing_type, joi.release_qty, joi.uom
532
+ group by joi.product_id, joi.batch_id, joi.packing_type, joi.release_qty, joi.uom
546
533
  `,
547
534
  [warehouseDomain.id, partnerBizplace.id]
548
535
  )
@@ -588,9 +575,9 @@ export const InventoryUtil = {
588
575
  * @returns orderInventories
589
576
  */
590
577
  async autoAssignInventoryForRelease(
591
- orderInventory: OrderInventory,
592
578
  product: Product,
593
- productDetail: ProductDetail,
579
+ orderInventory: OrderInventory,
580
+ packingType: string,
594
581
  locationSortingRules: any = [],
595
582
  customerBizplace: Bizplace,
596
583
  domain: Domain,
@@ -601,14 +588,16 @@ export const InventoryUtil = {
601
588
  qb.leftJoinAndSelect('iv.location', 'loc')
602
589
  .andWhere('"iv"."domain_id" = :domainId')
603
590
  .andWhere('"iv"."bizplace_id" = :bizplaceId')
604
- .andWhere('"iv"."product_detail_id" = :productDetailId')
591
+ .andWhere('"iv"."packing_type" = :packingType')
592
+ .andWhere('"iv"."product_id" = :productId')
605
593
  .andWhere('"iv"."status" = :status')
606
594
  .andWhere('"iv"."qty" - COALESCE("iv"."locked_qty", 0) > 0')
607
595
  .andWhere('"loc"."type" NOT IN (:...locationTypes)')
608
596
  .setParameters({
609
597
  domainId: domain.id,
610
598
  bizplaceId: customerBizplace.id,
611
- productDetailId: productDetail.id,
599
+ packingType: packingType,
600
+ productId: product.id,
612
601
  status: INVENTORY_STATUS.STORED,
613
602
  locationTypes: [LOCATION_TYPE.QUARANTINE, LOCATION_TYPE.RESERVE]
614
603
  })
@@ -670,7 +659,7 @@ export const InventoryUtil = {
670
659
  }
671
660
  })
672
661
 
673
- return _composeTargetInventories(productDetail, orderInventory, inventories)
662
+ return _composeTargetInventories(product, orderInventory, inventories)
674
663
  },
675
664
 
676
665
  /**
@@ -862,32 +851,31 @@ async function getConditions(
862
851
  case 'batch_product':
863
852
  productWhereClause += `
864
853
  AND (i.batch_id, p.id, i.packing_type, i.packing_size) ${operator === 'in' ? 'IN' : 'NOT IN'} (${value
865
- .map(
866
- (v: { batchId: string; productId: string; packingType: string; packingSize: string }) =>
867
- `('${v.batchId}', '${v.productId}', '${v.packingType}', '${v.packingSize}')`
868
- )
869
- .join()})
854
+ .map(
855
+ (v: { batchId: string; productId: string; packingType: string; packingSize: string }) =>
856
+ `('${v.batchId}', '${v.productId}', '${v.packingType}', '${v.packingSize}')`
857
+ )
858
+ .join()})
870
859
  `
871
860
  break
872
861
 
873
862
  case 'batch_bundle':
874
863
  bundleWhereClause += `
875
864
  ${bundleWhereClause == '' ? 'WHERE' : 'AND'} pb.id ${operator === 'in' ? 'IN' : 'NOT IN'} (${value
876
- .map((v: { productId: string }) => `('${v.productId}')`)
877
- .join()})
865
+ .map((v: { productId: string }) => `('${v.productId}')`)
866
+ .join()})
878
867
  `
879
868
  break
880
869
 
881
870
  case 'product':
882
871
  productDetailWhereClause += `
883
- AND (pd.product_id, pd.packing_type, pd.packing_size, pd.uom) ${
884
- operator === 'in' ? 'IN' : 'NOT IN'
872
+ AND (pd.product_id, pd.packing_type, pd.packing_size, pd.uom) ${operator === 'in' ? 'IN' : 'NOT IN'
885
873
  } (${value
886
- .map(
887
- (v: { productId: string; packingType: string; packingSize: string; uom: string }) =>
888
- `('${v.productId}', '${v.packingType}', '${v.packingSize}', '${v.uom}')`
889
- )
890
- .join()})
874
+ .map(
875
+ (v: { productId: string; packingType: string; packingSize: string; uom: string }) =>
876
+ `('${v.productId}', '${v.packingType}', '${v.packingSize}', '${v.uom}')`
877
+ )
878
+ .join()})
891
879
  `
892
880
  break
893
881
  }
@@ -939,6 +927,98 @@ async function updateInventory(
939
927
  return await trxMgr.getRepository(Inventory).save(inventory)
940
928
  }
941
929
 
930
+ async function generateInventoryHistory(
931
+ inventory: Inventory,
932
+ refOrder: any,
933
+ transactionType: string,
934
+ qty: number,
935
+ uomValue: number,
936
+ user: User,
937
+ trxMgr?: EntityManager
938
+ ): Promise<InventoryHistory> {
939
+ const invHistoryRepo: Repository<InventoryHistory> =
940
+ trxMgr?.getRepository(InventoryHistory) || getRepository(InventoryHistory)
941
+ const invRepo: Repository<Inventory> = trxMgr?.getRepository(Inventory) || getRepository(Inventory)
942
+
943
+ if (!inventory?.id) throw new Error(`Can't find out ID of inventory.`)
944
+ if (!refOrder?.id || !refOrder.name) throw new Error(`Can't find out ID or Name of Reference Order`)
945
+ if (
946
+ !inventory?.domain ||
947
+ !inventory?.bizplace ||
948
+ !inventory?.product?.id ||
949
+ !inventory?.warehouse?.id ||
950
+ !inventory?.location?.id
951
+ ) {
952
+ inventory = await invRepo.findOne({
953
+ where: { id: inventory.id },
954
+ relations: ['domain', 'bizplace', 'product', 'warehouse', 'location']
955
+ })
956
+ }
957
+
958
+ const domain: Domain = inventory.domain
959
+ const location: Location = inventory.location
960
+
961
+ const seq: number = await invHistoryRepo.count({ domain: inventory.domain, palletId: inventory.palletId })
962
+ let openingQty: number = 0
963
+ let openingUomValue: number = 0
964
+
965
+ if (seq) {
966
+ const lastInvHistory: InventoryHistory = await invHistoryRepo.findOne({
967
+ domain: inventory.domain,
968
+ palletId: inventory.palletId,
969
+ seq: seq - 1
970
+ })
971
+ openingQty = lastInvHistory.openingQty + lastInvHistory.qty
972
+ openingUomValue = lastInvHistory.openingUomValue + lastInvHistory.uomValue
973
+ }
974
+
975
+ let inventoryHistory: InventoryHistory = new InventoryHistory()
976
+ inventoryHistory.name = InventoryNoGenerator.inventoryHistoryName()
977
+ inventoryHistory.description = inventory.description
978
+ inventoryHistory.seq = seq
979
+ inventoryHistory.palletId = inventory.palletId
980
+ inventoryHistory.cartonId = inventory.cartonId
981
+ inventoryHistory.batchId = inventory.batchId
982
+ inventoryHistory.batchIdRef = inventory.batchIdRef
983
+ inventoryHistory.status = inventory.status
984
+ inventoryHistory.transactionType = transactionType
985
+ inventoryHistory.refOrderId = refOrder?.id || null
986
+ inventoryHistory.orderNo = refOrder?.name || null
987
+ inventoryHistory.orderRefNo = refOrder?.refNo || null
988
+ inventoryHistory.inventory = inventory
989
+ inventoryHistory.product = inventory.product
990
+ inventoryHistory.reusablePallet = inventory.reusablePallet
991
+ inventoryHistory.zone = inventory.zone
992
+ inventoryHistory.warehouse = inventory.warehouse
993
+ inventoryHistory.location = inventory.location
994
+ inventoryHistory.expirationDate = inventory.expirationDate
995
+ inventoryHistory.packingType = inventory.packingType
996
+ inventoryHistory.packingSize = inventory.packingSize
997
+ inventoryHistory.uom = inventory.uom
998
+ inventoryHistory.qty = qty
999
+ inventoryHistory.openingQty = openingQty
1000
+ inventoryHistory.uomValue = uomValue
1001
+ inventoryHistory.openingUomValue = openingUomValue
1002
+ inventoryHistory.unitCost = inventory.unitCost
1003
+ inventoryHistory.domain = inventory.domain
1004
+ inventoryHistory.bizplace = inventory.bizplace
1005
+ inventoryHistory.creator = user
1006
+ inventoryHistory.updater = user
1007
+
1008
+ inventoryHistory = await invHistoryRepo.save(inventoryHistory)
1009
+
1010
+ if (inventory.lastSeq !== seq) {
1011
+ await invRepo.save({
1012
+ ...inventory,
1013
+ lastSeq: inventoryHistory.seq,
1014
+ updater: user
1015
+ })
1016
+ }
1017
+
1018
+ await switchLocationStatus(domain, location, user, trxMgr)
1019
+ return inventoryHistory
1020
+ }
1021
+
942
1022
  /**
943
1023
  * @description: Check location emptiness and update status of location
944
1024
  * @param domain
@@ -977,17 +1057,12 @@ export async function switchLocationStatus(
977
1057
  return location
978
1058
  }
979
1059
 
980
- export function _composeTargetInventories(
981
- productDetail: ProductDetail,
982
- record: any,
983
- inventories: Inventory[]
984
- ): OrderInventory[] {
1060
+ export function _composeTargetInventories(product: Product, record: any, inventories: Inventory[]): OrderInventory[] {
985
1061
  let leftReleaseQty: number = record.releaseQty
986
1062
  let leftReleaseUomValue: number = record.releaseUomValue
987
1063
  let compReleaseQty: number = 0
988
1064
  let compReleaseUomValue: number = 0
989
1065
  let totalInventoryQty: number = inventories.reduce((total, inventory) => total + inventory.remainQty, 0)
990
- let product = productDetail.product
991
1066
 
992
1067
  if (totalInventoryQty < record.releaseQty) {
993
1068
  throw new Error(`invalid release qty for ${product?.sku}`)
@@ -1040,7 +1115,6 @@ export function _composeTargetInventories(
1040
1115
  batchIdRef,
1041
1116
  uom,
1042
1117
  product,
1043
- productDetail,
1044
1118
  type: ORDER_TYPES.RELEASE_OF_GOODS
1045
1119
  })
1046
1120
 
@@ -1,6 +1,5 @@
1
1
  {
2
2
  "error.Manifest(s) (x) already in open status": "Manifest(s) ({x}) already in open status",
3
- "error.product_not_found": "product not found",
4
3
  "error.release_order_not_found": "release order not found",
5
4
  "error.tracking_no_already_scanned_before": "tracking no already scanned before",
6
5
  "error.tracking_no_belongs_to_x": "tracking no belongs to {x}",
@@ -20,6 +20,5 @@
20
20
  "error.Manifest(s) (x) already in open status": "[ko] Manifest(s) ({x}) already in open status",
21
21
  "error.release_order_not_found": "[ko] release order not found",
22
22
  "error.tracking_no_belongs_to_x": "[ko] tracking no belongs to {x}",
23
- "error.tracking_no_already_scanned_before": "[ko] tracking no already scanned before",
24
- "error.product_not_found": "[ko] product not found"
23
+ "error.tracking_no_already_scanned_before": "[ko] tracking no already scanned before"
25
24
  }
@@ -20,6 +20,5 @@
20
20
  "error.Manifest(s) (x) already in open status": "[ms] Manifest(s) ({x}) already in open status",
21
21
  "error.release_order_not_found": "[ms] release order not found",
22
22
  "error.tracking_no_belongs_to_x": "[ms] tracking no belongs to {x}",
23
- "error.tracking_no_already_scanned_before": "[ms] tracking no already scanned before",
24
- "error.product_not_found": "[ms] product not found"
23
+ "error.tracking_no_already_scanned_before": "[ms] tracking no already scanned before"
25
24
  }
@@ -1,25 +0,0 @@
1
- {
2
- "error.Manifest(s) (x) already in open status": "[zh] Manifest(s) ({x}) already in open status",
3
- "error.product_not_found": "[zh] product not found",
4
- "error.release_order_not_found": "[zh] release order not found",
5
- "error.tracking_no_already_scanned_before": "t[zh] racking no already scanned before",
6
- "error.tracking_no_belongs_to_x": "[zh] tracking no belongs to {x}",
7
- "field.customer": "[zh] customer",
8
- "field.issued_on": "[zh] issued on",
9
- "field.payment_due": "[zh] payment due",
10
- "field.version": "[zh] version",
11
- "field.purchase_order": "[zh] purchase order",
12
- "field.revision_price_on": "[zh] revision_price_on",
13
- "field.option_name": "[zh] option_name",
14
- "field.option_value": "[zh] option_value",
15
- "field.type": "[zh] type",
16
- "field.price": "[zh] price",
17
- "field.quotation": "[zh] quotation",
18
- "field.value": "[zh] value",
19
- "field.quotation_item": "[zh] quotation item",
20
- "field.amount": "[zh] amount",
21
- "field.options": "[zh] options",
22
- "field.unit": "[zh] unit",
23
- "field.start_at": "[zh] start at",
24
- "field.end_at": "[zh] end at"
25
- }