diff --git a/account_invoice_base_invoicing_mode/models/account_invoice.py b/account_invoice_base_invoicing_mode/models/account_invoice.py index ff4dd27039c..7e548bf20ad 100644 --- a/account_invoice_base_invoicing_mode/models/account_invoice.py +++ b/account_invoice_base_invoicing_mode/models/account_invoice.py @@ -12,4 +12,4 @@ class AccountMove(models.Model): @job(default_channel="root.invoice_validation") @related_action(action="related_action_open_invoice") def _validate_invoice(self): - return self.action_post() + return self.sudo().action_post() diff --git a/account_invoice_base_invoicing_mode/models/sale_order.py b/account_invoice_base_invoicing_mode/models/sale_order.py index f22ee4cfefa..aa0f5832296 100644 --- a/account_invoice_base_invoicing_mode/models/sale_order.py +++ b/account_invoice_base_invoicing_mode/models/sale_order.py @@ -7,6 +7,4 @@ class SaleOrder(models.Model): _inherit = "sale.order" - invoicing_mode = fields.Selection( - related="partner_invoice_id.invoicing_mode", readonly=True - ) + invoicing_mode = fields.Selection(related="partner_invoice_id.invoicing_mode") diff --git a/account_invoice_mode_at_shipping/models/stock_picking.py b/account_invoice_mode_at_shipping/models/stock_picking.py index 45289dd0912..24a2c8a9e26 100644 --- a/account_invoice_mode_at_shipping/models/stock_picking.py +++ b/account_invoice_mode_at_shipping/models/stock_picking.py @@ -11,22 +11,26 @@ class StockPicking(models.Model): def action_done(self): res = super().action_done() - picking_to_invoice = self.filtered( - lambda r: r.sale_id.partner_invoice_id.invoicing_mode == "at_shipping" - and r.picking_type_code == "outgoing" - ) - for picking in picking_to_invoice: - picking.with_delay()._invoicing_at_shipping() + + for picking in self: + if picking._invoice_at_shipping(): + picking.with_delay()._invoicing_at_shipping() return res + def _invoice_at_shipping(self): + """Check if picking must be invoiced at shipping.""" + self.ensure_one() + return ( + self.picking_type_code == "outgoing" + and self.sale_id.partner_invoice_id.invoicing_mode == "at_shipping" + ) + @job(default_channel="root.invoice_at_shipping") def _invoicing_at_shipping(self): self.ensure_one() - sale_order_ids = self._get_sales_order_to_invoice() - partner = sale_order_ids.partner_invoice_id - invoices = sale_order_ids._create_invoices( - grouped=partner.one_invoice_per_order - ) + sales_order = self._get_sales_order_to_invoice() + partner = sales_order.partner_invoice_id + invoices = sales_order._create_invoices(grouped=partner.one_invoice_per_order) for invoice in invoices: invoice.with_delay()._validate_invoice() return invoices diff --git a/account_invoice_mode_at_shipping/tests/test_invoice_mode_at_shipping.py b/account_invoice_mode_at_shipping/tests/test_invoice_mode_at_shipping.py index f93f4702186..4466513d65e 100644 --- a/account_invoice_mode_at_shipping/tests/test_invoice_mode_at_shipping.py +++ b/account_invoice_mode_at_shipping/tests/test_invoice_mode_at_shipping.py @@ -8,6 +8,7 @@ class TestInvoiceModeAtShipping(SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) cls.partner = cls.env.ref("base.res_partner_1") cls.product = cls.env.ref("product.product_delivery_01") cls.so1 = cls.env["sale.order"].create( @@ -66,7 +67,7 @@ def test_invoice_created_at_shipping(self): self.assertEqual(self.so1.invoice_ids.state, "posted") def test_invoice_not_created_at_shipping(self): - """Check that an invoice is created when goods are shipped.""" + """Check that an invoice is not created when goods are shipped.""" self.partner.invoicing_mode = "standard" self.so1.action_confirm() for picking in self.so1.picking_ids: diff --git a/account_invoice_mode_monthly/tests/test_invoice_mode_monthly.py b/account_invoice_mode_monthly/tests/test_invoice_mode_monthly.py index 949fb35bf11..1096cbd59c9 100644 --- a/account_invoice_mode_monthly/tests/test_invoice_mode_monthly.py +++ b/account_invoice_mode_monthly/tests/test_invoice_mode_monthly.py @@ -9,6 +9,7 @@ class TestInvoiceModeMonthly(SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) cls.SaleOrder = cls.env["sale.order"] cls.partner = cls.env.ref("base.res_partner_1") cls.partner.invoicing_mode = "monthly"