@things-factory/warehouse-base 4.3.651 → 4.3.652
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.
- package/dist-server/constants/adjustment-code.js +13 -0
- package/dist-server/constants/adjustment-code.js.map +1 -0
- package/dist-server/constants/index.js +23 -0
- package/dist-server/constants/index.js.map +1 -0
- package/dist-server/constants/inventory.js +74 -0
- package/dist-server/constants/inventory.js.map +1 -0
- package/dist-server/constants/location.js +19 -0
- package/dist-server/constants/location.js.map +1 -0
- package/dist-server/constants/pallet.js +13 -0
- package/dist-server/constants/pallet.js.map +1 -0
- package/dist-server/constants/rule-type.js +8 -0
- package/dist-server/constants/rule-type.js.map +1 -0
- package/dist-server/constants/tote.js +9 -0
- package/dist-server/constants/tote.js.map +1 -0
- package/dist-server/controllers/ecommerce/ecommerce-controller.js +125 -0
- package/dist-server/controllers/ecommerce/ecommerce-controller.js.map +1 -0
- package/dist-server/controllers/ecommerce/index.js +22 -0
- package/dist-server/controllers/ecommerce/index.js.map +1 -0
- package/dist-server/controllers/ecommerce/pos-controller.js +65 -0
- package/dist-server/controllers/ecommerce/pos-controller.js.map +1 -0
- package/dist-server/controllers/ecommerce/powrup-controller.js +87 -0
- package/dist-server/controllers/ecommerce/powrup-controller.js.map +1 -0
- package/dist-server/controllers/ecommerce/sellercraft-controller.js +85 -0
- package/dist-server/controllers/ecommerce/sellercraft-controller.js.map +1 -0
- package/dist-server/controllers/ecommerce/webspert-controller.js +131 -0
- package/dist-server/controllers/ecommerce/webspert-controller.js.map +1 -0
- package/dist-server/controllers/index.js +19 -0
- package/dist-server/controllers/index.js.map +1 -0
- package/dist-server/controllers/warehouse-controller.js +143 -0
- package/dist-server/controllers/warehouse-controller.js.map +1 -0
- package/dist-server/index.js +35 -0
- package/dist-server/index.js.map +1 -0
- package/dist-server/middlewares/index.js +1 -0
- package/dist-server/middlewares/index.js.map +1 -0
- package/dist-server/migrations/index.js +12 -0
- package/dist-server/migrations/index.js.map +1 -0
- package/dist-server/service/index.js +96 -0
- package/dist-server/service/index.js.map +1 -0
- package/dist-server/service/inventory/index.js +9 -0
- package/dist-server/service/inventory/index.js.map +1 -0
- package/dist-server/service/inventory/inventory-mutation.js +414 -0
- package/dist-server/service/inventory/inventory-mutation.js.map +1 -0
- package/dist-server/service/inventory/inventory-query.js +1211 -0
- package/dist-server/service/inventory/inventory-query.js.map +1 -0
- package/dist-server/service/inventory/inventory-types.js +478 -0
- package/dist-server/service/inventory/inventory-types.js.map +1 -0
- package/dist-server/service/inventory/inventory.js +569 -0
- package/dist-server/service/inventory/inventory.js.map +1 -0
- package/dist-server/service/inventory-change/index.js +9 -0
- package/dist-server/service/inventory-change/index.js.map +1 -0
- package/dist-server/service/inventory-change/inventory-change-mutation.js +1188 -0
- package/dist-server/service/inventory-change/inventory-change-mutation.js.map +1 -0
- package/dist-server/service/inventory-change/inventory-change-query.js +150 -0
- package/dist-server/service/inventory-change/inventory-change-query.js.map +1 -0
- package/dist-server/service/inventory-change/inventory-change-types.js +73 -0
- package/dist-server/service/inventory-change/inventory-change-types.js.map +1 -0
- package/dist-server/service/inventory-change/inventory-change.js +225 -0
- package/dist-server/service/inventory-change/inventory-change.js.map +1 -0
- package/dist-server/service/inventory-history/index.js +9 -0
- package/dist-server/service/inventory-history/index.js.map +1 -0
- package/dist-server/service/inventory-history/inventory-history-mutation.js +121 -0
- package/dist-server/service/inventory-history/inventory-history-mutation.js.map +1 -0
- package/dist-server/service/inventory-history/inventory-history-query.js +1694 -0
- package/dist-server/service/inventory-history/inventory-history-query.js.map +1 -0
- package/dist-server/service/inventory-history/inventory-history-types.js +626 -0
- package/dist-server/service/inventory-history/inventory-history-types.js.map +1 -0
- package/dist-server/service/inventory-history/inventory-history.js +238 -0
- package/dist-server/service/inventory-history/inventory-history.js.map +1 -0
- package/dist-server/service/inventory-item/index.js +9 -0
- package/dist-server/service/inventory-item/index.js.map +1 -0
- package/dist-server/service/inventory-item/inventory-item-mutation.js +274 -0
- package/dist-server/service/inventory-item/inventory-item-mutation.js.map +1 -0
- package/dist-server/service/inventory-item/inventory-item-query.js +265 -0
- package/dist-server/service/inventory-item/inventory-item-query.js.map +1 -0
- package/dist-server/service/inventory-item/inventory-item-type.js +127 -0
- package/dist-server/service/inventory-item/inventory-item-type.js.map +1 -0
- package/dist-server/service/inventory-item/inventory-item.js +146 -0
- package/dist-server/service/inventory-item/inventory-item.js.map +1 -0
- package/dist-server/service/inventory-item-change/index.js +9 -0
- package/dist-server/service/inventory-item-change/index.js.map +1 -0
- package/dist-server/service/inventory-item-change/inventory-item-change-mutation.js +121 -0
- package/dist-server/service/inventory-item-change/inventory-item-change-mutation.js.map +1 -0
- package/dist-server/service/inventory-item-change/inventory-item-change-query.js +87 -0
- package/dist-server/service/inventory-item-change/inventory-item-change-query.js.map +1 -0
- package/dist-server/service/inventory-item-change/inventory-item-change-type.js +107 -0
- package/dist-server/service/inventory-item-change/inventory-item-change-type.js.map +1 -0
- package/dist-server/service/inventory-item-change/inventory-item-change.js +110 -0
- package/dist-server/service/inventory-item-change/inventory-item-change.js.map +1 -0
- package/dist-server/service/inventory-product/index.js +9 -0
- package/dist-server/service/inventory-product/index.js.map +1 -0
- package/dist-server/service/inventory-product/inventory-product-mutation.js +120 -0
- package/dist-server/service/inventory-product/inventory-product-mutation.js.map +1 -0
- package/dist-server/service/inventory-product/inventory-product-query.js +87 -0
- package/dist-server/service/inventory-product/inventory-product-query.js.map +1 -0
- package/dist-server/service/inventory-product/inventory-product-type.js +95 -0
- package/dist-server/service/inventory-product/inventory-product-type.js.map +1 -0
- package/dist-server/service/inventory-product/inventory-product.js +112 -0
- package/dist-server/service/inventory-product/inventory-product.js.map +1 -0
- package/dist-server/service/location/index.js +9 -0
- package/dist-server/service/location/index.js.map +1 -0
- package/dist-server/service/location/location-mutation.js +279 -0
- package/dist-server/service/location/location-mutation.js.map +1 -0
- package/dist-server/service/location/location-query.js +320 -0
- package/dist-server/service/location/location-query.js.map +1 -0
- package/dist-server/service/location/location-types.js +296 -0
- package/dist-server/service/location/location-types.js.map +1 -0
- package/dist-server/service/location/location.js +146 -0
- package/dist-server/service/location/location.js.map +1 -0
- package/dist-server/service/movement/index.js +9 -0
- package/dist-server/service/movement/index.js.map +1 -0
- package/dist-server/service/movement/movement-mutation.js +80 -0
- package/dist-server/service/movement/movement-mutation.js.map +1 -0
- package/dist-server/service/movement/movement-query.js +286 -0
- package/dist-server/service/movement/movement-query.js.map +1 -0
- package/dist-server/service/movement/movement-types.js +117 -0
- package/dist-server/service/movement/movement-types.js.map +1 -0
- package/dist-server/service/movement/movement.js +101 -0
- package/dist-server/service/movement/movement.js.map +1 -0
- package/dist-server/service/pallet/index.js +9 -0
- package/dist-server/service/pallet/index.js.map +1 -0
- package/dist-server/service/pallet/pallet-mutation.js +194 -0
- package/dist-server/service/pallet/pallet-mutation.js.map +1 -0
- package/dist-server/service/pallet/pallet-query.js +164 -0
- package/dist-server/service/pallet/pallet-query.js.map +1 -0
- package/dist-server/service/pallet/pallet-types.js +109 -0
- package/dist-server/service/pallet/pallet-types.js.map +1 -0
- package/dist-server/service/pallet/pallet.js +112 -0
- package/dist-server/service/pallet/pallet.js.map +1 -0
- package/dist-server/service/pallet-count/index.js +9 -0
- package/dist-server/service/pallet-count/index.js.map +1 -0
- package/dist-server/service/pallet-count/pallet-count-mutation.js +153 -0
- package/dist-server/service/pallet-count/pallet-count-mutation.js.map +1 -0
- package/dist-server/service/pallet-count/pallet-count-query.js +86 -0
- package/dist-server/service/pallet-count/pallet-count-query.js.map +1 -0
- package/dist-server/service/pallet-count/pallet-count-types.js +65 -0
- package/dist-server/service/pallet-count/pallet-count-types.js.map +1 -0
- package/dist-server/service/pallet-count/pallet-count.js +89 -0
- package/dist-server/service/pallet-count/pallet-count.js.map +1 -0
- package/dist-server/service/pallet-history/index.js +9 -0
- package/dist-server/service/pallet-history/index.js.map +1 -0
- package/dist-server/service/pallet-history/pallet-history-mutation.js +120 -0
- package/dist-server/service/pallet-history/pallet-history-mutation.js.map +1 -0
- package/dist-server/service/pallet-history/pallet-history-query.js +87 -0
- package/dist-server/service/pallet-history/pallet-history-query.js.map +1 -0
- package/dist-server/service/pallet-history/pallet-history-types.js +65 -0
- package/dist-server/service/pallet-history/pallet-history-types.js.map +1 -0
- package/dist-server/service/pallet-history/pallet-history.js +107 -0
- package/dist-server/service/pallet-history/pallet-history.js.map +1 -0
- package/dist-server/service/product-detail-stock/index.js +9 -0
- package/dist-server/service/product-detail-stock/index.js.map +1 -0
- package/dist-server/service/product-detail-stock/product-detail-stock-mutation.js +120 -0
- package/dist-server/service/product-detail-stock/product-detail-stock-mutation.js.map +1 -0
- package/dist-server/service/product-detail-stock/product-detail-stock-query.js +66 -0
- package/dist-server/service/product-detail-stock/product-detail-stock-query.js.map +1 -0
- package/dist-server/service/product-detail-stock/product-detail-stock-types.js +57 -0
- package/dist-server/service/product-detail-stock/product-detail-stock-types.js.map +1 -0
- package/dist-server/service/product-detail-stock/product-detail-stock.js +90 -0
- package/dist-server/service/product-detail-stock/product-detail-stock.js.map +1 -0
- package/dist-server/service/reduced-inventory-history/index.js +6 -0
- package/dist-server/service/reduced-inventory-history/index.js.map +1 -0
- package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js +133 -0
- package/dist-server/service/reduced-inventory-history/reduced-inventory-history.js.map +1 -0
- package/dist-server/service/tote/index.js +9 -0
- package/dist-server/service/tote/index.js.map +1 -0
- package/dist-server/service/tote/tote-mutation.js +192 -0
- package/dist-server/service/tote/tote-mutation.js.map +1 -0
- package/dist-server/service/tote/tote-query.js +162 -0
- package/dist-server/service/tote/tote-query.js.map +1 -0
- package/dist-server/service/tote/tote-types.js +75 -0
- package/dist-server/service/tote/tote-types.js.map +1 -0
- package/dist-server/service/tote/tote.js +98 -0
- package/dist-server/service/tote/tote.js.map +1 -0
- package/dist-server/service/warehouse/index.js +9 -0
- package/dist-server/service/warehouse/index.js.map +1 -0
- package/dist-server/service/warehouse/warehouse-mutation.js +148 -0
- package/dist-server/service/warehouse/warehouse-mutation.js.map +1 -0
- package/dist-server/service/warehouse/warehouse-query.js +91 -0
- package/dist-server/service/warehouse/warehouse-query.js.map +1 -0
- package/dist-server/service/warehouse/warehouse-types.js +82 -0
- package/dist-server/service/warehouse/warehouse-types.js.map +1 -0
- package/dist-server/service/warehouse/warehouse.js +114 -0
- package/dist-server/service/warehouse/warehouse.js.map +1 -0
- package/dist-server/utils/datetime-util.js +61 -0
- package/dist-server/utils/datetime-util.js.map +1 -0
- package/dist-server/utils/index.js +20 -0
- package/dist-server/utils/index.js.map +1 -0
- package/dist-server/utils/inventory-no-generator.js +17 -0
- package/dist-server/utils/inventory-no-generator.js.map +1 -0
- package/dist-server/utils/inventory-util.js +413 -0
- package/dist-server/utils/inventory-util.js.map +1 -0
- package/package.json +6 -6
|
@@ -0,0 +1,1188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.approveInventoryChanges = exports.InventoryChangeMutation = void 0;
|
|
16
|
+
const type_graphql_1 = require("type-graphql");
|
|
17
|
+
const typeorm_1 = require("typeorm");
|
|
18
|
+
const auth_base_1 = require("@things-factory/auth-base");
|
|
19
|
+
const biz_base_1 = require("@things-factory/biz-base");
|
|
20
|
+
const id_rule_base_1 = require("@things-factory/id-rule-base");
|
|
21
|
+
const integration_accounting_1 = require("@things-factory/integration-accounting");
|
|
22
|
+
const integration_marketplace_1 = require("@things-factory/integration-marketplace");
|
|
23
|
+
const integration_sellercraft_1 = require("@things-factory/integration-sellercraft");
|
|
24
|
+
const product_base_1 = require("@things-factory/product-base");
|
|
25
|
+
const setting_base_1 = require("@things-factory/setting-base");
|
|
26
|
+
const constants_1 = require("../../constants");
|
|
27
|
+
const controllers_1 = require("../../controllers");
|
|
28
|
+
const utils_1 = require("../../utils");
|
|
29
|
+
const inventory_history_1 = require("../inventory-history/inventory-history");
|
|
30
|
+
const inventory_item_change_1 = require("../inventory-item-change/inventory-item-change");
|
|
31
|
+
const inventory_item_1 = require("../inventory-item/inventory-item");
|
|
32
|
+
const inventory_item_query_1 = require("../inventory-item/inventory-item-query");
|
|
33
|
+
const inventory_1 = require("../inventory/inventory");
|
|
34
|
+
const inventory_types_1 = require("../inventory/inventory-types");
|
|
35
|
+
const location_1 = require("../location/location");
|
|
36
|
+
const inventory_change_1 = require("./inventory-change");
|
|
37
|
+
const inventory_change_types_1 = require("./inventory-change-types");
|
|
38
|
+
let InventoryChangeMutation = class InventoryChangeMutation {
|
|
39
|
+
async createInventoryChange(inventoryChange, context) {
|
|
40
|
+
const { domain, user, tx } = context.state;
|
|
41
|
+
return await tx.getRepository(inventory_change_1.InventoryChange).save(Object.assign(Object.assign({}, inventoryChange), { domain: domain, creator: user, updater: user }));
|
|
42
|
+
}
|
|
43
|
+
async updateInventoryChange(name, patch, context) {
|
|
44
|
+
const { domain, user, tx } = context.state;
|
|
45
|
+
const repository = tx.getRepository(inventory_change_1.InventoryChange);
|
|
46
|
+
const inventoryChange = await repository.findOne({
|
|
47
|
+
where: { domain: domain, name }
|
|
48
|
+
});
|
|
49
|
+
return await repository.save(Object.assign(Object.assign(Object.assign({}, inventoryChange), patch), { updater: user }));
|
|
50
|
+
}
|
|
51
|
+
async updateMultipleInventoryChange(patches, context) {
|
|
52
|
+
const { domain, user, tx } = context.state;
|
|
53
|
+
let results = [];
|
|
54
|
+
const _createRecords = patches.filter((patch) => patch.cuFlag.toUpperCase() === '+');
|
|
55
|
+
const _updateRecords = patches.filter((patch) => patch.cuFlag.toUpperCase() === 'M');
|
|
56
|
+
const inventoryChangeRepo = tx.getRepository(inventory_change_1.InventoryChange);
|
|
57
|
+
if (_createRecords.length > 0) {
|
|
58
|
+
for (let i = 0; i < _createRecords.length; i++) {
|
|
59
|
+
const newRecord = _createRecords[i];
|
|
60
|
+
const result = await inventoryChangeRepo.save(Object.assign(Object.assign({}, newRecord), { domain: domain, creator: user, updater: user }));
|
|
61
|
+
results.push(Object.assign(Object.assign({}, result), { cuFlag: '+' }));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (_updateRecords.length > 0) {
|
|
65
|
+
for (let i = 0; i < _updateRecords.length; i++) {
|
|
66
|
+
const newRecord = _updateRecords[i];
|
|
67
|
+
const inventoryChange = await inventoryChangeRepo.findOne(newRecord.id);
|
|
68
|
+
const result = await inventoryChangeRepo.save(Object.assign(Object.assign(Object.assign({}, inventoryChange), newRecord), { updater: user }));
|
|
69
|
+
results.push(Object.assign(Object.assign({}, result), { cuFlag: 'M' }));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return results;
|
|
73
|
+
}
|
|
74
|
+
async deleteInventoryChange(name, context) {
|
|
75
|
+
const { domain, tx } = context.state;
|
|
76
|
+
await tx.getRepository(inventory_change_1.InventoryChange).delete({ domain: domain, name });
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
async deleteInventoryChanges(names, context) {
|
|
80
|
+
const { domain, tx } = context.state;
|
|
81
|
+
await tx.getRepository(inventory_change_1.InventoryChange).delete({
|
|
82
|
+
domain: domain,
|
|
83
|
+
name: (0, typeorm_1.In)(names)
|
|
84
|
+
});
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
async submitInventoryChanges(patches, context) {
|
|
88
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
89
|
+
const { domain, user, tx } = context.state;
|
|
90
|
+
try {
|
|
91
|
+
const _createRecords = [];
|
|
92
|
+
const _updateRecords = [];
|
|
93
|
+
for (let i = 0; i < patches.length; i++) {
|
|
94
|
+
let foundExistingPallet = await tx
|
|
95
|
+
.getRepository(inventory_1.Inventory)
|
|
96
|
+
.createQueryBuilder('iv')
|
|
97
|
+
.where('iv.domain_id = :domain_id', { domain_id: domain.id })
|
|
98
|
+
.andWhere('(iv.pallet_id = :pallet_id or iv.id = :id)', {
|
|
99
|
+
pallet_id: (_a = patches[i]) === null || _a === void 0 ? void 0 : _a.palletId,
|
|
100
|
+
id: (_b = patches[i]) === null || _b === void 0 ? void 0 : _b.id
|
|
101
|
+
})
|
|
102
|
+
.getOne();
|
|
103
|
+
//remove any unchanged records
|
|
104
|
+
for (const [key, value] of Object.entries(patches[i])) {
|
|
105
|
+
if (foundExistingPallet &&
|
|
106
|
+
key != 'id' &&
|
|
107
|
+
key != 'cuFlag' &&
|
|
108
|
+
key != 'palletId' &&
|
|
109
|
+
value == foundExistingPallet[key]) {
|
|
110
|
+
delete patches[i][key];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// skip process when no processable data found
|
|
114
|
+
if (Object.keys(patches[i]).filter(x => x != 'id' && x != 'cuFlag').length == 0) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const invLockedQty = (foundExistingPallet === null || foundExistingPallet === void 0 ? void 0 : foundExistingPallet.lockedQty) == null ? 0 : foundExistingPallet === null || foundExistingPallet === void 0 ? void 0 : foundExistingPallet.lockedQty;
|
|
118
|
+
if (((_c = patches[i]) === null || _c === void 0 ? void 0 : _c.qty) < invLockedQty) {
|
|
119
|
+
throw new Error('Adjusted qty value should not be lower than released qty, kindly contact our support for assistance');
|
|
120
|
+
}
|
|
121
|
+
if (foundExistingPallet) {
|
|
122
|
+
delete patches[i].serialNumbers;
|
|
123
|
+
_updateRecords.push(patches[i]);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
_createRecords.push(patches[i]);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const posCtrl = new controllers_1.PosController(tx, domain, user);
|
|
130
|
+
const inventoryChangeRepo = tx.getRepository(inventory_change_1.InventoryChange);
|
|
131
|
+
const inventoryItemChangeRepo = tx.getRepository(inventory_item_change_1.InventoryItemChange);
|
|
132
|
+
let strictProduct = 'false';
|
|
133
|
+
let strictProductSelectionSetting;
|
|
134
|
+
let partnerStrictProductSelectionSetting;
|
|
135
|
+
let location;
|
|
136
|
+
// new inventory
|
|
137
|
+
if (_createRecords.length > 0) {
|
|
138
|
+
for (let i = 0; i < _createRecords.length; i++) {
|
|
139
|
+
const newRecord = _createRecords[i];
|
|
140
|
+
const locationConditions = !!((_d = newRecord === null || newRecord === void 0 ? void 0 : newRecord.location) === null || _d === void 0 ? void 0 : _d.id)
|
|
141
|
+
? {
|
|
142
|
+
domain: domain,
|
|
143
|
+
id: (_e = newRecord === null || newRecord === void 0 ? void 0 : newRecord.location) === null || _e === void 0 ? void 0 : _e.id
|
|
144
|
+
}
|
|
145
|
+
: {
|
|
146
|
+
domain: domain,
|
|
147
|
+
name: (_f = newRecord === null || newRecord === void 0 ? void 0 : newRecord.location) === null || _f === void 0 ? void 0 : _f.name
|
|
148
|
+
};
|
|
149
|
+
location = await tx.getRepository(location_1.Location).findOne({
|
|
150
|
+
where: locationConditions,
|
|
151
|
+
relations: ['warehouse']
|
|
152
|
+
});
|
|
153
|
+
if (!location) {
|
|
154
|
+
throw new Error(`Location not found for ${(_g = newRecord === null || newRecord === void 0 ? void 0 : newRecord.location) === null || _g === void 0 ? void 0 : _g.name} `);
|
|
155
|
+
}
|
|
156
|
+
newRecord.location = location;
|
|
157
|
+
newRecord.zone = location.zone;
|
|
158
|
+
newRecord.warehouse = location.warehouse;
|
|
159
|
+
const bizplace = await tx.getRepository(biz_base_1.Bizplace).findOne({
|
|
160
|
+
where: { id: newRecord.bizplace.id },
|
|
161
|
+
relations: ['domain']
|
|
162
|
+
});
|
|
163
|
+
newRecord.bizplace = bizplace;
|
|
164
|
+
//to get product name
|
|
165
|
+
let customerBizplaceId = newRecord.bizplace.id;
|
|
166
|
+
const companyBizplace = await (0, biz_base_1.getCompanyBizplace)(domain, null, customerBizplaceId, tx);
|
|
167
|
+
const qb = tx
|
|
168
|
+
.getRepository(product_base_1.ProductDetail)
|
|
169
|
+
.createQueryBuilder('ProductDetail');
|
|
170
|
+
qb.innerJoinAndSelect('ProductDetail.product', 'Product');
|
|
171
|
+
qb.where('Product.deletedAt IS NULL');
|
|
172
|
+
qb.andWhere('ProductDetail.domain = :domain', { domain: companyBizplace.domain.id });
|
|
173
|
+
let product;
|
|
174
|
+
let productDetail;
|
|
175
|
+
//define strictProduct in string
|
|
176
|
+
if (!((_h = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _h === void 0 ? void 0 : _h.id) && newRecord.sku) {
|
|
177
|
+
strictProductSelectionSetting = await tx.getRepository(setting_base_1.Setting).findOne({
|
|
178
|
+
where: { domain, category: 'id-rule', name: 'strict-product-selection' }
|
|
179
|
+
});
|
|
180
|
+
if (strictProductSelectionSetting)
|
|
181
|
+
strictProduct = strictProductSelectionSetting.value;
|
|
182
|
+
partnerStrictProductSelectionSetting = await tx.getRepository(setting_base_1.PartnerSetting).findOne({
|
|
183
|
+
where: {
|
|
184
|
+
setting: strictProductSelectionSetting,
|
|
185
|
+
domain,
|
|
186
|
+
partnerDomain: companyBizplace === null || companyBizplace === void 0 ? void 0 : companyBizplace.domain
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (partnerStrictProductSelectionSetting)
|
|
190
|
+
strictProduct = partnerStrictProductSelectionSetting.value;
|
|
191
|
+
}
|
|
192
|
+
//Condition for interface & if import new only have SKU or SKU and productName in Excel
|
|
193
|
+
if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.productDetailId) {
|
|
194
|
+
qb.andWhere('ProductDetail.id =:productDetailId', { productDetailId: newRecord.productDetailId });
|
|
195
|
+
}
|
|
196
|
+
else if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.sku) {
|
|
197
|
+
qb.andWhere('Product.bizplace =:companyBizplaceId', { companyBizplaceId: companyBizplace.id });
|
|
198
|
+
qb.andWhere('Product.sku =:sku', {
|
|
199
|
+
sku: newRecord.sku
|
|
200
|
+
});
|
|
201
|
+
//Condition if the strictProduct=false,
|
|
202
|
+
// the user will be allowed to create new PackingType/PackingSize/Uom
|
|
203
|
+
if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.productName) {
|
|
204
|
+
qb.andWhere('Product.name =:productName', {
|
|
205
|
+
productName: newRecord.productName
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (newRecord.packingType) {
|
|
209
|
+
qb.andWhere('ProductDetail.packingType =:packingType', {
|
|
210
|
+
packingType: newRecord.packingType
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (newRecord.packingSize) {
|
|
214
|
+
qb.andWhere('ProductDetail.packingSize =:packingSize', {
|
|
215
|
+
packingSize: newRecord.packingSize
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
if (newRecord.uom) {
|
|
219
|
+
qb.andWhere('ProductDetail.uom =:uom', {
|
|
220
|
+
uom: newRecord.uom
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
productDetail = await qb.getOne();
|
|
224
|
+
if (strictProduct == 'true') {
|
|
225
|
+
if ((newRecord === null || newRecord === void 0 ? void 0 : newRecord.packingType) && (newRecord === null || newRecord === void 0 ? void 0 : newRecord.packingType) !== (productDetail === null || productDetail === void 0 ? void 0 : productDetail.packingType)) {
|
|
226
|
+
throw new Error('Packing type does not match with product master');
|
|
227
|
+
}
|
|
228
|
+
if ((newRecord === null || newRecord === void 0 ? void 0 : newRecord.packingSize) && (newRecord === null || newRecord === void 0 ? void 0 : newRecord.packingSize) !== (productDetail === null || productDetail === void 0 ? void 0 : productDetail.packingSize)) {
|
|
229
|
+
throw new Error('Packing size does not match with product master');
|
|
230
|
+
}
|
|
231
|
+
if ((newRecord === null || newRecord === void 0 ? void 0 : newRecord.uom) && (newRecord === null || newRecord === void 0 ? void 0 : newRecord.uom) !== (productDetail === null || productDetail === void 0 ? void 0 : productDetail.uom)) {
|
|
232
|
+
throw new Error('Uom value does not match with product master');
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
//If could not find productDetail, and only have SKU it will take by default
|
|
237
|
+
if (!productDetail) {
|
|
238
|
+
const qb = tx
|
|
239
|
+
.getRepository(product_base_1.ProductDetail)
|
|
240
|
+
.createQueryBuilder('ProductDetail');
|
|
241
|
+
qb.innerJoinAndSelect('ProductDetail.product', 'Product');
|
|
242
|
+
qb.where('Product.deletedAt IS NULL');
|
|
243
|
+
qb.andWhere('ProductDetail.domain = :domain', { domain: companyBizplace.domain.id });
|
|
244
|
+
if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.sku) {
|
|
245
|
+
qb.andWhere('Product.sku =:sku', {
|
|
246
|
+
sku: newRecord.sku
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
qb.andWhere('ProductDetail.isDefault = true');
|
|
251
|
+
}
|
|
252
|
+
productDetail = await qb.getOne();
|
|
253
|
+
newRecord.product = productDetail.product;
|
|
254
|
+
newRecord.productDetail = productDetail;
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
//if the productDetail is found, and take by default
|
|
258
|
+
newRecord.product = productDetail.product;
|
|
259
|
+
newRecord.productDetail = productDetail;
|
|
260
|
+
}
|
|
261
|
+
//Update field
|
|
262
|
+
const fields = ['unitCost', 'packingType', 'packingSize', 'uom'];
|
|
263
|
+
fields.forEach(field => {
|
|
264
|
+
if (!newRecord[field] && field == 'unitCost') {
|
|
265
|
+
newRecord[field] = productDetail.product[field];
|
|
266
|
+
}
|
|
267
|
+
else if (!newRecord[field]) {
|
|
268
|
+
newRecord[field] = productDetail[field];
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
newRecord.status = 'PENDING';
|
|
272
|
+
newRecord.transactionType = 'NEW';
|
|
273
|
+
newRecord.expirationDate =
|
|
274
|
+
(((newRecord === null || newRecord === void 0 ? void 0 : newRecord.expirationDate) && new Date(newRecord.expirationDate).getFullYear()) || 0) < 2000
|
|
275
|
+
? null
|
|
276
|
+
: newRecord.expirationDate;
|
|
277
|
+
const newInventoryChange = await inventoryChangeRepo.save(Object.assign(Object.assign({}, newRecord), { id: undefined, name: utils_1.InventoryNoGenerator.inventoryName(), domain: domain, creator: user, updater: user }));
|
|
278
|
+
const prevBizAccount = await tx
|
|
279
|
+
.getRepository(integration_accounting_1.Account)
|
|
280
|
+
.findOne({ where: { domain: bizplace === null || bizplace === void 0 ? void 0 : bizplace.domain, status: 'active' }, relations: ['domain'] });
|
|
281
|
+
if (prevBizAccount) {
|
|
282
|
+
await posCtrl.updatePosStock(tx, prevBizAccount, domain, newRecord.bizplace, newRecord.product, newRecord.qty, 'Code Adjustment', newInventoryChange.id);
|
|
283
|
+
}
|
|
284
|
+
if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.serialNumbers) {
|
|
285
|
+
let serialNumbers = newRecord.serialNumbers;
|
|
286
|
+
let newSerialNumbers = serialNumbers.split(',');
|
|
287
|
+
let inventoryItemChanges = [];
|
|
288
|
+
for (let i = 0; i < newSerialNumbers.length; i++) {
|
|
289
|
+
let newSerialNumber = newSerialNumbers[i];
|
|
290
|
+
let inventoryItemChange = {
|
|
291
|
+
serialNumber: newSerialNumber,
|
|
292
|
+
type: constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
|
|
293
|
+
};
|
|
294
|
+
inventoryItemChanges.push(inventoryItemChange);
|
|
295
|
+
}
|
|
296
|
+
newRecord.inventoryItemChangesJson = JSON.stringify(inventoryItemChanges);
|
|
297
|
+
}
|
|
298
|
+
// create new inventory items
|
|
299
|
+
if (newRecord === null || newRecord === void 0 ? void 0 : newRecord.inventoryItemChangesJson) {
|
|
300
|
+
const inventoryItemChanges = JSON.parse(newRecord.inventoryItemChangesJson);
|
|
301
|
+
if ((_j = productDetail === null || productDetail === void 0 ? void 0 : productDetail.product) === null || _j === void 0 ? void 0 : _j.isRequireSerialNumberScanningInbound) {
|
|
302
|
+
if ((inventoryItemChanges === null || inventoryItemChanges === void 0 ? void 0 : inventoryItemChanges.length) != (newRecord === null || newRecord === void 0 ? void 0 : newRecord.qty)) {
|
|
303
|
+
throw new Error('Serial Numbers not tally with quantity');
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
let newInventoryItemChanges = [];
|
|
307
|
+
inventoryItemChanges.map(inventoryItemChange => {
|
|
308
|
+
let newInventoryItemChange = new inventory_item_change_1.InventoryItemChange();
|
|
309
|
+
newInventoryItemChange.name = utils_1.InventoryNoGenerator.inventoryName();
|
|
310
|
+
newInventoryItemChange.serialNumber = inventoryItemChange.serialNumber;
|
|
311
|
+
newInventoryItemChange.type = inventoryItemChange.type;
|
|
312
|
+
newInventoryItemChange.status = newInventoryChange.status;
|
|
313
|
+
newInventoryItemChange.inventoryItem =
|
|
314
|
+
(newInventoryItemChange === null || newInventoryItemChange === void 0 ? void 0 : newInventoryItemChange.type) == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
|
|
315
|
+
? null
|
|
316
|
+
: newInventoryItemChange.inventoryItem;
|
|
317
|
+
newInventoryItemChange.inventoryChange = newInventoryChange;
|
|
318
|
+
newInventoryItemChange.domain = domain;
|
|
319
|
+
newInventoryItemChange.creator = user;
|
|
320
|
+
newInventoryItemChange.updater = user;
|
|
321
|
+
newInventoryItemChanges.push(newInventoryItemChange);
|
|
322
|
+
});
|
|
323
|
+
await inventoryItemChangeRepo.save(newInventoryItemChanges);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// update inventory
|
|
328
|
+
if (_updateRecords.length > 0) {
|
|
329
|
+
for (let i = 0; i < _updateRecords.length; i++) {
|
|
330
|
+
const updateRecord = _updateRecords[i];
|
|
331
|
+
const adjustQty = (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.qty) | 0;
|
|
332
|
+
let qtyDiff = 0;
|
|
333
|
+
let existingRecord = await tx.getRepository(inventory_1.Inventory).findOne({
|
|
334
|
+
where: { domain, palletId: updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.palletId },
|
|
335
|
+
relations: ['location', 'warehouse', 'product', 'productDetail', 'bizplace', 'bizplace.domain']
|
|
336
|
+
});
|
|
337
|
+
if (!existingRecord) {
|
|
338
|
+
existingRecord = await tx.getRepository(inventory_1.Inventory).findOne({
|
|
339
|
+
where: { domain, id: updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.id },
|
|
340
|
+
relations: ['location', 'warehouse', 'product', 'productDetail', 'bizplace', 'bizplace.domain']
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
if (updateRecord.palletId && updateRecord.palletId !== existingRecord.palletId) {
|
|
344
|
+
throw new Error('Updating the Lot ID is not permitted');
|
|
345
|
+
}
|
|
346
|
+
if (updateRecord.cartonId && updateRecord.cartonId !== existingRecord.cartonId) {
|
|
347
|
+
throw new Error('Updating the Carton ID is not permitted');
|
|
348
|
+
}
|
|
349
|
+
if (adjustQty >= 0) {
|
|
350
|
+
let existingQty = existingRecord.qty - existingRecord.lockedQty;
|
|
351
|
+
qtyDiff = existingQty == adjustQty ? existingQty - adjustQty : adjustQty - existingQty;
|
|
352
|
+
}
|
|
353
|
+
if (!updateRecord.unitCost) {
|
|
354
|
+
if (existingRecord.unitCost != null) {
|
|
355
|
+
updateRecord.unitCost = existingRecord.unitCost || ((_k = existingRecord.product) === null || _k === void 0 ? void 0 : _k.costPrice);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (!!(updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location)) {
|
|
359
|
+
const locationConditions = !!((_l = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location) === null || _l === void 0 ? void 0 : _l.id)
|
|
360
|
+
? {
|
|
361
|
+
domain: domain,
|
|
362
|
+
id: (_m = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location) === null || _m === void 0 ? void 0 : _m.id
|
|
363
|
+
}
|
|
364
|
+
: {
|
|
365
|
+
domain: domain,
|
|
366
|
+
name: (_o = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location) === null || _o === void 0 ? void 0 : _o.name
|
|
367
|
+
};
|
|
368
|
+
location = await tx.getRepository(location_1.Location).findOne({
|
|
369
|
+
where: locationConditions,
|
|
370
|
+
relations: ['warehouse']
|
|
371
|
+
});
|
|
372
|
+
if (!location) {
|
|
373
|
+
throw new Error(`Location not found for ${(_p = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.location) === null || _p === void 0 ? void 0 : _p.name} `);
|
|
374
|
+
}
|
|
375
|
+
updateRecord.location = location;
|
|
376
|
+
updateRecord.zone = location.zone;
|
|
377
|
+
updateRecord.warehouse = location.warehouse;
|
|
378
|
+
}
|
|
379
|
+
if (!!((_q = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.bizplace) === null || _q === void 0 ? void 0 : _q.id)) {
|
|
380
|
+
var curBizplace = await tx
|
|
381
|
+
.getRepository(biz_base_1.Bizplace)
|
|
382
|
+
.findOne({ where: { id: updateRecord.bizplace.id }, relations: ['domain'] });
|
|
383
|
+
updateRecord.bizplace = curBizplace;
|
|
384
|
+
}
|
|
385
|
+
let customerBizplaceId = ((_r = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.bizplace) === null || _r === void 0 ? void 0 : _r.id) || ((_s = existingRecord === null || existingRecord === void 0 ? void 0 : existingRecord.bizplace) === null || _s === void 0 ? void 0 : _s.id);
|
|
386
|
+
const companyBizplace = await (0, biz_base_1.getCompanyBizplace)(domain, null, customerBizplaceId);
|
|
387
|
+
const qb = tx
|
|
388
|
+
.getRepository(product_base_1.ProductDetail)
|
|
389
|
+
.createQueryBuilder('ProductDetail');
|
|
390
|
+
qb.innerJoinAndSelect('ProductDetail.product', 'Product');
|
|
391
|
+
qb.where('Product.deletedAt IS NULL');
|
|
392
|
+
qb.andWhere('ProductDetail.domain = :domain', { domain: companyBizplace.domain.id });
|
|
393
|
+
let product;
|
|
394
|
+
let productDetail;
|
|
395
|
+
if (!((_t = updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.productDetail) === null || _t === void 0 ? void 0 : _t.id) && updateRecord.sku) {
|
|
396
|
+
strictProductSelectionSetting = await tx.getRepository(setting_base_1.Setting).findOne({
|
|
397
|
+
where: { domain, category: 'id-rule', name: 'strict-product-selection' }
|
|
398
|
+
});
|
|
399
|
+
if (strictProductSelectionSetting)
|
|
400
|
+
strictProduct = strictProductSelectionSetting.value;
|
|
401
|
+
partnerStrictProductSelectionSetting = await tx.getRepository(setting_base_1.PartnerSetting).findOne({
|
|
402
|
+
where: {
|
|
403
|
+
setting: strictProductSelectionSetting,
|
|
404
|
+
domain,
|
|
405
|
+
partnerDomain: companyBizplace === null || companyBizplace === void 0 ? void 0 : companyBizplace.domain
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
if (partnerStrictProductSelectionSetting)
|
|
409
|
+
strictProduct = partnerStrictProductSelectionSetting.value;
|
|
410
|
+
}
|
|
411
|
+
let foundProductDetail;
|
|
412
|
+
//Condition for interface & if import update only have SKU or SKU and productName in Excel
|
|
413
|
+
if ((updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.productDetailId) || updateRecord.sku) {
|
|
414
|
+
if (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.productDetailId) {
|
|
415
|
+
qb.andWhere('ProductDetail.id =:productDetailId', { productDetailId: updateRecord.productDetailId });
|
|
416
|
+
}
|
|
417
|
+
else if (updateRecord.sku) {
|
|
418
|
+
qb.andWhere('Product.bizplace =:companyBizplaceId', { companyBizplaceId: companyBizplace.id });
|
|
419
|
+
qb.andWhere('Product.sku =:sku', {
|
|
420
|
+
sku: (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.sku) ? updateRecord.sku : existingRecord.product.sku
|
|
421
|
+
});
|
|
422
|
+
//Condition if the strictProduct=false, the user will be allowed to update PackingType/PackingSize/Uom
|
|
423
|
+
//else it will take by default
|
|
424
|
+
if (updateRecord.productName) {
|
|
425
|
+
qb.andWhere('ProductDetail.name =:productName', {
|
|
426
|
+
productName: updateRecord.productName
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
if (strictProduct == 'false') {
|
|
430
|
+
if (updateRecord.packingType) {
|
|
431
|
+
qb.andWhere('ProductDetail.packingType =:packingType', {
|
|
432
|
+
packingType: (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.packingType) ? updateRecord.packingType : existingRecord.packingType
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
if (updateRecord.packingSize) {
|
|
436
|
+
qb.andWhere('ProductDetail.packingSize =:packingSize', {
|
|
437
|
+
packingSize: (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.packingSize) ? updateRecord.packingSize : existingRecord.packingSize
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
if (updateRecord.uom) {
|
|
441
|
+
qb.andWhere('ProductDetail.uom =:uom', {
|
|
442
|
+
uom: (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.uom) ? updateRecord.uom : existingRecord.uom
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
qb.andWhere('ProductDetail.isDefault = true');
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
foundProductDetail = await qb.getOne();
|
|
451
|
+
//Validation if the strictProduct= true, user are not allowed to update PackingType/PackingSize/Uom
|
|
452
|
+
//else it will take existing record from product/productDetail
|
|
453
|
+
if (foundProductDetail) {
|
|
454
|
+
if (strictProduct === 'true') {
|
|
455
|
+
if (updateRecord.packingType && updateRecord.packingType !== foundProductDetail.packingType) {
|
|
456
|
+
throw new Error('Updating the packing type is not permitted');
|
|
457
|
+
}
|
|
458
|
+
if (updateRecord.packingSize && updateRecord.packingSize !== foundProductDetail.packingSize) {
|
|
459
|
+
throw new Error('Updating the packing size is not permitted');
|
|
460
|
+
}
|
|
461
|
+
if (updateRecord.uom && updateRecord.uom !== foundProductDetail.uom) {
|
|
462
|
+
throw new Error('Updating the uom is not permitted');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
updateRecord.productDetail = foundProductDetail;
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
product = existingRecord.product;
|
|
469
|
+
productDetail = existingRecord.productDetail;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
let transactionTypes = ['MISSING', 'EXPIRED'];
|
|
473
|
+
if (!transactionTypes.includes(updateRecord.transactionType))
|
|
474
|
+
updateRecord.transactionType = 'CHANGES';
|
|
475
|
+
updateRecord.status = 'PENDING';
|
|
476
|
+
updateRecord.expirationDate =
|
|
477
|
+
(((updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.expirationDate) && new Date(updateRecord.expirationDate).getFullYear()) || 0) < 2000
|
|
478
|
+
? null
|
|
479
|
+
: updateRecord.expirationDate;
|
|
480
|
+
const updatedInventoryChange = await inventoryChangeRepo.save(Object.assign(Object.assign({}, updateRecord), { id: undefined, name: utils_1.InventoryNoGenerator.inventoryName(), inventory: existingRecord, palletId: existingRecord.palletId, cartonId: existingRecord.cartonId, domain: domain, creator: user, updater: user }));
|
|
481
|
+
const prevBizAccount = await tx
|
|
482
|
+
.getRepository(integration_accounting_1.Account)
|
|
483
|
+
.findOne({ where: { domain: (_u = existingRecord.bizplace) === null || _u === void 0 ? void 0 : _u.domain, status: 'active' }, relations: ['domain'] });
|
|
484
|
+
if (prevBizAccount) {
|
|
485
|
+
await posCtrl.updatePosStock(tx, prevBizAccount, domain, existingRecord.bizplace, existingRecord.product, qtyDiff, 'Receiving Error', updatedInventoryChange.id);
|
|
486
|
+
}
|
|
487
|
+
// create new inventory items
|
|
488
|
+
if (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.inventoryItemChangesJson) {
|
|
489
|
+
const originalInventoryItems = await tx.getRepository(inventory_item_1.InventoryItem).find({
|
|
490
|
+
where: { domain, inventory: existingRecord, status: (0, typeorm_1.Not)('DELETED') }
|
|
491
|
+
});
|
|
492
|
+
const inventoryItemChanges = JSON.parse(updateRecord.inventoryItemChangesJson);
|
|
493
|
+
if (product === null || product === void 0 ? void 0 : product.isRequireSerialNumberScanningInbound) {
|
|
494
|
+
// const combinedInventoryItems: any[] = [...originalInventoryItems, ...inventoryItemChanges]
|
|
495
|
+
let qty = (updateRecord === null || updateRecord === void 0 ? void 0 : updateRecord.qty) ? updateRecord.qty : existingRecord.qty;
|
|
496
|
+
if (originalInventoryItems.length == 0) {
|
|
497
|
+
if (inventoryItemChanges.length !== qty) {
|
|
498
|
+
throw new Error('Serial Numbers not tally with quantity');
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
if ((originalInventoryItems === null || originalInventoryItems === void 0 ? void 0 : originalInventoryItems.length) - inventoryItemChanges.length !== qty) {
|
|
503
|
+
throw new Error('Serial Numbers not tally with quantity');
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
let newInventoryItemChanges = [];
|
|
508
|
+
inventoryItemChanges.map(inventoryItemChange => {
|
|
509
|
+
let newInventoryItemChange = new inventory_item_change_1.InventoryItemChange();
|
|
510
|
+
newInventoryItemChange.name = utils_1.InventoryNoGenerator.inventoryName();
|
|
511
|
+
newInventoryItemChange.serialNumber = inventoryItemChange.serialNumber;
|
|
512
|
+
newInventoryItemChange.type = inventoryItemChange.type;
|
|
513
|
+
newInventoryItemChange.status = updatedInventoryChange.status;
|
|
514
|
+
newInventoryItemChange.inventory = updatedInventoryChange.inventory;
|
|
515
|
+
newInventoryItemChange.inventoryItem =
|
|
516
|
+
(newInventoryItemChange === null || newInventoryItemChange === void 0 ? void 0 : newInventoryItemChange.type) == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW
|
|
517
|
+
? null
|
|
518
|
+
: inventoryItemChange.inventoryItem;
|
|
519
|
+
newInventoryItemChange.inventoryChange = updatedInventoryChange;
|
|
520
|
+
newInventoryItemChange.domain = domain;
|
|
521
|
+
newInventoryItemChange.creator = user;
|
|
522
|
+
newInventoryItemChange.updater = user;
|
|
523
|
+
newInventoryItemChanges.push(newInventoryItemChange);
|
|
524
|
+
});
|
|
525
|
+
await inventoryItemChangeRepo.save(newInventoryItemChanges);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* @notes Temporary off sendNotification due to suspect of causing wms down
|
|
531
|
+
*/
|
|
532
|
+
// const notificationApprover: any = await tx
|
|
533
|
+
// .getRepository(Privilege)
|
|
534
|
+
// .createQueryBuilder('p')
|
|
535
|
+
// .select('u.*')
|
|
536
|
+
// .addSelect('r.*')
|
|
537
|
+
// .innerJoin('p.roles', 'r')
|
|
538
|
+
// .innerJoin('r.users', 'u')
|
|
539
|
+
// .where('p.name = :name', { name: 'mutation' })
|
|
540
|
+
// .andWhere('p.category = :category', { category: 'inventory' })
|
|
541
|
+
// .andWhere('r.domain_id = :domainId', { domainId: domain.id })
|
|
542
|
+
// .groupBy('u.id')
|
|
543
|
+
// .addGroupBy('r.id')
|
|
544
|
+
// .getRawMany()
|
|
545
|
+
// if (_updateRecords.some(res => res.transactionType == 'MISSING')) {
|
|
546
|
+
// if (notificationApprover?.length && context.header?.referer) {
|
|
547
|
+
// const receivers: any[] = notificationApprover.map(user => user.id)
|
|
548
|
+
// const msg = {
|
|
549
|
+
// title: `Missing stock identified. Review this transaction in Inventory Adjustment Approval.`,
|
|
550
|
+
// body: ``,
|
|
551
|
+
// url: context.header.referer,
|
|
552
|
+
// data: { url: context.header.referer }
|
|
553
|
+
// }
|
|
554
|
+
// await sendNotification({
|
|
555
|
+
// receivers,
|
|
556
|
+
// message: { ...msg }
|
|
557
|
+
// })
|
|
558
|
+
// }
|
|
559
|
+
// } else {
|
|
560
|
+
// if (notificationApprover?.length && context.header?.referer) {
|
|
561
|
+
// const receivers: any[] = notificationApprover.map(user => user.id)
|
|
562
|
+
// const msg = {
|
|
563
|
+
// title: `There is an inventory adjustment pending for approval, kindly review within 1 hour to avoid any operation conflict.`,
|
|
564
|
+
// body: ``,
|
|
565
|
+
// url: context.header.referer,
|
|
566
|
+
// data: { url: context.header.referer }
|
|
567
|
+
// }
|
|
568
|
+
// await sendNotification({
|
|
569
|
+
// receivers,
|
|
570
|
+
// message: { ...msg }
|
|
571
|
+
// })
|
|
572
|
+
// }
|
|
573
|
+
// }
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
576
|
+
catch (error) {
|
|
577
|
+
throw error;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
async approveInventoryChanges(patches, context) {
|
|
581
|
+
const inventoryChangeList = await approveInventoryChanges(patches, context);
|
|
582
|
+
return inventoryChangeList;
|
|
583
|
+
}
|
|
584
|
+
async rejectInventoryChanges(patches, context) {
|
|
585
|
+
const { domain, user, tx } = context.state;
|
|
586
|
+
const _inventoryChanges = await tx.getRepository(inventory_change_1.InventoryChange).find({
|
|
587
|
+
where: { id: (0, typeorm_1.In)(patches.map(item => item.id)) },
|
|
588
|
+
relations: [
|
|
589
|
+
'inventory',
|
|
590
|
+
'inventory.bizplace',
|
|
591
|
+
'inventory.product',
|
|
592
|
+
'inventory.location',
|
|
593
|
+
'inventory.warehouse',
|
|
594
|
+
'bizplace',
|
|
595
|
+
'product',
|
|
596
|
+
'productDetail',
|
|
597
|
+
'location'
|
|
598
|
+
]
|
|
599
|
+
});
|
|
600
|
+
if (_inventoryChanges.length > 0) {
|
|
601
|
+
for (let i = 0; i < _inventoryChanges.length; i++) {
|
|
602
|
+
if (_inventoryChanges[i].status.toLocaleLowerCase() != 'pending')
|
|
603
|
+
return true;
|
|
604
|
+
_inventoryChanges[i].status = 'REJECTED';
|
|
605
|
+
// Get last row of InventoryHistory
|
|
606
|
+
let latestEntry = await tx.getRepository(inventory_history_1.InventoryHistory).find({
|
|
607
|
+
where: { palletId: _inventoryChanges[i].palletId, domain: domain },
|
|
608
|
+
order: { seq: 'DESC' },
|
|
609
|
+
take: 1
|
|
610
|
+
});
|
|
611
|
+
if (latestEntry.length > 0)
|
|
612
|
+
_inventoryChanges[i].lastInventoryHistory = latestEntry[0];
|
|
613
|
+
await upsertInventoryItems(context, _inventoryChanges[i], 'REJECTED', tx);
|
|
614
|
+
}
|
|
615
|
+
await tx.getRepository(inventory_change_1.InventoryChange).save(_inventoryChanges.filter(change => change.status == 'REJECTED'));
|
|
616
|
+
}
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
async cancelInventoryChanges(inventoryChangeId, context) {
|
|
620
|
+
const { domain, user, tx } = context.state;
|
|
621
|
+
const inventoryChange = await tx.getRepository(inventory_change_1.InventoryChange).findOne({
|
|
622
|
+
where: { id: inventoryChangeId }
|
|
623
|
+
});
|
|
624
|
+
if (!inventoryChange)
|
|
625
|
+
throw new Error('Inventory change not found.');
|
|
626
|
+
if (inventoryChange.status != 'PENDING')
|
|
627
|
+
throw new Error('Inventory change is not in pending status');
|
|
628
|
+
await tx.getRepository(inventory_change_1.InventoryChange).delete({ id: inventoryChange.id });
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
__decorate([
|
|
633
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
634
|
+
(0, type_graphql_1.Mutation)(returns => inventory_change_1.InventoryChange),
|
|
635
|
+
__param(0, (0, type_graphql_1.Arg)('inventoryChange')),
|
|
636
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
637
|
+
__metadata("design:type", Function),
|
|
638
|
+
__metadata("design:paramtypes", [inventory_change_types_1.NewInventoryChange, Object]),
|
|
639
|
+
__metadata("design:returntype", Promise)
|
|
640
|
+
], InventoryChangeMutation.prototype, "createInventoryChange", null);
|
|
641
|
+
__decorate([
|
|
642
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
643
|
+
(0, type_graphql_1.Mutation)(returns => inventory_change_1.InventoryChange),
|
|
644
|
+
__param(0, (0, type_graphql_1.Arg)('name')),
|
|
645
|
+
__param(1, (0, type_graphql_1.Arg)('patch')),
|
|
646
|
+
__param(2, (0, type_graphql_1.Ctx)()),
|
|
647
|
+
__metadata("design:type", Function),
|
|
648
|
+
__metadata("design:paramtypes", [String, inventory_change_types_1.InventoryChangePatch, Object]),
|
|
649
|
+
__metadata("design:returntype", Promise)
|
|
650
|
+
], InventoryChangeMutation.prototype, "updateInventoryChange", null);
|
|
651
|
+
__decorate([
|
|
652
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
653
|
+
(0, type_graphql_1.Mutation)(returns => [inventory_change_1.InventoryChange]),
|
|
654
|
+
__param(0, (0, type_graphql_1.Arg)('patches', type => [inventory_change_types_1.InventoryChangePatch])),
|
|
655
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
656
|
+
__metadata("design:type", Function),
|
|
657
|
+
__metadata("design:paramtypes", [Array, Object]),
|
|
658
|
+
__metadata("design:returntype", Promise)
|
|
659
|
+
], InventoryChangeMutation.prototype, "updateMultipleInventoryChange", null);
|
|
660
|
+
__decorate([
|
|
661
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
662
|
+
(0, type_graphql_1.Mutation)(returns => Boolean),
|
|
663
|
+
__param(0, (0, type_graphql_1.Arg)('name')),
|
|
664
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
665
|
+
__metadata("design:type", Function),
|
|
666
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
667
|
+
__metadata("design:returntype", Promise)
|
|
668
|
+
], InventoryChangeMutation.prototype, "deleteInventoryChange", null);
|
|
669
|
+
__decorate([
|
|
670
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
671
|
+
(0, type_graphql_1.Mutation)(returns => Boolean),
|
|
672
|
+
__param(0, (0, type_graphql_1.Arg)('names', type => [String])),
|
|
673
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
674
|
+
__metadata("design:type", Function),
|
|
675
|
+
__metadata("design:paramtypes", [Array, Object]),
|
|
676
|
+
__metadata("design:returntype", Promise)
|
|
677
|
+
], InventoryChangeMutation.prototype, "deleteInventoryChanges", null);
|
|
678
|
+
__decorate([
|
|
679
|
+
(0, type_graphql_1.Directive)('@privilege(category: "inventory", privilege: "mutation")'),
|
|
680
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
681
|
+
(0, type_graphql_1.Mutation)(returns => Boolean),
|
|
682
|
+
__param(0, (0, type_graphql_1.Arg)('patches', type => [inventory_types_1.InventoryPatch])),
|
|
683
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
684
|
+
__metadata("design:type", Function),
|
|
685
|
+
__metadata("design:paramtypes", [Array, Object]),
|
|
686
|
+
__metadata("design:returntype", Promise)
|
|
687
|
+
], InventoryChangeMutation.prototype, "submitInventoryChanges", null);
|
|
688
|
+
__decorate([
|
|
689
|
+
(0, type_graphql_1.Directive)('@privilege(category: "inventory_adjustment_approval", privilege: "mutation")'),
|
|
690
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
691
|
+
(0, type_graphql_1.Mutation)(returns => inventory_change_types_1.InventoryChangeList),
|
|
692
|
+
__param(0, (0, type_graphql_1.Arg)('patches', type => [inventory_change_types_1.InventoryChangePatch])),
|
|
693
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
694
|
+
__metadata("design:type", Function),
|
|
695
|
+
__metadata("design:paramtypes", [Array, Object]),
|
|
696
|
+
__metadata("design:returntype", Promise)
|
|
697
|
+
], InventoryChangeMutation.prototype, "approveInventoryChanges", null);
|
|
698
|
+
__decorate([
|
|
699
|
+
(0, type_graphql_1.Directive)('@privilege(category: "inventory_adjustment_approval", privilege: "mutation")'),
|
|
700
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
701
|
+
(0, type_graphql_1.Mutation)(returns => Boolean),
|
|
702
|
+
__param(0, (0, type_graphql_1.Arg)('patches', type => [inventory_change_types_1.InventoryChangePatch])),
|
|
703
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
704
|
+
__metadata("design:type", Function),
|
|
705
|
+
__metadata("design:paramtypes", [Array, Object]),
|
|
706
|
+
__metadata("design:returntype", Promise)
|
|
707
|
+
], InventoryChangeMutation.prototype, "rejectInventoryChanges", null);
|
|
708
|
+
__decorate([
|
|
709
|
+
(0, type_graphql_1.Directive)('@privilege(category: "inventory_adjustment_approval", privilege: "mutation")'),
|
|
710
|
+
(0, type_graphql_1.Directive)('@transaction'),
|
|
711
|
+
(0, type_graphql_1.Mutation)(returns => Boolean),
|
|
712
|
+
__param(0, (0, type_graphql_1.Arg)('inventoryChangeId', type => String)),
|
|
713
|
+
__param(1, (0, type_graphql_1.Ctx)()),
|
|
714
|
+
__metadata("design:type", Function),
|
|
715
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
716
|
+
__metadata("design:returntype", Promise)
|
|
717
|
+
], InventoryChangeMutation.prototype, "cancelInventoryChanges", null);
|
|
718
|
+
InventoryChangeMutation = __decorate([
|
|
719
|
+
(0, type_graphql_1.Resolver)(inventory_change_1.InventoryChange)
|
|
720
|
+
], InventoryChangeMutation);
|
|
721
|
+
exports.InventoryChangeMutation = InventoryChangeMutation;
|
|
722
|
+
function clean(obj) {
|
|
723
|
+
var propNames = Object.getOwnPropertyNames(obj);
|
|
724
|
+
for (var i = 0; i < propNames.length; i++) {
|
|
725
|
+
var propName = propNames[i];
|
|
726
|
+
if (obj[propName] === null || obj[propName] === undefined) {
|
|
727
|
+
delete obj[propName];
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
async function upsertInventoryItems(context, inventoryChange, approvalStatus, tx) {
|
|
732
|
+
const { domain, user } = context.state;
|
|
733
|
+
let inventoryItemChanges = await tx.getRepository(inventory_item_change_1.InventoryItemChange).find({
|
|
734
|
+
where: { domain, inventoryChange: inventoryChange, status: 'PENDING' },
|
|
735
|
+
relations: ['domain', 'inventoryItem', 'inventory', 'inventory.product', 'inventoryChange']
|
|
736
|
+
});
|
|
737
|
+
await Promise.all(inventoryItemChanges.map(async (inventoryItemChange) => {
|
|
738
|
+
let inventoryItem = new inventory_item_1.InventoryItem();
|
|
739
|
+
if (approvalStatus == 'APPROVED') {
|
|
740
|
+
const changeType = inventoryItemChange.type;
|
|
741
|
+
const existingInventoryItem = inventoryItemChange.inventoryItem;
|
|
742
|
+
if (changeType == constants_1.INVENTORY_ITEM_CHANGE_TYPE.NEW) {
|
|
743
|
+
const foundSerialNumber = await (0, inventory_item_query_1.checkExistingSerialNumber)(inventoryItemChange.serialNumber, inventoryChange.inventory.product, context, inventoryChange.inventory, tx);
|
|
744
|
+
if (foundSerialNumber)
|
|
745
|
+
throw new Error(`Serial number ${inventoryItemChange.serialNumber} is duplicated`);
|
|
746
|
+
inventoryItem.name = utils_1.InventoryNoGenerator.inventoryName();
|
|
747
|
+
inventoryItem.serialNumber = inventoryItemChange.serialNumber;
|
|
748
|
+
inventoryItem.status = constants_1.INVENTORY_STATUS.STORED;
|
|
749
|
+
inventoryItem.product = inventoryChange.inventory.product;
|
|
750
|
+
inventoryItem.productDetail = inventoryChange.inventory.productDetail;
|
|
751
|
+
inventoryItem.domain = inventoryItemChange.domain;
|
|
752
|
+
inventoryItem.inventory = inventoryChange.inventory;
|
|
753
|
+
inventoryItem.source = constants_1.INVENTORY_ITEM_SOURCE.ADJUSTMENT;
|
|
754
|
+
inventoryItem.creator = user;
|
|
755
|
+
inventoryItem.updater = user;
|
|
756
|
+
}
|
|
757
|
+
else if (changeType == constants_1.INVENTORY_ITEM_CHANGE_TYPE.MODIFIED) {
|
|
758
|
+
const foundSerialNumber = await (0, inventory_item_query_1.checkExistingSerialNumber)(inventoryItemChange.serialNumber, inventoryItemChange.inventory.product, context, inventoryItemChange.inventory, tx);
|
|
759
|
+
if (foundSerialNumber)
|
|
760
|
+
throw new Error(`Serial number ${inventoryItemChange.serialNumber} is duplicated`);
|
|
761
|
+
inventoryItem = Object.assign(Object.assign({}, existingInventoryItem), { serialNumber: inventoryItemChange.serialNumber, product: inventoryItemChange.inventory.product, productDetail: inventoryItemChange.inventory.productDetail });
|
|
762
|
+
}
|
|
763
|
+
else if (changeType == constants_1.INVENTORY_ITEM_CHANGE_TYPE.REMOVED) {
|
|
764
|
+
inventoryItem = Object.assign(Object.assign({}, existingInventoryItem), { status: constants_1.INVENTORY_STATUS.DELETED });
|
|
765
|
+
}
|
|
766
|
+
inventoryItem = await tx.getRepository(inventory_item_1.InventoryItem).save(inventoryItem);
|
|
767
|
+
}
|
|
768
|
+
await tx
|
|
769
|
+
.getRepository(inventory_item_change_1.InventoryItemChange)
|
|
770
|
+
.update({ id: inventoryItemChange.id }, { status: approvalStatus, inventoryItem });
|
|
771
|
+
}));
|
|
772
|
+
}
|
|
773
|
+
async function approveInventoryChanges(patches, context) {
|
|
774
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
|
|
775
|
+
const { domain, user, tx } = context.state;
|
|
776
|
+
// Get Selected Inventory Change Data
|
|
777
|
+
const _inventoryChanges = await tx.getRepository(inventory_change_1.InventoryChange).find({
|
|
778
|
+
where: {
|
|
779
|
+
id: (0, typeorm_1.In)(patches.map(item => item.id)),
|
|
780
|
+
transactionType: (0, typeorm_1.Not)('EXPIRED')
|
|
781
|
+
},
|
|
782
|
+
relations: [
|
|
783
|
+
'inventory',
|
|
784
|
+
'inventory.bizplace',
|
|
785
|
+
'inventory.bizplace.domain',
|
|
786
|
+
'inventory.product',
|
|
787
|
+
'inventory.productDetail',
|
|
788
|
+
'inventory.location',
|
|
789
|
+
'inventory.warehouse',
|
|
790
|
+
'bizplace',
|
|
791
|
+
'bizplace.domain',
|
|
792
|
+
'bizplace.company',
|
|
793
|
+
'bizplace.company.domain',
|
|
794
|
+
'product',
|
|
795
|
+
'productDetail',
|
|
796
|
+
'product.productDetails',
|
|
797
|
+
'product.productDetails.childProductDetail',
|
|
798
|
+
'location'
|
|
799
|
+
],
|
|
800
|
+
order: {
|
|
801
|
+
createdAt: 'ASC'
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
let arrLockedInventory = [];
|
|
805
|
+
if (_inventoryChanges.length > 0) {
|
|
806
|
+
let today = new Date(), year = today.getFullYear(), month = today.getMonth(), date = today.getDate();
|
|
807
|
+
for (let i = 0; i < _inventoryChanges.length; i++) {
|
|
808
|
+
if (_inventoryChanges[i].status.toLocaleLowerCase() != 'pending')
|
|
809
|
+
continue;
|
|
810
|
+
const newRecord = Object.assign({}, _inventoryChanges[i]), newHistoryRecord = Object.assign({}, _inventoryChanges[i]);
|
|
811
|
+
// Adjustment of existing Inventory
|
|
812
|
+
if (_inventoryChanges[i].inventory != null) {
|
|
813
|
+
let inventoryId = _inventoryChanges[i].inventory.id;
|
|
814
|
+
let inventory = await tx.getRepository(inventory_1.Inventory).findOne({
|
|
815
|
+
where: { id: inventoryId },
|
|
816
|
+
relations: [
|
|
817
|
+
'domain',
|
|
818
|
+
'bizplace',
|
|
819
|
+
'bizplace.domain',
|
|
820
|
+
'bizplace.company',
|
|
821
|
+
'bizplace.company.domain',
|
|
822
|
+
'product',
|
|
823
|
+
'productDetail',
|
|
824
|
+
'product.productDetails',
|
|
825
|
+
'product.productDetails.childProductDetail',
|
|
826
|
+
'warehouse',
|
|
827
|
+
'location',
|
|
828
|
+
'creator',
|
|
829
|
+
'updater'
|
|
830
|
+
]
|
|
831
|
+
});
|
|
832
|
+
// Check for locked inventory, and stop from udpating inventory
|
|
833
|
+
if (inventory.qty < inventory.lockedQty) {
|
|
834
|
+
arrLockedInventory.push(_inventoryChanges[i]);
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
// Check for Terminated inventory, then update InventoryChange entity, and stop from udpating inventory
|
|
838
|
+
if (inventory.status == constants_1.INVENTORY_STATUS.TERMINATED) {
|
|
839
|
+
_inventoryChanges[i] = Object.assign(Object.assign({}, _inventoryChanges[i]), { status: 'TERMINATED' });
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
let lastSeq = inventory.lastSeq, transactionType = '';
|
|
843
|
+
newHistoryRecord.openingQty = inventory.qty;
|
|
844
|
+
newHistoryRecord.openingUomValue = inventory.uomValue;
|
|
845
|
+
// Get last row of InventoryHistory
|
|
846
|
+
let latestEntry = await tx.getRepository(inventory_history_1.InventoryHistory).findOne({
|
|
847
|
+
where: { palletId: inventory.palletId, domain: domain },
|
|
848
|
+
order: { seq: 'DESC' }
|
|
849
|
+
});
|
|
850
|
+
_inventoryChanges[i].lastInventoryHistory = latestEntry;
|
|
851
|
+
// Check Change of existing inventory location
|
|
852
|
+
if (newRecord.location && newRecord.location.id != inventory.location.id) {
|
|
853
|
+
newRecord.zone = newRecord.location.zone;
|
|
854
|
+
newRecord.warehouse = newRecord.location.warehouse;
|
|
855
|
+
transactionType = 'ADJUSTMENT';
|
|
856
|
+
// Check and set current location status
|
|
857
|
+
let currentLocationInventoryCount = await tx.getRepository(inventory_1.Inventory).count({
|
|
858
|
+
where: { location: inventory.location, status: 'STORED', id: (0, typeorm_1.Not)(inventory.id) }
|
|
859
|
+
});
|
|
860
|
+
if (currentLocationInventoryCount == 0) {
|
|
861
|
+
let currentLocation = await tx.getRepository(location_1.Location).findOne({
|
|
862
|
+
where: { id: inventory.location.id }
|
|
863
|
+
});
|
|
864
|
+
await tx.getRepository(location_1.Location).save(Object.assign(Object.assign({}, currentLocation), { status: constants_1.LOCATION_STATUS.EMPTY }));
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
// Check Change of existing inventory quantity
|
|
868
|
+
if (newRecord.qty != null && newRecord.qty != inventory.qty) {
|
|
869
|
+
newHistoryRecord.qty = newRecord.qty - inventory.qty;
|
|
870
|
+
if (newRecord.qty < 1) {
|
|
871
|
+
newRecord.qty = 0;
|
|
872
|
+
newRecord.uomValue = 0;
|
|
873
|
+
}
|
|
874
|
+
transactionType = 'ADJUSTMENT';
|
|
875
|
+
}
|
|
876
|
+
else {
|
|
877
|
+
newHistoryRecord.qty = 0;
|
|
878
|
+
}
|
|
879
|
+
// Check Change of existing inventory uomValue
|
|
880
|
+
if (newRecord.uomValue != null && newRecord.uomValue != inventory.uomValue) {
|
|
881
|
+
newHistoryRecord.uomValue = newRecord.uomValue - inventory.uomValue;
|
|
882
|
+
Math.round(newHistoryRecord.uomValue * 100) / 100;
|
|
883
|
+
transactionType = 'ADJUSTMENT';
|
|
884
|
+
}
|
|
885
|
+
else {
|
|
886
|
+
newHistoryRecord.uomValue = 0;
|
|
887
|
+
}
|
|
888
|
+
// Terminate current inventory history if there is change of bizplace, product, batchId, packingType, or uom
|
|
889
|
+
if ((newRecord.bizplace && inventory.bizplace.id !== newRecord.bizplace.id) ||
|
|
890
|
+
(newRecord.product && inventory.product.id !== newRecord.product.id) ||
|
|
891
|
+
(newRecord.batchId && inventory.batchId !== newRecord.batchId) ||
|
|
892
|
+
(newRecord.packingType && inventory.packingType !== newRecord.packingType) ||
|
|
893
|
+
(newRecord.uom && inventory.uom !== newRecord.uom)) {
|
|
894
|
+
transactionType = 'ADJUSTMENT';
|
|
895
|
+
lastSeq = lastSeq + 1;
|
|
896
|
+
let inventoryHistory = Object.assign(Object.assign({}, inventory), { domain: domain, bizplace: inventory.bizplace.id, openingQty: inventory.qty, openingUomValue: inventory.uomValue, qty: -inventory.qty || 0, uom: inventory.uom, uomValue: -inventory.uomValue || 0, name: utils_1.InventoryNoGenerator.inventoryHistoryName(), seq: lastSeq, transactionType: transactionType, status: constants_1.INVENTORY_STATUS.TERMINATED, productId: inventory.product.id, warehouseId: inventory.warehouse.id, locationId: inventory.location.id, packingType: inventory.packingType, creator: user, updater: user });
|
|
897
|
+
delete inventoryHistory.id;
|
|
898
|
+
delete inventoryHistory.createdAt;
|
|
899
|
+
delete inventoryHistory.updatedAt;
|
|
900
|
+
await tx.getRepository(inventory_history_1.InventoryHistory).save(inventoryHistory);
|
|
901
|
+
newHistoryRecord.qty = newRecord.qty != null ? newRecord.qty : inventory.qty || 0;
|
|
902
|
+
newHistoryRecord.uomValue = newRecord.uomValue != null ? newRecord.uomValue : inventory.uomValue || 0;
|
|
903
|
+
newHistoryRecord.openingQty = 0;
|
|
904
|
+
newHistoryRecord.openingUomValue = 0;
|
|
905
|
+
}
|
|
906
|
+
// Set and update inventory and inventory history data
|
|
907
|
+
lastSeq = lastSeq + 1;
|
|
908
|
+
clean(newHistoryRecord);
|
|
909
|
+
let inventoryHistory = Object.assign(Object.assign(Object.assign({}, inventory), newHistoryRecord), { domain: domain, creator: user, updater: user, name: utils_1.InventoryNoGenerator.inventoryHistoryName(), status: newHistoryRecord.transactionType == 'MISSING' ? 'MISSING' : 'STORED', seq: lastSeq, transactionType: transactionType == '' ? 'ADJUSTMENT' : transactionType, productId: newRecord.product ? newRecord.product.id : inventory.product.id, warehouseId: newRecord.warehouse ? newRecord.warehouse.id : inventory.warehouse.id, locationId: newRecord.location && newRecord.location.id != inventory.location.id
|
|
910
|
+
? newRecord.location.id
|
|
911
|
+
: inventory.location.id, uom: newRecord.uom != null ? newRecord.uom : inventory.uom });
|
|
912
|
+
delete inventoryHistory.id;
|
|
913
|
+
delete inventoryHistory.createdAt;
|
|
914
|
+
delete inventoryHistory.updatedAt;
|
|
915
|
+
await tx.getRepository(inventory_history_1.InventoryHistory).save(inventoryHistory);
|
|
916
|
+
if (newRecord.qty != null && newRecord.qty == 0) {
|
|
917
|
+
++lastSeq;
|
|
918
|
+
delete inventoryHistory.id;
|
|
919
|
+
inventoryHistory = Object.assign(Object.assign({}, inventoryHistory), { name: utils_1.InventoryNoGenerator.inventoryHistoryName(), qty: 0, uomValue: 0, openingQty: 0, openingUomValue: 0, seq: lastSeq, transactionType: 'TERMINATED', status: 'TERMINATED' });
|
|
920
|
+
await tx.getRepository(inventory_history_1.InventoryHistory).save(inventoryHistory);
|
|
921
|
+
}
|
|
922
|
+
let originalInventoryQty = await getTotalInventoryQty(tx, domain, inventory.bizplace, inventory.product);
|
|
923
|
+
clean(newRecord);
|
|
924
|
+
let statusFilter = '';
|
|
925
|
+
if (newRecord.transactionType == 'MISSING') {
|
|
926
|
+
statusFilter = 'MISSING';
|
|
927
|
+
}
|
|
928
|
+
else {
|
|
929
|
+
statusFilter = (newRecord.qty != null ? newRecord.qty : inventory.qty) > 0 ? 'STORED' : 'TERMINATED';
|
|
930
|
+
}
|
|
931
|
+
let expirationDate = (newRecord === null || newRecord === void 0 ? void 0 : newRecord.expirationDate) ? newRecord.expirationDate : inventory.expirationDate;
|
|
932
|
+
const { minOutboundShelfLife } = inventory.product;
|
|
933
|
+
if (minOutboundShelfLife && expirationDate) {
|
|
934
|
+
let date = new Date();
|
|
935
|
+
expirationDate = new Date(expirationDate);
|
|
936
|
+
const releaseBefore = new Date(expirationDate);
|
|
937
|
+
releaseBefore.setDate(expirationDate.getDate() - minOutboundShelfLife);
|
|
938
|
+
if (date > releaseBefore) {
|
|
939
|
+
inventory.obsolete = true;
|
|
940
|
+
}
|
|
941
|
+
else {
|
|
942
|
+
inventory.obsolete = false;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
delete newRecord.createdAt;
|
|
946
|
+
await tx.getRepository(inventory_1.Inventory).save(Object.assign(Object.assign(Object.assign({}, inventory), newRecord), { id: inventoryId, status: statusFilter, updater: user, lastSeq: lastSeq, createdAt: inventory.createdAt }));
|
|
947
|
+
//Check and set latest location status
|
|
948
|
+
if (newRecord.qty != null ? newRecord.qty : inventory.qty > 0) {
|
|
949
|
+
var location = await tx.getRepository(location_1.Location).findOne({
|
|
950
|
+
where: { id: newRecord.location ? newRecord.location.id : inventory.location.id }
|
|
951
|
+
});
|
|
952
|
+
await tx.getRepository(location_1.Location).save(Object.assign(Object.assign({}, location), { status: constants_1.LOCATION_STATUS.OCCUPIED }));
|
|
953
|
+
}
|
|
954
|
+
else {
|
|
955
|
+
let latestLocationInventoryCount = await tx.getRepository(inventory_1.Inventory).count({
|
|
956
|
+
where: {
|
|
957
|
+
location: newRecord.location ? newRecord.location.id : inventory.location.id,
|
|
958
|
+
status: 'STORED',
|
|
959
|
+
id: (0, typeorm_1.Not)(inventory.id)
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
if (latestLocationInventoryCount == 0) {
|
|
963
|
+
let latestLocation = await tx.getRepository(location_1.Location).findOne({
|
|
964
|
+
where: { id: newRecord.location ? newRecord.location.id : inventory.location.id }
|
|
965
|
+
});
|
|
966
|
+
await tx.getRepository(location_1.Location).save(Object.assign(Object.assign({}, latestLocation), { status: constants_1.LOCATION_STATUS.EMPTY }));
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
await upsertInventoryItems(context, _inventoryChanges[i], 'APPROVED', tx);
|
|
970
|
+
const companyDomain = (_a = inventory.bizplace) === null || _a === void 0 ? void 0 : _a.company.domain;
|
|
971
|
+
const sellercraftCtrl = new controllers_1.SellercraftController(tx, domain, user);
|
|
972
|
+
const powrupController = new controllers_1.PowrupController();
|
|
973
|
+
if (((_b = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _b === void 0 ? void 0 : _b.id) && inventory.bizplace.id != ((_c = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _c === void 0 ? void 0 : _c.id)) {
|
|
974
|
+
const prevCompanyDomain = (_d = inventory.bizplace) === null || _d === void 0 ? void 0 : _d.company.domain;
|
|
975
|
+
const prevCustomerDomain = (_e = inventory.bizplace) === null || _e === void 0 ? void 0 : _e.domain;
|
|
976
|
+
// Sellercraft
|
|
977
|
+
const prevBizSC = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({
|
|
978
|
+
where: {
|
|
979
|
+
domain: prevCustomerDomain,
|
|
980
|
+
status: integration_sellercraft_1.SellercraftStatus.ACTIVE
|
|
981
|
+
},
|
|
982
|
+
relations: ['domain']
|
|
983
|
+
});
|
|
984
|
+
const prevApplication = await tx.getRepository(auth_base_1.Application).findOne({
|
|
985
|
+
domain: prevCompanyDomain,
|
|
986
|
+
status: 'ACTIVE',
|
|
987
|
+
type: (0, typeorm_1.In)([auth_base_1.ApplicationType.POWRUP, auth_base_1.ApplicationType.WEBSPERT])
|
|
988
|
+
});
|
|
989
|
+
if (prevBizSC) {
|
|
990
|
+
await sellercraftCtrl.updateSellercraftStock(prevBizSC, Object.assign(Object.assign({}, inventory), { bizplace: inventory.bizplace }));
|
|
991
|
+
}
|
|
992
|
+
switch (prevApplication === null || prevApplication === void 0 ? void 0 : prevApplication.type) {
|
|
993
|
+
case auth_base_1.ApplicationType.POWRUP:
|
|
994
|
+
if (((_f = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _f === void 0 ? void 0 : _f.id) && ((_g = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _g === void 0 ? void 0 : _g.id) != ((_h = inventory === null || inventory === void 0 ? void 0 : inventory.product) === null || _h === void 0 ? void 0 : _h.id))
|
|
995
|
+
powrupController.updateStock([inventory], (_j = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _j === void 0 ? void 0 : _j.domain, user, tx);
|
|
996
|
+
powrupController.updateStock([inventory], prevApplication, user, tx);
|
|
997
|
+
break;
|
|
998
|
+
case auth_base_1.ApplicationType.WEBSPERT:
|
|
999
|
+
controllers_1.WebspertController.updateStock([inventory.productDetail], inventory.bizplace, domain, user, tx);
|
|
1000
|
+
break;
|
|
1001
|
+
default:
|
|
1002
|
+
break;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
const currentCustomerDomain = (_k = inventory === null || inventory === void 0 ? void 0 : inventory.bizplace) === null || _k === void 0 ? void 0 : _k.domain;
|
|
1006
|
+
const currentBizSC = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({
|
|
1007
|
+
where: {
|
|
1008
|
+
domain: ((_l = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _l === void 0 ? void 0 : _l.domain) || ((_m = inventory === null || inventory === void 0 ? void 0 : inventory.bizplace) === null || _m === void 0 ? void 0 : _m.domain),
|
|
1009
|
+
status: integration_sellercraft_1.SellercraftStatus.ACTIVE
|
|
1010
|
+
},
|
|
1011
|
+
relations: ['domain']
|
|
1012
|
+
});
|
|
1013
|
+
const currentApplication = await tx.getRepository(auth_base_1.Application).findOne({
|
|
1014
|
+
where: {
|
|
1015
|
+
domain: ((_o = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _o === void 0 ? void 0 : _o.company.domain) || companyDomain,
|
|
1016
|
+
status: 'ACTIVE',
|
|
1017
|
+
type: (0, typeorm_1.In)([auth_base_1.ApplicationType.POWRUP, auth_base_1.ApplicationType.WEBSPERT])
|
|
1018
|
+
},
|
|
1019
|
+
relations: ['domain']
|
|
1020
|
+
});
|
|
1021
|
+
if (currentBizSC) {
|
|
1022
|
+
if (((_p = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _p === void 0 ? void 0 : _p.id) && ((_q = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _q === void 0 ? void 0 : _q.id) != ((_r = inventory === null || inventory === void 0 ? void 0 : inventory.product) === null || _r === void 0 ? void 0 : _r.id))
|
|
1023
|
+
await sellercraftCtrl.updateSellercraftStock(currentBizSC, Object.assign(Object.assign({}, inventory), { bizplace: (newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) || (inventory === null || inventory === void 0 ? void 0 : inventory.bizplace), product: inventory.product }));
|
|
1024
|
+
await sellercraftCtrl.updateSellercraftStock(currentBizSC, Object.assign(Object.assign({}, inventory), { bizplace: (newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) || (inventory === null || inventory === void 0 ? void 0 : inventory.bizplace), product: (newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) || (inventory === null || inventory === void 0 ? void 0 : inventory.product) }));
|
|
1025
|
+
}
|
|
1026
|
+
switch (currentApplication === null || currentApplication === void 0 ? void 0 : currentApplication.type) {
|
|
1027
|
+
case auth_base_1.ApplicationType.POWRUP:
|
|
1028
|
+
if (((_s = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _s === void 0 ? void 0 : _s.id) && ((_t = newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) === null || _t === void 0 ? void 0 : _t.id) != ((_u = inventory === null || inventory === void 0 ? void 0 : inventory.product) === null || _u === void 0 ? void 0 : _u.id))
|
|
1029
|
+
powrupController.updateStock([inventory], (_v = newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace) === null || _v === void 0 ? void 0 : _v.domain, user, tx);
|
|
1030
|
+
powrupController.updateStock([inventory], currentCustomerDomain, user, tx);
|
|
1031
|
+
break;
|
|
1032
|
+
case auth_base_1.ApplicationType.WEBSPERT:
|
|
1033
|
+
controllers_1.WebspertController.updateStock([inventory.productDetail], inventory.bizplace, domain, user, tx);
|
|
1034
|
+
break;
|
|
1035
|
+
default:
|
|
1036
|
+
break;
|
|
1037
|
+
}
|
|
1038
|
+
// check for any existing active marketplace connection, update marketplace selling qty
|
|
1039
|
+
const marketplaceStores = await tx.getRepository(integration_marketplace_1.MarketplaceStore).find({
|
|
1040
|
+
where: { domain: companyDomain, status: 'ACTIVE', isAutoUpdateStockQty: true },
|
|
1041
|
+
relations: ['marketplaceDistributors']
|
|
1042
|
+
});
|
|
1043
|
+
if ((marketplaceStores === null || marketplaceStores === void 0 ? void 0 : marketplaceStores.length) && marketplaceStores.some(store => store.isAutoUpdateStockQty)) {
|
|
1044
|
+
const ecommerceCtrl = new controllers_1.EcommerceController(tx, domain, user);
|
|
1045
|
+
await ecommerceCtrl.updateProductVariationStock(marketplaceStores, inventory.product.id, companyDomain);
|
|
1046
|
+
if ((newRecord === null || newRecord === void 0 ? void 0 : newRecord.product) || (newRecord === null || newRecord === void 0 ? void 0 : newRecord.bizplace))
|
|
1047
|
+
await ecommerceCtrl.updateProductVariationStock(marketplaceStores, newRecord.product.id, companyDomain);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
// Adding Inventory
|
|
1051
|
+
else {
|
|
1052
|
+
const total = await tx.getRepository(inventory_1.Inventory).count({
|
|
1053
|
+
createdAt: (0, typeorm_1.MoreThan)(new Date(year, month, date))
|
|
1054
|
+
});
|
|
1055
|
+
const yy = String(year).substr(String(year).length - 2);
|
|
1056
|
+
const mm = String(month + 1).padStart(2, '0');
|
|
1057
|
+
const dd = String(date).padStart(2, '0');
|
|
1058
|
+
const dateStr = yy + mm + dd;
|
|
1059
|
+
let palletId = newRecord.palletId;
|
|
1060
|
+
if (!((_w = newRecord.palletId) === null || _w === void 0 ? void 0 : _w.trim())) {
|
|
1061
|
+
palletId = await (0, id_rule_base_1.generateId)({
|
|
1062
|
+
domain: domain,
|
|
1063
|
+
type: 'adjustment_pallet_id',
|
|
1064
|
+
seed: {
|
|
1065
|
+
batchId: newRecord.batchId,
|
|
1066
|
+
date: dateStr
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
if (!palletId)
|
|
1070
|
+
throw new Error('No adjustment pallet id setting rule found');
|
|
1071
|
+
}
|
|
1072
|
+
var location = await tx.getRepository(location_1.Location).findOne({
|
|
1073
|
+
where: { id: newRecord.location.id },
|
|
1074
|
+
relations: ['warehouse']
|
|
1075
|
+
});
|
|
1076
|
+
newRecord.location = location;
|
|
1077
|
+
newRecord.zone = location.zone;
|
|
1078
|
+
newRecord.warehouse = location.warehouse;
|
|
1079
|
+
newRecord.status = constants_1.INVENTORY_STATUS.STORED;
|
|
1080
|
+
newRecord.name = palletId;
|
|
1081
|
+
newRecord.palletId = palletId;
|
|
1082
|
+
const warehouseCartonSetting = await tx.getRepository(setting_base_1.Setting).findOne({
|
|
1083
|
+
where: { domain, category: 'id-rule', name: 'enable-carton-label' }
|
|
1084
|
+
});
|
|
1085
|
+
const partnerCartonSetting = await tx.getRepository(setting_base_1.PartnerSetting).findOne({
|
|
1086
|
+
where: { setting: warehouseCartonSetting, domain, partnerDomain: (_x = newRecord.bizplace) === null || _x === void 0 ? void 0 : _x.domain }
|
|
1087
|
+
});
|
|
1088
|
+
if (partnerCartonSetting === null || partnerCartonSetting === void 0 ? void 0 : partnerCartonSetting.value) {
|
|
1089
|
+
let cartonId = await (0, id_rule_base_1.generateId)({
|
|
1090
|
+
domain: domain,
|
|
1091
|
+
type: 'adjustment_carton_id',
|
|
1092
|
+
seed: { date: dateStr }
|
|
1093
|
+
});
|
|
1094
|
+
if (!cartonId)
|
|
1095
|
+
throw new Error('No adjustment carton id setting rule found');
|
|
1096
|
+
newRecord.cartonId = cartonId;
|
|
1097
|
+
}
|
|
1098
|
+
let expirationDate = (((newRecord === null || newRecord === void 0 ? void 0 : newRecord.expirationDate) && new Date(newRecord.expirationDate).getFullYear()) || 0) < 2000
|
|
1099
|
+
? null
|
|
1100
|
+
: newRecord.expirationDate;
|
|
1101
|
+
const { minOutboundShelfLife } = newRecord.product;
|
|
1102
|
+
if (minOutboundShelfLife && expirationDate) {
|
|
1103
|
+
let date = new Date();
|
|
1104
|
+
expirationDate = new Date(expirationDate);
|
|
1105
|
+
const releaseBefore = new Date(expirationDate);
|
|
1106
|
+
releaseBefore.setDate(expirationDate.getDate() - minOutboundShelfLife);
|
|
1107
|
+
if (date > releaseBefore) {
|
|
1108
|
+
newRecord.obsolete = true;
|
|
1109
|
+
}
|
|
1110
|
+
else {
|
|
1111
|
+
newRecord.obsolete = false;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
let savedInventory = await tx.getRepository(inventory_1.Inventory).save(Object.assign(Object.assign({}, newRecord), { expirationDate, domain: domain, creator: user, updater: user, lastSeq: 0 }));
|
|
1115
|
+
await tx.getRepository(inventory_history_1.InventoryHistory).save(Object.assign(Object.assign({}, newRecord), { domain: domain, creator: user, updater: user, name: utils_1.InventoryNoGenerator.inventoryHistoryName(), seq: 0, transactionType: 'NEW', productId: newRecord.product.id, warehouseId: newRecord.warehouse.id, locationId: newRecord.location.id, inventory: savedInventory, expirationDate: expirationDate }));
|
|
1116
|
+
await tx.getRepository(location_1.Location).save(Object.assign(Object.assign({}, location), { status: constants_1.LOCATION_STATUS.OCCUPIED }));
|
|
1117
|
+
_inventoryChanges[i].inventory = savedInventory;
|
|
1118
|
+
_inventoryChanges[i].palletId = savedInventory.palletId;
|
|
1119
|
+
await upsertInventoryItems(context, _inventoryChanges[i], 'APPROVED', tx);
|
|
1120
|
+
const companyDomain = (_y = newRecord.bizplace) === null || _y === void 0 ? void 0 : _y.company.domain;
|
|
1121
|
+
const customerDomain = (_z = newRecord.bizplace) === null || _z === void 0 ? void 0 : _z.domain;
|
|
1122
|
+
const sellercraft = await tx.getRepository(integration_sellercraft_1.Sellercraft).findOne({
|
|
1123
|
+
where: { domain: newRecord.bizplace.domain, status: integration_sellercraft_1.SellercraftStatus.ACTIVE },
|
|
1124
|
+
relations: ['domain']
|
|
1125
|
+
});
|
|
1126
|
+
const application = await tx.getRepository(auth_base_1.Application).findOne({
|
|
1127
|
+
domain: companyDomain,
|
|
1128
|
+
status: 'ACTIVE',
|
|
1129
|
+
type: (0, typeorm_1.In)([auth_base_1.ApplicationType.POWRUP, auth_base_1.ApplicationType.WEBSPERT])
|
|
1130
|
+
});
|
|
1131
|
+
if (sellercraft) {
|
|
1132
|
+
const sellercraftCtrl = new controllers_1.SellercraftController(tx, domain, user);
|
|
1133
|
+
await sellercraftCtrl.updateSellercraftStock(sellercraft, newRecord);
|
|
1134
|
+
}
|
|
1135
|
+
// if (application) {
|
|
1136
|
+
// const powrupController: PowrupController = new PowrupController()
|
|
1137
|
+
// powrupController.updateStock([newRecord], customerDomain, user, tx)
|
|
1138
|
+
// }
|
|
1139
|
+
switch (application === null || application === void 0 ? void 0 : application.type) {
|
|
1140
|
+
case auth_base_1.ApplicationType.POWRUP:
|
|
1141
|
+
const powrupController = new controllers_1.PowrupController();
|
|
1142
|
+
powrupController.updateStock([newRecord], customerDomain, user, tx);
|
|
1143
|
+
break;
|
|
1144
|
+
case auth_base_1.ApplicationType.WEBSPERT:
|
|
1145
|
+
controllers_1.WebspertController.updateStock([newRecord.productDetail], newRecord.bizplace, domain, user, tx);
|
|
1146
|
+
break;
|
|
1147
|
+
default:
|
|
1148
|
+
break;
|
|
1149
|
+
}
|
|
1150
|
+
// update marketplace store qty
|
|
1151
|
+
// check for any existing active marketplace connection, update marketplace selling qty
|
|
1152
|
+
const marketplaceStores = await tx.getRepository(integration_marketplace_1.MarketplaceStore).find({
|
|
1153
|
+
where: { domain: companyDomain, status: 'ACTIVE', isAutoUpdateStockQty: true },
|
|
1154
|
+
relations: ['marketplaceDistributors']
|
|
1155
|
+
});
|
|
1156
|
+
if ((marketplaceStores === null || marketplaceStores === void 0 ? void 0 : marketplaceStores.length) && marketplaceStores.some(store => store.isAutoUpdateStockQty)) {
|
|
1157
|
+
const ecommerceCtrl = new controllers_1.EcommerceController(tx, domain, user);
|
|
1158
|
+
await ecommerceCtrl.updateProductVariationStock(marketplaceStores, newRecord.product.id, companyDomain);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
_inventoryChanges[i].status = 'APPROVED';
|
|
1162
|
+
}
|
|
1163
|
+
await tx.getRepository(inventory_change_1.InventoryChange).save(_inventoryChanges);
|
|
1164
|
+
}
|
|
1165
|
+
return { items: arrLockedInventory, total: arrLockedInventory.length };
|
|
1166
|
+
}
|
|
1167
|
+
exports.approveInventoryChanges = approveInventoryChanges;
|
|
1168
|
+
async function getTotalInventoryQty(tx, domain, bizplace, product) {
|
|
1169
|
+
let qb = tx.getRepository(inventory_1.Inventory).createQueryBuilder('inv');
|
|
1170
|
+
qb.select('SUM(inv.qty)', 'totalQty')
|
|
1171
|
+
.addSelect('SUM(inv.lockedQty)', 'totalLockedQty')
|
|
1172
|
+
.leftJoin('inv.location', 'loc')
|
|
1173
|
+
.andWhere('"inv"."domain_id" = :domainId')
|
|
1174
|
+
.andWhere('"inv"."bizplace_id" = :bizplaceId')
|
|
1175
|
+
.andWhere('"inv"."product_id" = :productId')
|
|
1176
|
+
.andWhere('"inv"."status" = :status')
|
|
1177
|
+
.andWhere('"loc"."type" NOT IN (:...locationTypes)')
|
|
1178
|
+
.setParameters({
|
|
1179
|
+
domainId: domain.id,
|
|
1180
|
+
bizplaceId: bizplace.id,
|
|
1181
|
+
productId: product.id,
|
|
1182
|
+
status: constants_1.INVENTORY_STATUS.STORED,
|
|
1183
|
+
locationTypes: [constants_1.LOCATION_TYPE.QUARANTINE, constants_1.LOCATION_TYPE.RESERVE]
|
|
1184
|
+
});
|
|
1185
|
+
let result = await qb.getRawOne();
|
|
1186
|
+
return result.totalQty - result.totalLockedQty || 0;
|
|
1187
|
+
}
|
|
1188
|
+
//# sourceMappingURL=inventory-change-mutation.js.map
|