From 13007f30c832f90a9da37dc9684a628bafe95e48 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 10 Nov 2022 22:31:22 +0100 Subject: [PATCH] asset: produkt, einheit + test --- asset.py | 57 +++++++++++++++++++++ asset.xml | 2 +- locale/de.po | 19 +++++++ locale/en.po | 20 ++++++++ tests/__init__.py | 24 +++++++++ tests/test_asset.py | 117 ++++++++++++++++++++++++++++++++++++++++++++ tryton.cfg | 2 + view/asset_form.xml | 11 +++++ view/asset_list.xml | 2 + 9 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_asset.py diff --git a/asset.py b/asset.py index 4d4d162..9503311 100644 --- a/asset.py +++ b/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 diff --git a/asset.xml b/asset.xml index c90cd67..6946a72 100644 --- a/asset.xml +++ b/asset.xml @@ -45,7 +45,7 @@ full copyright notices and license terms. --> - + diff --git a/locale/de.po b/locale/de.po index 08040d4..5ecd515 100644 --- a/locale/de.po +++ b/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" diff --git a/locale/en.po b/locale/en.po index afbfae4..66fc058 100644 --- a/locale/en.po +++ b/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" + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..04676f1 --- /dev/null +++ b/tests/__init__.py @@ -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 diff --git a/tests/test_asset.py b/tests/test_asset.py new file mode 100644 index 0000000..fd46058 --- /dev/null +++ b/tests/test_asset.py @@ -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 diff --git a/tryton.cfg b/tryton.cfg index 835fdd2..3a75410 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -3,6 +3,8 @@ version=6.0.0 depends: res company + currency + product xml: icon.xml group.xml diff --git a/view/asset_form.xml b/view/asset_form.xml index 25722e0..d53566b 100644 --- a/view/asset_form.xml +++ b/view/asset_form.xml @@ -3,8 +3,19 @@ The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. -->
+