@things-factory/warehouse-base 4.3.256 → 4.3.261

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/warehouse-base",
3
- "version": "4.3.256",
3
+ "version": "4.3.261",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -31,5 +31,5 @@
31
31
  "@things-factory/product-base": "^4.3.253",
32
32
  "@things-factory/setting-base": "^4.3.253"
33
33
  },
34
- "gitHead": "c1b9ba08f80595ac4bf77dedd5bac12720c4c581"
34
+ "gitHead": "c085fa97b4a7a9abd06aab1820d4e7dbc4ed83ad"
35
35
  }
@@ -5,6 +5,7 @@ import { Powrup, PowrupAPI, Platform } from '@things-factory/integration-powrup'
5
5
  import { Product, ProductDetail } from '@things-factory/product-base'
6
6
  import { INVENTORY_STATUS, LOCATION_TYPE } from '../../constants'
7
7
  import { Inventory } from '../../service/inventory/inventory'
8
+ import { ProductDetailStock } from '../../service/product-detail-stock/product-detail-stock'
8
9
  import { Domain, User } from '@things-factory/shell'
9
10
 
10
11
  export class PowrupController {
@@ -71,12 +72,16 @@ export class PowrupController {
71
72
  }
72
73
  })
73
74
 
75
+ const productDetailStock: ProductDetailStock = await trxMgr
76
+ .getRepository(ProductDetailStock)
77
+ .findOne({ productDetail })
78
+
74
79
  const powrupInvs: any[] = [
75
80
  {
76
81
  refCode: productDetail.refCode,
77
82
  stock: {
78
83
  quantityTotal: inventoryTotalQty || 0,
79
- quantityReserved: inventoryTotalLockedQty || 0,
84
+ quantityReserved: inventoryTotalLockedQty + (productDetailStock.unassignedQty || 0) || 0,
80
85
  quantityBuffer: inventoryBufferQty || 0
81
86
  }
82
87
  }
@@ -236,13 +236,12 @@ export class InventoryQuery {
236
236
 
237
237
  let bizplaceQuery = ''
238
238
  if (permittedBizplaceIds.length > 0) {
239
- bizplaceQuery = `AND EXISTS (
240
- SELECT * FROM (VALUES ${permittedBizplaceIds.map(id => `('${id}')`).join(',')})
241
- AS bizFilter(bizplace_id)
242
- WHERE bizFilter.bizplace_id::uuid = "Inventory"."bizplace_id"
243
- )`
239
+ bizplaceQuery = `
240
+ select bizplace_id::uuid from (VALUES ${permittedBizplaceIds.map(id => `('${id}')`).join(',')})
241
+ as bizFilter(bizplace_id)
242
+ `
244
243
  } else {
245
- bizplaceQuery = `1 = 0`
244
+ bizplaceQuery = `select null as bizplace_id`
246
245
  }
247
246
 
248
247
  let productQuery = ''
@@ -255,13 +254,10 @@ export class InventoryQuery {
255
254
  })
256
255
  .join(',')
257
256
  productQuery = `AND (
258
- Lower("Product"."name") LIKE ANY(ARRAY[${productValue}])
259
- OR Lower("Product"."sku") LIKE ANY(ARRAY[${productValue}])
260
- OR Lower("Product"."brand") LIKE ANY(ARRAY[${productValue}])
261
- OR Lower("Product"."description") LIKE ANY(ARRAY[${productValue}])
262
- OR Lower("ProductRef"."name") LIKE ANY(ARRAY[${productValue}])
263
- OR Lower("ProductRef"."sku") LIKE ANY(ARRAY[${productValue}])
264
- OR Lower("ProductRef"."description") LIKE ANY(ARRAY[${productValue}])
257
+ Lower("p"."name") LIKE ANY(ARRAY[${productValue}])
258
+ OR Lower("p"."sku") LIKE ANY(ARRAY[${productValue}])
259
+ OR Lower("p"."brand") LIKE ANY(ARRAY[${productValue}])
260
+ OR Lower("p"."description") LIKE ANY(ARRAY[${productValue}])
265
261
  )`
266
262
  }
267
263
 
