@things-factory/sales-base 4.3.342 → 4.3.344

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,12 +1,31 @@
1
- import { Resolver, Query, FieldResolver, Root, Args, Arg, Ctx, Directive } from 'type-graphql'
2
- import { getRepository, EntityManager, SelectQueryBuilder } from 'typeorm'
3
- import { Domain, ListParam, convertListParams } from '@things-factory/shell'
1
+ import {
2
+ Arg,
3
+ Args,
4
+ Ctx,
5
+ FieldResolver,
6
+ Query,
7
+ Resolver,
8
+ Root
9
+ } from 'type-graphql'
10
+ import {
11
+ Brackets,
12
+ getRepository
13
+ } from 'typeorm'
14
+
4
15
  import { User } from '@things-factory/auth-base'
16
+ import {
17
+ convertListParams,
18
+ Domain,
19
+ Filter,
20
+ ListParam,
21
+ Pagination,
22
+ Sorting
23
+ } from '@things-factory/shell'
24
+
25
+ import { OrderInventory } from '../order-inventory/order-inventory'
26
+ import { OrderInventoryList } from '../order-inventory/order-inventory-types'
5
27
  import { Replenishment } from './replenishment'
6
28
  import { ReplenishmentList } from './replenishment-type'
7
- import { OrderInventory } from '../order-inventory/order-inventory'
8
- import { Inventory } from '@things-factory/warehouse-base'
9
- import { ORDER_INVENTORY_STATUS } from 'server/constants'
10
29
 
11
30
  @Resolver(Replenishment)
12
31
  export class ReplenishmentQuery {
@@ -59,6 +78,91 @@ export class ReplenishmentQuery {
59
78
  }
60
79
  }
61
80
 
81
+ @Query(returns => OrderInventoryList)
82
+ async replenishmentReport(
83
+ @Ctx() context: any,
84
+ @Arg('filters', type => [Filter], { nullable: true }) filters?: Filter[],
85
+ @Arg('pagination', type => Pagination, { nullable: true }) pagination?: Pagination,
86
+ @Arg('sortings', type => [Sorting], { nullable: true }) sortings?: Sorting[]
87
+ ): Promise<OrderInventoryList> {
88
+
89
+ let bizplaceFilter = filters.find(itm => itm.name === 'bizplace')
90
+ let orderNoFilter = filters.find(item => item.name === 'orderNo')
91
+ let statusFilter = filters.find(itm => itm.name === 'status')
92
+ let productInfoFilter = filters.find(itm => itm.name === 'productInfo')
93
+ let pickingBinFilter = filters.find(itm => itm.name === 'pickingBin')
94
+
95
+ let productFilterValue: string
96
+ if (productInfoFilter) {
97
+ productFilterValue = `%${productInfoFilter.value.toLowerCase()}%`
98
+ }
99
+
100
+ let pickingBinValues: string[] = []
101
+ if (pickingBinFilter) {
102
+ pickingBinValues = pickingBinFilter.value.split(',').map(value => value.trim())
103
+ }
104
+
105
+ const qb = getRepository(OrderInventory).createQueryBuilder('oi')
106
+
107
+ qb.select('r.name', 'orderNo')
108
+ .addSelect('p.sku', 'productSKU')
109
+ .addSelect(`string_agg(l.name, ', ')`, 'pickingBin')
110
+ .addSelect('sum(oi.release_qty)', 'transferQty')
111
+ .addSelect('r.status', 'status')
112
+ .addSelect('biz.id', 'bizplaceId')
113
+ .addSelect('biz.name', 'bizplaceName')
114
+ .leftJoin('oi.bizplace', 'biz')
115
+ .leftJoin('oi.binLocation', 'l')
116
+ .innerJoin('oi.product', 'p')
117
+ .innerJoin('oi.replenishment', 'r')
118
+ .where('oi.domain_id = :domainId', { domainId: context.state.domain.id })
119
+ .andWhere(`r.status NOT IN (:...statuses)`, { statuses: ['DONE', 'CANCELLED'] })
120
+
121
+ bizplaceFilter ?
122
+ qb.andWhere('biz.id = :bizplaceId', { bizplaceId: bizplaceFilter.value }) : ''
123
+
124
+ orderNoFilter ?
125
+ qb.andWhere('r.name ilike :orderNo', { orderNo: orderNoFilter.value }) : ''
126
+
127
+ statusFilter ?
128
+ qb.andWhere('r.status = :status', { status: statusFilter.value }) : ''
129
+
130
+ productInfoFilter ?
131
+ qb.andWhere(
132
+ new Brackets(qb => {
133
+ qb.where('p.name ilike :productInfo', { productInfo: productFilterValue })
134
+ .orWhere('p.sku ilike :productInfo', { productInfo: productFilterValue })
135
+ .orWhere('p.description ilike :productInfo', { productInfo: productFilterValue })
136
+ .orWhere('p.brand ilike :productInfo', { productInfo: productFilterValue })
137
+ .orWhere('p.brand_sku ilike :productInfo', { productInfo: productFilterValue })
138
+ })
139
+ ) : ''
140
+
141
+ pickingBinFilter ?
142
+ qb.andWhere(`l.name in (:...pickingBins)`, { pickingBins: pickingBinValues }) : ''
143
+
144
+ qb.groupBy('p.sku')
145
+ .addGroupBy('oi.replenishment_id')
146
+ .addGroupBy('r.id')
147
+ .addGroupBy('biz.id')
148
+ .orderBy('r.name')
149
+
150
+ let items = await qb.getRawMany()
151
+ let total = items.length
152
+
153
+ items = items.map(itm => {
154
+ return {
155
+ ...itm,
156
+ bizplace: {
157
+ id: itm.bizplaceId,
158
+ name: itm.bizplaceName
159
+ }
160
+ }
161
+ })
162
+ return { items, total }
163
+ }
164
+
165
+
62
166
  @FieldResolver(type => Domain)
