payservedb 3.8.2 → 3.8.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": "3.8.2",
3
+ "version": "3.8.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -33,11 +33,33 @@ const invoiceSchema = new mongoose.Schema(
33
33
  ref: "Facility",
34
34
  required: true,
35
35
  },
36
+ name: {
37
+ type: String,
38
+ required: true
39
+ }
36
40
  },
37
41
  unit: {
38
42
  id: { type: mongoose.Schema.Types.ObjectId, ref: "Unit", required: true },
39
43
  name: { type: String, required: true },
40
44
  },
45
+ currency: {
46
+ id: {
47
+ type: mongoose.Schema.Types.ObjectId,
48
+ ref: "Currency",
49
+ required: true
50
+ },
51
+ name: {
52
+ type: String,
53
+ required: true
54
+ },
55
+ code: {
56
+ type: String,
57
+ required: true,
58
+ uppercase: true,
59
+ minlength: 3,
60
+ maxlength: 3
61
+ }
62
+ },
41
63
  items: [
42
64
  {
43
65
  description: { type: String, required: true },
@@ -113,7 +135,15 @@ const invoiceSchema = new mongoose.Schema(
113
135
  paymentReference: String,
114
136
  paymentCompletion: String,
115
137
  remainingBalance: Number,
116
- notes: String
138
+ notes: String,
139
+ exchangeRate: {
140
+ type: Number,
141
+ default: 1 // For cross-currency reconciliations
142
+ },
143
+ originalCurrency: {
144
+ code: String, // Original currency code if different from invoice currency
145
+ amount: Number // Amount in original currency
146
+ }
117
147
  }],
118
148
  paymentDetails: {
119
149
  paymentStatus: { type: String, required: true },
@@ -133,12 +163,43 @@ invoiceSchema.index({ status: 1 });
133
163
  invoiceSchema.index({ 'client.clientId': 1, status: 1 });
134
164
  invoiceSchema.index({ 'reconciliationHistory.paymentReference': 1 });
135
165
  invoiceSchema.index({ issueDate: -1 });
166
+ invoiceSchema.index({ 'currency.code': 1 }); // Add index for currency code
167
+ invoiceSchema.index({ 'currency.id': 1 }); // Add index for currency ID
168
+ invoiceSchema.index({ 'currency.code': 1, 'client.clientId': 1, status: 1 }); // Compound index for currency-based queries
136
169
 
137
170
  // Add virtual field for calculating balance
138
171
  invoiceSchema.virtual('calculatedBalance').get(function () {
139
172
  return this.totalAmount - (this.amountPaid || 0);
140
173
  });
141
174
 
175
+ // Add method for currency conversion if needed
176
+ invoiceSchema.methods.convertAmount = function (amount, fromCurrency, toCurrency, exchangeRate) {
177
+ if (fromCurrency === toCurrency) {
178
+ return amount;
179
+ }
180
+ return amount * exchangeRate;
181
+ };
182
+
183
+ // Add static method to find invoices by currency
184
+ invoiceSchema.statics.findByCurrency = function (currencyCode) {
185
+ return this.find({ 'currency.code': currencyCode.toUpperCase() });
186
+ };
187
+
188
+ // Add static method to calculate totals by currency
189
+ invoiceSchema.statics.calculateTotalsByCurrency = function (query = {}) {
190
+ return this.aggregate([
191
+ { $match: query },
192
+ {
193
+ $group: {
194
+ _id: '$currency.code',
195
+ totalAmount: { $sum: '$totalAmount' },
196
+ totalPaid: { $sum: '$amountPaid' },
197
+ count: { $sum: 1 }
198
+ }
199
+ }
200
+ ]);
201
+ };
202
+
142
203
  const Invoice = mongoose.model('Invoice', invoiceSchema);
143
204
 
144
205
  module.exports = Invoice;