Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Margins in Sales Orders

Entity: sale.order

Compute Method: _compute_margin

Inputs Method Outputs
  • order_line
  • amount_untaxed
  • margin
  • id
if not all(self._ids):
            for order in self:
                order.margin = sum(order.order_line.mapped("margin"))
                order.margin_percent = order.amount_untaxed and order.margin / order.amount_untaxed
        else:
            # On batch records recomputation (e.g. at install), compute the margins
            # with a single read_group query for better performance.
            # This isn't done in an onchange environment because (part of) the data
            # may not be stored in database (new records or unsaved modifications).
            grouped_order_lines_data = self.env["sale.order.line"]._read_group(
                [("order_id", "in", self.ids)], ["order_id"], ["margin:sum"]
            )
            mapped_data = {order.id: margin for order, margin in grouped_order_lines_data}
            for order in self:
                order.margin = mapped_data.get(order.id, 0.0)
                order.margin_percent = order.amount_untaxed and order.margin / order.amount_untaxed

Back to entity