payservedb 9.1.1 → 9.1.2

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