From c6eadfa6eff2933c058e91befe7ac9e2c69a5664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 1 Apr 2017 00:33:03 +0200 Subject: [PATCH] Remove Property fields issue2349 review32201002 --- __init__.py | 2 + configuration.py | 126 ++++++++++++++++++++++++++++++++++++++++------ configuration.xml | 13 ----- purchase.py | 14 +++--- 4 files changed, 120 insertions(+), 35 deletions(-) diff --git a/__init__.py b/__init__.py index 5f2c0e1fef..0161e47963 100644 --- a/__init__.py +++ b/__init__.py @@ -29,6 +29,8 @@ def register(): HandleShipmentExceptionAsk, HandleInvoiceExceptionAsk, Configuration, + ConfigurationSequence, + ConfigurationPurchaseMethod, Invoice, InvoiceLine, Location, diff --git a/configuration.py b/configuration.py index c4b27fdf96..4496ee27dd 100644 --- a/configuration.py +++ b/configuration.py @@ -1,30 +1,124 @@ # 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', 'ConfigurationPurchaseMethod'] +purchase_invoice_method = fields.Selection( + 'get_purchase_invoice_method', "Invoice Method") -class Configuration(ModelSingleton, ModelSQL, ModelView): +def get_purchase_methods(field_name): + @classmethod + def func(cls): + pool = Pool() + Purchase = pool.get('purchase.purchase') + return Purchase.fields_get([field_name])[field_name]['selection'] + return func + + +class Configuration( + ModelSingleton, ModelSQL, ModelView, CompanyMultiValueMixin): 'Purchase Configuration' __name__ = 'purchase.configuration' - purchase_sequence = fields.Property(fields.Many2One('ir.sequence', - 'Purchase Sequence', domain=[ + purchase_sequence = fields.MultiValue(fields.Many2One( + 'ir.sequence', "Purchase Sequence", required=True, + domain=[ ('company', 'in', [Eval('context', {}).get('company', -1), None]), ('code', '=', 'purchase.purchase'), - ], required=True)) - purchase_invoice_method = fields.Property(fields.Selection( - 'get_purchase_invoice_method', 'Invoice Method', - states={ - 'required': Bool(Eval('context', {}).get('company')), - })) + ])) + purchase_invoice_method = fields.MultiValue(purchase_invoice_method) + get_purchase_invoice_method = get_purchase_methods('invoice_method') @classmethod - def get_purchase_invoice_method(cls): + def multivalue_model(cls, field): pool = Pool() - Purchase = pool.get('purchase.purchase') - return Purchase.fields_get( - ['invoice_method'])['invoice_method']['selection'] + if field == 'purchase_invoice_method': + return pool.get('purchase.configuration.purchase_method') + if field == 'purchase_sequence': + return pool.get('purchase.configuration.sequence') + return super(Configuration, cls).multivalue_model(field) + + @classmethod + def default_purchase_sequence(cls, **pattern): + return cls.multivalue_model( + 'purchase_sequence').default_purchase_sequence() + + @classmethod + def default_purchase_invoice_method(cls, **pattern): + return cls.multivalue_model( + 'purchase_invoice_method').default_purchase_invoice_method() + + +class ConfigurationSequence(ModelSQL, CompanyValueMixin): + "Purchase Configuration Sequence" + __name__ = 'purchase.configuration.sequence' + purchase_sequence = fields.Many2One( + 'ir.sequence', "Purchase Sequence", required=True, + domain=[ + ('company', 'in', [Eval('company', -1), None]), + ('code', '=', 'purchase.purchase'), + ], + depends=['company']) + + @classmethod + 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('purchase_sequence') + value_names.append('purchase_sequence') + fields.append('company') + migrate_property( + 'purchase.configuration', field_names, cls, value_names, + fields=fields) + + @classmethod + def default_purchase_sequence(cls): + pool = Pool() + ModelData = pool.get('ir.model.data') + return ModelData.get_id('purchase', 'sequence_purchase') + + +class ConfigurationPurchaseMethod(ModelSQL, ValueMixin): + "Purchase Configuration Purchase Method" + __name__ = 'purchase.configuration.purchase_method' + purchase_invoice_method = purchase_invoice_method + get_purchase_invoice_method = get_purchase_methods('invoice_method') + + @classmethod + def __register__(cls, module_name): + TableHandler = backend.get('TableHandler') + exist = TableHandler.table_exist(cls._table) + + super(ConfigurationPurchaseMethod, cls).__register__(module_name) + + if not exist: + cls._migrate_property([], [], []) + + @classmethod + def _migrate_property(cls, field_names, value_names, fields): + field_names.append('purchase_invoice_method') + value_names.append('purchase_invoice_method') + migrate_property( + 'purchase.configuration', field_names, cls, value_names, + fields=fields) + + @classmethod + def default_purchase_invoice_method(cls): + return 'order' diff --git a/configuration.xml b/configuration.xml index a7da161af7..05f1e324d3 100644 --- a/configuration.xml +++ b/configuration.xml @@ -22,17 +22,4 @@ this repository contains the full copyright notices and license terms. --> action="act_purchase_configuration_form" id="menu_purchase_configuration" icon="tryton-list"/> - - - - - - - - ,order - - diff --git a/purchase.py b/purchase.py index 004a9bda70..40832cc53b 100644 --- a/purchase.py +++ b/purchase.py @@ -999,8 +999,8 @@ class PurchaseLine(sequence_ordered(), ModelSQL, ModelView): 'misses the supplier location for line "%(line)s".'), 'missing_account_expense': ('Product "%(product)s" of ' 'purchase %(purchase)s misses an expense account.'), - 'missing_account_expense_property': ('Purchase "%(purchase)s" ' - 'misses an "account expense" default property.'), + 'missing_default_account_expense': ('Purchase "%(purchase)s" ' + 'misses a default "account expense".'), }) @classmethod @@ -1247,8 +1247,9 @@ class PurchaseLine(sequence_ordered(), ModelSQL, ModelView): def get_invoice_line(self): 'Return a list of invoice line for purchase 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 @@ -1284,12 +1285,13 @@ class PurchaseLine(sequence_ordered(), ModelSQL, ModelView): 'purchase': self.purchase.rec_name, }) else: - for model in ('product.template', 'product.category'): - invoice_line.account = Property.get('account_expense', model) + for name in ['default_product_account_expense', + 'default_category_account_expense']: + invoice_line.account = account_config.get_multivalue(name) if invoice_line.account: break if not invoice_line.account: - self.raise_user_error('missing_account_expense_property', + self.raise_user_error('missing_default_account_expense', {'purchase': self.purchase.rec_name}) invoice_line.stock_moves = self._get_invoice_line_moves() return [invoice_line]