Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][FIX] fatturapa withholding tax invoice received e-invoice 1.6 #1878

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions l10n_it_fatturapa/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ class WelfareFundDataLine(models.Model):
)


class WithholdingDataLine(models.Model):
_name = "withholding.data.line"
_description = 'E-invoice Withholding Data'

name = fields.Selection(
selection=[
('RT01', 'Natural Person'),
('RT02', 'Legal Person'),
('RT03', 'INPS'),
('RT04', 'ENASARCO'),
('RT05', 'ENPAM'),
('RT06', 'OTHER'),
],
string='Withholding Type'
)
amount = fields.Float('Withholding amount')
invoice_id = fields.Many2one(
'account.invoice', 'Related Invoice',
ondelete='cascade', index=True
)


class DiscountRisePrice(models.Model):
# _position = ['2.1.1.8', '2.2.1.10']
_name = "discount.rise.price"
Expand Down Expand Up @@ -324,15 +346,19 @@ class AccountInvoice(models.Model):
sender = fields.Selection(
[('CC', 'Assignee / Partner'), ('TZ', 'Third Person')], 'Sender')
# 2.1.1.5
# 2.1.1.5.1
ftpa_withholding_type = fields.Selection(
[('RT01', 'Natural Person'), ('RT02', 'Legal Person')],
'Withholding Type'
)
# 2.1.1.5.2
ftpa_withholding_amount = fields.Float('Withholding amount')
# fields migrated to new ftpa_withholding_ids
# # 2.1.1.5.1
# ftpa_withholding_type = fields.Selection(
# [('RT01', 'Natural Person'), ('RT02', 'Legal Person')],
# 'Withholding Type'
# )
# # 2.1.1.5.2
# ftpa_withholding_amount = fields.Float('Withholding amount')
# 2.1.1.5.2 2.1.1.5.3 2.1.1.5.4 mapped to l10n_it_withholding_tax fields

