@things-factory/sales-base 4.3.212 → 4.3.214

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.
@@ -212,11 +212,11 @@ export class OtherQuery {
212
212
  productDetail: availableItem ? { id: availableItem.productDetailId } : null,
213
213
  inventory: availableItem
214
214
  ? {
215
- ...availableItem,
216
- remainQty: availableItem ? availableItem.remainQty : null,
217
- remainUomValue: availableItem ? availableItem.remainUomValue : null,
218
- remainUomValueWithUom: availableItem ? availableItem.remainUomValueWithUom : ''
219
- }
215
+ ...availableItem,
216
+ remainQty: availableItem ? availableItem.remainQty : null,
217
+ remainUomValue: availableItem ? availableItem.remainUomValue : null,
218
+ remainUomValueWithUom: availableItem ? availableItem.remainUomValueWithUom : ''
219
+ }
220
220
  : null,
221
221
  productId: availableItem ? availableItem.productId : null,
222
222
  productDetailID: availableItem ? availableItem.productDetailId : null,
@@ -299,8 +299,6 @@ export class OtherQuery {
299
299
  const { domain, user, tx, bizplace }: { domain: Domain; user: User; tx: EntityManager; bizplace: Bizplace } =
300
300
  context.state
301
301
 
302
- const productDetailRepo = tx.getRepository(ProductDetail)
303
-
304
302
  let permittedBizplaces: Bizplace[] = await getPermittedBizplaces(domain, user)
305
303
  let foundPermittedBizplace: Bizplace
306
304
  let companyBizplace: Bizplace
@@ -336,90 +334,118 @@ export class OtherQuery {
336
334
 
337
335
  if (partnerStrictProductSelectionSetting) strictProduct = partnerStrictProductSelectionSetting.value
338
336
 
339
- let newOrderProducts: any[] = []
340
-
341
- for (var i = 0; i < orderProducts.length; i++) {
342
- const orderProduct = orderProducts[i]
343
- const productSKU: string = orderProduct.productSKU
344
- const foundProduct: Product = await tx.getRepository(Product).findOne({
345
- where: {
346
- sku: productSKU,
347
- bizplace: companyBizplace
337
+ const json_op = JSON.stringify(
338
+ orderProducts.map(orderProduct => {
339
+ return {
340
+ sku: orderProduct.productSKU,
341
+ packing_type: orderProduct.packingType,
342
+ pack_qty: orderProduct.packQty,
343
+ pallet_qty: orderProduct.palletQty,
344
+ unit_price: orderProduct.unitPrice,
345
+ batch_id: orderProduct.batchId,
346
+ uom: orderProduct.uom,
347
+ uom_value: orderProduct.uomValue
348
348
  }
349
349
  })
350
+ )
350
351
 
351
- let newOrderProduct: any
352
-
353
- if (foundProduct) {
354
- let productDetail: ProductDetail
355
- const hasConditions: boolean = Boolean(
356
- (orderProduct?.packingType && orderProduct?.packingType.trim() != '') ||
357
- (orderProduct?.uom && orderProduct?.uom.trim() != '') ||
358
- orderProduct?.uomValue
359
- )
360
- let productDetailCondition: any = { product: foundProduct }
361
-
362
- if (hasConditions && strictProduct == 'true') {
363
- if (orderProduct?.packingType && orderProduct.packingType.trim() != '')
364
- productDetailCondition.packingType = orderProduct.packingType.trim()
365
-
366
- if (orderProduct?.uom && orderProduct.uom.trim() != '') productDetailCondition.uom = orderProduct.uom.trim()
367
-
368
- if (orderProduct?.uomValue) productDetailCondition.uomValue = orderProduct.uomValue
369
- } else {
370
- productDetailCondition.isDefault = true
371
- }
372
-
373
- productDetail = await productDetailRepo.findOne({
374
- where: productDetailCondition
375
- })
376
-
377
- //check if is strict product selection for uom value, uom, packing type
378
- const uomValue: number =
379
- strictProduct == 'true' && productDetail ? productDetail.uomValue : orderProduct.uomValue
380
-
381
- const uom = strictProduct == 'true' && productDetail ? productDetail.uom : orderProduct.uom
382
-
383
- const packingType =
384
- strictProduct == 'true' && productDetail ? productDetail.packingType : orderProduct.packingType
385
-
386
- newOrderProduct = {
387
- ...orderProduct,
388
- packingType,
389
- packingSize: productDetail ? productDetail.packingSize : orderProduct.packingSize,
390
- uom,
391
- uomValue,
392
- productName: `${foundProduct.name}(${foundProduct.description}(${foundProduct.sku}))`,
393
- productBrand: foundProduct?.brand ? foundProduct.brand : null,
394
- product: {
395
- id: foundProduct.id,
396
- name: foundProduct.name,
397
- sku: foundProduct.sku,
398
- type: foundProduct.type,
399
- brand: foundProduct.brand,
400
- primaryUnit: productDetail ? productDetail.uom : null,
401
- primaryValue: productDetail ? productDetail.uomValue : null,
402
- costPrice: foundProduct.costPrice
403
- },
404
- productDetail,
405
- totalUomValue:
406
- orderProduct.packQty > 0
407
- ? uomValue
408
- ? `${(orderProduct.packQty * uomValue).toFixed(2)} ${uom}`
409
- : null
410
- : null,
411
- isError: productDetail ? false : true
412
- }
413
- } else {
414
- newOrderProduct = {
415
- ...orderProduct,
416
- productName: orderProduct.productSKU,
417
- isError: true
352
+ // create temp table to hold the input data
353
+ await tx.query(`
354
+ CREATE TEMP TABLE raw_order_products(
355
+ sku VARCHAR(150),
356
+ packing_type VARCHAR(50),
357
+ pack_qty FLOAT,
358
+ pallet_qty FLOAT,
359
+ unit_price FLOAT,
360
+ batch_id VARCHAR(50),
361
+ uom VARCHAR(10),
362
+ uom_value FLOAT
363
+ );
364
+ `)
365
+
366
+ // insert input data into temp table
367
+ await tx.query(`
368
+ INSERT INTO raw_order_products
369
+ SELECT * FROM JSON_POPULATE_RECORDSET(NULL::raw_order_products, $1) js
370
+ `, [json_op])
371
+
372
+ // find closest matching product details from input data
373
+ await tx.query(`
374
+ create temp table matching_product_details as (
375
+ select row_number() over(partition by rop.sku order by is_default desc) as rn,
376
+ rop.sku as "rop_sku", rop.packing_type as "rop_packing_type", rop.uom as "rop_uom", rop.uom_value as "rop_uom_value", pd.*
377
+ from product_details pd
378
+ inner join products p on p.id = pd.product_id AND p.bizplace_id = $1
379
+ inner join raw_order_products rop ON LOWER(rop.sku) = LOWER(p.sku)
380
+ where
381
+ pd.packing_type = coalesce(rop.packing_type, pd.packing_type)
382
+ and pd.uom = coalesce(rop.uom, pd.uom)
383
+ and pd.uom_value = coalesce(rop.uom_value, pd.uom_value)
384
+ )`, [companyBizplace.id])
385
+
386
+ // massage data into defined structure
387
+ let results = await tx.query(`
388
+ select *,
389
+ case when "packQty" > 0 and "uomValue" is not null then "packQty" * "uomValue" else null end as "totalUomValue"
390
+ from (
391
+ SELECT
392
+ js.sku,
393
+ js.pack_qty as "packQty",
394
+ js.pallet_qty as "palletQty",
395
+ js.unit_price as "unitPrice",
396
+ js.batch_id as "batchId",
397
+ p.id AS "productId",
398
+ p.name as "productName",
399
+ p.brand as "productBrand",
400
+ p.type as "productType",
401
+ coalesce(mpd.uom, pdd.uom) as "productPrimaryUnit",
402
+ coalesce(mpd.uom_value, pdd.uom_value) as "productPrimaryValue",
403
+ coalesce(mpd.packing_size, pdd.packing_size) as "packingSize",
404
+ ${strictProduct.toLowerCase() === 'false' ? `
405
+ coalesce(js.packing_type, mpd.packing_type, pdd.packing_type) as "packingType",
406
+ coalesce(js.uom, mpd.uom, js.uom) as "uom",
407
+ coalesce(js.uom_value, mpd.uom_value, js.uom_value) as "uomValue",
408
+ ` : `
409
+ coalesce(mpd.packing_type, pdd.packing_type) as "packingType",
410
+ coalesce(mpd.uom, js.uom) as "uom",
411
+ coalesce(mpd.uom_value, js.uom_value) as "uomValue",
412
+ `}
413
+ coalesce(mpd.id, pdd.id) AS "productDetailId",
414
+ coalesce(mpd.cost_price, pdd.cost_price) AS "productDetailCostPrice",
415
+ CASE WHEN p.id is null then js.sku else concat(p.name,' (',p.description,' (',js.sku,'))') end as "productName",
416
+ CASE WHEN p.id is null or coalesce(mpd.id, pdd.id) is null then true else false end as "isError"
417
+ FROM raw_order_products js
418
+ LEFT JOIN products p ON LOWER(js.sku) = LOWER(p.sku) AND p.bizplace_id = $1
419
+ left join matching_product_details mpd on mpd.rn = 1 and mpd.rop_sku = js.sku and mpd.rop_packing_type = js.packing_type and mpd.rop_uom = js.uom
420
+ left join product_details pdd on p.id = pdd.product_id and pdd.is_default = true
421
+ ) dt
422
+ `, [companyBizplace.id])
423
+
424
+ // remove all temp table
425
+ await tx.query(`DROP TABLE matching_product_details`)
426
+ await tx.query(`DROP TABLE raw_order_products`)
427
+
428
+ // build output structure
429
+ const newOrderProducts: any[] = results.map(result => {
430
+ return {
431
+ ...result,
432
+ product: {
433
+ id: result.productId,
434
+ name: result.productName,
435
+ sku: result.productSku,
436
+ description: result.productDescription,
437
+ type: result.productType,
438
+ brand: result.productBrand,
439
+ primaryUnit: result.productPrimaryUnit,
440
+ primaryValue: result.productPrimaryValue,
441
+ costPrice: result.productDetailCostPrice,
442
+ },
443
+ productDetail: {
444
+ id: result.productDetailId,
445
+ costPrice: result.productDetailCostPrice,
418
446
  }
419
447
  }
420
-
421
- newOrderProducts.push(newOrderProduct)
422
- }
448
+ })
423
449
 
424
450
  return newOrderProducts
425
451
  } catch (e) {
@@ -494,7 +520,7 @@ export class OtherQuery {
494
520
  let productDetail: ProductDetail
495
521
  const hasConditions: boolean = Boolean(
496
522
  (orderProduct?.packingType && orderProduct?.packingType.trim() != '') ||
497
- (orderProduct?.uom && orderProduct?.uom.trim() != '')
523
+ (orderProduct?.uom && orderProduct?.uom.trim() != '')
498
524
  )
499
525
  let productDetailCondition: any = { product: foundProduct }
500
526
 
@@ -519,8 +545,8 @@ export class OtherQuery {
519
545
  ? productDetail.uomValue
520
546
  : null
521
547
  : orderProduct?.uomValue
522
- ? orderProduct.uomValue
523
- : productDetail.uomValue
548
+ ? orderProduct.uomValue
549
+ : productDetail.uomValue
524
550
 
525
551
  newOrderProduct = {
526
552
  ...orderProduct,