@things-factory/warehouse-base 4.3.80-alpha.4 → 4.3.81

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.
Files changed (71) hide show
  1. package/dist-server/controllers/warehouse-controller.js.map +1 -1
  2. package/dist-server/service/inventory/inventory-mutation.js.map +1 -1
  3. package/dist-server/service/inventory/inventory-query.js +4 -10
  4. package/dist-server/service/inventory/inventory-query.js.map +1 -1
  5. package/dist-server/service/inventory/inventory-types.js.map +1 -1
  6. package/dist-server/service/inventory/inventory.js +1 -6
  7. package/dist-server/service/inventory/inventory.js.map +1 -1
  8. package/dist-server/service/inventory-change/inventory-change-mutation.js +5 -16
  9. package/dist-server/service/inventory-change/inventory-change-mutation.js.map +1 -1
  10. package/dist-server/service/inventory-change/inventory-change-query.js.map +1 -1
  11. package/dist-server/service/inventory-change/inventory-change-types.js.map +1 -1
  12. package/dist-server/service/inventory-change/inventory-change.js.map +1 -1
  13. package/dist-server/service/inventory-history/inventory-history-mutation.js.map +1 -1
  14. package/dist-server/service/inventory-history/inventory-history-query.js +1 -1
  15. package/dist-server/service/inventory-history/inventory-history-query.js.map +1 -1
  16. package/dist-server/service/inventory-history/inventory-history-types.js.map +1 -1
  17. package/dist-server/service/inventory-history/inventory-history.js.map +1 -1
  18. package/dist-server/service/inventory-item/inventory-item-mutation.js.map +1 -1
  19. package/dist-server/service/inventory-item/inventory-item-query.js.map +1 -1
  20. package/dist-server/service/inventory-item/inventory-item-type.js.map +1 -1
  21. package/dist-server/service/inventory-item/inventory-item.js +3 -8
  22. package/dist-server/service/inventory-item/inventory-item.js.map +1 -1
  23. package/dist-server/service/inventory-item-change/inventory-item-change-mutation.js.map +1 -1
  24. package/dist-server/service/inventory-item-change/inventory-item-change-query.js.map +1 -1
  25. package/dist-server/service/inventory-item-change/inventory-item-change-type.js.map +1 -1
  26. package/dist-server/service/inventory-item-change/inventory-item-change.js.map +1 -1
  27. package/dist-server/service/inventory-product/inventory-product-mutation.js.map +1 -1
  28. package/dist-server/service/inventory-product/inventory-product-query.js.map +1 -1
  29. package/dist-server/service/inventory-product/inventory-product-type.js.map +1 -1
  30. package/dist-server/service/inventory-product/inventory-product.js +3 -8
  31. package/dist-server/service/inventory-product/inventory-product.js.map +1 -1
  32. package/dist-server/service/location/location-mutation.js.map +1 -1
  33. package/dist-server/service/location/location-query.js.map +1 -1
  34. package/dist-server/service/location/location-types.js.map +1 -1
  35. package/dist-server/service/location/location.js.map +1 -1
  36. package/dist-server/service/movement/movement-mutation.js.map +1 -1
  37. package/dist-server/service/movement/movement-query.js.map +1 -1
  38. package/dist-server/service/movement/movement-types.js.map +1 -1
  39. package/dist-server/service/movement/movement.js.map +1 -1
  40. package/dist-server/service/pallet/pallet-mutation.js.map +1 -1
  41. package/dist-server/service/pallet/pallet-query.js.map +1 -1
  42. package/dist-server/service/pallet/pallet-types.js.map +1 -1
  43. package/dist-server/service/pallet/pallet.js.map +1 -1
  44. package/dist-server/service/pallet-count/pallet-count-mutation.js.map +1 -1
  45. package/dist-server/service/pallet-count/pallet-count-query.js.map +1 -1
  46. package/dist-server/service/pallet-count/pallet-count-types.js.map +1 -1
  47. package/dist-server/service/pallet-count/pallet-count.js.map +1 -1
  48. package/dist-server/service/pallet-history/pallet-history-mutation.js.map +1 -1
  49. package/dist-server/service/pallet-history/pallet-history-query.js.map +1 -1
  50. package/dist-server/service/pallet-history/pallet-history-types.js.map +1 -1
  51. package/dist-server/service/pallet-history/pallet-history.js.map +1 -1
  52. package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js.map +1 -1
  53. package/dist-server/service/tote/tote-mutation.js.map +1 -1
  54. package/dist-server/service/tote/tote-query.js.map +1 -1
  55. package/dist-server/service/tote/tote-types.js.map +1 -1
  56. package/dist-server/service/tote/tote.js.map +1 -1
  57. package/dist-server/service/warehouse/warehouse-mutation.js.map +1 -1
  58. package/dist-server/service/warehouse/warehouse-query.js.map +1 -1
  59. package/dist-server/service/warehouse/warehouse-types.js.map +1 -1
  60. package/dist-server/service/warehouse/warehouse.js.map +1 -1
  61. package/dist-server/utils/inventory-util.js +24 -83
  62. package/dist-server/utils/inventory-util.js.map +1 -1
  63. package/package.json +8 -8
  64. package/server/controllers/warehouse-controller.ts +1 -0
  65. package/server/service/inventory/inventory-query.ts +12 -25
  66. package/server/service/inventory/inventory.ts +0 -3
  67. package/server/service/inventory-change/inventory-change-mutation.ts +11 -18
  68. package/server/service/inventory-history/inventory-history-query.ts +1 -1
  69. package/server/service/inventory-item/inventory-item.ts +2 -9
  70. package/server/service/inventory-product/inventory-product.ts +1 -5
  71. package/server/utils/inventory-util.ts +51 -117
