@things-factory/sales-base 4.3.246 → 4.3.249

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.
@@ -1,29 +1,10 @@
1
- import {
2
- EntityManager,
3
- Equal,
4
- getRepository,
5
- In,
6
- Not,
7
- Raw,
8
- Repository,
9
- SelectQueryBuilder
10
- } from 'typeorm'
1
+ import { EntityManager, Equal, getRepository, In, Not, Raw, Repository, SelectQueryBuilder } from 'typeorm'
11
2
 
12
3
  import { User } from '@things-factory/auth-base'
13
4
  import { Bizplace } from '@things-factory/biz-base'
14
- import {
15
- Product,
16
- ProductBundle,
17
- ProductDetail
18
- } from '@things-factory/product-base'
19
- import {
20
- PartnerSetting,
21
- Setting
22
- } from '@things-factory/setting-base'
23
- import {
24
- Domain,
25
- ListParam
26
- } from '@things-factory/shell'
5
+ import { Product, ProductBundle, ProductDetail } from '@things-factory/product-base'
6
+ import { PartnerSetting, Setting } from '@things-factory/setting-base'
7
+ import { Domain, ListParam } from '@things-factory/shell'
27
8
  import {
28
9
  generateInventoryHistory,
29
10
  Inventory,
@@ -122,14 +103,15 @@ export const InventoryUtil = {
122
103
  coalesce(p.brand, '') AS "productBrand",
123
104
  p.id AS "productId",
124
105
  pd.id AS "productDetailId",
125
- 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",
126
- 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",
127
- 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",
106
+ COALESCE(SUM(COALESCE(i.qty, 0)) - SUM(COALESCE(i.locked_qty, 0)) - SUM(COALESCE(pds.unassigned_qty, 0)) - MAX(COALESCE(oi.release_qty, 0)) - MAX(COALESCE(bp.bundle_product_release_qty, 0)),0) AS "remainQty",
107
+ COALESCE(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - SUM(COALESCE(pds.unassigned_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)),0) AS "remainUomValue",
108
+ concat(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - SUM(COALESCE(pds.unassigned_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)), ' ', pd.uom) AS "remainUomValueWithUom",
128
109
  COALESCE(SUM(COALESCE(i.transfer_qty, 0)), 0) AS "transferQty",
129
110
  COALESCE(SUM(COALESCE(i.transfer_uom_value, 0)), 0) AS "transferUomValue",
130
111
  'SINGLE' AS "groupType"
131
112
  FROM products p
132
113
  INNER join product_details pd on pd.product_id = p.id
114
+ LEFT JOIN product_detail_stocks pds on pds.product_detail_id = pd.id
133
115
  LEFT JOIN (
134
116
  SELECT i.* FROM inventories i
135
117
  INNER JOIN locations l2 ON i.location_id = l2.id AND i.domain_id = l2.domain_id
@@ -175,13 +157,14 @@ export const InventoryUtil = {
175
157
  FROM product_bundles pb
176
158
  LEFT JOIN (
177
159
  SELECT pbs.product_id, pbs.product_bundle_id, min(pbs.bundle_qty),
178
- (SUM(COALESCE(i2.qty, 0)) - SUM(COALESCE(i2.locked_qty, 0)) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty) AS "availableQty",
179
- (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",
160
+ (SUM(COALESCE(i2.qty, 0)) - SUM(COALESCE(i2.locked_qty, 0)) - SUM(COALESCE(pds2.unassigned_qty, 0)) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty) AS "availableQty",
161
+ (SUM(COALESCE(i2.uom_value, 0)) - SUM(COALESCE(i2.locked_uom_value, 0)) - SUM(COALESCE(pds2.unassigned_uom_value, 0)) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue",
180
162
  (SUM(COALESCE(i2.transfer_qty, 0)) / min(pbs.bundle_qty)) AS "transferQty",
181
163
  (SUM(COALESCE(i2.transfer_uom_value, 0)) / min(pbs.bundle_qty)) AS "transferUomValue",
182
164
  pbs.product_detail_id
183
165
  FROM product_bundle_settings pbs
184
166
  LEFT JOIN inventories i2 ON i2.product_id = pbs.product_id AND i2.domain_id = $1 AND i2.status = 'STORED'
167
+ LEFT JOIN product_detail_stocks pds2 ON pds2.product_detail_id = pbs.product_detail_id
185
168
  INNER JOIN locations l2 ON i2.location_id = l2.id
186
169
  LEFT JOIN oi ON oi.product_id = i2.product_id
187
170
  WHERE l2.type NOT IN ('${LOCATION_TYPE.QUARANTINE}', '${LOCATION_TYPE.RESERVE}')
@@ -305,13 +288,14 @@ export const InventoryUtil = {
305
288
  coalesce(p.brand, '') AS "productBrand",
306
289
  p.id AS "productId",
307
290
  i.product_detail_id AS "productDetailId",
308
- 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",
309
- 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",
310
- 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",
291
+ 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)) - SUM(COALESCE(pds.unassigned_qty, 0)) AS "remainQty",
292
+ 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)) - SUM(COALESCE(pds.unassigned_uom_value)) AS "remainUomValue",
293
+ 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)) - SUM(COALESCE(pds.unassigned_uom_value)), ' ', i.uom) AS "remainUomValueWithUom",
311
294
  'SINGLE' AS "groupType"
312
295
  FROM
313
296
  inventories i
314
297
  LEFT JOIN products p ON i.product_id = p.id
298
+ LEFT JOIN product_detail_stocks pds ON pds.product_detail_id = i.product_detail_id
315
299
  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
316
300
  LEFT JOIN locations l2 ON i.location_id = l2.id AND i.domain_id = l2.domain_id
317
301
  LEFT JOIN (
@@ -321,7 +305,7 @@ export const InventoryUtil = {
321
305
  GROUP BY pbs.product_id
322
306
  ) bp on i.product_id = bp.product_id
323
307
  WHERE i.domain_id = $1
324
- AND l2.type NOT IN ('${LOCATION_TYPE.QUARANTINE}', '${LOCATION_TYPE.RESERVE}')
308
+ AND l2.type NOT IN ('${LOCATION_TYPE.QUARANTINE}', '${LOCATION_TYPE.RESERVE}', '${LOCATION_TYPE.DAMAGE}')
325
309
  AND i.obsolete = false
326
310
  AND i.transfer_qty <= 0
327
311
  AND i.transfer_uom_value <= 0
@@ -535,7 +519,7 @@ export const InventoryUtil = {
535
519
  validOrderInventories.map(x => {
536
520
  return {
537
521
  product_detail_id: x.productDetail.id,
538
- batch_id: x.batchId.replace(`'`,`''`),
522
+ batch_id: x.batchId.replace(`'`, `''`),
539
523
  release_qty: x.releaseQty,
540
524
  uom: x.uom
541
525
  }
@@ -628,7 +612,8 @@ export const InventoryUtil = {
628
612
  domain: Domain,
629
613
  trxMgr: EntityManager,
630
614
  batchId?: string,
631
- preferLocation: string = null
615
+ preferLocation: string = null,
616
+ recall: boolean = null
632
617
  ): Promise<OrderInventory[]> {
633
618
  let strictProduct = 'false'
634
619
 
@@ -652,6 +637,12 @@ export const InventoryUtil = {
652
637
 
653
638
  if (partnerStrictProductSelectionSetting) strictProduct = partnerStrictProductSelectionSetting.value
654
639
 
640
+ let locationTypes = [LOCATION_TYPE.QUARANTINE, LOCATION_TYPE.RESERVE]
641
+
642
+ if (recall === false) {
643
+ locationTypes.push(LOCATION_TYPE.DAMAGE)
644
+ }
645
+
655
646
  let qb: SelectQueryBuilder<Inventory> = trxMgr.getRepository(Inventory).createQueryBuilder('iv')
656
647
  qb.innerJoinAndSelect('iv.location', 'loc')
657
648
  .innerJoin('warehouse_inventory_assignment_rankings', 'wiar', '"wiar"."location_type" = "loc"."type"')
@@ -673,7 +664,7 @@ export const InventoryUtil = {
673
664
  bizplaceId: customerBizplace.id,
674
665
  productId: product.id,
675
666
  status: INVENTORY_STATUS.STORED,
676
- locationTypes: [LOCATION_TYPE.QUARANTINE, LOCATION_TYPE.RESERVE]
667
+ locationTypes
677
668
  })
678
669
 
679
670
  if (batchId) {
@@ -804,7 +795,7 @@ async function getConditions(
804
795
  WHERE pb.status = 'ACTIVATED'
805
796
  `
806
797
 
807
- let whereClause = hasRemainingQty ? ` WHERE "remainQty" > 0` : ` WHERE 1 = 1`
798
+ let whereClause = hasRemainingQty ? ` WHERE "remainQty" > 0 ` : ` WHERE 1 = 1 `
808
799
 
809
800
  let productDetailWhereClause = ``
810
801
 
@@ -837,7 +828,7 @@ async function getConditions(
837
828
 
838
829
  case 'batchId':
839
830
  let batchId = `${value}`
840
- whereClause += `AND LOWER("batchId") LIKE '${batchId.replace(`'`,`''`).toLowerCase()}'`
831
+ whereClause += `AND LOWER("batchId") LIKE '${batchId.replace(`'`, `''`).toLowerCase()}'`
841
832
  break
842
833
 
843
834
  case 'batchIdRef':
@@ -941,7 +932,7 @@ async function getConditions(
941
932
  AND (i.batch_id, p.id, i.packing_type, i.packing_size) ${operator === 'in' ? 'IN' : 'NOT IN'} (${value
942
933
  .map(
943
934
  (v: { batchId: string; productId: string; packingType: string; packingSize: string }) =>
944
- `('${v.batchId.replace(`'`,`''`)}', '${v.productId}', '${v.packingType}', '${v.packingSize}')`
935
+ `('${v.batchId.replace(`'`, `''`)}', '${v.productId}', '${v.packingType}', '${v.packingSize}')`
945
936
  )
946
937
  .join()})
947
938
  `
@@ -1109,6 +1100,7 @@ export function _composeTargetInventories(
1109
1100
  }
1110
1101
 
1111
1102
  orderInventories.push({
1103
+ ...record,
1112
1104
  ...orderInventory,
1113
1105
  inventory,
1114
1106
  packingType,