Skip to content

Commit

Permalink
[16.0] [IMP] l10n_ro_fiscal_validation - Improve fiscal validation nu…
Browse files Browse the repository at this point in the history
…mber of vat numbers and request linit.
  • Loading branch information
feketemihai committed Sep 27, 2024
1 parent ed949e5 commit 3b6eb65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
5 changes: 5 additions & 0 deletions l10n_ro_fiscal_validation/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Usage

A cron is set to update partners daily.

You can set system parameters:

l10n_ro_fiscal_validation_limit - to change the number of CUI's sent
l10n_ro_fiscal_validation_timeout - request timeout

Bug Tracker
===========

Expand Down
31 changes: 24 additions & 7 deletions l10n_ro_fiscal_validation/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
import time

import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,8 +39,16 @@ def update_l10n_ro_vat_subjected(self):
anaf_dict.append(partner.l10n_ro_vat_number)
chunk = []
chunks = []
# Process 500 vat numbers once
max_no = 499
# Process 100 vat numbers once, this is maximum allowed by ANAF
get_param = self.env["ir.config_parameter"].sudo().get_param
l10n_ro_fiscal_validation_limit = get_param(
"l10n_ro_fiscal_validation_limit", "100"
)
max_no = safe_eval(l10n_ro_fiscal_validation_limit)
l10n_ro_fiscal_validation_timeout = get_param(
"l10n_ro_fiscal_validation_timeout", "30"
)
max_timeout = safe_eval(l10n_ro_fiscal_validation_timeout)
for position in range(0, len(anaf_dict), max_no):
chunk = anaf_dict[position : position + max_no]
chunks.append(chunk)
Expand All @@ -49,7 +59,7 @@ def update_l10n_ro_vat_subjected(self):
anaf_ask.append({"cui": int(item), "data": check_date})
try:
res = requests.post(
ANAF_BULK_URL, json=anaf_ask, headers=headers, timeout=30
ANAF_BULK_URL, json=anaf_ask, headers=headers, timeout=max_timeout
)
if res.status_code == 200:
result = {}
Expand All @@ -58,11 +68,18 @@ def update_l10n_ro_vat_subjected(self):
except Exception:
_logger.warning("ANAF sync not working: %s" % res.content)
if result.get("correlationId"):
time.sleep(3)
resp = False
try:
resp = requests.get(
ANAF_CORR % result["correlationId"], timeout=30
retry = Retry(

Check warning on line 73 in l10n_ro_fiscal_validation/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

l10n_ro_fiscal_validation/models/res_partner.py#L73

Added line #L73 was not covered by tests
total=5,
backoff_factor=2,
status_forcelist=[403, 429, 500, 502, 503, 504],
)
adapter = HTTPAdapter(max_retries=retry)
session = requests.Session()
session.mount("https://", adapter)
resp = session.get(

Check warning on line 81 in l10n_ro_fiscal_validation/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

l10n_ro_fiscal_validation/models/res_partner.py#L78-L81

Added lines #L78 - L81 were not covered by tests
ANAF_CORR % result["correlationId"], timeout=max_timeout
)
except Exception as e:
_logger.warning("ANAF sync not working: %s" % e)
Expand Down
5 changes: 5 additions & 0 deletions l10n_ro_fiscal_validation/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
A cron is set to update partners daily.

You can set system parameters:

l10n_ro_fiscal_validation_limit - to change the number of CUI's sent
l10n_ro_fiscal_validation_timeout - request timeout
4 changes: 4 additions & 0 deletions l10n_ro_fiscal_validation/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ <h1><a class="toc-backref" href="#toc-entry-1">Installation</a></h1>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<p>A cron is set to update partners daily.</p>
<p>You can set system parameters:</p>
<blockquote>
l10n_ro_fiscal_validation_limit - to change the number of CUI’s sent
l10n_ro_fiscal_validation_timeout - request timeout</blockquote>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
Expand Down

0 comments on commit 3b6eb65

Please sign in to comment.