payservedb 4.3.4 → 4.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "4.3.4",
3
+ "version": "4.3.6",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -84,10 +84,12 @@ const invoiceSchema = new mongoose.Schema(
84
84
  default: 0,
85
85
  min: 0,
86
86
  },
87
+ // Mark as deprecated, keep for backward compatibility
87
88
  overpay: {
88
89
  type: Number,
89
90
  default: 0,
90
91
  min: 0,
92
+ deprecated: true
91
93
  },
92
94
  issueDate: {
93
95
  type: Date,
@@ -114,7 +116,9 @@ const invoiceSchema = new mongoose.Schema(
114
116
  type: String,
115
117
  default: null,
116
118
  },
117
- // New field for balance brought forward
119
+ // Enhanced balanceBroughtForward field
120
+ // Positive values = customer owes money
121
+ // Negative values = credit (previously tracked in overpay field)
118
122
  balanceBroughtForward: {
119
123
  type: Number,
120
124
  default: 0,
@@ -171,10 +175,29 @@ invoiceSchema.index({ issueDate: -1 });
171
175
  invoiceSchema.index({ 'currency.code': 1 }); // Add index for currency code
172
176
  invoiceSchema.index({ 'currency.id': 1 }); // Add index for currency ID
173
177
  invoiceSchema.index({ 'currency.code': 1, 'client.clientId': 1, status: 1 }); // Compound index for currency-based queries
178
+ invoiceSchema.index({ 'client.clientId': 1, balanceBroughtForward: 1 }); // Add index for finding invoices with credit balances
174
179
 
175
180
  // Add virtual field for calculating balance
176
181
  invoiceSchema.virtual('calculatedBalance').get(function () {
177
- return this.totalAmount - (this.amountPaid || 0);
182
+ const baseBalance = this.totalAmount - (this.amountPaid || 0);
183
+
184
+ // Add positive balanceBroughtForward (customer owes money)
185
+ if (this.balanceBroughtForward > 0) {
186
+ return baseBalance + this.balanceBroughtForward;
187
+ }
188
+
189
+ // Subtract negative balanceBroughtForward (credit)
190
+ return baseBalance;
191
+ });
192
+
193
+ // Add virtual field for credit balance
194
+ invoiceSchema.virtual('creditBalance').get(function () {
195
+ return this.balanceBroughtForward < 0 ? Math.abs(this.balanceBroughtForward) : 0;
196
+ });
197
+
198
+ // Getter for compatible overpay field
199
+ invoiceSchema.virtual('effectiveOverpay').get(function () {
200
+ return this.balanceBroughtForward < 0 ? Math.abs(this.balanceBroughtForward) : 0;
178
201
  });
179
202
 
180
203
  // Add method for currency conversion if needed
@@ -190,6 +213,14 @@ invoiceSchema.statics.findByCurrency = function (currencyCode) {
190
213
  return this.find({ 'currency.code': currencyCode.toUpperCase() });
191
214
  };
192
215
 
216
+ // Add static method to find invoices with credit balance
217
+ invoiceSchema.statics.findWithCreditBalance = function(clientId) {
218
+ return this.find({
219
+ 'client.clientId': clientId,
220
+ 'balanceBroughtForward': { $lt: 0 }
221
+ }).sort({ updatedAt: -1 });
222
+ };
223
+
193
224
  // Add static method to calculate totals by currency
194
225
  invoiceSchema.statics.calculateTotalsByCurrency = function (query = {}) {
195
226
  return this.aggregate([
@@ -205,6 +236,17 @@ invoiceSchema.statics.calculateTotalsByCurrency = function (query = {}) {
205
236
  ]);
206
237
  };
207
238
 
239
+ // Pre-save middleware to ensure overpay and balanceBroughtForward stay in sync during transition
240
+ invoiceSchema.pre('save', function(next) {
241
+ // If balanceBroughtForward is negative (credit), sync with overpay for backwards compatibility
242
+ if (this.balanceBroughtForward < 0) {
243
+ this.overpay = Math.abs(this.balanceBroughtForward);
244
+ } else {
245
+ this.overpay = 0; // No overpay if there's no negative balance
246
+ }
247
+ next();
248
+ });
249
+
208
250
  const Invoice = mongoose.model('Invoice', invoiceSchema);
209
251
 
210
- module.exports = Invoice;
252
+ module.exports = Invoice;
@@ -9,7 +9,6 @@ const waterMeterAccountSchema = new mongoose.Schema({
9
9
  account_no: {
10
10
  type: String,
11
11
  required: true,
12
- unique: true,
13
12
  trim: true
14
13
  },
15
14
  customerId: {