payservedb 9.8.4 → 9.8.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.
- package/package.json +1 -1
- package/src/models/invoice.js +71 -0
- package/src/models/levy_deposits.js +32 -1
package/package.json
CHANGED
package/src/models/invoice.js
CHANGED
|
@@ -390,6 +390,58 @@ const invoiceSchema = new mongoose.Schema(
|
|
|
390
390
|
|
|
391
391
|
}
|
|
392
392
|
},
|
|
393
|
+
// Zoho Books sync tracking
|
|
394
|
+
zohoSync: {
|
|
395
|
+
synced: {
|
|
396
|
+
type: Boolean,
|
|
397
|
+
default: false,
|
|
398
|
+
},
|
|
399
|
+
status: {
|
|
400
|
+
type: String,
|
|
401
|
+
enum: ["pending", "synced", "failed", "not-applicable"],
|
|
402
|
+
default: "not-applicable",
|
|
403
|
+
},
|
|
404
|
+
zohoInvoiceId: {
|
|
405
|
+
type: String,
|
|
406
|
+
default: null,
|
|
407
|
+
},
|
|
408
|
+
zohoInvoiceNumber: {
|
|
409
|
+
type: String,
|
|
410
|
+
default: null,
|
|
411
|
+
},
|
|
412
|
+
zohoCustomerId: {
|
|
413
|
+
type: String,
|
|
414
|
+
default: null,
|
|
415
|
+
},
|
|
416
|
+
zohoCustomerName: {
|
|
417
|
+
type: String,
|
|
418
|
+
default: null,
|
|
419
|
+
},
|
|
420
|
+
paymentId: {
|
|
421
|
+
type: String,
|
|
422
|
+
default: null,
|
|
423
|
+
},
|
|
424
|
+
paymentReference: {
|
|
425
|
+
type: String,
|
|
426
|
+
default: null,
|
|
427
|
+
},
|
|
428
|
+
syncedAt: {
|
|
429
|
+
type: Date,
|
|
430
|
+
default: null,
|
|
431
|
+
},
|
|
432
|
+
lastAttemptAt: {
|
|
433
|
+
type: Date,
|
|
434
|
+
default: null,
|
|
435
|
+
},
|
|
436
|
+
errorMessage: {
|
|
437
|
+
type: String,
|
|
438
|
+
default: null,
|
|
439
|
+
},
|
|
440
|
+
attempts: {
|
|
441
|
+
type: Number,
|
|
442
|
+
default: 0,
|
|
443
|
+
},
|
|
444
|
+
},
|
|
393
445
|
// New fields for double entry accounts
|
|
394
446
|
invoiceDoubleEntryAccount: {
|
|
395
447
|
type: mongoose.Schema.Types.ObjectId,
|
|
@@ -441,6 +493,9 @@ invoiceSchema.index({ "notificationsSent.email": 1 }); // For finding email sent
|
|
|
441
493
|
// eTims tax sync indexes
|
|
442
494
|
invoiceSchema.index({ "txsync.status": 1 }); // For finding invoices by sync status
|
|
443
495
|
invoiceSchema.index({ "txsync.etimsInvoiceNo": 1 }); // For looking up by eTims invoice number
|
|
496
|
+
// Zoho Books sync indexes
|
|
497
|
+
invoiceSchema.index({ "zohoSync.status": 1 }); // For finding invoices by Zoho sync status
|
|
498
|
+
invoiceSchema.index({ "zohoSync.zohoInvoiceId": 1 }); // For looking up by Zoho invoice ID
|
|
444
499
|
|
|
445
500
|
// Add virtual field for calculating balance
|
|
446
501
|
invoiceSchema.virtual("calculatedBalance").get(function () {
|
|
@@ -568,6 +623,22 @@ invoiceSchema.statics.findFailedTaxSync = function (facilityId) {
|
|
|
568
623
|
});
|
|
569
624
|
};
|
|
570
625
|
|
|
626
|
+
// Find invoices pending Zoho sync
|
|
627
|
+
invoiceSchema.statics.findPendingZohoSync = function (facilityId) {
|
|
628
|
+
return this.find({
|
|
629
|
+
"facility.id": facilityId,
|
|
630
|
+
"zohoSync.status": "pending",
|
|
631
|
+
});
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// Find invoices with failed Zoho sync
|
|
635
|
+
invoiceSchema.statics.findFailedZohoSync = function (facilityId) {
|
|
636
|
+
return this.find({
|
|
637
|
+
"facility.id": facilityId,
|
|
638
|
+
"zohoSync.status": "failed",
|
|
639
|
+
});
|
|
640
|
+
};
|
|
641
|
+
|
|
571
642
|
// Pre-save middleware to ensure overpay and balanceBroughtForward stay in sync during transition
|
|
572
643
|
invoiceSchema.pre("save", function (next) {
|
|
573
644
|
|
|
@@ -27,6 +27,25 @@ const DepositSchema = new Schema(
|
|
|
27
27
|
type: Number,
|
|
28
28
|
min: [0, 'Amount cannot be negative']
|
|
29
29
|
},
|
|
30
|
+
|
|
31
|
+
amountPaid: {
|
|
32
|
+
type: Number,
|
|
33
|
+
default: 0,
|
|
34
|
+
min: [0, 'Amount paid cannot be negative']
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
payments: [
|
|
38
|
+
{
|
|
39
|
+
amount: { type: Number, required: true },
|
|
40
|
+
appliedAmount: { type: Number, required: true },
|
|
41
|
+
overpaymentAmount: { type: Number, default: 0 },
|
|
42
|
+
datePaid: { type: Date, required: true },
|
|
43
|
+
paymentReference: { type: String, trim: true },
|
|
44
|
+
paymentMethod: { type: String, trim: true },
|
|
45
|
+
recordedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
|
46
|
+
recordedAt: { type: Date, default: Date.now }
|
|
47
|
+
}
|
|
48
|
+
],
|
|
30
49
|
datePaid: {
|
|
31
50
|
type: Date
|
|
32
51
|
},
|
|
@@ -47,7 +66,8 @@ const DepositSchema = new Schema(
|
|
|
47
66
|
}],
|
|
48
67
|
status: {
|
|
49
68
|
type: String,
|
|
50
|
-
|
|
69
|
+
|
|
70
|
+
enum: ['Pending', 'Partially Paid', 'Held', 'Refunded', 'Partially Refunded', 'Forfeited'],
|
|
51
71
|
default: 'Pending',
|
|
52
72
|
required: [true, 'Status is required']
|
|
53
73
|
},
|
|
@@ -92,6 +112,10 @@ DepositSchema.virtual('balanceRemaining').get(function () {
|
|
|
92
112
|
return this.amount - (this.refundedAmount || 0);
|
|
93
113
|
});
|
|
94
114
|
|
|
115
|
+
DepositSchema.virtual('amountDue').get(function () {
|
|
116
|
+
return Math.max(this.amount - (this.amountPaid || 0), 0);
|
|
117
|
+
});
|
|
118
|
+
|
|
95
119
|
DepositSchema.pre('save', function (next) {
|
|
96
120
|
if (
|
|
97
121
|
(this.status === 'Refunded' || this.status === 'Partially Refunded') &&
|
|
@@ -109,6 +133,13 @@ DepositSchema.pre('save', function (next) {
|
|
|
109
133
|
next();
|
|
110
134
|
});
|
|
111
135
|
|
|
136
|
+
DepositSchema.pre('save', function (next) {
|
|
137
|
+
if ((this.amountPaid || 0) > this.amount) {
|
|
138
|
+
return next(new Error('Amount paid cannot exceed deposit amount'));
|
|
139
|
+
}
|
|
140
|
+
next();
|
|
141
|
+
});
|
|
142
|
+
|
|
112
143
|
module.exports = {
|
|
113
144
|
schema: DepositSchema,
|
|
114
145
|
name: 'Deposit'
|