From 3b6eb65ef642591da8edbcf1955501746ed2cbe5 Mon Sep 17 00:00:00 2001 From: Mihai Fekete Date: Fri, 27 Sep 2024 12:39:22 +0300 Subject: [PATCH] [16.0] [IMP] l10n_ro_fiscal_validation - Improve fiscal validation number of vat numbers and request linit. --- l10n_ro_fiscal_validation/README.rst | 5 +++ .../models/res_partner.py | 31 ++++++++++++++----- l10n_ro_fiscal_validation/readme/USAGE.rst | 5 +++ .../static/description/index.html | 4 +++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/l10n_ro_fiscal_validation/README.rst b/l10n_ro_fiscal_validation/README.rst index 66473847e..f8243f739 100644 --- a/l10n_ro_fiscal_validation/README.rst +++ b/l10n_ro_fiscal_validation/README.rst @@ -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 =========== diff --git a/l10n_ro_fiscal_validation/models/res_partner.py b/l10n_ro_fiscal_validation/models/res_partner.py index 7f7793162..a2f958a9b 100644 --- a/l10n_ro_fiscal_validation/models/res_partner.py +++ b/l10n_ro_fiscal_validation/models/res_partner.py @@ -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__) @@ -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) @@ -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 = {} @@ -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( + 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( + ANAF_CORR % result["correlationId"], timeout=max_timeout ) except Exception as e: _logger.warning("ANAF sync not working: %s" % e) diff --git a/l10n_ro_fiscal_validation/readme/USAGE.rst b/l10n_ro_fiscal_validation/readme/USAGE.rst index 0cd5dc7bb..b81b272f8 100644 --- a/l10n_ro_fiscal_validation/readme/USAGE.rst +++ b/l10n_ro_fiscal_validation/readme/USAGE.rst @@ -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 diff --git a/l10n_ro_fiscal_validation/static/description/index.html b/l10n_ro_fiscal_validation/static/description/index.html index 7df88f109..ce38c5144 100644 --- a/l10n_ro_fiscal_validation/static/description/index.html +++ b/l10n_ro_fiscal_validation/static/description/index.html @@ -399,6 +399,10 @@

Installation

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