@@ -276,7 +272,7 @@ export class InventoryQuery {
276
272
  productQuery =
277
273
  productQuery +
278
274
  ` AND (
279
- Lower("Product"."type") LIKE ANY(ARRAY[${productTypeValue}])
275
+ Lower("p"."type") LIKE ANY(ARRAY[${productTypeValue}])
280
276
  )`
281
277
  }
282
278
 
@@ -297,7 +293,8 @@ export class InventoryQuery {
297
293
  }
298
294
 
299
295
  if (availableStockFilter?.value) {
300
- availableStockQuery = ` AND SUM("Inventory"."qty") > 0`
296
+ //availableStockQuery = ` AND SUM(coalesce(i."qty",0)) >= 0`
297
+ availableStockQuery = ` AND "availableQty" > 0`
301
298
  }
302
299
 
303
300
  if (availableStockFilter?.value && quarantineStockFilter?.value) {
@@ -364,91 +361,102 @@ export class InventoryQuery {
364
361
  `
365
362
  create temp table temp_inv_history AS
366
363
  (
367
- SELECT "Product"."id" AS "id","ProductDetails"."id" AS "productDetailId", "Product"."sku" AS "sku", "Product"."brand" AS "brand", "Product"."name" AS "name", "Product"."type" AS "type", "Product"."description" AS "description",
368
- "Inventory"."packing_type" AS "packingType", "ProductDetails"."aux_unit_1" AS "auxUnit1", "ProductDetails"."aux_value_1" AS "auxValue1",
369
- "ProductDetails"."aux_unit_2" AS "auxUnit2", "ProductDetails"."aux_value_2" AS "auxValue2",
370
- "ProductDetails"."aux_unit_3" AS "auxUnit3", "ProductDetails"."aux_value_3" AS "auxValue3",
371
- "ProductDetails"."aux_unit_4" AS "auxUnit4", "ProductDetails"."aux_value_4" AS "auxValue4",
372
- "ProductDetails"."aux_unit_5" AS "auxUnit5", "ProductDetails"."aux_value_5" AS "auxValue5",
373
- "Product"."weight" AS "weight", "ProductRef"."id" AS "productRefId",
374
- "ProductRef"."description" AS "productRefDesciption", "Bizplace"."id" AS "bizplaceId", "Bizplace"."name" AS "bizplaceName",
375
- CASE WHEN SUM("Inventory"."qty") > 0 THEN SUM(COALESCE("Inventory"."unit_cost", 0) * "Inventory"."qty")/ SUM("Inventory"."qty") ELSE 0 END AS "averageUnitCost",
376
- COALESCE("ProductDetailBizplaceSetting"."min_qty", "Product"."min_qty",0) AS "minQty",
377
- COALESCE("ProductDetailBizplaceSetting"."max_qty", "Product"."max_qty",0) AS "maxQty",
378
- (CASE WHEN "Location"."type" NOT IN ('${LOCATION_TYPE.QUARANTINE}','${LOCATION_TYPE.RESERVE}')
379
- AND CASE WHEN "Inventory"."expiration_date" is not null and "Product"."shelf_life" is not null then CURRENT_DATE < "Inventory"."expiration_date" - "Product"."shelf_life" else true end
380
- AND "Inventory"."obsolete" is false
381
- THEN
382
- CASE WHEN SUM("Inventory"."qty") > 0
383
- THEN SUM("Inventory"."qty")
384
- ELSE 0 END
385
- ELSE 0 END) AS "qty",
386
- (CASE WHEN "Location"."type" NOT IN ('${LOCATION_TYPE.QUARANTINE}','${LOCATION_TYPE.RESERVE}')
387
- AND CASE WHEN "Inventory"."expiration_date" is not null and "Product"."shelf_life" is not null then CURRENT_DATE < "Inventory"."expiration_date" - "Product"."shelf_life" else true end
388
- AND "Inventory"."obsolete" is false
389
- THEN
390
- CASE WHEN SUM("Inventory"."qty") > 0 THEN SUM("Inventory"."qty") ELSE 0 END - SUM(COALESCE("Inventory"."locked_qty", 0))
391
- ELSE 0 END) AS "availableQty",
392
- CASE WHEN "Location"."type" = '${LOCATION_TYPE.QUARANTINE}'
393
- THEN
394
- CASE WHEN SUM("Inventory"."qty") > 0
395
- THEN SUM("Inventory"."qty")-SUM(COALESCE("Inventory"."locked_qty", 0))
396
- ELSE 0 END
397
- ELSE 0 END AS "quarantineQty",
398
- CASE WHEN "Location"."type" = '${LOCATION_TYPE.RESERVE}'
399
- THEN
400
- CASE WHEN SUM("Inventory"."qty") > 0
401
- THEN SUM("Inventory"."qty")-SUM(COALESCE("Inventory"."locked_qty", 0))
402
- ELSE 0 END
403
- ELSE 0 END AS "reserveQty",
404
- CASE WHEN "Location"."type" = '${LOCATION_TYPE.BIN}'
405
- THEN SUM("Inventory"."qty")
406
- ELSE 0
407
- END AS "binQty",
408
- CASE WHEN "Inventory"."obsolete" is true or
409
- CASE WHEN "Inventory"."expiration_date" is not null and "Product"."shelf_life" is not null then CURRENT_DATE > "Inventory"."expiration_date" - "Product"."shelf_life" else false end
410
- THEN
411
- CASE WHEN SUM("Inventory"."qty") > 0
412
- THEN SUM("Inventory"."qty")
413
- ELSE 0 END
414
- ELSE 0 END AS "obsoleteQty",
415
- CASE WHEN "Location"."type" = '${LOCATION_TYPE.DAMAGE}'
416
- THEN
417
- CASE WHEN SUM("Inventory"."qty") > 0
418
- THEN SUM("Inventory"."qty")
419
- ELSE 0 END
420
- ELSE 0 END AS "damageQty",
421
- CASE WHEN "Location"."type" = '${LOCATION_TYPE.STORAGE}'
422
- THEN
423
- CASE WHEN SUM("Inventory"."qty") > 0
424
- THEN SUM("Inventory"."qty")-SUM(COALESCE("Inventory"."locked_qty", 0))
425
- ELSE 0 END
426
- ELSE 0 END AS "storageQty",
427
- CASE WHEN "Inventory"."transfer_qty" > 0
428
- THEN
429
- SUM("Inventory"."transfer_qty")
430
- ELSE 0 END AS "transferQty",
431
- SUM(COALESCE("Inventory"."locked_qty", 0)) AS "locked_qty"
432
- FROM "inventories" "Inventory"
433
- LEFT JOIN "products" "Product" ON "Product"."id"="Inventory"."product_id"
434
- LEFT JOIN "product_details" "ProductDetails" ON "ProductDetails"."product_id" = "Product"."id" AND "ProductDetails"."is_default"
435
- LEFT JOIN "product_detail_bizplace_settings" "ProductDetailBizplaceSetting" ON "ProductDetailBizplaceSetting"."product_detail_id" = "ProductDetails"."id"
436
- LEFT JOIN "product_detail_stocks" "ProductDetailStock" ON "ProductDetailStock"."product_detail_id" = "ProductDetails"."id"
437
- AND "ProductDetailBizplaceSetting"."domain_id" = "Inventory"."domain_id"
438
- LEFT JOIN "products" "ProductRef" ON "ProductRef"."id"="Product"."product_ref_id"
439
- INNER JOIN "bizplaces" "Bizplace" ON "Bizplace"."id"="Inventory"."bizplace_id"
440
- INNER JOIN "locations" "Location" ON "Location"."id" = "Inventory"."location_id"
441
- WHERE "Inventory"."qty" >= 0
442
- AND "Inventory"."status" <> 'MISSING' AND "Inventory"."status" <> 'PUTTING_AWAY'
443
- AND "Inventory"."domain_id" = $1
444
- ${bizplaceQuery}
445
- ${productQuery}
446
- GROUP BY "Product"."id","Bizplace"."id","Bizplace"."name" ,"ProductDetailBizplaceSetting"."id", "ProductDetails"."id",
447
- "Inventory"."packing_type", "Inventory"."packing_size", "Inventory"."uom",
448
- "Location"."type", "ProductRef"."id","Inventory"."transfer_qty",
449
- "Inventory"."expiration_date", "Inventory"."obsolete"
450
- HAVING 1 = 1
451
- ${availableStockQuery}
364
+ select p."id" as "id", pd."id" as "productDetailId", p."sku" as "sku", p."brand" as "brand", p."name" as "name", p."type" as "type", p."description" as "description",
365
+ coalesce(i."packing_type", pd."packing_type") as "packingType", p."weight" as "weight",
366
+ pd."aux_unit_1" as "auxUnit1", pd."aux_value_1" as "auxValue1", pd."aux_unit_2" as "auxUnit2", pd."aux_value_2" as "auxValue2",
367
+ pd."aux_unit_3" as "auxUnit3", pd."aux_value_3" as "auxValue3", pd."aux_unit_4" as "auxUnit4", pd."aux_value_4" as "auxValue4",
368
+ pd."aux_unit_5" as "auxUnit5", pd."aux_value_5" as "auxValue5", coalesce(inv_bz."id", bz2."id") as "bizplaceId", coalesce(inv_bz."name", bz2."name") as "bizplaceName",
369
+ COALESCE(pdbs."min_qty", p."min_qty",0) as "minQty", COALESCE(pdbs."max_qty", p."max_qty",0) as "maxQty",
370
+ (
371
+ case when sum(coalesce(i."qty",0)) > 0 then sum(coalesce(i."unit_cost", 0) * coalesce(i."qty",0))/ sum(coalesce(i."qty",0))
372
+ else 0 end
373
+ ) as "averageUnitCost",
374
+ (
375
+ case when inv_loc."type" NOT IN ('QUARANTINE','RESERVE') and
376
+ case when i."expiration_date" is not null and p."shelf_life" is not null then CURRENT_DATE < i."expiration_date" - p."shelf_life" else true end
377
+ and i."obsolete" is false
378
+ then
379
+ case when sum(coalesce(i."qty",0)) > 0 then sum(coalesce(i."qty",0)) else 0 end
380
+ else 0 end
381
+ ) as "qty",
382
+ (
383
+ case when inv_loc."type" NOT IN ('QUARANTINE','RESERVE')
384
+ and case when i."expiration_date" is not null and p."shelf_life" is not null then CURRENT_DATE < i."expiration_date" - p."shelf_life" else true end
385
+ and i."obsolete" is false
386
+ then
387
+ case when sum(coalesce(i."qty",0)) > 0 then sum(coalesce(i."qty",0)) else 0 end - sum(COALESCE(i."locked_qty", 0))
388
+ else 0 end
389
+ ) as "availableQty",
390
+ (
391
+ case when inv_loc."type" = 'QUARANTINE'
392
+ then case when sum(coalesce(i."qty",0)) > 0
393
+ then sum(coalesce(i."qty",0))-sum(COALESCE(i."locked_qty", 0))
394
+ else 0 end
395
+ else 0 end
396
+ ) as "quarantineQty",
397
+ (
398
+ case when inv_loc."type" = 'RESERVE'
399
+ then
400
+ case when sum(coalesce(i."qty",0)) > 0
401
+ then sum(coalesce(i."qty",0))-sum(COALESCE(i."locked_qty", 0))
402
+ else 0 end
403
+ else 0 end
404
+ ) as "reserveQty",
405
+ (
406
+ case when inv_loc."type" = 'BIN'
407
+ then sum(coalesce(i."qty",0))
408
+ else 0
409
+ end
410
+ )as "binQty",
411
+ (
412
+ case when i."obsolete" is true or
413
+ case when i."expiration_date" is not null and p."shelf_life" is not null then CURRENT_DATE > i."expiration_date" - p."shelf_life" else false end
414
+ then
415
+ case when sum(coalesce(i."qty",0)) > 0 then sum(coalesce(i."qty",0)) else 0 end
416
+ else 0 end
417
+ ) as "obsoleteQty",
418
+ (
419
+ case when inv_loc."type" = 'DAMAGE'
420
+ then
421
+ case when sum(coalesce(i."qty",0)) > 0
422
+ then sum(coalesce(i."qty",0))
423
+ else 0 end
424
+ else 0 end
425
+ ) as "damageQty",
426
+ (
427
+ case when inv_loc."type" = 'STORAGE'
428
+ then
429
+ case when sum(coalesce(i."qty",0)) > 0
430
+ then sum(coalesce(i."qty",0))-sum(COALESCE(i."locked_qty", 0))
431
+ else 0 end
432
+ else 0 end
433
+ ) as "storageQty",
434
+ (
435
+ case when i."transfer_qty" > 0
436
+ then
437
+ sum(i."transfer_qty")
438
+ else 0 end
439
+ ) as "transferQty",
440
+ sum(coalesce(i."locked_qty", 0)) as "locked_qty"
441
+ from (
442
+ ${bizplaceQuery}
443
+ ) b
444
+ inner join bizplaces bz on bz.id = b.bizplace_id
445
+ inner join companies c on c.id = bz.company_id
446
+ inner join bizplaces bz2 on bz2.domain_id = c.domain_id
447
+ inner join products p on (p.bizplace_id = b.bizplace_id::uuid or p.bizplace_id = bz2.id)
448
+ inner join product_details pd on pd.product_id = p.id and pd.is_default
449
+ left join product_detail_bizplace_settings pdbs on pdbs.product_detail_id = pd.id
450
+ left join inventories i on i.bizplace_id =b.bizplace_id and i.product_id = p.id and i.qty >= 0 and i.status <> 'MISSING' and i.status <> 'PUTTING_AWAY' and
451
+ i.domain_id = $1
452
+ left join product_detail_stocks pds on pds.product_detail_id = pd.id and pds.domain_id = i.domain_id
453
+ left join bizplaces inv_bz on inv_bz.id = i.bizplace_id
454
+ left join locations inv_loc on inv_loc.id = i.location_id
455
+ where 1=1
456
+ ${productQuery}
457
+ group by p."id", coalesce(inv_bz."id", bz2."id"), coalesce(inv_bz."name", bz2."name"), pdbs."id", pd."id",
458
+ i."packing_type", i."packing_size", i."uom", i."transfer_qty", i."expiration_date", i."obsolete",
459
+ inv_loc."type"
452
460
  )`,
453
461
  [domain.id]
454
462
  )
@@ -464,7 +472,7 @@ export class InventoryQuery {
464
472
  SUM("quarantineQty") AS "quarantineQty",
465
473
  SUM("reserveQty") AS "reserveQty",
466
474
  COALESCE("opd"."total_release_qty", 0) as "draftQty",
467
- SUM("tih"."locked_qty") + "pds"."unassigned_qty" as "releaseQty",
475
+ SUM(COALESCE("tih"."locked_qty",0)) + COALESCE("pds"."unassigned_qty",0) as "releaseQty",
468
476
  COALESCE("opd"."total_release_qty", 0) - SUM("availableQty") - COALESCE("putawayinv"."putaway_qty", 0) AS "insufficientQty",
469
477
  SUM("availableQty") + COALESCE("putawayinv"."putaway_qty", 0) - COALESCE("pds"."unassigned_qty",0) AS "availableQty",
470
478
  SUM("quarantineQty" + "reserveQty" + "availableQty") + COALESCE("putawayinv"."putaway_qty", 0) - COALESCE("opd"."total_release_qty", 0) AS "warehouseQty",
@@ -486,7 +494,7 @@ export class InventoryQuery {
486
494
  GROUP BY "op"."product_id","op"."packing_type" ,"op"."packing_size", "op"."uom"
487
495
  ) AS "opd"
488
496
  ON "tih"."id" = "opd"."product_id"
489
- AND "tih"."packingType" = "opd"."packing_type"
497
+ AND "tih"."packingType" = "opd"."packing_type"
490
498
  LEFT JOIN
491
499
  (
492
500
  SELECT SUM(i.qty) as "putaway_qty", i.product_id, i.packing_type, i.packing_size, i.uom FROM "inventories" i
@@ -505,6 +513,8 @@ export class InventoryQuery {
505
513
  const results: any = await tx.query(
506
514
  `
507
515
  SELECT * FROM grouped_inventories_product
516
+ WHERE 1 = 1
517
+ ${availableStockQuery}
508
518
  ORDER BY "bizplaceName", "sku"
509
519
  OFFSET $1 LIMIT $2
510
520
  `,