From 1cfcb6f40d28c6c94f64a7e4adbffb0f453867b3 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 6 Aug 2019 18:19:01 +0200 Subject: [PATCH 1/6] [IMP] shopinvader: Allows to download paid invoices for confirmed sale orders --- shopinvader/tests/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shopinvader/tests/__init__.py b/shopinvader/tests/__init__.py index 1bc82ef0bd..0f1ddc3415 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -12,6 +12,8 @@ from . import test_customer from . import test_shopinvader_partner from . import test_shopinvader_variant_seo_title +from . import test_res_partner +from . import test_invoice from . import test_shopinvader_partner_binding from . import test_res_partner from . import test_invoice From b51b06397fd8b4582f2f4e3754d8361f9d526e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Tue, 17 Sep 2019 16:29:25 +0200 Subject: [PATCH 2/6] Continue --- shopinvader/services/invoice.py | 6 +- shopinvader/tests/__init__.py | 2 +- shopinvader_invoice/services/invoice.py | 22 +-- .../tests/test_invoice_service.py | 133 ++++++++---------- 4 files changed, 65 insertions(+), 98 deletions(-) diff --git a/shopinvader/services/invoice.py b/shopinvader/services/invoice.py index 4e5dfdb955..32f8e003a7 100644 --- a/shopinvader/services/invoice.py +++ b/shopinvader/services/invoice.py @@ -15,7 +15,7 @@ class InvoiceService(Component): - _inherit = "base.shopinvader.service" + _inherit = "shopinvader.abstract.mail.service" _name = "shopinvader.invoice.service" _usage = "invoice" _expose_model = "account.invoice" @@ -32,7 +32,7 @@ def download(self, _id, **params): invoice = self._get(_id) headers, content = self._get_binary_content(invoice) if not content: - raise MissingError(_("No image found for partner %s") % _id) + raise MissingError(_("No content found for invoice %s") % _id) response = request.make_response(content, headers) response.status_code = 200 return response @@ -89,7 +89,7 @@ def _get_base_search_domain(self): ("typology", "=", "sale"), ] # invoice_ids on sale.order is a computed field... - # to avoid to duplicate the logic, we search for the sale oders + # to avoid to duplicate the logic, we search for the sale orders # and check if the invoice_id is into the list of sale.invoice_ids sales = self.env["sale.order"].search(so_domain) invoice_ids = sales.mapped("invoice_ids").ids diff --git a/shopinvader/tests/__init__.py b/shopinvader/tests/__init__.py index 0f1ddc3415..b76f28da4f 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from . import test_controller +# from . import test_controller from . import test_backend from . import test_cart from . import test_cart_item diff --git a/shopinvader_invoice/services/invoice.py b/shopinvader_invoice/services/invoice.py index cd720ee077..1efa027399 100644 --- a/shopinvader_invoice/services/invoice.py +++ b/shopinvader_invoice/services/invoice.py @@ -6,10 +6,7 @@ class InvoiceService(Component): - _inherit = "shopinvader.abstract.mail.service" - _name = "shopinvader.invoice.service" - _usage = "invoice" - _expose_model = "account.invoice" + _inherit = "shopinvader.invoice.service" def search(self, **params): """ @@ -110,20 +107,3 @@ def _to_json(self, invoices): for invoice in invoices: res.append(self._to_json_invoice(invoice)) return res - - def _get_base_search_domain(self): - """ - Get every account.invoice (customer invoices or refunds) - related to current user. - If the current user is the anonymous one, it'll return an invalid - domain (to have 0 invoice as result) - :return: - """ - if self.shopinvader_backend.anonymous_partner_id == self.partner: - return [(0, "=", 1)] - return [ - ("partner_id", "=", self.partner.id), - ("type", "in", ("out_invoice", "out_refund")), - ("state", "in", ["open", "paid"]), - ("shopinvader_backend_id", "=", self.shopinvader_backend.id), - ] diff --git a/shopinvader_invoice/tests/test_invoice_service.py b/shopinvader_invoice/tests/test_invoice_service.py index 905546d371..1c1e7138b5 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -13,8 +13,18 @@ class TestInvoiceService(CommonCase): def setUp(self, *args, **kwargs): super(TestInvoiceService, self).setUp(*args, **kwargs) self.invoice_obj = self.env["account.invoice"] - self.partner = self.env.ref("base.res_partner_2").copy() + self.journal_obj = self.env['account.journal'] + self.register_payments_obj = self.env['account.register.payments'] + self.sale = self.env.ref("shopinvader.sale_order_2") + self.partner = self.env.ref("shopinvader.partner_1") self.product = self.env.ref("product.product_product_4") + self.bank_journal_euro = self.journal_obj.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BNK67', + }) + self.payment_method_manual_in = self.env.ref( + "account.account_payment_method_manual_in") self.precision = 2 with self.work_on_services(partner=self.partner) as work: self.service = work.component(usage="invoice") @@ -44,13 +54,7 @@ def _check_data_content(self, data, invoices): :return: bool """ # To have them into correct order - invoices = invoices.search( - [ - ("id", "in", invoices.ids), - # Invoice must be paid to be in data - ("state", "in", ["open", "paid"]), - ] - ) + invoices = invoices.search(self.service._get_base_search_domain()) self.assertEquals(len(data), len(invoices)) for current_data, invoice in zip(data, invoices): state_label = self._get_selection_label(invoice, "state") @@ -74,65 +78,51 @@ def _check_data_content(self, data, invoices): self.assertEquals(current_data.get("amount_due"), invoice.residual) return True - def _create_invoice( - self, partner=False, inv_type="out_invoice", validate=False - ): + def _confirm_and_invoice_sale(self, sale, payment=True): """ - Create a new invoice - :param partner: res.partner - :param inv_type: str - :param validate: bool - :return: stock.invoice recordset + Confirm the given SO and create an invoice. + Can also make the payment if payment parameter is True + :param sale: sale.order recordset + :param payment: bool + :return: account.invoice """ - partner = partner or self.partner - account = self.product.categ_id.property_account_expense_categ_id - values = { - "partner_id": partner.id, - "partner_shipping_id": partner.id, - "shopinvader_backend_id": self.backend.id, - "date_invoice": fields.Date.today(), - "type": inv_type, - "invoice_line_ids": [ - ( - 0, - False, - { - "product_id": self.product.id, - "quantity": 10, - "price_unit": 1250, - "account_id": account.id, - "name": self.product.display_name, - }, - ) - ], - } - invoice = self.invoice_obj.create(values) - if validate: - invoice.action_invoice_open() + sale.action_confirm() + for line in sale.order_line: + line.write({"qty_delivered": line.product_uom_qty}) + invoice_id = sale.action_invoice_create() + invoice = self.env["account.invoice"].browse(invoice_id) + invoice.action_invoice_open() + invoice.action_move_create() + if payment: + self._make_payment(invoice) return invoice - def test_get_invoice_anonymous(self): + def _make_payment(self, invoice, journal=False, amount=False): """ - Test the get on guest mode (using anonymous user). - It should not return any result, even if the anonymous user has some - invoices - :return: + Make payment for given invoice + :param invoice: account.invoice recordset + :param amount: float + :return: bool """ - # Check first without invoice related to the anonymous user - result = self.service_guest.dispatch("search") - data = result.get("data", []) - self.assertFalse(data) - # Then create a invoice related to the anonymous user - invoice = self._create_invoice( - partner=self.backend.anonymous_partner_id, validate=True - ) - self.assertEquals( - invoice.partner_id, self.backend.anonymous_partner_id - ) - result = self.service_guest.dispatch("search") - data = result.get("data", []) - self.assertFalse(data) - return + ctx = { + 'active_model': invoice._name, + 'active_ids': invoice.ids, + } + wizard_obj = self.register_payments_obj.with_context(ctx) + register_payments = wizard_obj.create({ + 'payment_date': fields.Date.today(), + 'journal_id': self.bank_journal_euro.id, + 'payment_method_id': self.payment_method_manual_in.id, + }) + if journal: + register_payments.write({ + 'journal_id': journal.id, + }) + if amount: + register_payments.write({ + 'amount': amount, + }) + register_payments.create_payment() def test_get_invoice_logged(self): """ @@ -146,12 +136,12 @@ def test_get_invoice_logged(self): data = result.get("data", []) self.assertFalse(data) # Then create a invoice related to partner - invoice = self._create_invoice(partner=self.service.partner) + invoice = self._confirm_and_invoice_sale(self.sale, payment=False) self.assertEquals(invoice.partner_id, self.service.partner) result = self.service.dispatch("search") data = result.get("data", []) self._check_data_content(data, invoice) - invoice.action_invoice_open() + self._make_payment(invoice) result = self.service.dispatch("search") data = result.get("data", []) self._check_data_content(data, invoice) @@ -164,16 +154,13 @@ def test_get_multi_invoice(self): But to the second, he should have one. :return: """ - invoice1 = self._create_invoice( - partner=self.service.partner, validate=True - ) - invoice2 = self._create_invoice( - partner=self.service.partner, validate=True - ) - invoice3 = self._create_invoice( - partner=self.service.partner, validate=True - ) - invoice4 = self._create_invoice(partner=self.service.partner) + sale2 = self.sale.copy() + sale3 = self.sale.copy() + sale4 = self.sale.copy() + invoice1 = self._confirm_and_invoice_sale(self.sale) + invoice2 = self._confirm_and_invoice_sale(sale2) + invoice3 = self._confirm_and_invoice_sale(sale3) + invoice4 = self._confirm_and_invoice_sale(sale4) invoices = invoice1 | invoice2 | invoice3 | invoice4 self.assertEquals(invoice1.partner_id, self.service.partner) self.assertEquals(invoice2.partner_id, self.service.partner) From df6c0f0f40f428eb99aef67b9df89a1cdb30b66a Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 6 Aug 2019 18:19:01 +0200 Subject: [PATCH 3/6] [IMP] shopinvader: Allows to download paid invoices for confirmed sale orders --- shopinvader/services/invoice.py | 2 +- shopinvader/tests/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shopinvader/services/invoice.py b/shopinvader/services/invoice.py index 32f8e003a7..22b82cf7ad 100644 --- a/shopinvader/services/invoice.py +++ b/shopinvader/services/invoice.py @@ -15,7 +15,7 @@ class InvoiceService(Component): - _inherit = "shopinvader.abstract.mail.service" + _inherit = "base.shopinvader.service" _name = "shopinvader.invoice.service" _usage = "invoice" _expose_model = "account.invoice" diff --git a/shopinvader/tests/__init__.py b/shopinvader/tests/__init__.py index b76f28da4f..0f1ddc3415 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# from . import test_controller +from . import test_controller from . import test_backend from . import test_cart from . import test_cart_item From 617de38cf42661f04c8caa5c7e9def8f6a665064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Wed, 18 Sep 2019 08:02:54 +0200 Subject: [PATCH 4/6] pre-commit + update test --- .../tests/test_invoice_service.py | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/shopinvader_invoice/tests/test_invoice_service.py b/shopinvader_invoice/tests/test_invoice_service.py index 1c1e7138b5..7d5ebdb850 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -13,18 +13,17 @@ class TestInvoiceService(CommonCase): def setUp(self, *args, **kwargs): super(TestInvoiceService, self).setUp(*args, **kwargs) self.invoice_obj = self.env["account.invoice"] - self.journal_obj = self.env['account.journal'] - self.register_payments_obj = self.env['account.register.payments'] + self.journal_obj = self.env["account.journal"] + self.register_payments_obj = self.env["account.register.payments"] self.sale = self.env.ref("shopinvader.sale_order_2") self.partner = self.env.ref("shopinvader.partner_1") self.product = self.env.ref("product.product_product_4") - self.bank_journal_euro = self.journal_obj.create({ - 'name': 'Bank', - 'type': 'bank', - 'code': 'BNK67', - }) + self.bank_journal_euro = self.journal_obj.create( + {"name": "Bank", "type": "bank", "code": "BNK67"} + ) self.payment_method_manual_in = self.env.ref( - "account.account_payment_method_manual_in") + "account.account_payment_method_manual_in" + ) self.precision = 2 with self.work_on_services(partner=self.partner) as work: self.service = work.component(usage="invoice") @@ -104,24 +103,19 @@ def _make_payment(self, invoice, journal=False, amount=False): :param amount: float :return: bool """ - ctx = { - 'active_model': invoice._name, - 'active_ids': invoice.ids, - } + ctx = {"active_model": invoice._name, "active_ids": invoice.ids} wizard_obj = self.register_payments_obj.with_context(ctx) - register_payments = wizard_obj.create({ - 'payment_date': fields.Date.today(), - 'journal_id': self.bank_journal_euro.id, - 'payment_method_id': self.payment_method_manual_in.id, - }) + register_payments = wizard_obj.create( + { + "payment_date": fields.Date.today(), + "journal_id": self.bank_journal_euro.id, + "payment_method_id": self.payment_method_manual_in.id, + } + ) if journal: - register_payments.write({ - 'journal_id': journal.id, - }) + register_payments.write({"journal_id": journal.id}) if amount: - register_payments.write({ - 'amount': amount, - }) + register_payments.write({"amount": amount}) register_payments.create_payment() def test_get_invoice_logged(self): From fedbb27667be232ffb886a5184eafbc5159be289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Mon, 21 Oct 2019 12:29:38 +0200 Subject: [PATCH 5/6] Update after review + fix conflicts --- shopinvader/services/invoice.py | 6 ++++-- shopinvader/tests/__init__.py | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/shopinvader/services/invoice.py b/shopinvader/services/invoice.py index 22b82cf7ad..31fb710b8c 100644 --- a/shopinvader/services/invoice.py +++ b/shopinvader/services/invoice.py @@ -107,13 +107,15 @@ def _get_binary_content(self, invoice): invoice_report_def = invoice.invoice_print() report_name = invoice_report_def["report_name"] report_type = invoice_report_def["report_type"] - content, format = self.env["ir.actions.report.xml"].render_report( + content, file_format = self.env["ir.actions.report.xml"].render_report( res_ids=invoice.ids, name=report_name, data={"report_type": report_type}, ) report = self._get_report(report_name, report_type) - filename = self._get_binary_content_filename(invoice, report, format) + filename = self._get_binary_content_filename( + invoice, report, file_format + ) mimetype = mimetypes.guess_type(filename) if mimetype: mimetype = mimetype[0] diff --git a/shopinvader/tests/__init__.py b/shopinvader/tests/__init__.py index 0f1ddc3415..68e324f556 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -15,5 +15,3 @@ from . import test_res_partner from . import test_invoice from . import test_shopinvader_partner_binding -from . import test_res_partner -from . import test_invoice From 0f46580e0781237e0736b7dd745c915e64ee6680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Honor=C3=A9?= Date: Wed, 23 Oct 2019 09:44:54 +0200 Subject: [PATCH 6/6] Update after review --- shopinvader_invoice/tests/test_invoice_service.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/shopinvader_invoice/tests/test_invoice_service.py b/shopinvader_invoice/tests/test_invoice_service.py index 7d5ebdb850..5581b45cfc 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -104,7 +104,7 @@ def _make_payment(self, invoice, journal=False, amount=False): :return: bool """ ctx = {"active_model": invoice._name, "active_ids": invoice.ids} - wizard_obj = self.register_payments_obj.with_context(ctx) + wizard_obj = self.register_payments_obj.with_context(**ctx) register_payments = wizard_obj.create( { "payment_date": fields.Date.today(), @@ -112,10 +112,13 @@ def _make_payment(self, invoice, journal=False, amount=False): "payment_method_id": self.payment_method_manual_in.id, } ) + values = {} if journal: - register_payments.write({"journal_id": journal.id}) + values.update({"journal_id": journal.id}) if amount: - register_payments.write({"amount": amount}) + values.update({"amount": amount}) + if values: + register_payments.write(values) register_payments.create_payment() def test_get_invoice_logged(self):