ftpa_withholding_ids = fields.One2many(
'withholding.data.line', 'invoice_id',
'Withholding', copy=False
)
# 2.1.1.7
welfare_fund_ids = fields.One2many(
'welfare.fund.data.line', 'invoice_id',
Expand Down
3 changes: 2 additions & 1 deletion l10n_it_fatturapa/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ access_fatturapa_related_ddt_manager,access_fatturapa_related_ddt_manager,model_
access_fatturapa_related_ddt,access_fatturapa_related_ddt,model_fatturapa_related_ddt,account.group_account_invoice,1,0,0,0
access_faturapa_summary_data_manager,access_faturapa_summary_data_manager,model_faturapa_summary_data,account.group_account_manager,1,1,1,1
access_faturapa_summary_data,access_faturapa_summary_data,model_faturapa_summary_data,account.group_account_invoice,1,0,0,0

access_withholding_data_line_manager,access_withholding_data_line_manager,model_withholding_data_line,account.group_account_manager,1,1,1,1
access_withholding_data_line,access_withholding_data_line,model_withholding_data_line,account.group_account_invoice,1,0,0,0
2 changes: 1 addition & 1 deletion l10n_it_fatturapa_in/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{
'name': 'ITA - Fattura elettronica - Ricezione',
'version': '12.0.1.18.2',
'version': '12.0.1.19.0',
"development_status": "Beta",
'category': 'Localization/Italy',
'summary': 'Ricezione fatture elettroniche',
Expand Down
38 changes: 38 additions & 0 deletions l10n_it_fatturapa_in/migrations/12.0.1.19.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2020 Sergio Corato <https://github.com/sergiocorato>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade


def create_withholding_data_lines(env):
# create ftpa_withholding_ids from ftpa_withholding_type and ftpa_withholding_amount
openupgrade.logged_query(
env.cr, """
INSERT INTO withholding_data_line
(
name,
amount,
invoice_id,
create_uid,
create_date,
write_date,
write_uid
)
SELECT
ai.ftpa_withholding_type,
ai.ftpa_withholding_amount,
Copy link
Member

@tafaRU tafaRU Oct 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sergiocorato il post migrated script viene eseguito dopo l'aggiornamento del modulo, di conseguenza questo campo ftpa_withholding_amount, nel momento dell'esecuzione dello script non è più presente: come fa pertanto a funzionare?

Su un nostro cliente che usa Odoo.sh otteniamo questo errore:

10/15/2020 16:00:29ERRORserverFailed to load registry Traceback (most recent call last): File "/home/odoo/src/odoo/odoo/modules/registry.py", line 86, in new odoo.modules.load_modules(registry._db, force_demo, status, update_module) File "/home/odoo/src/odoo/odoo/modules/loading.py", line 417, in load_modules force, status, report, loaded_modules, update_module, models_to_check) File "/home/odoo/src/odoo/odoo/modules/loading.py", line 313, in load_marked_modules perform_checks=perform_checks, models_to_check=models_to_check File "/home/odoo/src/odoo/odoo/modules/loading.py", line 227, in load_module_graph migrations.migrate_module(package, 'post') File "/home/odoo/src/odoo/odoo/modules/migration.py", line 190, in migrate_module migrate(self.cr, installed_version) File "/home/odoo/.local/lib/python3.6/site-packages/openupgradelib/openupgrade.py", line 1796, in wrapped_function if use_env2 else cr, version) File "/home/odoo/src/user/OCA/l10n-italy/l10n_it_fatturapa_in/migrations/12.0.1.19.0/post-migration.py", line 38, in migrate create_withholding_data_lines(env) File "/home/odoo/src/user/OCA/l10n-italy/l10n_it_fatturapa_in/migrations/12.0.1.19.0/post-migration.py", line 30, in create_withholding_data_lines """ File "/home/odoo/.local/lib/python3.6/site-packages/openupgradelib/openupgrade.py", line 1262, in logged_query cr.execute(query, args) File "/home/odoo/src/odoo/odoo/sql_db.py", line 148, in wrapper return f(self, *args, **kwargs) File "/home/odoo/src/odoo/odoo/sql_db.py", line 225, in execute res = self._obj.execute(query, params) psycopg2.ProgrammingError: column ai.ftpa_withholding_amount does not exist LINE 14: ai.ftpa_withholding_amount, ^

che mi pare riconducibile a quanto scrivo sopra. Condividi l'analisi o mi sta sfuggendo qualcosa?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vedi #1914

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cioè chi lo elimina ftpa_withholding_amount? Odoo stesso quando rimuovi il campo dal model?

Io aggiornato a 12.0.2.0.0.99.dev2 ma a questo punto non so perchè abbia funzionato

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odoo stesso quando rimuovi il campo dal model?

eh sì

aggiornato a 12.0.2.0.0.99.dev2 ma a questo punto non so perchè abbia funzionato

cioè hai usato #1914? funziona perché lo script di migrazione controlla se tale campo esiste prima di effettuare la query.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@tafaRU tafaRU Oct 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odoo stesso quando rimuovi il campo dal model?

Ho assunto di sì AFAIK, mi pare logico, @eLBati tu dici che non è così scontato? cc @sergiocorato

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In effetti assumevo di no, forse in vecchie versioni era no. Ma magari ora è sì 🤯 😁

Sarebbe da provare

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si, se esiste la colonna ftpa_withholding_amount la copia e la usa, altrimenti fa solo la ftpa_withholding_type

ai.id,
ai.create_uid,
ai.create_date,
ai.write_date,
ai.write_uid
FROM account_invoice ai
WHERE ai.ftpa_withholding_type IS NOT NULL;
"""
)


@openupgrade.migrate()
def migrate(env, version):
if not version:
return
create_withholding_data_lines(env)
8 changes: 5 additions & 3 deletions l10n_it_fatturapa_in/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,18 @@ def e_inv_dati_ritenuta(self):
error_message = ''
# ftpa_withholding_type is set when DatiRitenuta is set,
# withholding_tax is not set if no lines with Ritenuta = SI are found
if self.ftpa_withholding_type and not self.withholding_tax:
if self.ftpa_withholding_ids and not self.withholding_tax:
error_message += (_(
"E-bill contains DatiRitenuta but no lines subjected to Ritenuta was "
"found. Please manually check Withholding tax Amount\n"
))
if self.ftpa_withholding_amount != self.withholding_tax_amount:
if sum(self.ftpa_withholding_ids.mapped('amount'))\
!= self.withholding_tax_amount:
error_message += (_(
"E-bill contains ImportoRitenuta %s but created invoice has got"
" %s\n" % (
self.ftpa_withholding_amount, self.withholding_tax_amount
sum(self.ftpa_withholding_ids.mapped('amount')),
self.withholding_tax_amount
)
))
return error_message
Expand Down
89 changes: 89 additions & 0 deletions l10n_it_fatturapa_in/tests/data/IT01234567890_FPR11.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<p:FatturaElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPR12" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2">
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>05979361218</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>FPR11</ProgressivoInvio>
<FormatoTrasmissione>FPR12</FormatoTrasmissione>
<CodiceDestinatario>0000000</CodiceDestinatario>
<ContattiTrasmittente />
</DatiTrasmissione>
<CedentePrestatore>
<DatiAnagrafici>
<IdFiscaleIVA>
<IdPaese>IT</IdPaese>
<IdCodice>02780790107</IdCodice>
</IdFiscaleIVA>
<Anagrafica>
<Denominazione>SOCIETA' ALPHA SRL</Denominazione>
</Anagrafica>
<RegimeFiscale>RF01</RegimeFiscale>
</DatiAnagrafici>
<Sede>
<Indirizzo>VIALE ROMA 543</Indirizzo>
<CAP>07100</CAP>
<Comune>SASSARI</Comune>
<Provincia>SS</Provincia>
<Nazione>IT</Nazione>
</Sede>
</CedentePrestatore>
<CessionarioCommittente>
<DatiAnagrafici>
<CodiceFiscale>03533590174</CodiceFiscale>
<Anagrafica>
<Denominazione>BETA GAMMA</Denominazione>
</Anagrafica>
</DatiAnagrafici>
<Sede>
<Indirizzo>VIA TORINO 38-B</Indirizzo>
<CAP>00145</CAP>
<Comune>ROMA</Comune>
<Provincia>RM</Provincia>
<Nazione>IT</Nazione>
</Sede>
</CessionarioCommittente>
</FatturaElettronicaHeader>
<FatturaElettronicaBody>
<DatiGenerali>
<DatiGeneraliDocumento>
<TipoDocumento>TD01</TipoDocumento>
<Divisa>EUR</Divisa>
<Data>2020-01-09</Data>
<Numero>124</Numero>
<DatiRitenuta>
<TipoRitenuta>RT02</TipoRitenuta>
<ImportoRitenuta>46.00</ImportoRitenuta>
<AliquotaRitenuta>23.00</AliquotaRitenuta>
<CausalePagamento>Q</CausalePagamento>
</DatiRitenuta>
<DatiRitenuta>
<TipoRitenuta>RT04</TipoRitenuta>
<ImportoRitenuta>40.00</ImportoRitenuta>
<AliquotaRitenuta>4.00</AliquotaRitenuta>
<CausalePagamento>Q</CausalePagamento>
</DatiRitenuta>
<ImportoTotaleDocumento>1220.00</ImportoTotaleDocumento>
</DatiGeneraliDocumento>
</DatiGenerali>
<DatiBeniServizi>
<DettaglioLinee>
<NumeroLinea>1</NumeroLinea>
<Descrizione>Provvigione</Descrizione>
<Quantita>1.000</Quantita>
<PrezzoUnitario>1000.00000</PrezzoUnitario>
<PrezzoTotale>1000.00</PrezzoTotale>
<AliquotaIVA>22.00</AliquotaIVA>
<Ritenuta>SI</Ritenuta>
</DettaglioLinee>
<DatiRiepilogo>
<AliquotaIVA>22.00</AliquotaIVA>
<ImponibileImporto>1000.00</ImponibileImporto>
<Imposta>220.00</Imposta>
<EsigibilitaIVA>I</EsigibilitaIVA>
</DatiRiepilogo>
</DatiBeniServizi>
</FatturaElettronicaBody>
</p:FatturaElettronica>
24 changes: 24 additions & 0 deletions l10n_it_fatturapa_in/tests/fatturapa_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ def create_wt_23_50(self):
self.env.ref('l10n_it_causali_pagamento.a').id,
})

def create_wt_23_20q(self):
return self.env['withholding.tax'].create({
'name': '2320q',
'code': '2320q',
'account_receivable_id': self.payable_account_id,
'account_payable_id': self.payable_account_id,
'payment_term': self.env.ref('account.account_payment_term').id,
'rate_ids': [(0, 0, {'tax': 23.0, 'base': 0.2})],
'causale_pagamento_id':
self.env.ref('l10n_it_causali_pagamento.q').id,
})

def create_wt_4q(self):
return self.env['withholding.tax'].create({
'name': '4q',
'code': '4q',
'account_receivable_id': self.payable_account_id,
'account_payable_id': self.payable_account_id,
'payment_term': self.env.ref('account.account_payment_term').id,
'rate_ids': [(0, 0, {'tax': 4.0, 'base': 1.0})],
'causale_pagamento_id':
self.env.ref('l10n_it_causali_pagamento.q').id,
})

def create_res_bank(self):
return self.env['res.bank'].create({
'name': 'Banca generica',
Expand Down
11 changes: 11 additions & 0 deletions l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ def test_38_xml_import_dates(self):
self.assertEqual(invoices[0].fatturapa_attachment_in_id.invoices_date,
'18/12/2014 20/12/2014')

def test_39_xml_import_withholding(self):
self.wt = self.create_wt_4q()
self.wtq = self.create_wt_23_20q()
res = self.run_wizard('test39', 'IT01234567890_FPR11.xml')
invoice_id = res.get('domain')[0][2][0]
invoice = self.invoice_model.browse(invoice_id)
self.assertTrue(len(invoice.ftpa_withholding_ids), 2)
self.assertAlmostEquals(invoice.amount_total, 1220.0)
self.assertAlmostEquals(invoice.withholding_tax_amount, 86.0)
self.assertAlmostEquals(invoice.amount_net_pay, 1134.0)

def test_01_xml_link(self):
"""
E-invoice lines are created.
Expand Down
8 changes: 8 additions & 0 deletions l10n_it_fatturapa_in/views/account_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@
</tree>
</field>
</group>
<group string="Withholding">
<field name="ftpa_withholding_ids" nolabel="1" readonly="1">
<tree editable="bottom" string="Withholding Details">
<field name="name"/>
<field name="amount"/>
</tree>
</field>
</group>
<group string="Payments">
<field name="fatturapa_payments" nolabel="1" readonly="1">
<tree string="Payments">
Expand Down
Loading