payservedb 8.9.2 → 8.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/.env +2 -2
  2. package/ZOHO_INTEGRATION_SCHEMA.md +644 -644
  3. package/index.js +326 -327
  4. package/package.json +17 -17
  5. package/src/models/account.js +52 -52
  6. package/src/models/agent_departments.js +59 -59
  7. package/src/models/agent_notifications.js +53 -53
  8. package/src/models/agent_performance.js +127 -127
  9. package/src/models/agent_roles.js +77 -77
  10. package/src/models/agents.js +154 -154
  11. package/src/models/apilog.js +18 -18
  12. package/src/models/approvalsWorkflows.js +49 -49
  13. package/src/models/archivedapilog.js +18 -18
  14. package/src/models/asset.js +92 -92
  15. package/src/models/assetsAssignment.js +64 -64
  16. package/src/models/auditTrail.js +346 -346
  17. package/src/models/bankdetails.js +47 -47
  18. package/src/models/billerAddress.js +124 -124
  19. package/src/models/booking_invoice.js +165 -165
  20. package/src/models/bookinganalytics.js +63 -63
  21. package/src/models/bookingconfig.js +45 -45
  22. package/src/models/bookingproperty.js +179 -179
  23. package/src/models/bookingreservation.js +239 -239
  24. package/src/models/bookingrevenuerecord.js +84 -84
  25. package/src/models/budget.js +95 -95
  26. package/src/models/budgetCategory.js +19 -19
  27. package/src/models/campaigns.js +108 -108
  28. package/src/models/cashpayment.js +290 -290
  29. package/src/models/combinedUnits.js +62 -62
  30. package/src/models/combined_invoice.js +424 -424
  31. package/src/models/common_area_electricity.js +38 -38
  32. package/src/models/common_area_generator.js +41 -41
  33. package/src/models/common_area_utility_alert.js +37 -37
  34. package/src/models/common_area_water.js +39 -39
  35. package/src/models/communication_status.js +33 -33
  36. package/src/models/communication_user_opt.js +32 -32
  37. package/src/models/community_guidelines.js +35 -35
  38. package/src/models/company.js +53 -53
  39. package/src/models/coreBaseSettings.js +16 -16
  40. package/src/models/coreInvoiceSettings.js +100 -100
  41. package/src/models/counter_schema.js +21 -21
  42. package/src/models/country_tax.js +42 -42
  43. package/src/models/currency_settings.js +39 -39
  44. package/src/models/customer.js +214 -214
  45. package/src/models/customer_preference.js +52 -52
  46. package/src/models/customer_satisfaction_survey.js +297 -297
  47. package/src/models/customer_surveys.js +139 -139
  48. package/src/models/customer_tickets.js +237 -237
  49. package/src/models/dailyChecklist.js +312 -312
  50. package/src/models/default_payment_details.js +17 -17
  51. package/src/models/deliveryTimeMarks.js +18 -18
  52. package/src/models/document_type.js +19 -19
  53. package/src/models/dutyRosterChecklist.js +250 -250
  54. package/src/models/dutyroster.js +136 -136
  55. package/src/models/email.js +37 -37
  56. package/src/models/email_sms_queue.js +61 -61
  57. package/src/models/email_thread.js +35 -35
  58. package/src/models/entry_exit.js +53 -53
  59. package/src/models/expense.js +99 -99
  60. package/src/models/expense_category.js +45 -45
  61. package/src/models/facility.js +76 -76
  62. package/src/models/facilityBillingPrices.js +29 -29
  63. package/src/models/facilityInvoice.js +240 -240
  64. package/src/models/facilityInvoicePayment.js +52 -52
  65. package/src/models/facilityInvoiceRecipient.js +32 -32
  66. package/src/models/facilityWalletTransactionsMetadata.js +236 -236
  67. package/src/models/facility_departements.js +20 -20
  68. package/src/models/facility_payment_details.js +20 -20
  69. package/src/models/facility_rating.js +78 -78
  70. package/src/models/facilityasset.js +25 -25
  71. package/src/models/faq.js +14 -14
  72. package/src/models/gl_account_double_entries.js +25 -25
  73. package/src/models/gl_accounts.js +56 -56
  74. package/src/models/gl_entries.js +49 -49
  75. package/src/models/goodsReceivedNotes.js +115 -115
  76. package/src/models/guard.js +47 -47
  77. package/src/models/handover.js +258 -258
  78. package/src/models/inspection_category.js +38 -38
  79. package/src/models/invoice.js +480 -484
  80. package/src/models/invoice_generation_approval.js +86 -86
  81. package/src/models/invoicing_schedule.js +40 -40
  82. package/src/models/item_inspection.js +96 -96
  83. package/src/models/knowledge_base.js +109 -109
  84. package/src/models/knowledge_base_rating.js +44 -44
  85. package/src/models/leaseagreement.js +243 -243
  86. package/src/models/leasetemplate.js +17 -17
  87. package/src/models/levy.js +212 -223
  88. package/src/models/levy_invoice_settings.js +26 -26
  89. package/src/models/levycontract.js +216 -216
  90. package/src/models/levytype.js +23 -23
  91. package/src/models/maintenance_service_vendor.js +38 -38
  92. package/src/models/maintenance_services.js +17 -17
  93. package/src/models/maintenancerequisition.js +31 -31
  94. package/src/models/master_workplan.js +32 -32
  95. package/src/models/master_workplan_child.js +34 -34
  96. package/src/models/message.js +38 -38
  97. package/src/models/module.js +21 -21
  98. package/src/models/movein_application.js +29 -29
  99. package/src/models/movein_landlord.js +18 -18
  100. package/src/models/movein_user.js +15 -15
  101. package/src/models/notification.js +44 -44
  102. package/src/models/paymentTermsMarks.js +19 -19
  103. package/src/models/penalty.js +76 -76
  104. package/src/models/pendingCredentials.js +32 -32
  105. package/src/models/powerMeterCommunicationProtocol.js +17 -17
  106. package/src/models/powerMeterCustomerAccount.js +78 -78
  107. package/src/models/powerMeterCustomerBand.js +14 -14
  108. package/src/models/powerMeterDailyReading.js +30 -30
  109. package/src/models/powerMeterGateways.js +40 -40
  110. package/src/models/powerMeterMonthlyReading.js +34 -34
  111. package/src/models/powerMeterPowerCharges.js +85 -85
  112. package/src/models/powerMeterSettings.js +159 -159
  113. package/src/models/powerMeterSingleDayReading.js +32 -32
  114. package/src/models/powerMeters.js +116 -116
  115. package/src/models/powerMetersManufacturer.js +14 -14
  116. package/src/models/power_meter_account.js +81 -81
  117. package/src/models/power_meter_command_logs.js +30 -30
  118. package/src/models/power_meter_command_queue.js +33 -33
  119. package/src/models/power_meter_negative_balance.js +44 -44
  120. package/src/models/power_prepaid_credits.js +47 -47
  121. package/src/models/power_prepaid_debits.js +53 -53
  122. package/src/models/power_prepaid_orders.js +78 -78
  123. package/src/models/power_sms_notification.js +26 -26
  124. package/src/models/privacy_policy.js +19 -19
  125. package/src/models/propertyManagerContract.js +556 -556
  126. package/src/models/propertyManagerRevenue.js +195 -195
  127. package/src/models/purchaseOrderInvoice.js +74 -74
  128. package/src/models/purchase_order.js +213 -213
  129. package/src/models/purchase_request.js +110 -110
  130. package/src/models/quickbooks_config.js +52 -52
  131. package/src/models/refresh_token.js +23 -23
  132. package/src/models/reminder.js +197 -197
  133. package/src/models/report.js +13 -13
  134. package/src/models/resident.js +121 -121
  135. package/src/models/rfq_details.js +131 -131
  136. package/src/models/rfq_response.js +153 -153
  137. package/src/models/service_charge_invoice_upload.js +42 -42
  138. package/src/models/service_charge_payments.js +27 -27
  139. package/src/models/servicerequest.js +55 -55
  140. package/src/models/settings.js +62 -62
  141. package/src/models/short_urls.js +21 -21
  142. package/src/models/smart_meter_daily_consumption.js +44 -44
  143. package/src/models/sms_africastalking.js +20 -20
  144. package/src/models/sms_balance_notification.js +26 -26
  145. package/src/models/sms_meliora.js +20 -20
  146. package/src/models/staff.js +36 -36
  147. package/src/models/stocksandspare.js +161 -161
  148. package/src/models/suppliers.js +74 -74
  149. package/src/models/terms_and_conditions.js +19 -19
  150. package/src/models/tickets.js +186 -186
  151. package/src/models/tickets_category.js +72 -72
  152. package/src/models/unitManagementTemplate.js +44 -44
  153. package/src/models/unitasset.js +25 -25
  154. package/src/models/units.js +130 -130
  155. package/src/models/user.js +186 -186
  156. package/src/models/valueaddedservices.js +21 -21
  157. package/src/models/vas_invoices_upload.js +50 -50
  158. package/src/models/vas_payments.js +24 -24
  159. package/src/models/vasinvoice.js +192 -192
  160. package/src/models/vasvendor.js +52 -52
  161. package/src/models/visitLog.js +95 -95
  162. package/src/models/visitor.js +67 -67
  163. package/src/models/waitlist.js +45 -45
  164. package/src/models/wallet.js +44 -44
  165. package/src/models/wallet_transactions.js +50 -50
  166. package/src/models/water_invoice.js +351 -351
  167. package/src/models/water_meter_Command_Queue.js +33 -33
  168. package/src/models/water_meter_account.js +86 -86
  169. package/src/models/water_meter_billing.js +58 -58
  170. package/src/models/water_meter_combined_accounts.js +92 -92
  171. package/src/models/water_meter_communication.js +17 -17
  172. package/src/models/water_meter_communication_logs.js +39 -39
  173. package/src/models/water_meter_concentrator.js +70 -70
  174. package/src/models/water_meter_daily_history.js +32 -32
  175. package/src/models/water_meter_high_risk.js +36 -36
  176. package/src/models/water_meter_iot_cards.js +34 -34
  177. package/src/models/water_meter_loan_deduction.js +134 -134
  178. package/src/models/water_meter_manufacturer.js +35 -35
  179. package/src/models/water_meter_monthly_history.js +36 -36
  180. package/src/models/water_meter_negative_amounts.js +44 -44
  181. package/src/models/water_meter_settings.js +290 -290
  182. package/src/models/water_meter_single_day_history.js +34 -34
  183. package/src/models/water_meter_size.js +15 -15
  184. package/src/models/water_meters.js +176 -176
  185. package/src/models/water_meters_delivery.js +76 -76
  186. package/src/models/water_prepaid_credit.js +47 -47
  187. package/src/models/water_prepaid_debit.js +50 -50
  188. package/src/models/whatsapp_conversation.js +23 -23
  189. package/src/models/workorder.js +49 -49
  190. package/src/models/zohoAccount.js +453 -453
  191. package/src/models/zohoIntegration.js +262 -262
  192. package/src/models/zohoItem.js +504 -504
  193. package/src/models/power_invoice.js +0 -471