63
167
  async domain(@Root() replenishment: Replenishment): Promise<Domain> {
64
168
  return await getRepository(Domain).findOne(replenishment.domainId)
@@ -107,8 +107,14 @@ export const InventoryUtil = {
107
107
  CASE WHEN (COALESCE(SUM(COALESCE(i.qty, 0)) - GREATEST(SUM(COALESCE(i.locked_qty, 0)), 0) - GREATEST(COALESCE(pds.unassigned_qty, 0), 0) - MAX(COALESCE(oi.release_qty, 0)) - MAX(COALESCE(bp.bundle_product_release_qty, 0)),0)) < 0 THEN 0
108
108
  ELSE COALESCE(SUM(COALESCE(i.qty, 0)) - GREATEST(SUM(COALESCE(i.locked_qty, 0)), 0) - GREATEST(COALESCE(pds.unassigned_qty, 0), 0) - MAX(COALESCE(oi.release_qty, 0)) - MAX(COALESCE(bp.bundle_product_release_qty, 0)),0) END
109
109
  ) AS "remainQty",
110
- COALESCE(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - 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",
111
- concat(SUM(COALESCE(i.uom_value, 0)) - SUM(COALESCE(i.locked_uom_value, 0)) - 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",
110
+ (
111
+ CASE WHEN (COALESCE(SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - GREATEST(COALESCE(pds.unassigned_uom_value, 0), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)),0)) < 0 THEN 0
112
+ ELSE COALESCE(SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - GREATEST(COALESCE(pds.unassigned_uom_value, 0), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)),0) END
113
+ ) AS "remainUomValue",
114
+ (
115
+ CASE WHEN (SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - GREATEST(COALESCE(pds.unassigned_uom_value, 0), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0))) < 0 THEN concat(0, ' ', pd.uom)
116
+ ELSE concat(SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - GREATEST(COALESCE(pds.unassigned_uom_value, 0), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)), ' ', pd.uom) END
117
+ ) AS "remainUomValueWithUom",
112
118
  COALESCE(SUM(COALESCE(i.transfer_qty, 0)), 0) AS "transferQty",
113
119
  COALESCE(SUM(COALESCE(i.transfer_uom_value, 0)), 0) AS "transferUomValue",
114
120
  'SINGLE' AS "groupType"
