@tomei/rental 0.17.4-dev.4 → 0.17.4-dev.6

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 (198) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +18 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/src/ClassMappings/ItemClassMap.d.ts +4 -0
  5. package/dist/src/ClassMappings/ItemClassMap.js +8 -0
  6. package/dist/src/ClassMappings/ItemClassMap.js.map +1 -0
  7. package/dist/src/ClassMappings/index.d.ts +2 -0
  8. package/dist/src/ClassMappings/index.js +6 -0
  9. package/dist/src/ClassMappings/index.js.map +1 -0
  10. package/dist/src/components/agreement/agreement.d.ts +27 -0
  11. package/dist/src/components/agreement/agreement.js +139 -0
  12. package/dist/src/components/agreement/agreement.js.map +1 -0
  13. package/dist/src/components/agreement/agreement.repository.d.ts +8 -0
  14. package/dist/src/components/agreement/agreement.repository.js +67 -0
  15. package/dist/src/components/agreement/agreement.repository.js.map +1 -0
  16. package/dist/src/components/agreement-history/agreement-history.d.ts +17 -0
  17. package/dist/src/components/agreement-history/agreement-history.js +51 -0
  18. package/dist/src/components/agreement-history/agreement-history.js.map +1 -0
  19. package/dist/src/components/agreement-history/agreement-history.repository.d.ts +8 -0
  20. package/dist/src/components/agreement-history/agreement-history.repository.js +67 -0
  21. package/dist/src/components/agreement-history/agreement-history.repository.js.map +1 -0
  22. package/dist/src/components/agreement-signature/agreement-signature.d.ts +27 -0
  23. package/dist/src/components/agreement-signature/agreement-signature.js +112 -0
  24. package/dist/src/components/agreement-signature/agreement-signature.js.map +1 -0
  25. package/dist/src/components/agreement-signature/agreement-signature.repository.d.ts +8 -0
  26. package/dist/src/components/agreement-signature/agreement-signature.repository.js +68 -0
  27. package/dist/src/components/agreement-signature/agreement-signature.repository.js.map +1 -0
  28. package/dist/src/components/booking/booking.d.ts +46 -0
  29. package/dist/src/components/booking/booking.js +314 -0
  30. package/dist/src/components/booking/booking.js.map +1 -0
  31. package/dist/src/components/booking/booking.repository.d.ts +8 -0
  32. package/dist/src/components/booking/booking.repository.js +67 -0
  33. package/dist/src/components/booking/booking.repository.js.map +1 -0
  34. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.d.ts +32 -0
  35. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js +121 -0
  36. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.js.map +1 -0
  37. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.d.ts +9 -0
  38. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js +79 -0
  39. package/dist/src/components/hirer-change-request-new-hirer/hirer-change-request-new-hirer.repository.js.map +1 -0
  40. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.d.ts +22 -0
  41. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js +104 -0
  42. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.js.map +1 -0
  43. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.d.ts +9 -0
  44. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js +80 -0
  45. package/dist/src/components/hirer-change-request-remove-hirer/hirer-change-request-remove-hirer.repository.js.map +1 -0
  46. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.d.ts +30 -0
  47. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.js +225 -0
  48. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.js.map +1 -0
  49. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.d.ts +9 -0
  50. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.js +80 -0
  51. package/dist/src/components/hirer-change-request-signature/hirer-change-request-signature.repository.js.map +1 -0
  52. package/dist/src/components/joint-hirer/joint-hirer.d.ts +26 -0
  53. package/dist/src/components/joint-hirer/joint-hirer.js +152 -0
  54. package/dist/src/components/joint-hirer/joint-hirer.js.map +1 -0
  55. package/dist/src/components/joint-hirer/joint-hirer.repository.d.ts +8 -0
  56. package/dist/src/components/joint-hirer/joint-hirer.repository.js +67 -0
  57. package/dist/src/components/joint-hirer/joint-hirer.repository.js.map +1 -0
  58. package/dist/src/components/rental/rental.d.ts +72 -0
  59. package/dist/src/components/rental/rental.js +914 -0
  60. package/dist/src/components/rental/rental.js.map +1 -0
  61. package/dist/src/components/rental/rental.repository.d.ts +8 -0
  62. package/dist/src/components/rental/rental.repository.js +67 -0
  63. package/dist/src/components/rental/rental.repository.js.map +1 -0
  64. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.d.ts +71 -0
  65. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.js +584 -0
  66. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.js.map +1 -0
  67. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.d.ts +9 -0
  68. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.js +79 -0
  69. package/dist/src/components/rental-hirer-change-request/rental-hirer-change-request.repository.js.map +1 -0
  70. package/dist/src/components/rental-price/rental-price.d.ts +18 -0
  71. package/dist/src/components/rental-price/rental-price.js +79 -0
  72. package/dist/src/components/rental-price/rental-price.js.map +1 -0
  73. package/dist/src/components/rental-price/rental-price.repository.d.ts +8 -0
  74. package/dist/src/components/rental-price/rental-price.repository.js +67 -0
  75. package/dist/src/components/rental-price/rental-price.repository.js.map +1 -0
  76. package/dist/src/database.d.ts +4 -0
  77. package/dist/src/database.js +37 -0
  78. package/dist/src/database.js.map +1 -0
  79. package/dist/src/enum/account-type.enum.d.ts +4 -0
  80. package/dist/src/enum/account-type.enum.js +9 -0
  81. package/dist/src/enum/account-type.enum.js.map +1 -0
  82. package/dist/src/enum/aggrement-status.enum.d.ts +5 -0
  83. package/dist/src/enum/aggrement-status.enum.js +10 -0
  84. package/dist/src/enum/aggrement-status.enum.js.map +1 -0
  85. package/dist/src/enum/agreement-signature-status.enum.d.ts +4 -0
  86. package/dist/src/enum/agreement-signature-status.enum.js +9 -0
  87. package/dist/src/enum/agreement-signature-status.enum.js.map +1 -0
  88. package/dist/src/enum/booking.enum.d.ts +5 -0
  89. package/dist/src/enum/booking.enum.js +10 -0
  90. package/dist/src/enum/booking.enum.js.map +1 -0
  91. package/dist/src/enum/hirer-type.enum.d.ts +4 -0
  92. package/dist/src/enum/hirer-type.enum.js +9 -0
  93. package/dist/src/enum/hirer-type.enum.js.map +1 -0
  94. package/dist/src/enum/index.d.ts +10 -0
  95. package/dist/src/enum/index.js +22 -0
  96. package/dist/src/enum/index.js.map +1 -0
  97. package/dist/src/enum/rental-hirer-change-request-hirer-role.d.ts +4 -0
  98. package/dist/src/enum/rental-hirer-change-request-hirer-role.js +9 -0
  99. package/dist/src/enum/rental-hirer-change-request-hirer-role.js.map +1 -0
  100. package/dist/src/enum/rental-hirer-change-request-status.d.ts +7 -0
  101. package/dist/src/enum/rental-hirer-change-request-status.js +12 -0
  102. package/dist/src/enum/rental-hirer-change-request-status.js.map +1 -0
  103. package/dist/src/enum/rental-hirer-change-request-type.d.ts +4 -0
  104. package/dist/src/enum/rental-hirer-change-request-type.js +9 -0
  105. package/dist/src/enum/rental-hirer-change-request-type.js.map +1 -0
  106. package/dist/src/enum/rental-status.enum.d.ts +9 -0
  107. package/dist/src/enum/rental-status.enum.js +14 -0
  108. package/dist/src/enum/rental-status.enum.js.map +1 -0
  109. package/dist/src/index.d.ts +28 -0
  110. package/dist/src/index.js +69 -0
  111. package/dist/src/index.js.map +1 -0
  112. package/dist/src/interfaces/agreemeent-signature-attr.interface.d.ts +14 -0
  113. package/dist/src/interfaces/agreemeent-signature-attr.interface.js +3 -0
  114. package/dist/src/interfaces/agreemeent-signature-attr.interface.js.map +1 -0
  115. package/dist/src/interfaces/agreement-attr.interface.d.ts +7 -0
  116. package/dist/src/interfaces/agreement-attr.interface.js +3 -0
  117. package/dist/src/interfaces/agreement-attr.interface.js.map +1 -0
  118. package/dist/src/interfaces/agreement-history-attr.interface.d.ts +7 -0
  119. package/dist/src/interfaces/agreement-history-attr.interface.js +3 -0
  120. package/dist/src/interfaces/agreement-history-attr.interface.js.map +1 -0
  121. package/dist/src/interfaces/booking-attr.interface.d.ts +18 -0
  122. package/dist/src/interfaces/booking-attr.interface.js +3 -0
  123. package/dist/src/interfaces/booking-attr.interface.js.map +1 -0
  124. package/dist/src/interfaces/booking-find-all-search-attr.interface.d.ts +12 -0
  125. package/dist/src/interfaces/booking-find-all-search-attr.interface.js +3 -0
  126. package/dist/src/interfaces/booking-find-all-search-attr.interface.js.map +1 -0
  127. package/dist/src/interfaces/hirer-change-request-new-hirer-attr.interface.d.ts +15 -0
  128. package/dist/src/interfaces/hirer-change-request-new-hirer-attr.interface.js +3 -0
  129. package/dist/src/interfaces/hirer-change-request-new-hirer-attr.interface.js.map +1 -0
  130. package/dist/src/interfaces/hirer-change-request-remove-hirer-attr.interface.d.ts +5 -0
  131. package/dist/src/interfaces/hirer-change-request-remove-hirer-attr.interface.js +3 -0
  132. package/dist/src/interfaces/hirer-change-request-remove-hirer-attr.interface.js.map +1 -0
  133. package/dist/src/interfaces/hirer-change-request-signature-attr.interface.d.ts +14 -0
  134. package/dist/src/interfaces/hirer-change-request-signature-attr.interface.js +3 -0
  135. package/dist/src/interfaces/hirer-change-request-signature-attr.interface.js.map +1 -0
  136. package/dist/src/interfaces/index.d.ts +14 -0
  137. package/dist/src/interfaces/index.js +3 -0
  138. package/dist/src/interfaces/index.js.map +1 -0
  139. package/dist/src/interfaces/joint-hirer-attr.interface.d.ts +11 -0
  140. package/dist/src/interfaces/joint-hirer-attr.interface.js +3 -0
  141. package/dist/src/interfaces/joint-hirer-attr.interface.js.map +1 -0
  142. package/dist/src/interfaces/rental-attr.interface.d.ts +24 -0
  143. package/dist/src/interfaces/rental-attr.interface.js +3 -0
  144. package/dist/src/interfaces/rental-attr.interface.js.map +1 -0
  145. package/dist/src/interfaces/rental-find-all-search-attr.interface.d.ts +10 -0
  146. package/dist/src/interfaces/rental-find-all-search-attr.interface.js +3 -0
  147. package/dist/src/interfaces/rental-find-all-search-attr.interface.js.map +1 -0
  148. package/dist/src/interfaces/rental-hirer-change-request.attr.interface.d.ts +16 -0
  149. package/dist/src/interfaces/rental-hirer-change-request.attr.interface.js +3 -0
  150. package/dist/src/interfaces/rental-hirer-change-request.attr.interface.js.map +1 -0
  151. package/dist/src/interfaces/rental-hirer-change-request.update.interface.d.ts +4 -0
  152. package/dist/src/interfaces/rental-hirer-change-request.update.interface.js +3 -0
  153. package/dist/src/interfaces/rental-hirer-change-request.update.interface.js.map +1 -0
  154. package/dist/src/interfaces/rental-price-attr.interface.d.ts +7 -0
  155. package/dist/src/interfaces/rental-price-attr.interface.js +3 -0
  156. package/dist/src/interfaces/rental-price-attr.interface.js.map +1 -0
  157. package/dist/src/interfaces/response-hirer-signature-attr.interface.d.ts +14 -0
  158. package/dist/src/interfaces/response-hirer-signature-attr.interface.js +3 -0
  159. package/dist/src/interfaces/response-hirer-signature-attr.interface.js.map +1 -0
  160. package/dist/src/models/agreement-history.entity.d.ts +10 -0
  161. package/dist/src/models/agreement-history.entity.js +65 -0
  162. package/dist/src/models/agreement-history.entity.js.map +1 -0
  163. package/dist/src/models/agreement-signature.entity.d.ts +18 -0
  164. package/dist/src/models/agreement-signature.entity.js +101 -0
  165. package/dist/src/models/agreement-signature.entity.js.map +1 -0
  166. package/dist/src/models/agreement.entity.d.ts +14 -0
  167. package/dist/src/models/agreement.entity.js +70 -0
  168. package/dist/src/models/agreement.entity.js.map +1 -0
  169. package/dist/src/models/booking.entity.d.ts +21 -0
  170. package/dist/src/models/booking.entity.js +128 -0
  171. package/dist/src/models/booking.entity.js.map +1 -0
  172. package/dist/src/models/hirer-change-request-new-hirer.entity.d.ts +19 -0
  173. package/dist/src/models/hirer-change-request-new-hirer.entity.js +121 -0
  174. package/dist/src/models/hirer-change-request-new-hirer.entity.js.map +1 -0
  175. package/dist/src/models/hirer-change-request-remove-hirer.entity.d.ts +11 -0
  176. package/dist/src/models/hirer-change-request-remove-hirer.entity.js +57 -0
  177. package/dist/src/models/hirer-change-request-remove-hirer.entity.js.map +1 -0
  178. package/dist/src/models/hirer-change-request-signature.entity.d.ts +18 -0
  179. package/dist/src/models/hirer-change-request-signature.entity.js +101 -0
  180. package/dist/src/models/hirer-change-request-signature.entity.js.map +1 -0
  181. package/dist/src/models/index.d.ts +12 -0
  182. package/dist/src/models/index.js +26 -0
  183. package/dist/src/models/index.js.map +1 -0
  184. package/dist/src/models/joint-hirer.entity.d.ts +14 -0
  185. package/dist/src/models/joint-hirer.entity.js +87 -0
  186. package/dist/src/models/joint-hirer.entity.js.map +1 -0
  187. package/dist/src/models/rental-hirer-change-request.entity.d.ts +22 -0
  188. package/dist/src/models/rental-hirer-change-request.entity.js +108 -0
  189. package/dist/src/models/rental-hirer-change-request.entity.js.map +1 -0
  190. package/dist/src/models/rental-price.entity.d.ts +8 -0
  191. package/dist/src/models/rental-price.entity.js +59 -0
  192. package/dist/src/models/rental-price.entity.js.map +1 -0
  193. package/dist/src/models/rental.entity.d.ts +29 -0
  194. package/dist/src/models/rental.entity.js +160 -0
  195. package/dist/src/models/rental.entity.js.map +1 -0
  196. package/dist/tsconfig.tsbuildinfo +1 -0
  197. package/package.json +1 -1
  198. package/src/components/rental-hirer-change-request/rental-hirer-change-request.ts +1 -0
