Files
Odoo-Modules/Fusion Accounting/models/account_journal_csv.py
2026-02-22 01:22:18 -05:00

74 lines
2.8 KiB
Python

# Fusion Accounting - CSV/XLS/XLSX Bank Statement Import
# Registers spreadsheet formats and routes uploads to the base_import wizard
from odoo import _, models
from odoo.exceptions import UserError
class FusionJournalCSVImport(models.Model):
"""Extends the journal import pipeline with CSV, XLS, and XLSX
support. Uploads matching these formats are routed through
the ``base_import`` wizard for column mapping."""
_inherit = 'account.journal'
# ---- Format Registration ----
def _get_bank_statements_available_import_formats(self):
"""Append spreadsheet formats to the list of importable types."""
supported = super()._get_bank_statements_available_import_formats()
supported.extend(['CSV', 'XLS', 'XLSX'])
return supported
# ---- Helpers ----
def _is_spreadsheet_file(self, filename):
"""Return True when *filename* has a CSV/XLS/XLSX extension."""
return bool(
filename
and filename.lower().strip().endswith(('.csv', '.xls', '.xlsx'))
)
# ---- Import Override ----
def _import_bank_statement(self, attachments):
"""Intercept spreadsheet uploads and redirect them to the
interactive column-mapping wizard. Non-spreadsheet files fall
through to the standard import chain.
Mixing CSV files with other formats or uploading more than one
CSV file at a time is not permitted.
"""
if len(attachments) > 1:
is_spreadsheet = [
bool(self._is_spreadsheet_file(att.name)) for att in attachments
]
if True in is_spreadsheet and False in is_spreadsheet:
raise UserError(
_('Mixing CSV/XLS files with other file types is not allowed.')
)
if is_spreadsheet.count(True) > 1:
raise UserError(_('Only one CSV/XLS file can be selected at a time.'))
return super()._import_bank_statement(attachments)
if not self._is_spreadsheet_file(attachments.name):
return super()._import_bank_statement(attachments)
# Create the base_import wizard and launch the interactive mapper
env_ctx = dict(self.env.context)
wizard = self.env['base_import.import'].create({
'res_model': 'account.bank.statement.line',
'file': attachments.raw,
'file_name': attachments.name,
'file_type': attachments.mimetype,
})
env_ctx['wizard_id'] = wizard.id
env_ctx['default_journal_id'] = self.id
return {
'type': 'ir.actions.client',
'tag': 'import_bank_stmt',
'params': {
'model': 'account.bank.statement.line',
'context': env_ctx,
'filename': 'bank_statement_import.csv',
},
}