add field function last cost

This commit is contained in:
wilsongomez 2022-05-06 10:46:53 -05:00
parent 82fd0bb718
commit b3cfd02b34
5 changed files with 50 additions and 1 deletions

View File

@ -3,10 +3,12 @@
from trytond.pool import Pool
from . import purchase
from . import product
def register():
Pool.register(
product.Product,
purchase.Configuration,
purchase.Purchase,
purchase.Line,

24
product.py Normal file
View File

@ -0,0 +1,24 @@
# 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 decimal import Decimal
from trytond.model import fields
from trytond.pool import PoolMeta, Pool
from trytond.modules.product import price_digits
class Product(metaclass=PoolMeta):
__name__ = 'product.product'
last_cost = fields.Function(fields.Numeric('Last Cost', digits=price_digits,
help='Last purchase price'), 'get_last_cost')
def get_last_cost(self, name=None):
res = self.cost_price
InvoiceLine = Pool().get('account.invoice.line')
products = InvoiceLine.search_read([
('product', '=', self.id),
('invoice.state', 'in', ['posted', 'paid']),
('invoice.type', '=', 'in'),
], fields_names=['unit_price'], order=[('invoice.invoice_date', 'DESC')], limit=1)
if products:
res = products[0]['unit_price']
return res

13
product.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<tryton>
<data>
<record model="ir.ui.view" id="product_view_form_simple">
<field name="model">product.product</field>
<field name="inherit" ref="product.product_view_form_simple"/>
<field name="name">product_form_simple</field>
</record>
</data>
</tryton>

View File

@ -1,8 +1,9 @@
[tryton]
version=6.0.3
version=6.0.4
depends:
product
purchase
account_col
xml:
purchase.xml
product.xml

View File

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<data>
<xpath expr="/form/field[@name='cost_price']" position="after">
<label name="last_cost"/>
<field name="last_cost"/>
</xpath>
</data>