From 736e816bae5e5c5e2397be2737f7b6564bbae933 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Wed, 21 May 2025 17:17:31 +0200 Subject: [PATCH] [REF] partner_statement: Allow to override XLSX structure --- .../report/outstanting_statement_xlsx.py | 178 ++++++++++++------ 1 file changed, 123 insertions(+), 55 deletions(-) diff --git a/partner_statement/report/outstanting_statement_xlsx.py b/partner_statement/report/outstanting_statement_xlsx.py index c0b2ebfe..3b3397dc 100644 --- a/partner_statement/report/outstanting_statement_xlsx.py +++ b/partner_statement/report/outstanting_statement_xlsx.py @@ -35,6 +35,124 @@ class OutstandingStatementXslx(models.AbstractModel): report_name = report_name + suffix return report_name + def _get_currency_header_row_data(self, partner, currency, data): + return [ + { + "col_pos": col_pos, + "sheet_func": "write", + "args": args, + } + for col_pos, args in enumerate( + [ + (_("Reference Number"), FORMATS["format_theader_yellow_center"]), + (_("Date"), FORMATS["format_theader_yellow_center"]), + (_("Due Date"), FORMATS["format_theader_yellow_center"]), + (_("Description"), FORMATS["format_theader_yellow_center"]), + (_("Original"), FORMATS["format_theader_yellow_center"]), + (_("Open Amount"), FORMATS["format_theader_yellow_center"]), + (_("Balance"), FORMATS["format_theader_yellow_center"]), + ] + ) + ] + + def _get_currency_line_row_data(self, partner, currency, data, line): + if line.get("blocked"): + format_tcell_left = FORMATS["format_tcell_left_blocked"] + format_tcell_date_left = FORMATS["format_tcell_date_left_blocked"] + format_distributed = FORMATS["format_distributed_blocked"] + current_money_format = FORMATS["current_money_format_blocked"] + else: + format_tcell_left = FORMATS["format_tcell_left"] + format_tcell_date_left = FORMATS["format_tcell_date_left"] + format_distributed = FORMATS["format_distributed"] + current_money_format = FORMATS["current_money_format"] + + name_to_show = ( + line.get("name", "") == "/" or not line.get("name", "") + ) and line.get("ref", "") + if line.get("name", "") and line.get("name", "") != "/": + if not line.get("ref", ""): + name_to_show = line.get("name", "") + else: + if (line.get("ref", "") in line.get("name", "")) or ( + line.get("name", "") == line.get("ref", "") + ): + name_to_show = line.get("name", "") + else: + name_to_show = line.get("ref", "") + + return [ + { + "col_pos": col_pos, + "sheet_func": "write", + "args": args, + } + for col_pos, args in enumerate( + [ + (line.get("move_id", ""), format_tcell_left), + (line.get("date", ""), format_tcell_date_left), + (line.get("date_maturity", ""), format_tcell_date_left), + (name_to_show, format_distributed), + (line.get("amount", ""), current_money_format), + (line.get("open_amount", ""), current_money_format), + (line.get("balance", ""), current_money_format), + ] + ) + ] + + def _get_currency_footer_row_data(self, partner, currency, data): + partner_data = data.get("data", {}).get(partner.id, {}) + currency_data = partner_data.get("currencies", {}).get(currency.id) + return [ + { + "col_pos": 1, + "sheet_func": "write", + "args": (partner_data.get("end"), FORMATS["format_tcell_date_left"]), + }, + { + "col_pos": 2, + "sheet_func": "merge_range", + "span": 3, + "args": (_("Ending Balance"), FORMATS["format_tcell_left"]), + }, + { + "col_pos": 6, + "sheet_func": "write", + "args": ( + currency_data.get("amount_due"), + FORMATS["current_money_format"], + ), + }, + ] + + def _write_row_data(self, sheet, row_pos, row_data): + for cell_data in row_data: + sheet_func_name = cell_data.get("sheet_func", "") + sheet_func = getattr(sheet, sheet_func_name, None) + if callable(sheet_func): + col_pos = cell_data["col_pos"] + args = cell_data["args"] + span = cell_data.get("span") + if span: + args = row_pos, col_pos + span, *args + + sheet_func(row_pos, col_pos, *args) + + def _write_currency_header(self, row_pos, sheet, partner, currency, data): + row_data = self._get_currency_header_row_data(partner, currency, data) + self._write_row_data(sheet, row_pos, row_data) + return row_pos + + def _write_currency_line(self, row_pos, sheet, partner, currency, data, line): + row_data = self._get_currency_line_row_data(partner, currency, data, line) + self._write_row_data(sheet, row_pos, row_data) + return row_pos + + def _write_currency_footer(self, row_pos, sheet, partner, currency, data): + row_data = self._get_currency_footer_row_data(partner, currency, data) + self._write_row_data(sheet, row_pos, row_data) + return row_pos + def _write_currency_lines(self, row_pos, sheet, partner, currency, data): partner_data = data.get("data", {}).get(partner.id, {}) currency_data = partner_data.get("currencies", {}).get(currency.id) @@ -50,65 +168,15 @@ class OutstandingStatementXslx(models.AbstractModel): row_pos, 0, row_pos, 6, statement_header, FORMATS["format_left_bold"] ) row_pos += 1 - sheet.write( - row_pos, 0, _("Reference Number"), FORMATS["format_theader_yellow_center"] - ) - sheet.write(row_pos, 1, _("Date"), FORMATS["format_theader_yellow_center"]) - sheet.write(row_pos, 2, _("Due Date"), FORMATS["format_theader_yellow_center"]) - sheet.write( - row_pos, 3, _("Description"), FORMATS["format_theader_yellow_center"] - ) - sheet.write(row_pos, 4, _("Original"), FORMATS["format_theader_yellow_center"]) - sheet.write( - row_pos, 5, _("Open Amount"), FORMATS["format_theader_yellow_center"] - ) - sheet.write(row_pos, 6, _("Balance"), FORMATS["format_theader_yellow_center"]) - format_tcell_left = FORMATS["format_tcell_left"] - format_tcell_date_left = FORMATS["format_tcell_date_left"] - format_distributed = FORMATS["format_distributed"] - current_money_format = FORMATS["current_money_format"] + row_pos = self._write_currency_header(row_pos, sheet, partner, currency, data) for line in currency_data.get("lines"): - if line.get("blocked"): - format_tcell_left = FORMATS["format_tcell_left_blocked"] - format_tcell_date_left = FORMATS["format_tcell_date_left_blocked"] - format_distributed = FORMATS["format_distributed_blocked"] - current_money_format = FORMATS["current_money_format_blocked"] row_pos += 1 - name_to_show = ( - line.get("name", "") == "/" or not line.get("name", "") - ) and line.get("ref", "") - if line.get("name", "") and line.get("name", "") != "/": - if not line.get("ref", ""): - name_to_show = line.get("name", "") - else: - if (line.get("ref", "") in line.get("name", "")) or ( - line.get("name", "") == line.get("ref", "") - ): - name_to_show = line.get("name", "") - else: - name_to_show = line.get("ref", "") - sheet.write(row_pos, 0, line.get("move_id", ""), format_tcell_left) - sheet.write(row_pos, 1, line.get("date", ""), format_tcell_date_left) - sheet.write( - row_pos, - 2, - line.get("date_maturity", ""), - format_tcell_date_left, + row_pos = self._write_currency_line( + row_pos, sheet, partner, currency, data, line ) - sheet.write(row_pos, 3, name_to_show, format_distributed) - sheet.write(row_pos, 4, line.get("amount", ""), current_money_format) - sheet.write(row_pos, 5, line.get("open_amount", ""), current_money_format) - sheet.write(row_pos, 6, line.get("balance", ""), current_money_format) + row_pos += 1 - sheet.write( - row_pos, 1, partner_data.get("end"), FORMATS["format_tcell_date_left"] - ) - sheet.merge_range( - row_pos, 2, row_pos, 4, _("Ending Balance"), FORMATS["format_tcell_left"] - ) - sheet.write( - row_pos, 6, currency_data.get("amount_due"), FORMATS["current_money_format"] - ) + row_pos = self._write_currency_footer(row_pos, sheet, partner, currency, data) return row_pos def _write_currency_buckets(self, row_pos, sheet, partner, currency, data):