@@ -0,0 +1,914 @@
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
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Rental = void 0;
13
+ const rental_status_enum_1 = require("../../enum/rental-status.enum");
14
+ const rental_repository_1 = require("./rental.repository");
15
+ const general_1 = require("@tomei/general");
16
+ const config_1 = require("@tomei/config");
17
+ const sequelize_1 = require("sequelize");
18
+ const activity_history_1 = require("@tomei/activity-history");
19
+ const account_type_enum_1 = require("../../enum/account-type.enum");
20
+ const joint_hirer_1 = require("../joint-hirer/joint-hirer");
21
+ const joint_hirer_entity_1 = require("../../models/joint-hirer.entity");
22
+ const agreement_repository_1 = require("../agreement/agreement.repository");
23
+ const rentalDb = require("../../database");
24
+ const rental_entity_1 = require("../../models/rental.entity");
25
+ const joint_hirer_repository_1 = require("../joint-hirer/joint-hirer.repository");
26
+ const models_1 = require("../../models");
27
+ const agreement_1 = require("../agreement/agreement");
28
+ const aggrement_status_enum_1 = require("../../enum/aggrement-status.enum");
29
+ const agreement_signature_repository_1 = require("../agreement-signature/agreement-signature.repository");
30
+ const agreement_signature_status_enum_1 = require("../../enum/agreement-signature-status.enum");
31
+ const hirer_type_enum_1 = require("../../enum/hirer-type.enum");
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");
35
+ class Rental extends general_1.ObjectBase {
36
+ get RentalId() {
37
+ return this.ObjectId;
38
+ }
39
+ set RentalId(value) {
40
+ this.ObjectId = value;
41
+ }
42
+ get Status() {
43
+ return this._Status;
44
+ }
45
+ get EscheatmentYN() {
46
+ return this._EscheatmentYN;
47
+ }
48
+ get CreatedById() {
49
+ return this._CreatedById;
50
+ }
51
+ get CreatedAt() {
52
+ return this._CreatedAt;
53
+ }
54
+ get UpdatedById() {
55
+ return this._UpdatedById;
56
+ }
57
+ get UpdatedAt() {
58
+ return this._UpdatedAt;
59
+ }
60
+ setTerminated() {
61
+ this._Status = rental_status_enum_1.RentalStatusEnum.TERMINATED;
62
+ }
63
+ constructor(rentalAttr) {
64
+ super();
65
+ this.ObjectType = 'Rental';
66
+ this.JointHirers = [];
67
+ this.Invoices = [];
68
+ this._EscheatmentYN = 'N';
69
+ if (rentalAttr) {
70
+ this.RentalId = rentalAttr.RentalId;
71
+ this.CustomerId = rentalAttr.CustomerId;
72
+ this.CustomerType = rentalAttr.CustomerType;
73
+ this.ItemId = rentalAttr.ItemId;
74
+ this.ItemType = rentalAttr.ItemType;
75
+ this.PriceId = rentalAttr.PriceId;
76
+ this.StartDateTime = rentalAttr.StartDateTime;
77
+ this.EndDateTime = rentalAttr.EndDateTime;
78
+ this.CancelRemarks = rentalAttr.CancelRemarks;
79
+ this.TerminateRemarks = rentalAttr.TerminateRemarks;
80
+ this.AgreementNo = rentalAttr.AgreementNo;
81
+ this.AccountType = rentalAttr.AccountType;
82
+ this._Status = rentalAttr.Status;
83
+ this._EscheatmentYN = rentalAttr.EscheatmentYN;
84
+ this._CreatedById = rentalAttr.CreatedById;
85
+ this._CreatedAt = rentalAttr.CreatedAt;
86
+ this._UpdatedById = rentalAttr.UpdatedById;
87
+ this._UpdatedAt = rentalAttr.UpdatedAt;
88
+ }
89
+ }
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
+ }
101
+ }
102
+ return new Rental();
103
+ }
104
+ catch (error) {
105
+ throw new general_1.ClassError('Rental', 'RentalErrMsg00', 'Failed To Initialize Price');
106
+ }
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.");
116
+ }
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;
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;
210
+ }
211
+ catch (error) {
212
+ throw error;
213
+ }
214
+ });
215
+ }
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
+ },
229
+ },
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;
240
+ }
241
+ catch (error) {
242
+ throw error;
243
+ }
244
+ });
245
+ }
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');
252
+ if (!isPrivileged) {
253
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'Rental - View' privilege.");
254
+ }
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
+ });
302
+ }
303
+ else {
304
+ options.include = [
305
+ {
306
+ model: models_1.AgreementModel,
307
+ required: false,
308
+ },
309
+ ];
310
+ }
311
+ options = Object.assign(Object.assign({}, options), { where: queryObj });
312
+ }
313
+ return yield Rental._Repo.findAndCountAll(options);
314
+ }
315
+ catch (err) {
316
+ throw err;
317
+ }
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
+ }
344
+ }
345
+ catch (err) {
346
+ throw err;
347
+ }
348
+ });
349
+ }
350
+ setStartDateTime(startDateTime) {
351
+ const startDateTimeObject = new Date(startDateTime);
352
+ startDateTimeObject.setHours(0, 0, 0, 0);
353
+ if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
354
+ throw new general_1.ClassError('Rental', 'RentalErrMsg02', 'StartDateTime cannot be a past datetime.');
355
+ }
356
+ if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
357
+ throw new general_1.ClassError('Rental', 'RentalErrMsg03', 'StartDateTime cannot be more than EndDateTime.');
358
+ }
359
+ if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
360
+ this._Status = rental_status_enum_1.RentalStatusEnum.RESERVED;
361
+ }
362
+ else {
363
+ this._Status = rental_status_enum_1.RentalStatusEnum.ACTIVE;
364
+ }
365
+ }
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;
434
+ }
435
+ catch (err) {
436
+ throw err;
437
+ }
438
+ });
439
+ }
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 = `
449
+ SELECT
450
+ r.*
451
+ FROM
452
+ rental_Rental r
453
+ LEFT JOIN
454
+ rental_JointHirer jh ON r.RentalId = jh.RentalId
455
+ WHERE
456
+ r.Status = 'Active'
457
+ AND (
458
+ r.CustomerId = '${CustomerId}'
459
+ OR jh.CustomerId = '${CustomerId}'
460
+ )
461
+ GROUP BY
462
+ r.RentalId
463
+ `;
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
+ });
499
+ }
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;
519
+ }
520
+ catch (error) {
521
+ console.error('Error in getJointHirers:', error);
522
+ throw error;
523
+ }
524
+ });
525
+ }
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
+ },
541
+ transaction: dbTransaction,
542
+ };
543
+ const joinHirers = yield Rental._JointHirerRepo.findAll(options);
544
+ const customerIds = [this.CustomerId];
545
+ for (let i = 0; i < joinHirers.length; i++) {
546
+ const jointHirer = joinHirers[i];
547
+ customerIds.push(jointHirer.CustomerId);
548
+ }
549
+ return customerIds;
550
+ }
551
+ catch (error) {
552
+ throw error;
553
+ }
554
+ });
555
+ }
556
+ signAgreement(loginUser, party, PartyId, PartyType, dbTransaction, statusAfterSign) {
557
+ return __awaiter(this, void 0, void 0, function* () {
558
+ try {
559
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
560
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_SIGN');
561
+ if (!isPrivileged) {
562
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_SIGN' privilege.");
563
+ }
564
+ const agreement = yield agreement_1.Agreement.init(this.AgreementNo, dbTransaction);
565
+ if (this.Status !== rental_status_enum_1.RentalStatusEnum.PENDING_SIGNING &&
566
+ agreement.Status !== aggrement_status_enum_1.AggrementStatusEnum.GENERATED) {
567
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental is not ready to be signed yet.');
568
+ }
569
+ const signatureList = yield agreement_1.Agreement.getSignatureList(loginUser, this.AgreementNo, dbTransaction);
570
+ const customerSignature = signatureList.find((sig) => sig.PartyId === PartyId &&
571
+ sig.PartyType === PartyType &&
572
+ sig.Party === party &&
573
+ sig.SignatureStatus === agreement_signature_status_enum_1.AgreementSignatureStatusEnum.PENDING);
574
+ if (!customerSignature) {
575
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Signature is not pending.');
576
+ }
577
+ const signaturePayload = {
578
+ SignatureStatus: agreement_signature_status_enum_1.AgreementSignatureStatusEnum.SIGNED,
579
+ SignedAt: new Date(),
580
+ UpdatedById: loginUser.ObjectId,
581
+ UpdatedAt: new Date(),
582
+ };
583
+ yield Rental._AgreementSignatureRepo.update(signaturePayload, {
584
+ where: {
585
+ AgreementNo: this.AgreementNo,
586
+ Party: party,
587
+ PartyId: PartyId,
588
+ PartyType: PartyType,
589
+ },
590
+ transaction: dbTransaction,
591
+ });
592
+ const signatureActivity = new activity_history_1.Activity();
593
+ signatureActivity.ActivityId = signatureActivity.createId();
594
+ signatureActivity.Action = activity_history_1.ActionEnum.UPDATE;
595
+ signatureActivity.Description = 'Update hirer signature';
596
+ signatureActivity.EntityType = 'AgreementSignature';
597
+ signatureActivity.EntityId = this.AgreementNo;
598
+ signatureActivity.EntityValueBefore = JSON.stringify(customerSignature);
599
+ signatureActivity.EntityValueAfter = JSON.stringify(signaturePayload);
600
+ yield signatureActivity.create(loginUser.ObjectId, dbTransaction);
601
+ const updatedSignatureList = yield agreement_1.Agreement.getSignatureList(loginUser, this.AgreementNo, dbTransaction);
602
+ const pendingSignatures = updatedSignatureList.filter((sig) => sig.SignatureStatus === 'Pending');
603
+ if (pendingSignatures.length > 0) {
604
+ return;
605
+ }
606
+ const entityValueAgreementBefore = {
607
+ AgreementNo: agreement.AgreementNo,
608
+ Status: agreement.Status,
609
+ DateSigned: agreement.DateSigned,
610
+ };
611
+ const payload = {
612
+ DateSigned: new Date(),
613
+ Status: aggrement_status_enum_1.AggrementStatusEnum.SIGNED,
614
+ };
615
+ yield Rental._AgreementRepo.update(payload, {
616
+ where: {
617
+ AgreementNo: this.AgreementNo,
618
+ },
619
+ transaction: dbTransaction,
620
+ });
621
+ const entityValueAgreementAfter = Object.assign({ entityValueAgreementBefore }, payload);
622
+ const activity = new activity_history_1.Activity();
623
+ activity.ActivityId = activity.createId();
624
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
625
+ activity.Description = 'Update agreement';
626
+ activity.EntityType = 'RentalAgreement';
627
+ activity.EntityId = this.AgreementNo;
628
+ activity.EntityValueBefore = JSON.stringify(entityValueAgreementBefore);
629
+ activity.EntityValueAfter = JSON.stringify(entityValueAgreementAfter);
630
+ yield activity.create(loginUser.ObjectId, dbTransaction);
631
+ const entityValueRentalBefore = {
632
+ RentalId: this.RentalId,
633
+ CustomerId: this.CustomerId,
634
+ CustomerType: this.CustomerType,
635
+ ItemId: this.ItemId,
636
+ ItemType: this.ItemType,
637
+ PriceId: this.PriceId,
638
+ StartDateTime: this.StartDateTime,
639
+ EndDateTime: this.EndDateTime,
640
+ CancelRemarks: this.CancelRemarks,
641
+ TerminateRemarks: this.TerminateRemarks,
642
+ AgreementNo: this.AgreementNo,
643
+ AccountType: this.AccountType,
644
+ Status: this.Status,
645
+ EscheatmentYN: this.EscheatmentYN,
646
+ CreatedById: this.CreatedById,
647
+ CreatedAt: this.CreatedAt,
648
+ UpdatedById: this.UpdatedById,
649
+ UpdatedAt: this.UpdatedAt,
650
+ };
651
+ this._Status = statusAfterSign
652
+ ? statusAfterSign
653
+ : rental_status_enum_1.RentalStatusEnum.ACTIVE;
654
+ const payloadRental = {
655
+ Status: this._Status,
656
+ UpdatedById: loginUser.ObjectId,
657
+ UpdatedAt: new Date(),
658
+ };
659
+ yield Rental._Repo.update(payloadRental, {
660
+ where: {
661
+ RentalId: this.RentalId,
662
+ },
663
+ transaction: dbTransaction,
664
+ });
665
+ const entityValueRentalAfter = Object.assign({ entityValueRentalBefore }, payloadRental);
666
+ const rentalActivity = new activity_history_1.Activity();
667
+ rentalActivity.ActivityId = activity.createId();
668
+ rentalActivity.Action = activity_history_1.ActionEnum.UPDATE;
669
+ rentalActivity.Description = 'Sign rental agreement';
670
+ rentalActivity.EntityType = 'Rental';
671
+ rentalActivity.EntityId = this.RentalId;
672
+ rentalActivity.EntityValueBefore = JSON.stringify(entityValueRentalBefore);
673
+ rentalActivity.EntityValueAfter = JSON.stringify(entityValueRentalAfter);
674
+ yield rentalActivity.create(loginUser.ObjectId, dbTransaction);
675
+ }
676
+ catch (error) {
677
+ throw error;
678
+ }
679
+ });
680
+ }
681
+ generateAgreement(loginUser, dbTransaction, MediaId) {
682
+ return __awaiter(this, void 0, void 0, function* () {
683
+ try {
684
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
685
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_AGREEMENT_CREATE');
686
+ if (!isPrivileged) {
687
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_AGREEMENT_CREATE' privilege.");
688
+ }
689
+ const agreement = yield agreement_1.Agreement.init(this.AgreementNo, dbTransaction);
690
+ if (!MediaId) {
691
+ throw new general_1.ClassError('Rental', 'RentalErrMsg0X', 'MediaId is required.');
692
+ }
693
+ const EntityValueBefore = agreement;
694
+ agreement.Status = aggrement_status_enum_1.AggrementStatusEnum.GENERATED;
695
+ agreement.MediaId = MediaId;
696
+ yield Rental._AgreementRepo.update({
697
+ Status: agreement.Status,
698
+ MediaId: agreement.MediaId,
699
+ }, {
700
+ where: {
701
+ AgreementNo: this.AgreementNo,
702
+ },
703
+ transaction: dbTransaction,
704
+ });
705
+ const EntityValueAfter = agreement;
706
+ const activity = new activity_history_1.Activity();
707
+ activity.ActivityId = activity.createId();
708
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
709
+ activity.Description = 'Generate agreement.';
710
+ activity.EntityType = 'RentalAgreement';
711
+ activity.EntityId = this.AgreementNo;
712
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
713
+ activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
714
+ yield activity.create(loginUser.ObjectId, dbTransaction);
715
+ const agreementHistory = yield Rental._AgreementHistoryRepo.create({
716
+ AgreementNo: this.AgreementNo,
717
+ MediaId,
718
+ ActivityCompleted: 'Agreement Generated',
719
+ CreatedAt: new Date(),
720
+ }, { transaction: dbTransaction });
721
+ const entityValueAfter = agreementHistory.get({ plain: true });
722
+ const activityHistory = new activity_history_1.Activity();
723
+ activityHistory.ActivityId = activityHistory.createId();
724
+ activityHistory.Action = activity_history_1.ActionEnum.CREATE;
725
+ activityHistory.Description = 'Generate agreement.';
726
+ activityHistory.EntityType = 'RentalAgreementHistory';
727
+ activityHistory.EntityId = agreementHistory.HistoryId.toString();
728
+ activityHistory.EntityValueBefore = JSON.stringify({});
729
+ activityHistory.EntityValueAfter = JSON.stringify(entityValueAfter);
730
+ yield activityHistory.create(loginUser.ObjectId, dbTransaction);
731
+ }
732
+ catch (error) {
733
+ throw error;
734
+ }
735
+ });
736
+ }
737
+ toJSON() {
738
+ return {
739
+ RentalId: this.RentalId,
740
+ CustomerId: this.CustomerId,
741
+ CustomerType: this.CustomerType,
742
+ ItemId: this.ItemId,
743
+ ItemType: this.ItemType,
744
+ PriceId: this.PriceId,
745
+ StartDateTime: this.StartDateTime,
746
+ EndDateTime: this.EndDateTime,
747
+ Status: this.Status,
748
+ CancelRemarks: this.CancelRemarks,
749
+ TerminateRemarks: this.TerminateRemarks,
750
+ EscheatmentYN: this.EscheatmentYN,
751
+ AgreementNo: this.AgreementNo,
752
+ AccountType: this.AccountType,
753
+ CreatedById: this.CreatedById,
754
+ CreatedAt: this.CreatedAt,
755
+ UpdatedById: this.UpdatedById,
756
+ UpdatedAt: this.UpdatedAt,
757
+ };
758
+ }
759
+ getInvoices(loginUser, dbTransaction, accountingSystem) {
760
+ return __awaiter(this, void 0, void 0, function* () {
761
+ try {
762
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
763
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW_INVOICE');
764
+ if (!isPrivileged) {
765
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_VIEW_INVOICE' privilege.");
766
+ }
767
+ if (!this.ObjectId) {
768
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'ObjectId is missing.');
769
+ }
770
+ if (this.Invoices.length > 0) {
771
+ return this.Invoices;
772
+ }
773
+ const search = {
774
+ DocType: finance_1.DocType.INVOICE,
775
+ RelatedObjectId: this.ObjectId,
776
+ RelatedObjectType: this.ObjectType,
777
+ };
778
+ const documents = yield finance_1.Document.findAll(loginUser, dbTransaction, undefined, undefined, search, undefined, accountingSystem);
779
+ this.Invoices = documents.rows.map((doc) => {
780
+ const document = new finance_1.Document(dbTransaction, doc);
781
+ return document;
782
+ });
783
+ return this.Invoices;
784
+ }
785
+ catch (error) {
786
+ throw error;
787
+ }
788
+ });
789
+ }
790
+ getItem(dbTransaction) {
791
+ return __awaiter(this, void 0, void 0, function* () {
792
+ if (this.Item) {
793
+ return this.Item;
794
+ }
795
+ else {
796
+ const ItemClass = ItemClassMap_1.ItemClassMap[this.ItemType];
797
+ if (!ItemClass || typeof ItemClass.init !== 'function') {
798
+ throw new Error(`Invalid or unregistered ItemType: ${this.ItemType}`);
799
+ }
800
+ ItemClass._Repo = ItemClass._Repo || ItemClass.getRepo();
801
+ if (!ItemClass._Repo) {
802
+ throw new Error(`ItemType ${this.ItemType} does not have a repository.`);
803
+ }
804
+ const itemInstance = yield ItemClass.init(this.ItemId, dbTransaction);
805
+ if (!itemInstance) {
806
+ throw new Error(`${this.ItemType} not found with ID ${this.ItemId}`);
807
+ }
808
+ this.Item = itemInstance;
809
+ if (typeof this.Item.setAvailable !== 'function') {
810
+ throw new Error(`${this.ItemType} does not implement setAvailable()`);
811
+ }
812
+ return this.Item;
813
+ }
814
+ });
815
+ }
816
+ cancel(loginUser, dbTransaction, remarks) {
817
+ return __awaiter(this, void 0, void 0, function* () {
818
+ try {
819
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
820
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'RENTAL_CANCEL');
821
+ if (!isPrivileged) {
822
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_CANCEL' privilege.");
823
+ }
824
+ if (!this.ObjectId) {
825
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental not found. Rental Id is empty.');
826
+ }
827
+ if (this.Status === rental_status_enum_1.RentalStatusEnum.ACTIVE ||
828
+ this.Status === rental_status_enum_1.RentalStatusEnum.SUSPENDED) {
829
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', `Rental already ${this.Status}, please do termination process.`);
830
+ }
831
+ if (!remarks) {
832
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Cancellation remarks is required.');
833
+ }
834
+ const entityValueBefore = this.toJSON();
835
+ this._Status = rental_status_enum_1.RentalStatusEnum.CANCELLED;
836
+ this.CancelRemarks = remarks;
837
+ this._UpdatedById = loginUser.ObjectId;
838
+ this._UpdatedAt = new Date();
839
+ yield Rental._Repo.update({
840
+ Status: this.Status,
841
+ CancelRemarks: this.CancelRemarks,
842
+ UpdatedById: this.UpdatedById,
843
+ UpdatedAt: this.UpdatedAt,
844
+ }, {
845
+ where: {
846
+ RentalId: this.RentalId,
847
+ },
848
+ transaction: dbTransaction,
849
+ });
850
+ const item = yield this.getItem(dbTransaction);
851
+ yield item.setAvailable(loginUser, dbTransaction);
852
+ const entityValueAfter = this.toJSON();
853
+ const activity = new activity_history_1.Activity();
854
+ activity.ActivityId = activity.createId();
855
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
856
+ activity.Description = 'Rental Cancellation';
857
+ activity.EntityType = this.ObjectType;
858
+ activity.EntityId = this.ObjectId;
859
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
860
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
861
+ yield activity.create(loginUser.ObjectId, dbTransaction);
862
+ return this;
863
+ }
864
+ catch (error) {
865
+ throw error;
866
+ }
867
+ });
868
+ }
869
+ update(loginUser, dbTransaction, updates) {
870
+ return __awaiter(this, void 0, void 0, function* () {
871
+ try {
872
+ const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
873
+ const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'RENTAL_UPDATE');
874
+ if (!isPrivileged) {
875
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', "You do not have 'RENTAL_UPDATE' privilege.");
876
+ }
877
+ if (!this.ObjectId) {
878
+ throw new general_1.ClassError('Rental', 'RentalErrMsg01', 'Rental not found. Rental Id is empty.');
879
+ }
880
+ const entityValueBefore = this.toJSON();
881
+ yield Rental._Repo.update(updates, {
882
+ where: {
883
+ RentalId: this.RentalId,
884
+ },
885
+ transaction: dbTransaction,
886
+ });
887
+ Object.assign(this, updates);
888
+ this._UpdatedById = loginUser.ObjectId;
889
+ this._UpdatedAt = new Date();
890
+ const entityValueAfter = this.toJSON();
891
+ const activity = new activity_history_1.Activity();
892
+ activity.ActivityId = activity.createId();
893
+ activity.Action = activity_history_1.ActionEnum.UPDATE;
894
+ activity.Description = 'Update Rental';
895
+ activity.EntityType = this.ObjectType;
896
+ activity.EntityId = this.ObjectId;
897
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
898
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
899
+ yield activity.create(loginUser.ObjectId, dbTransaction);
900
+ return this;
901
+ }
902
+ catch (error) {
903
+ throw error;
904
+ }
905
+ });
906
+ }
907
+ }
908
+ exports.Rental = Rental;
909
+ Rental._Repo = new rental_repository_1.RentalRepository();
910
+ Rental._AgreementRepo = new agreement_repository_1.AgreementRepository();
911
+ Rental._JointHirerRepo = new joint_hirer_repository_1.JointHirerRepository();
912
+ Rental._AgreementSignatureRepo = new agreement_signature_repository_1.AgreementSignatureRepository();
913
+ Rental._AgreementHistoryRepo = new agreement_history_repository_1.AgreementHistoryRepository();
914
+ //# sourceMappingURL=rental.js.map