operating-unit icon indicating copy to clipboard operation
operating-unit copied to clipboard

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

Open tarteo opened this issue 5 months ago • 1 comments

Origin: https://github.com/OCA/operating-unit/pull/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

https://github.com/OCA/operating-unit/blob/77d307addc884574aedd5010b9d7e117d827bfde/sales_team_operating_unit/models/crm_team.py#L13-L16

https://github.com/onesteinbv/addons-oca/commit/66582dee6d5a190424267d3c3b683fb42c8073fd

tarteo avatar Jul 15 '25 12:07 tarteo

I am ok with your fix, if the user can change the OU manually in the SO and no dependency is added to the module, then I think we can do the fix in the base module itself.

AaronHForgeFlow avatar Jul 15 '25 15:07 AaronHForgeFlow