bank_statement_reconciliation/static/src/js/bank_statement_total_widget.js

81 lines
2.5 KiB
JavaScript

/** @odoo-module **/
import { Component, useState, onWillStart, onWillUpdateProps } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
export class BankStatementTotalWidget extends Component {
setup() {
this.orm = useService("orm");
this.state = useState({
totalAmount: 0,
selectedCount: 0,
currency: null,
});
onWillStart(async () => {
await this.updateTotals();
});
onWillUpdateProps(async (nextProps) => {
await this.updateTotals(nextProps);
});
}
async updateTotals(props = this.props) {
const resIds = props.resIds || [];
if (resIds.length === 0) {
this.state.totalAmount = 0;
this.state.selectedCount = 0;
this.state.currency = null;
return;
}
try {
const records = await this.orm.searchRead(
"account.bank.statement.line",
[["id", "in", resIds]],
["amount", "currency_id"]
);
let total = 0;
let currency = null;
records.forEach(record => {
total += record.amount || 0;
if (!currency && record.currency_id) {
currency = record.currency_id;
}
});
this.state.totalAmount = total;
this.state.selectedCount = records.length;
this.state.currency = currency;
} catch (error) {
console.error("Error fetching bank statement totals:", error);
this.state.totalAmount = 0;
this.state.selectedCount = 0;
this.state.currency = null;
}
}
get formattedTotal() {
if (!this.state.currency) {
return this.state.totalAmount.toFixed(2);
}
// Format with currency symbol if available
return this.state.totalAmount.toFixed(2);
}
get displayClass() {
return this.state.totalAmount < 0 ? 'text-danger' : 'text-success';
}
}
BankStatementTotalWidget.template = "bank_statement_reconciliation.BankStatementTotalWidget";
BankStatementTotalWidget.props = {
resIds: { type: Array, optional: true },
};
registry.category("view_widgets").add("bank_statement_total_widget", BankStatementTotalWidget);