Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
from io import BytesIO

from markupsafe import Markup
from odoo import _, models
from odoo import _, fields, models
from openpyxl import Workbook, load_workbook


class AccountStatementImport(models.TransientModel):
_name = "account.statement.import"
_inherit = ["account.statement.import", "base.bg"]

csv_or_xls = fields.Selection([("csv", "CSV"), ("xls", "Excel")], string="File Type")

def import_file_button(self, wizard_data=None):
"""Process the file chosen in the wizard, create a bank statement
and return a link to its reconciliation page."""
Expand All @@ -37,9 +39,21 @@ def import_file_button(self, wizard_data=None):

if files:
for idx, file in enumerate(files):
# Encode the file to string format, because background jobs cannot
# be executed if the parameters passed are not serializable (the original format is bytes).
# It is decoded back in import_file_button to be processed normally (line 99)
if not isinstance(file, str):
try:
file_bytes = base64.b64decode(file)
file_str = file_bytes.decode("utf-8")
self.csv_or_xls = "csv"
except Exception:
file_str = base64.b64encode(file_bytes).decode("ascii")
self.csv_or_xls = "xls"

# Create wizard data to be passed to bg job
wizard_data = {
"statement_file": file,
"statement_file": file_str,
"statement_filename": self.statement_filename,
"sheet_mapping_id": self.sheet_mapping_id.id,
"part_number": idx + 1,
Expand Down Expand Up @@ -86,6 +100,15 @@ def import_file_button(self, wizard_data=None):
# Extract part info before creating wizard
part_number = wizard_data.pop("part_number", None)
total_parts = wizard_data.pop("total_parts", None)
# Decode file from string back to bytes based on file type
statement_file = wizard_data.get("statement_file")
if statement_file and isinstance(statement_file, str):
if self.csv_or_xls == "csv":
# CSV files use UTF-8 encoding
wizard_data["statement_file"] = base64.b64encode(statement_file.encode("utf-8"))
elif self.csv_or_xls == "xls":
# Excel files are already base64 encoded as ASCII strings
wizard_data["statement_file"] = statement_file.encode("ascii")
wizard = self.create(wizard_data)
else:
wizard = self
Expand Down