From e972d54f5450125ccd5707a30de9ec5a14966d4e Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Fri, 2 Dec 2022 23:29:01 +0100 Subject: [PATCH] felder symbol, name, product_uom optimiert, felder company_currency entfernt --- asset.py | 107 +++++++++++++++++++++++--------------------- locale/de.po | 10 +---- locale/en.po | 10 +---- tests/test_asset.py | 2 +- view/asset_list.xml | 2 +- 5 files changed, 61 insertions(+), 70 deletions(-) diff --git a/asset.py b/asset.py index c6bda9b..d162425 100644 --- a/asset.py +++ b/asset.py @@ -13,6 +13,7 @@ from datetime import time from sql.functions import CurrentDate, CurrentTimestamp, Round, Extract from sql.conditionals import Case, Coalesce from sql import Literal +from .diagram import Concat2 digits_percent = 2 @@ -28,7 +29,7 @@ class Asset(ModelSQL, ModelView): __name__ = 'investment.asset' name = fields.Function(fields.Char(string='Name', readonly=True), - 'on_change_with_name', searcher='search_rec_name') + 'get_name_symbol', searcher='search_rec_name') company = fields.Many2One(string='Company', model_name='company.company', required=True, ondelete="RESTRICT") product = fields.Many2One(string='Product', required=True, @@ -36,8 +37,7 @@ class Asset(ModelSQL, ModelView): 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') + help='Category of unit on the product.'), 'get_name_symbol') uom = fields.Many2One(string='UOM', required=True, model_name='product.uom', ondelete='RESTRICT', states={ @@ -46,8 +46,8 @@ class Asset(ModelSQL, ModelView): domain=[ ('category', '=', Eval('product_uom')), ], depends=['product_uom', 'product']) - uom_symbol = fields.Function(fields.Char(string='UOM', readonly=True), - 'on_change_with_uom_symbol', searcher='search_uom_symbol') + symbol = fields.Function(fields.Char(string='UOM', readonly=True), + 'get_name_symbol', searcher='search_uom_symbol') rates = fields.One2Many(string='Rates', field='asset', model_name='investment.rate') rate = fields.Function(fields.Numeric(string='Current Rate', @@ -58,14 +58,6 @@ class Asset(ModelSQL, ModelView): help='Date of current rate'), 'get_rate_data', searcher='search_date') - company_currency = fields.Function(fields.Many2One(readonly=True, - string='Company Currency', states={'invisible': True}, - model_name='currency.currency'), - 'on_change_with_company_currency') - company_currency_digits = fields.Function(fields.Integer( - string='Currency Digits (Ref.)', readonly=True), - 'on_change_with_currency_digits') - currency = fields.Many2One(string='Currency', select=True, required=True, model_name='currency.currency', ondelete='RESTRICT') currency_digits = fields.Integer(string='Digits', required=True) @@ -228,47 +220,63 @@ class Asset(ModelSQL, ModelView): if self.currency: self.currency_digits = self.currency.digits - @fields.depends('product') - def on_change_with_name(self, name=None): - """ get name of product + @classmethod + def get_name_symbol_sql(cls): + """ get sql for name, uom, digits, etc. """ - if self.product: - return self.product.name + pool = Pool() + Product = pool.get('product.product') + ProdTempl = pool.get('product.template') + Uom = pool.get('product.uom') + Currency = pool.get('currency.currency') + tab_asset = cls.__table__() + tab_templ = ProdTempl.__table__() + tab_prod = Product.__table__() + tab_uom = Uom.__table__() + tab_uom2 = Uom.__table__() + tab_cur = Currency.__table__() - @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 + query = tab_asset.join(tab_prod, + condition=tab_asset.product==tab_prod.id, + ).join(tab_templ, + condition=tab_templ.id==tab_prod.template, + ).join(tab_uom, + condition=tab_templ.default_uom==tab_uom.id, + ).join(tab_cur, + condition=tab_asset.currency==tab_cur.id, + ).join(tab_uom2, + condition=tab_asset.uom==tab_uom2.id, + ).select( + tab_asset.id, + tab_templ.name, + tab_uom.category.as_('product_uom'), + Concat2(tab_cur.symbol, '/', tab_uom2.symbol).as_('symbol'), + ) + return (query, tab_asset) - @fields.depends('currency') - def on_change_with_currency_digits(self, name=None): - """ currency of cashbook + @classmethod + def get_name_symbol(cls, assets, names): + """ get date and rate of asset """ - if self.currency: - return self.currency.digits - else: - return 2 + cursor = Transaction().connection.cursor() - @fields.depends('company', 'currency') - def on_change_with_company_currency(self, name=None): - """ get company-currency if its different from current - asset-currency - """ - if self.company: - if self.currency: - if self.company.currency.id != self.currency.id: - return self.company.currency.id + (query, tab_asset) = cls.get_name_symbol_sql() + query.where=tab_asset.id.in_([x.id for x in assets]) - @fields.depends('uom', 'currency') - def on_change_with_uom_symbol(self, name=None): - """ get symbl of uom - """ - return '%(currency)s/%(unit)s' % { - 'currency': self.currency.symbol if self.currency is not None else '-', - 'unit': self.uom.symbol if self.uom is not None else '-', - } + cursor.execute(*query) + records = cursor.fetchall() + result = {x:{y.id: None for y in assets} for x in names} + + for record in records: + values = { + 'name': record[1], + 'product_uom': record[2], + 'symbol': record[3], + } + + for name in names: + result[name][record[0]] = values[name] + return result @classmethod def search_uom_symbol(cls, names, clause): @@ -305,7 +313,6 @@ class Asset(ModelSQL, ModelView): """ get date and rate of asset """ cursor = Transaction().connection.cursor() - (query, tab_asset) = cls.get_rate_data_sql() query.where=tab_asset.id.in_([x.id for x in assets]) @@ -798,7 +805,7 @@ class Asset(ModelSQL, ModelView): """ return '%(prod)s | %(rate)s %(unit)s | %(date)s' % { 'prod': getattr(self.product, 'rec_name', '-'), - 'unit': self.uom_symbol, + 'unit': self.symbol, 'rate': Report.format_number(self.rate, lang=None, digits=self.currency_digits or 4) \ if self.rate is not None else '-', diff --git a/locale/de.po b/locale/de.po index 3e80241..a3858a1 100644 --- a/locale/de.po +++ b/locale/de.po @@ -122,14 +122,6 @@ msgctxt "field:investment.asset,company:" msgid "Company" msgstr "Unternehmen" -msgctxt "field:investment.asset,company_currency:" -msgid "Company Currency" -msgstr "Unternehmenswährung" - -msgctxt "field:investment.asset,company_currency_digits:" -msgid "Currency Digits (Ref.)" -msgstr "Nachkommastellen Währung (Ref.)" - msgctxt "field:investment.asset,currency:" msgid "Currency" msgstr "Währung" @@ -154,7 +146,7 @@ msgctxt "field:investment.asset,uom:" msgid "UOM" msgstr "Einheit" -msgctxt "field:investment.asset,uom_symbol:" +msgctxt "field:investment.asset,symbol:" msgid "UOM" msgstr "Einheit" diff --git a/locale/en.po b/locale/en.po index a8572bc..e3466ac 100644 --- a/locale/en.po +++ b/locale/en.po @@ -90,14 +90,6 @@ msgctxt "field:investment.asset,company:" msgid "Company" msgstr "Company" -msgctxt "field:investment.asset,company_currency:" -msgid "Company Currency" -msgstr "Company Currency" - -msgctxt "field:investment.asset,company_currency_digits:" -msgid "Currency Digits (Ref.)" -msgstr "Currency Digits (Ref.)" - msgctxt "field:investment.asset,currency:" msgid "Currency" msgstr "Currency" @@ -122,7 +114,7 @@ msgctxt "field:investment.asset,uom:" msgid "UOM" msgstr "UOM" -msgctxt "field:investment.asset,uom_symbol:" +msgctxt "field:investment.asset,symbol:" msgid "UOM" msgstr "UOM" diff --git a/tests/test_asset.py b/tests/test_asset.py index 3805d6c..8fe64c7 100644 --- a/tests/test_asset.py +++ b/tests/test_asset.py @@ -83,7 +83,7 @@ class AssetTestCase(ModuleTestCase): asset = self.prep_asset_item( company=company, product = product) - self.assertEqual(asset.uom_symbol, 'usd/u') + self.assertEqual(asset.symbol, 'usd/u') @with_transaction() def test_asset_rec_name(self): diff --git a/view/asset_list.xml b/view/asset_list.xml index 1f5e868..bfa9123 100644 --- a/view/asset_list.xml +++ b/view/asset_list.xml @@ -10,7 +10,7 @@ full copyright notices and license terms. --> - +