@things-factory/sales-base 4.3.78 → 4.3.79-alpha.0

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.
@@ -3,7 +3,7 @@ import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
3
3
  import { EntityManager, getConnection, getRepository, In, Not, Repository } from 'typeorm'
4
4
 
5
5
  import { Attachment, createAttachments } from '@things-factory/attachment-base'
6
- import { Partner, Role, User } from '@things-factory/auth-base'
6
+ import { ApplicationType, Partner, Role, User } from '@things-factory/auth-base'
7
7
  import {
8
8
  Bizplace,
9
9
  getDomainUsers,
@@ -14,7 +14,6 @@ import {
14
14
  import { generateId } from '@things-factory/id-rule-base'
15
15
  import { MarketplaceStore } from '@things-factory/integration-marketplace'
16
16
  import { Sellercraft, SellercraftStatus } from '@things-factory/integration-sellercraft'
17
- import { Sftp } from '@things-factory/integration-sftp'
18
17
  import { MarketplaceOrder, MarketplaceProductVariation } from '@things-factory/marketplace-base'
19
18
  // import { sendNotification } from '@things-factory/notification'
20
19
  import { Product, ProductBundleSetting } from '@things-factory/product-base'
@@ -485,20 +484,18 @@ export async function generateReleaseGoodFunction(
485
484
  await InventoryUtil.validateWarehousePartnersProductsQuantity(domain, bizplace, orderInventories, context, tx)
486
485
  /** End Validate Release Order Product Quantity Section */
487
486
 
488
- if (releaseGood.type == 'b2c') {
489
- const sellercraft: Sellercraft = await tx
490
- .getRepository(Sellercraft)
491
- .findOne({ domain: bizplace.domain, status: SellercraftStatus.ACTIVE })
487
+ const orderSource: string = releaseGood.source
488
+ switch (orderSource) {
489
+ case ApplicationType.SELLERCRAFT:
490
+ const sellercraft: Sellercraft = await tx
491
+ .getRepository(Sellercraft)
492
+ .findOne({ domain: bizplace.domain, status: SellercraftStatus.ACTIVE })
492
493
 
493
- let customerAvailableSftp: Sftp = await tx.getRepository(Sftp).findOne({
494
- where: { domain: bizplace.domain }
495
- })
496
-
497
- if (sellercraft) {
498
494
  const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
499
495
  await sellercraftCtrl.checkExistingReleaseGood(sellercraft, releaseGood)
500
- } else if (customerAvailableSftp) {
501
- } else {
496
+ break
497
+
498
+ case ApplicationType.MMS:
502
499
  const refNo: string = releaseGood.refNo
503
500
  const foundReleaseGood: ReleaseGood = await tx.getRepository(ReleaseGood).findOne({
504
501
  where: {
@@ -532,9 +529,62 @@ export async function generateReleaseGoodFunction(
532
529
  throw new Error('Existing release order found')
533
530
  }
534
531
  }
535
- }
532
+ break
533
+
534
+ default:
535
+ break
536
536
  }
537
537
 
538
+ // if (releaseGood.type == 'b2c') {
539
+ // const sellercraft: Sellercraft = await tx
540
+ // .getRepository(Sellercraft)
541
+ // .findOne({ domain: bizplace.domain, status: SellercraftStatus.ACTIVE })
542
+
543
+ // let customerAvailableSftp: Sftp = await tx.getRepository(Sftp).findOne({
544
+ // where: { domain: bizplace.domain }
545
+ // })
546
+
547
+ // if (sellercraft) {
548
+ // const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
549
+ // await sellercraftCtrl.checkExistingReleaseGood(sellercraft, releaseGood)
550
+ // } else if (customerAvailableSftp) {
551
+ // } else {
552
+ // const refNo: string = releaseGood.refNo
553
+ // const foundReleaseGood: ReleaseGood = await tx.getRepository(ReleaseGood).findOne({
554
+ // where: {
555
+ // domain: warehouseDomain,
556
+ // refNo,
557
+ // status: Not(In([ORDER_STATUS.CANCELLED, ORDER_STATUS.PENDING_CANCEL]))
558
+ // },
559
+ // relations: ['bizplace', 'bizplace.domain', 'bizplace.company', 'bizplace.company.domain']
560
+ // })
561
+
562
+ // if (foundReleaseGood) {
563
+ // const customerCompanyDomain: Domain = foundReleaseGood.bizplace.company.domain
564
+ // const marketplaceOrder: MarketplaceOrder = await tx.getRepository(MarketplaceOrder).findOne({
565
+ // where: { orderNo: refNo, domain: customerCompanyDomain }
566
+ // })
567
+
568
+ // // Need to restructure the validation
569
+ // if (marketplaceOrder?.isSplitted) {
570
+ // const refNo2: string = releaseGood.refNo2
571
+ // const foundSplittedReleaseGood = await tx.getRepository(ReleaseGood).findOne({
572
+ // where: {
573
+ // domain: warehouseDomain,
574
+ // refNo2,
575
+ // status: Not(In([ORDER_STATUS.CANCELLED, ORDER_STATUS.PENDING_CANCEL]))
576
+ // },
577
+ // relations: ['bizplace', 'bizplace.domain', 'bizplace.company', 'bizplace.company.domain']
578
+ // })
579
+
580
+ // if (foundSplittedReleaseGood) throw new Error('Existing release order found')
581
+ // } else {
582
+ // throw new Error('Existing release order found')
583
+ // }
584
+ // }
585
+ // }
586
+ // }
587
+
538
588
  let newReleaseGood: ReleaseGood = new ReleaseGood()
539
589
 
540
590
  /** Generate Shipping Order */
@@ -887,16 +937,28 @@ export async function confirmReleaseGood(name: string, context: any, tx?: Entity
887
937
  let foundOVs: OrderVas[] = foundReleaseGood.orderVass
888
938
  let customerBizplace: Bizplace = foundReleaseGood.bizplace
889
939
  const companyDomain: Domain = customerBizplace?.company.domain
890
- const sellercraft: Sellercraft = await tx.getRepository(Sellercraft).findOne({
891
- where: { domain: foundReleaseGood.bizplace.domain, status: SellercraftStatus.ACTIVE },
892
- relations: ['domain']
893
- })
894
940
 
895
- if (foundReleaseGood.type == 'b2c') {
896
- if (sellercraft) {
941
+ const orderSource: string = foundReleaseGood.source
942
+ switch (orderSource) {
943
+ case ApplicationType.SELLERCRAFT:
944
+ const sellercraft: Sellercraft = await tx.getRepository(Sellercraft).findOne({
945
+ where: { domain: foundReleaseGood.bizplace.domain, status: SellercraftStatus.ACTIVE },
946
+ relations: ['domain']
947
+ })
948
+
897
949
  const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
898
- foundReleaseGood = await sellercraftCtrl.packOrder(sellercraft, { ...foundReleaseGood, orderProducts: foundOPs })
899
- } else {
950
+ if (foundReleaseGood.type == 'b2c') {
951
+ foundReleaseGood = await sellercraftCtrl.packOrder(sellercraft, {
952
+ ...foundReleaseGood,
953
+ orderProducts: foundOPs
954
+ })
955
+ } else if (foundReleaseGood.type == 'b2b') {
956
+ await sellercraftCtrl.updateSellercraftStock(sellercraft, foundOPs, 'CONFIRM_ORDER')
957
+ }
958
+
959
+ break
960
+
961
+ case ApplicationType.MMS:
900
962
  // find for any existing marketplace store connections
901
963
  let marketplaceStores: MarketplaceStore[] = await tx.getRepository(MarketplaceStore).find({
902
964
  where: { domain: companyDomain, status: 'ACTIVE' },
@@ -959,16 +1021,94 @@ export async function confirmReleaseGood(name: string, context: any, tx?: Entity
959
1021
  const ecommerceCtrl: EcommerceController = new EcommerceController(tx, domain, user)
960
1022
  await ecommerceCtrl.updateProductVariationStock(marketplaceStores, productIds, companyDomain)
961
1023
  }
962
- }
963
- }
1024
+ break
964
1025
 
965
- if (foundReleaseGood.type == 'b2b') {
966
- if (sellercraft) {
967
- const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
968
- await sellercraftCtrl.updateSellercraftStock(sellercraft, foundOPs, 'CONFIRM_ORDER')
969
- }
1026
+ default:
1027
+ break
970
1028
  }
971
1029
 
1030
+ // const sellercraft: Sellercraft = await tx.getRepository(Sellercraft).findOne({
1031
+ // where: { domain: foundReleaseGood.bizplace.domain, status: SellercraftStatus.ACTIVE },
1032
+ // relations: ['domain']
1033
+ // })
1034
+
1035
+ // if (foundReleaseGood.type == 'b2c') {
1036
+ // if (sellercraft) {
1037
+ // const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
1038
+ // foundReleaseGood = await sellercraftCtrl.packOrder(sellercraft, { ...foundReleaseGood, orderProducts: foundOPs })
1039
+ // } else {
1040
+ // // find for any existing marketplace store connections
1041
+ // let marketplaceStores: MarketplaceStore[] = await tx.getRepository(MarketplaceStore).find({
1042
+ // where: { domain: companyDomain, status: 'ACTIVE' },
1043
+ // relations: ['marketplaceDistributors']
1044
+ // })
1045
+
1046
+ // const foundMarketplaceOrder: MarketplaceOrder = await tx.getRepository(MarketplaceOrder).findOne({
1047
+ // where: { domain: companyDomain, orderNo: foundReleaseGood.refNo },
1048
+ // relations: ['marketplaceStore']
1049
+ // })
1050
+
1051
+ // let matchedMarketplaceStore: MarketplaceStore
1052
+ // if (foundMarketplaceOrder) {
1053
+ // matchedMarketplaceStore = marketplaceStores.filter(
1054
+ // marketplaceStore => marketplaceStore.id == foundMarketplaceOrder.marketplaceStore.id
1055
+ // )[0]
1056
+ // }
1057
+
1058
+ // let combinedOrderInventories: any[] = foundOIs.map(oi => {
1059
+ // return {
1060
+ // sku: oi.product.sku,
1061
+ // releaseQty: oi.releaseQty
1062
+ // }
1063
+ // })
1064
+
1065
+ // if (foundBundleInfo?.length) {
1066
+ // foundBundleInfo.map(bundle => {
1067
+ // combinedOrderInventories.push({
1068
+ // sku: bundle.sku,
1069
+ // releaseQty: bundle.releaseQty
1070
+ // })
1071
+ // })
1072
+ // }
1073
+
1074
+ // for (let oi of combinedOrderInventories) {
1075
+ // let foundMarketplaceProductVariations: MarketplaceProductVariation[] = await tx
1076
+ // .getRepository(MarketplaceProductVariation)
1077
+ // .find({
1078
+ // where: { domain: companyDomain, sku: oi.sku },
1079
+ // relations: ['marketplaceProduct', 'marketplaceProduct.marketplaceStore']
1080
+ // })
1081
+
1082
+ // if (foundMarketplaceProductVariations) {
1083
+ // await Promise.all(
1084
+ // foundMarketplaceProductVariations.map(async variation => {
1085
+ // if (variation.marketplaceProduct.marketplaceStore.reserveEnabled) {
1086
+ // variation.reserveQty -= oi.releaseQty
1087
+ // }
1088
+
1089
+ // await tx.getRepository(MarketplaceProductVariation).save(variation)
1090
+ // })
1091
+ // )
1092
+ // }
1093
+ // }
1094
+
1095
+ // if (marketplaceStores?.length && marketplaceStores.some(store => store.isAutoUpdateStockQty)) {
1096
+ // marketplaceStores = marketplaceStores.filter(marketplaceStore => marketplaceStore.isAutoUpdateStockQty)
1097
+ // let productIds: string[] = foundOIs.map(oi => oi.product.id)
1098
+ // productIds = Array.from(new Set([...productIds]))
1099
+ // const ecommerceCtrl: EcommerceController = new EcommerceController(tx, domain, user)
1100
+ // await ecommerceCtrl.updateProductVariationStock(marketplaceStores, productIds, companyDomain)
1101
+ // }
1102
+ // }
1103
+ // }
1104
+
1105
+ // if (foundReleaseGood.type == 'b2b') {
1106
+ // if (sellercraft) {
1107
+ // const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
1108
+ // await sellercraftCtrl.updateSellercraftStock(sellercraft, foundOPs, 'CONFIRM_ORDER')
1109
+ // }
1110
+ // }
1111
+
972
1112
  // 1. RO Status change (PENDING => PENDING_RECEIVE)
973
1113
  const releaseGood: ReleaseGood = await tx.getRepository(ReleaseGood).save({
974
1114
  ...foundReleaseGood,
@@ -1183,15 +1323,32 @@ export async function rejectReleaseGood(
1183
1323
  }
1184
1324
 
1185
1325
  if (foundOPs && foundOPs.length) {
1186
- const sellercraft: Sellercraft = await tx
1187
- .getRepository(Sellercraft)
1188
- .findOne({ domain: releaseGood.bizplace.domain, status: SellercraftStatus.ACTIVE })
1326
+ const orderSource: string = releaseGood.source
1327
+ switch (orderSource) {
1328
+ case ApplicationType.SELLERCRAFT:
1329
+ const sellercraft: Sellercraft = await tx
1330
+ .getRepository(Sellercraft)
1331
+ .findOne({ domain: releaseGood.bizplace.domain, status: SellercraftStatus.ACTIVE })
1332
+
1333
+ if (sellercraft) {
1334
+ const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
1335
+ await sellercraftCtrl.updateSellercraftStock(sellercraft, foundOPs, 'REJECT_ORDER')
1336
+ }
1337
+ break
1189
1338
 
1190
- if (sellercraft) {
1191
- const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
1192
- await sellercraftCtrl.updateSellercraftStock(sellercraft, foundOPs, 'REJECT_ORDER')
1339
+ default:
1340
+ break
1193
1341
  }
1194
1342
 
1343
+ // const sellercraft: Sellercraft = await tx
1344
+ // .getRepository(Sellercraft)
1345
+ // .findOne({ domain: releaseGood.bizplace.domain, status: SellercraftStatus.ACTIVE })
1346
+
1347
+ // if (sellercraft) {
1348
+ // const sellercraftCtrl: SellercraftController = new SellercraftController(tx, domain, user)
1349
+ // await sellercraftCtrl.updateSellercraftStock(sellercraft, foundOPs, 'REJECT_ORDER')
1350
+ // }
1351
+
1195
1352
  await tx.getRepository(OrderProduct).save(
1196
1353
  await Promise.all(
1197
1354
  foundOPs.map(async (op: OrderProduct) => {
@@ -1288,6 +1445,7 @@ export async function bulkGenerateReleaseGood(
1288
1445
  phone1: releaseGood?.phone1 || null,
1289
1446
  phone2: releaseGood?.phone2 || null,
1290
1447
  email: releaseGood?.email || null,
1448
+ source: releaseGood?.source || null,
1291
1449
  airwayBill: releaseGood?.airwayBill,
1292
1450
  refNo: releaseGood.refNo,
1293
1451
  refNo2: releaseGood.refNo2,
@@ -1296,8 +1454,8 @@ export async function bulkGenerateReleaseGood(
1296
1454
  ownTransport: releaseGood?.ownTransport || false,
1297
1455
  orderMethod: ORDER_METHOD.SELECT_BY_PRODUCT,
1298
1456
  status: ORDER_STATUS.PENDING,
1299
- creator: user,
1300
- updater: user
1457
+ creator: releaseGood?.creator || user,
1458
+ updater: releaseGood?.updater || user
1301
1459
  }
1302
1460
 
1303
1461
  newReleaseGood = await tx.getRepository(ReleaseGood).save(newReleaseGood)
@@ -1370,8 +1528,8 @@ export async function bulkGenerateReleaseGood(
1370
1528
  status: ORDER_INVENTORY_STATUS.PENDING,
1371
1529
  name: OrderNoGenerator.orderInventory(),
1372
1530
  releaseGood: newReleaseGood,
1373
- creator: user,
1374
- updater: user
1531
+ creator: newReleaseGood?.creator || user,
1532
+ updater: newReleaseGood?.updater || user
1375
1533
  }
1376
1534
 
1377
1535
  let newOrderProduct: Partial<OrderProduct>
@@ -1393,8 +1551,8 @@ export async function bulkGenerateReleaseGood(
1393
1551
  actualPalletQty: 0,
1394
1552
  status: ORDER_PRODUCT_STATUS.ASSIGNED,
1395
1553
  releaseGood: newReleaseGood,
1396
- creator: user,
1397
- updater: user
1554
+ creator: newReleaseGood?.creator || user,
1555
+ updater: newReleaseGood?.updater || user
1398
1556
  }
1399
1557
 
1400
1558
  newOrderProduct = await tx.getRepository(OrderProduct).save(newOrderProduct)
@@ -1403,7 +1561,7 @@ export async function bulkGenerateReleaseGood(
1403
1561
  ...oi.orderProduct,
1404
1562
  totalUomValue: `${oi.orderProduct.releaseUomValue} ${oi.orderProduct.uom}`,
1405
1563
  status: ORDER_PRODUCT_STATUS.ASSIGNED,
1406
- updater: user
1564
+ updater: newReleaseGood?.updater || user
1407
1565
  })
1408
1566
  }
1409
1567
 
@@ -95,8 +95,8 @@ export class ReleaseGood {
95
95
  DATABASE_TYPE == 'postgres' || DATABASE_TYPE == 'mysql' || DATABASE_TYPE == 'mariadb'
96
96
  ? 'enum'
97
97
  : DATABASE_TYPE == 'oracle'
98
- ? 'varchar2'
99
- : 'smallint',
98
+ ? 'varchar2'
99
+ : 'smallint',
100
100
  enum: ReleaseOrderType,
101
101
  default: ReleaseOrderType.B2B
102
102
  })
@@ -167,15 +167,13 @@ export class ReleaseGood {
167
167
  DATABASE_TYPE == 'postgres' || DATABASE_TYPE == 'mysql' || DATABASE_TYPE == 'mariadb'
168
168
  ? 'enum'
169
169
  : DATABASE_TYPE == 'oracle'
170
- ? 'varchar2'
171
- : 'smallint',
170
+ ? 'varchar2'
171
+ : 'smallint',
172
172
  enum: DispatchmentStatus,
173
173
  default: DispatchmentStatus.READY_TO_DISPATCH
174
174
  })
175
175
  dispatchmentStatus: DispatchmentStatus
176
176
 
177
-
178
-
179
177
  @Column({ nullable: true })
180
178
  @Field({ nullable: true })
181
179
  refNo: String
@@ -443,6 +441,10 @@ export class ReleaseGood {
443
441
  @Field({ nullable: true })
444
442
  stopId: string
445
443
 
444
+ @Column({ nullable: true })
445
+ @Field({ nullable: true })
446
+ source: string
447
+
446
448
  @Column({ nullable: true })
447
449
  @Field({ nullable: true })
448
450
  manifestedAt: Date