Skip to content

Commit

Permalink
Merge pull request #832 from NextERP-Romania/14.0-imp_edi_ubl
Browse files Browse the repository at this point in the history
[14.0] adaugare import factura si modificare export_cius_ro
  • Loading branch information
feketemihai authored Feb 7, 2024
2 parents 1977be5 + daeae3f commit 79de3a0
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 39 deletions.
2 changes: 1 addition & 1 deletion l10n_ro_account_edi_ubl/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Romania - eFactura Account EDI UBL
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f0742680032ecf2d84a81327307c618d7b00e80c530e4ea063f68dbea1db3002
!! source digest: sha256:135a7168588c1a063969fa7e09e27650225a9ee3e08a25bed1bbe074c279aad1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Mature-brightgreen.png
Expand Down
2 changes: 1 addition & 1 deletion l10n_ro_account_edi_ubl/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"views/cius_template.xml",
],
"license": "AGPL-3",
"version": "14.0.1.26.1",
"version": "14.0.1.26.2",
"author": "Terrabit," "NextERP Romania," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/l10n-romania",
"installable": True,
Expand Down
66 changes: 34 additions & 32 deletions l10n_ro_account_edi_ubl/models/account_edi_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,41 @@ class AccountEdiXmlCIUSRO(models.Model):
def _export_cius_ro(self, invoice):
self.ensure_one()
# Create file content.
builder = self._get_xml_builder(invoice.company_id)
xml_content, errors = builder._export_invoice(invoice)
if errors:
raise UserError(
_("The following errors occurred while generating the " "XML file:\n%s")
% "\n".join(errors)
res = self.env["ir.attachment"]
if not invoice.l10n_ro_edi_transaction:
edi_document = invoice.edi_document_ids.filtered(
lambda l: l.edi_format_id.code == "cius_ro"
)
xml_content = xml_content.decode()
xml_content = xml_content.encode()
xml_name = builder._export_invoice_filename(invoice)
old_attachment = self.env["ir.attachment"].search(
[
("res_model", "=", "account.move"),
("res_id", "=", invoice.id),
("name", "=", xml_name),
]
)
edi_document = invoice.edi_document_ids.filtered(
lambda x: x.attachment_id in old_attachment.ids
)
if edi_document:
edi_document.attachment_id = False

old_attachment.unlink()
return self.env["ir.attachment"].create(
{
"name": xml_name,
"raw": xml_content,
"mimetype": "application/xml",
"res_model": "account.move",
"res_id": invoice.id,
}
)
if edi_document:
builder = self._get_xml_builder(invoice.company_id)
xml_content, errors = builder._export_invoice(invoice)
if errors:
raise UserError(
_(
"The following errors occurred while generating the "
"XML file:\n%s"
)
% "\n".join(errors)
)
xml_content = xml_content.decode()
xml_content = xml_content.encode()
xml_name = builder._export_invoice_filename(invoice)
old_attachment = edi_document.attachment_id
if old_attachment:
edi_document.attachment_id = False
old_attachment.unlink()
res = self.env["ir.attachment"].create(
{
"name": xml_name,
"raw": xml_content,
"mimetype": "application/xml",
"res_model": "account.move",
"res_id": invoice.id,
}
)
else:
res = invoice._get_edi_attachment(self)
return res

def _export_invoice_filename(self, invoice):
return f"{invoice.name.replace('/', '_')}_cius_ro.xml"
Expand Down
90 changes: 86 additions & 4 deletions l10n_ro_account_edi_ubl/models/account_edi_xml_cius_ro.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Copyright (C) 2022 NextERP Romania
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from base64 import b64decode, b64encode

import requests

from odoo import _, models

Expand Down Expand Up @@ -49,7 +52,10 @@ def _get_tax_category_list(self, invoice, taxes):
# for vals in vals_list:
# vals.pop('tax_exemption_reason', None)
for vals in vals_list:
if "Invers" in taxes.name:
word_to_check = "Invers"
if any(
word_to_check.lower() in word.lower() for word in taxes.mapped("name")
):
vals["id"] = "AE"
vals["tax_category_code"] = "AE"
vals["tax_exemption_reason_code"] = "VATEX-EU-AE"
Expand Down Expand Up @@ -87,9 +93,12 @@ def _get_invoice_line_item_vals(self, line, taxes_vals):
vals = super()._get_invoice_line_item_vals(line, taxes_vals)
vals["description"] = vals["description"][:200]
vals["name"] = vals["name"][:100]
if vals["classified_tax_category_vals"][0]["tax_category_code"] == "AE":
vals["classified_tax_category_vals"][0]["tax_exemption_reason_code"] = ""
vals["classified_tax_category_vals"][0]["tax_exemption_reason"] = ""
if vals["classified_tax_category_vals"]:
if vals["classified_tax_category_vals"][0]["tax_category_code"] == "AE":
vals["classified_tax_category_vals"][0][
"tax_exemption_reason_code"
] = ""
vals["classified_tax_category_vals"][0]["tax_exemption_reason"] = ""
return vals

def _get_invoice_line_price_vals(self, line):
Expand Down Expand Up @@ -219,3 +228,76 @@ def _import_fill_invoice_line_taxes(
return super()._import_fill_invoice_line_taxes(
journal, tax_nodes, invoice_line_form, inv_line_vals, logs
)

def _import_invoice(self, journal, filename, tree, existing_invoice=None):
invoice = super(AccountEdiXmlCIUSRO, self)._import_invoice(
journal, filename, tree, existing_invoice=existing_invoice
)
additional_docs = tree.findall("./{*}AdditionalDocumentReference")
if len(additional_docs) == 0:
res = self.l10n_ro_renderAnafPdf(invoice)
if not res:
pdf = (
self.env.ref("account.account_invoices_without_payment")
.sudo()
._render_qweb_pdf([invoice.id])
)
b64_pdf = b64encode(pdf[0])
self.l10n_ro_addPDF_from_att(invoice, b64_pdf)
return invoice

def l10n_ro_renderAnafPdf(self, invoice):
inv_attachments = self.env["ir.attachment"].search(
[
("res_model", "=", "account.move"),
("res_id", "=", invoice.id),
]
)
attachment = inv_attachments.filtered(
lambda x: f"{invoice.l10n_ro_edi_transaction}.xml" in x.name
)
if not attachment:
return False
headers = {"Content-Type": "text/plain"}
xml = b64decode(attachment.datas)
val1 = "refund" in invoice.move_type and "FCN" or "FACT1"
val2 = "DA"
try:
res = requests.post(
f"https://webservicesp.anaf.ro/prod/FCTEL/rest/transformare/{val1}/{val2}",
data=xml,
headers=headers,
timeout=10,
)
if "The requested URL was rejected" in res.text:
xml = xml.replace(
b'xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../../UBL-2.1(1)/xsd/maindoc/UBLInvoice-2.1.xsd"', # noqa: B950
"",
)
res = requests.post(
f"https://webservicesp.anaf.ro/prod/FCTEL/rest/transformare/{val1}/{val2}",
data=xml,
headers=headers,
timeout=10,
)
except Exception:
return False
else:
return self.l10n_ro_addPDF_from_att(invoice, b64encode(res.content))

def l10n_ro_addPDF_from_att(self, invoice, pdf):
attachments = self.env["ir.attachment"].create(
{
"name": invoice.ref,
"res_id": invoice.id,
"res_model": "account.move",
"datas": pdf + b"=" * (len(pdf) % 3), # Fix incorrect padding
"type": "binary",
"mimetype": "application/pdf",
}
)
if attachments:
invoice.with_context(no_new_invoice=True).message_post(
attachment_ids=attachments.ids
)
return True
2 changes: 1 addition & 1 deletion l10n_ro_account_edi_ubl/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Romania - eFactura Account EDI UBL</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f0742680032ecf2d84a81327307c618d7b00e80c530e4ea063f68dbea1db3002
!! source digest: sha256:135a7168588c1a063969fa7e09e27650225a9ee3e08a25bed1bbe074c279aad1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Mature" src="https://img.shields.io/badge/maturity-Mature-brightgreen.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/l10n-romania/tree/14.0/l10n_ro_account_edi_ubl"><img alt="OCA/l10n-romania" src="https://img.shields.io/badge/github-OCA%2Fl10n--romania-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/l10n-romania-14-0/l10n-romania-14-0-l10n_ro_account_edi_ubl"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/l10n-romania&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>The module will generate E-Invoice XML files according to ANAF E-Invoicing service.</p>
Expand Down

0 comments on commit 79de3a0

Please sign in to comment.