diff --git a/delivery_carrier_partner_account/models/sales_order.py b/delivery_carrier_partner_account/models/sales_order.py index 5f31c6d..be8ef38 100644 --- a/delivery_carrier_partner_account/models/sales_order.py +++ b/delivery_carrier_partner_account/models/sales_order.py @@ -10,7 +10,7 @@ class SalesOrder(models.Model): recipient_id = fields.Many2one( comodel_name="res.partner", - related="partner_id", + related="partner_shipping_id", ) sender_id = fields.Many2one( comodel_name="res.partner", diff --git a/delivery_carrier_partner_account/tests/test_sale_order.py b/delivery_carrier_partner_account/tests/test_sale_order.py index bcb6337..c8c1a90 100644 --- a/delivery_carrier_partner_account/tests/test_sale_order.py +++ b/delivery_carrier_partner_account/tests/test_sale_order.py @@ -1,5 +1,6 @@ from .test_carrier_account_common import TestCarrierAccountCommon from odoo.tests import Form +from odoo import Command class TestSalesOrder(TestCarrierAccountCommon): @@ -44,6 +45,37 @@ class TestSalesOrder(TestCarrierAccountCommon): f"{self.delivery_carrier_1.name} [THIRD PARTY] #{self.third_party_account_1.account_number}", ) + def test_sale_order_shipping_to_third_party_collect(self): + # We create an order where we are shipping to a third party and the billing mode is collect + # This should work, but was previously failing with an error that the carrier account did not belong to the recipient + order = self.env["sale.order"].create( + { + "partner_id": self.client_partner.id, + "partner_shipping_id": self.random_partner.id, + "order_line": [ + Command.create( + {"product_id": self.env.ref("product.product_product_4").id} + ) + ], + } + ) + # Confirming the order is important because the sender and recipient need to be in sync + # on the sale order and delivery order. This was the point of failure previously. + order.action_confirm() + + wiz = self._get_shipping_wizard(order) + with Form(wiz) as form: + form.carrier_id = self.delivery_carrier_1 + form.delivery_billing_mode = "collect" + self.assertIn(self.third_party_account_1, wiz.valid_carrier_account_ids) + wiz.button_confirm() + + self.assertEqual( + order.carrier_account_id.partner_id, + self.random_partner, + "The carrier account should belong to the recipient (sale order shipping address)", + ) + def _get_shipping_wizard(self, order): wizard_action = order.action_open_delivery_wizard() return ( diff --git a/delivery_carrier_partner_account/wizard/choose_delivery_carrier.py b/delivery_carrier_partner_account/wizard/choose_delivery_carrier.py index 52bcd0e..3804885 100644 --- a/delivery_carrier_partner_account/wizard/choose_delivery_carrier.py +++ b/delivery_carrier_partner_account/wizard/choose_delivery_carrier.py @@ -8,7 +8,7 @@ class ChooseDeliveryCarrier(models.TransientModel): _name = "choose.delivery.carrier" sender_id = fields.Many2one(related="company_id.partner_id") - recipient_id = fields.Many2one(related="partner_id") + recipient_id = fields.Many2one(related="order_id.partner_shipping_id") def button_confirm(self): vals = {}