Files
tryton/modules/party/configuration.py
T

119 lines
4.1 KiB
Python
Raw Normal View History

2014-12-02 12:55:24 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2021-03-18 22:36:31 +01:00
from trytond.i18n import gettext
2021-12-11 17:58:42 +01:00
from trytond.model import (
ModelSingleton, ModelSQL, ModelView, MultiValueMixin, ValueMixin, fields)
2021-03-18 22:36:31 +01:00
from trytond.model.exceptions import AccessError
2019-04-04 19:11:44 +02:00
from trytond.pool import Pool
2021-03-11 23:00:10 +01:00
from trytond.pyson import Id
2023-09-26 23:46:13 +02:00
from .party import IDENTIFIER_TYPES, replace_vat
2021-03-18 22:36:31 +01:00
2017-04-01 00:31:21 +02:00
party_sequence = fields.Many2One('ir.sequence', 'Party Sequence',
domain=[
2021-03-11 23:00:10 +01:00
('sequence_type', '=', Id('party', 'sequence_type_party')),
2017-04-01 00:31:21 +02:00
],
help="Used to generate the party code.")
party_lang = fields.Many2One("ir.lang", 'Party Language',
help="The default language for new parties.")
2012-09-24 22:10:20 +02:00
2017-04-01 00:31:21 +02:00
class Configuration(ModelSingleton, ModelSQL, ModelView, MultiValueMixin):
2010-04-08 19:03:23 +02:00
'Party Configuration'
2012-09-24 22:10:20 +02:00
__name__ = 'party.configuration'
2017-04-01 00:31:21 +02:00
party_sequence = fields.MultiValue(party_sequence)
party_lang = fields.MultiValue(party_lang)
2021-03-18 22:36:31 +01:00
identifier_types = fields.MultiSelection(
IDENTIFIER_TYPES, "Identifier Types",
help="Defines which identifier types are available.\n"
"Leave empty for all of them.")
2017-04-01 00:31:21 +02:00
2023-09-26 23:46:13 +02:00
@classmethod
def __register__(cls, module):
super().__register__(module)
# Migration from 6.8: Use vat alias
configuration = cls(1)
if configuration.identifier_types:
identifier_types = list(map(
2023-09-26 23:46:13 +02:00
replace_vat, configuration.identifier_types))
if configuration.identifier_types != identifier_types:
configuration.identifier_types = identifier_types
configuration.save()
2023-09-26 23:46:13 +02:00
2019-04-04 19:11:44 +02:00
@classmethod
def default_party_sequence(cls, **pattern):
2019-04-04 19:11:44 +02:00
pool = Pool()
ModelData = pool.get('ir.model.data')
try:
return ModelData.get_id('party', 'sequence_party')
except KeyError:
return None
2021-03-18 22:36:31 +01:00
def get_identifier_types(self):
selection = self.fields_get(
['identifier_types'])['identifier_types']['selection']
if self.identifier_types:
selection = [
(k, v) for k, v in selection if k in self.identifier_types]
return selection
@classmethod
def create(cls, vlist):
records = super().create(vlist)
ModelView._fields_view_get_cache.clear()
return records
@classmethod
def write(cls, *args):
super().write(*args)
ModelView._fields_view_get_cache.clear()
@classmethod
def delete(cls, records):
super().delete(records)
ModelView._fields_view_get_cache.clear()
@classmethod
2022-04-11 23:24:21 +02:00
def validate_fields(cls, records, field_names):
super().validate_fields(records, field_names)
cls(1).check_identifier_types(field_names)
2021-03-18 22:36:31 +01:00
2022-04-11 23:24:21 +02:00
def check_identifier_types(self, field_names=None):
2021-03-18 22:36:31 +01:00
pool = Pool()
Identifier = pool.get('party.identifier')
2022-04-11 23:24:21 +02:00
if field_names and 'identifier_types' not in field_names:
return
2021-03-18 22:36:31 +01:00
if self.identifier_types:
identifier_types = [None, ''] + list(self.identifier_types)
2021-03-18 22:36:31 +01:00
identifiers = Identifier.search([
('type', 'not in', identifier_types),
2021-03-18 22:36:31 +01:00
], limit=1, order=[])
if identifiers:
identifier, = identifiers
selection = self.fields_get(
['identifier_types'])['identifier_types']['selection']
selection = dict(selection)
raise AccessError(gettext(
'party.msg_identifier_type_remove',
type=selection.get(identifier.type, identifier.type),
identifier=identifier.rec_name,
))
2017-04-01 00:31:21 +02:00
2023-01-19 11:25:25 +01:00
class ConfigurationSequence(ModelSQL, ValueMixin):
2017-04-01 00:31:21 +02:00
'Party Configuration Sequence'
__name__ = 'party.configuration.party_sequence'
party_sequence = party_sequence
@classmethod
def check_xml_record(cls, records, values):
pass
2017-04-01 00:31:21 +02:00
2023-01-19 11:25:25 +01:00
class ConfigurationLang(ModelSQL, ValueMixin):
2017-04-01 00:31:21 +02:00
'Party Configuration Lang'
__name__ = 'party.configuration.party_lang'
party_lang = party_lang