|
| 1 | +# Copyright 2026 Domatix |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo import fields |
| 5 | + |
| 6 | +from .test_subscription_oca import TestSubscriptionOCA |
| 7 | + |
| 8 | + |
| 9 | +class TestSubscriptionInvoicingOptions(TestSubscriptionOCA): |
| 10 | + """Cover the orthogonal invoicing options that replaced ``invoicing_mode``. |
| 11 | +
|
| 12 | + The three template fields (``create_sale_order``, ``invoice_state`` and |
| 13 | + ``send_invoice``) must be independent of each other, so the behaviour is |
| 14 | + tested as a matrix rather than a single mode selector. |
| 15 | + """ |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + super().setUpClass() |
| 20 | + # Invoice on order confirmation so the sale-order path is invoiceable. |
| 21 | + (cls.product_1 | cls.product_2).write({"invoice_policy": "order"}) |
| 22 | + |
| 23 | + def _make_subscription(self, template_vals): |
| 24 | + template = self.create_sub_template(template_vals) |
| 25 | + subscription = self.create_sub( |
| 26 | + { |
| 27 | + "template_id": template.id, |
| 28 | + "date_start": fields.Date.today(), |
| 29 | + "recurring_next_date": fields.Date.today(), |
| 30 | + "journal_id": self.sale_journal.id, |
| 31 | + "in_progress": True, |
| 32 | + } |
| 33 | + ) |
| 34 | + self.create_sub_line(subscription) |
| 35 | + return subscription |
| 36 | + |
| 37 | + def test_draft_invoice_without_sale_order(self): |
| 38 | + subscription = self._make_subscription( |
| 39 | + {"create_sale_order": False, "invoice_state": "draft"} |
| 40 | + ) |
| 41 | + subscription.generate_invoice() |
| 42 | + self.assertFalse(subscription.sale_order_ids) |
| 43 | + self.assertEqual(len(subscription.invoice_ids), 1) |
| 44 | + self.assertEqual(subscription.invoice_ids.state, "draft") |
| 45 | + |
| 46 | + def test_posted_invoice_not_sent(self): |
| 47 | + subscription = self._make_subscription( |
| 48 | + { |
| 49 | + "create_sale_order": False, |
| 50 | + "invoice_state": "posted", |
| 51 | + "send_invoice": False, |
| 52 | + } |
| 53 | + ) |
| 54 | + subscription.generate_invoice() |
| 55 | + invoice = subscription.invoice_ids |
| 56 | + self.assertEqual(invoice.state, "posted") |
| 57 | + self.assertFalse(invoice.is_move_sent) |
| 58 | + |
| 59 | + def test_posted_invoice_sent(self): |
| 60 | + subscription = self._make_subscription( |
| 61 | + { |
| 62 | + "create_sale_order": False, |
| 63 | + "invoice_state": "posted", |
| 64 | + "send_invoice": True, |
| 65 | + "invoice_mail_template_id": self.env.ref( |
| 66 | + "account.email_template_edi_invoice" |
| 67 | + ).id, |
| 68 | + } |
| 69 | + ) |
| 70 | + subscription.generate_invoice() |
| 71 | + invoice = subscription.invoice_ids |
| 72 | + self.assertEqual(invoice.state, "posted") |
| 73 | + self.assertTrue(invoice.is_move_sent) |
| 74 | + |
| 75 | + def test_sale_order_with_posted_invoice(self): |
| 76 | + subscription = self._make_subscription( |
| 77 | + { |
| 78 | + "create_sale_order": True, |
| 79 | + "invoice_state": "posted", |
| 80 | + "send_invoice": False, |
| 81 | + } |
| 82 | + ) |
| 83 | + subscription.generate_invoice() |
| 84 | + order = subscription.sale_order_ids |
| 85 | + self.assertTrue(order) |
| 86 | + self.assertEqual(order.state, "sale") |
| 87 | + invoice = order.invoice_ids |
| 88 | + self.assertEqual(invoice.state, "posted") |
| 89 | + self.assertIn(order.name, invoice.invoice_origin) |
| 90 | + self.assertIn(subscription.name, invoice.invoice_origin) |
| 91 | + |
| 92 | + def test_sale_order_with_draft_invoice(self): |
| 93 | + # The axes are independent: a sale order can be generated while the |
| 94 | + # invoice is still left in draft (impossible with the old selector). |
| 95 | + subscription = self._make_subscription( |
| 96 | + { |
| 97 | + "create_sale_order": True, |
| 98 | + "invoice_state": "draft", |
| 99 | + "send_invoice": False, |
| 100 | + } |
| 101 | + ) |
| 102 | + subscription.generate_invoice() |
| 103 | + order = subscription.sale_order_ids |
| 104 | + self.assertTrue(order) |
| 105 | + invoice = order.invoice_ids |
| 106 | + self.assertEqual(invoice.state, "draft") |
0 commit comments