plugin-financials/repositories/BankStatementRepository.js
2025-11-03 13:51:33 +02:00

89 lines
2.3 KiB
JavaScript

const BaseFinancialRepository = require('./BaseFinancialRepository');
/**
* Bank Statement Repository
*
* Manages bank statements and their transactions for reconciliation.
*/
class BankStatementRepository extends BaseFinancialRepository {
constructor() {
super('pg_fn_bank_statements');
}
/**
* Find statements by bank account
* @param {string} bankAccountId - Bank account ID
* @returns {Promise<Array>} Array of bank statements
*/
async findByBankAccountId(bankAccountId) {
try {
return await this.findAll({ bank_account_id: bankAccountId });
} catch (error) {
throw error;
}
}
/**
* Get unreconciled statements
* @param {string} bankAccountId - Bank account ID
* @returns {Promise<Array>} Array of unreconciled statements
*/
async findUnreconciled(bankAccountId) {
try {
return await this.findAll({
bank_account_id: bankAccountId,
is_reconciled: false
});
} catch (error) {
throw error;
}
}
/**
* Create bank statement with transactions
* @param {Object} statementData - Statement data
* @param {Array} transactions - Array of transaction data
* @returns {Promise<Object>} Created statement with transactions
*/
async createStatementWithTransactions(statementData, transactions = []) {
try {
const statement = await this.create(statementData);
if (transactions.length > 0) {
const { bankStatementTransactionRepository } = require('./index');
for (const transaction of transactions) {
await bankStatementTransactionRepository.create({
...transaction,
bank_statement_id: statement.id
});
}
}
return statement;
} catch (error) {
throw error;
}
}
/**
* Mark statement as reconciled
* @param {string} statementId - Statement ID
* @param {string} userId - User ID who reconciled
* @returns {Promise<Object>} Updated statement
*/
async markAsReconciled(statementId, userId) {
try {
return await this.updateById(statementId, {
is_reconciled: true,
reconciled_date: new Date(),
reconciled_by: userId,
updated_at: new Date()
});
} catch (error) {
throw error;
}
}
}
module.exports = BankStatementRepository;