From 4609ca6880d313ed96c89f361a3437ea229e1b10 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 6 Aug 2019 18:19:01 +0200 Subject: [PATCH 1/7] [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 45608665a5..f9bbacfe10 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -10,6 +10,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 c25d2839429f3347940cefe5085124db101eb7a6 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 6 Aug 2019 18:19:01 +0200 Subject: [PATCH 2/7] [IMP] shopinvader: Allows to download paid invoices for confirmed sale orders From 0f5683ae9e09c8c739075087e2386288bb60775f 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 3/7] Continue --- shopinvader/services/invoice.py | 4 +- shopinvader_invoice/services/invoice.py | 22 +-- .../tests/test_invoice_service.py | 125 ++++++++---------- 3 files changed, 56 insertions(+), 95 deletions(-) diff --git a/shopinvader/services/invoice.py b/shopinvader/services/invoice.py index 1a92f495a3..d7c68fbd02 100644 --- a/shopinvader/services/invoice.py +++ b/shopinvader/services/invoice.py @@ -17,7 +17,7 @@ class InvoiceService(Component): - _inherit = "base.shopinvader.service" + _inherit = "shopinvader.abstract.mail.service" _name = "shopinvader.invoice.service" _usage = "invoice" _expose_model = "account.invoice" @@ -91,7 +91,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_invoice/services/invoice.py b/shopinvader_invoice/services/invoice.py index 46e794c608..8744ea6871 100644 --- a/shopinvader_invoice/services/invoice.py +++ b/shopinvader_invoice/services/invoice.py @@ -5,10 +5,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): """ @@ -109,20 +106,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 8fa56ebd15..09d0f68dc7 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -12,8 +12,17 @@ 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") @@ -43,13 +52,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 +77,46 @@ 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 + 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, + } ) - self.assertEquals( - invoice.partner_id, self.backend.anonymous_partner_id - ) - result = self.service_guest.dispatch("search") - data = result.get("data", []) - self.assertFalse(data) - return + 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 +130,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 +148,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 10db057993709b8c8839d397cb6eb6df75ae2a4f 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/7] pre-commit + update test From 784c1705bc363738882c46ff12af56f5a27f8d58 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/7] Update after review + fix conflicts --- shopinvader/tests/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/shopinvader/tests/__init__.py b/shopinvader/tests/__init__.py index f9bbacfe10..3f7ceaca94 100644 --- a/shopinvader/tests/__init__.py +++ b/shopinvader/tests/__init__.py @@ -13,5 +13,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 1d51e7fe52ca3102e2088c0cfb62a7e1c6c6097a 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/7] 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 09d0f68dc7..73031d21d1 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): From 2d7ae2ef2c2df6c8bf3421ae53336aee23e69f54 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Thu, 31 Oct 2019 10:01:49 +0100 Subject: [PATCH 7/7] [FIX] shopinvader_invoice: Adapt code to 12.0 after forward port of #407 --- shopinvader_invoice/tests/test_invoice_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shopinvader_invoice/tests/test_invoice_service.py b/shopinvader_invoice/tests/test_invoice_service.py index 73031d21d1..10d5f83891 100644 --- a/shopinvader_invoice/tests/test_invoice_service.py +++ b/shopinvader_invoice/tests/test_invoice_service.py @@ -119,7 +119,7 @@ def _make_payment(self, invoice, journal=False, amount=False): values.update({"amount": amount}) if values: register_payments.write(values) - register_payments.create_payment() + register_payments.create_payments() def test_get_invoice_logged(self): """