@things-factory/operato-hub 6.0.1 → 6.0.3

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 (104) hide show
  1. package/dist-server/tsconfig.tsbuildinfo +1 -1
  2. package/package.json +27 -27
  3. package/dist-server/entities/email-attachment.js +0 -77
  4. package/dist-server/entities/email-attachment.js.map +0 -1
  5. package/dist-server/entities/email-recipient.js +0 -69
  6. package/dist-server/entities/email-recipient.js.map +0 -1
  7. package/dist-server/entities/email.js +0 -90
  8. package/dist-server/entities/email.js.map +0 -1
  9. package/dist-server/graphql/resolvers/email/index.js +0 -6
  10. package/dist-server/graphql/resolvers/email/index.js.map +0 -1
  11. package/dist-server/graphql/resolvers/email/send-emails.js +0 -197
  12. package/dist-server/graphql/resolvers/email/send-emails.js.map +0 -1
  13. package/dist-server/graphql/resolvers/reports/inbound-order-details-report.js +0 -200
  14. package/dist-server/graphql/resolvers/reports/inbound-order-details-report.js.map +0 -1
  15. package/dist-server/graphql/resolvers/reports/index.js +0 -7
  16. package/dist-server/graphql/resolvers/reports/index.js.map +0 -1
  17. package/dist-server/graphql/resolvers/reports/outbound-order-details-report.js +0 -231
  18. package/dist-server/graphql/resolvers/reports/outbound-order-details-report.js.map +0 -1
  19. package/dist-server/graphql/types/reports/inbound-order-details-report-list.js +0 -14
  20. package/dist-server/graphql/types/reports/inbound-order-details-report-list.js.map +0 -1
  21. package/dist-server/graphql/types/reports/inbound-order-details-report.js +0 -59
  22. package/dist-server/graphql/types/reports/inbound-order-details-report.js.map +0 -1
  23. package/dist-server/graphql/types/reports/index.js +0 -13
  24. package/dist-server/graphql/types/reports/index.js.map +0 -1
  25. package/dist-server/graphql/types/reports/outbound-order-details-report-list.js +0 -14
  26. package/dist-server/graphql/types/reports/outbound-order-details-report-list.js.map +0 -1
  27. package/dist-server/graphql/types/reports/outbound-order-details-report.js +0 -76
  28. package/dist-server/graphql/types/reports/outbound-order-details-report.js.map +0 -1
  29. package/dist-server/routers/api/restful-apis/v1/company/add-contact-points.js +0 -105
  30. package/dist-server/routers/api/restful-apis/v1/company/add-contact-points.js.map +0 -1
  31. package/dist-server/routers/api/restful-apis/v1/company/add-marketplace-order.js +0 -197
  32. package/dist-server/routers/api/restful-apis/v1/company/add-marketplace-order.js.map +0 -1
  33. package/dist-server/routers/api/restful-apis/v1/company/add-products.js +0 -79
  34. package/dist-server/routers/api/restful-apis/v1/company/add-products.js.map +0 -1
  35. package/dist-server/routers/api/restful-apis/v1/company/cancel-marketplace-order.js +0 -90
  36. package/dist-server/routers/api/restful-apis/v1/company/cancel-marketplace-order.js.map +0 -1
  37. package/dist-server/routers/api/restful-apis/v1/company/get-bizplace-list.js +0 -49
  38. package/dist-server/routers/api/restful-apis/v1/company/get-bizplace-list.js.map +0 -1
  39. package/dist-server/routers/api/restful-apis/v1/company/get-contact-point-list.js +0 -62
  40. package/dist-server/routers/api/restful-apis/v1/company/get-contact-point-list.js.map +0 -1
  41. package/dist-server/routers/api/restful-apis/v1/company/get-marketplace-order-shipping-list.js +0 -98
  42. package/dist-server/routers/api/restful-apis/v1/company/get-marketplace-order-shipping-list.js.map +0 -1
  43. package/dist-server/routers/api/restful-apis/v1/company/get-marketplace-store-list.js +0 -51
  44. package/dist-server/routers/api/restful-apis/v1/company/get-marketplace-store-list.js.map +0 -1
  45. package/dist-server/routers/api/restful-apis/v1/company/get-partner-list.js +0 -88
  46. package/dist-server/routers/api/restful-apis/v1/company/get-partner-list.js.map +0 -1
  47. package/dist-server/routers/api/restful-apis/v1/company/get-product-list.js +0 -99
  48. package/dist-server/routers/api/restful-apis/v1/company/get-product-list.js.map +0 -1
  49. package/dist-server/routers/api/restful-apis/v1/company/index.js +0 -15
  50. package/dist-server/routers/api/restful-apis/v1/company/index.js.map +0 -1
  51. package/dist-server/routers/api/restful-apis/v1/company/update-marketplace-order-delivered.js +0 -62
  52. package/dist-server/routers/api/restful-apis/v1/company/update-marketplace-order-delivered.js.map +0 -1
  53. package/dist-server/routers/api/restful-apis/v1/company/update-marketplace-shipping-order.js +0 -40
  54. package/dist-server/routers/api/restful-apis/v1/company/update-marketplace-shipping-order.js.map +0 -1
  55. package/dist-server/routers/api/restful-apis/v1/middlewares/business-middleware.js +0 -70
  56. package/dist-server/routers/api/restful-apis/v1/middlewares/business-middleware.js.map +0 -1
  57. package/dist-server/routers/api/restful-apis/v1/middlewares/index.js +0 -40
  58. package/dist-server/routers/api/restful-apis/v1/middlewares/index.js.map +0 -1
  59. package/dist-server/routers/api/restful-apis/v1/middlewares/logging-middleware.js +0 -23
  60. package/dist-server/routers/api/restful-apis/v1/middlewares/logging-middleware.js.map +0 -1
  61. package/dist-server/routers/api/restful-apis/v1/middlewares/store-middleware.js +0 -32
  62. package/dist-server/routers/api/restful-apis/v1/middlewares/store-middleware.js.map +0 -1
  63. package/dist-server/routers/api/restful-apis/v1/middlewares/validation-middleware.js +0 -118
  64. package/dist-server/routers/api/restful-apis/v1/middlewares/validation-middleware.js.map +0 -1
  65. package/dist-server/routers/api/restful-apis/v1/middlewares/warehouse-middleware.js +0 -31
  66. package/dist-server/routers/api/restful-apis/v1/middlewares/warehouse-middleware.js.map +0 -1
  67. package/dist-server/routers/api/restful-apis/v1/utils/error-util.js +0 -71
  68. package/dist-server/routers/api/restful-apis/v1/utils/error-util.js.map +0 -1
  69. package/dist-server/routers/api/restful-apis/v1/utils/params.js +0 -563
  70. package/dist-server/routers/api/restful-apis/v1/utils/params.js.map +0 -1
  71. package/dist-server/routers/api/restful-apis/v1/utils/validate-util.js +0 -66
  72. package/dist-server/routers/api/restful-apis/v1/utils/validate-util.js.map +0 -1
  73. package/dist-server/routers/api/restful-apis/v1/warehouse/add-inbound-order.js +0 -236
  74. package/dist-server/routers/api/restful-apis/v1/warehouse/add-inbound-order.js.map +0 -1
  75. package/dist-server/routers/api/restful-apis/v1/warehouse/add-release-order.js +0 -801
  76. package/dist-server/routers/api/restful-apis/v1/warehouse/add-release-order.js.map +0 -1
  77. package/dist-server/routers/api/restful-apis/v1/warehouse/cancel-inbound-order.js +0 -199
  78. package/dist-server/routers/api/restful-apis/v1/warehouse/cancel-inbound-order.js.map +0 -1
  79. package/dist-server/routers/api/restful-apis/v1/warehouse/cancel-release-order.js +0 -192
  80. package/dist-server/routers/api/restful-apis/v1/warehouse/cancel-release-order.js.map +0 -1
  81. package/dist-server/routers/api/restful-apis/v1/warehouse/get-delivery-order-list.js +0 -138
  82. package/dist-server/routers/api/restful-apis/v1/warehouse/get-delivery-order-list.js.map +0 -1
  83. package/dist-server/routers/api/restful-apis/v1/warehouse/get-goods-received-note-list.js +0 -97
  84. package/dist-server/routers/api/restful-apis/v1/warehouse/get-goods-received-note-list.js.map +0 -1
  85. package/dist-server/routers/api/restful-apis/v1/warehouse/get-inbound-order-details.js +0 -81
  86. package/dist-server/routers/api/restful-apis/v1/warehouse/get-inbound-order-details.js.map +0 -1
  87. package/dist-server/routers/api/restful-apis/v1/warehouse/get-inbound-order-list.js +0 -94
  88. package/dist-server/routers/api/restful-apis/v1/warehouse/get-inbound-order-list.js.map +0 -1
  89. package/dist-server/routers/api/restful-apis/v1/warehouse/get-inventory-product-group-list.js +0 -87
  90. package/dist-server/routers/api/restful-apis/v1/warehouse/get-inventory-product-group-list.js.map +0 -1
  91. package/dist-server/routers/api/restful-apis/v1/warehouse/get-onhand-inventory-list.js +0 -87
  92. package/dist-server/routers/api/restful-apis/v1/warehouse/get-onhand-inventory-list.js.map +0 -1
  93. package/dist-server/routers/api/restful-apis/v1/warehouse/get-release-order-details.js +0 -96
  94. package/dist-server/routers/api/restful-apis/v1/warehouse/get-release-order-details.js.map +0 -1
  95. package/dist-server/routers/api/restful-apis/v1/warehouse/get-release-order-list.js +0 -110
  96. package/dist-server/routers/api/restful-apis/v1/warehouse/get-release-order-list.js.map +0 -1
  97. package/dist-server/routers/api/restful-apis/v1/warehouse/index.js +0 -16
  98. package/dist-server/routers/api/restful-apis/v1/warehouse/index.js.map +0 -1
  99. package/dist-server/routers/api/restful-apis/v1/warehouse/update-release-order-details.js +0 -71
  100. package/dist-server/routers/api/restful-apis/v1/warehouse/update-release-order-details.js.map +0 -1
  101. package/dist-server/routers/xilnex-router.js +0 -1104
  102. package/dist-server/routers/xilnex-router.js.map +0 -1
  103. package/dist-server/utils/report-query-util.js +0 -42
  104. package/dist-server/utils/report-query-util.js.map +0 -1