@@ -17,13 +17,13 @@ import { logger } from '@things-factory/env'
17
17
  export class InventoryQuery {
18
18
  /**
19
19
  * Combined single query resolver to perform extraction of data with or without pagination
20
- * @param context
21
- * @param filters
22
- * @param pagination
23
- * @param sortings
24
- * @param locationSortingRules
25
- * @param exportItem
26
- * @returns
20
+ * @param context
21
+ * @param filters
22
+ * @param pagination
23
+ * @param sortings
24
+ * @param locationSortingRules
25
+ * @param exportItem
26
+ * @returns
27
27
  */
28
28
  @Directive('@privilege(category: "inventory", privilege: "query")')
29
29
  @Directive('@transaction')
@@ -45,7 +45,7 @@ export class InventoryQuery {
45
45
  const remainOnlyParam = filters.find((filter: any) => filter.name == 'remainOnly')
46
46
  const bizplace = filters.find((filter: any) => filter.name === 'bizplace')
47
47
 
48
- filters = filters.filter(x => ['productInfo', 'remainOnly'].indexOf(x.name) < 0)
48
+ filters = filters.filter(x => (['productInfo', 'remainOnly']).indexOf(x.name) < 0)
49
49
 
50
50
  const params = { filters, pagination }
51
51
 
@@ -66,12 +66,7 @@ export class InventoryQuery {
66
66
 
67
67
  qb.leftJoinAndSelect('inventory.bizplace', 'bizplace')
68
68
  .leftJoinAndSelect('inventory.product', 'product')
69
- .leftJoinAndSelect('inventory.productDetail', 'productDetail')
70
- .leftJoinAndSelect(
71
- 'product.productDetails',
72
- 'productDetails',
73
- 'productDetails.id = inventory.product_detail_id'
74
- )
69
+ .leftJoinAndSelect('product.productDetails', 'productDetail', 'productDetail.id = inventory.product_detail_id')
75
70
  .leftJoinAndSelect('inventory.warehouse', 'warehouse')
76
71
  .leftJoinAndSelect('inventory.location', 'location')
77
72
  .leftJoinAndSelect('inventory.creator', 'creator')
@@ -82,10 +77,7 @@ export class InventoryQuery {
82
77
  subQuery => {
83
78
  return subQuery
84
79
  .select('inventoryItems.inventory_id', 'inventory_item_inventory_id')
85
- .addSelect(
86
- `SUM(case when "inventoryItems"."status" = 'STORED' then 1 else 0 end)`,
87
- 'inventory_item_count'
88
- )
80
+ .addSelect(`SUM(case when "inventoryItems"."status" = 'STORED' then 1 else 0 end)`, 'inventory_item_count')
89
81
  .addSelect(`string_agg(inventoryItems.serial_number, ', ')`, 'serial_numbers')
90
82
  .from('inventory_items', 'inventoryItems')
91
83
  .where(`inventoryItems.domain_id = :domainId`, { domainId: domain.id })
@@ -141,9 +133,7 @@ export class InventoryQuery {
141
133
  }
142
134
 
143
135
  if (locationSortingRules?.length > 0) {
144
- locationSortingRules.forEach(rule => {
145
- qb.addOrderBy(`location.${rule.name}`, rule.desc ? 'DESC' : 'ASC')
146
- })
136
+ locationSortingRules.forEach((rule) => { qb.addOrderBy(`location.${rule.name}`, rule.desc ? 'DESC' : 'ASC') })
147
137
  }
148
138
 
149
139
  // Fetch all row for exporting
@@ -655,7 +645,7 @@ export class InventoryQuery {
655
645
 
656
646
  const productBundleSettings: ProductBundleSetting[] = await getRepository(ProductBundleSetting).find({
657
647
  where: { productBundle: productBundleId },
658
- relations: ['product', 'productDetail', 'productBundle']
648
+ relations: ['product', 'productBundle']
659
649
  })
660
650
 
661
651
  if (!productBundleSettings.length) {
@@ -666,7 +656,6 @@ export class InventoryQuery {
666
656
  buildQuery(qb, params, context)
667
657
 
668
658
  qb.select('iv.product_id', 'productId')
669
- .addSelect('iv.product_detail_id', 'productDetailId')
670
659
  .addSelect('iv.batch_id', 'batchId')
671
660
  .addSelect('iv.batch_id_ref', 'batchIdRef')
672
661
  .addSelect('iv.packing_type', 'packingType')
@@ -695,7 +684,6 @@ export class InventoryQuery {
695
684
  productIds: productBundleSettings.map(productBundle => productBundle.product.id)
696
685
  })
697
686
  .groupBy('iv.product_id')
698
- .addGroupBy('iv.product_detail_id')
699
687
  .addGroupBy('iv.batch_id')
700
688
  .addGroupBy('iv.batch_id_ref')
701
689
  .addGroupBy('iv.packing_type')
@@ -724,7 +712,6 @@ export class InventoryQuery {
724
712
  return {
725
713
  id: pbs.id,
726
714
  productId: pbs.product.id,
727
- productDetailId: pbs.productDetail.id,
728
715
  bundleId: pbs.productBundle.id,
729
716
  bundleQty: pbs.bundleQty,
730
717
  releaseQty: bundleReleaseQty * pbs.bundleQty,
@@ -210,9 +210,6 @@ export class Inventory {
210
210
  @Field({ nullable: true })
211
211
  productId: string
212
212
 
213
- @Field({ nullable: true })
214
- productDetailId: string
215
-
216
213
  @Field({ nullable: true })
217
214
  productName: string
218
215
 
@@ -6,7 +6,8 @@ import { Bizplace } from '@things-factory/biz-base'
6
6
  import { generateId } from '@things-factory/id-rule-base'
7
7
  import { MarketplaceStore } from '@things-factory/integration-marketplace'
8
8
  import { Sellercraft, SellercraftStatus } from '@things-factory/integration-sellercraft'
9
- import { Product, ProductDetail } from '@things-factory/product-base'
9
+ import { sendNotification } from '@things-factory/notification'
10
+ import { Product } from '@things-factory/product-base'
10
11
  import { PartnerSetting, Setting } from '@things-factory/setting-base'
11
12
  import { Domain } from '@things-factory/shell'
12
13
 
@@ -137,18 +138,14 @@ export class InventoryChangeMutation {
137
138
  ) {
138
139
  const { domain, user, tx }: { domain: Domain; user: User; tx: EntityManager } = context.state
139
140
  try {
141
+
140
142
  const _createRecords = []
141
143
  const _updateRecords = []
142
144
 
143
145
  for (let i = 0; i < patches.length; i++) {
144
- let foundExistingPallet = await tx
145
- .getRepository(Inventory)
146
- .createQueryBuilder('iv')
146
+ let foundExistingPallet = await tx.getRepository(Inventory).createQueryBuilder('iv')
147
147
  .where('iv.domain_id = :domain_id', { domain_id: domain.id })
148
- .andWhere('(iv.pallet_id = :pallet_id or iv.id = :id)', {
149
- pallet_id: patches[i]?.palletId,
150
- id: patches[i]?.id
151
- })
148
+ .andWhere('(iv.pallet_id = :pallet_id or iv.id = :id)', { pallet_id: patches[i]?.palletId, id: patches[i]?.id })
152
149
  .getOne()
153
150
 
154
151
  //remove any unchanged records
@@ -196,9 +193,7 @@ export class InventoryChangeMutation {
196
193
  newRecord.bizplace = await tx.getRepository(Bizplace).findOne(newRecord.bizplace.id)
197
194
 
198
195
  var product: Product = await tx.getRepository(Product).findOne(newRecord.product.id)
199
- var productDetail: ProductDetail = await tx.getRepository(ProductDetail).findOne(newRecord.productDetailId)
200
196
  newRecord.product = product
201
- newRecord.productDetail = productDetail
202
197
 
203
198
  newRecord.status = 'PENDING'
204
199
  newRecord.transactionType = 'NEW'
@@ -360,9 +355,7 @@ export class InventoryChangeMutation {
360
355
  newInventoryItemChange.status = updatedInventoryChange.status
361
356
  newInventoryItemChange.inventory = updatedInventoryChange.inventory
362
357
  newInventoryItemChange.inventoryItem =
363
- newInventoryItemChange?.type == INVENTORY_ITEM_CHANGE_TYPE.NEW
364
- ? null
365
- : inventoryItemChange.inventoryItem
358
+ newInventoryItemChange?.type == INVENTORY_ITEM_CHANGE_TYPE.NEW ? null : inventoryItemChange.inventoryItem
366
359
  newInventoryItemChange.inventoryChange = updatedInventoryChange
367
360
  newInventoryItemChange.domain = domain
368
361
  newInventoryItemChange.creator = user
@@ -424,9 +417,11 @@ export class InventoryChangeMutation {
424
417
  // }
425
418
 
426
419
  return true
420
+
427
421
  } catch (error) {
428
422
  throw error
429
423
  }
424
+
430
425
  }
431
426
 
432
427
  @Directive('@privilege(category: "inventory_adjustment_approval", privilege: "mutation")')
@@ -525,7 +520,6 @@ async function upsertInventoryItems(context, inventoryChange, approvalStatus, tx
525
520
  inventoryItem.serialNumber = inventoryItemChange.serialNumber
526
521
  inventoryItem.status = INVENTORY_STATUS.STORED
527
522
  inventoryItem.product = inventoryChange.inventory.product
528
- inventoryItem.productDetail = inventoryChange.inventory.productDetail
529
523
  inventoryItem.domain = inventoryItemChange.domain
530
524
  inventoryItem.inventory = inventoryChange.inventory
531
525
  inventoryItem.source = INVENTORY_ITEM_SOURCE.ADJUSTMENT
@@ -545,8 +539,7 @@ async function upsertInventoryItems(context, inventoryChange, approvalStatus, tx
545
539
  inventoryItem = {
546
540
  ...existingInventoryItem,
547
541
  serialNumber: inventoryItemChange.serialNumber,
548
- product: inventoryItemChange.inventory.product,
549
- productDetail: inventoryItemChange.inventory.productDetail
542
+ product: inventoryItemChange.inventory.product
550
543
  }
551
544
  } else if (changeType == INVENTORY_ITEM_CHANGE_TYPE.REMOVED) {
552
545
  inventoryItem = {
@@ -583,7 +576,6 @@ export async function approveInventoryChanges(patches: any, context: any) {
583
576
  'bizplace.company',
584
577
  'bizplace.company.domain',
585
578
  'product',
586
- 'productDetail',
587
579
  'product.productDetails',
588
580
  'product.productDetails.childProductDetail',
589
581
  'location'
@@ -619,7 +611,6 @@ export async function approveInventoryChanges(patches: any, context: any) {
619
611
  'bizplace.company',
620
612
  'bizplace.company.domain',
621
613
  'product',
622
- 'productDetail',
623
614
  'product.productDetails',
624
615
  'product.productDetails.childProductDetail',
625
616
  'warehouse',
@@ -629,6 +620,8 @@ export async function approveInventoryChanges(patches: any, context: any) {
629
620
  ]
630
621
  })
631
622
 
623
+ const customerDomain: Domain = inventory.bizplace.domain
624
+
632
625
  // Check for locked inventory, and stop from udpating inventory
633
626
  if (inventory.qty < inventory.lockedQty) {
634
627
  arrLockedInventory.push(_inventoryChanges[i])
@@ -1425,7 +1425,7 @@ export class InventoryHistoryQuery {
1425
1425
  rih.bizplace_id as bizplace_id,
1426
1426
  cast(rih.created_at + (interval '8 hour') as DATE) as "tempDate"
1427
1427
  from reduced_inventory_histories rih
1428
- left join product_details pd on pd.id = rih.product_detail_id
1428
+ left join product_details pd on pd.product_id = rih.product_id and pd.packing_type = rih.packing_type
1429
1429
  where rih.status not like 'TERMINATED' and rih.bizplace_id = $1
1430
1430
  group by CAST(rih.created_at + (interval '8 hour') as DATE), rih.bizplace_id
1431
1431
  )
@@ -11,7 +11,7 @@ import {
11
11
  } from 'typeorm'
12
12
 
13
13
  import { User } from '@things-factory/auth-base'
14
- import { Product, ProductDetail} from '@things-factory/product-base'
14
+ import { Product } from '@things-factory/product-base'
15
15
  import { Domain } from '@things-factory/shell'
16
16
 
17
17
  import { Inventory } from '../inventory/inventory'
@@ -32,8 +32,7 @@ export class InventoryItem {
32
32
 
33
33
  @RelationId((inventoryItem: InventoryItem) => inventoryItem.domain)
34
34
  domainId?: string
35
-
36
-
35
+
37
36
  @Column()
38
37
  @Field()
39
38
  name: string
@@ -58,19 +57,13 @@ export class InventoryItem {
58
57
  @Field({ nullable: true })
59
58
  outboundOrderId: string
60
59
 
61
-
62
60
  @ManyToOne(type => Product)
63
61
  @Field(type => Product, { nullable: true })
64
62
  product?: Product
65
63
 
66
-
67
64
  @RelationId((inventoryItem: InventoryItem) => inventoryItem.product)
68
65
  productId?: string
69
66
 
70
- @ManyToOne(type => ProductDetail, { nullable: true })
71
- @Field({ nullable: true })
72
- productDetail: ProductDetail
73
-
74
67
  @ManyToOne(type => Inventory)
75
68
  @Field(type => Inventory)
76
69
  inventory?: Inventory
@@ -11,7 +11,7 @@ import {
11
11
  } from 'typeorm'
12
12
 
13
13
  import { User } from '@things-factory/auth-base'
14
- import { Product, ProductDetail } from '@things-factory/product-base'
14
+ import { Product } from '@things-factory/product-base'
15
15
  import { Domain } from '@things-factory/shell'
16
16
 
17
17
  @Entity()
@@ -60,10 +60,6 @@ export class InventoryProduct {
60
60
  @RelationId((inventoryProduct: InventoryProduct) => inventoryProduct.product)
61
61
  productId?: string
62
62
 
63
- @ManyToOne(type => ProductDetail, { nullable: true })
64
- @Field({ nullable: true })
65
- productDetail: ProductDetail
66
-
67
63
  @CreateDateColumn()
68
64
  @Field({ nullable: true })
69
65
  createdAt?: Date
@@ -147,134 +147,68 @@ export async function generateInventoryHistory(
147
147
  }
148
148
  })
149
149
 
150
- let lastSeq: number
151
- let res: any
152
- let inventoryHistoryFields: any = {
150
+ let seq: number = 0
151
+ let openingQty: number = 0
152
+ let openingUomValue: number = 0
153
+
154
+ if (lastInvHistory) {
155
+ openingQty = lastInvHistory.openingQty + lastInvHistory.qty
156
+ openingUomValue = lastInvHistory.openingUomValue + lastInvHistory.uomValue
157
+ seq = lastInvHistory.seq + 1
158
+ }
159
+
160
+ let remainQty = (openingQty || 0) + (qty || 0)
161
+
162
+ let inventoryHistory: any = new InventoryHistory()
163
+
164
+ inventoryHistory = {
165
+ ...inventory,
166
+ name: InventoryNoGenerator.inventoryHistoryName(),
167
+ inventory,
168
+ seq: seq,
169
+ status: remainQty == 0 ? INVENTORY_STATUS.TERMINATED : inventory.status,
170
+ transactionType,
153
171
  refOrderId: refOrder?.id || null,
154
172
  orderNo: refOrder?.name || null,
155
173
  orderRefNo: refOrder?.refNo || null,
156
- uom: inventory?.uom,
157
-
174
+ qty,
175
+ openingQty,
176
+ uomValue,
177
+ openingUomValue: openingUomValue,
158
178
  creator: user,
159
- updater: user,
160
- domain: inventory?.domain,
161
- bizplace: inventory?.bizplace,
162
- reusablePallet: inventory?.reusablePallet,
163
- inventory: inventory,
164
- product: inventory?.product,
165
- warehouse: inventory?.warehouse,
166
- location: inventory?.location,
167
-
168
- palletId: inventory?.palletId,
169
- batchId: inventory?.batchId,
170
- zone: inventory?.zone,
171
- packingType: inventory?.packingType,
172
- description: inventory?.description,
173
- expirationDate: inventory?.expirationDate,
174
- unitCost: inventory?.unitCost,
175
- batchIdRef: inventory?.batchIdRef,
176
- cartonId: inventory?.cartonId,
177
- manufactureYear: inventory?.manufactureYear,
178
- packingSize: inventory?.packingSize,
179
- manufactureDate: inventory?.manufactureDate
179
+ updater: user
180
180
  }
181
181
 
182
- if (!lastInvHistory) {
183
- res = await invHistoryRepo
184
- .createQueryBuilder()
185
- .insert()
186
- .into(InventoryHistory)
187
- .values([
188
- {
189
- ...inventoryHistoryFields,
190
- name: InventoryNoGenerator.inventoryHistoryName(),
191
- status: inventory.status,
192
- transactionType,
193
- qty,
194
- openingQty: 0,
195
- uomValue,
196
- openingUomValue: 0,
197
- seq: 0
198
- }
199
- ])
200
- .returning('*')
201
- .execute()
202
- }
203
- else {
204
- if (transactionType == 'PICKING' || transactionType == 'UNLOADING') {
205
- const findSameOrderHistory: InventoryHistory[] = await invHistoryRepo.find({
206
- domain: inventory.domain,
207
- palletId: inventory.palletId,
208
- refOrderId: refOrder.id
209
- })
210
-
211
- if (findSameOrderHistory.length > 0) {
212
- let prevTotalQty = 0
213
- let prevTotalUomValue = 0
214
- for (let oh of findSameOrderHistory) {
215
- prevTotalQty += oh.qty
216
- prevTotalUomValue += oh.uomValue
217
- }
218
- qty -= prevTotalQty
219
- uomValue -= prevTotalUomValue
220
- }
182
+ delete inventoryHistory.updatedAt
183
+ delete inventoryHistory.createdAt
184
+ delete inventoryHistory.id
185
+
186
+ let newInventoryHistory = await invHistoryRepo.save(inventoryHistory)
187
+
188
+ if (remainQty == 0) {
189
+ seq = seq + 1
190
+ let terminatedHistory = {
191
+ ...newInventoryHistory,
192
+ name: InventoryNoGenerator.inventoryHistoryName(),
193
+ seq: seq,
194
+ status: INVENTORY_STATUS.TERMINATED,
195
+ transactionType: INVENTORY_TRANSACTION_TYPE.TERMINATED,
196
+ uom: inventory.uom,
197
+ qty: 0,
198
+ openingQty: 0,
199
+ uomValue: 0,
200
+ openingUomValue: 0
221
201
  }
222
-
223
- res = await invHistoryRepo
224
- .createQueryBuilder()
225
- .insert()
226
- .into(InventoryHistory)
227
- .values([
228
- {
229
- ...inventoryHistoryFields,
230
- name: InventoryNoGenerator.inventoryHistoryName(),
231
- status: () =>
232
- `(select case when opening_qty + ${qty} = 0 then 'TERMINATED' else 'STORED' end as status from inventory_histories where inventory_id = '${inventory.id}' order by seq desc limit 1)`,
233
- transactionType,
234
- qty,
235
- openingQty: () =>
236
- `(select opening_qty from inventory_histories where inventory_id = '${inventory.id}' order by seq desc limit 1) + ${qty}`,
237
- uomValue,
238
- openingUomValue: () =>
239
- `(select opening_uom_value from inventory_histories where inventory_id = '${inventory.id}' order by seq desc limit 1) + ${uomValue}`,
240
- seq: () =>
241
- `(select seq from inventory_histories where inventory_id = '${inventory.id}' order by seq desc limit 1) + 1`
242
- }
243
- ])
244
- .returning('*')
245
- .execute()
246
- }
247
202
 
248
- let newInventoryHistory: InventoryHistory = res.generatedMaps[0]
249
- lastSeq = newInventoryHistory.seq
250
-
251
- if (newInventoryHistory.openingQty == 0) {
252
- let terminatedRes = await invHistoryRepo
253
- .createQueryBuilder()
254
- .insert()
255
- .into(InventoryHistory)
256
- .values([
257
- {
258
- ...inventoryHistoryFields,
259
- name: InventoryNoGenerator.inventoryHistoryName(),
260
- status: INVENTORY_STATUS.TERMINATED,
261
- transactionType: INVENTORY_TRANSACTION_TYPE.TERMINATED,
262
- qty: 0,
263
- openingQty: 0,
264
- uomValue: 0,
265
- openingUomValue: 0,
266
- seq: () =>
267
- `(select seq from inventory_histories where inventory_id = '${inventory.id}' order by seq desc limit 1) + 1`
268
- }
269
- ])
270
- .returning('seq')
271
- .execute()
272
-
273
- lastSeq = terminatedRes.generatedMaps[0].seq
203
+ delete terminatedHistory.id
204
+
205
+ newInventoryHistory = await invHistoryRepo.save(terminatedHistory)
274
206
  inventory.status = INVENTORY_STATUS.TERMINATED
275
207
  }
276
208
 
277
- await invRepo.update(inventory.id, { lastSeq, updater: user })
209
+ if (inventory.lastSeq !== seq) {
210
+ await invRepo.update(inventory.id, { lastSeq: newInventoryHistory.seq, updater: user })
211
+ }
278
212
 
279
213
  await switchLocationStatus(domain, location, user, trxMgr)
280
214
  return newInventoryHistory