Spaces:
Running
Running
| const mongoose = require('mongoose'); | |
| // Each CapitalAccount represents a physical location of money: | |
| // - Cash (ููุฏู) | |
| // - Bank account (e.g. CIB, QNB, Banque Misr, etc.) | |
| // - E-wallet (e.g. Vodafone Cash, InstaPay, etc.) | |
| // | |
| // The sum of all account balances = total capital (currentCapital) | |
| const transferLogSchema = new mongoose.Schema({ | |
| amount: { | |
| type: Number, | |
| required: true, | |
| min: [0.01, 'Transfer amount must be positive'], | |
| }, | |
| fromAccount: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'CapitalAccount', | |
| }, | |
| toAccount: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'CapitalAccount', | |
| }, | |
| note: { | |
| type: String, | |
| trim: true, | |
| default: '', | |
| }, | |
| date: { | |
| type: Date, | |
| default: Date.now, | |
| }, | |
| createdBy: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| }, | |
| }); | |
| const capitalAccountSchema = new mongoose.Schema( | |
| { | |
| name: { | |
| type: String, | |
| required: [true, 'Account must have a name'], | |
| trim: true, | |
| // e.g. "Cash", "CIB Bank", "QNB", "Vodafone Cash" | |
| }, | |
| type: { | |
| type: String, | |
| required: [true, 'Account must have a type'], | |
| enum: ['cash', 'bank', 'e_wallet', 'other'], | |
| default: 'cash', | |
| }, | |
| bankName: { | |
| type: String, | |
| trim: true, | |
| default: '', | |
| // Only relevant for type='bank' โ e.g. "CIB", "QNB", "Banque Misr" | |
| }, | |
| accountNumber: { | |
| type: String, | |
| trim: true, | |
| default: '', | |
| // Bank account number or wallet number (optional, for reference) | |
| }, | |
| balance: { | |
| type: Number, | |
| default: 0, | |
| // Current balance in this account | |
| }, | |
| isActive: { | |
| type: Boolean, | |
| default: true, | |
| }, | |
| notes: { | |
| type: String, | |
| trim: true, | |
| default: '', | |
| }, | |
| }, | |
| { | |
| timestamps: true, | |
| }, | |
| ); | |
| capitalAccountSchema.index({ type: 1 }); | |
| capitalAccountSchema.index({ isActive: 1 }); | |
| const transferSchema = new mongoose.Schema( | |
| { | |
| fromAccount: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'CapitalAccount', | |
| required: [true, 'Transfer must have a source account'], | |
| }, | |
| toAccount: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'CapitalAccount', | |
| required: [true, 'Transfer must have a destination account'], | |
| }, | |
| amount: { | |
| type: Number, | |
| required: [true, 'Transfer must have an amount'], | |
| min: [0.01, 'Transfer amount must be positive'], | |
| }, | |
| note: { | |
| type: String, | |
| trim: true, | |
| default: '', | |
| }, | |
| date: { | |
| type: Date, | |
| default: Date.now, | |
| }, | |
| createdBy: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| required: true, | |
| }, | |
| }, | |
| { | |
| timestamps: true, | |
| }, | |
| ); | |
| transferSchema.index({ date: -1 }); | |
| transferSchema.index({ fromAccount: 1 }); | |
| transferSchema.index({ toAccount: 1 }); | |
| const CapitalAccount = mongoose.model('CapitalAccount', capitalAccountSchema); | |
| const CapitalTransfer = mongoose.model('CapitalTransfer', transferSchema); | |
| module.exports = { CapitalAccount, CapitalTransfer }; | |