diff --git a/addons/event_booth_sale/models/sale_order_line.py b/addons/event_booth_sale/models/sale_order_line.py index 649cdd8122f..bd3d3c88f1b 100644 --- a/addons/event_booth_sale/models/sale_order_line.py +++ b/addons/event_booth_sale/models/sale_order_line.py @@ -80,9 +80,9 @@ class SaleOrderLine(models.Model): raise ValidationError( _('The following booths are unavailable, please remove them to continue : %(booth_names)s', booth_names=''.join('\n\t- %s' % booth.display_name for booth in unavailable))) - so_line.event_booth_registration_ids.action_confirm() + so_line.event_booth_registration_ids.sudo().action_confirm() if so_line.event_booth_ids and set_paid: - so_line.event_booth_ids.action_set_paid() + so_line.event_booth_ids.sudo().action_set_paid() return True def _get_sale_order_line_multiline_description_sale(self): diff --git a/addons/event_booth_sale/tests/test_event_booth_sale.py b/addons/event_booth_sale/tests/test_event_booth_sale.py index 02732f4b946..69d24233635 100644 --- a/addons/event_booth_sale/tests/test_event_booth_sale.py +++ b/addons/event_booth_sale/tests/test_event_booth_sale.py @@ -6,15 +6,15 @@ from datetime import datetime, timedelta from odoo import Command, fields from odoo.addons.event_booth_sale.tests.common import TestEventBoothSaleCommon from odoo.addons.sales_team.tests.common import TestSalesCommon -from odoo.tests.common import users +from odoo.tests.common import tagged, users from odoo.tools import float_compare -class TestEventBoothSale(TestEventBoothSaleCommon, TestSalesCommon): +class TestEventBoothSaleWData(TestEventBoothSaleCommon, TestSalesCommon): @classmethod def setUpClass(cls): - super(TestEventBoothSale, cls).setUpClass() + super(TestEventBoothSaleWData, cls).setUpClass() cls.event_0 = cls.env['event.event'].create({ 'name': 'TestEvent', @@ -24,17 +24,17 @@ class TestEventBoothSale(TestEventBoothSaleCommon, TestSalesCommon): 'date_tz': 'Europe/Brussels', }) - cls.booth_1 = cls.env['event.booth'].create({ - 'name': 'Test Booth 1', - 'booth_category_id': cls.event_booth_category_1.id, - 'event_id': cls.event_0.id, - }) - - cls.booth_2 = cls.env['event.booth'].create({ - 'name': 'Test Booth 2', - 'booth_category_id': cls.event_booth_category_1.id, - 'event_id': cls.event_0.id, - }) + cls.booth_1, cls.booth_2 = cls.env['event.booth'].create([ + { + 'name': 'Test Booth 1', + 'booth_category_id': cls.event_booth_category_1.id, + 'event_id': cls.event_0.id, + }, { + 'name': 'Test Booth 2', + 'booth_category_id': cls.event_booth_category_1.id, + 'event_id': cls.event_0.id, + } + ]) cls.tax_10 = cls.env['account.tax'].sudo().create({ 'name': 'Tax 10', @@ -45,9 +45,13 @@ class TestEventBoothSale(TestEventBoothSaleCommon, TestSalesCommon): 'name': 'Test Pricelist', }) + cls.event_booth_product.taxes_id = cls.tax_10 + + +class TestEventBoothSale(TestEventBoothSaleWData): + @users('user_sales_salesman') def test_event_booth_prices_with_sale_order(self): - self.event_booth_product.taxes_id = self.tax_10 sale_order = self.env['sale.order'].create({ 'partner_id': self.event_customer.id, 'pricelist_id': self.pricelist.id, @@ -83,3 +87,88 @@ class TestEventBoothSale(TestEventBoothSaleCommon, TestSalesCommon): "Untaxed amount should be the sum of the booths prices ($200.0).") self.assertEqual(float_compare(sale_order.amount_total, 220.0, precision_rounding=0.1), 0, "Total amount should be the sum of the booths prices with 10% taxes ($200.0 + $20.0).") + + # Confirm the SO. + sale_order.action_confirm() + + for booth in self.booth_1 + self.booth_2: + self.assertEqual( + booth.sale_order_id.id, sale_order.id, + "Booth sale order should be the same as the original sale order.") + self.assertEqual( + booth.sale_order_line_id.id, sale_order.order_line[0].id, + "Booth sale order line should the same as the order line in the original sale order.") + self.assertEqual( + booth.partner_id.id, self.event_customer.id, + "Booth partner should be the same as sale order customer.") + self.assertEqual( + booth.contact_email, self.event_customer.email, + "Booth contact email should be the same as sale order customer email.") + self.assertEqual( + booth.contact_name, self.event_customer.name, + "Booth contact name should be the same as sale order customer name.") + self.assertEqual( + booth.contact_mobile, self.event_customer.mobile, + "Booth contact mobile should be the same as sale order customer mobile.") + self.assertEqual( + booth.contact_phone, self.event_customer.phone, + "Booth contact phone should be the same as sale order customer phone.") + self.assertEqual( + booth.state, 'unavailable', + "Booth should not be available anymore.") + + +@tagged('post_install', '-at_install') +class TestEventBoothSaleInvoice(TestEventBoothSaleWData): + + @classmethod + def setUpClass(cls): + super(TestEventBoothSaleInvoice, cls).setUpClass() + + # Add group `group_account_invoice` to user_sales_salesman to allow to pay the invoice + cls.user_sales_salesman.groups_id += cls.env.ref('account.group_account_invoice') + + @users('user_sales_salesman') + def test_event_booth_with_invoice(self): + booth = self.booth_1.with_env(self.env) + self.assertEqual(booth.state, 'available') + + sale_order = self.env['sale.order'].create({ + 'partner_id': self.event_customer.id, + 'pricelist_id': self.pricelist.id, + 'order_line': [ + Command.create({ + 'product_id': self.event_booth_product.id, + 'event_id': self.event_0.id, + 'event_booth_pending_ids': booth.ids + }) + ] + }) + sale_order.action_confirm() + self.assertEqual(booth.state, 'unavailable') + self.assertFalse(booth.is_paid) + + # Create and check that the invoice was created + invoice = sale_order._create_invoices() + self.assertEqual(len(sale_order.invoice_ids), 1, "Invoice not created.") + + # Confirm the invoice and check SO invoice status + invoice.action_post() + self.assertEqual( + sale_order.invoice_status, 'invoiced', + f"Order is in '{sale_order.invoice_status}' status while it should be 'invoiced'.") + # Pay the invoice. + journal = self.env['account.journal'].search([('type', '=', 'cash'), ('company_id', '=', sale_order.company_id.id)], limit=1) + + register_payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=invoice.ids).create({ + 'journal_id': journal.id, + }) + register_payments._create_payments() + + # Check the invoice payment state after paying the invoice + in_payment_state = invoice._get_invoice_in_payment_state() + self.assertEqual(invoice.payment_state, in_payment_state, + f"Invoice payment is in '{invoice.payment_state}' status while it should be '{in_payment_state}'.") + + self.assertEqual(booth.state, 'unavailable') + self.assertTrue(booth.is_paid)