mirror of
https://github.com/bringout/oca-ocb-sale.git
synced 2026-04-27 12:52:07 +02:00
Initial commit: Sale packages
This commit is contained in:
commit
14e3d26998
6469 changed files with 2479670 additions and 0 deletions
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
_inherit = "sale.order"
|
||||
|
||||
margin = fields.Monetary("Margin", compute='_compute_margin', store=True)
|
||||
margin_percent = fields.Float("Margin (%)", compute='_compute_margin', store=True, group_operator="avg")
|
||||
|
||||
@api.depends('order_line.margin', 'amount_untaxed')
|
||||
def _compute_margin(self):
|
||||
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),
|
||||
], ['margin', 'order_id'], ['order_id'])
|
||||
mapped_data = {m['order_id'][0]: m['margin'] for m 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue