@things-factory/warehouse-base 4.3.677 → 4.3.679

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.
@@ -235,7 +235,8 @@ export class InventoryHistoryQuery {
235
235
  OR Lower(prd.description) LIKE ANY(ARRAY[${productValue}])
236
236
  OR Lower(prd.brand) LIKE ANY(ARRAY[${productValue}])
237
237
  OR Lower(prd.brand_sku) LIKE ANY(ARRAY[${productValue}])
238
- )`
238
+ )
239
+ `
239
240
  }
240
241
 
241
242
  let productDescQuery = ''
@@ -247,12 +248,11 @@ export class InventoryHistoryQuery {
247
248
  if (hasTransactionOrBalanceFilter && hasTransactionOrBalanceFilter.value) {
248
249
  hasTransactionOrBalanceQuery = 'and (src.totalRow > 1 or src.totalQty <> 0)'
249
250
  }
250
-
251
251
  await tx.query(
252
252
  `
253
253
  create temp table temp_products AS
254
254
  (
255
- select prd.*, prd.id::varchar as product_id from products prd
255
+ select prd.id, prd.sku, prd.name, prd.description, prd.type, prd.aux_value_1 from products prd
256
256
  inner join bizplaces b on b.id = prd.bizplace_id
257
257
  inner join companies c on c.domain_id = b.domain_id
258
258
  inner join bizplaces b2 on b2.company_id = c.id
@@ -261,20 +261,20 @@ export class InventoryHistoryQuery {
261
261
  )`,
262
262
  [bizplace.id]
263
263
  )
264
-
265
264
  await tx.query(
266
265
  `
267
266
  create temp table temp_data_src AS
268
267
  (
269
268
  SELECT prd.sku as sku, prd.name AS product_name, prd.description AS product_description, prd.type as product_type, prd.aux_value_1 AS product_aux_value_1, trim(invh.batch_id) as batch_id, invh.product_id,
270
- invh.packing_type, invh.bizplace_id, invh.domain_id,
271
- invh.ref_order_id, invh.order_no, invh.order_ref_no, invh.transaction_type, invh.status, invh.created_at,
269
+ invh.packing_type, invh.bizplace_id,
270
+ invh.order_no, invh.order_ref_no, invh.transaction_type, invh.status, invh.created_at,
272
271
  TRUNC(CAST(invh.qty AS NUMERIC), 3) as qty, TRUNC(CAST(invh.opening_qty AS NUMERIC), 3) as opening_qty,
273
272
  invh.uom, TRUNC(CAST(COALESCE(invh.uom_value, 0) AS NUMERIC), 3) as uom_value, TRUNC(CAST(COALESCE(invh.opening_uom_value, 0) AS NUMERIC), 3) as opening_uom_value
274
- FROM reduced_inventory_histories invh
273
+ FROM inventory_histories invh
275
274
  INNER JOIN temp_products prd ON prd.id = invh.product_id::uuid
276
275
  WHERE
277
- invh.domain_id = $1
276
+ (invh.qty <> 0 OR invh.uom_value <> 0)
277
+ AND invh.domain_id = $1
278
278
  AND invh.bizplace_id = $2
279
279
  AND invh.created_at <= $3::timestamp
280
280
  ${batchNoQuery}
@@ -285,105 +285,94 @@ export class InventoryHistoryQuery {
285
285
 
286
286
  await tx.query(
287
287
  `
288
- create temp table temp_inv_history as (
289
- SELECT src.sku, src.product_name, src.product_description, src.product_type, src.product_aux_value_1, src.batch_id, src.product_id, src.packing_type, src.uom,
290
- src.bizplace_id, src.domain_id,
291
- TRUNC(CAST(SUM(COALESCE(src.qty,0)) AS NUMERIC), 3) AS qty,
292
- TRUNC(CAST(SUM(COALESCE(src.opening_qty,0)) AS NUMERIC), 3) AS opening_qty,
293
- TRUNC(CAST(SUM(COALESCE(src.uom_value,0)) AS NUMERIC), 3) AS uom_value,
294
- TRUNC(CAST(SUM(COALESCE(src.opening_uom_value,0)) AS NUMERIC), 3) AS opening_uom_value,
288
+ CREATE TEMP TABLE temp_inv_history AS (
289
+ SELECT
290
+ src.sku,
291
+ src.product_name,
292
+ src.product_description,
293
+ src.product_type,
294
+ src.product_aux_value_1,
295
+ src.batch_id,
296
+ src.product_id,
297
+ src.packing_type,
298
+ src.uom,
299
+ src.bizplace_id,
300
+ TRUNC(CAST(SUM(COALESCE(src.qty, 0)) AS NUMERIC), 3) AS qty,
301
+ TRUNC(CAST(SUM(COALESCE(src.opening_qty, 0)) AS NUMERIC), 3) AS opening_qty,
302
+ TRUNC(CAST(SUM(COALESCE(src.uom_value, 0)) AS NUMERIC), 3) AS uom_value,
303
+ TRUNC(CAST(SUM(COALESCE(src.opening_uom_value, 0)) AS NUMERIC), 3) AS opening_uom_value,
295
304
  'Opening Balance' AS order_name,
296
305
  '-' AS ref_no,
297
- '-' as transaction_type,
306
+ '-' AS transaction_type,
298
307
  0 AS rn,
299
- $1::timestamp AS created_at,
300
- $1::date AS created_date
301
- FROM temp_data_src src
302
- WHERE src.created_at < $1::timestamp
303
- GROUP BY src.sku, src.product_name, src.product_description, src.product_type, src.product_aux_value_1, src.batch_id, src.product_id, src.packing_type, src.uom,
304
- src.bizplace_id, src.domain_id
305
- UNION ALL
306
- SELECT sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
307
- domain_id, TRUNC(CAST(sum(qty) AS NUMERIC), 3) as qty, TRUNC(CAST(sum(opening_qty) AS NUMERIC), 3) as opening_qty, TRUNC(CAST(sum(uom_value) AS NUMERIC), 3) as uom_value, TRUNC(CAST(sum(opening_uom_value) AS NUMERIC), 3) as opening_uom_value,
308
- order_name, ref_no, '-' as transaction_type ,1 AS rn, MIN(created_at) - $2::interval as created_at, (MIN(created_at) - $2::interval)::DATE as created_date
309
- FROM (
310
- SELECT invh.sku, invh.product_name, invh.product_description, invh.product_type, invh.product_aux_value_1, invh.batch_id, invh.product_id, invh.packing_type, invh.bizplace_id,
311
- invh.domain_id,
312
- invh.qty, invh.opening_qty, invh.uom_value, invh.uom, invh.opening_uom_value,
313
- COALESCE(order_no, '-') AS order_name,
314
- COALESCE(order_ref_no, '-') AS ref_no,
315
- COALESCE(invh.transaction_type, '-') AS transaction_type,
316
- invh.created_at,
317
- invh.created_at::date as created_date
318
- FROM temp_data_src invh
319
- WHERE (invh.qty <> 0 OR invh.uom_value <> 0) AND
320
- invh.transaction_type <> 'ADJUSTMENT' AND
321
- invh.transaction_type <> 'PICKING' AND
322
- invh.transaction_type <> 'CANCEL_ORDER' AND
323
- invh.transaction_type <>'NEW'
324
- AND invh.created_at >= $1::timestamp
325
- ) AS inv_movement
326
- GROUP BY sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
327
- domain_id, order_name, ref_no, rn
328
- UNION ALL
329
- SELECT sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
330
- domain_id, TRUNC(CAST(sum(qty) AS NUMERIC), 3) as qty, TRUNC(CAST(sum(opening_qty) AS NUMERIC), 3) as opening_qty, TRUNC(CAST(sum(uom_value) AS NUMERIC), 3) as uom_value, TRUNC(CAST(sum(opening_uom_value) AS NUMERIC), 3) as opening_uom_value,
331
- CASE
332
- WHEN transaction_type IN ('PICKING', 'CANCEL_ORDER') THEN order_no
333
- ELSE order_name
334
- END AS order_name,
308
+ $1::timestamp AS created_at
309
+ FROM temp_data_src src
310
+ WHERE src.created_at < $1::timestamp
311
+ GROUP BY
312
+ src.sku,
313
+ src.product_name,
314
+ src.product_description,
315
+ src.product_type,
316
+ src.product_aux_value_1,
317
+ src.batch_id,
318
+ src.product_id,
319
+ src.packing_type,
320
+ src.uom,
321
+ src.bizplace_id
322
+
323
+ UNION ALL
324
+
325
+ -- Part 2: All Inventory Movements (combined into a single scan)
326
+ -- This SELECT statement replaces the three separate UNION ALLs for
327
+ -- transaction movements, using CASE statements to handle different
328
+ -- transaction types and statuses in a single pass.
329
+ SELECT
330
+ sku,
331
+ product_name,
332
+ product_description,
333
+ product_type,
334
+ product_aux_value_1,
335
+ batch_id,
336
+ product_id,
337
+ packing_type,
338
+ uom,
339
+ bizplace_id,
340
+ TRUNC(CAST(SUM(
341
+ -- qty and uom_value logic for all cases combined
342
+ CASE
343
+ WHEN transaction_type = 'ADJUSTMENT' AND status = 'MISSING' THEN -opening_qty
344
+ ELSE qty
345
+ END
346
+ ) AS NUMERIC), 3) AS qty,
347
+ TRUNC(CAST(SUM(opening_qty) AS NUMERIC), 3) AS opening_qty,
348
+ TRUNC(CAST(SUM(
349
+ CASE
350
+ WHEN transaction_type = 'ADJUSTMENT' AND status = 'MISSING' THEN -opening_uom_value
351
+ ELSE uom_value
352
+ END
353
+ ) AS NUMERIC), 3) AS uom_value,
354
+ TRUNC(CAST(SUM(opening_uom_value) AS NUMERIC), 3) AS opening_uom_value,
335
355
  CASE
336
- WHEN transaction_type IN ('PICKING', 'CANCEL_ORDER') THEN ref_no
337
- ELSE order_name
338
- END AS ref_no,
339
- transaction_type,
340
- 1 AS rn,
341
- created_at - $2::interval as created_at,
342
- created_at::date as created_date
343
- FROM (
344
- SELECT
345
- invh.sku, invh.product_name, invh.product_description, invh.product_type, invh.product_aux_value_1, invh.batch_id, invh.product_id,
346
- invh.packing_type, invh.uom, invh.bizplace_id, invh.domain_id,
347
- invh.qty, invh.opening_qty, invh.uom_value, invh.opening_uom_value,
348
- invh.transaction_type,
349
- invh.transaction_type AS order_name,
350
- order_no,
351
- order_ref_no AS ref_no,
352
- invh.created_at,
353
- invh.created_at::date as created_date
354
- FROM temp_data_src invh
355
- WHERE (invh.qty <> 0 OR invh.uom_value <> 0) AND
356
- (invh.transaction_type = 'ADJUSTMENT'
357
- OR invh.transaction_type = 'PICKING'
358
- OR invh.transaction_type = 'CANCEL_ORDER'
359
- OR invh.transaction_type = 'NEW')
360
- AND invh.created_at >= $1::timestamp
361
- ) AS inv_movement
362
- GROUP BY
363
- sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
364
- domain_id, inv_movement.transaction_type,inv_movement.order_no,order_name, ref_no, rn, created_at
365
- UNION ALL
366
- SELECT sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
367
- domain_id, TRUNC(CAST(sum(-opening_qty) AS NUMERIC), 3) as qty, TRUNC(CAST(sum(opening_qty) AS NUMERIC), 3) as opening_qty, TRUNC(CAST(sum(-opening_uom_value) AS NUMERIC), 3) as uom_value, TRUNC(CAST(sum(opening_uom_value) AS NUMERIC), 3) as opening_uom_value,
368
- order_name, ref_no, transaction_type,1 AS rn, created_at - $2::interval as created_at, created_at::date as created_date
369
- FROM (
370
- SELECT invh.sku, invh.product_name, invh.product_description, invh.product_type, invh.product_aux_value_1, invh.batch_id, invh.product_id, invh.packing_type, invh.uom, invh.bizplace_id,
371
- invh.domain_id,
372
- invh.qty, invh.opening_qty, invh.uom_value, invh.opening_uom_value,
373
- invh.transaction_type as transaction_type,
374
- invh.transaction_type AS order_name,
375
- invh.transaction_type AS ref_no,
376
- invh.created_at,
377
- invh.created_at:: date as created_date
378
- FROM temp_data_src invh
379
- WHERE
380
- invh.transaction_type = 'ADJUSTMENT'
381
- AND invh.status = 'MISSING'
382
- AND invh.created_at >= $1::timestamp
383
- ) AS inv_movement
384
- GROUP BY sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
385
- domain_id, order_name, ref_no, inv_movement.transaction_type,rn, created_at
386
- )`,
356
+ WHEN transaction_type IN ('PICKING', 'CANCEL_ORDER') THEN order_no
357
+ WHEN transaction_type IN ('ADJUSTMENT', 'NEW') THEN transaction_type
358
+ ELSE COALESCE(order_no, '-')
359
+ END AS order_name,
360
+ CASE
361
+ WHEN transaction_type IN ('PICKING', 'CANCEL_ORDER') THEN order_ref_no
362
+ WHEN transaction_type IN ('ADJUSTMENT', 'NEW') THEN transaction_type
363
+ ELSE COALESCE(order_ref_no, '-')
364
+ END AS ref_no,
365
+ transaction_type,
366
+ 1 AS rn,
367
+ MIN(created_at) - $2::interval AS created_at
368
+ FROM temp_data_src
369
+ WHERE
370
+ created_at >= $1::timestamp
371
+ GROUP BY
372
+ sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id,
373
+ packing_type, uom, bizplace_id,
374
+ order_name, ref_no, transaction_type
375
+ )`,
387
376
  [fromDate.value, tzoffset]
388
377
  )
389
378
 
@@ -393,7 +382,7 @@ export class InventoryHistoryQuery {
393
382
  result = await tx.query(
394
383
  `
395
384
  select sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
396
- domain_id, qty, opening_qty, round(uom_value::decimal,2) as uom_value, opening_uom_value, order_name, ref_no, transaction_type,created_at::date
385
+ qty, opening_qty, round(uom_value::decimal,2) as uom_value, opening_uom_value, order_name, ref_no, transaction_type,created_at::date
397
386
  from temp_inv_history invh where
398
387
  exists (
399
388
  select 1 from (
@@ -410,10 +399,10 @@ export class InventoryHistoryQuery {
410
399
  result = await tx.query(
411
400
  `
412
401
  select sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
413
- domain_id, sum(qty) as qty, sum(opening_qty) as opening_qty, sum(uom_value) as uom_value, sum(opening_uom_value) as opening_uom_value, order_name, ref_no, transaction_type, created_at::date
402
+ sum(qty) as qty, sum(opening_qty) as opening_qty, sum(uom_value) as uom_value, sum(opening_uom_value) as opening_uom_value, order_name, ref_no, transaction_type, created_at::date
414
403
  from(
415
404
  select sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
416
- domain_id, qty, opening_qty, TRUNC(CAST(uom_value AS NUMERIC), 3) as uom_value, opening_uom_value, order_name, ref_no, transaction_type,created_at::date
405
+ qty, opening_qty, TRUNC(CAST(uom_value AS NUMERIC), 3) as uom_value, opening_uom_value, order_name, ref_no, transaction_type,created_at
417
406
  from temp_inv_history invh where
418
407
  exists (
419
408
  select 1 from (
@@ -425,7 +414,7 @@ export class InventoryHistoryQuery {
425
414
  )
426
415
  ) foo
427
416
  GROUP BY sku, product_name, product_description, product_type, product_aux_value_1, batch_id, product_id, packing_type, uom, bizplace_id,
428
- domain_id, order_name, ref_no, transaction_type, created_at
417
+ order_name, ref_no, transaction_type, created_at
429
418
  ORDER BY sku asc, product_name asc, product_description asc, packing_type asc, batch_id asc, uom asc, created_at asc,
430
419
  CASE
431
420
  WHEN transaction_type = 'CANCEL_ORDER' THEN 2