payservedb 5.1.2 → 5.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/package.json +1 -1
- package/src/models/invoice.js +41 -0
- package/src/models/levycontract.js +73 -0
- package/src/models/suppliers.js +7 -7
package/package.json
CHANGED
package/src/models/invoice.js
CHANGED
|
@@ -189,6 +189,28 @@ const invoiceSchema = new mongoose.Schema(
|
|
|
189
189
|
facilityId: { type: mongoose.Schema.Types.ObjectId, ref: "Facility" },
|
|
190
190
|
userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" }
|
|
191
191
|
}]
|
|
192
|
+
},
|
|
193
|
+
// New fields for double entry accounts
|
|
194
|
+
invoiceDoubleEntryAccount: {
|
|
195
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
196
|
+
ref: "GLAccountDoubleEntries",
|
|
197
|
+
required: false,
|
|
198
|
+
},
|
|
199
|
+
paymentDoubleEntryAccount: {
|
|
200
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
201
|
+
ref: "GLAccountDoubleEntries",
|
|
202
|
+
required: false,
|
|
203
|
+
},
|
|
204
|
+
// GL account details for invoice entries
|
|
205
|
+
accountdebitedData: {
|
|
206
|
+
amount: { type: Number },
|
|
207
|
+
description: { type: String },
|
|
208
|
+
isActive: { type: Boolean, default: true }
|
|
209
|
+
},
|
|
210
|
+
accountcreditedData: {
|
|
211
|
+
amount: { type: Number },
|
|
212
|
+
description: { type: String },
|
|
213
|
+
isActive: { type: Boolean, default: true }
|
|
192
214
|
}
|
|
193
215
|
},
|
|
194
216
|
{
|
|
@@ -208,6 +230,9 @@ invoiceSchema.index({ 'currency.code': 1, 'client.clientId': 1, status: 1 }); //
|
|
|
208
230
|
invoiceSchema.index({ 'client.clientId': 1, balanceBroughtForward: 1 }); // Add index for finding invoices with credit balances
|
|
209
231
|
invoiceSchema.index({ 'viewStatus.isOpened': 1 }); // Add index for view status
|
|
210
232
|
invoiceSchema.index({ 'viewStatus.openedBy.facilityId': 1 }); // Add index for facility view tracking
|
|
233
|
+
// Add index for double entry accounts
|
|
234
|
+
invoiceSchema.index({ invoiceDoubleEntryAccount: 1 });
|
|
235
|
+
invoiceSchema.index({ paymentDoubleEntryAccount: 1 });
|
|
211
236
|
|
|
212
237
|
// Add virtual field for calculating balance
|
|
213
238
|
invoiceSchema.virtual('calculatedBalance').get(function () {
|
|
@@ -232,6 +257,22 @@ invoiceSchema.virtual('effectiveOverpay').get(function () {
|
|
|
232
257
|
return this.balanceBroughtForward < 0 ? Math.abs(this.balanceBroughtForward) : 0;
|
|
233
258
|
});
|
|
234
259
|
|
|
260
|
+
// Add virtual populate for invoice double entry account
|
|
261
|
+
invoiceSchema.virtual('invoiceDoubleEntry', {
|
|
262
|
+
ref: 'GLAccountDoubleEntries',
|
|
263
|
+
localField: 'invoiceDoubleEntryAccount',
|
|
264
|
+
foreignField: '_id',
|
|
265
|
+
justOne: true
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Add virtual populate for payment double entry account
|
|
269
|
+
invoiceSchema.virtual('paymentDoubleEntry', {
|
|
270
|
+
ref: 'GLAccountDoubleEntries',
|
|
271
|
+
localField: 'paymentDoubleEntryAccount',
|
|
272
|
+
foreignField: '_id',
|
|
273
|
+
justOne: true
|
|
274
|
+
});
|
|
275
|
+
|
|
235
276
|
// Add method for currency conversion if needed
|
|
236
277
|
invoiceSchema.methods.convertAmount = function (amount, fromCurrency, toCurrency, exchangeRate) {
|
|
237
278
|
if (fromCurrency === toCurrency) {
|
|
@@ -66,6 +66,9 @@ const LevyContractSchema = new Schema(
|
|
|
66
66
|
ref: 'Facility',
|
|
67
67
|
required: [true, 'Facility ID is required']
|
|
68
68
|
},
|
|
69
|
+
lastInvoiceDate: {
|
|
70
|
+
type: Date
|
|
71
|
+
},
|
|
69
72
|
createdBy: {
|
|
70
73
|
type: mongoose.Schema.Types.ObjectId,
|
|
71
74
|
ref: 'User'
|
|
@@ -77,9 +80,79 @@ const LevyContractSchema = new Schema(
|
|
|
77
80
|
},
|
|
78
81
|
{
|
|
79
82
|
timestamps: true,
|
|
83
|
+
toJSON: { virtuals: true },
|
|
84
|
+
toObject: { virtuals: true }
|
|
80
85
|
}
|
|
81
86
|
);
|
|
82
87
|
|
|
88
|
+
// Virtual populate for Levy details
|
|
89
|
+
LevyContractSchema.virtual('levy', {
|
|
90
|
+
ref: 'Levy',
|
|
91
|
+
localField: 'levyId',
|
|
92
|
+
foreignField: '_id',
|
|
93
|
+
justOne: true
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Virtual populate for Customer details
|
|
97
|
+
LevyContractSchema.virtual('customer', {
|
|
98
|
+
ref: 'Customer',
|
|
99
|
+
localField: 'customerId',
|
|
100
|
+
foreignField: '_id',
|
|
101
|
+
justOne: true
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Virtual populate for Unit details
|
|
105
|
+
LevyContractSchema.virtual('unit', {
|
|
106
|
+
ref: 'Unit',
|
|
107
|
+
localField: 'unitId',
|
|
108
|
+
foreignField: '_id',
|
|
109
|
+
justOne: true
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Virtual populate for invoice double entry account
|
|
113
|
+
LevyContractSchema.virtual('invoiceDoubleEntry', {
|
|
114
|
+
ref: 'GLAccountDoubleEntries',
|
|
115
|
+
localField: 'invoiceDoubleEntryAccount',
|
|
116
|
+
foreignField: '_id',
|
|
117
|
+
justOne: true
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Virtual populate for payment double entry account
|
|
121
|
+
LevyContractSchema.virtual('paymentDoubleEntry', {
|
|
122
|
+
ref: 'GLAccountDoubleEntries',
|
|
123
|
+
localField: 'paymentDoubleEntryAccount',
|
|
124
|
+
foreignField: '_id',
|
|
125
|
+
justOne: true
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Pre-save middleware to ensure endDate is after startDate
|
|
129
|
+
LevyContractSchema.pre('save', function (next) {
|
|
130
|
+
if (this.startDate && this.endDate && this.startDate >= this.endDate) {
|
|
131
|
+
next(new Error('End date must be after start date'));
|
|
132
|
+
} else {
|
|
133
|
+
next();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Pre-save middleware to maintain backward compatibility during migration
|
|
138
|
+
LevyContractSchema.pre('save', function (next) {
|
|
139
|
+
// If we have the old doubleEntryAccount but not the new ones,
|
|
140
|
+
// use the old one for both new fields during the transition
|
|
141
|
+
if (this.doubleEntryAccount &&
|
|
142
|
+
(!this.invoiceDoubleEntryAccount || !this.paymentDoubleEntryAccount)) {
|
|
143
|
+
this.invoiceDoubleEntryAccount = this.doubleEntryAccount;
|
|
144
|
+
this.paymentDoubleEntryAccount = this.doubleEntryAccount;
|
|
145
|
+
}
|
|
146
|
+
next();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Index for efficient queries
|
|
150
|
+
LevyContractSchema.index({ levyId: 1, unitId: 1, status: 1 });
|
|
151
|
+
LevyContractSchema.index({ facilityId: 1 });
|
|
152
|
+
LevyContractSchema.index({ customerId: 1 });
|
|
153
|
+
LevyContractSchema.index({ invoiceDoubleEntryAccount: 1 });
|
|
154
|
+
LevyContractSchema.index({ paymentDoubleEntryAccount: 1 });
|
|
155
|
+
|
|
83
156
|
module.exports = {
|
|
84
157
|
schema: LevyContractSchema,
|
|
85
158
|
name: 'LevyContract'
|
package/src/models/suppliers.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
const mongoose = require('mongoose');
|
|
2
|
-
|
|
3
1
|
const supplierSchema = new mongoose.Schema({
|
|
2
|
+
// Existing fields remain unchanged
|
|
4
3
|
facilityId: {
|
|
5
4
|
type: mongoose.Schema.Types.ObjectId,
|
|
6
5
|
ref: 'Facility',
|
|
@@ -60,10 +59,11 @@ const supplierSchema = new mongoose.Schema({
|
|
|
60
59
|
default: 'active',
|
|
61
60
|
index: true
|
|
62
61
|
},
|
|
62
|
+
userId: {
|
|
63
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
64
|
+
ref: 'User',
|
|
65
|
+
index: true
|
|
66
|
+
}
|
|
63
67
|
}, {
|
|
64
68
|
timestamps: true
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
const Supplier = mongoose.model('Supplier', supplierSchema);
|
|
68
|
-
|
|
69
|
-
module.exports = Supplier;
|
|
69
|
+
});
|