@@ -166,7 +172,10 @@ export const InventoryUtil = {
166
172
  CASE WHEN ((SUM(COALESCE(i2.qty, 0)) - GREATEST(SUM(COALESCE(i2.locked_qty, 0)), 0) - GREATEST(COALESCE(pds2.unassigned_qty, 0), 0) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty)) < 0 THEN 0
167
173
  ELSE (SUM(COALESCE(i2.qty, 0)) - GREATEST(SUM(COALESCE(i2.locked_qty, 0)), 0) - GREATEST(COALESCE(pds2.unassigned_qty, 0), 0) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty) END
168
174
  ) AS "availableQty",
169
- (SUM(COALESCE(i2.uom_value, 0)) - SUM(COALESCE(i2.locked_uom_value, 0)) - COALESCE(pds2.unassigned_uom_value, 0) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue",
175
+ (
176
+ CASE WHEN ((SUM(COALESCE(i2.uom_value, 0)) - GREATEST(SUM(COALESCE(i2.locked_uom_value, 0)), 0) - GREATEST(COALESCE(pds2.unassigned_uom_value, 0), 0) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty)) < 0 THEN 0
177
+ ELSE (SUM(COALESCE(i2.uom_value, 0)) - GREATEST(SUM(COALESCE(i2.locked_uom_value, 0)), 0) - GREATEST(COALESCE(pds2.unassigned_uom_value, 0), 0) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) END
178
+ ) AS "availableUomValue",
170
179
  (SUM(COALESCE(i2.transfer_qty, 0)) / min(pbs.bundle_qty)) AS "transferQty",
171
180
  (SUM(COALESCE(i2.transfer_uom_value, 0)) / min(pbs.bundle_qty)) AS "transferUomValue",
172
181
  pbs.product_detail_id
@@ -299,8 +308,14 @@ export const InventoryUtil = {
299
308
  p.id AS "productId",
300
309
  i.product_detail_id AS "productDetailId",
301
310
  SUM(COALESCE(i.qty, 0)) - GREATEST(SUM(COALESCE(i.locked_qty, 0)), 0) - MAX(COALESCE(oi.release_qty, 0)) - MAX(COALESCE(bp.bundle_product_release_qty, 0)) - GREATEST(SUM(COALESCE(pds.unassigned_qty, 0)), 0) AS "remainQty",
302
- 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, 0)) AS "remainUomValue",
303
- 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, 0)), ' ', i.uom) AS "remainUomValueWithUom",
311
+ (
312
+ CASE WHEN (SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)) - GREATEST(SUM(COALESCE(pds.unassigned_uom_value, 0)), 0)) < 0 THEN 0
313
+ ELSE SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)) - GREATEST(SUM(COALESCE(pds.unassigned_uom_value, 0)), 0) END
314
+ ) AS "remainUomValue",
315
+ (
316
+ CASE WHEN (SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)) - GREATEST(SUM(COALESCE(pds.unassigned_uom_value, 0)), 0)) < 0 THEN concat(0, ' ', i.uom)
317
+ ELSE concat(SUM(COALESCE(i.uom_value, 0)) - GREATEST(SUM(COALESCE(i.locked_uom_value, 0)), 0) - MAX(COALESCE(oi.release_uom_value, 0)) - MAX(COALESCE(bp.bundle_product_release_uom_value, 0)) - GREATEST(SUM(COALESCE(pds.unassigned_uom_value, 0)), 0), ' ', i.uom) END
318
+ ) AS "remainUomValueWithUom",
304
319
  'SINGLE' AS "groupType"
305
320
  FROM
306
321
  inventories i
@@ -339,7 +354,7 @@ export const InventoryUtil = {
339
354
  INNER JOIN (
340
355
  SELECT pbs.product_id, pbs.product_bundle_id, min(pbs.bundle_qty),
341
356
  (SUM(COALESCE(i2.qty, 0)) - GREATEST(SUM(COALESCE(i2.locked_qty, 0)), 0) - MAX(COALESCE(oi.release_qty, 0))) / min(pbs.bundle_qty) AS "availableQty",
342
- (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",
357
+ (SUM(COALESCE(i2.uom_value, 0)) - GREATEST(SUM(COALESCE(i2.locked_uom_value, 0)), 0) - MAX(COALESCE(oi.release_uom_value, 0))) / min(pbs.bundle_qty) AS "availableUomValue",
343
358
  pbs.product_detail_id
344
359
  FROM product_bundle_settings pbs
345
360
  LEFT JOIN inventories i2 ON i2.product_id = pbs.product_id AND i2.domain_id = $1