payservedb 9.1.2 → 9.1.4
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/index.js +1 -0
- package/package.json +1 -1
- package/src/models/levy_deposits.js +127 -0
package/index.js
CHANGED
|
@@ -94,6 +94,7 @@ const models = {
|
|
|
94
94
|
Levy: require("./src/models/levy"),
|
|
95
95
|
LevyType: require("./src/models/levytype"),
|
|
96
96
|
LevyContract: require("./src/models/levycontract"),
|
|
97
|
+
DepositSchema: require("./src/models/levy_deposits"),
|
|
97
98
|
Invoice: require("./src/models/invoice"),
|
|
98
99
|
InvoiceEditLog: require("./src/models/invoice_edit_log"),
|
|
99
100
|
CombinedInvoice: require("./src/models/combined_invoice"),
|
package/package.json
CHANGED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
const Schema = mongoose.Schema;
|
|
3
|
+
|
|
4
|
+
const DepositSchema = new Schema(
|
|
5
|
+
{
|
|
6
|
+
contractId: {
|
|
7
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
8
|
+
ref: 'LevyContract',
|
|
9
|
+
required: [true, 'Contract ID is required']
|
|
10
|
+
},
|
|
11
|
+
customerId: {
|
|
12
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
13
|
+
ref: 'Customer',
|
|
14
|
+
required: [true, 'Customer ID is required']
|
|
15
|
+
},
|
|
16
|
+
unitId: {
|
|
17
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
18
|
+
ref: 'Unit',
|
|
19
|
+
required: [true, 'Unit ID is required']
|
|
20
|
+
},
|
|
21
|
+
facilityId: {
|
|
22
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
23
|
+
ref: 'Facility',
|
|
24
|
+
required: [true, 'Facility ID is required']
|
|
25
|
+
},
|
|
26
|
+
amount: {
|
|
27
|
+
type: Number,
|
|
28
|
+
required: [true, 'Amount is required'],
|
|
29
|
+
min: [0, 'Amount cannot be negative']
|
|
30
|
+
},
|
|
31
|
+
datePaid: {
|
|
32
|
+
type: Date,
|
|
33
|
+
required: [true, 'Date paid is required']
|
|
34
|
+
},
|
|
35
|
+
// NEW FIELDS FOR DEPOSIT/UPFRONT PAYMENT
|
|
36
|
+
receiptNumber: {
|
|
37
|
+
type: String,
|
|
38
|
+
trim: true
|
|
39
|
+
},
|
|
40
|
+
paymentReference: {
|
|
41
|
+
type: String,
|
|
42
|
+
trim: true
|
|
43
|
+
},
|
|
44
|
+
// For upfront payment tracking
|
|
45
|
+
upfrontPaymentId: {
|
|
46
|
+
type: String,
|
|
47
|
+
trim: true
|
|
48
|
+
},
|
|
49
|
+
billingPeriods: [{
|
|
50
|
+
type: String
|
|
51
|
+
}],
|
|
52
|
+
// Status fields
|
|
53
|
+
status: {
|
|
54
|
+
type: String,
|
|
55
|
+
enum: ['Held', 'Refunded', 'Partially Refunded', 'Forfeited'],
|
|
56
|
+
default: 'Held',
|
|
57
|
+
required: [true, 'Status is required']
|
|
58
|
+
},
|
|
59
|
+
refundedAmount: {
|
|
60
|
+
type: Number,
|
|
61
|
+
default: 0,
|
|
62
|
+
min: [0, 'Refunded amount cannot be negative']
|
|
63
|
+
},
|
|
64
|
+
refundDate: {
|
|
65
|
+
type: Date
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
timestamps: true,
|
|
70
|
+
toJSON: { virtuals: true },
|
|
71
|
+
toObject: { virtuals: true }
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Virtual populate for Contract details
|
|
76
|
+
DepositSchema.virtual('contract', {
|
|
77
|
+
ref: 'LevyContract',
|
|
78
|
+
localField: 'contractId',
|
|
79
|
+
foreignField: '_id',
|
|
80
|
+
justOne: true
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Virtual populate for Customer details
|
|
84
|
+
DepositSchema.virtual('customer', {
|
|
85
|
+
ref: 'Customer',
|
|
86
|
+
localField: 'customerId',
|
|
87
|
+
foreignField: '_id',
|
|
88
|
+
justOne: true
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Virtual populate for Unit details
|
|
92
|
+
DepositSchema.virtual('unit', {
|
|
93
|
+
ref: 'Unit',
|
|
94
|
+
localField: 'unitId',
|
|
95
|
+
foreignField: '_id',
|
|
96
|
+
justOne: true
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Virtual for remaining (unrefunded) balance
|
|
100
|
+
DepositSchema.virtual('balanceRemaining').get(function () {
|
|
101
|
+
return this.amount - (this.refundedAmount || 0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Pre-save middleware: set refundDate when status moves to Refunded/Partially Refunded
|
|
105
|
+
DepositSchema.pre('save', function (next) {
|
|
106
|
+
if (
|
|
107
|
+
(this.status === 'Refunded' || this.status === 'Partially Refunded') &&
|
|
108
|
+
!this.refundDate &&
|
|
109
|
+
this.isModified('status')
|
|
110
|
+
) {
|
|
111
|
+
this.refundDate = new Date();
|
|
112
|
+
}
|
|
113
|
+
// Guard against refunded amount exceeding original amount
|
|
114
|
+
if ((this.refundedAmount || 0) > this.amount) {
|
|
115
|
+
return next(new Error('Refunded amount cannot exceed deposit amount'));
|
|
116
|
+
}
|
|
117
|
+
// Auto-flip to fully Refunded when refundedAmount covers the amount
|
|
118
|
+
if (this.refundedAmount >= this.amount) {
|
|
119
|
+
this.status = 'Refunded';
|
|
120
|
+
}
|
|
121
|
+
next();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
module.exports = {
|
|
125
|
+
schema: DepositSchema,
|
|
126
|
+
name: 'Deposit'
|
|
127
|
+
};
|