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

[ADD][l10n_it_fatturapa_out] add method to convert euro to currency #2684

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions l10n_it_fatturapa_out/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'depends': [
'l10n_it_account',
'l10n_it_fatturapa',
'l10n_it_reverse_charge',
'l10n_it_split_payment',
],
'data': [
Expand Down
109 changes: 50 additions & 59 deletions l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ def _domain_ir_values(self):
return [('binding_model_id', '=', model_name),
('type', '=', 'ir.actions.report')]

@api.model
def _to_EUR(self, currency, amount, invoice, company=None, today=None):
# Dispatch exchange date to convert euros in strange currency
if today:
exchange_date = today
elif invoice and invoice.rc_purchase_invoice_id.date:
exchange_date = invoice.rc_purchase_invoice_id.date
elif invoice and invoice.date:
exchange_date = invoice.date
else:
exchange_date = fields.Date.today()

company = company or self.env.user.company_id
euro = self.env.ref('base.EUR')
if currency == euro:
return amount
return currency.with_context(date=exchange_date).compute(amount, euro)

report_print_menu = fields.Many2one(
comodel_name='ir.actions.actions',
domain=_domain_ir_values,
Expand Down Expand Up @@ -542,12 +560,14 @@ def setDatiGeneraliDocumento(self, invoice, body):
_('Invoice %s does not have a number.' % invoice.display_name))

TipoDocumento = invoice.fiscal_document_type_id.code
ImportoTotaleDocumento = invoice.amount_total
ImportoTotaleDocumento = self._to_EUR(
invoice.currency_id, invoice.amount_total, invoice=invoice)
if invoice.split_payment:
ImportoTotaleDocumento += invoice.amount_sp
ImportoTotaleDocumento += self._to_EUR(
invoice.currency_id, invoice.amount_sp, invoice=invoice)
body.DatiGenerali.DatiGeneraliDocumento = DatiGeneraliDocumentoType(
TipoDocumento=TipoDocumento,
Divisa=invoice.currency_id.name,
Divisa=self.env.ref('base.EUR').name,
Data=invoice.date_invoice,
Numero=invoice.number,
ImportoTotaleDocumento='%.2f' % float_round(ImportoTotaleDocumento, 2))
Expand Down Expand Up @@ -662,7 +682,9 @@ def setDettaglioLinea(
aliquota = line.invoice_line_tax_ids[0].amount
AliquotaIVA = '%.2f' % float_round(aliquota, 2)
line.ftpa_line_number = line_no
prezzo_unitario = self._get_prezzo_unitario(line)
prezzo_unitario = self._to_EUR(
line.currency_id, self._get_prezzo_unitario(line),
invoice=line.invoice_id)
DettaglioLinea = DettaglioLineeType(
NumeroLinea=str(line_no),
Descrizione=encode_for_export(line.name, 1000),
Expand All @@ -672,8 +694,21 @@ def setDettaglioLinea(
qta=line.quantity, precision=uom_precision),
UnitaMisura=line.uom_id and (
unidecode(line.uom_id.name)) or None,
PrezzoTotale='%.2f' % float_round(line.price_subtotal, 2),
PrezzoTotale='%.2f' % float_round(
self._to_EUR(line.currency_id, line.price_subtotal,
invoice=line.invoice_id), 2),
AliquotaIVA=AliquotaIVA)
if line.currency_id != self.env.ref('base.EUR'):
prezzo_unitario = DettaglioLinea.PrezzoTotale / DettaglioLinea.Quantita
DettaglioLinea.PrezzoUnitario = '{prezzo:.{precision}f}'.format(
prezzo=prezzo_unitario, precision=price_precision)
AltriDatiGestionali = AltriDatiGestionaliType(
TipoDato="Valuta",
RiferimentoTesto=line.currency_id.name,
RiferimentoNumero=self._get_prezzo_unitario(line),
RiferimentoData=line.invoice_id.date
)
DettaglioLinea.AltriDatiGestionali.append(AltriDatiGestionali)
DettaglioLinea.ScontoMaggiorazione.extend(
self.setScontoMaggiorazione(line))
if aliquota == 0.0:
Expand Down Expand Up @@ -734,8 +769,11 @@ def setDatiRiepilogo(self, invoice, body):
tax = tax_line.tax_id
riepilogo = DatiRiepilogoType(
AliquotaIVA='%.2f' % float_round(tax.amount, 2),
ImponibileImporto='%.2f' % float_round(tax_line.base, 2),
Imposta='%.2f' % float_round(tax_line.amount, 2)
ImponibileImporto='%.2f' % float_round(self._to_EUR(
invoice.currency_id, tax_line.base, invoice=invoice), 2),
Imposta='%.2f' % float_round(
self._to_EUR(invoice.currency_id, tax_line.amount,
invoice=invoice), 2)
)
if tax.amount == 0.0:
if not tax.kind_id:
Expand Down Expand Up @@ -778,7 +816,10 @@ def setDatiPagamento(self, invoice, body):
for move_line_id in payment_line_ids:
move_line = move_line_pool.browse(move_line_id)
ImportoPagamento = '%.2f' % float_round(
move_line.amount_currency or move_line.debit, 2)
self._to_EUR(invoice.currency_id,
move_line.amount_currency or
move_line.debit, invoice),
2)
# Create with only mandatory fields
DettaglioPagamento = DettaglioPagamentoType(
ModalitaPagamento=(
Expand Down Expand Up @@ -933,54 +974,4 @@ def exportFatturaPA(self):
context_partner = self.env.context.copy()
context_partner.update({'lang': partner.lang})
for invoice_ids in invoices_by_partner[partner]:
fatturapa, number = self.exportInvoiceXML(
company, partner, invoice_ids, context=context_partner)

attach = self.saveAttachment(fatturapa, number)
attachments |= attach

for invoice_id in invoice_ids:
inv = invoice_obj.browse(invoice_id)
inv.write({'fatturapa_attachment_out_id': attach.id})

action = {
'view_type': 'form',
'name': "Export Electronic Invoice",
'res_model': 'fatturapa.attachment.out',
'type': 'ir.actions.act_window',
}
if len(attachments) == 1:
action['view_mode'] = 'form'
action['res_id'] = attachments[0].id
else:
action['view_mode'] = 'tree,form'
action['domain'] = [('id', 'in', attachments.ids)]
return action

def generate_attach_report(self, inv):
binding_model_id = self.with_context(
lang=None).report_print_menu.binding_model_id.id
name = self.report_print_menu.name
report_model = self.env['ir.actions.report'].with_context(
lang=None
).search(
[('binding_model_id', '=', binding_model_id),
('name', '=', name)]
)
attachment, attachment_type = report_model.render_qweb_pdf(inv.ids)
att_id = self.env['ir.attachment'].create({
'name': inv.number,
'type': 'binary',
'datas': base64.encodebytes(attachment),
'datas_fname': '{}.pdf'.format(inv.number),
'res_model': 'account.invoice',
'res_id': inv.id,
'mimetype': 'application/x-pdf'
})
inv.write({
'fatturapa_doc_attachments': [(0, 0, {
'is_pdf_invoice_print': True,
'ir_attachment_id': att_id.id,
'description': _("Attachment generated by "
"electronic invoice export")})]
})
fatt