diff --git a/account_statement_import_sheet_file_bg/models/account_statement_import.py b/account_statement_import_sheet_file_bg/models/account_statement_import.py index e2b004bb..6ec72806 100644 --- a/account_statement_import_sheet_file_bg/models/account_statement_import.py +++ b/account_statement_import_sheet_file_bg/models/account_statement_import.py @@ -6,7 +6,7 @@ from io import BytesIO from markupsafe import Markup -from odoo import _, models +from odoo import _, fields, models from openpyxl import Workbook, load_workbook @@ -14,6 +14,8 @@ 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.""" @@ -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, @@ -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