trytondo-analytic_operations/sale.py

55 lines
1.9 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.pool import PoolMeta
from trytond.model import fields
from trytond.model.exceptions import ValidationError
class Sale(metaclass=PoolMeta):
'Sale Analytic Operation'
__name__ = 'sale.sale'
account_analytic = fields.Many2One('analytic_account.account',
'Analytic Account',
domain=[
('type', '=', 'normal'),
])
class SaleLine(metaclass=PoolMeta):
'Sale Line Analytic Operation'
__name__ = 'sale.line'
@fields.depends('product', 'unit', 'sale',
'_parent_sale.party', '_parent_sale.invoice_party',
'_parent_sale.account_analytic', 'analytic_accounts',
methods=['compute_taxes', 'compute_unit_price',
'on_change_with_amount'])
def on_change_product(self):
if not self.product:
return
party = None
if self.sale:
party = self.sale.invoice_party or self.sale.party
# Set taxes before unit_price to have taxes in context of sale price
self.taxes = self.compute_taxes(party)
category = self.product.sale_uom.category
if not self.unit or self.unit.category != category:
self.unit = self.product.sale_uom
self.unit_price = self.compute_unit_price()
self.type = 'line'
self.amount = self.on_change_with_amount()
self.analytic_accounts = tuple()
try:
self.analytic_accounts += (
{'root': self.sale.account_analytic.root,
'account': self.sale.account_analytic},)
except Exception as e:
raise ValidationError(f"Se produjo un error: {e}")