trytondo-analytic_operations/purchase.py

70 lines
2.3 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.pyson import Eval
from trytond.model.exceptions import ValidationError
class Purchase(metaclass=PoolMeta):
'Purchase Analytic Operation'
__name__ = 'purchase.purchase'
_states = {
'readonly': Eval('state') != 'draft',
}
account_analytic = fields.Many2One(
'analytic_account.account', 'Analytic Account', required=True,
domain=[('type', '=', 'normal')],
states=_states)
class PurchaseLine(metaclass=PoolMeta):
'Purchase Line Analytic Operation'
__name__ = 'purchase.line'
@fields.depends(
'product', 'unit', 'purchase',
'_parent_purchase.party', '_parent_purchase.invoice_party',
'_parent_purchase.account_analytic', 'product_supplier',
'analytic_accounts',
methods=['compute_taxes', 'compute_unit_price',
'_get_product_supplier_pattern'])
def on_change_product(self):
if not self.product:
return
party = None
if self.purchase:
party = self.purchase.invoice_party or self.purchase.party
# Set taxes before unit_price to have taxes in context of purchase
# price
self.taxes = self.compute_taxes(party)
category = self.product.purchase_uom.category
if not self.unit or self.unit.category != category:
self.unit = self.product.purchase_uom
product_suppliers = list(self.product.product_suppliers_used(
**self._get_product_supplier_pattern()))
if len(product_suppliers) == 1:
self.product_supplier, = product_suppliers
elif (self.product_supplier
and self.product_supplier not in product_suppliers):
self.product_supplier = None
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.purchase.account_analytic.root,
'account': self.purchase.account_analytic},)
except Exception as e:
raise ValidationError(f"Se produjo un error: {e}")