gemcap-be-common 1.2.79 → 1.2.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.updateAvailabilitySigns = exports.addAvailabilitySign = exports.getAvailabilitySigns = exports.findAvailabilitySignsByBBCDateId = void 0;
7
7
  const mongoose_1 = __importDefault(require("mongoose"));
8
8
  const AvailabilitySigns_model_1 = require("../models/AvailabilitySigns.model");
9
+ const _models_1 = require("../models/_models");
9
10
  const findAvailabilitySignsByBBCDateId = async (bbcDateId) => {
10
11
  return AvailabilitySigns_model_1.AvailabilitySignsModel.findOne({ bbcDateId: new mongoose_1.default.Types.ObjectId(bbcDateId) }).lean();
11
12
  };
@@ -23,14 +24,14 @@ const getAvailabilitySigns = async (bbcDateIds) => {
23
24
  },
24
25
  }, {
25
26
  $lookup: {
26
- from: 'users',
27
+ from: _models_1.MODEL_NAMES.users,
27
28
  localField: 'signs.userSignedId',
28
29
  foreignField: '_id',
29
30
  as: 'signs.userSigned',
30
31
  },
31
32
  }, {
32
33
  $lookup: {
33
- from: 'users',
34
+ from: _models_1.MODEL_NAMES.users,
34
35
  localField: 'signs.userRevokedId',
35
36
  foreignField: '_id',
36
37
  as: 'signs.userRevoked',
@@ -7,6 +7,7 @@ import {
7
7
  IAvailabilitySignView,
8
8
  } from '../models/AvailabilitySigns.model';
9
9
  import { IResult } from '../interfaces/result.interface';
10
+ import { MODEL_NAMES } from '../models/_models';
10
11
 
11
12
  export const findAvailabilitySignsByBBCDateId = async (bbcDateId: string): Promise<IAvailabilitySignDoc> => {
12
13
  return AvailabilitySignsModel.findOne({ bbcDateId: new mongoose.Types.ObjectId(bbcDateId) }).lean();
@@ -25,7 +26,7 @@ export const getAvailabilitySigns = async (bbcDateIds: string[]) => {
25
26
  },
26
27
  }, {
27
28
  $lookup: {
28
- from: 'users',
29
+ from: MODEL_NAMES.users,
29
30
  localField: 'signs.userSignedId',
30
31
  foreignField: '_id',
31
32
  as: 'signs.userSigned',
@@ -33,7 +34,7 @@ export const getAvailabilitySigns = async (bbcDateIds: string[]) => {
33
34
  }, {
34
35
  $lookup:
35
36
  {
36
- from: 'users',
37
+ from: MODEL_NAMES.users,
37
38
  localField: 'signs.userRevokedId',
38
39
  foreignField: '_id',
39
40
  as: 'signs.userRevoked',
package/db/bbcDates.db.js CHANGED
@@ -8,6 +8,7 @@ const mongoose_1 = __importDefault(require("mongoose"));
8
8
  const BBCDate_model_1 = require("../models/BBCDate.model");
9
9
  const BBCSheet_model_1 = require("../models/BBCSheet.model");
10
10
  const availability_db_1 = require("./availability.db");
11
+ const _models_1 = require("../models/_models");
11
12
  const getBBCDateDoc = async (bbcDateId) => {
12
13
  return BBCDate_model_1.BBCDateModel.findById(bbcDateId).lean();
13
14
  };
@@ -111,7 +112,7 @@ const getBBCDatesByType = (borrowerId, collateralType) => {
111
112
  },
112
113
  {
113
114
  $lookup: {
114
- from: 'bbcdates',
115
+ from: _models_1.MODEL_NAMES.BBCDates,
115
116
  localField: 'bbcDateId',
116
117
  foreignField: '_id',
117
118
  as: 'bbc',
package/db/bbcDates.db.ts CHANGED
@@ -4,6 +4,7 @@ import { BBCDateModel, IBBCDateDoc } from '../models/BBCDate.model';
4
4
  import { BBCSheetModel } from '../models/BBCSheet.model';
5
5
  import { getAvailabilitySigns } from './availability.db';
6
6
  import { ECollaterals } from '../enums/collaterals.enum';
7
+ import { MODEL_NAMES } from '../models/_models';
7
8
 
8
9
  export const getBBCDateDoc = async (bbcDateId: string) => {
9
10
  return BBCDateModel.findById(bbcDateId).lean();
@@ -108,7 +109,7 @@ export const getBBCDatesByType = (borrowerId: string, collateralType: ECollatera
108
109
  },
109
110
  {
110
111
  $lookup: {
111
- from: 'bbcdates',
112
+ from: MODEL_NAMES.BBCDates,
112
113
  localField: 'bbcDateId',
113
114
  foreignField: '_id',
114
115
  as: 'bbc',
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createBBCSheet = exports.getUniqueDatesForPeriod = exports.getUniqueDates = exports.getBBCSheetsByTypeGroupedByBBC = exports.getBBCSheetsByType = void 0;
7
7
  const mongoose_1 = __importDefault(require("mongoose"));
8
8
  const BBCSheet_model_1 = require("../models/BBCSheet.model");
9
+ const _models_1 = require("../models/_models");
9
10
  const getBBCSheetsByType = async (bbcDateIds, collateralType) => {
10
11
  return BBCSheet_model_1.BBCSheetModel.aggregate([
11
12
  {
@@ -60,7 +61,7 @@ const getUniqueDates = async (borrowerId, limit, collateral) => {
60
61
  },
61
62
  {
62
63
  $lookup: {
63
- from: 'bbcdates',
64
+ from: _models_1.MODEL_NAMES.BBCDates,
64
65
  localField: 'bbcDateId',
65
66
  foreignField: '_id',
66
67
  as: 'bbc',
@@ -117,7 +118,7 @@ const getUniqueDatesForPeriod = async (borrowerId, collateral, startDate, endDat
117
118
  },
118
119
  {
119
120
  $lookup: {
120
- from: 'bbcdates',
121
+ from: _models_1.MODEL_NAMES.BBCDates,
121
122
  localField: 'bbcDateId',
122
123
  foreignField: '_id',
123
124
  as: 'bbc',
@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
2
2
 
3
3
  import { ECollaterals } from '../enums/collaterals.enum';
4
4
  import { BBCSheetModel, IBBCSheet, IBBCSheetDoc } from '../models/BBCSheet.model';
5
+ import { MODEL_NAMES } from '../models/_models';
5
6
 
6
7
  export const getBBCSheetsByType = async (bbcDateIds: string[], collateralType: ECollaterals) => {
7
8
  return BBCSheetModel.aggregate<IBBCSheetDoc>([
@@ -57,7 +58,7 @@ export const getUniqueDates = async (borrowerId: string, limit: number, collater
57
58
  },
58
59
  {
59
60
  $lookup: {
60
- from: 'bbcdates',
61
+ from: MODEL_NAMES.BBCDates,
61
62
  localField: 'bbcDateId',
62
63
  foreignField: '_id',
63
64
  as: 'bbc',
@@ -114,7 +115,7 @@ export const getUniqueDatesForPeriod = async (borrowerId: string, collateral: EC
114
115
  },
115
116
  {
116
117
  $lookup: {
117
- from: 'bbcdates',
118
+ from: MODEL_NAMES.BBCDates,
118
119
  localField: 'bbcDateId',
119
120
  foreignField: '_id',
120
121
  as: 'bbc',
package/db/brokers.db.js CHANGED
@@ -8,6 +8,7 @@ const decimal_js_1 = __importDefault(require("decimal.js"));
8
8
  const mongoose_1 = __importDefault(require("mongoose"));
9
9
  const reports_db_1 = require("./reports.db");
10
10
  const ProductBroker_model_1 = require("../models/ProductBroker.model");
11
+ const _models_1 = require("../models/_models");
11
12
  const getProductBrokers = async (productId) => {
12
13
  return ProductBroker_model_1.ProductBrokerModel.aggregate([
13
14
  {
@@ -18,7 +19,7 @@ const getProductBrokers = async (productId) => {
18
19
  $sort: { 'order': 1 },
19
20
  }, {
20
21
  $lookup: {
21
- from: 'loan_brokers',
22
+ from: _models_1.MODEL_NAMES.loanBrokers,
22
23
  localField: 'loanBrokerId',
23
24
  foreignField: '_id',
24
25
  as: 'loanBroker',
package/db/brokers.db.ts CHANGED
@@ -4,6 +4,7 @@ import mongoose from 'mongoose';
4
4
  import { EChargeType, ILedgerDataRow } from './reports.db';
5
5
  import { ILoanProductDoc } from '../models/LoanProducts.model';
6
6
  import { IProductBrokerDocWithBroker, ProductBrokerModel } from '../models/ProductBroker.model';
7
+ import { MODEL_NAMES } from '../models/_models';
7
8
 
8
9
  export const getProductBrokers = async (productId: string) => {
9
10
  return ProductBrokerModel.aggregate<IProductBrokerDocWithBroker>([
@@ -15,7 +16,7 @@ export const getProductBrokers = async (productId: string) => {
15
16
  $sort: { 'order': 1 },
16
17
  }, {
17
18
  $lookup: {
18
- from: 'loan_brokers',
19
+ from: MODEL_NAMES.loanBrokers,
19
20
  localField: 'loanBrokerId',
20
21
  foreignField: '_id',
21
22
  as: 'loanBroker',
@@ -28,7 +28,7 @@ exports.collateralFields = {
28
28
  exports.COLLATERALS_LOOKUP = [
29
29
  {
30
30
  $lookup: {
31
- from: 'bbcsheets',
31
+ from: _models_1.MODEL_NAMES.BBCSheets,
32
32
  localField: 'bbcSheetId',
33
33
  foreignField: '_id',
34
34
  as: 'bbcSheet',
@@ -164,7 +164,7 @@ const getCollateralListBySheet = async (sheetIds, collateralType) => {
164
164
  },
165
165
  {
166
166
  $lookup: {
167
- from: 'bbcsheets',
167
+ from: _models_1.MODEL_NAMES.BBCSheets,
168
168
  localField: 'bbcSheetId',
169
169
  foreignField: '_id',
170
170
  as: 'bbcSheet',
@@ -177,7 +177,7 @@ const getCollateralListBySheet = async (sheetIds, collateralType) => {
177
177
  },
178
178
  {
179
179
  $lookup: {
180
- from: 'bbcdates',
180
+ from: _models_1.MODEL_NAMES.BBCDates,
181
181
  localField: 'bbcSheet.bbcDateId',
182
182
  foreignField: '_id',
183
183
  as: 'bbc',
@@ -206,7 +206,7 @@ const getPreviousCollateralDocument = async (borrowerId, date, collateralType, l
206
206
  },
207
207
  {
208
208
  $lookup: {
209
- from: 'bbcdates',
209
+ from: _models_1.MODEL_NAMES.BBCDates,
210
210
  localField: 'bbcDateId',
211
211
  foreignField: '_id',
212
212
  as: 'bbc',
@@ -35,7 +35,7 @@ export const collateralFields = {
35
35
  export const COLLATERALS_LOOKUP = [
36
36
  {
37
37
  $lookup: {
38
- from: 'bbcsheets',
38
+ from: MODEL_NAMES.BBCSheets,
39
39
  localField: 'bbcSheetId',
40
40
  foreignField: '_id',
41
41
  as: 'bbcSheet',
@@ -173,7 +173,7 @@ export const getCollateralListBySheet = async (sheetIds: string[], collateralTyp
173
173
  },
174
174
  {
175
175
  $lookup: {
176
- from: 'bbcsheets',
176
+ from: MODEL_NAMES.BBCSheets,
177
177
  localField: 'bbcSheetId',
178
178
  foreignField: '_id',
179
179
  as: 'bbcSheet',
@@ -186,7 +186,7 @@ export const getCollateralListBySheet = async (sheetIds: string[], collateralTyp
186
186
  },
187
187
  {
188
188
  $lookup: {
189
- from: 'bbcdates',
189
+ from: MODEL_NAMES.BBCDates,
190
190
  localField: 'bbcSheet.bbcDateId',
191
191
  foreignField: '_id',
192
192
  as: 'bbc',
@@ -216,7 +216,7 @@ export const getPreviousCollateralDocument = async (borrowerId: string, date: Da
216
216
  {
217
217
  $lookup:
218
218
  {
219
- from: 'bbcdates',
219
+ from: MODEL_NAMES.BBCDates,
220
220
  localField: 'bbcDateId',
221
221
  foreignField: '_id',
222
222
  as: 'bbc',
@@ -42,3 +42,7 @@ export declare const getSKUQtyValueUnitCost: (bbcDateId: string) => Promise<{
42
42
  qty: number;
43
43
  value: number;
44
44
  }[]>;
45
+ export declare const getInventoryMovement: (borrowerId: string, startDate: string, endDate: string) => Promise<{
46
+ reportAdditions: any[];
47
+ reportRemoved: any[];
48
+ }>;
package/db/inventories.js CHANGED
@@ -3,12 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getSKUQtyValueUnitCost = exports.getAllUniqSKUInRangeNew = exports.getSKUUnitCostForAllDates = void 0;
6
+ exports.getInventoryMovement = exports.getSKUQtyValueUnitCost = exports.getAllUniqSKUInRangeNew = exports.getSKUUnitCostForAllDates = void 0;
7
7
  const mongoose_1 = __importDefault(require("mongoose"));
8
+ const lodash_1 = __importDefault(require("lodash"));
8
9
  const InventoryItem_model_1 = require("../models/InventoryItem.model");
9
10
  const collaterals_db_1 = require("./collaterals.db");
10
11
  const bbcSheets_db_1 = require("./bbcSheets.db");
11
12
  const collaterals_enum_1 = require("../enums/collaterals.enum");
13
+ const _models_1 = require("../models/_models");
14
+ const bbcDates_db_1 = require("./bbcDates.db");
12
15
  const getSKUUnitCostForAllDates = async (sheetsIds) => {
13
16
  return InventoryItem_model_1.InventoryItemModel.aggregate([
14
17
  {
@@ -55,7 +58,7 @@ const getAllUniqSKUInRangeNew = async (bbcSheetIds) => {
55
58
  },
56
59
  {
57
60
  $lookup: {
58
- from: 'bbcsheets',
61
+ from: _models_1.MODEL_NAMES.BBCSheets,
59
62
  localField: 'bbcSheetId',
60
63
  foreignField: '_id',
61
64
  as: 'bbcSheet',
@@ -67,7 +70,7 @@ const getAllUniqSKUInRangeNew = async (bbcSheetIds) => {
67
70
  },
68
71
  }, {
69
72
  $lookup: {
70
- from: 'bbcdates',
73
+ from: _models_1.MODEL_NAMES.BBCDates,
71
74
  localField: 'bbcSheet.bbcDateId',
72
75
  foreignField: '_id',
73
76
  as: 'bbc',
@@ -116,3 +119,104 @@ const getSKUQtyValueUnitCost = async (bbcDateId) => {
116
119
  ]);
117
120
  };
118
121
  exports.getSKUQtyValueUnitCost = getSKUQtyValueUnitCost;
122
+ const getMovementDifference = async (skus, BBCs) => {
123
+ if (skus) {
124
+ const groupedBbc = skus.map((sku) => {
125
+ let minUnitCost = 0;
126
+ const totalSum = BBCs.map((bbc) => {
127
+ const values = bbc.items
128
+ .filter((item) => item.sku === sku)
129
+ .map((item) => item.unitCost);
130
+ if (values.length) {
131
+ minUnitCost = Math.min(...values);
132
+ }
133
+ const sum = bbc.items.reduce((acc, item) => {
134
+ if (item.sku === sku) {
135
+ return acc + item.qty;
136
+ }
137
+ return acc;
138
+ }, 0);
139
+ return { bbc: bbc.bbcDate, sum };
140
+ });
141
+ return { sku, totalSum, minUnitCost };
142
+ });
143
+ const groupedBbcWithDifferences = groupedBbc.map((group) => {
144
+ const differences = [];
145
+ group.totalSum.forEach((sumGroup, index) => {
146
+ if (index > 0) {
147
+ differences.push({
148
+ bbc: sumGroup.bbc,
149
+ diff: group.totalSum[index - 1].sum - sumGroup.sum,
150
+ previousValue: group.totalSum[index - 1].sum,
151
+ currentValue: sumGroup.sum,
152
+ });
153
+ }
154
+ });
155
+ return { ...group, differences };
156
+ });
157
+ const reportAdditions = [];
158
+ const reportRemoved = [];
159
+ groupedBbcWithDifferences.forEach((group) => {
160
+ const totalDifference = group.differences.reduce((acc, g) => {
161
+ if (g.diff > 0) {
162
+ const diff = acc.increasesOverPeriod + g.diff;
163
+ return {
164
+ ...acc,
165
+ increasesOverPeriod: diff,
166
+ previousValue: g.previousValue,
167
+ currentValue: g.currentValue,
168
+ };
169
+ }
170
+ else {
171
+ const diff = acc.decreasesOverPeriod + Math.abs(g.diff);
172
+ return {
173
+ ...acc,
174
+ decreasesOverPeriod: diff,
175
+ previousValue: g.previousValue,
176
+ currentValue: g.currentValue,
177
+ };
178
+ }
179
+ }, { increasesOverPeriod: 0, decreasesOverPeriod: 0, previousValue: 0, currentValue: 0 });
180
+ const { increasesOverPeriod, decreasesOverPeriod, previousValue, currentValue } = totalDifference;
181
+ if (increasesOverPeriod === 0 && decreasesOverPeriod === 0) {
182
+ return;
183
+ }
184
+ if (increasesOverPeriod > 0) {
185
+ reportRemoved.push({
186
+ sku: group.sku,
187
+ units: increasesOverPeriod,
188
+ value: increasesOverPeriod * group.minUnitCost,
189
+ previousValue: previousValue,
190
+ currentValue: currentValue,
191
+ });
192
+ }
193
+ if (decreasesOverPeriod > 0) {
194
+ reportAdditions.push({
195
+ sku: group.sku,
196
+ units: decreasesOverPeriod,
197
+ value: decreasesOverPeriod * group.minUnitCost,
198
+ previousValue: previousValue,
199
+ currentValue: currentValue,
200
+ });
201
+ }
202
+ });
203
+ return {
204
+ reportAdditions: lodash_1.default.orderBy(reportAdditions, ['sku'], ['asc']),
205
+ reportRemoved: lodash_1.default.orderBy(reportRemoved, ['sku'], ['asc']),
206
+ };
207
+ }
208
+ return { reportAdditions: [], reportRemoved: [] };
209
+ };
210
+ const getInventoryMovement = async (borrowerId, startDate, endDate) => {
211
+ const bbcDates = await (0, bbcDates_db_1.getBBCDatesForPeriod)(borrowerId, new Date(startDate), new Date(endDate));
212
+ const bbcIds = bbcDates.map((bbc) => bbc._id);
213
+ const bbcSheets = await (0, bbcSheets_db_1.getBBCSheetsByType)(bbcIds, collaterals_enum_1.ECollaterals.INVENTORY);
214
+ const skuGroups = await (0, exports.getAllUniqSKUInRangeNew)(bbcSheets.map((bbcSheet) => bbcSheet._id.toString()));
215
+ if (skuGroups.length > 0) {
216
+ const { skus } = skuGroups.pop();
217
+ const BBCs = await (0, collaterals_db_1.getCollateralDocsByBBC)(bbcIds, collaterals_enum_1.ECollaterals.INVENTORY);
218
+ return await getMovementDifference(skus, BBCs);
219
+ }
220
+ return { reportAdditions: [], reportRemoved: [] };
221
+ };
222
+ exports.getInventoryMovement = getInventoryMovement;
package/db/inventories.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  import mongoose from 'mongoose';
2
+ import _ from 'lodash';
2
3
 
3
4
  import { InventoryItemModel } from '../models/InventoryItem.model';
4
- import { COLLATERALS_LOOKUP } from './collaterals.db';
5
+ import { COLLATERALS_LOOKUP, getCollateralDocsByBBC } from './collaterals.db';
5
6
  import { getBBCSheetsByType } from './bbcSheets.db';
6
7
  import { ECollaterals } from '../enums/collaterals.enum';
8
+ import { MODEL_NAMES } from '../models/_models';
9
+ import { getBBCDatesForPeriod } from './bbcDates.db';
7
10
 
8
11
  export const getSKUUnitCostForAllDates = async (sheetsIds: mongoose.Types.ObjectId[]) => {
9
12
  return InventoryItemModel.aggregate<{
@@ -56,7 +59,7 @@ export const getAllUniqSKUInRangeNew = async (bbcSheetIds: string[]): Promise<{
56
59
  },
57
60
  {
58
61
  $lookup: {
59
- from: 'bbcsheets',
62
+ from: MODEL_NAMES.BBCSheets,
60
63
  localField: 'bbcSheetId',
61
64
  foreignField: '_id',
62
65
  as: 'bbcSheet',
@@ -68,7 +71,7 @@ export const getAllUniqSKUInRangeNew = async (bbcSheetIds: string[]): Promise<{
68
71
  },
69
72
  }, {
70
73
  $lookup: {
71
- from: 'bbcdates',
74
+ from: MODEL_NAMES.BBCDates,
72
75
  localField: 'bbcSheet.bbcDateId',
73
76
  foreignField: '_id',
74
77
  as: 'bbc',
@@ -116,3 +119,107 @@ export const getSKUQtyValueUnitCost = async (bbcDateId: string) => {
116
119
  { $sort: { _id: -1 } },
117
120
  ]);
118
121
  };
122
+
123
+ const getMovementDifference = async (skus: string[], BBCs) => {
124
+ if (skus) {
125
+ const groupedBbc = skus.map((sku) => {
126
+ let minUnitCost = 0;
127
+ const totalSum = BBCs.map((bbc) => {
128
+ const values = bbc.items
129
+ .filter((item) => item.sku === sku)
130
+ .map((item) => item.unitCost);
131
+ if (values.length) {
132
+ minUnitCost = Math.min(...values);
133
+ }
134
+ const sum = bbc.items.reduce((acc, item) => {
135
+ if (item.sku === sku) {
136
+ return acc + item.qty;
137
+ }
138
+ return acc;
139
+ }, 0);
140
+ return { bbc: bbc.bbcDate, sum };
141
+ });
142
+ return { sku, totalSum, minUnitCost };
143
+ });
144
+
145
+ const groupedBbcWithDifferences = groupedBbc.map((group) => {
146
+ const differences = [];
147
+ group.totalSum.forEach((sumGroup, index) => {
148
+ if (index > 0) {
149
+ differences.push({
150
+ bbc: sumGroup.bbc,
151
+ diff: group.totalSum[index - 1].sum - sumGroup.sum,
152
+ previousValue: group.totalSum[index - 1].sum,
153
+ currentValue: sumGroup.sum,
154
+ });
155
+ }
156
+ });
157
+ return { ...group, differences };
158
+ });
159
+
160
+ const reportAdditions = [];
161
+ const reportRemoved = [];
162
+ groupedBbcWithDifferences.forEach((group) => {
163
+ const totalDifference = group.differences.reduce((acc, g) => {
164
+ if (g.diff > 0) {
165
+ const diff = acc.increasesOverPeriod + g.diff;
166
+ return {
167
+ ...acc,
168
+ increasesOverPeriod: diff,
169
+ previousValue: g.previousValue,
170
+ currentValue: g.currentValue,
171
+ };
172
+ } else {
173
+ const diff = acc.decreasesOverPeriod + Math.abs(g.diff);
174
+ return {
175
+ ...acc,
176
+ decreasesOverPeriod: diff,
177
+ previousValue: g.previousValue,
178
+ currentValue: g.currentValue,
179
+ };
180
+ }
181
+ }, { increasesOverPeriod: 0, decreasesOverPeriod: 0, previousValue: 0, currentValue: 0 });
182
+
183
+ const { increasesOverPeriod, decreasesOverPeriod, previousValue, currentValue } = totalDifference;
184
+ if (increasesOverPeriod === 0 && decreasesOverPeriod === 0) {
185
+ return;
186
+ }
187
+ if (increasesOverPeriod > 0) {
188
+ reportRemoved.push({
189
+ sku: group.sku,
190
+ units: increasesOverPeriod,
191
+ value: increasesOverPeriod * group.minUnitCost,
192
+ previousValue: previousValue,
193
+ currentValue: currentValue,
194
+ });
195
+ }
196
+ if (decreasesOverPeriod > 0) {
197
+ reportAdditions.push({
198
+ sku: group.sku,
199
+ units: decreasesOverPeriod,
200
+ value: decreasesOverPeriod * group.minUnitCost,
201
+ previousValue: previousValue,
202
+ currentValue: currentValue,
203
+ });
204
+ }
205
+ });
206
+ return {
207
+ reportAdditions: _.orderBy(reportAdditions, ['sku'], ['asc']),
208
+ reportRemoved: _.orderBy(reportRemoved, ['sku'], ['asc']),
209
+ };
210
+ }
211
+ return { reportAdditions: [], reportRemoved: [] };
212
+ }
213
+
214
+ export const getInventoryMovement = async (borrowerId: string, startDate: string, endDate: string) => {
215
+ const bbcDates = await getBBCDatesForPeriod(borrowerId, new Date(startDate), new Date(endDate));
216
+ const bbcIds = bbcDates.map((bbc) => bbc._id);
217
+ const bbcSheets = await getBBCSheetsByType(bbcIds, ECollaterals.INVENTORY);
218
+ const skuGroups = await getAllUniqSKUInRangeNew(bbcSheets.map((bbcSheet) => bbcSheet._id.toString()));
219
+ if (skuGroups.length > 0) {
220
+ const { skus } = skuGroups.pop();
221
+ const BBCs = await getCollateralDocsByBBC(bbcIds, ECollaterals.INVENTORY);
222
+ return await getMovementDifference(skus, BBCs);
223
+ }
224
+ return { reportAdditions: [], reportRemoved: [] };
225
+ }
@@ -15,6 +15,7 @@ const PostponedTransactions_model_1 = require("../models/PostponedTransactions.m
15
15
  const LoanProducts_model_1 = require("../models/LoanProducts.model");
16
16
  const LoanTransaction_model_1 = require("../models/LoanTransaction.model");
17
17
  const loan_types_enum_1 = require("../enums/loan-types.enum");
18
+ const _models_1 = require("../models/_models");
18
19
  dayjs_1.default.extend(minMax_1.default);
19
20
  exports.fieldsToUnset = ['order', '__v', 'createdAt', 'updatedAt', 'borrowerId'];
20
21
  const getLoanProductById = async (productId) => {
@@ -116,7 +117,7 @@ const getPostponedTransactions = async (date, productId, removePostponed = false
116
117
  },
117
118
  }, {
118
119
  $lookup: {
119
- from: 'loan_transactions',
120
+ from: _models_1.MODEL_NAMES.loanTransactions,
120
121
  localField: 'transactionId',
121
122
  foreignField: '_id',
122
123
  as: 'transaction',
@@ -12,6 +12,7 @@ import { ILoanChargeView } from '../models/LoanCharges.model';
12
12
  import { ILoanTransactionDoc, LoanTransaction } from '../models/LoanTransaction.model';
13
13
  import { IStatementPeriod } from '../models/LoanStatementTransaction.model';
14
14
  import { ELoanTypes } from '../enums/loan-types.enum';
15
+ import { MODEL_NAMES } from '../models/_models';
15
16
 
16
17
  dayjs.extend(minMax);
17
18
 
@@ -122,7 +123,7 @@ export const getPostponedTransactions = async (date: Date, productId: string, re
122
123
  },
123
124
  }, {
124
125
  $lookup: {
125
- from: 'loan_transactions',
126
+ from: MODEL_NAMES.loanTransactions,
126
127
  localField: 'transactionId',
127
128
  foreignField: '_id',
128
129
  as: 'transaction',
@@ -12,6 +12,7 @@ const collaterals_enum_1 = require("../enums/collaterals.enum");
12
12
  const Receivable_model_1 = require("../models/Receivable.model");
13
13
  const ReceivableAvailability_model_1 = require("../models/ReceivableAvailability.model");
14
14
  const bbcSheets_db_1 = require("./bbcSheets.db");
15
+ const _models_1 = require("../models/_models");
15
16
  exports.CUSTOMER_NUMBER_SEPARATOR = '~@~';
16
17
  const getLastDigitFromAllValues = async (allBbcDates) => {
17
18
  const allIds = allBbcDates.reduce((acc, bbcDate) => [...acc, ...bbcDate.ids], []);
@@ -239,7 +240,7 @@ const aggregateBbcExposuresGrouped = async (bbcSheetIds) => {
239
240
  ...collaterals_db_1.COLLATERALS_LOOKUP,
240
241
  {
241
242
  $lookup: {
242
- from: 'customergroups',
243
+ from: _models_1.MODEL_NAMES.customerGroups,
243
244
  let: {
244
245
  'res_cod_it': 'customerTitle',
245
246
  'borrowerId': '$borrowerId',
@@ -7,6 +7,7 @@ import { ECollaterals } from '../enums/collaterals.enum';
7
7
  import { ReceivableModel } from '../models/Receivable.model';
8
8
  import { ReceivableAvailabilityModel } from '../models/ReceivableAvailability.model';
9
9
  import { getBBCSheetsByType, getUniqueDates } from './bbcSheets.db';
10
+ import { MODEL_NAMES } from '../models/_models';
10
11
 
11
12
  export const CUSTOMER_NUMBER_SEPARATOR = '~@~';
12
13
 
@@ -250,7 +251,7 @@ export const aggregateBbcExposuresGrouped = async (bbcSheetIds: mongoose.Types.O
250
251
  ...COLLATERALS_LOOKUP,
251
252
  {
252
253
  $lookup: {
253
- from: 'customergroups',
254
+ from: MODEL_NAMES.customerGroups,
254
255
  let: {
255
256
  'res_cod_it': 'customerTitle',
256
257
  'borrowerId': '$borrowerId',
package/db/reports.db.js CHANGED
@@ -21,6 +21,7 @@ const loan_payments_db_1 = require("./loan-payments.db");
21
21
  const brokers_db_1 = require("./brokers.db");
22
22
  const TermLoan_model_1 = require("../models/TermLoan.model");
23
23
  const TermLoanCalculated_model_1 = require("../models/TermLoanCalculated.model");
24
+ const _models_1 = require("../models/_models");
24
25
  var EChargeType;
25
26
  (function (EChargeType) {
26
27
  EChargeType["INTEREST_FEE"] = "INTEREST";
@@ -92,7 +93,7 @@ const getLedgerData = async (params, options = defaultReportLedgerOptions) => {
92
93
  },
93
94
  }, {
94
95
  $lookup: {
95
- from: 'loan_products',
96
+ from: _models_1.MODEL_NAMES.loanProducts,
96
97
  localField: 'productId',
97
98
  foreignField: '_id',
98
99
  as: 'product',
@@ -172,7 +173,7 @@ const getLedgerData = async (params, options = defaultReportLedgerOptions) => {
172
173
  },
173
174
  }, {
174
175
  $lookup: {
175
- from: 'loan_charges',
176
+ from: _models_1.MODEL_NAMES.loanCharges,
176
177
  localField: 'chargeId',
177
178
  foreignField: '_id',
178
179
  as: 'charge',
package/db/reports.db.ts CHANGED
@@ -23,6 +23,7 @@ import { getPaymentForTransaction } from './loan-payments.db';
23
23
  import { enrichWithBrokers } from './brokers.db';
24
24
  import { TermLoanModel } from '../models/TermLoan.model';
25
25
  import { TermLoanCalculatedModel } from '../models/TermLoanCalculated.model';
26
+ import { MODEL_NAMES } from '../models/_models';
26
27
 
27
28
  export enum EChargeType {
28
29
  INTEREST_FEE = 'INTEREST',
@@ -162,7 +163,7 @@ const getLedgerData = async (params: ReportLedgerParams, options = defaultReport
162
163
  },
163
164
  }, {
164
165
  $lookup: {
165
- from: 'loan_products',
166
+ from: MODEL_NAMES.loanProducts,
166
167
  localField: 'productId',
167
168
  foreignField: '_id',
168
169
  as: 'product',
@@ -247,7 +248,7 @@ const getLedgerData = async (params: ReportLedgerParams, options = defaultReport
247
248
  },
248
249
  }, {
249
250
  $lookup: {
250
- from: 'loan_charges',
251
+ from: MODEL_NAMES.loanCharges,
251
252
  localField: 'chargeId',
252
253
  foreignField: '_id',
253
254
  as: 'charge',