payservedb 5.1.1 → 5.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "5.1.1",
3
+ "version": "5.1.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -11,22 +11,25 @@ const commonAreaGeneratorReadingSchema = new mongoose.Schema({
11
11
  type: Date,
12
12
  required: true
13
13
  },
14
- openingReadingLtrs: {
15
- type: Number,
14
+ openingReading: {
15
+ type: Number, // Stored in total minutes (e.g., 3hrs 40mins = 220 minutes)
16
16
  required: true
17
17
  },
18
- closingReadingLtrs: {
19
- type: Number,
18
+ closingReading: {
19
+ type: Number, // Stored in total minutes (e.g., 8hrs 15mins = 495 minutes)
20
20
  required: true
21
21
  },
22
22
  totalHrsRun: {
23
- type: Number,
23
+ type: Number, // Can be calculated or entered directly
24
24
  required: true
25
25
  },
26
- totalFuelLevel: {
26
+ totalFuelLevelFraction: {
27
27
  type: String,
28
28
  trim: true
29
29
  },
30
+ totalFuelLevelLiters: {
31
+ type: Number
32
+ },
30
33
  refillAmountLiters: {
31
34
  type: Number
32
35
  }
@@ -34,5 +37,6 @@ const commonAreaGeneratorReadingSchema = new mongoose.Schema({
34
37
  timestamps: true
35
38
  });
36
39
 
40
+
37
41
  const CommonAreaGeneratorReading = mongoose.model('CommonAreaGeneratorReading', commonAreaGeneratorReadingSchema);
38
- module.exports = CommonAreaGeneratorReading;
42
+ module.exports = CommonAreaGeneratorReading;
@@ -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'