Skip to content

[16.0] sale_operating_unit: Issue with operating units in e-Commerce #784

@tarteo

Description

@tarteo

Origin: #600
I'm not sure if this should be a separate module like website_sale_operating_unit or it should be handled here

To reproduce:

  1. Install website_sale;
  2. create a product and publish it;
  3. make sure the sales team is set to "Website" on the website (this is by default)
  4. create a OU "Online Orders" or whatever and change the operating unit on the sales team to it
  5. go to /shop and click "Add to Cart"
  6. This error occurs:
  7. Configuration error. The Operating Unit of the sales team must match with that of the quote/sales order.
Image

I fixed it by replacing it with compute and inverse but keeping the onchanges for ux

    operating_unit_id = fields.Many2one(
        comodel_name="operating.unit",
        string="Operating Unit",
        compute="_compute_operating_unit_id",
        default=_default_operating_unit,
        store=True,
        readonly=True,
        states={"draft": [("readonly", False)], "sent": [("readonly", False)]},
    )

    @api.onchange("team_id")
    @api.depends("team_id", "team_id.operating_unit_id")
    def _compute_operating_unit_id(self):
        for sale in self.filtered(lambda s: s.state in ("draft", "sent") and s.team_id.operating_unit_id):
            sale.operating_unit_id = sale.team_id.operating_unit_id

    @api.onchange("operating_unit_id")
    def _onchange_operating_unit_id(self):
        for sale in self.filtered(
            lambda s: s.state in ("draft", "sent")
            and s.operating_unit_id != s.team_id.operating_unit_id
        ):
            sale.team_id = self.env["crm.team"].search(
                [("operating_unit_id", "=", sale.operating_unit_id.id)], limit=1
            )

This will also need a pre_init_hook, because the root user is assigned to an operating unit, which causes the sales team to be assigned an operating unit as well when the sales_team_operating_unit module is installed.

https://github.com/OCA/operating-unit/blob/16.0/operating_unit/data/operating_unit_data.xml

operating_unit_id = fields.Many2one(
"operating.unit",
default=lambda self: self.env["res.users"].operating_unit_default_get(),
)

onesteinbv/addons-oca@66582de

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions