diff --git a/__init__.py b/__init__.py index fe99eb38d3..fde1c43984 100644 --- a/__init__.py +++ b/__init__.py @@ -2,7 +2,7 @@ # this repository contains the full copyright notices and license terms. from trytond.pool import Pool -from . import account, account_payment, party, reporting_tax +from . import account, account_payment, company, party, reporting_tax def register(): @@ -13,6 +13,7 @@ def register(): account.TaxTemplate, account.Tax, account.Invoice, + company.Company, party.Party, party.Identifier, reporting_tax.ESVATList, diff --git a/aeat347.txt b/aeat347.txt index dc1ed78fe9..94edd50962 100644 --- a/aeat347.txt +++ b/aeat347.txt @@ -1,4 +1,4 @@ -1347${year}${company.party.tax_identifier.es_code()}${justify(company.rec_name.upper(), 40)}T000000000${justify('', 40)}347${year}000001 0000000000000${format_integer(len(records), 9)}${format_decimal(records_amount)}${format_integer(0, 9)}${format_decimal(0)}${justify('', 315)} +1347${year}${company.party.tax_identifier.es_code()}${justify(company.party.name.upper(), 40)}T${format_phone(company.es_aeat_contact_phone)}${justify(company.party.name.upper(), 40)}347${year}000001 0000000000000${format_integer(len(records), 9)}${format_decimal(records_amount)}${format_integer(0, 9)}${format_decimal(0)}${justify('', 315)} {% for record in records %}\ 2347${year}${justify(identifier_code(record.company_tax_identifier), 9)}${justify(identifier_code(record.party_tax_identifier), 9)}${justify('', 9)}${justify(strip_accents(record.party.name.upper()), 40)}D${justify(record.province_code, 2)}${justify(country_code(record), 2)} ${record.code}${format_decimal(record.amount)} 000000000000000${format_decimal(0)}0000${format_decimal(record.first_period_amount or 0)}${format_decimal(0)}${format_decimal(record.second_period_amount or 0)}${format_decimal(0)}${format_decimal(record.third_period_amount or 0)}${format_decimal(0)}${format_decimal(record.fourth_period_amount or 0)}${format_decimal(0)}${justify('', 20)}${format_decimal(0)}${justify('', 201)} {% end %}\ diff --git a/aeat349.txt b/aeat349.txt index ae23e30d32..2484bda3bd 100644 --- a/aeat349.txt +++ b/aeat349.txt @@ -1,4 +1,4 @@ -1349${year}${company.party.tax_identifier.code[2:]}${justify(company.rec_name.upper(), 40)}T000000000${justify('', 40)}349${year}${period_number}0001 0000000000000${period}${format_integer(len(records), 9)}${format_decimal(records_amount, 15)}${format_integer(0, 9)}${format_decimal(0), 15}${justify('', 315)} +1349${year}${company.party.tax_identifier.code[2:]}${justify(company.party.name.upper(), 40)}T${format_phone(company.es_aeat_contact_phone)}${justify(company.party.name.upper(), 40)}349${year}${period_number}0001 0000000000000${period}${format_integer(len(records), 9)}${format_decimal(records_amount, 15)}${format_integer(0, 9)}${format_decimal(0), 15}${justify('', 315)} {% for record in records %}\ 2349${year}${justify(identifier_code(record.company_tax_identifier), 9)}${justify('', 58)}${justify(record.party_tax_identifier.code if record.party_tax_identifier else '', 17)}${justify(record.party.name.upper(), 40)}${record.code}${format_decimal(record.amount)}${justify('', 354)} {% end %}\ diff --git a/company.py b/company.py index 347f972fe5..e491ed64a9 100644 --- a/company.py +++ b/company.py @@ -1,5 +1,8 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +import phonenumbers +from phonenumbers import NumberParseException + from trytond.pool import PoolMeta @@ -8,4 +11,15 @@ class Company(metaclass=PoolMeta): @property def es_aeat_contact_phone(self): - return self.party.phone + phone = None + for contact_mechanism in self.party.contact_mechanisms: + if contact_mechanism.type in {'phone', 'mobile'}: + try: + phonenumber = phonenumbers.parse( + contact_mechanism.value, None) + except NumberParseException: + continue + if phonenumber and phonenumber.country_code == '34': + phone = contact_mechanism.value + break + return phone diff --git a/reporting_tax.py b/reporting_tax.py index eca54b5da4..19ed1a4424 100644 --- a/reporting_tax.py +++ b/reporting_tax.py @@ -7,9 +7,11 @@ from decimal import Decimal from io import BytesIO, TextIOWrapper from operator import attrgetter +import phonenumbers from dateutil.relativedelta import relativedelta # XXX fix: https://genshi.edgewall.org/ticket/582 from genshi.template.astutil import ASTCodeGenerator, ASTTransformer +from phonenumbers import NumberParseException from sql import Cast, Literal, Null from sql.aggregate import Count, Min, Sum from sql.conditionals import Case, Coalesce @@ -68,6 +70,17 @@ def format_percentage(n, size=5): return ('{0:.2f}'.format(n)).replace('.', '').rjust(size, '0') +def format_phone(phone): + try: + phone = phonenumbers.format_number( + phonenumbers.parse(phone, 'ES'), + phonenumbers.PhoneNumberFormat.NATIONAL) + except NumberParseException: + phone = '' + phone = phone.replace(' ', '') + return phone[:9].rjust(9, '0') + + def identifier_code(identifier): if identifier: return identifier.es_code() @@ -543,6 +556,7 @@ class AEAT347(Report): 15, '0') context['format_decimal'] = format_decimal context['format_integer'] = format_integer + context['format_phone'] = format_phone context['identifier_code'] = identifier_code context['country_code'] = country_code context['strip_accents'] = strip_accents @@ -699,6 +713,7 @@ class AEAT349(Report): return ('{0:.2f}'.format(abs(n))).replace('.', '').rjust( digits, '0') context['format_decimal'] = format_decimal + context['format_phone'] = format_phone return context diff --git a/setup.py b/setup.py index f82a018eff..cdd66463e1 100755 --- a/setup.py +++ b/setup.py @@ -58,7 +58,7 @@ else: if local_version: version += '+' + '.'.join(local_version) -requires = ['python-sql >= 1.1.0'] +requires = ['python-sql >= 1.1.0', 'phonenumbers'] for dep in info.get('depends', []): if not re.match(r'(ir|res)(\W|$)', dep): requires.append(get_require_version('trytond_%s' % dep)) diff --git a/tests/347.txt b/tests/347.txt index ae99e72b47..2ab626ac01 100644 --- a/tests/347.txt +++ b/tests/347.txt @@ -1,2 +1,2 @@ -13472018B01000009DUNDER MIFFLIN T000000000 3472018000001 0000000000000000000001 000000000024200000000000 000000000000000 +13472018B01000009DUNDER MIFFLIN T000000000DUNDER MIFFLIN 3472018000001 0000000000000000000001 000000000024200000000000 000000000000000 23472018B0100000900000000T PARTY D25 B 000000000024200 000000000000000 0000000000000000000 000000000024200 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 diff --git a/tests/349.txt b/tests/349.txt index 441e54aad6..2d31f23749 100644 --- a/tests/349.txt +++ b/tests/349.txt @@ -1,2 +1,2 @@ -13492018B01000009DUNDER MIFFLIN T000000000 3492018010001 000000000000001000000001000000000010000000000000000000000000015 +13492018B01000009DUNDER MIFFLIN T000000000DUNDER MIFFLIN 3492018010001 000000000000001000000001000000000010000000000000000000000000015 23492018B01000009 BE0897290877 INTRACOMUNITARY SUPPLIER A0000000010000 diff --git a/tryton.cfg b/tryton.cfg index 039da86f20..c406ed36ab 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -1,6 +1,7 @@ [tryton] version=6.3.0 depends: + company currency ir account