Replace fiscalyear to year report

#059815
This commit is contained in:
Raimon Esteve 2022-11-08 10:09:22 +01:00
parent 03c6ad1102
commit 398d499e3e
10 changed files with 112 additions and 79 deletions

76
aeat.py
View file

@ -15,6 +15,7 @@ from trytond.pyson import Bool, Eval, Not
from trytond.transaction import Transaction
from trytond.i18n import gettext
from trytond.exceptions import UserError
from sql.functions import Extract
__all__ = ['Report', 'PartyRecord', 'PropertyRecord']
@ -78,11 +79,7 @@ class Report(Workflow, ModelSQL, ModelView):
help='Legal Representative VAT number.', states={
'readonly': Eval('state') == 'done',
}, depends=['state'])
fiscalyear = fields.Many2One('account.fiscalyear', 'Fiscal Year',
required=True, select=True, states={
'readonly': Eval('state') == 'done',
}, depends=['state'])
fiscalyear_code = fields.Integer('Fiscal Year Code', required=True)
year = fields.Integer("Year", required=True)
company_vat = fields.Char('VAT number', size=9, states={
'required': True,
'readonly': Eval('state') == 'done',
@ -148,6 +145,10 @@ class Report(Workflow, ModelSQL, ModelView):
@classmethod
def __setup__(cls):
super(Report, cls).__setup__()
cls._order = [
('year', 'DESC'),
('id', 'DESC'),
]
cls._buttons.update({
'draft': {
'invisible': ~Eval('state').in_(['calculated',
@ -173,6 +174,29 @@ class Report(Workflow, ModelSQL, ModelView):
('cancelled', 'draft'),
))
@classmethod
def __register__(cls, module_name):
pool = Pool()
FiscalYear = pool.get('account.fiscalyear')
cursor = Transaction().connection.cursor()
table = cls.__table_handler__(module_name)
sql_table = cls.__table__()
fiscalyear_table = FiscalYear.__table__()
super().__register__(module_name)
# migration fiscalyear to year
if table.column_exist('fiscalyear'):
query = sql_table.update(columns=[sql_table.year],
values=[Extract('YEAR', fiscalyear_table.start_date)],
from_=[fiscalyear_table],
where=sql_table.fiscalyear == fiscalyear_table.id)
cursor.execute(*query)
table.drop_column('fiscalyear')
if table.column_exist('fiscalyear_code'):
table.drop_column('fiscalyear_code')
@staticmethod
def default_operation_limit():
return Decimal('3005.06')
@ -201,35 +225,22 @@ class Report(Workflow, ModelSQL, ModelView):
def default_company():
return Transaction().context.get('company')
@staticmethod
def default_fiscalyear():
FiscalYear = Pool().get('account.fiscalyear')
return FiscalYear.find(
Transaction().context.get('company'), exception=False)
def get_rec_name(self, name):
return '%s - %s' % (self.company.rec_name,
self.fiscalyear.name)
self.year)
@classmethod
def search_rec_name(self, name, clause):
return ['OR',
('company.rec_name',) + tuple(clause[1:]),
('fiscalyear.rec_name',) + tuple(clause[1:])
('year',) + tuple(clause[1:])
]
def get_currency(self, name):
return self.company.currency.id
def get_filename(self, name):
return 'aeat347-%s.txt' % self.fiscalyear_code
@fields.depends('fiscalyear')
def on_change_with_fiscalyear_code(self):
code = None
if self.fiscalyear:
code = self.fiscalyear.start_date.year
return code
return 'aeat347-%s.txt' % self.year
@fields.depends('company')
def on_change_with_company_vat(self, name=None):
@ -269,6 +280,17 @@ class Report(Workflow, ModelSQL, ModelView):
default['properties'] = None
return super(Report, cls).copy(reports, default=default)
def pre_validate(self):
super().pre_validate()
self.check_year_digits()
@fields.depends('year')
def check_year_digits(self):
if self.year and len(str(self.year)) != 4:
raise UserError(
gettext('aeat_347.msg_invalid_year',
year=self.year))
@classmethod
def validate(cls, reports):
for report in reports:
@ -322,13 +344,13 @@ class Report(Workflow, ModelSQL, ModelView):
FROM
aeat_347_record as r
WHERE
r.fiscalyear = %s AND
r.year = %s AND
r.party_tax_identifier is not null
GROUP BY
r.party_tax_identifier, r.operation_key
HAVING
sum(amount) > %s
""" % (cls.aggregate_function(), report.fiscalyear.id,
""" % (cls.aggregate_function(), report.year,
report.operation_limit)
cursor.execute(query)
result = cursor.fetchall()
@ -418,21 +440,21 @@ class Report(Workflow, ModelSQL, ModelView):
('state', '=', 'done'),
],
order=[
('fiscalyear', 'DESC'),
('year', 'DESC'),
], count=True)
return count + 1
def create_file(self):
records = []
record = Record(aeat347.PRESENTER_HEADER_RECORD)
record.fiscalyear = str(self.fiscalyear_code)
record.year = str(self.year)
record.nif = self.company_vat
record.presenter_name = self.company.party.name
record.support_type = self.support_type
record.contact_phone = self.contact_phone
record.contact_name = self.contact_name
record.declaration_number = int('347{}{:0>6}'.format(
self.fiscalyear_code,
self.year,
self.auto_sequence()))
record.previous_declaration_number = self.previous_number
record.party_count = len(self.parties)
@ -443,7 +465,7 @@ class Report(Workflow, ModelSQL, ModelView):
records.append(record)
for line in itertools.chain(self.parties, self.properties):
record = line.get_record()
record.fiscalyear = str(self.fiscalyear_code)
record.year = str(self.year)
record.nif = self.company_vat
records.append(record)
try:

View file

@ -6,6 +6,7 @@ from trytond.wizard import Wizard, StateView, StateTransition, Button
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from sql.operators import In
from sql.functions import Extract
from .aeat import OPERATION_KEY
from sql.aggregate import Min
##from trytond.modules.aeat_347.aeat import OPERATION_KEY
@ -26,8 +27,7 @@ class Record(ModelSQL, ModelView):
company = fields.Many2One('company.company', 'Company', required=True,
readonly=True)
fiscalyear = fields.Many2One('account.fiscalyear', 'Fiscal Year',
required=True, readonly=True)
year = fields.Integer("Year", required=True, readonly=True)
month = fields.Integer('Month', readonly=True)
operation_key = fields.Selection(OPERATION_KEY, 'Operation key',
required=True, readonly=True)
@ -39,18 +39,30 @@ class Record(ModelSQL, ModelView):
party_tax_identifier = fields.Many2One('party.identifier',
'Party Tax Identifier')
@classmethod
def __setup__(cls):
super(Record, cls).__setup__()
cls._order = [
('year', 'DESC'),
('id', 'DESC'),
]
@classmethod
def __register__(cls, module_name):
cursor = Transaction().connection.cursor()
table = cls.__table_handler__(module_name)
sql_table = cls.__table__()
pool = Pool()
Invoice = pool.get('account.invoice')
invoice = Invoice.__table__()
Party = pool.get('party.party')
party = Party.__table__()
Identifier = pool.get('party.identifier')
FiscalYear = pool.get('account.fiscalyear')
table = cls.__table_handler__(module_name)
sql_table = cls.__table__()
invoice = Invoice.__table__()
party = Party.__table__()
identifier = Identifier.__table__()
fiscalyear_table = FiscalYear.__table__()
cursor = Transaction().connection.cursor()
exist_tax_identifier = table.column_exist('tax_identifier')
exist_party = table.column_exist('party')
@ -97,6 +109,15 @@ class Record(ModelSQL, ModelView):
table.drop_column('party')
# migration fiscalyear to year
if table.column_exist('fiscalyear'):
query = sql_table.update(columns=[sql_table.year],
values=[Extract('YEAR', fiscalyear_table.start_date)],
from_=[fiscalyear_table],
where=sql_table.fiscalyear == fiscalyear_table.id)
cursor.execute(*query)
table.drop_column('fiscalyear')
@classmethod
def delete_record(cls, invoices):
with Transaction().set_user(0, set_context=True):
@ -210,19 +231,9 @@ class Invoice(metaclass=PoolMeta):
operation_key = invoice.aeat347_operation_key
amount = invoice.get_aeat347_total_amount()
if invoice.type == 'in':
accounting_date = (invoice.accounting_date
or invoice.invoice_date)
period_id = Period.find(
invoice.company.id, date=accounting_date)
period = Period(period_id)
fiscalyear = period.fiscalyear
else:
fiscalyear = invoice.move.period.fiscalyear
to_create[invoice.id] = {
'company': invoice.company.id,
'fiscalyear': fiscalyear,
'year': (invoice.accounting_date or invoice.invoice_date).year,
'month': invoice.invoice_date.month,
'amount': amount,
'operation_key': operation_key,

View file

@ -26,10 +26,6 @@ msgctxt "field:aeat.347.record,company:"
msgid "Company"
msgstr "Empresa"
msgctxt "field:aeat.347.record,fiscalyear:"
msgid "Fiscal Year"
msgstr "Exercici fiscal"
msgctxt "field:aeat.347.record,invoice:"
msgid "Invoice"
msgstr "Factura"
@ -50,6 +46,10 @@ msgctxt "field:aeat.347.record,party_tax_identifier:"
msgid "Party Tax Identifier"
msgstr "Identificador fiscal del tercer"
msgctxt "field:aeat.347.record,year:"
msgid "Year"
msgstr "Any"
msgctxt "field:aeat.347.report,amount:"
msgid "Amount"
msgstr "Import"
@ -90,14 +90,6 @@ msgctxt "field:aeat.347.report,filename:"
msgid "File Name"
msgstr "Nom fitxer"
msgctxt "field:aeat.347.report,fiscalyear:"
msgid "Fiscal Year"
msgstr "Exercici fiscal"
msgctxt "field:aeat.347.report,fiscalyear_code:"
msgid "Fiscal Year Code"
msgstr "Any exercici fiscal"
msgctxt "field:aeat.347.report,on_behalf_third_party_limit:"
msgid "On Behalf of Third Party Limit (3)"
msgstr "Límit en nom de tercers (3)"
@ -154,6 +146,10 @@ msgctxt "field:aeat.347.report,type:"
msgid "Statement Type"
msgstr "Tipus de declaració"
msgctxt "field:aeat.347.report,fiscalyear:"
msgid "Year"
msgstr "Any"
msgctxt "field:aeat.347.report.party,amount:"
msgid "Operations Amount"
msgstr "Import operacions"
@ -548,6 +544,10 @@ msgctxt "model:ir.message,text:invalid_currency"
msgid "Currency in AEAT 347 report \"%(report)s\" must be Euro."
msgstr "La moneda de l'informe AEAT 347 \"%(report)s\" ha de ser Euro."
msgctxt "model:ir.message,text:msg_invalid_year"
msgid "The year \"%(year)s\" is not valid."
msgstr "L'any \"%(year)s\" no és vàlid."
msgctxt "model:ir.model.button,string:aeat_347_report_calculate_button"
msgid "Calculate"
msgstr "Calcula"

View file

@ -26,10 +26,6 @@ msgctxt "field:aeat.347.record,company:"
msgid "Company"
msgstr "Empresa"
msgctxt "field:aeat.347.record,fiscalyear:"
msgid "Fiscal Year"
msgstr "Ejercicio fiscal"
msgctxt "field:aeat.347.record,invoice:"
msgid "Invoice"
msgstr "Factura"
@ -50,6 +46,10 @@ msgctxt "field:aeat.347.record,party_tax_identifier:"
msgid "Party Tax Identifier"
msgstr "Identificador fiscal del tercero"
msgctxt "field:aeat.347.record,year:"
msgid "Year"
msgstr "Año"
msgctxt "field:aeat.347.report,amount:"
msgid "Amount"
msgstr "Importe"
@ -90,14 +90,6 @@ msgctxt "field:aeat.347.report,filename:"
msgid "File Name"
msgstr "Nombre archivo"
msgctxt "field:aeat.347.report,fiscalyear:"
msgid "Fiscal Year"
msgstr "Ejercicio fiscal"
msgctxt "field:aeat.347.report,fiscalyear_code:"
msgid "Fiscal Year Code"
msgstr "Año ejercicio fiscal"
msgctxt "field:aeat.347.report,on_behalf_third_party_limit:"
msgid "On Behalf of Third Party Limit (3)"
msgstr "Límite por cuenta de tercero (3)"
@ -154,6 +146,10 @@ msgctxt "field:aeat.347.report,type:"
msgid "Statement Type"
msgstr "Tipo de declaración"
msgctxt "field:aeat.347.report,fiscalyear:"
msgid "Year"
msgstr "Any"
msgctxt "field:aeat.347.report.party,amount:"
msgid "Operations Amount"
msgstr "Importe operaciones"
@ -549,6 +545,10 @@ msgctxt "model:ir.message,text:invalid_currency"
msgid "Currency in AEAT 347 report \"%(report)s\" must be Euro."
msgstr "La moneda del informe AEAT 349 \"%(report)s\" debe ser Euro."
msgctxt "model:ir.message,text:msg_invalid_year"
msgid "The year \"%(year)s\" is not valid."
msgstr "El año \"%(year)s\" no es correcto."
msgctxt "model:ir.model.button,string:aeat_347_report_calculate_button"
msgid "Calculate"
msgstr "Calcular"

View file

@ -6,5 +6,8 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="invalid_currency">
<field name="text">Currency in AEAT 347 report "%(report)s" must be Euro.</field>
</record>
<record model="ir.message" id="msg_invalid_year">
<field name="text">The year "%(year)s" is not valid.</field>
</record>
</data>
</tryton>

View file

@ -289,8 +289,7 @@ Generate 347 Report::
>>> Report = Model.get('aeat.347.report')
>>> report = Report()
>>> report.fiscalyear = fiscalyear
>>> report.fiscalyear_code = 2013
>>> report.year = today.year
>>> report.company_vat = '123456789'
>>> report.contact_name = 'Guido van Rosum'
>>> report.contact_phone = '987654321'

View file

@ -2,8 +2,8 @@
<!--The COPYRIGHT file at the top level of this repository
contains the full copyright notices and license terms. -->
<form>
<label name="fiscalyear"/>
<field name="fiscalyear"/>
<label name="year"/>
<field name="year"/>
<label name="month"/>
<field name="month"/>
<label name="party_tax_identifier"/>

View file

@ -2,7 +2,7 @@
<!--The COPYRIGHT file at the top level of this repository
contains the full copyright notices and license terms. -->
<tree>
<field name="fiscalyear"/>
<field name="year"/>
<field name="month"/>
<field name="party_tax_identifier"/>
<field name="invoice"/>

View file

@ -7,10 +7,8 @@ contains the full copyright notices and license terms. -->
<label string="Declaration Number" id="declaration_number"/>
<field name="id"/>
<newline/>
<label name="fiscalyear"/>
<field name="fiscalyear"/>
<label name="fiscalyear_code"/>
<field name="fiscalyear_code"/>
<label name="year"/>
<field name="year"/>
<label name="company_vat"/>
<field name="company_vat"/>
<label name="previous_number"/>

View file

@ -3,7 +3,7 @@
contains the full copyright notices and license terms. -->
<tree>
<field name="company"/>
<field name="fiscalyear"/>
<field name="year"/>
<field name="state"/>
<field name="calculation_date" widget="date"/>
<button name="draft" tree_invisible="1"/>