mirror of
https://github.com/bringout/oca-ocb-sale.git
synced 2026-04-26 15:32:00 +02:00
19.0 vanilla
This commit is contained in:
parent
79f83631d5
commit
73afc09215
6267 changed files with 1534193 additions and 1130106 deletions
|
|
@ -8,7 +8,7 @@ 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")
|
||||
margin_percent = fields.Float("Margin (%)", compute='_compute_margin', store=True, aggregator="avg")
|
||||
|
||||
@api.depends('order_line.margin', 'amount_untaxed')
|
||||
def _compute_margin(self):
|
||||
|
|
@ -21,11 +21,11 @@ class SaleOrder(models.Model):
|
|||
# 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(
|
||||
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}
|
||||
], ['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
|
||||
|
|
|
|||
|
|
@ -9,52 +9,40 @@ class SaleOrderLine(models.Model):
|
|||
|
||||
margin = fields.Float(
|
||||
"Margin", compute='_compute_margin',
|
||||
digits='Product Price', store=True, groups="base.group_user", precompute=True)
|
||||
min_display_digits='Product Price', store=True, groups="base.group_user", precompute=True)
|
||||
margin_percent = fields.Float(
|
||||
"Margin (%)", compute='_compute_margin', store=True, groups="base.group_user", precompute=True)
|
||||
purchase_price = fields.Float(
|
||||
string="Cost", compute="_compute_purchase_price",
|
||||
digits='Product Price', store=True, readonly=False, copy=False, precompute=True,
|
||||
min_display_digits='Product Price', store=True, readonly=False, copy=False, precompute=True,
|
||||
groups="base.group_user")
|
||||
|
||||
@api.depends('product_id', 'company_id', 'currency_id', 'product_uom')
|
||||
@api.depends('product_id', 'company_id', 'currency_id', 'product_uom_id')
|
||||
def _compute_purchase_price(self):
|
||||
for line in self:
|
||||
if not line.product_id:
|
||||
line.purchase_price = 0.0
|
||||
continue
|
||||
line = line.with_company(line.company_id)
|
||||
product_cost = line.product_id.standard_price
|
||||
line.purchase_price = line._convert_price(product_cost, line.product_id.uom_id)
|
||||
|
||||
# Convert the cost to the line UoM
|
||||
product_cost = line.product_id.uom_id._compute_price(
|
||||
line.product_id.standard_price,
|
||||
line.product_uom_id,
|
||||
)
|
||||
|
||||
line.purchase_price = line._convert_to_sol_currency(
|
||||
product_cost,
|
||||
line.product_id.cost_currency_id)
|
||||
|
||||
@api.depends('price_subtotal', 'product_uom_qty', 'purchase_price')
|
||||
def _compute_margin(self):
|
||||
for line in self:
|
||||
line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty)
|
||||
line.margin_percent = line.price_subtotal and line.margin/line.price_subtotal
|
||||
|
||||
def _convert_price(self, product_cost, from_uom):
|
||||
self.ensure_one()
|
||||
if not product_cost:
|
||||
# If the standard_price is 0
|
||||
# Avoid unnecessary computations
|
||||
# and currency conversions
|
||||
if not self.purchase_price:
|
||||
return product_cost
|
||||
from_currency = self.product_id.cost_currency_id
|
||||
to_cur = self.currency_id or self.order_id.currency_id
|
||||
to_uom = self.product_uom
|
||||
if to_uom and to_uom != from_uom:
|
||||
product_cost = from_uom._compute_price(
|
||||
product_cost,
|
||||
to_uom,
|
||||
)
|
||||
return from_currency._convert(
|
||||
from_amount=product_cost,
|
||||
to_currency=to_cur,
|
||||
company=self.company_id or self.env.company,
|
||||
date=self.order_id.date_order or fields.Date.today(),
|
||||
round=False,
|
||||
) if to_cur and product_cost else product_cost
|
||||
# The pricelist may not have been set, therefore no conversion
|
||||
# is needed because we don't know the target currency..
|
||||
# Find alternative calculation when line is added to order from delivery
|
||||
if line.qty_delivered and not line.product_uom_qty:
|
||||
calculated_subtotal = line.price_unit * line.qty_delivered
|
||||
line.margin = calculated_subtotal - (line.purchase_price * line.qty_delivered)
|
||||
line.margin_percent = calculated_subtotal and line.margin / calculated_subtotal
|
||||
else:
|
||||
line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty)
|
||||
line.margin_percent = line.price_subtotal and line.margin / line.price_subtotal
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue