@tomei/rental 0.17.3-dev.6 → 0.17.3-dev.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/dist/components/agreement-signature/agreement-signature.d.ts +4 -0
  2. package/dist/components/agreement-signature/agreement-signature.js +8 -0
  3. package/dist/components/agreement-signature/agreement-signature.js.map +1 -1
  4. package/dist/components/rental/rental.d.ts +2 -1
  5. package/dist/components/rental/rental.js +4 -1
  6. package/dist/components/rental/rental.js.map +1 -1
  7. package/dist/enum/agreement-signature-verification-method.enum.d.ts +4 -0
  8. package/dist/enum/agreement-signature-verification-method.enum.js +9 -0
  9. package/dist/enum/agreement-signature-verification-method.enum.js.map +1 -0
  10. package/dist/enum/index.d.ts +2 -1
  11. package/dist/enum/index.js +3 -1
  12. package/dist/enum/index.js.map +1 -1
  13. package/dist/interfaces/agreemeent-signature-attr.interface.d.ts +4 -0
  14. package/dist/models/agreement-signature.entity.d.ts +4 -0
  15. package/dist/models/agreement-signature.entity.js +22 -0
  16. package/dist/models/agreement-signature.entity.js.map +1 -1
  17. package/dist/src/ClassMappings/ItemClassMap.d.ts +4 -0
  18. package/dist/src/ClassMappings/ItemClassMap.js +8 -0
  19. package/dist/src/ClassMappings/ItemClassMap.js.map +1 -0
  20. package/dist/src/ClassMappings/index.d.ts +2 -0
  21. package/dist/src/ClassMappings/index.js +6 -0
  22. package/dist/src/ClassMappings/index.js.map +1 -0
  23. package/dist/src/components/agreement/agreement.js +91 -72
  24. package/dist/src/components/agreement/agreement.js.map +1 -1
  25. package/dist/src/components/agreement/agreement.repository.js +50 -35
  26. package/dist/src/components/agreement/agreement.repository.js.map +1 -1
  27. package/dist/src/components/agreement-history/agreement-history.js +21 -10
  28. package/dist/src/components/agreement-history/agreement-history.js.map +1 -1
  29. package/dist/src/components/agreement-history/agreement-history.repository.js +50 -35
  30. package/dist/src/components/agreement-history/agreement-history.repository.js.map +1 -1
  31. package/dist/src/components/agreement-signature/agreement-signature.js +70 -57
  32. package/dist/src/components/agreement-signature/agreement-signature.js.map +1 -1
  33. package/dist/src/components/agreement-signature/agreement-signature.repository.js +51 -36
  34. package/dist/src/components/agreement-signature/agreement-signature.repository.js.map +1 -1
  35. package/dist/src/components/booking/booking.js +231 -206
  36. package/dist/src/components/booking/booking.js.map +1 -1
  37. package/dist/src/components/booking/booking.repository.js +50 -35
  38. package/dist/src/components/booking/booking.repository.js.map +1 -1
  39. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js +62 -47
  40. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js.map +1 -1
  41. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js +63 -46
  42. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js.map +1 -1
  43. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js +64 -49
  44. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js.map +1 -1
  45. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js +64 -47
  46. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js.map +1 -1
  47. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.js +167 -150
  48. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.js.map +1 -1
  49. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.js +64 -47
  50. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.js.map +1 -1
  51. package/dist/src/components/joint-hirer/joint-hirer.d.ts +2 -0
  52. package/dist/src/components/joint-hirer/joint-hirer.js +133 -81
  53. package/dist/src/components/joint-hirer/joint-hirer.js.map +1 -1
  54. package/dist/src/components/joint-hirer/joint-hirer.repository.js +50 -35
  55. package/dist/src/components/joint-hirer/joint-hirer.repository.js.map +1 -1
  56. package/dist/src/components/rental/rental.d.ts +12 -3
  57. package/dist/src/components/rental/rental.js +755 -549
  58. package/dist/src/components/rental/rental.js.map +1 -1
  59. package/dist/src/components/rental/rental.repository.js +50 -35
  60. package/dist/src/components/rental/rental.repository.js.map +1 -1
  61. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.js +456 -416
  62. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.js.map +1 -1
  63. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.js +63 -46
  64. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.js.map +1 -1
  65. package/dist/src/components/rental-price/rental-price.js +46 -33
  66. package/dist/src/components/rental-price/rental-price.js.map +1 -1
  67. package/dist/src/components/rental-price/rental-price.repository.js +50 -35
  68. package/dist/src/components/rental-price/rental-price.repository.js.map +1 -1
  69. package/dist/src/enum/agreement-signature-verification-method.enum.d.ts +4 -0
  70. package/dist/src/enum/agreement-signature-verification-method.enum.js +9 -0
  71. package/dist/src/enum/agreement-signature-verification-method.enum.js.map +1 -0
  72. package/dist/src/enum/index.d.ts +2 -1
  73. package/dist/src/enum/index.js +3 -1
  74. package/dist/src/enum/index.js.map +1 -1
  75. package/dist/src/index.d.ts +2 -1
  76. package/dist/src/index.js +3 -1
  77. package/dist/src/index.js.map +1 -1
  78. package/dist/src/models/agreement-signature.entity.d.ts +4 -0
  79. package/dist/src/models/agreement-signature.entity.js +22 -0
  80. package/dist/src/models/agreement-signature.entity.js.map +1 -1
  81. package/dist/src/models/hirer-change-request-new-hirer.entity.d.ts +1 -1
  82. package/dist/src/models/hirer-change-request-remove-hirer.entity.d.ts +1 -1
  83. package/dist/src/models/hirer-change-request-signature.entity.d.ts +1 -1
  84. package/dist/src/models/joint-hirer.entity.js +0 -4
  85. package/dist/src/models/joint-hirer.entity.js.map +1 -1
  86. package/dist/tsconfig.build.tsbuildinfo +1 -1
  87. package/dist/tsconfig.tsbuildinfo +1 -1
  88. package/migrations/agreement-signature-table-migration.js +12 -0
  89. package/package.json +1 -1
  90. package/src/components/agreement-signature/agreement-signature.ts +12 -0
  91. package/src/components/rental/rental.ts +7 -1
  92. package/src/enum/agreement-signature-verification-method.enum.ts +4 -0
  93. package/src/enum/index.ts +2 -0
  94. package/src/interfaces/agreemeent-signature-attr.interface.ts +4 -0
  95. package/src/models/agreement-signature.entity.ts +19 -0
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.Rental = void 0;
4
13
  const rental_status_enum_1 = require("../../enum/rental-status.enum");
@@ -21,6 +30,8 @@ const agreement_signature_repository_1 = require("../agreement-signature/agreeme
21
30
  const agreement_signature_status_enum_1 = require("../../enum/agreement-signature-status.enum");
22
31
  const hirer_type_enum_1 = require("../../enum/hirer-type.enum");
23
32
  const agreement_history_repository_1 = require("../agreement-history/agreement-history.repository");
33
+ const finance_1 = require("@tomei/finance");
34
+ const ItemClassMap_1 = require("../../ClassMappings/ItemClassMap");
24
35
  class Rental extends general_1.ObjectBase {
25
36
  get RentalId() {
26
37
  return this.ObjectId;
@@ -53,6 +64,7 @@ class Rental extends general_1.ObjectBase {
53
64
  super();
54
65
  this.ObjectType = 'Rental';
55
66
  this.JointHirers = [];
67
+ this.Invoices = [];
56
68
  this._EscheatmentYN = 'N';
57
69
  if (rentalAttr) {
58
70
  this.RentalId = rentalAttr.RentalId;
@@ -75,257 +87,265 @@ class Rental extends general_1.ObjectBase {
75
87
  this._UpdatedAt = rentalAttr.UpdatedAt;
76
88
  }
77
89
  }
78
- static async init(dbTransaction, rentalId) {
79
- try {
80
- if (rentalId) {
81
- const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
82
- if (rental) {
83
- return new Rental(rental);
84
- }
85
- else {
86
- throw new general_1.ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
90
+ static init(dbTransaction, rentalId) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ try {
93
+ if (rentalId) {
94
+ const rental = yield Rental._Repo.findByPk(rentalId, dbTransaction);
95
+ if (rental) {
96
+ return new Rental(rental);
97
+ }
98
+ else {
99
+ throw new general_1.ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
100
+ }
87
101
  }
102
+ return new Rental();
88
103
  }
89
- return new Rental();
90
- }
91
- catch (error) {
92
- throw new general_1.ClassError('Rental', 'RentalErrMsg00', 'Failed To Initialize Price');
93
- }
94
- }
95
- async create(rentalPrice, loginUser, jointHirers, dbTransaction) {
96
- try {
97
- const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
98
- const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Rental - Create');
99
- if (!isPrivileged) {
100
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - Create' privilege.");
101
- }
102
- const isItemAvailable = await Rental.isItemAvailable(this.ItemId, this.ItemType, this.StartDateTime, this.EndDateTime, dbTransaction);
103
- if (!isItemAvailable) {
104
- throw new general_1.ClassError('Rental', 'RentalErrMsg02', 'Rental Item is not available at current date.');
104
+ catch (error) {
105
+ throw new general_1.ClassError('Rental', 'RentalErrMsg00', 'Failed To Initialize Rental');
105
106
  }
106
- await Rental._AgreementRepo.create({
107
- AgreementNo: this.AgreementNo,
108
- Status: 'Not Generated',
109
- }, {
110
- transaction: dbTransaction,
111
- });
112
- await Rental._AgreementSignatureRepo.create({
113
- SignatureId: this.createId(),
114
- AgreementNo: this.AgreementNo,
115
- Party: hirer_type_enum_1.HirerTypeEnum.PRIMARY,
116
- PartyId: this.CustomerId,
117
- PartyType: 'Customer',
118
- SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING,
119
- SignedAt: new Date(),
120
- CreatedById: loginUser.ObjectId,
121
- CreatedAt: new Date(),
122
- UpdatedById: loginUser.ObjectId,
123
- UpdatedAt: new Date(),
124
- }, { transaction: dbTransaction });
125
- if (jointHirers && jointHirers.length > 0) {
126
- for (const jointHirer of jointHirers) {
127
- await Rental._AgreementSignatureRepo.create({
128
- SignatureId: this.createId(),
129
- AgreementNo: this.AgreementNo,
130
- Party: hirer_type_enum_1.HirerTypeEnum.JOINT,
131
- PartyId: jointHirer.CustomerId,
132
- PartyType: 'Customer',
133
- SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING,
134
- SignedAt: new Date(),
135
- CreatedById: loginUser.ObjectId,
136
- CreatedAt: new Date(),
137
- UpdatedById: loginUser.ObjectId,
138
- UpdatedAt: new Date(),
139
- }, { transaction: dbTransaction });
107
+ });
108
+ }
109
+ create(rentalPrice, loginUser, jointHirers, dbTransaction) {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ try {
112
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
113
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'Rental - Create');
114
+ if (!isPrivileged) {
115
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - Create' privilege.");
140
116
  }
141
- }
142
- if (!rentalPrice.PriceId) {
143
- const rentalPriceData = await rentalPrice.create(loginUser, dbTransaction);
144
- this.PriceId = rentalPriceData.PriceId;
145
- }
146
- else {
147
- this.PriceId = rentalPrice.PriceId;
148
- }
149
- this.RentalId = this.createId();
150
- this._Status = rental_status_enum_1.RentalStatusEnum.PENDING_SIGNING;
151
- this._CreatedById = loginUser.ObjectId;
152
- this._UpdatedById = loginUser.ObjectId;
153
- this._CreatedAt = new Date();
154
- this._UpdatedAt = new Date();
155
- const data = {
156
- RentalId: this.RentalId,
157
- CustomerId: this.CustomerId,
158
- CustomerType: this.CustomerType,
159
- ItemId: this.ItemId,
160
- ItemType: this.ItemType,
161
- PriceId: this.PriceId,
162
- StartDateTime: this.StartDateTime,
163
- EndDateTime: this.EndDateTime,
164
- CancelRemarks: this.CancelRemarks,
165
- TerminateRemarks: this.TerminateRemarks,
166
- AgreementNo: this.AgreementNo,
167
- AccountType: this.AccountType,
168
- Status: this.Status,
169
- EscheatmentYN: this.EscheatmentYN,
170
- CreatedById: this.CreatedById,
171
- CreatedAt: this.CreatedAt,
172
- UpdatedById: this.UpdatedById,
173
- UpdatedAt: this.UpdatedAt,
174
- };
175
- await Rental._Repo.create(data, {
176
- transaction: dbTransaction,
177
- });
178
- if (jointHirers) {
179
- for (let i = 0; i < jointHirers.length; i++) {
180
- jointHirers[i].RentalId = this.RentalId;
181
- await jointHirers[i].create(loginUser, dbTransaction);
182
- this.JointHirers.push(jointHirers[i]);
117
+ const isItemAvailable = yield Rental.isItemAvailable(this.ItemId, this.ItemType, this.StartDateTime, this.EndDateTime, dbTransaction);
118
+ if (!isItemAvailable) {
119
+ throw new general_1.ClassError('Rental', 'RentalErrMsg02', 'Rental Item is not available at current date.');
120
+ }
121
+ yield Rental._AgreementRepo.create({
122
+ AgreementNo: this.AgreementNo,
123
+ Status: 'Not Generated',
124
+ }, {
125
+ transaction: dbTransaction,
126
+ });
127
+ yield Rental._AgreementSignatureRepo.create({
128
+ SignatureId: this.createId(),
129
+ AgreementNo: this.AgreementNo,
130
+ Party: hirer_type_enum_1.HirerTypeEnum.PRIMARY,
131
+ PartyId: this.CustomerId,
132
+ PartyType: 'Customer',
133
+ SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING,
134
+ SignedAt: null,
135
+ CreatedById: loginUser.ObjectId,
136
+ CreatedAt: new Date(),
137
+ UpdatedById: loginUser.ObjectId,
138
+ UpdatedAt: new Date(),
139
+ }, { transaction: dbTransaction });
140
+ if (jointHirers && jointHirers.length > 0) {
141
+ for (const jointHirer of jointHirers) {
142
+ yield Rental._AgreementSignatureRepo.create({
143
+ SignatureId: this.createId(),
144
+ AgreementNo: this.AgreementNo,
145
+ Party: hirer_type_enum_1.HirerTypeEnum.JOINT,
146
+ PartyId: jointHirer.CustomerId,
147
+ PartyType: 'Customer',
148
+ SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING,
149
+ SignedAt: null,
150
+ CreatedById: loginUser.ObjectId,
151
+ CreatedAt: new Date(),
152
+ UpdatedById: loginUser.ObjectId,
153
+ UpdatedAt: new Date(),
154
+ }, { transaction: dbTransaction });
155
+ }
156
+ }
157
+ if (!rentalPrice.PriceId) {
158
+ const rentalPriceData = yield rentalPrice.create(loginUser, dbTransaction);
159
+ this.PriceId = rentalPriceData.PriceId;
160
+ }
161
+ else {
162
+ this.PriceId = rentalPrice.PriceId;
183
163
  }
164
+ this.RentalId = this.createId();
165
+ this._Status = rental_status_enum_1.RentalStatusEnum.PENDING_SIGNING;
166
+ this._CreatedById = loginUser.ObjectId;
167
+ this._UpdatedById = loginUser.ObjectId;
168
+ this._CreatedAt = new Date();
169
+ this._UpdatedAt = new Date();
170
+ const data = {
171
+ RentalId: this.RentalId,
172
+ CustomerId: this.CustomerId,
173
+ CustomerType: this.CustomerType,
174
+ ItemId: this.ItemId,
175
+ ItemType: this.ItemType,
176
+ PriceId: this.PriceId,
177
+ StartDateTime: this.StartDateTime,
178
+ EndDateTime: this.EndDateTime,
179
+ CancelRemarks: this.CancelRemarks,
180
+ TerminateRemarks: this.TerminateRemarks,
181
+ AgreementNo: this.AgreementNo,
182
+ AccountType: this.AccountType,
183
+ Status: this.Status,
184
+ EscheatmentYN: this.EscheatmentYN,
185
+ CreatedById: this.CreatedById,
186
+ CreatedAt: this.CreatedAt,
187
+ UpdatedById: this.UpdatedById,
188
+ UpdatedAt: this.UpdatedAt,
189
+ };
190
+ yield Rental._Repo.create(data, {
191
+ transaction: dbTransaction,
192
+ });
193
+ if (jointHirers) {
194
+ for (let i = 0; i < jointHirers.length; i++) {
195
+ jointHirers[i].RentalId = this.RentalId;
196
+ yield jointHirers[i].create(loginUser, dbTransaction);
197
+ this.JointHirers.push(jointHirers[i]);
198
+ }
199
+ }
200
+ const activity = new activity_history_1.Activity();
201
+ activity.ActivityId = activity.createId();
202
+ activity.Action = activity_history_1.ActionEnum.CREATE;
203
+ activity.Description = 'Add Rental';
204
+ activity.EntityType = 'Rental';
205
+ activity.EntityId = this.RentalId;
206
+ activity.EntityValueBefore = JSON.stringify({});
207
+ activity.EntityValueAfter = JSON.stringify(data);
208
+ yield activity.create(loginUser.ObjectId, dbTransaction);
209
+ return this;
184
210
  }
185
- const activity = new activity_history_1.Activity();
186
- activity.ActivityId = activity.createId();
187
- activity.Action = activity_history_1.ActionEnum.CREATE;
188
- activity.Description = 'Add Rental';
189
- activity.EntityType = 'Rental';
190
- activity.EntityId = this.RentalId;
191
- activity.EntityValueBefore = JSON.stringify({});
192
- activity.EntityValueAfter = JSON.stringify(data);
193
- await activity.create(loginUser.ObjectId, dbTransaction);
194
- return this;
195
- }
196
- catch (error) {
197
- throw error;
198
- }
211
+ catch (error) {
212
+ throw error;
213
+ }
214
+ });
199
215
  }
200
- static async isItemAvailable(itemId, itemType, startDateTime, endDateTime, dbTransaction) {
201
- try {
202
- const rental = await Rental._Repo.findOne({
203
- where: {
204
- ItemId: itemId,
205
- ItemType: itemType,
206
- StartDateTime: {
207
- [sequelize_1.Op.lte]: endDateTime,
208
- },
209
- EndDateTime: {
210
- [sequelize_1.Op.gte]: startDateTime,
216
+ static isItemAvailable(itemId, itemType, startDateTime, endDateTime, dbTransaction) {
217
+ return __awaiter(this, void 0, void 0, function* () {
218
+ try {
219
+ const rental = yield Rental._Repo.findOne({
220
+ where: {
221
+ ItemId: itemId,
222
+ ItemType: itemType,
223
+ StartDateTime: {
224
+ [sequelize_1.Op.lte]: endDateTime,
225
+ },
226
+ EndDateTime: {
227
+ [sequelize_1.Op.gte]: startDateTime,
228
+ },
211
229
  },
212
- },
213
- transaction: dbTransaction,
214
- });
215
- if (!rental) {
216
- return true;
230
+ transaction: dbTransaction,
231
+ });
232
+ if (!rental) {
233
+ return true;
234
+ }
235
+ if (rental.Status === rental_status_enum_1.RentalStatusEnum.TERMINATED ||
236
+ rental.Status === rental_status_enum_1.RentalStatusEnum.CANCELLED) {
237
+ return true;
238
+ }
239
+ return false;
217
240
  }
218
- if (rental.Status === rental_status_enum_1.RentalStatusEnum.TERMINATED ||
219
- rental.Status === rental_status_enum_1.RentalStatusEnum.CANCELLED) {
220
- return true;
241
+ catch (error) {
242
+ throw error;
221
243
  }
222
- return false;
223
- }
224
- catch (error) {
225
- throw error;
226
- }
244
+ });
227
245
  }
228
- static async findAll(authContext, dbTransaction, page, row, search) {
229
- var _a;
230
- try {
231
- const systemCode = await config_1.ApplicationConfig.getComponentConfigValue('system-code');
232
- if ('loginUser' in authContext) {
233
- const isPrivileged = await authContext.loginUser.checkPrivileges(systemCode, 'Rental - View');
246
+ static findAll(loginUser, dbTransaction, page, row, search) {
247
+ return __awaiter(this, void 0, void 0, function* () {
248
+ var _a;
249
+ try {
250
+ const systemCode = yield config_1.ApplicationConfig.getComponentConfigValue('system-code');
251
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'Rental - View');
234
252
  if (!isPrivileged) {
235
253
  throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
236
254
  }
237
- }
238
- const queryObj = {};
239
- let options = {
240
- transaction: dbTransaction,
241
- order: [['CreatedAt', 'DESC']],
242
- };
243
- if (page && row) {
244
- options = Object.assign(Object.assign({}, options), { limit: row, offset: row * (page - 1) });
245
- }
246
- if (search) {
247
- Object.entries(search).forEach(([key, value]) => {
248
- if (key === 'StartDateTime') {
249
- queryObj[key] = {
250
- [sequelize_1.Op.gte]: value,
251
- };
252
- }
253
- else if (key === 'EndDateTime') {
254
- queryObj[key] = {
255
- [sequelize_1.Op.lte]: value,
256
- };
255
+ const queryObj = {};
256
+ let options = {
257
+ transaction: dbTransaction,
258
+ order: [['CreatedAt', 'DESC']],
259
+ };
260
+ if (page && row) {
261
+ options = Object.assign(Object.assign({}, options), { limit: row, offset: row * (page - 1) });
262
+ }
263
+ if (search) {
264
+ Object.entries(search).forEach(([key, value]) => {
265
+ if (key === 'StartDateTime') {
266
+ queryObj[key] = {
267
+ [sequelize_1.Op.gte]: value,
268
+ };
269
+ }
270
+ else if (key === 'EndDateTime') {
271
+ queryObj[key] = {
272
+ [sequelize_1.Op.lte]: value,
273
+ };
274
+ }
275
+ else if (key === 'CustomerId') {
276
+ queryObj[key] = {
277
+ [sequelize_1.Op.substring]: value,
278
+ };
279
+ options.include = [
280
+ {
281
+ model: joint_hirer_entity_1.JointHirerModel,
282
+ required: false,
283
+ where: {
284
+ CustomerId: {
285
+ [sequelize_1.Op.substring]: value,
286
+ },
287
+ },
288
+ },
289
+ ];
290
+ }
291
+ else {
292
+ queryObj[key] = {
293
+ [sequelize_1.Op.substring]: value,
294
+ };
295
+ }
296
+ });
297
+ if (((_a = options === null || options === void 0 ? void 0 : options.include) === null || _a === void 0 ? void 0 : _a.length) > 1) {
298
+ options.include.push({
299
+ model: models_1.AgreementModel,
300
+ required: false,
301
+ });
257
302
  }
258
- else if (key === 'CustomerId') {
259
- queryObj[key] = {
260
- [sequelize_1.Op.substring]: value,
261
- };
303
+ else {
262
304
  options.include = [
263
305
  {
264
- model: joint_hirer_entity_1.JointHirerModel,
306
+ model: models_1.AgreementModel,
265
307
  required: false,
266
- where: {
267
- CustomerId: {
268
- [sequelize_1.Op.substring]: value,
269
- },
270
- },
271
308
  },
272
309
  ];
273
310
  }
274
- else {
275
- queryObj[key] = {
276
- [sequelize_1.Op.substring]: value,
277
- };
278
- }
279
- });
280
- if (((_a = options === null || options === void 0 ? void 0 : options.include) === null || _a === void 0 ? void 0 : _a.length) > 1) {
281
- options.include.push({
282
- model: models_1.AgreementModel,
283
- required: false,
284
- });
285
- }
286
- else {
287
- options.include = [
288
- {
289
- model: models_1.AgreementModel,
290
- required: false,
291
- },
292
- ];
311
+ options = Object.assign(Object.assign({}, options), { where: queryObj });
293
312
  }
294
- options = Object.assign(Object.assign({}, options), { where: queryObj });
313
+ return yield Rental._Repo.findAndCountAll(options);
295
314
  }
296
- return await Rental._Repo.findAndCountAll(options);
297
- }
298
- catch (err) {
299
- throw err;
300
- }
301
- }
302
- static async checkActiveByItemId(loginUser, itemId, itemType, dbTransaction) {
303
- try {
304
- const systemCode = await config_1.ApplicationConfig.getComponentConfigValue('system-code');
305
- const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Rental - View');
306
- if (!isPrivileged) {
307
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
315
+ catch (err) {
316
+ throw err;
308
317
  }
309
- const queryObj = {
310
- ItemId: itemId,
311
- ItemType: itemType,
312
- Status: rental_status_enum_1.RentalStatusEnum.ACTIVE,
313
- };
314
- const options = {
315
- transaction: dbTransaction,
316
- where: queryObj,
317
- };
318
- const rental = await Rental._Repo.findOne(options);
319
- if (rental) {
320
- return true;
318
+ });
319
+ }
320
+ static checkActiveByItemId(loginUser, itemId, itemType, dbTransaction) {
321
+ return __awaiter(this, void 0, void 0, function* () {
322
+ try {
323
+ const systemCode = yield config_1.ApplicationConfig.getComponentConfigValue('system-code');
324
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'Rental - View');
325
+ if (!isPrivileged) {
326
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
327
+ }
328
+ const queryObj = {
329
+ ItemId: itemId,
330
+ ItemType: itemType,
331
+ Status: rental_status_enum_1.RentalStatusEnum.ACTIVE,
332
+ };
333
+ const options = {
334
+ transaction: dbTransaction,
335
+ where: queryObj,
336
+ };
337
+ const rental = yield Rental._Repo.findOne(options);
338
+ if (rental) {
339
+ return true;
340
+ }
341
+ else {
342
+ return false;
343
+ }
321
344
  }
322
- else {
323
- return false;
345
+ catch (err) {
346
+ throw err;
324
347
  }
325
- }
326
- catch (err) {
327
- throw err;
328
- }
348
+ });
329
349
  }
330
350
  setStartDateTime(startDateTime) {
331
351
  const startDateTimeObject = new Date(startDateTime);
@@ -343,86 +363,89 @@ class Rental extends general_1.ObjectBase {
343
363
  this._Status = rental_status_enum_1.RentalStatusEnum.ACTIVE;
344
364
  }
345
365
  }
346
- async periodEndProcess(loginUser, dbTransaction, stockInventory) {
347
- try {
348
- const systemCode = await config_1.ApplicationConfig.getComponentConfigValue('system-code');
349
- const isPrivileged = await loginUser.checkPrivileges(systemCode, 'Rental – PeriodEndProcess');
350
- if (!isPrivileged) {
351
- throw new general_1.ClassError('Rental', 'RentalErrMsg04', "You do not have 'Rental - PeriodEndProcess' privilege.");
352
- }
353
- if (this.AgreementNo === undefined || this.AgreementNo === null) {
354
- throw new general_1.ClassError('Rental', 'RentalErrMsg05', 'Rental must be existing rental.');
366
+ periodEndProcess(loginUser, dbTransaction, stockInventory) {
367
+ return __awaiter(this, void 0, void 0, function* () {
368
+ try {
369
+ const systemCode = yield config_1.ApplicationConfig.getComponentConfigValue('system-code');
370
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'Rental – PeriodEndProcess');
371
+ if (!isPrivileged) {
372
+ throw new general_1.ClassError('Rental', 'RentalErrMsg04', "You do not have 'Rental - PeriodEndProcess' privilege.");
373
+ }
374
+ if (this.AgreementNo === undefined || this.AgreementNo === null) {
375
+ throw new general_1.ClassError('Rental', 'RentalErrMsg05', 'Rental must be existing rental.');
376
+ }
377
+ if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
378
+ throw new general_1.ClassError('Rental', 'RentalErrMsg06', 'Rental period is not ending today.');
379
+ }
380
+ yield stockInventory.setAvailable(loginUser, dbTransaction);
381
+ const entityValueBefore = {
382
+ RentalId: this.RentalId,
383
+ CustomerId: this.CustomerId,
384
+ CustomerType: this.CustomerType,
385
+ ItemId: this.ItemId,
386
+ ItemType: this.ItemType,
387
+ PriceId: this.PriceId,
388
+ StartDateTime: this.StartDateTime,
389
+ EndDateTime: this.EndDateTime,
390
+ CancelRemarks: this.CancelRemarks,
391
+ TerminateRemarks: this.TerminateRemarks,
392
+ AgreementNo: this.AgreementNo,
393
+ AccountType: this.AccountType,
394
+ Status: this.Status,
395
+ EscheatmentYN: this.EscheatmentYN,
396
+ CreatedById: this.CreatedById,
397
+ CreatedAt: this.CreatedAt,
398
+ UpdatedById: this.UpdatedById,
399
+ UpdatedAt: this.UpdatedAt,
400
+ };
401
+ this.setTerminated();
402
+ this._UpdatedById = loginUser.ObjectId;
403
+ this._UpdatedAt = new Date();
404
+ const entityValueAfter = {
405
+ RentalId: this.RentalId,
406
+ CustomerId: this.CustomerId,
407
+ CustomerType: this.CustomerType,
408
+ ItemId: this.ItemId,
409
+ ItemType: this.ItemType,
410
+ PriceId: this.PriceId,
411
+ StartDateTime: this.StartDateTime,
412
+ EndDateTime: this.EndDateTime,
413
+ CancelRemarks: this.CancelRemarks,
414
+ TerminateRemarks: this.TerminateRemarks,
415
+ AgreementNo: this.AgreementNo,
416
+ AccountType: this.AccountType,
417
+ Status: this.Status,
418
+ EscheatmentYN: this.EscheatmentYN,
419
+ CreatedById: this.CreatedById,
420
+ CreatedAt: this.CreatedAt,
421
+ UpdatedById: this.UpdatedById,
422
+ UpdatedAt: this.UpdatedAt,
423
+ };
424
+ const activity = new activity_history_1.Activity();
425
+ activity.ActivityId = activity.createId();
426
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
427
+ activity.Description = 'Set Rental as Terminated';
428
+ activity.EntityType = 'Rental';
429
+ activity.EntityId = this.RentalId;
430
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
431
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
432
+ yield activity.create(loginUser.ObjectId, dbTransaction);
433
+ return this;
355
434
  }
356
- if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
357
- throw new general_1.ClassError('Rental', 'RentalErrMsg06', 'Rental period is not ending today.');
435
+ catch (err) {
436
+ throw err;
358
437
  }
359
- await stockInventory.setAvailable(loginUser, dbTransaction);
360
- const entityValueBefore = {
361
- RentalId: this.RentalId,
362
- CustomerId: this.CustomerId,
363
- CustomerType: this.CustomerType,
364
- ItemId: this.ItemId,
365
- ItemType: this.ItemType,
366
- PriceId: this.PriceId,
367
- StartDateTime: this.StartDateTime,
368
- EndDateTime: this.EndDateTime,
369
- CancelRemarks: this.CancelRemarks,
370
- TerminateRemarks: this.TerminateRemarks,
371
- AgreementNo: this.AgreementNo,
372
- AccountType: this.AccountType,
373
- Status: this.Status,
374
- EscheatmentYN: this.EscheatmentYN,
375
- CreatedById: this.CreatedById,
376
- CreatedAt: this.CreatedAt,
377
- UpdatedById: this.UpdatedById,
378
- UpdatedAt: this.UpdatedAt,
379
- };
380
- this.setTerminated();
381
- this._UpdatedById = loginUser.ObjectId;
382
- this._UpdatedAt = new Date();
383
- const entityValueAfter = {
384
- RentalId: this.RentalId,
385
- CustomerId: this.CustomerId,
386
- CustomerType: this.CustomerType,
387
- ItemId: this.ItemId,
388
- ItemType: this.ItemType,
389
- PriceId: this.PriceId,
390
- StartDateTime: this.StartDateTime,
391
- EndDateTime: this.EndDateTime,
392
- CancelRemarks: this.CancelRemarks,
393
- TerminateRemarks: this.TerminateRemarks,
394
- AgreementNo: this.AgreementNo,
395
- AccountType: this.AccountType,
396
- Status: this.Status,
397
- EscheatmentYN: this.EscheatmentYN,
398
- CreatedById: this.CreatedById,
399
- CreatedAt: this.CreatedAt,
400
- UpdatedById: this.UpdatedById,
401
- UpdatedAt: this.UpdatedAt,
402
- };
403
- const activity = new activity_history_1.Activity();
404
- activity.ActivityId = activity.createId();
405
- activity.Action = activity_history_1.ActionEnum.UPDATE;
406
- activity.Description = 'Set Rental as Terminated';
407
- activity.EntityType = 'Rental';
408
- activity.EntityId = this.RentalId;
409
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
410
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
411
- await activity.create(loginUser.ObjectId, dbTransaction);
412
- return this;
413
- }
414
- catch (err) {
415
- throw err;
416
- }
438
+ });
417
439
  }
418
- async getCustomerActiveRentals(loginUser, dbTransaction, CustomerId) {
419
- try {
420
- const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
421
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
422
- if (!isPrivileged) {
423
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
424
- }
425
- const query = `
440
+ getCustomerActiveRentals(loginUser, dbTransaction, CustomerId) {
441
+ return __awaiter(this, void 0, void 0, function* () {
442
+ try {
443
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
444
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
445
+ if (!isPrivileged) {
446
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
447
+ }
448
+ const query = `
426
449
  SELECT
427
450
  r.*
428
451
  FROM
@@ -438,269 +461,452 @@ class Rental extends general_1.ObjectBase {
438
461
  GROUP BY
439
462
  r.RentalId
440
463
  `;
441
- const db = rentalDb.getConnection();
442
- const result = await db.query(query, {
443
- type: sequelize_1.QueryTypes.SELECT,
444
- transaction: dbTransaction,
445
- model: rental_entity_1.RentalModel,
446
- mapToModel: true,
447
- });
448
- const records = result.map((record) => {
449
- return {
450
- RentalId: record.RentalId,
451
- CustomerId: record.CustomerId,
452
- CustomerType: record.CustomerType,
453
- ItemId: record.ItemId,
454
- ItemType: record.ItemType,
455
- PriceId: record.PriceId,
456
- StartDateTime: record.StartDateTime,
457
- EndDateTime: record.EndDateTime,
458
- CancelRemarks: record.CancelRemarks,
459
- TerminateRemarks: record.TerminateRemarks,
460
- AgreementNo: record.AgreementNo,
461
- AccountType: record.AccountType,
462
- Status: record.Status,
463
- EscheatmentYN: record.EscheatmentYN,
464
- CreatedById: record.CreatedById,
465
- CreatedAt: record.CreatedAt,
466
- UpdatedById: record.UpdatedById,
467
- UpdatedAt: record.UpdatedAt,
468
- };
469
- });
470
- return records;
471
- }
472
- catch (error) {
473
- throw error;
474
- }
464
+ const db = rentalDb.getConnection();
465
+ const result = yield db.query(query, {
466
+ type: sequelize_1.QueryTypes.SELECT,
467
+ transaction: dbTransaction,
468
+ model: rental_entity_1.RentalModel,
469
+ mapToModel: true,
470
+ });
471
+ const records = result.map((record) => {
472
+ return {
473
+ RentalId: record.RentalId,
474
+ CustomerId: record.CustomerId,
475
+ CustomerType: record.CustomerType,
476
+ ItemId: record.ItemId,
477
+ ItemType: record.ItemType,
478
+ PriceId: record.PriceId,
479
+ StartDateTime: record.StartDateTime,
480
+ EndDateTime: record.EndDateTime,
481
+ CancelRemarks: record.CancelRemarks,
482
+ TerminateRemarks: record.TerminateRemarks,
483
+ AgreementNo: record.AgreementNo,
484
+ AccountType: record.AccountType,
485
+ Status: record.Status,
486
+ EscheatmentYN: record.EscheatmentYN,
487
+ CreatedById: record.CreatedById,
488
+ CreatedAt: record.CreatedAt,
489
+ UpdatedById: record.UpdatedById,
490
+ UpdatedAt: record.UpdatedAt,
491
+ };
492
+ });
493
+ return records;
494
+ }
495
+ catch (error) {
496
+ throw error;
497
+ }
498
+ });
475
499
  }
476
- async getJointHirers(dbTransaction) {
477
- try {
478
- if (!this.RentalId) {
479
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
500
+ getJointHirers(dbTransaction) {
501
+ return __awaiter(this, void 0, void 0, function* () {
502
+ try {
503
+ if (!this.RentalId) {
504
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
505
+ }
506
+ if (this.AccountType !== account_type_enum_1.RentalAccountTypeEnum.JOINT) {
507
+ throw new general_1.ClassError('Rental', 'RentalErrMsg07', 'This rental does not have joint hirers.');
508
+ }
509
+ const jointHirers = yield Rental._JointHirerRepo.findAll({
510
+ where: {
511
+ RentalId: this.RentalId,
512
+ },
513
+ transaction: dbTransaction,
514
+ });
515
+ const jointHirerInstances = Promise.all(jointHirers.map((hirer) => __awaiter(this, void 0, void 0, function* () {
516
+ return yield joint_hirer_1.JointHirer.init(hirer.HirerId, dbTransaction);
517
+ })));
518
+ return jointHirerInstances;
480
519
  }
481
- if (this.AccountType !== account_type_enum_1.RentalAccountTypeEnum.JOINT) {
482
- throw new general_1.ClassError('Rental', 'RentalErrMsg07', 'This rental does not have joint hirers.');
520
+ catch (error) {
521
+ console.error('Error in getJointHirers:', error);
522
+ throw error;
483
523
  }
484
- const jointHirers = await Rental._JointHirerRepo.findAll({
485
- where: {
486
- RentalId: this.RentalId,
487
- },
488
- transaction: dbTransaction,
489
- });
490
- const jointHirerInstances = Promise.all(jointHirers.map(async (hirer) => {
491
- return await joint_hirer_1.JointHirer.init(hirer.HirerId, dbTransaction);
492
- }));
493
- return jointHirerInstances;
494
- }
495
- catch (error) {
496
- console.error('Error in getJointHirers:', error);
497
- throw error;
498
- }
524
+ });
499
525
  }
500
- async getCustomerIds(loginUser, dbTransaction) {
501
- try {
502
- const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
503
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
504
- if (!isPrivileged) {
505
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
526
+ getCustomerIds(loginUser, dbTransaction) {
527
+ return __awaiter(this, void 0, void 0, function* () {
528
+ try {
529
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
530
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
531
+ if (!isPrivileged) {
532
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
533
+ }
534
+ if (!this.RentalId) {
535
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
536
+ }
537
+ const options = {
538
+ where: {
539
+ RentalId: this.RentalId,
540
+ Status: 'Active',
541
+ },
542
+ transaction: dbTransaction,
543
+ };
544
+ const joinHirers = yield Rental._JointHirerRepo.findAll(options);
545
+ const customerIds = [this.CustomerId];
546
+ for (let i = 0; i < joinHirers.length; i++) {
547
+ const jointHirer = joinHirers[i];
548
+ customerIds.push(jointHirer.CustomerId);
549
+ }
550
+ return customerIds;
506
551
  }
507
- if (!this.RentalId) {
508
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
552
+ catch (error) {
553
+ throw error;
509
554
  }
510
- const options = {
511
- where: {
555
+ });
556
+ }
557
+ signAgreement(loginUser, party, PartyId, PartyType, dbTransaction, statusAfterSign, method, justification) {
558
+ return __awaiter(this, void 0, void 0, function* () {
559
+ try {
560
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
561
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_SIGN');
562
+ if (!isPrivileged) {
563
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_SIGN' privilege.");
564
+ }
565
+ const agreement = yield agreement_1.Agreement.init(this.AgreementNo, dbTransaction);
566
+ if (this.Status !== rental_status_enum_1.RentalStatusEnum.PENDING_SIGNING &&
567
+ agreement.Status !== aggrement_status_enum_1.AggrementStatusEnum.GENERATED) {
568
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental is not ready to be signed yet.');
569
+ }
570
+ const signatureList = yield agreement_1.Agreement.getSignatureList(loginUser, this.AgreementNo, dbTransaction);
571
+ const customerSignature = signatureList.find((sig) => sig.PartyId === PartyId &&
572
+ sig.PartyType === PartyType &&
573
+ sig.Party === party &&
574
+ sig.SignatureStatus === agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING);
575
+ if (!customerSignature) {
576
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Signature is not pending.');
577
+ }
578
+ const signaturePayload = {
579
+ SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.SIGNED,
580
+ SignedAt: new Date(),
581
+ UpdatedById: loginUser.ObjectId,
582
+ UpdatedAt: new Date(),
583
+ VerificationMethod: method,
584
+ VerificationJustification: justification,
585
+ VerifiedById: loginUser.ObjectId,
586
+ };
587
+ yield Rental._AgreementSignatureRepo.update(signaturePayload, {
588
+ where: {
589
+ AgreementNo: this.AgreementNo,
590
+ Party: party,
591
+ PartyId: PartyId,
592
+ PartyType: PartyType,
593
+ },
594
+ transaction: dbTransaction,
595
+ });
596
+ const signatureActivity = new activity_history_1.Activity();
597
+ signatureActivity.ActivityId = signatureActivity.createId();
598
+ signatureActivity.Action = activity_history_1.ActionEnum.UPDATE;
599
+ signatureActivity.Description = 'Update hirer signature';
600
+ signatureActivity.EntityType = 'AgreementSignature';
601
+ signatureActivity.EntityId = this.AgreementNo;
602
+ signatureActivity.EntityValueBefore = JSON.stringify(customerSignature);
603
+ signatureActivity.EntityValueAfter = JSON.stringify(signaturePayload);
604
+ yield signatureActivity.create(loginUser.ObjectId, dbTransaction);
605
+ const updatedSignatureList = yield agreement_1.Agreement.getSignatureList(loginUser, this.AgreementNo, dbTransaction);
606
+ const pendingSignatures = updatedSignatureList.filter((sig) => sig.SignatureStatus === 'Pending');
607
+ if (pendingSignatures.length > 0) {
608
+ return;
609
+ }
610
+ const entityValueAgreementBefore = {
611
+ AgreementNo: agreement.AgreementNo,
612
+ Status: agreement.Status,
613
+ DateSigned: agreement.DateSigned,
614
+ };
615
+ const payload = {
616
+ DateSigned: new Date(),
617
+ Status: aggrement_status_enum_1.AggrementStatusEnum.SIGNED,
618
+ };
619
+ yield Rental._AgreementRepo.update(payload, {
620
+ where: {
621
+ AgreementNo: this.AgreementNo,
622
+ },
623
+ transaction: dbTransaction,
624
+ });
625
+ const entityValueAgreementAfter = Object.assign({ entityValueAgreementBefore }, payload);
626
+ const activity = new activity_history_1.Activity();
627
+ activity.ActivityId = activity.createId();
628
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
629
+ activity.Description = 'Update agreement';
630
+ activity.EntityType = 'RentalAgreement';
631
+ activity.EntityId = this.AgreementNo;
632
+ activity.EntityValueBefore = JSON.stringify(entityValueAgreementBefore);
633
+ activity.EntityValueAfter = JSON.stringify(entityValueAgreementAfter);
634
+ yield activity.create(loginUser.ObjectId, dbTransaction);
635
+ const entityValueRentalBefore = {
512
636
  RentalId: this.RentalId,
513
- },
514
- transaction: dbTransaction,
515
- };
516
- const joinHirers = await Rental._JointHirerRepo.findAll(options);
517
- const customerIds = [this.CustomerId];
518
- for (let i = 0; i < joinHirers.length; i++) {
519
- const jointHirer = joinHirers[i];
520
- customerIds.push(jointHirer.CustomerId);
637
+ CustomerId: this.CustomerId,
638
+ CustomerType: this.CustomerType,
639
+ ItemId: this.ItemId,
640
+ ItemType: this.ItemType,
641
+ PriceId: this.PriceId,
642
+ StartDateTime: this.StartDateTime,
643
+ EndDateTime: this.EndDateTime,
644
+ CancelRemarks: this.CancelRemarks,
645
+ TerminateRemarks: this.TerminateRemarks,
646
+ AgreementNo: this.AgreementNo,
647
+ AccountType: this.AccountType,
648
+ Status: this.Status,
649
+ EscheatmentYN: this.EscheatmentYN,
650
+ CreatedById: this.CreatedById,
651
+ CreatedAt: this.CreatedAt,
652
+ UpdatedById: this.UpdatedById,
653
+ UpdatedAt: this.UpdatedAt,
654
+ };
655
+ this._Status = statusAfterSign
656
+ ? statusAfterSign
657
+ : rental_status_enum_1.RentalStatusEnum.ACTIVE;
658
+ const payloadRental = {
659
+ Status: this._Status,
660
+ UpdatedById: loginUser.ObjectId,
661
+ UpdatedAt: new Date(),
662
+ };
663
+ yield Rental._Repo.update(payloadRental, {
664
+ where: {
665
+ RentalId: this.RentalId,
666
+ },
667
+ transaction: dbTransaction,
668
+ });
669
+ const entityValueRentalAfter = Object.assign({ entityValueRentalBefore }, payloadRental);
670
+ const rentalActivity = new activity_history_1.Activity();
671
+ rentalActivity.ActivityId = activity.createId();
672
+ rentalActivity.Action = activity_history_1.ActionEnum.UPDATE;
673
+ rentalActivity.Description = 'Sign rental agreement';
674
+ rentalActivity.EntityType = 'Rental';
675
+ rentalActivity.EntityId = this.RentalId;
676
+ rentalActivity.EntityValueBefore = JSON.stringify(entityValueRentalBefore);
677
+ rentalActivity.EntityValueAfter = JSON.stringify(entityValueRentalAfter);
678
+ yield rentalActivity.create(loginUser.ObjectId, dbTransaction);
521
679
  }
522
- return customerIds;
523
- }
524
- catch (error) {
525
- throw error;
526
- }
680
+ catch (error) {
681
+ throw error;
682
+ }
683
+ });
527
684
  }
528
- async signAgreement(loginUser, party, PartyId, PartyType, dbTransaction, statusAfterSign) {
529
- try {
530
- const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
531
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_SIGN');
532
- if (!isPrivileged) {
533
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_SIGN' privilege.");
685
+ generateAgreement(loginUser, dbTransaction, MediaId) {
686
+ return __awaiter(this, void 0, void 0, function* () {
687
+ try {
688
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
689
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_AGREEMENT_CREATE');
690
+ if (!isPrivileged) {
691
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_AGREEMENT_CREATE' privilege.");
692
+ }
693
+ const agreement = yield agreement_1.Agreement.init(this.AgreementNo, dbTransaction);
694
+ if (!MediaId) {
695
+ throw new general_1.ClassError('Rental', 'RentalErrMsg0X', 'MediaId is required.');
696
+ }
697
+ const EntityValueBefore = agreement;
698
+ agreement.Status = aggrement_status_enum_1.AggrementStatusEnum.GENERATED;
699
+ agreement.MediaId = MediaId;
700
+ yield Rental._AgreementRepo.update({
701
+ Status: agreement.Status,
702
+ MediaId: agreement.MediaId,
703
+ }, {
704
+ where: {
705
+ AgreementNo: this.AgreementNo,
706
+ },
707
+ transaction: dbTransaction,
708
+ });
709
+ const EntityValueAfter = agreement;
710
+ const activity = new activity_history_1.Activity();
711
+ activity.ActivityId = activity.createId();
712
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
713
+ activity.Description = 'Generate agreement.';
714
+ activity.EntityType = 'RentalAgreement';
715
+ activity.EntityId = this.AgreementNo;
716
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
717
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
718
+ yield activity.create(loginUser.ObjectId, dbTransaction);
719
+ const agreementHistory = yield Rental._AgreementHistoryRepo.create({
720
+ AgreementNo: this.AgreementNo,
721
+ MediaId,
722
+ ActivityCompleted: 'Agreement Generated',
723
+ CreatedAt: new Date(),
724
+ }, { transaction: dbTransaction });
725
+ const entityValueAfter = agreementHistory.get({ plain: true });
726
+ const activityHistory = new activity_history_1.Activity();
727
+ activityHistory.ActivityId = activityHistory.createId();
728
+ activityHistory.Action = activity_history_1.ActionEnum.CREATE;
729
+ activityHistory.Description = 'Generate agreement.';
730
+ activityHistory.EntityType = 'RentalAgreementHistory';
731
+ activityHistory.EntityId = agreementHistory.HistoryId.toString();
732
+ activityHistory.EntityValueBefore = JSON.stringify({});
733
+ activityHistory.EntityValueAfter = JSON.stringify(entityValueAfter);
734
+ yield activityHistory.create(loginUser.ObjectId, dbTransaction);
534
735
  }
535
- const agreement = await agreement_1.Agreement.init(this.AgreementNo, dbTransaction);
536
- if (this.Status !== rental_status_enum_1.RentalStatusEnum.PENDING_SIGNING &&
537
- agreement.Status !== aggrement_status_enum_1.AggrementStatusEnum.GENERATED) {
538
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental is not ready to be signed yet.');
736
+ catch (error) {
737
+ throw error;
738
+ }
739
+ });
740
+ }
741
+ toJSON() {
742
+ return {
743
+ RentalId: this.RentalId,
744
+ CustomerId: this.CustomerId,
745
+ CustomerType: this.CustomerType,
746
+ ItemId: this.ItemId,
747
+ ItemType: this.ItemType,
748
+ PriceId: this.PriceId,
749
+ StartDateTime: this.StartDateTime,
750
+ EndDateTime: this.EndDateTime,
751
+ Status: this.Status,
752
+ CancelRemarks: this.CancelRemarks,
753
+ TerminateRemarks: this.TerminateRemarks,
754
+ EscheatmentYN: this.EscheatmentYN,
755
+ AgreementNo: this.AgreementNo,
756
+ AccountType: this.AccountType,
757
+ CreatedById: this.CreatedById,
758
+ CreatedAt: this.CreatedAt,
759
+ UpdatedById: this.UpdatedById,
760
+ UpdatedAt: this.UpdatedAt,
761
+ };
762
+ }
763
+ getInvoices(loginUser, dbTransaction, accountingSystem) {
764
+ return __awaiter(this, void 0, void 0, function* () {
765
+ try {
766
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
767
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW_INVOICE');
768
+ if (!isPrivileged) {
769
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_VIEW_INVOICE' privilege.");
770
+ }
771
+ if (!this.ObjectId) {
772
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'ObjectId is missing.');
773
+ }
774
+ if (this.Invoices.length > 0) {
775
+ return this.Invoices;
776
+ }
777
+ const search = {
778
+ DocType: finance_1.DocType.INVOICE,
779
+ RelatedObjectId: this.ObjectId,
780
+ RelatedObjectType: this.ObjectType,
781
+ };
782
+ const documents = yield finance_1.Document.findAll(loginUser, dbTransaction, undefined, undefined, search, undefined, accountingSystem);
783
+ this.Invoices = documents.rows.map((doc) => {
784
+ const document = new finance_1.Document(dbTransaction, doc);
785
+ return document;
786
+ });
787
+ return this.Invoices;
539
788
  }
540
- const signatureList = await agreement_1.Agreement.getSignatureList(loginUser, this.AgreementNo, dbTransaction);
541
- const customerSignature = signatureList.find((sig) => sig.PartyId === PartyId &&
542
- sig.PartyType === PartyType &&
543
- sig.Party === party &&
544
- sig.SignatureStatus === agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING);
545
- if (!customerSignature) {
546
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Signature is not pending.');
789
+ catch (error) {
790
+ throw error;
547
791
  }
548
- const signaturePayload = {
549
- SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.SIGNED,
550
- SignedAt: new Date(),
551
- UpdatedById: loginUser.ObjectId,
552
- UpdatedAt: new Date(),
553
- };
554
- await Rental._AgreementSignatureRepo.update(signaturePayload, {
555
- where: {
556
- AgreementNo: this.AgreementNo,
557
- Party: party,
558
- PartyId: PartyId,
559
- PartyType: PartyType,
560
- },
561
- transaction: dbTransaction,
562
- });
563
- const signatureActivity = new activity_history_1.Activity();
564
- signatureActivity.ActivityId = signatureActivity.createId();
565
- signatureActivity.Action = activity_history_1.ActionEnum.UPDATE;
566
- signatureActivity.Description = 'Update hirer signature';
567
- signatureActivity.EntityType = 'AgreementSignature';
568
- signatureActivity.EntityId = this.AgreementNo;
569
- signatureActivity.EntityValueBefore = JSON.stringify(customerSignature);
570
- signatureActivity.EntityValueAfter = JSON.stringify(signaturePayload);
571
- await signatureActivity.create(loginUser.ObjectId, dbTransaction);
572
- const updatedSignatureList = await agreement_1.Agreement.getSignatureList(loginUser, this.AgreementNo, dbTransaction);
573
- const pendingSignatures = updatedSignatureList.filter((sig) => sig.SignatureStatus === 'Pending');
574
- if (pendingSignatures.length > 0) {
575
- return;
792
+ });
793
+ }
794
+ getItem(dbTransaction) {
795
+ return __awaiter(this, void 0, void 0, function* () {
796
+ if (this.Item) {
797
+ return this.Item;
576
798
  }
577
- const entityValueAgreementBefore = {
578
- AgreementNo: agreement.AgreementNo,
579
- Status: agreement.Status,
580
- DateSigned: agreement.DateSigned,
581
- };
582
- const payload = {
583
- DateSigned: new Date(),
584
- Status: aggrement_status_enum_1.AggrementStatusEnum.SIGNED,
585
- };
586
- await Rental._AgreementRepo.update(payload, {
587
- where: {
588
- AgreementNo: this.AgreementNo,
589
- },
590
- transaction: dbTransaction,
591
- });
592
- const entityValueAgreementAfter = Object.assign({ entityValueAgreementBefore }, payload);
593
- const activity = new activity_history_1.Activity();
594
- activity.ActivityId = activity.createId();
595
- activity.Action = activity_history_1.ActionEnum.UPDATE;
596
- activity.Description = 'Update agreement';
597
- activity.EntityType = 'RentalAgreement';
598
- activity.EntityId = this.AgreementNo;
599
- activity.EntityValueBefore = JSON.stringify(entityValueAgreementBefore);
600
- activity.EntityValueAfter = JSON.stringify(entityValueAgreementAfter);
601
- await activity.create(loginUser.ObjectId, dbTransaction);
602
- const entityValueRentalBefore = {
603
- RentalId: this.RentalId,
604
- CustomerId: this.CustomerId,
605
- CustomerType: this.CustomerType,
606
- ItemId: this.ItemId,
607
- ItemType: this.ItemType,
608
- PriceId: this.PriceId,
609
- StartDateTime: this.StartDateTime,
610
- EndDateTime: this.EndDateTime,
611
- CancelRemarks: this.CancelRemarks,
612
- TerminateRemarks: this.TerminateRemarks,
613
- AgreementNo: this.AgreementNo,
614
- AccountType: this.AccountType,
615
- Status: this.Status,
616
- EscheatmentYN: this.EscheatmentYN,
617
- CreatedById: this.CreatedById,
618
- CreatedAt: this.CreatedAt,
619
- UpdatedById: this.UpdatedById,
620
- UpdatedAt: this.UpdatedAt,
621
- };
622
- this._Status = statusAfterSign
623
- ? statusAfterSign
624
- : rental_status_enum_1.RentalStatusEnum.ACTIVE;
625
- const payloadRental = {
626
- Status: this._Status,
627
- UpdatedById: loginUser.ObjectId,
628
- UpdatedAt: new Date(),
629
- };
630
- await Rental._Repo.update(payloadRental, {
631
- where: {
632
- RentalId: this.RentalId,
633
- },
634
- transaction: dbTransaction,
635
- });
636
- const entityValueRentalAfter = Object.assign({ entityValueRentalBefore }, payloadRental);
637
- const rentalActivity = new activity_history_1.Activity();
638
- rentalActivity.ActivityId = activity.createId();
639
- rentalActivity.Action = activity_history_1.ActionEnum.UPDATE;
640
- rentalActivity.Description = 'Sign rental agreement';
641
- rentalActivity.EntityType = 'Rental';
642
- rentalActivity.EntityId = this.RentalId;
643
- rentalActivity.EntityValueBefore = JSON.stringify(entityValueRentalBefore);
644
- rentalActivity.EntityValueAfter = JSON.stringify(entityValueRentalAfter);
645
- await rentalActivity.create(loginUser.ObjectId, dbTransaction);
646
- }
647
- catch (error) {
648
- throw error;
649
- }
799
+ else {
800
+ const ItemClass = ItemClassMap_1.ItemClassMap[this.ItemType];
801
+ if (!ItemClass || typeof ItemClass.init !== 'function') {
802
+ throw new Error(`Invalid or unregistered ItemType: ${this.ItemType}`);
803
+ }
804
+ ItemClass._Repo = ItemClass._Repo || ItemClass.getRepo();
805
+ if (!ItemClass._Repo) {
806
+ throw new Error(`ItemType ${this.ItemType} does not have a repository.`);
807
+ }
808
+ const itemInstance = yield ItemClass.init(this.ItemId, dbTransaction);
809
+ if (!itemInstance) {
810
+ throw new Error(`${this.ItemType} not found with ID ${this.ItemId}`);
811
+ }
812
+ this.Item = itemInstance;
813
+ if (typeof this.Item.setAvailable !== 'function') {
814
+ throw new Error(`${this.ItemType} does not implement setAvailable()`);
815
+ }
816
+ return this.Item;
817
+ }
818
+ });
819
+ }
820
+ cancel(loginUser, dbTransaction, remarks) {
821
+ return __awaiter(this, void 0, void 0, function* () {
822
+ try {
823
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
824
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'RENTAL_CANCEL');
825
+ if (!isPrivileged) {
826
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_CANCEL' privilege.");
827
+ }
828
+ if (!this.ObjectId) {
829
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental not found. Rental Id is empty.');
830
+ }
831
+ if (this.Status === rental_status_enum_1.RentalStatusEnum.ACTIVE ||
832
+ this.Status === rental_status_enum_1.RentalStatusEnum.SUSPENDED) {
833
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', `Rental already ${this.Status}, please do termination process.`);
834
+ }
835
+ if (!remarks) {
836
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Cancellation remarks is required.');
837
+ }
838
+ const entityValueBefore = this.toJSON();
839
+ this._Status = rental_status_enum_1.RentalStatusEnum.CANCELLED;
840
+ this.CancelRemarks = remarks;
841
+ this._UpdatedById = loginUser.ObjectId;
842
+ this._UpdatedAt = new Date();
843
+ yield Rental._Repo.update({
844
+ Status: this.Status,
845
+ CancelRemarks: this.CancelRemarks,
846
+ UpdatedById: this.UpdatedById,
847
+ UpdatedAt: this.UpdatedAt,
848
+ }, {
849
+ where: {
850
+ RentalId: this.RentalId,
851
+ },
852
+ transaction: dbTransaction,
853
+ });
854
+ const item = yield this.getItem(dbTransaction);
855
+ yield item.setAvailable(loginUser, dbTransaction);
856
+ const entityValueAfter = this.toJSON();
857
+ const activity = new activity_history_1.Activity();
858
+ activity.ActivityId = activity.createId();
859
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
860
+ activity.Description = 'Rental Cancellation';
861
+ activity.EntityType = this.ObjectType;
862
+ activity.EntityId = this.ObjectId;
863
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
864
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
865
+ yield activity.create(loginUser.ObjectId, dbTransaction);
866
+ return this;
867
+ }
868
+ catch (error) {
869
+ throw error;
870
+ }
871
+ });
650
872
  }
651
- async generateAgreement(loginUser, dbTransaction, MediaId) {
652
- try {
653
- const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
654
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_AGREEMENT_CREATE');
655
- if (!isPrivileged) {
656
- throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_AGREEMENT_CREATE' privilege.");
873
+ update(loginUser, dbTransaction, updates) {
874
+ return __awaiter(this, void 0, void 0, function* () {
875
+ try {
876
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
877
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'RENTAL_UPDATE');
878
+ if (!isPrivileged) {
879
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_UPDATE' privilege.");
880
+ }
881
+ if (!this.ObjectId) {
882
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental not found. Rental Id is empty.');
883
+ }
884
+ const entityValueBefore = this.toJSON();
885
+ yield Rental._Repo.update(updates, {
886
+ where: {
887
+ RentalId: this.RentalId,
888
+ },
889
+ transaction: dbTransaction,
890
+ });
891
+ Object.assign(this, updates);
892
+ this._UpdatedById = loginUser.ObjectId;
893
+ this._UpdatedAt = new Date();
894
+ const entityValueAfter = this.toJSON();
895
+ const activity = new activity_history_1.Activity();
896
+ activity.ActivityId = activity.createId();
897
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
898
+ activity.Description = 'Update Rental';
899
+ activity.EntityType = this.ObjectType;
900
+ activity.EntityId = this.ObjectId;
901
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
902
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
903
+ yield activity.create(loginUser.ObjectId, dbTransaction);
904
+ return this;
657
905
  }
658
- const agreement = await agreement_1.Agreement.init(this.AgreementNo, dbTransaction);
659
- if (!MediaId) {
660
- throw new general_1.ClassError('Rental', 'RentalErrMsg0X', 'MediaId is required.');
906
+ catch (error) {
907
+ throw error;
661
908
  }
662
- const EntityValueBefore = agreement;
663
- agreement.Status = aggrement_status_enum_1.AggrementStatusEnum.GENERATED;
664
- agreement.MediaId = MediaId;
665
- await Rental._AgreementRepo.update({
666
- Status: agreement.Status,
667
- MediaId: agreement.MediaId,
668
- }, {
669
- where: {
670
- AgreementNo: this.AgreementNo,
671
- },
672
- transaction: dbTransaction,
673
- });
674
- const EntityValueAfter = agreement;
675
- const activity = new activity_history_1.Activity();
676
- activity.ActivityId = activity.createId();
677
- activity.Action = activity_history_1.ActionEnum.UPDATE;
678
- activity.Description = 'Generate agreement.';
679
- activity.EntityType = 'RentalAgreement';
680
- activity.EntityId = this.AgreementNo;
681
- activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
682
- activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
683
- await activity.create(loginUser.ObjectId, dbTransaction);
684
- const agreementHistory = await Rental._AgreementHistoryRepo.create({
685
- AgreementNo: this.AgreementNo,
686
- MediaId,
687
- ActivityCompleted: 'Agreement Generated',
688
- CreatedAt: new Date(),
689
- }, { transaction: dbTransaction });
690
- const entityValueAfter = agreementHistory.get({ plain: true });
691
- const activityHistory = new activity_history_1.Activity();
692
- activityHistory.ActivityId = activityHistory.createId();
693
- activityHistory.Action = activity_history_1.ActionEnum.CREATE;
694
- activityHistory.Description = 'Generate agreement.';
695
- activityHistory.EntityType = 'RentalAgreementHistory';
696
- activityHistory.EntityId = agreementHistory.HistoryId.toString();
697
- activityHistory.EntityValueBefore = JSON.stringify({});
698
- activityHistory.EntityValueAfter = JSON.stringify(entityValueAfter);
699
- await activityHistory.create(loginUser.ObjectId, dbTransaction);
700
- }
701
- catch (error) {
702
- throw error;
703
- }
909
+ });
704
910
  }
705
911
  }
706
912
  exports.Rental = Rental;