From ff30aef9b65eeec0366b6fbcf901824572550210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 1 Apr 2017 00:36:49 +0200 Subject: [PATCH] Remove Property fields issue2349 review28151002 --- __init__.py | 2 + configuration.py | 145 ++++++++++++++++++++++++++++++++++++++-------- configuration.xml | 17 ------ sale.py | 14 +++-- 4 files changed, 130 insertions(+), 48 deletions(-) diff --git a/__init__.py b/__init__.py index 888febc3e2..1e151f618c 100644 --- a/__init__.py +++ b/__init__.py @@ -28,6 +28,8 @@ def register(): HandleInvoiceExceptionAsk, ReturnSaleStart, Configuration, + ConfigurationSequence, + ConfigurationSaleMethod, Invoice, InvoiceLine, module='sale', type_='model') diff --git a/configuration.py b/configuration.py index 115dc401b1..371947173b 100644 --- a/configuration.py +++ b/configuration.py @@ -1,42 +1,137 @@ # 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.model import ModelView, ModelSQL, ModelSingleton, fields +from trytond import backend +from trytond.model import (ModelView, ModelSQL, ModelSingleton, ValueMixin, + fields) from trytond.pool import Pool -from trytond.pyson import Eval, Bool +from trytond.pyson import Eval +from trytond.tools.multivalue import migrate_property +from trytond.modules.company.model import ( + CompanyMultiValueMixin, CompanyValueMixin) -__all__ = ['Configuration'] +__all__ = ['Configuration', + 'ConfigurationSequence', 'ConfigurationSaleMethod'] +sale_invoice_method = fields.Selection( + 'get_sale_shipment_methods', "Sale Invoice Method") +sale_shipment_method = fields.Selection( + 'get_sale_shipment_methods', "Sale Shipment Method") -class Configuration(ModelSingleton, ModelSQL, ModelView): +def get_sale_methods(field_name): + @classmethod + def func(cls): + pool = Pool() + Sale = pool.get('sale.sale') + return Sale.fields_get([field_name])[field_name]['selection'] + return func + + +def default_func(field_name): + @classmethod + def default(cls, **pattern): + return getattr( + cls.multivalue_model(field_name), + 'default_%s' % field_name, lambda: None)() + return default + + +class Configuration( + ModelSingleton, ModelSQL, ModelView, CompanyMultiValueMixin): 'Sale Configuration' __name__ = 'sale.configuration' - sale_sequence = fields.Property(fields.Many2One('ir.sequence', - 'Sale Sequence', domain=[ + sale_sequence = fields.MultiValue(fields.Many2One( + 'ir.sequence', "Sale Sequence", required=True, + domain=[ ('company', 'in', [Eval('context', {}).get('company', -1), None]), ('code', '=', 'sale.sale'), - ], required=True)) - sale_invoice_method = fields.Property(fields.Selection( - 'get_sale_invoice_methods', 'Sale Invoice Method', - states={ - 'required': Bool(Eval('context', {}).get('company')), - })) - sale_shipment_method = fields.Property(fields.Selection( - 'get_sale_shipment_methods', 'Sale Shipment Method', - states={ - 'required': Bool(Eval('context', {}).get('company')), - })) + ])) + sale_invoice_method = fields.MultiValue(sale_invoice_method) + get_sale_invoice_methods = get_sale_methods('invoice_method') + sale_shipment_method = fields.MultiValue(sale_shipment_method) + get_sale_shipment_methods = get_sale_methods('shipment_method') @classmethod - def get_sale_invoice_methods(cls): + def multivalue_model(cls, field): pool = Pool() - Sale = pool.get('sale.sale') - field_name = 'invoice_method' - return Sale.fields_get([field_name])[field_name]['selection'] + if field in {'sale_invoice_method', 'sale_shipment_method'}: + return pool.get('sale.configuration.sale_method') + if field == 'sale_sequence': + return pool.get('sale.configuration.sequence') + return super(Configuration, cls).multivalue_model(field) + + default_sale_sequence = default_func('sale_sequence') + default_sale_invoice_method = default_func('sale_invoice_method') + default_sale_shipment_method = default_func('sale_shipment_method') + + +class ConfigurationSequence(ModelSQL, CompanyValueMixin): + "Sale Configuration Sequence" + __name__ = 'sale.configuration.sequence' + sale_sequence = fields.Many2One( + 'ir.sequence', "Sale Sequence", required=True, + domain=[ + ('company', 'in', [Eval('company', -1), None]), + ('code', '=', 'sale.sale'), + ], + depends=['company']) @classmethod - def get_sale_shipment_methods(cls): + def __register__(cls, module_name): + TableHandler = backend.get('TableHandler') + exist = TableHandler.table_exist(cls._table) + + super(ConfigurationSequence, cls).__register__(module_name) + + if not exist: + cls._migrate_property([], [], []) + + @classmethod + def _migrate_property(cls, field_names, value_names, fields): + field_names.append('sale_sequence') + value_names.append('sale_sequence') + fields.append('company') + migrate_property( + 'sale.configuration', field_names, cls, value_names, + fields=fields) + + @classmethod + def default_sale_sequence(cls): pool = Pool() - Sale = pool.get('sale.sale') - field_name = 'shipment_method' - return Sale.fields_get([field_name])[field_name]['selection'] + ModelData = pool.get('ir.model.data') + return ModelData.get_id('sale', 'sequence_sale') + + +class ConfigurationSaleMethod(ModelSQL, ValueMixin): + "Sale Configuration Sale Method" + __name__ = 'sale.configuration.sale_method' + sale_invoice_method = sale_invoice_method + get_sale_invoice_methods = get_sale_methods('invoice_method') + sale_shipment_method = sale_shipment_method + get_sale_shipment_methods = get_sale_methods('shipment_method') + + @classmethod + def __register__(cls, module_name): + TableHandler = backend.get('TableHandler') + exist = TableHandler.table_exist(cls._table) + + super(ConfigurationSaleMethod, cls).__register__(module_name) + + if not exist: + cls._migrate_property([], [], []) + + @classmethod + def _migrate_property(cls, field_names, value_names, fields): + field_names.extend(['sale_invoice_method', 'sale_shipment_method']) + value_names.extend(['sale_invoice_method', 'sale_shipment_method']) + migrate_property( + 'sale.configuration', field_names, cls, value_names, + fields=fields) + + @classmethod + def default_sale_invoice_method(cls): + return 'order' + + @classmethod + def default_sale_shipment_method(cls): + return 'order' diff --git a/configuration.xml b/configuration.xml index c54c6e83ba..0c165f65fb 100644 --- a/configuration.xml +++ b/configuration.xml @@ -30,21 +30,4 @@ this repository contains the full copyright notices and license terms. --> action="act_sale_configuration_form" id="menu_sale_configuration" icon="tryton-list"/> - - - - - - - - ,order - - - - ,order - - diff --git a/sale.py b/sale.py index f4abf250ed..00ba48283e 100644 --- a/sale.py +++ b/sale.py @@ -1072,8 +1072,8 @@ class SaleLine(sequence_ordered(), ModelSQL, ModelView): 'customer location in line "%(line)s".'), 'missing_account_revenue': ('Product "%(product)s" of sale ' '%(sale)s misses a revenue account.'), - 'missing_account_revenue_property': ('Sale "%(sale)s" ' - 'misses an "account revenue" default property.'), + 'missing_default_account_revenue': ('Sale "%(sale)s" ' + 'misses a default "account revenue".'), }) @classmethod @@ -1309,8 +1309,9 @@ class SaleLine(sequence_ordered(), ModelSQL, ModelView): def get_invoice_line(self): 'Return a list of invoice lines for sale line' pool = Pool() - Property = pool.get('ir.property') InvoiceLine = pool.get('account.invoice.line') + AccountConfiguration = pool.get('account.configuration') + account_config = AccountConfiguration(1) invoice_line = InvoiceLine() invoice_line.type = self.type @@ -1346,12 +1347,13 @@ class SaleLine(sequence_ordered(), ModelSQL, ModelView): 'product': self.product.rec_name, }) else: - for model in ('product.template', 'product.category'): - invoice_line.account = Property.get('account_revenue', model) + for name in ['default_product_account_revenue', + 'default_category_account_revenue']: + invoice_line.account = account_config.get_multivalue(name) if invoice_line.account: break if not invoice_line.account: - self.raise_user_error('missing_account_revenue_property', { + self.raise_user_error('missing_default_account_revenue', { 'sale': self.sale.rec_name, }) invoice_line.stock_moves = self._get_invoice_line_moves()