asset: produkt, einheit + test
This commit is contained in:
parent
0d8ef5081e
commit
13007f30c8
9 changed files with 253 additions and 1 deletions
57
asset.py
57
asset.py
|
@ -8,6 +8,7 @@ from trytond.exceptions import UserError
|
|||
from trytond.i18n import gettext
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
from trytond.pyson import Eval, Bool
|
||||
|
||||
|
||||
class Asset(ModelSQL, ModelView):
|
||||
|
@ -16,6 +17,21 @@ class Asset(ModelSQL, ModelView):
|
|||
|
||||
company = fields.Many2One(string='Company', model_name='company.company',
|
||||
required=True, ondelete="RESTRICT")
|
||||
product = fields.Many2One(string='Product', required=True,
|
||||
model_name='product.product', ondelete='RESTRICT',
|
||||
domain=[('type', '=', 'assets')])
|
||||
product_uom = fields.Function(fields.Many2One(string='UOM Category',
|
||||
readonly=True, model_name='product.uom.category',
|
||||
help='Category of unit on the product.'),
|
||||
'on_change_with_product_uom')
|
||||
uom = fields.Many2One(string='UOM', required=True,
|
||||
model_name='product.uom', ondelete='RESTRICT',
|
||||
states={
|
||||
'readonly': ~Bool(Eval('product')),
|
||||
},
|
||||
domain=[
|
||||
('category', '=', Eval('product_uom')),
|
||||
], depends=['product_uom', 'product'])
|
||||
|
||||
company_currency = fields.Function(fields.Many2One(readonly=True,
|
||||
string='Company Currency', states={'invisible': True},
|
||||
|
@ -46,6 +62,35 @@ class Asset(ModelSQL, ModelView):
|
|||
def default_company():
|
||||
return Transaction().context.get('company') or None
|
||||
|
||||
@classmethod
|
||||
def default_currency_digits(cls):
|
||||
""" default: 2
|
||||
"""
|
||||
return 2
|
||||
|
||||
@fields.depends('product', 'uom')
|
||||
def on_change_product(self):
|
||||
""" update unit by product
|
||||
"""
|
||||
if self.product:
|
||||
self.uom = self.product.default_uom
|
||||
return
|
||||
self.uom = None
|
||||
|
||||
@fields.depends('currency', 'currency_digits')
|
||||
def on_change_currency(self):
|
||||
""" update currency_digits by value on currency
|
||||
"""
|
||||
if self.currency:
|
||||
self.currency_digits = self.currency.digits
|
||||
|
||||
@fields.depends('product')
|
||||
def on_change_with_product_uom(self, name=None):
|
||||
""" get category of product-uom
|
||||
"""
|
||||
if self.product:
|
||||
return self.product.default_uom.category.id
|
||||
|
||||
@fields.depends('currency')
|
||||
def on_change_with_currency_digits(self, name=None):
|
||||
""" currency of cashbook
|
||||
|
@ -65,4 +110,16 @@ class Asset(ModelSQL, ModelView):
|
|||
if self.company.currency.id != self.currency.id:
|
||||
return self.company.currency.id
|
||||
|
||||
def get_rec_name(self, name):
|
||||
if self.product:
|
||||
return self.product.rec_name
|
||||
else:
|
||||
return self.id
|
||||
|
||||
@classmethod
|
||||
def search_rec_name(cls, name, clause):
|
||||
""" search in rec_name
|
||||
"""
|
||||
return [('product.rec_name',) + tuple(clause[1:])]
|
||||
|
||||
# end Asset
|
||||
|
|
|
@ -45,7 +45,7 @@ full copyright notices and license terms. -->
|
|||
<field name="perm_delete" eval="False"/>
|
||||
</record>
|
||||
<!-- group_investment: read -->
|
||||
<record model="ir.model.access" id="access_asset-group_cashbook_admin">
|
||||
<record model="ir.model.access" id="access_asset-group_investment">
|
||||
<field name="model" search="[('model', '=', 'investment.asset')]"/>
|
||||
<field name="group" ref="group_investment"/>
|
||||
<field name="perm_read" eval="True"/>
|
||||
|
|
19
locale/de.po
19
locale/de.po
|
@ -42,6 +42,10 @@ msgctxt "model:investment.asset,name:"
|
|||
msgid "Asset"
|
||||
msgstr "Vermögenswert"
|
||||
|
||||
msgctxt "view:investment.asset:"
|
||||
msgid "Currency and Units"
|
||||
msgstr "Währung und Einheiten"
|
||||
|
||||
msgctxt "field:investment.asset,company:"
|
||||
msgid "Company"
|
||||
msgstr "Unternehmen"
|
||||
|
@ -62,4 +66,19 @@ msgctxt "field:investment.asset,currency_digits:"
|
|||
msgid "Currency Digits"
|
||||
msgstr "Nachkommastellen Währung"
|
||||
|
||||
msgctxt "field:investment.asset,product:"
|
||||
msgid "Product"
|
||||
msgstr "Artikel"
|
||||
|
||||
msgctxt "field:investment.asset,product_uom:"
|
||||
msgid "UOM Category"
|
||||
msgstr "Einheit-Kategorie"
|
||||
|
||||
msgctxt "help:investment.asset,product_uom:"
|
||||
msgid "Category of unit on the product."
|
||||
msgstr "Kategorie der Einheit am Produkt."
|
||||
|
||||
msgctxt "field:investment.asset,uom:"
|
||||
msgid "UOM"
|
||||
msgstr "Einheit"
|
||||
|
||||
|
|
20
locale/en.po
20
locale/en.po
|
@ -26,6 +26,10 @@ msgctxt "model:investment.asset,name:"
|
|||
msgid "Asset"
|
||||
msgstr "Asset"
|
||||
|
||||
msgctxt "view:investment.asset:"
|
||||
msgid "Currency and Units"
|
||||
msgstr "Currency and Units"
|
||||
|
||||
msgctxt "field:investment.asset,company:"
|
||||
msgid "Company"
|
||||
msgstr "Company"
|
||||
|
@ -46,3 +50,19 @@ msgctxt "field:investment.asset,currency_digits:"
|
|||
msgid "Currency Digits"
|
||||
msgstr "Currency Digits"
|
||||
|
||||
msgctxt "field:investment.asset,product:"
|
||||
msgid "Product"
|
||||
msgstr "Product"
|
||||
|
||||
msgctxt "field:investment.asset,product_uom:"
|
||||
msgid "UOM Category"
|
||||
msgstr "UOM Category"
|
||||
|
||||
msgctxt "help:investment.asset,product_uom:"
|
||||
msgid "Category of unit on the product."
|
||||
msgstr "Category of unit on the product."
|
||||
|
||||
msgctxt "field:investment.asset,uom:"
|
||||
msgid "UOM"
|
||||
msgstr "UOM"
|
||||
|
||||
|
|
24
tests/__init__.py
Normal file
24
tests/__init__.py
Normal 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.
|
||||
|
||||
import trytond.tests.test_tryton
|
||||
import unittest
|
||||
|
||||
from trytond.modules.investment.tests.test_asset import AssetTestCase
|
||||
|
||||
|
||||
__all__ = ['suite']
|
||||
|
||||
|
||||
class InvestmentTestCase(\
|
||||
AssetTestCase,\
|
||||
):
|
||||
'Test investment module'
|
||||
module = 'investment'
|
||||
|
||||
# end InvestmentTestCase
|
||||
|
||||
def suite():
|
||||
suite = trytond.tests.test_tryton.suite()
|
||||
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(InvestmentTestCase))
|
||||
return suite
|
117
tests/test_asset.py
Normal file
117
tests/test_asset.py
Normal file
|
@ -0,0 +1,117 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# This file is part of the cashbook-module from m-ds for Tryton.
|
||||
# The COPYRIGHT file at the top level of this repository contains the
|
||||
# full copyright notices and license terms.
|
||||
|
||||
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
|
||||
from trytond.pool import Pool
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.exceptions import UserError
|
||||
from trytond.modules.company.tests import create_company
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
class AssetTestCase(ModuleTestCase):
|
||||
'Test asset module'
|
||||
module = 'investment'
|
||||
|
||||
def prep_asset_company(self):
|
||||
""" get/create company
|
||||
"""
|
||||
Company = Pool().get('company.company')
|
||||
|
||||
company = Company.search([])
|
||||
if len(company) > 0:
|
||||
company = company[0]
|
||||
else :
|
||||
company = create_company(name='m-ds')
|
||||
return company
|
||||
|
||||
def prep_asset_product(self, name='Product 1', description=None, unit='u', unit_name='Units'):
|
||||
""" create product
|
||||
"""
|
||||
pool = Pool()
|
||||
Product = pool.get('product.template')
|
||||
Uom = pool.get('product.uom')
|
||||
|
||||
uom, = Uom.search([('symbol', '=', unit)])
|
||||
prod_templ, = Product.create([{
|
||||
'name': name,
|
||||
'type': 'assets',
|
||||
'list_price': Decimal('1.0'),
|
||||
'default_uom': uom.id,
|
||||
'products': [('create', [{
|
||||
'description': description,
|
||||
}])],
|
||||
}])
|
||||
self.assertEqual(prod_templ.default_uom.symbol, unit)
|
||||
self.assertEqual(prod_templ.products[0].description, description)
|
||||
return prod_templ.products[0]
|
||||
|
||||
def prep_asset_item(self, company, product):
|
||||
""" create asset
|
||||
"""
|
||||
pool = Pool()
|
||||
Asset = pool.get('investment.asset')
|
||||
|
||||
asset, = Asset.create([{
|
||||
'company': company.id,
|
||||
'product': product.id,
|
||||
'currency': company.currency.id,
|
||||
'currency_digits': 4,
|
||||
'uom': product.default_uom.id,
|
||||
}])
|
||||
self.assertEqual(asset.rec_name, product.name)
|
||||
self.assertEqual(asset.currency.rec_name, 'usd')
|
||||
self.assertEqual(asset.currency_digits, 4)
|
||||
self.assertEqual(asset.product.rec_name, product.name)
|
||||
self.assertEqual(asset.uom.symbol, product.default_uom.symbol)
|
||||
return asset
|
||||
|
||||
@with_transaction()
|
||||
def test_asset_create(self):
|
||||
""" create asset
|
||||
"""
|
||||
company = self.prep_asset_company()
|
||||
product = self.prep_asset_product(
|
||||
name='Product 1',
|
||||
description='some asset')
|
||||
|
||||
asset = self.prep_asset_item(
|
||||
company=company,
|
||||
product = product)
|
||||
|
||||
@with_transaction()
|
||||
def test_asset_check_product_update(self):
|
||||
""" create asset
|
||||
"""
|
||||
company = self.prep_asset_company()
|
||||
product1 = self.prep_asset_product(
|
||||
name='Product unit', unit='u')
|
||||
product2 = self.prep_asset_product(
|
||||
name='Product gram', unit='g')
|
||||
self.assertEqual(product2.default_uom.digits, 2)
|
||||
|
||||
asset = self.prep_asset_item(
|
||||
company=company,
|
||||
product = product1)
|
||||
|
||||
self.assertEqual(asset.product.rec_name, 'Product unit')
|
||||
self.assertEqual(asset.product.default_uom.rec_name, 'Unit')
|
||||
self.assertEqual(asset.uom.rec_name, 'Unit')
|
||||
self.assertEqual(asset.currency_digits, 4)
|
||||
|
||||
asset.product = product2
|
||||
asset.on_change_product()
|
||||
asset.save()
|
||||
|
||||
self.assertEqual(asset.product.rec_name, 'Product gram')
|
||||
self.assertEqual(asset.product.default_uom.rec_name, 'Gram')
|
||||
self.assertEqual(asset.uom.rec_name, 'Gram')
|
||||
|
||||
asset.on_change_currency()
|
||||
asset.save()
|
||||
self.assertEqual(asset.currency_digits, 2)
|
||||
|
||||
# end AssetTestCase
|
|
@ -3,6 +3,8 @@ version=6.0.0
|
|||
depends:
|
||||
res
|
||||
company
|
||||
currency
|
||||
product
|
||||
xml:
|
||||
icon.xml
|
||||
group.xml
|
||||
|
|
|
@ -3,8 +3,19 @@
|
|||
The COPYRIGHT file at the top level of this repository contains the
|
||||
full copyright notices and license terms. -->
|
||||
<form col="4">
|
||||
<label name="product" />
|
||||
<field name="product" />
|
||||
<newline/>
|
||||
|
||||
<separator id="sepunits" colspan="4" string="Currency and Units"/>
|
||||
<label name="currency" />
|
||||
<field name="currency" />
|
||||
<label name="currency_digits" />
|
||||
<field name="currency_digits" />
|
||||
|
||||
<label name="uom" />
|
||||
<field name="uom" />
|
||||
<label name="product_uom" />
|
||||
<field name="product_uom" />
|
||||
|
||||
</form>
|
||||
|
|
|
@ -3,5 +3,7 @@
|
|||
The COPYRIGHT file at the top level of this repository contains the
|
||||
full copyright notices and license terms. -->
|
||||
<tree>
|
||||
<field name="rec_name"/>
|
||||
<field name="currency"/>
|
||||
<field name="uom"/>
|
||||
</tree>
|
||||
|
|
Loading…
Reference in a new issue