@@ -1,484 +1,480 @@
1
- const mongoose = require("mongoose");
2
-
3
- const invoiceSchema = new mongoose.Schema(
4
- {
5
- invoiceNumber: {
6
- type: String,
7
- required: true,
8
- unique: true,
9
- },
10
- accountNumber: {
11
- type: String,
12
- required: true,
13
- },
14
- client: {
15
- clientId: {
16
- type: mongoose.Schema.Types.ObjectId,
17
- ref: "Customer",
18
- required: true,
19
- },
20
- firstName: {
21
- type: String,
22
- required: true,
23
- },
24
- lastName: {
25
- type: String,
26
- required: true,
27
- },
28
- kraPin: {
29
- type: String,
30
- default: null,
31
- },
32
- },
33
- facility: {
34
- id: {
35
- type: mongoose.Schema.Types.ObjectId,
36
- ref: "Facility",
37
- required: true,
38
- },
39
- name: {
40
- type: String,
41
- required: true,
42
- },
43
- },
44
- unit: {
45
- id: { type: mongoose.Schema.Types.ObjectId, ref: "Unit", required: true },
46
- name: { type: String, required: true },
47
- },
48
- currency: {
49
- id: {
50
- type: mongoose.Schema.Types.ObjectId,
51
- ref: "Currency",
52
- required: true,
53
- },
54
- name: {
55
- type: String,
56
- required: true,
57
- },
58
- code: {
59
- type: String,
60
- required: true,
61
- uppercase: true,
62
- minlength: 3,
63
- maxlength: 3,
64
- },
65
- },
66
- items: [
67
- {
68
- description: { type: String, required: true },
69
- quantity: { type: Number, required: true, min: 1 },
70
- unitPrice: { type: Number, required: true, min: 0 },
71
- },
72
- ],
73
- subTotal: {
74
- type: Number,
75
- required: true,
76
- },
77
- tax: {
78
- type: Number,
79
- required: true,
80
- },
81
- totalAmount: {
82
- type: Number,
83
- required: true,
84
- },
85
- amountPaid: {
86
- type: Number,
87
- default: 0,
88
- min: 0,
89
- },
90
- // Mark as deprecated, keep for backward compatibility
91
- overpay: {
92
- type: Number,
93
- default: 0,
94
- min: 0,
95
- deprecated: true,
96
- },
97
- issueDate: {
98
- type: Date,
99
- required: true,
100
- },
101
- dueDate: {
102
- type: Date,
103
- required: true,
104
- },
105
- status: {
106
- type: String,
107
- required: true,
108
- enum: [
109
- "Unpaid",
110
- "Pending",
111
- "Paid",
112
- "Overdue",
113
- "Cancelled",
114
- "Partially Paid",
115
- "Void",
116
- ],
117
- },
118
- penalty: {
119
- type: Number,
120
- default: 0,
121
- },
122
- whatFor: {
123
- invoiceType: { type: String, required: true },
124
- description: { type: String },
125
- },
126
- invoiceNote: {
127
- type: String,
128
- default: null,
129
- },
130
- balanceBroughtForward: {
131
- type: Number,
132
- default: 0,
133
- },
134
- // NEW FIELD 1: Year-Month for easy filtering
135
- yearMonth: {
136
- type: String,
137
- match: /^\d{4}-\d{2}$/, // Validates format like "2025-08"
138
- index: true,
139
- },
140
- // NEW FIELD 2: Simple notification tracking
141
- notificationsSent: {
142
- sms: {
143
- type: Boolean,
144
- default: false,
145
- },
146
- email: {
147
- type: Boolean,
148
- default: false,
149
- },
150
- attempts: {
151
- type: Number,
152
- default: 0,
153
- },
154
- },
155
- voidMetadata: {
156
- voidedBy: {
157
- userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
158
- name: { type: String },
159
- role: { type: String },
160
- },
161
- voidedAt: { type: Date },
162
- reason: { type: String },
163
- },
164
- cancelMetadata: {
165
- cancelledBy: {
166
- userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
167
- name: { type: String },
168
- role: { type: String },
169
- },
170
- cancelledAt: { type: Date },
171
- reason: { type: String },
172
- },
173
- lastReminderSent: Date,
174
- reminderHistory: [
175
- {
176
- sentAt: Date,
177
- reminderId: mongoose.Schema.Types.ObjectId,
178
- notificationTypes: [String],
179
- },
180
- ],
181
- reconciliationHistory: [
182
- {
183
- date: { type: Date, required: true },
184
- amount: { type: Number, required: true },
185
- type: {
186
- type: String,
187
- enum: [
188
- "payment",
189
- "cash",
190
- "cheque",
191
- "bank-transfer",
192
- "mpesa-transfer",
193
- "overpay-transfer",
194
- "balance-deduction",
195
- "overpay-received",
196
- "credit-forward",
197
- "debit-forward",
198
- ],
199
- required: true,
200
- },
201
- sourceInvoice: String,
202
- destinationInvoice: String,
203
- paymentReference: String,
204
- paymentCompletion: String,
205
- remainingBalance: Number,
206
- notes: String,
207
- exchangeRate: {
208
- type: Number,
209
- default: 1, // For cross-currency reconciliations
210
- },
211
- originalCurrency: {
212
- code: String, // Original currency code if different from invoice currency
213
- amount: Number, // Amount in original currency
214
- },
215
- },
216
- ],
217
- paymentDetails: {
218
- paymentStatus: { type: String, required: true },
219
- paymentMethod: { type: String },
220
- paymentDate: { type: Date },
221
- transactionId: { type: String },
222
- },
223
- // New field to track when an invoice has been viewed
224
- viewStatus: {
225
- isOpened: { type: Boolean, default: false },
226
- openedAt: { type: Date, default: null },
227
- openedBy: {
228
- facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
229
- userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
230
- userRole: { type: String },
231
- },
232
- viewHistory: [
233
- {
234
- viewedAt: { type: Date, required: true },
235
- facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
236
- userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
237
- },
238
- ],
239
- },
240
- invoiceUrl: {
241
- type: String,
242
- default: null,
243
- },
244
- // eTims tax integration fields
245
- ticketUrl: {
246
- type: String,
247
- default: null,
248
- description: "QR code URL from eTims/KRA for tax invoice verification",
249
- },
250
- txsync: {
251
- status: {
252
- type: String,
253
- enum: ["pending", "synced", "failed", "not-applicable"],
254
- default: "not-applicable",
255
- },
256
- syncedAt: {
257
- type: Date,
258
- default: null,
259
- },
260
- etimsInvoiceNo: {
261
- type: String,
262
- default: null,
263
- },
264
- sdcId: {
265
- type: String,
266
- default: null,
267
- },
268
- receiptNo: {
269
- type: String,
270
- default: null,
271
- },
272
- errorMessage: {
273
- type: String,
274
- default: null,
275
- },
276
- attempts: {
277
- type: Number,
278
- default: 0,
279
- },
280
- lastAttemptAt: {
281
- type: Date,
282
- default: null,
283
- },
284
- },
285
- // New fields for double entry accounts
286
- invoiceDoubleEntryAccount: {
287
- type: mongoose.Schema.Types.ObjectId,
288
- ref: "GLAccountDoubleEntries",
289
- required: false,
290
- },
291
- paymentDoubleEntryAccount: {
292
- type: mongoose.Schema.Types.ObjectId,
293
- ref: "GLAccountDoubleEntries",
294
- required: false,
295
- },
296
- // GL account details for invoice entries
297
- accountdebitedData: {
298
- amount: { type: Number },
299
- description: { type: String },
300
- isActive: { type: Boolean, default: true },
301
- },
302
- accountcreditedData: {
303
- amount: { type: Number },
304
- description: { type: String },
305
- isActive: { type: Boolean, default: true },
306
- },
307
- },
308
- {
309
- timestamps: true,
310
- },
311
- );
312
-
313
- // Add indexes for frequently queried fields
314
- invoiceSchema.index({ accountNumber: 1 });
315
- invoiceSchema.index({ status: 1 });
316
- invoiceSchema.index({ "client.clientId": 1, status: 1 });
317
- invoiceSchema.index({ "reconciliationHistory.paymentReference": 1 });
318
- invoiceSchema.index({ issueDate: -1 });
319
- invoiceSchema.index({ "currency.code": 1 }); // Add index for currency code
320
- invoiceSchema.index({ "currency.id": 1 }); // Add index for currency ID
321
- invoiceSchema.index({ "currency.code": 1, "client.clientId": 1, status: 1 }); // Compound index for currency-based queries
322
- invoiceSchema.index({ "client.clientId": 1, balanceBroughtForward: 1 }); // Add index for finding invoices with credit balances
323
- invoiceSchema.index({ "viewStatus.isOpened": 1 }); // Add index for view status
324
- invoiceSchema.index({ "viewStatus.openedBy.facilityId": 1 }); // Add index for facility view tracking
325
- // Add index for double entry accounts
326
- invoiceSchema.index({ invoiceDoubleEntryAccount: 1 });
327
- invoiceSchema.index({ paymentDoubleEntryAccount: 1 });
328
- // NEW INDEXES for new fields
329
- invoiceSchema.index({ yearMonth: 1 }); // For filtering by year-month
330
- invoiceSchema.index({ yearMonth: 1, status: 1 }); // Combined index for monthly reports
331
- invoiceSchema.index({ "notificationsSent.sms": 1 }); // For finding SMS sent/not sent
332
- invoiceSchema.index({ "notificationsSent.email": 1 }); // For finding email sent/not sent
333
- // eTims tax sync indexes
334
- invoiceSchema.index({ "txsync.status": 1 }); // For finding invoices by sync status
335
- invoiceSchema.index({ "txsync.etimsInvoiceNo": 1 }); // For looking up by eTims invoice number
336
-
337
- // Add virtual field for calculating balance
338
- invoiceSchema.virtual("calculatedBalance").get(function () {
339
- const baseBalance = this.totalAmount - (this.amountPaid || 0);
340
-
341
- // Add positive balanceBroughtForward (customer owes money)
342
- if (this.balanceBroughtForward > 0) {
343
- return baseBalance + this.balanceBroughtForward;
344
- }
345
-
346
- // Subtract negative balanceBroughtForward (credit)
347
- return baseBalance;
348
- });
349
-
350
- // Add virtual field for credit balance
351
- invoiceSchema.virtual("creditBalance").get(function () {
352
- return this.balanceBroughtForward < 0
353
- ? Math.abs(this.balanceBroughtForward)
354
- : 0;
355
- });
356
-
357
- // Getter for compatible overpay field
358
- invoiceSchema.virtual("effectiveOverpay").get(function () {
359
- return this.balanceBroughtForward < 0
360
- ? Math.abs(this.balanceBroughtForward)
361
- : 0;
362
- });
363
-
364
- // Add virtual populate for invoice double entry account
365
- invoiceSchema.virtual("invoiceDoubleEntry", {
366
- ref: "GLAccountDoubleEntries",
367
- localField: "invoiceDoubleEntryAccount",
368
- foreignField: "_id",
369
- justOne: true,
370
- });
371
-
372
- // Add virtual populate for payment double entry account
373
- invoiceSchema.virtual("paymentDoubleEntry", {
374
- ref: "GLAccountDoubleEntries",
375
- localField: "paymentDoubleEntryAccount",
376
- foreignField: "_id",
377
- justOne: true,
378
- });
379
-
380
- // Add method for currency conversion if needed
381
- invoiceSchema.methods.convertAmount = function (
382
- amount,
383
- fromCurrency,
384
- toCurrency,
385
- exchangeRate,
386
- ) {
387
- if (fromCurrency === toCurrency) {
388
- return amount;
389
- }
390
- return amount * exchangeRate;
391
- };
392
-
393
- // Add static method to find invoices by currency
394
- invoiceSchema.statics.findByCurrency = function (currencyCode) {
395
- return this.find({ "currency.code": currencyCode.toUpperCase() });
396
- };
397
-
398
- // Add static method to find invoices with credit balance
399
- invoiceSchema.statics.findWithCreditBalance = function (clientId) {
400
- return this.find({
401
- "client.clientId": clientId,
402
- balanceBroughtForward: { $lt: 0 },
403
- }).sort({ updatedAt: -1 });
404
- };
405
-
406
- // Add static method to calculate totals by currency
407
- invoiceSchema.statics.calculateTotalsByCurrency = function (query = {}) {
408
- return this.aggregate([
409
- { $match: query },
410
- {
411
- $group: {
412
- _id: "$currency.code",
413
- totalAmount: { $sum: "$totalAmount" },
414
- totalPaid: { $sum: "$amountPaid" },
415
- count: { $sum: 1 },
416
- },
417
- },
418
- ]);
419
- };
420
-
421
- // New static method to find all unviewed invoices
422
- invoiceSchema.statics.findUnviewedInvoices = function (facilityId) {
423
- return this.find({
424
- "facility.id": facilityId,
425
- "viewStatus.isOpened": false,
426
- });
427
- };
428
-
429
- // NEW STATIC METHODS for new fields
430
- // Find invoices by year-month
431
- invoiceSchema.statics.findByYearMonth = function (yearMonth) {
432
- return this.find({ yearMonth: yearMonth });
433
- };
434
-
435
- // Find invoices where notifications haven't been sent
436
- invoiceSchema.statics.findPendingNotifications = function (facilityId) {
437
- return this.find({
438
- "facility.id": facilityId,
439
- status: { $in: ["Unpaid", "Overdue"] },
440
- $or: [
441
- { "notificationsSent.sms": false },
442
- { "notificationsSent.email": false },
443
- ],
444
- });
445
- };
446
-
447
- // Find invoices pending eTims sync
448
- invoiceSchema.statics.findPendingTaxSync = function (facilityId) {
449
- return this.find({
450
- "facility.id": facilityId,
451
- "txsync.status": "pending",
452
- });
453
- };
454
-
455
- // Find invoices with failed eTims sync
456
- invoiceSchema.statics.findFailedTaxSync = function (facilityId) {
457
- return this.find({
458
- "facility.id": facilityId,
459
- "txsync.status": "failed",
460
- });
461
- };
462
-
463
- // Pre-save middleware to ensure overpay and balanceBroughtForward stay in sync during transition
464
- invoiceSchema.pre("save", function (next) {
465
- // If balanceBroughtForward is negative (credit), sync with overpay for backwards compatibility
466
- if (this.balanceBroughtForward < 0) {
467
- this.overpay = Math.abs(this.balanceBroughtForward);
468
- } else {
469
- this.overpay = 0; // No overpay if there's no negative balance
470
- }
471
-
472
- // NEW: Auto-generate yearMonth from issueDate if not provided
473
- if (!this.yearMonth && this.issueDate) {
474
- const year = this.issueDate.getFullYear();
475
- const month = String(this.issueDate.getMonth() + 1).padStart(2, "0");
476
- this.yearMonth = `${year}-${month}`;
477
- }
478
-
479
- next();
480
- });
481
-
482
- const Invoice = mongoose.model("Invoice", invoiceSchema);
483
-
484
- module.exports = Invoice;
1
+ const mongoose = require("mongoose");
2
+
3
+ const invoiceSchema = new mongoose.Schema(
4
+ {
5
+ invoiceNumber: {
6
+ type: String,
7
+ required: true,
8
+ unique: true,
9
+ },
10
+ accountNumber: {
11
+ type: String,
12
+ required: true,
13
+ },
14
+ client: {
15
+ clientId: {
16
+ type: mongoose.Schema.Types.ObjectId,
17
+ ref: "Customer",
18
+ required: true,
19
+ },
20
+ firstName: {
21
+ type: String,
22
+ required: true,
23
+ },
24
+ lastName: {
25
+ type: String,
26
+ required: true,
27
+ },
28
+ },
29
+ facility: {
30
+ id: {
31
+ type: mongoose.Schema.Types.ObjectId,
32
+ ref: "Facility",
33
+ required: true,
34
+ },
35
+ name: {
36
+ type: String,
37
+ required: true,
38
+ },
39
+ },
40
+ unit: {
41
+ id: { type: mongoose.Schema.Types.ObjectId, ref: "Unit", required: true },
42
+ name: { type: String, required: true },
43
+ },
44
+ currency: {
45
+ id: {
46
+ type: mongoose.Schema.Types.ObjectId,
47
+ ref: "Currency",
48
+ required: true,
49
+ },
50
+ name: {
51
+ type: String,
52
+ required: true,
53
+ },
54
+ code: {
55
+ type: String,
56
+ required: true,
57
+ uppercase: true,
58
+ minlength: 3,
59
+ maxlength: 3,
60
+ },
61
+ },
62
+ items: [
63
+ {
64
+ description: { type: String, required: true },
65
+ quantity: { type: Number, required: true, min: 1 },
66
+ unitPrice: { type: Number, required: true, min: 0 },
67
+ },
68
+ ],
69
+ subTotal: {
70
+ type: Number,
71
+ required: true,
72
+ },
73
+ tax: {
74
+ type: Number,
75
+ required: true,
76
+ },
77
+ totalAmount: {
78
+ type: Number,
79
+ required: true,
80
+ },
81
+ amountPaid: {
82
+ type: Number,
83
+ default: 0,
84
+ min: 0,
85
+ },
86
+ // Mark as deprecated, keep for backward compatibility
87
+ overpay: {
88
+ type: Number,
89
+ default: 0,
90
+ min: 0,
91
+ deprecated: true,
92
+ },
93
+ issueDate: {
94
+ type: Date,
95
+ required: true,
96
+ },
97
+ dueDate: {
98
+ type: Date,
99
+ required: true,
100
+ },
101
+ status: {
102
+ type: String,
103
+ required: true,
104
+ enum: [
105
+ "Unpaid",
106
+ "Pending",
107
+ "Paid",
108
+ "Overdue",
109
+ "Cancelled",
110
+ "Partially Paid",
111
+ "Void",
112
+ ],
113
+ },
114
+ penalty: {
115
+ type: Number,
116
+ default: 0,
117
+ },
118
+ whatFor: {
119
+ invoiceType: { type: String, required: true },
120
+ description: { type: String },
121
+ },
122
+ invoiceNote: {
123
+ type: String,
124
+ default: null,
125
+ },
126
+ balanceBroughtForward: {
127
+ type: Number,
128
+ default: 0,
129
+ },
130
+ // NEW FIELD 1: Year-Month for easy filtering
131
+ yearMonth: {
132
+ type: String,
133
+ match: /^\d{4}-\d{2}$/, // Validates format like "2025-08"
134
+ index: true,
135
+ },
136
+ // NEW FIELD 2: Simple notification tracking
137
+ notificationsSent: {
138
+ sms: {
139
+ type: Boolean,
140
+ default: false,
141
+ },
142
+ email: {
143
+ type: Boolean,
144
+ default: false,
145
+ },
146
+ attempts: {
147
+ type: Number,
148
+ default: 0,
149
+ },
150
+ },
151
+ voidMetadata: {
152
+ voidedBy: {
153
+ userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
154
+ name: { type: String },
155
+ role: { type: String },
156
+ },
157
+ voidedAt: { type: Date },
158
+ reason: { type: String },
159
+ },
160
+ cancelMetadata: {
161
+ cancelledBy: {
162
+ userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
163
+ name: { type: String },
164
+ role: { type: String },
165
+ },
166
+ cancelledAt: { type: Date },
167
+ reason: { type: String },
168
+ },
169
+ lastReminderSent: Date,
170
+ reminderHistory: [
171
+ {
172
+ sentAt: Date,
173
+ reminderId: mongoose.Schema.Types.ObjectId,
174
+ notificationTypes: [String],
175
+ },
176
+ ],
177
+ reconciliationHistory: [
178
+ {
179
+ date: { type: Date, required: true },
180
+ amount: { type: Number, required: true },
181
+ type: {
182
+ type: String,
183
+ enum: [
184
+ "payment",
185
+ "cash",
186
+ "cheque",
187
+ "bank-transfer",
188
+ "mpesa-transfer",
189
+ "overpay-transfer",
190
+ "balance-deduction",
191
+ "overpay-received",
192
+ "credit-forward",
193
+ "debit-forward",
194
+ ],
195
+ required: true,
196
+ },
197
+ sourceInvoice: String,
198
+ destinationInvoice: String,
199
+ paymentReference: String,
200
+ paymentCompletion: String,
201
+ remainingBalance: Number,
202
+ notes: String,
203
+ exchangeRate: {
204
+ type: Number,
205
+ default: 1, // For cross-currency reconciliations
206
+ },
207
+ originalCurrency: {
208
+ code: String, // Original currency code if different from invoice currency
209
+ amount: Number, // Amount in original currency
210
+ },
211
+ },
212
+ ],
213
+ paymentDetails: {
214
+ paymentStatus: { type: String, required: true },
215
+ paymentMethod: { type: String },
216
+ paymentDate: { type: Date },
217
+ transactionId: { type: String },
218
+ },
219
+ // New field to track when an invoice has been viewed
220
+ viewStatus: {
221
+ isOpened: { type: Boolean, default: false },
222
+ openedAt: { type: Date, default: null },
223
+ openedBy: {
224
+ facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
225
+ userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
226
+ userRole: { type: String },
227
+ },
228
+ viewHistory: [
229
+ {
230
+ viewedAt: { type: Date, required: true },
231
+ facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
232
+ userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
233
+ },
234
+ ],
235
+ },
236
+ invoiceUrl: {
237
+ type: String,
238
+ default: null,
239
+ },
240
+ // eTims tax integration fields
241
+ ticketUrl: {
242
+ type: String,
243
+ default: null,
244
+ description: "QR code URL from eTims/KRA for tax invoice verification",
245
+ },
246
+ txsync: {
247
+ status: {
248
+ type: String,
249
+ enum: ["pending", "synced", "failed", "not-applicable"],
250
+ default: "not-applicable",
251
+ },
252
+ syncedAt: {
253
+ type: Date,
254
+ default: null,
255
+ },
256
+ etimsInvoiceNo: {
257
+ type: String,
258
+ default: null,
259
+ },
260
+ sdcId: {
261
+ type: String,
262
+ default: null,
263
+ },
264
+ receiptNo: {
265
+ type: String,
266
+ default: null,
267
+ },
268
+ errorMessage: {
269
+ type: String,
270
+ default: null,
271
+ },
272
+ attempts: {
273
+ type: Number,
274
+ default: 0,
275
+ },
276
+ lastAttemptAt: {
277
+ type: Date,
278
+ default: null,
279
+ },
280
+ },
281
+ // New fields for double entry accounts
282
+ invoiceDoubleEntryAccount: {
283
+ type: mongoose.Schema.Types.ObjectId,
284
+ ref: "GLAccountDoubleEntries",
285
+ required: false,
286
+ },
287
+ paymentDoubleEntryAccount: {
288
+ type: mongoose.Schema.Types.ObjectId,
289
+ ref: "GLAccountDoubleEntries",
290
+ required: false,
291
+ },
292
+ // GL account details for invoice entries
293
+ accountdebitedData: {
294
+ amount: { type: Number },
295
+ description: { type: String },
296
+ isActive: { type: Boolean, default: true },
297
+ },
298
+ accountcreditedData: {
299
+ amount: { type: Number },
300
+ description: { type: String },
301
+ isActive: { type: Boolean, default: true },
302
+ },
303
+ },
304
+ {
305
+ timestamps: true,
306
+ },
307
+ );
308
+
309
+ // Add indexes for frequently queried fields
310
+ invoiceSchema.index({ accountNumber: 1 });
311
+ invoiceSchema.index({ status: 1 });
312
+ invoiceSchema.index({ "client.clientId": 1, status: 1 });
313
+ invoiceSchema.index({ "reconciliationHistory.paymentReference": 1 });
314
+ invoiceSchema.index({ issueDate: -1 });
315
+ invoiceSchema.index({ "currency.code": 1 }); // Add index for currency code
316
+ invoiceSchema.index({ "currency.id": 1 }); // Add index for currency ID
317
+ invoiceSchema.index({ "currency.code": 1, "client.clientId": 1, status: 1 }); // Compound index for currency-based queries
318
+ invoiceSchema.index({ "client.clientId": 1, balanceBroughtForward: 1 }); // Add index for finding invoices with credit balances
319
+ invoiceSchema.index({ "viewStatus.isOpened": 1 }); // Add index for view status
320
+ invoiceSchema.index({ "viewStatus.openedBy.facilityId": 1 }); // Add index for facility view tracking
321
+ // Add index for double entry accounts
322
+ invoiceSchema.index({ invoiceDoubleEntryAccount: 1 });
323
+ invoiceSchema.index({ paymentDoubleEntryAccount: 1 });
324
+ // NEW INDEXES for new fields
325
+ invoiceSchema.index({ yearMonth: 1 }); // For filtering by year-month
326
+ invoiceSchema.index({ yearMonth: 1, status: 1 }); // Combined index for monthly reports
327
+ invoiceSchema.index({ "notificationsSent.sms": 1 }); // For finding SMS sent/not sent
328
+ invoiceSchema.index({ "notificationsSent.email": 1 }); // For finding email sent/not sent
329
+ // eTims tax sync indexes
330
+ invoiceSchema.index({ "txsync.status": 1 }); // For finding invoices by sync status
331
+ invoiceSchema.index({ "txsync.etimsInvoiceNo": 1 }); // For looking up by eTims invoice number
332
+
333
+ // Add virtual field for calculating balance
334
+ invoiceSchema.virtual("calculatedBalance").get(function () {
335
+ const baseBalance = this.totalAmount - (this.amountPaid || 0);
336
+
337
+ // Add positive balanceBroughtForward (customer owes money)
338
+ if (this.balanceBroughtForward > 0) {
339
+ return baseBalance + this.balanceBroughtForward;
340
+ }
341
+
342
+ // Subtract negative balanceBroughtForward (credit)
343
+ return baseBalance;
344
+ });
345
+
346
+ // Add virtual field for credit balance
347
+ invoiceSchema.virtual("creditBalance").get(function () {
348
+ return this.balanceBroughtForward < 0
349
+ ? Math.abs(this.balanceBroughtForward)
350
+ : 0;
351
+ });
352
+
353
+ // Getter for compatible overpay field
354
+ invoiceSchema.virtual("effectiveOverpay").get(function () {
355
+ return this.balanceBroughtForward < 0
356
+ ? Math.abs(this.balanceBroughtForward)
357
+ : 0;
358
+ });
359
+
360
+ // Add virtual populate for invoice double entry account
361
+ invoiceSchema.virtual("invoiceDoubleEntry", {
362
+ ref: "GLAccountDoubleEntries",
363
+ localField: "invoiceDoubleEntryAccount",
364
+ foreignField: "_id",
365
+ justOne: true,
366
+ });
367
+
368
+ // Add virtual populate for payment double entry account
369
+ invoiceSchema.virtual("paymentDoubleEntry", {
370
+ ref: "GLAccountDoubleEntries",
371
+ localField: "paymentDoubleEntryAccount",
372
+ foreignField: "_id",
373
+ justOne: true,
374
+ });
375
+
376
+ // Add method for currency conversion if needed
377
+ invoiceSchema.methods.convertAmount = function (
378
+ amount,
379
+ fromCurrency,
380
+ toCurrency,
381
+ exchangeRate,
382
+ ) {
383
+ if (fromCurrency === toCurrency) {
384
+ return amount;
385
+ }
386
+ return amount * exchangeRate;
387
+ };
388
+
389
+ // Add static method to find invoices by currency
390
+ invoiceSchema.statics.findByCurrency = function (currencyCode) {
391
+ return this.find({ "currency.code": currencyCode.toUpperCase() });
392
+ };
393
+
394
+ // Add static method to find invoices with credit balance
395
+ invoiceSchema.statics.findWithCreditBalance = function (clientId) {
396
+ return this.find({
397
+ "client.clientId": clientId,
398
+ balanceBroughtForward: { $lt: 0 },
399
+ }).sort({ updatedAt: -1 });
400
+ };
401
+
402
+ // Add static method to calculate totals by currency
403
+ invoiceSchema.statics.calculateTotalsByCurrency = function (query = {}) {
404
+ return this.aggregate([
405
+ { $match: query },
406
+ {
407
+ $group: {
408
+ _id: "$currency.code",
409
+ totalAmount: { $sum: "$totalAmount" },
410
+ totalPaid: { $sum: "$amountPaid" },
411
+ count: { $sum: 1 },
412
+ },
413
+ },
414
+ ]);
415
+ };
416
+
417
+ // New static method to find all unviewed invoices
418
+ invoiceSchema.statics.findUnviewedInvoices = function (facilityId) {
419
+ return this.find({
420
+ "facility.id": facilityId,
421
+ "viewStatus.isOpened": false,
422
+ });
423
+ };
424
+
425
+ // NEW STATIC METHODS for new fields
426
+ // Find invoices by year-month
427
+ invoiceSchema.statics.findByYearMonth = function (yearMonth) {
428
+ return this.find({ yearMonth: yearMonth });
429
+ };
430
+
431
+ // Find invoices where notifications haven't been sent
432
+ invoiceSchema.statics.findPendingNotifications = function (facilityId) {
433
+ return this.find({
434
+ "facility.id": facilityId,
435
+ status: { $in: ["Unpaid", "Overdue"] },
436
+ $or: [
437
+ { "notificationsSent.sms": false },
438
+ { "notificationsSent.email": false },
439
+ ],
440
+ });
441
+ };
442
+
443
+ // Find invoices pending eTims sync
444
+ invoiceSchema.statics.findPendingTaxSync = function (facilityId) {
445
+ return this.find({
446
+ "facility.id": facilityId,
447
+ "txsync.status": "pending",
448
+ });
449
+ };
450
+
451
+ // Find invoices with failed eTims sync
452
+ invoiceSchema.statics.findFailedTaxSync = function (facilityId) {
453
+ return this.find({
454
+ "facility.id": facilityId,
455
+ "txsync.status": "failed",
456
+ });
457
+ };
458
+
459
+ // Pre-save middleware to ensure overpay and balanceBroughtForward stay in sync during transition
460
+ invoiceSchema.pre("save", function (next) {
461
+ // If balanceBroughtForward is negative (credit), sync with overpay for backwards compatibility
462
+ if (this.balanceBroughtForward < 0) {
463
+ this.overpay = Math.abs(this.balanceBroughtForward);
464
+ } else {
465
+ this.overpay = 0; // No overpay if there's no negative balance
466
+ }
467
+
468
+ // NEW: Auto-generate yearMonth from issueDate if not provided
469
+ if (!this.yearMonth && this.issueDate) {
470
+ const year = this.issueDate.getFullYear();
471
+ const month = String(this.issueDate.getMonth() + 1).padStart(2, "0");
472
+ this.yearMonth = `${year}-${month}`;
473
+ }
474
+
475
+ next();
476
+ });
477
+
478
+ const Invoice = mongoose.model("Invoice", invoiceSchema);
479
+
480
+ module.exports = Invoice;