From e1d4f63eca13dc30a1b2c4ca96822f42052b4b20 Mon Sep 17 00:00:00 2001 From: smdc-odoo Date: Mon, 16 Jan 2023 14:07:59 +0000 Subject: [PATCH] [IMP] account_edi: Add the possibility to download edi document from list view In some occasions, we want to be able to download all edi documents so the user can upload them on a governemental platform. From the list view of all the invoice, it is now possible to selected as many invoice as we want, and with a newly added action download them into a zip. task-3122424 closes odoo/odoo#116854 X-original-commit: 5171f607e1fddde06b769ebb78d82548ad4dc84d Signed-off-by: Nicolas Viseur (vin) --- addons/account_edi/__init__.py | 1 + addons/account_edi/controller/__init__.py | 1 + addons/account_edi/controller/main.py | 18 +++++++++++++ addons/account_edi/models/account_move.py | 26 +++++++++++++++++++ .../account_edi/views/account_move_views.xml | 19 +++++++++++--- 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 addons/account_edi/controller/__init__.py create mode 100644 addons/account_edi/controller/main.py diff --git a/addons/account_edi/__init__.py b/addons/account_edi/__init__.py index 21ff7c22f3c..75da1c3431e 100644 --- a/addons/account_edi/__init__.py +++ b/addons/account_edi/__init__.py @@ -1,3 +1,4 @@ # -*- encoding: utf-8 -*- from . import models +from . import controller diff --git a/addons/account_edi/controller/__init__.py b/addons/account_edi/controller/__init__.py new file mode 100644 index 00000000000..12a7e529b67 --- /dev/null +++ b/addons/account_edi/controller/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/addons/account_edi/controller/main.py b/addons/account_edi/controller/main.py new file mode 100644 index 00000000000..065b7dff157 --- /dev/null +++ b/addons/account_edi/controller/main.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import http +from odoo.http import request, content_disposition + + +class EdiDocumentDownloadController(http.Controller): + @http.route('/account_edi/download_edi_documents', type='http', auth='user') + def download_edi_documents(self, **args): + ids = list(map(int, request.httprequest.args.getlist('ids'))) + content = request.env['account.move'].browse(ids)._create_zipped() + headers = [ + ('Content-Type', 'zip'), + ('X-Content-Type-Options', 'nosniff'), + ('Content-Length', len(content)), + ('Content-Disposition', content_disposition('edi_documents.zip')), + ] + return request.make_response(content, headers) diff --git a/addons/account_edi/models/account_move.py b/addons/account_edi/models/account_move.py index 37c3dec9535..fc1bdf414db 100644 --- a/addons/account_edi/models/account_move.py +++ b/addons/account_edi/models/account_move.py @@ -1,5 +1,10 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. + +import io +import zipfile +from werkzeug.urls import url_encode + from odoo import api, fields, models, _ from odoo.exceptions import UserError @@ -434,6 +439,27 @@ class AccountMove(models.Model): move_result.setdefault('attachments', []).extend(edi_attachments.get('attachments', [])) return result + #################################################### + # Export Electronic Document + #################################################### + + def _action_download_electronic_invoice(self): + if not self: + return False + return { + 'type': 'ir.actions.act_url', + 'url': '/account_edi/download_edi_documents?%s' % url_encode({'ids': self.filtered('edi_document_ids').ids}), + 'target': 'new', + } + + def _create_zipped(self): + buffer = io.BytesIO() + with zipfile.ZipFile(buffer, 'w', compression=zipfile.ZIP_DEFLATED) as zipfile_obj: + for invoice in self: + for document in invoice.edi_document_ids: + if document.state in {'sent', 'cancelled'}: + zipfile_obj.writestr(document.display_name, document.attachment_id.raw) + return buffer.getvalue() class AccountMoveLine(models.Model): _inherit = 'account.move.line' diff --git a/addons/account_edi/views/account_move_views.xml b/addons/account_edi/views/account_move_views.xml index 566b333de65..bb9ddfe8d3c 100644 --- a/addons/account_edi/views/account_move_views.xml +++ b/addons/account_edi/views/account_move_views.xml @@ -90,13 +90,13 @@
The invoice will be processed asynchronously by the following E-invoicing service :
-