2017-06-16 09:32:47 +02:00
|
|
|
# The COPYRIGHT file at the top level of this repository contains
|
|
|
|
# the full copyright notices and license terms.
|
|
|
|
from trytond.model import fields
|
2019-03-25 11:06:07 +01:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
2017-06-16 09:32:47 +02:00
|
|
|
from . import aeat
|
|
|
|
|
2019-03-25 11:06:07 +01:00
|
|
|
__all__ = ['Party', 'PartyIdentifier']
|
2017-06-16 09:32:47 +02:00
|
|
|
|
|
|
|
|
2018-10-11 15:32:09 +02:00
|
|
|
class Party(metaclass=PoolMeta):
|
2017-06-16 09:32:47 +02:00
|
|
|
__name__ = 'party.party'
|
2017-06-22 23:13:59 +02:00
|
|
|
sii_identifier_type = fields.Selection(aeat.PARTY_IDENTIFIER_TYPE,
|
2019-03-25 11:06:07 +01:00
|
|
|
'SII Identifier Type', sort=False)
|
2017-06-22 23:13:59 +02:00
|
|
|
sii_vat_code = fields.Function(fields.Char('SII VAT Code', size=9),
|
2017-06-16 09:32:47 +02:00
|
|
|
'get_sii_vat_data')
|
|
|
|
|
|
|
|
def get_sii_vat_data(self, name=None):
|
2017-11-01 08:18:21 +01:00
|
|
|
identifier = self.tax_identifier or (
|
|
|
|
self.identifiers and self.identifiers[0])
|
2017-10-30 12:11:32 +01:00
|
|
|
if identifier:
|
2017-06-16 09:32:47 +02:00
|
|
|
if name == 'sii_vat_code':
|
2017-11-01 08:18:21 +01:00
|
|
|
if (identifier.type == 'eu_vat' and
|
|
|
|
not identifier.code.startswith('ES') and
|
|
|
|
self.sii_identifier_type == '02'):
|
|
|
|
return identifier.code
|
|
|
|
return identifier.code[2:]
|
2019-03-25 11:06:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PartyIdentifier(metaclass=PoolMeta):
|
|
|
|
__name__ = 'party.identifier'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_sii_identifier_type(cls, identifiers):
|
|
|
|
Party = Pool().get('party.party')
|
|
|
|
|
|
|
|
to_write = []
|
|
|
|
for identifier in identifiers:
|
|
|
|
write = True
|
|
|
|
type_ = identifier.type
|
|
|
|
if type_ == 'eu_vat':
|
|
|
|
if identifier.code.startswith('ES'):
|
|
|
|
sii_identifier_type = None
|
|
|
|
else:
|
|
|
|
sii_identifier_type = '02'
|
|
|
|
elif type_ == 'eu_not_vat':
|
|
|
|
sii_identifier_type = '04'
|
|
|
|
else:
|
|
|
|
write = False
|
|
|
|
|
|
|
|
if write:
|
|
|
|
to_write.extend(([identifier.party], {
|
|
|
|
'sii_identifier_type': sii_identifier_type}))
|
|
|
|
|
|
|
|
if to_write:
|
|
|
|
Party.write(*to_write)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, vlist):
|
|
|
|
identifiers = super(PartyIdentifier, cls).create(vlist)
|
|
|
|
cls.set_sii_identifier_type(identifiers)
|
|
|
|
return identifiers
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def write(cls, *args):
|
|
|
|
super(PartyIdentifier, cls).write(*args)
|
|
|
|
|
|
|
|
def get_identifiers(identifiers):
|
|
|
|
return list(set(identifiers))
|
|
|
|
|
|
|
|
actions = iter(args)
|
|
|
|
for identifiers, values in zip(actions, actions):
|
|
|
|
cls.set_sii_identifier_type(get_identifiers(identifiers))
|