@@ -1,801 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const typeorm_1 = require("typeorm");
4
- const uuid_1 = require("uuid");
5
- const api_1 = require("@things-factory/api");
6
- const biz_base_1 = require("@things-factory/biz-base");
7
- const id_rule_base_1 = require("@things-factory/id-rule-base");
8
- const integration_sellercraft_1 = require("@things-factory/integration-sellercraft");
9
- const marketplace_base_1 = require("@things-factory/marketplace-base");
10
- const product_base_1 = require("@things-factory/product-base");
11
- const sales_base_1 = require("@things-factory/sales-base");
12
- const setting_base_1 = require("@things-factory/setting-base");
13
- const warehouse_base_1 = require("@things-factory/warehouse-base");
14
- const geography_1 = require("@things-factory/geography");
15
- const middlewares_1 = require("../middlewares");
16
- const error_util_1 = require("../utils/error-util");
17
- const debug = require('debug')('things-factory:operato-hub:restful-api:unstable:add-release-order');
18
- const apiVersion = 'v1';
19
- api_1.restfulApiRouter.post(`/${apiVersion}/warehouse/add-release-order`, middlewares_1.businessMiddleware, middlewares_1.validationMiddleware, middlewares_1.loggingMiddleware, async (context, next) => {
20
- return await (0, typeorm_1.getConnection)().transaction(async (tx) => {
21
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
22
- const { client, domain, user, application } = context.state;
23
- const { bizplaceId: customerBizplaceId } = context.request.body.data;
24
- const { t } = context;
25
- const bodyReq = context.request.body.data;
26
- const requiredDraft = bodyReq.hasOwnProperty('requiredDraft') ? bodyReq.requiredDraft : true;
27
- let releaseGood;
28
- let massagedData = { orderProducts: [], combinedItems: [] };
29
- // const isAutoAssign: Boolean = bodyReq.hasOwnProperty('isAutoAssign') ? bodyReq.isAutoAssign : true
30
- try {
31
- if (!customerBizplaceId)
32
- throw new error_util_1.ApiError('E04', 'warehouse id not found');
33
- if (typeof bodyReq === undefined || !((_a = Object.keys(bodyReq)) === null || _a === void 0 ? void 0 : _a.length)) {
34
- throw new error_util_1.ApiError('E01', 'invalid body request format');
35
- }
36
- debug('post:/add-release-order request.body', bodyReq);
37
- // get customer company's bizplace
38
- const custCompanyBizplace = await (0, biz_base_1.getCompanyBizplace)(null, null, customerBizplaceId, tx);
39
- const customerBizplace = await tx.getRepository(biz_base_1.Bizplace).findOne({
40
- where: { id: customerBizplaceId },
41
- relations: ['domain']
42
- });
43
- // conditional validation
44
- if (bodyReq.ownTransport && !bodyReq.collectionOrderNo)
45
- throw new error_util_1.ApiError('E03', 'collectionOrderNo');
46
- if (bodyReq.exportOption && !bodyReq.shippingOrder)
47
- throw new error_util_1.ApiError('E03', 'shippingOrder');
48
- if (bodyReq.exportOption && !bodyReq.ownTransport)
49
- throw new error_util_1.ApiError('E01', 'ownTransport');
50
- bodyReq.exportOption;
51
- releaseGood = {
52
- domain,
53
- bizplace: customerBizplace,
54
- companyBizplace: custCompanyBizplace,
55
- courierOption: bodyReq.courierOption,
56
- collectionOrderNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.collectionOrderNo,
57
- exportOption: bodyReq.exportOption,
58
- ownTransport: bodyReq.ownTransport,
59
- packingOption: bodyReq.packingOption,
60
- refNo: bodyReq.refNo,
61
- refNo2: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo2,
62
- refNo3: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.refNo3,
63
- releaseDate: bodyReq.releaseDate,
64
- type: bodyReq.type,
65
- marketplaceOrderStatus: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.marketplaceOrderStatus,
66
- remark: (bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.remark) || null,
67
- billingAddress: ((_b = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.billTo) === null || _b === void 0 ? void 0 : _b.billingAddress) || null,
68
- deliveryAddress1: ((_c = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _c === void 0 ? void 0 : _c.deliveryAddress1) || null,
69
- deliveryAddress2: ((_d = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _d === void 0 ? void 0 : _d.deliveryAddress2) || null,
70
- deliveryAddress3: ((_e = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _e === void 0 ? void 0 : _e.deliveryAddress3) || null,
71
- deliveryAddress4: ((_f = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _f === void 0 ? void 0 : _f.deliveryAddress4) || null,
72
- deliveryAddress5: ((_g = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _g === void 0 ? void 0 : _g.deliveryAddress5) || null,
73
- attentionTo: ((_h = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _h === void 0 ? void 0 : _h.attentionTo) || null,
74
- attentionCompany: ((_j = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _j === void 0 ? void 0 : _j.attentionCompany) || null,
75
- city: ((_k = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _k === void 0 ? void 0 : _k.city) || null,
76
- state: ((_l = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _l === void 0 ? void 0 : _l.state) || null,
77
- postalCode: ((_m = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _m === void 0 ? void 0 : _m.postalCode) || null,
78
- country: ((_o = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _o === void 0 ? void 0 : _o.country) || null,
79
- phone1: ((_p = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _p === void 0 ? void 0 : _p.phone1) || null,
80
- phone2: ((_q = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _q === void 0 ? void 0 : _q.phone2) || null,
81
- email: ((_r = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.deliverTo) === null || _r === void 0 ? void 0 : _r.email) || null,
82
- transporter: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.transporter,
83
- trackingNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.trackingNo,
84
- airwayBill: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.airwayBill,
85
- invoice: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.invoice,
86
- packageId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.packageId,
87
- storeName: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.storeName,
88
- storeId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.storeId,
89
- routeId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.routeId,
90
- stopId: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.stopId,
91
- source: application === null || application === void 0 ? void 0 : application.type,
92
- platformCode: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.platformCode,
93
- platformCountry: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.platformCountry,
94
- platformName: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.platformName,
95
- discountAmount: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.discountAmount,
96
- taxAmount: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.taxAmount,
97
- shippingFee: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingFee,
98
- shippingOrder: {
99
- shipName: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder.shipName,
100
- containerNo: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder.containerNo,
101
- loadType: (_s = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder) === null || _s === void 0 ? void 0 : _s.loadType,
102
- from: (_t = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder) === null || _t === void 0 ? void 0 : _t.from,
103
- to: (_u = bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder) === null || _u === void 0 ? void 0 : _u.to,
104
- containerArrivalDate: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder.containerArrivalDate,
105
- containerLeavingDate: bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.shippingOrder.containerLeavingDate
106
- }
107
- };
108
- // check existing order
109
- await checkExistingOrder(releaseGood, customerBizplace, bodyReq === null || bodyReq === void 0 ? void 0 : bodyReq.checkDuplicationValidation, context, tx);
110
- // massage data
111
- massagedData = await massageOrderItems(releaseGood, bodyReq.orderInventories, context);
112
- // validation
113
- await Promise.all(massagedData.combinedItems.map(async (item) => {
114
- let itemList = await tx.query(`
115
- select wboi.* from warehouse_bizplace_onhand_inventories wboi
116
- where wboi."domain_id"= $1 and
117
- wboi."bizplace_id" = $2 and
118
- wboi."group_type" = 'SINGLE' and
119
- wboi."product_id" = $3 and
120
- wboi."packing_type" = $4 and
121
- wboi."packing_size" = $5 and
122
- wboi."uom" = $6 and
123
- wboi."remain_qty" >= $7 and
124
- wboi."remain_uom_value" >= $8
125
- `, [
126
- domain.id,
127
- customerBizplace.id,
128
- item.product.id,
129
- item.packingType,
130
- item.packingSize,
131
- item.uom,
132
- item.releaseQty,
133
- item.releaseUomValue
134
- ]);
135
- if (itemList.length <= 0) {
136
- throw new error_util_1.ApiError('E01', 'INSUFFICIENT_STOCK');
137
- }
138
- console.log();
139
- }));
140
- // assignment
141
- // console.time('assign')
142
- let assignedOrderProducts = await assignToInventory(massagedData.orderProducts, customerBizplace, context, tx);
143
- // console.timeEnd('assign')
144
- // create order
145
- // console.time('save')
146
- let result = await createReleaseGood(releaseGood, assignedOrderProducts, customerBizplace, context, tx);
147
- // console.timeEnd('save')
148
- let data = {
149
- id: result.id,
150
- roNo: result.name,
151
- refNo: result.refNo,
152
- refNo2: result.refNo2,
153
- refNo3: result.refNo3,
154
- status: result.status,
155
- truckNo: result.truckNo,
156
- ownTransport: result.ownTransport,
157
- crossDocking: result.crossDocking,
158
- marketplaceOrderStatus: result.marketplaceOrderStatus,
159
- billingAddress: result.billingAddress,
160
- deliveryAddress1: result.deliveryAddress1,
161
- deliveryAddress2: result.deliveryAddress2,
162
- deliveryAddress3: result.deliveryAddress3,
163
- deliveryAddress4: result.deliveryAddress4,
164
- deliveryAddress5: result.deliveryAddress5,
165
- attentionTo: result.attentionTo,
166
- attentionCompany: result.attentionCompany,
167
- city: result.city,
168
- state: result.state,
169
- postalCode: result.postalCode,
170
- country: result.country,
171
- phone1: result.phone1,
172
- phone2: result.phone2,
173
- email: result.email,
174
- transporter: result.transporter,
175
- trackingNo: result.trackingNo,
176
- airwayBill: result.airwayBill,
177
- invoice: result.invoice,
178
- type: result.type,
179
- packageId: result.packageId,
180
- storeName: result.storeName,
181
- storeId: result.storeId,
182
- routeId: result.routeId,
183
- stopId: result.stopId,
184
- exportOption: result.exportOption,
185
- releaseDate: result.releaseDate,
186
- collectionOrderNo: result.collectionOrderNo,
187
- platformCode: result === null || result === void 0 ? void 0 : result.platformCode,
188
- platformCountry: result === null || result === void 0 ? void 0 : result.platformCountry,
189
- platformName: result === null || result === void 0 ? void 0 : result.platformName,
190
- discountAmount: result === null || result === void 0 ? void 0 : result.discountAmount,
191
- taxAmount: result === null || result === void 0 ? void 0 : result.taxAmount,
192
- shippingFee: result === null || result === void 0 ? void 0 : result.shippingFee,
193
- bizplace: { name: result.bizplace.name },
194
- domain: { name: result.domain.name },
195
- orderInventories: result.orderInventories
196
- };
197
- // return body
198
- context.body = { result: { data } };
199
- }
200
- catch (error) {
201
- if (error.details) {
202
- if (error.details == 'INSUFFICIENT_STOCK' && requiredDraft) {
203
- const result = await createDraftOrder(releaseGood, massagedData.orderProducts, context);
204
- let data = {
205
- id: result.id,
206
- name: result.name,
207
- refNo: result.refNo,
208
- refNo2: result.refNo2,
209
- refNo3: result.refNo3,
210
- status: result.status,
211
- truckNo: result.truckNo,
212
- ownTransport: result.ownTransport,
213
- marketplaceOrderStatus: result.marketplaceOrderStatus,
214
- billingAddress: result.billingAddress,
215
- deliveryAddress1: result.deliveryAddress1,
216
- deliveryAddress2: result.deliveryAddress2,
217
- deliveryAddress3: result.deliveryAddress3,
218
- deliveryAddress4: result.deliveryAddress4,
219
- deliveryAddress5: result.deliveryAddress5,
220
- attentionTo: result.attentionTo,
221
- attentionCompany: result.attentionCompany,
222
- city: result.city,
223
- state: result.state,
224
- postalCode: result.postalCode,
225
- country: result.country,
226
- phone1: result.phone1,
227
- phone2: result.phone2,
228
- email: result.email,
229
- type: result.type,
230
- packageId: result.packageId,
231
- exportOption: result.exportOption,
232
- releaseDate: result.releaseDate,
233
- collectionOrderNo: result.collectionOrderNo,
234
- platformCode: result === null || result === void 0 ? void 0 : result.platformCode,
235
- platformCountry: result === null || result === void 0 ? void 0 : result.platformCountry,
236
- platformName: result === null || result === void 0 ? void 0 : result.platformName,
237
- discountAmount: result === null || result === void 0 ? void 0 : result.discountAmount,
238
- taxAmount: result === null || result === void 0 ? void 0 : result.taxAmount,
239
- shippingFee: result === null || result === void 0 ? void 0 : result.shippingFee,
240
- bizplace: { name: result.bizplace.name },
241
- domain: { name: result.domain.name }
242
- };
243
- context.body = { result: { data } };
244
- }
245
- else {
246
- if (error instanceof error_util_1.ApiError)
247
- (0, error_util_1.ApiErrorHandler)(context, error);
248
- else
249
- (0, error_util_1.throwInternalServerError)(context, error);
250
- }
251
- }
252
- else {
253
- if (error instanceof error_util_1.ApiError)
254
- (0, error_util_1.ApiErrorHandler)(context, error);
255
- else
256
- (0, error_util_1.throwInternalServerError)(context, error);
257
- }
258
- }
259
- });
260
- });
261
- async function checkExistingOrder(releaseGood, bizplace, checkDuplicationValidation = true, context, tx) {
262
- const { domain, user } = context.state;
263
- const refNo = releaseGood.refNo;
264
- const refNo2 = releaseGood.refNo2;
265
- const refNo3 = releaseGood.refNo3;
266
- if (checkDuplicationValidation) {
267
- const foundDraftReleaseGood = await tx.getRepository(sales_base_1.DraftReleaseGood).findOne({
268
- where: {
269
- domain,
270
- refNo: refNo || null,
271
- refNo2: refNo2 || null,
272
- refNo3: refNo3 || null,
273
- status: (0, typeorm_1.Not)((0, typeorm_1.In)([sales_base_1.DRAFT_RELEASE_ORDER_STATUS.CANCELLED]))
274
- }
275
- });
276
- if (foundDraftReleaseGood) {
277
- throw new error_util_1.ApiError('E05', `existing draft release order found: ${foundDraftReleaseGood.name}`);
278
- }
279
- const sellercraft = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({ domain: bizplace.domain });
280
- if (sellercraft) {
281
- const sellercraftCtrl = new sales_base_1.SellercraftController(tx, bizplace.domain, user);
282
- await sellercraftCtrl.checkExistingReleaseGood(sellercraft, releaseGood);
283
- }
284
- else {
285
- const foundReleaseGood = await tx.getRepository(sales_base_1.ReleaseGood).findOne({
286
- where: {
287
- domain: domain,
288
- refNo,
289
- status: (0, typeorm_1.Not)((0, typeorm_1.In)([sales_base_1.ORDER_STATUS.CANCELLED, sales_base_1.ORDER_STATUS.PENDING_CANCEL]))
290
- },
291
- relations: ['bizplace', 'bizplace.domain', 'bizplace.company', 'bizplace.company.domain']
292
- });
293
- if (foundReleaseGood) {
294
- const customerCompanyDomain = foundReleaseGood.bizplace.company.domain;
295
- const marketplaceOrder = await tx.getRepository(marketplace_base_1.MarketplaceOrder).findOne({
296
- where: { orderNo: refNo, domain: customerCompanyDomain }
297
- });
298
- // Need to restructure the validation
299
- if (marketplaceOrder === null || marketplaceOrder === void 0 ? void 0 : marketplaceOrder.isSplitted) {
300
- const refNo2 = releaseGood.refNo2;
301
- const foundSplittedReleaseGood = await tx.getRepository(sales_base_1.ReleaseGood).findOne({
302
- where: {
303
- domain,
304
- refNo2,
305
- status: (0, typeorm_1.Not)((0, typeorm_1.In)([sales_base_1.ORDER_STATUS.CANCELLED, sales_base_1.ORDER_STATUS.PENDING_CANCEL]))
306
- },
307
- relations: ['bizplace', 'bizplace.domain', 'bizplace.company', 'bizplace.company.domain']
308
- });
309
- if (foundSplittedReleaseGood)
310
- throw new error_util_1.ApiError('E05', `existing release order found: ${foundSplittedReleaseGood.name}`);
311
- }
312
- else {
313
- throw new error_util_1.ApiError('E05', `existing release order found: ${foundReleaseGood.name}`);
314
- }
315
- }
316
- }
317
- }
318
- }
319
- async function createDraftOrder(releaseGood, orderProducts, context) {
320
- return await (0, typeorm_1.getConnection)().transaction(async (tx) => {
321
- return await (0, sales_base_1.createDraftReleaseGoodFunction)(null, releaseGood, orderProducts, null, [], context, tx);
322
- });
323
- }
324
- async function createReleaseGood(releaseGood, orderProducts, bizplace, context, tx) {
325
- const { domain, user } = context.state;
326
- const settingRepo = (tx === null || tx === void 0 ? void 0 : tx.getRepository(setting_base_1.Setting)) || (0, typeorm_1.getRepository)(setting_base_1.Setting);
327
- const geoCountryRepo = (tx === null || tx === void 0 ? void 0 : tx.getRepository(geography_1.GeoCountry)) || (0, typeorm_1.getRepository)(geography_1.GeoCountry);
328
- if (orderProducts.length <= 0) {
329
- throw new error_util_1.ApiError('E04', `no order items found: ${JSON.stringify(releaseGood)}`);
330
- }
331
- let newReleaseGood = new sales_base_1.ReleaseGood();
332
- // find RO number rule setting
333
- const roNoSetting = await settingRepo.findOne({
334
- where: {
335
- domain,
336
- name: sales_base_1.ORDER_NUMBER_SETTING_KEY.RO_NUMBER_RULE
337
- }
338
- });
339
- let shippingOrder;
340
- if (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.shippingOrder) {
341
- shippingOrder = await tx.getRepository(sales_base_1.ShippingOrder).save({
342
- name: sales_base_1.OrderNoGenerator.shippingOrder(),
343
- shipName: releaseGood.shippingOrder.shipName,
344
- containerNo: releaseGood.shippingOrder.containerNo,
345
- loadType: releaseGood.shippingOrder.loadType,
346
- from: releaseGood.shippingOrder.from,
347
- to: releaseGood.shippingOrder.to,
348
- containerArrivalDate: releaseGood.shippingOrder.containerArrivalDate,
349
- containerLeavingDate: releaseGood.shippingOrder.containerLeavingDate,
350
- status: sales_base_1.ORDER_STATUS.PENDING,
351
- creator: user,
352
- updater: user
353
- });
354
- }
355
- // standardise country
356
- let deliveryCountry = (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.country)
357
- ? await geoCountryRepo.findOne({
358
- where: [{ name: (0, typeorm_1.ILike)(releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.country) }, { description: (0, typeorm_1.ILike)(releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.country) }]
359
- })
360
- : null;
361
- let platformCountry = (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.platformCountry)
362
- ? await geoCountryRepo.findOne({
363
- where: [{ name: (0, typeorm_1.ILike)(releaseGood.platformCountry) }, { description: (0, typeorm_1.ILike)(releaseGood.platformCountry) }]
364
- })
365
- : null;
366
- newReleaseGood = {
367
- name: roNoSetting
368
- ? await (0, id_rule_base_1.generateId)({ domain, type: sales_base_1.ORDER_NUMBER_RULE_TYPE.RO_NUMBER, seed: {} })
369
- : sales_base_1.OrderNoGenerator.releaseGood(),
370
- domain: domain,
371
- bizplace: bizplace,
372
- collectionOrderNo: releaseGood.collectionOrderNo,
373
- courierOption: releaseGood.courierOption,
374
- exportOption: releaseGood.exportOption,
375
- ownTransport: releaseGood.ownTransport,
376
- packingOption: releaseGood.packingOption,
377
- marketplaceOrderStatus: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.marketplaceOrderStatus) || null,
378
- billingAddress: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.billingAddress) || null,
379
- deliveryAddress1: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress1) || null,
380
- deliveryAddress2: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress2) || null,
381
- deliveryAddress3: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress3) || null,
382
- deliveryAddress4: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress4) || null,
383
- deliveryAddress5: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.deliveryAddress5) || null,
384
- attentionTo: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.attentionTo) || null,
385
- attentionCompany: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.attentionCompany) || null,
386
- city: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.city) || null,
387
- state: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.state) || null,
388
- postalCode: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.postalCode) || null,
389
- country: (deliveryCountry === null || deliveryCountry === void 0 ? void 0 : deliveryCountry.description) || (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.country) || null,
390
- phone1: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.phone1) || null,
391
- phone2: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.phone2) || null,
392
- email: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.email) || null,
393
- transporter: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.transporter,
394
- trackingNo: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.trackingNo,
395
- airwayBill: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.airwayBill,
396
- invoice: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.invoice,
397
- refNo: releaseGood.refNo,
398
- refNo2: releaseGood.refNo2 || null,
399
- refNo3: releaseGood.refNo3 || null,
400
- remark: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.remark) || null,
401
- releaseDate: releaseGood.releaseDate,
402
- truckNo: releaseGood.truckNo,
403
- type: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.type) ? releaseGood.type : 'b2b',
404
- status: sales_base_1.ORDER_STATUS.PENDING_WORKSHEET,
405
- storeName: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.storeName,
406
- storeId: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.storeId,
407
- routeId: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.routeId,
408
- stopId: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.stopId,
409
- packageId: (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.packageId) || null,
410
- noOfItems: orderProducts.length,
411
- source: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.source,
412
- platformCode: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.platformCode,
413
- platformCountry: (platformCountry === null || platformCountry === void 0 ? void 0 : platformCountry.description) || (releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.platformCountry) || null,
414
- platformName: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.platformName,
415
- discountAmount: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.discountAmount,
416
- taxAmount: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.taxAmount,
417
- shippingFee: releaseGood === null || releaseGood === void 0 ? void 0 : releaseGood.shippingFee,
418
- creator: user,
419
- updater: user,
420
- acceptedBy: user,
421
- acceptedAt: new Date(),
422
- shippingOrder: shippingOrder ? shippingOrder : null
423
- };
424
- let resultReleaseGood = await tx.getRepository(sales_base_1.ReleaseGood).save(newReleaseGood);
425
- let combinedOrderInventories = [];
426
- let savedOrderProducts = await Promise.all(orderProducts.map(async (orderProduct) => {
427
- let orderInventories = orderProduct.orderInventories;
428
- orderProduct = Object.assign(new sales_base_1.OrderProduct(), Object.assign(Object.assign({}, orderProduct), { name: (0, uuid_1.v4)(), domain: domain, bizplace: bizplace, releaseGood: resultReleaseGood, type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS, status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED }));
429
- let result = await tx.getRepository(sales_base_1.OrderProduct).save(orderProduct);
430
- for (let oiIdx = 0; oiIdx < orderInventories.length; oiIdx++) {
431
- let orderInventory = Object.assign(new sales_base_1.OrderInventory(), Object.assign(Object.assign({}, orderInventories[oiIdx]), { name: (0, uuid_1.v4)(), domain: domain, bizplace: bizplace, releaseGood: resultReleaseGood, type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS, status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET }));
432
- await tx.getRepository(sales_base_1.OrderInventory).save(orderInventory);
433
- combinedOrderInventories.push({
434
- product: { name: orderInventory.product.name, sku: orderInventory.product.sku },
435
- batchId: orderInventory.batchId,
436
- packingType: orderInventory.packingType,
437
- packingSize: orderInventory.packingSize,
438
- releaseQty: orderInventory.releaseQty,
439
- releaseUomValue: orderInventory.releaseUomValue
440
- });
441
- }
442
- return result;
443
- }));
444
- newReleaseGood.orderProducts = savedOrderProducts;
445
- newReleaseGood.orderInventories = combinedOrderInventories;
446
- return newReleaseGood;
447
- }
448
- async function massageOrderItems(releaseGood, inputOrderProducts, context) {
449
- const { t } = context;
450
- let orderProducts = [];
451
- //combine any repeated skus
452
- inputOrderProducts = inputOrderProducts.reduce((acc, itm, idx) => {
453
- let { sku, refCode } = itm.product;
454
- let { packingType, packingSize, batchId, releaseQty, sellingPrice, paidAmount } = itm;
455
- if (!sku && !refCode) {
456
- throw new error_util_1.ApiError('E01', 'sku or refCode not found');
457
- }
458
- if (releaseQty <= 0) {
459
- throw new error_util_1.ApiError('E01', 'negative stock request');
460
- }
461
- if (releaseQty % 1 !== 0)
462
- throw new error_util_1.ApiError('E01', `releaseQty is not an integer: ${releaseQty}`);
463
- let existingIndex = acc.findIndex(itm => (itm === null || itm === void 0 ? void 0 : itm.sku) == (sku === null || sku === void 0 ? void 0 : sku.trim()) &&
464
- (itm === null || itm === void 0 ? void 0 : itm.refCode) == (refCode === null || refCode === void 0 ? void 0 : refCode.trim()) &&
465
- (itm === null || itm === void 0 ? void 0 : itm.packingType) == (packingType === null || packingType === void 0 ? void 0 : packingType.trim()) &&
466
- (itm === null || itm === void 0 ? void 0 : itm.packingSize) == packingSize);
467
- if (existingIndex >= 0 && releaseQty > 0) {
468
- acc[existingIndex].releaseQty = acc[existingIndex].releaseQty + releaseQty;
469
- }
470
- else {
471
- acc.push({
472
- sku: sku === null || sku === void 0 ? void 0 : sku.trim(),
473
- refCode: refCode === null || refCode === void 0 ? void 0 : refCode.trim(),
474
- packingType: packingType === null || packingType === void 0 ? void 0 : packingType.trim(),
475
- packingSize: packingSize,
476
- releaseQty: releaseQty,
477
- sellingPrice: sellingPrice,
478
- paidAmount: paidAmount
479
- });
480
- }
481
- return acc;
482
- }, []);
483
- //find customer sku in system
484
- await Promise.all(inputOrderProducts.map(async (inputOrderItem) => {
485
- const sku = inputOrderItem.sku;
486
- const refCode = inputOrderItem.refCode;
487
- const packingType = inputOrderItem.packingType;
488
- const packingSize = inputOrderItem.packingSize;
489
- const qb = (0, typeorm_1.getRepository)(product_base_1.ProductDetail)
490
- .createQueryBuilder('PD')
491
- .innerJoinAndSelect('PD.product', 'PROD')
492
- .where('PROD.bizplace_id = :bizplaceId', { bizplaceId: releaseGood.companyBizplace.id });
493
- if (!sku && !refCode) {
494
- throw new error_util_1.ApiError('E04', t('sku or refCode not found'));
495
- }
496
- if (refCode) {
497
- qb.andWhere('PD.ref_code = :refCode', { refCode });
498
- }
499
- if (sku) {
500
- qb.andWhere('PROD.sku = :sku', { sku });
501
- }
502
- if (packingType) {
503
- qb.andWhere('PD.packing_type = :packingType', { packingType });
504
- }
505
- if ((!packingType && !refCode) || !packingSize) {
506
- qb.andWhere('PD.is_default = :isDefault', { isDefault: true });
507
- }
508
- const productDetail = await qb.getOne();
509
- // order Product based on product detail
510
- if (productDetail) {
511
- let newOrderProduct = Object.assign({}, inputOrderItem);
512
- newOrderProduct = Object.assign(Object.assign({}, newOrderProduct), { product: productDetail.product, productDetail: productDetail, packingType: productDetail.packingType, packingSize: packingSize || productDetail.packingSize, uom: productDetail.uom, uomValue: productDetail.uomValue, refCode: inputOrderItem.refCode, releaseQty: inputOrderItem.releaseQty, releaseUomValue: inputOrderItem.releaseQty * (productDetail.uomValue || 1), packQty: 0, actualPackQty: 0, palletQty: 0, actualPalletQty: 0, type: 'RELEASE_OF_GOODS', status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED, batchId: '-' });
513
- newOrderProduct.orderInventories = [
514
- Object.assign(Object.assign({}, newOrderProduct), { packQty: 0, actualPackQty: 0, palletQty: 0, actualPalletQty: 0, type: 'RELEASE_OF_GOODS', status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET })
515
- ];
516
- orderProducts.push(newOrderProduct);
517
- }
518
- if (!productDetail) {
519
- const qb = (0, typeorm_1.getRepository)(product_base_1.ProductBundle)
520
- .createQueryBuilder('PB')
521
- .innerJoinAndSelect('PB.productBundleSettings', 'PBS')
522
- .innerJoinAndSelect('PBS.productDetail', 'PBD')
523
- .innerJoinAndSelect('PBD.product', 'PROD')
524
- .where('PB.bizplace_id = :bizplaceId', { bizplaceId: releaseGood.companyBizplace.id });
525
- if (!sku && !refCode) {
526
- throw new error_util_1.ApiError('E04', 'sku or refCode not found');
527
- }
528
- if (refCode) {
529
- qb.andWhere('PB.ref_code = :refCode', { refCode });
530
- }
531
- if (sku) {
532
- qb.andWhere('PB.sku = :sku', { sku });
533
- }
534
- if (packingType) {
535
- qb.andWhere('PB.packing_type = :packingType', { packingType });
536
- }
537
- const foundProductBundle = await qb.getOne();
538
- if (foundProductBundle) {
539
- let orderProduct = {
540
- productBundle: foundProductBundle,
541
- refCode,
542
- packingType: foundProductBundle.packingType,
543
- packingSize: packingSize || 1,
544
- uom: inputOrderItem === null || inputOrderItem === void 0 ? void 0 : inputOrderItem.uom,
545
- releaseQty: inputOrderItem.releaseQty,
546
- releaseUomValue: inputOrderItem.releaseQty,
547
- packQty: 0,
548
- actualPackQty: 0,
549
- palletQty: 0,
550
- actualPalletQty: 0,
551
- orderInventories: [],
552
- type: 'RELEASE_OF_GOODS',
553
- status: sales_base_1.ORDER_PRODUCT_STATUS.ASSIGNED,
554
- batchId: '-'
555
- };
556
- const bundleProductSettings = foundProductBundle.productBundleSettings;
557
- bundleProductSettings.map(bundleProductSetting => {
558
- const productDetailBundle = bundleProductSetting.productDetail;
559
- let newOrderInventory = {
560
- product: productDetailBundle.product,
561
- productDetail: productDetailBundle,
562
- sku: productDetailBundle.product.sku,
563
- packingType: productDetailBundle.packingType,
564
- packingSize: productDetailBundle.packingSize,
565
- uom: productDetailBundle.uom,
566
- packQty: 0,
567
- actualPackQty: 0,
568
- palletQty: 0,
569
- actualPalletQty: 0,
570
- releaseQty: inputOrderItem.releaseQty * bundleProductSetting.bundleQty,
571
- releaseUomValue: inputOrderItem.releaseQty *
572
- bundleProductSetting.bundleQty *
573
- (productDetailBundle.uomValue <= 0 ? 1 : productDetailBundle.uomValue),
574
- type: 'RELEASE_OF_GOODS',
575
- status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET
576
- };
577
- orderProduct.orderInventories.push(newOrderInventory);
578
- });
579
- orderProducts.push(orderProduct);
580
- }
581
- else {
582
- if (sku && refCode) {
583
- throw new error_util_1.ApiError('E04', t(`error.sku ${sku} and refCode ${refCode} not exist in product / bundle master`));
584
- }
585
- if (sku) {
586
- throw new error_util_1.ApiError('E04', t(`error.sku ${sku} not exist in product / bundle master`));
587
- }
588
- if (refCode) {
589
- throw new error_util_1.ApiError('E04', t(`error.refCode ${refCode} not exist in product / bundle master`));
590
- }
591
- }
592
- }
593
- }));
594
- //combine any repeated skus
595
- let combinedItems = orderProducts.reduce((acc, op, idx) => {
596
- let oi = (op === null || op === void 0 ? void 0 : op.orderInventories) || [];
597
- oi.forEach(itm => {
598
- let { sku, refCode } = itm.product;
599
- let { packingType, packingSize, batchId, releaseQty, product, productDetail, uom } = itm;
600
- if (!sku && !refCode) {
601
- throw new error_util_1.ApiError('E04', t('error.sku or refCode not found'));
602
- }
603
- let existingIndex = acc.findIndex(itm => (itm === null || itm === void 0 ? void 0 : itm.sku) == (sku === null || sku === void 0 ? void 0 : sku.trim()) &&
604
- (itm === null || itm === void 0 ? void 0 : itm.refCode) == (refCode === null || refCode === void 0 ? void 0 : refCode.trim()) &&
605
- (itm === null || itm === void 0 ? void 0 : itm.packingType) == (packingType === null || packingType === void 0 ? void 0 : packingType.trim()) &&
606
- (itm === null || itm === void 0 ? void 0 : itm.packingSize) == packingSize &&
607
- (itm === null || itm === void 0 ? void 0 : itm.batchId) == (batchId === null || batchId === void 0 ? void 0 : batchId.trim()) &&
608
- (itm === null || itm === void 0 ? void 0 : itm.uom) == (uom === null || uom === void 0 ? void 0 : uom.trim()));
609
- if (existingIndex >= 0 && releaseQty > 0) {
610
- acc[existingIndex].releaseQty = acc[existingIndex].releaseQty + releaseQty;
611
- acc[existingIndex].releaseUomValue =
612
- acc[existingIndex].releaseUomValue + releaseQty * (productDetail.uomValue <= 0 ? 1 : productDetail.uomValue);
613
- }
614
- else {
615
- let updateObj = {
616
- sku: sku === null || sku === void 0 ? void 0 : sku.trim(),
617
- packingType: packingType === null || packingType === void 0 ? void 0 : packingType.trim(),
618
- packingSize: packingSize,
619
- uom: uom === null || uom === void 0 ? void 0 : uom.trim(),
620
- releaseQty: releaseQty,
621
- releaseUomValue: releaseQty * (productDetail.uomValue <= 0 ? 1 : productDetail.uomValue),
622
- product,
623
- productDetail
624
- };
625
- if (refCode)
626
- updateObj['refCode'] = refCode.trim();
627
- updateObj['batchId'] = batchId ? batchId === null || batchId === void 0 ? void 0 : batchId.trim() : '-';
628
- acc.push(updateObj);
629
- }
630
- });
631
- return acc;
632
- }, []);
633
- return { orderProducts, combinedItems };
634
- }
635
- async function assignToInventory(orderProducts, customerBizplace, context, tx) {
636
- const { client, domain, user } = context.state;
637
- for (let opIdx = 0; opIdx < orderProducts.length; opIdx++) {
638
- let orderProduct = orderProducts[opIdx];
639
- let assignedOrderInventories = [];
640
- const pickingProductSetting = await tx.getRepository(setting_base_1.Setting).findOne({
641
- where: { domain, name: 'rule-for-picking-product' }
642
- });
643
- for (let oiIdx = 0; oiIdx < orderProducts[opIdx].orderInventories.length; oiIdx++) {
644
- const orderInventory = orderProduct.orderInventories[oiIdx];
645
- let sortings = [];
646
- switch (orderInventory.product.pickingStrategy) {
647
- case 'LIFO':
648
- sortings.push({ name: 'iv.created_at', desc: true });
649
- if (pickingProductSetting === null || pickingProductSetting === void 0 ? void 0 : pickingProductSetting.value) {
650
- for (const [key, value] of Object.entries(JSON.parse(pickingProductSetting.value))) {
651
- sortings.push({ name: `loc.${key}`, desc: value == 'DESC' ? true : false });
652
- }
653
- }
654
- else {
655
- sortings.push({ name: 'loc.name', desc: false }, { name: 'iv.created_at', desc: false });
656
- }
657
- break;
658
- case 'FEFO':
659
- sortings.push({ name: 'iv.expiration_date', desc: false }, { name: 'iv.created_at', desc: false });
660
- if (pickingProductSetting === null || pickingProductSetting === void 0 ? void 0 : pickingProductSetting.value) {
661
- for (const [key, value] of Object.entries(JSON.parse(pickingProductSetting.value))) {
662
- sortings.push({ name: `loc.${key}`, desc: value == 'DESC' ? true : false });
663
- }
664
- }
665
- else {
666
- sortings.push({ name: 'loc.name', desc: false }, { name: 'iv.created_at', desc: false });
667
- }
668
- break;
669
- case 'FMFO':
670
- sortings.push({ name: 'iv.manufacture_date', desc: false }, { name: 'iv.created_at', desc: false });
671
- if (pickingProductSetting === null || pickingProductSetting === void 0 ? void 0 : pickingProductSetting.value) {
672
- for (const [key, value] of Object.entries(JSON.parse(pickingProductSetting.value))) {
673
- sortings.push({ name: `loc.${key}`, desc: value == 'DESC' ? true : false });
674
- }
675
- }
676
- else {
677
- sortings.push({ name: 'loc.name', desc: false }, { name: 'iv.created_at', desc: false });
678
- }
679
- break;
680
- case 'LOCATION':
681
- if (pickingProductSetting === null || pickingProductSetting === void 0 ? void 0 : pickingProductSetting.value) {
682
- for (const [key, value] of Object.entries(JSON.parse(pickingProductSetting.value))) {
683
- sortings.push({ name: `loc.${key}`, desc: value == 'DESC' ? true : false });
684
- }
685
- }
686
- else {
687
- sortings.push({ name: 'loc.name', desc: false }, { name: 'iv.created_at', desc: false });
688
- }
689
- break;
690
- //Every other case includes 'FIFO' will be applicable for this case
691
- default:
692
- sortings.push({ name: 'iv.created_at', desc: false });
693
- if (pickingProductSetting === null || pickingProductSetting === void 0 ? void 0 : pickingProductSetting.value) {
694
- for (const [key, value] of Object.entries(JSON.parse(pickingProductSetting.value))) {
695
- sortings.push({ name: `loc.${key}`, desc: value == 'DESC' ? true : false });
696
- }
697
- }
698
- else {
699
- sortings.push({ name: 'loc.name', desc: false });
700
- sortings.push({ name: 'iv.pallet_id', desc: false });
701
- }
702
- break;
703
- }
704
- let queryFilters = [];
705
- let queryStrings = queryFilters.reduce((acc, itm, idx, arr) => {
706
- acc.values.push(itm.filters);
707
- switch (itm === null || itm === void 0 ? void 0 : itm.operator) {
708
- case 'notin':
709
- case 'in':
710
- acc.query.push(`${itm.query} ${itm.operator == 'notin' ? 'NOT IN' : 'IN'} (${itm.filtes
711
- .map((itm, idx) => {
712
- return `$${acc.values.length + 1}`;
713
- })
714
- .join(',')})`);
715
- break;
716
- default:
717
- acc.query.push(`${itm.query} ${(itm === null || itm === void 0 ? void 0 : itm.operator) ? itm.operator : '='} $${acc.values.length + 1}`);
718
- break;
719
- }
720
- acc.query.push(`${itm.query} ${(itm === null || itm === void 0 ? void 0 : itm.operator) ? itm.operator : '='} $${acc.values.length + 1}`);
721
- }, {
722
- query: [],
723
- values: []
724
- });
725
- let sortQuery = sortings
726
- .map(itm => {
727
- return `${itm.name} ${itm.desc ? 'DESC' : 'ASC'}`;
728
- })
729
- .join(`,`);
730
- let updatedInventories = await tx.getRepository(warehouse_base_1.Inventory).query(`
731
- update inventories tgt set locked_qty = coalesce(locked_qty,0) + src.reserve_qty,
732
- locked_uom_value = coalesce(locked_uom_value,0) + src.reserve_uom_value,
733
- updated_at = NOW(),
734
- updater_id = $1
735
- from (
736
- select "id", "pallet_id","carton_id", "batch_id", "batch_id_ref", "unit", "uom", "packing_type", "packing_size", "manufacture_year",
737
- "reserve_qty", (("uom_value"/"qty") * "reserve_qty") as "reserve_uom_value" from (
738
- select "sort_seq", "id", "pallet_id", "batch_id", "batch_id_ref", "unit", "uom", "packing_type", "packing_size", "manufacture_year", "carton_id", "uom_value", "locked_uom_value", "qty", "locked_qty", "created_at",
739
- "release_qty", "release_uom_value", lock_amount,
740
- case when "lock_amount" < 0 then "available_qty" else "available_qty" - "lock_amount" end as "reserve_qty"
741
- from (
742
- select *,
743
- qty - locked_qty as available_qty,
744
- sum(qty - locked_qty - release_qty) over (order by sort_seq asc rows between unbounded preceding and current row) as lock_amount
745
- from (
746
- SELECT 0 as sort_seq, null as id, null as pallet_id, null as batch_id, null as batch_id_ref,
747
- null as unit, '${orderInventory.uom}' as uom, '${orderInventory.packingType}' as packing_type, '${orderInventory.packingSize}' as packing_size,
748
- null as manufacture_year, null as carton_id, 0 as uom_value, 0 as locked_uom_value, 0 as qty, 0 as locked_qty, null as created_at,
749
- '${orderInventory.releaseQty}' as release_qty, '${orderInventory.releaseUomValue}' as release_uom_value
750
- UNION
751
- (
752
- SELECT ROW_NUMBER() OVER(PARTITION BY iv.domain_id ORDER BY ${sortQuery}) as sort_seq,
753
- iv.id, iv.pallet_id, iv.batch_id, iv.batch_id_ref,
754
- iv.unit, iv.uom, iv.packing_type, iv.packing_size,
755
- iv.manufacture_year, iv.carton_id, iv.uom_value,
756
- coalesce(iv.locked_uom_value,0) as locked_uom_value, iv.qty, coalesce(iv.locked_qty,0) as locked_qty, iv.created_at,
757
- 0 as release_qty, 0 as release_uom_value
758
- FROM "inventories" "iv"
759
- INNER JOIN "locations" "loc" ON "loc"."id"="iv"."location_id"
760
- INNER JOIN "products" "p" ON "p"."id"="iv"."product_id"
761
- WHERE case when coalesce("iv"."locked_qty",0) < 0 then 0 else ("iv"."qty" - coalesce("iv"."locked_qty",0)) end > 0 AND
762
- "iv"."domain_id" = $2 AND "iv"."bizplace_id" = $3 AND "iv"."packing_type" = $4 AND "iv"."packing_size" = $5 AND "iv"."product_id" = $6 AND "iv"."status" = $7 AND "loc"."type" NOT IN ($8, $9)
763
- AND "iv"."obsolete" = false AND case when "iv"."expiration_date" is not null and "p"."shelf_life" is not null then CURRENT_DATE < "iv"."expiration_date" - "p"."shelf_life" else true end
764
- ${queryStrings.query.length > 0 ? `AND ${queryStrings.join(' AND ')}` : ''}
765
- ORDER BY ${sortQuery}
766
- )
767
- ) dt1
768
- ) dt2 where case when "lock_amount" < 0 then "available_qty" else "available_qty" - "lock_amount" end > 0
769
- ) dt3 where sort_seq > 0
770
- ) src where src.id = tgt.id
771
- returning
772
- tgt."id", tgt."qty", tgt."pallet_id", tgt."carton_id", tgt."batch_id", tgt."batch_id_ref", tgt."unit",
773
- tgt."uom", tgt."packing_type", tgt."packing_size", tgt."manufacture_year",
774
- tgt."locked_qty", tgt."uom_value", tgt."locked_uom_value", src."reserve_qty", src."reserve_uom_value";`, [
775
- user.id,
776
- domain.id,
777
- customerBizplace.id,
778
- orderInventory.packingType,
779
- orderInventory.packingSize,
780
- orderInventory.product.id,
781
- warehouse_base_1.INVENTORY_STATUS.STORED,
782
- warehouse_base_1.LOCATION_TYPE.QUARANTINE,
783
- warehouse_base_1.LOCATION_TYPE.RESERVE,
784
- ...queryStrings.values
785
- ]);
786
- let totalAssigned = updatedInventories[0].reduce((acc, inv) => {
787
- return acc + inv.reserve_qty;
788
- }, 0);
789
- if (orderInventory.releaseQty != totalAssigned) {
790
- throw new error_util_1.ApiError('E01', 'INSUFFICIENT_STOCK');
791
- }
792
- updatedInventories[0].forEach(inv => {
793
- let oi = Object.assign(Object.assign({}, orderInventory), { inventory: { id: inv.id }, uom: inv.uom, packingType: inv.packing_type, batchId: inv.batch_id, releaseQty: inv.reserve_qty, releaseUomValue: inv.reserve_uom_value, pickedQty: 0, packedQty: 0, type: sales_base_1.ORDER_TYPES.RELEASE_OF_GOODS, status: sales_base_1.ORDER_INVENTORY_STATUS.PENDING_WORKSHEET, domain: { id: domain.id }, bizplace: { id: customerBizplace.id } });
794
- assignedOrderInventories.push(oi);
795
- });
796
- }
797
- orderProducts[opIdx].orderInventories = assignedOrderInventories;
798
- }
799
- return orderProducts;
800
- }
801
- //# sourceMappingURL=add-release-order.js.map