|
| 1 | +# Copyright 2026 Domatix - Alvaro |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo.addons.account.tests.common import AccountTestInvoicingCommon |
| 5 | + |
| 6 | + |
| 7 | +class TestSubscriptionPartnerAddresses(AccountTestInvoicingCommon): |
| 8 | + """Invoice and delivery addresses configured on the subscription must be |
| 9 | + propagated to the recurring invoices and to the generated sale orders.""" |
| 10 | + |
| 11 | + @classmethod |
| 12 | + def setUpClass(cls): |
| 13 | + super().setUpClass() |
| 14 | + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) |
| 15 | + cls.env.user.group_ids += cls.env.ref("sales_team.group_sale_manager") |
| 16 | + cls.addr_customer = cls.env["res.partner"].create({"name": "Main customer"}) |
| 17 | + cls.addr_invoice_address = cls.env["res.partner"].create( |
| 18 | + { |
| 19 | + "name": "Billing dept", |
| 20 | + "type": "invoice", |
| 21 | + "parent_id": cls.addr_customer.id, |
| 22 | + } |
| 23 | + ) |
| 24 | + cls.addr_shipping_address = cls.env["res.partner"].create( |
| 25 | + { |
| 26 | + "name": "Warehouse", |
| 27 | + "type": "delivery", |
| 28 | + "parent_id": cls.addr_customer.id, |
| 29 | + } |
| 30 | + ) |
| 31 | + # Prefixed on purpose: AccountTestInvoicingCommon < ProductCommon already |
| 32 | + # defines cls.pricelist (and cls.company/cls.product_a); avoid shadowing. |
| 33 | + cls.addr_pricelist = cls.env["product.pricelist"].create( |
| 34 | + {"name": "Addr PL", "currency_id": cls.company.currency_id.id} |
| 35 | + ) |
| 36 | + cls.addr_template = cls.env["sale.subscription.template"].create( |
| 37 | + { |
| 38 | + "name": "Addr template", |
| 39 | + "code": "ADDR", |
| 40 | + "recurring_rule_type": "months", |
| 41 | + "recurring_interval": 1, |
| 42 | + "invoicing_mode": "draft", |
| 43 | + } |
| 44 | + ) |
| 45 | + cls.addr_stage = cls.env["sale.subscription.stage"].search( |
| 46 | + [("type", "=", "in_progress")], limit=1 |
| 47 | + ) |
| 48 | + |
| 49 | + def _new_subscription(self, partner): |
| 50 | + sub = self.env["sale.subscription"].create( |
| 51 | + { |
| 52 | + "partner_id": partner.id, |
| 53 | + "template_id": self.addr_template.id, |
| 54 | + "pricelist_id": self.addr_pricelist.id, |
| 55 | + "stage_id": self.addr_stage.id, |
| 56 | + } |
| 57 | + ) |
| 58 | + self.env["sale.subscription.line"].create( |
| 59 | + { |
| 60 | + "sale_subscription_id": sub.id, |
| 61 | + "product_id": self.product_a.id, |
| 62 | + "product_uom_qty": 1.0, |
| 63 | + "price_unit": 100.0, |
| 64 | + "tax_ids": [(6, 0, [])], |
| 65 | + } |
| 66 | + ) |
| 67 | + return sub |
| 68 | + |
| 69 | + def test_addresses_default_from_partner_children(self): |
| 70 | + sub = self._new_subscription(self.addr_customer) |
| 71 | + self.assertEqual(sub.partner_invoice_id, self.addr_invoice_address) |
| 72 | + self.assertEqual(sub.partner_shipping_id, self.addr_shipping_address) |
| 73 | + |
| 74 | + def test_addresses_default_to_partner_without_children(self): |
| 75 | + plain = self.env["res.partner"].create({"name": "No child"}) |
| 76 | + sub = self._new_subscription(plain) |
| 77 | + self.assertEqual(sub.partner_invoice_id, plain) |
| 78 | + self.assertEqual(sub.partner_shipping_id, plain) |
| 79 | + |
| 80 | + def test_addresses_empty_without_partner(self): |
| 81 | + # A record without partner yet (e.g. while filling the form) must |
| 82 | + # keep both addresses empty instead of crashing. |
| 83 | + sub = self.env["sale.subscription"].new({}) |
| 84 | + self.assertFalse(sub.partner_invoice_id) |
| 85 | + self.assertFalse(sub.partner_shipping_id) |
| 86 | + |
| 87 | + def test_invoice_uses_subscription_addresses(self): |
| 88 | + sub = self._new_subscription(self.addr_customer) |
| 89 | + invoice = sub.create_invoice() |
| 90 | + # The invoice is addressed to the invoice address, with its delivery one. |
| 91 | + self.assertEqual(invoice.partner_id, self.addr_invoice_address) |
| 92 | + self.assertEqual(invoice.partner_shipping_id, self.addr_shipping_address) |
| 93 | + |
| 94 | + def test_invoice_commercial_partner_rolls_up_to_customer(self): |
| 95 | + # Addressing the invoice to a child invoice contact must not move the |
| 96 | + # receivable away from the contracting company. |
| 97 | + sub = self._new_subscription(self.addr_customer) |
| 98 | + invoice = sub.create_invoice() |
| 99 | + self.assertEqual(invoice.commercial_partner_id, self.addr_customer) |
| 100 | + |
| 101 | + def test_invoice_follows_manual_address_override(self): |
| 102 | + other_invoice = self.env["res.partner"].create( |
| 103 | + { |
| 104 | + "name": "Alt billing", |
| 105 | + "type": "invoice", |
| 106 | + "parent_id": self.addr_customer.id, |
| 107 | + } |
| 108 | + ) |
| 109 | + sub = self._new_subscription(self.addr_customer) |
| 110 | + sub.partner_invoice_id = other_invoice |
| 111 | + invoice = sub.create_invoice() |
| 112 | + self.assertEqual(invoice.partner_id, other_invoice) |
| 113 | + |
| 114 | + def test_sale_order_carries_addresses(self): |
| 115 | + self.addr_template.invoicing_mode = "sale_and_invoice" |
| 116 | + sub = self._new_subscription(self.addr_customer) |
| 117 | + order = sub.create_sale_order() |
| 118 | + self.assertEqual(order.partner_id, self.addr_customer) |
| 119 | + self.assertEqual(order.partner_invoice_id, self.addr_invoice_address) |
| 120 | + self.assertEqual(order.partner_shipping_id, self.addr_shipping_address) |
0 commit comments