mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
ponto: date field to use is now configurable
Add debug messages Set raw_data in bank statement lines
This commit is contained in:
committed by
Ronald Portier (Therp BV)
parent
c32a65fe3c
commit
d34fac97eb
@@ -8,7 +8,7 @@ from datetime import datetime
|
||||
|
||||
import pytz
|
||||
|
||||
from odoo import _, api, models
|
||||
from odoo import _, api, fields, models
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -16,6 +16,19 @@ _logger = logging.getLogger(__name__)
|
||||
class OnlineBankStatementProviderPonto(models.Model):
|
||||
_inherit = "online.bank.statement.provider"
|
||||
|
||||
ponto_date_field = fields.Selection(
|
||||
[
|
||||
("execution_date", "Execution Date"),
|
||||
("value_date", "Value Date"),
|
||||
],
|
||||
string="Ponto Date Field",
|
||||
default="execution_date",
|
||||
help="Select the Ponto date field that will be used for "
|
||||
"the Odoo bank statement line date. If you change this parameter "
|
||||
"on a provider that already has transactions, you will have to "
|
||||
"purge the Ponto buffers.",
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_available_services(self):
|
||||
"""Each provider model must register its service."""
|
||||
@@ -46,7 +59,7 @@ class OnlineBankStatementProviderPonto(models.Model):
|
||||
new_transactions = []
|
||||
sequence = 0
|
||||
for transaction in lines:
|
||||
date = self._ponto_get_execution_datetime(transaction)
|
||||
date = self._ponto_get_transaction_datetime(transaction)
|
||||
if date < date_since or date > date_until:
|
||||
continue
|
||||
sequence += 1
|
||||
@@ -69,7 +82,7 @@ class OnlineBankStatementProviderPonto(models.Model):
|
||||
while transactions:
|
||||
lines.extend(transactions)
|
||||
latest_identifier = transactions[-1].get("id")
|
||||
earliest_datetime = self._ponto_get_execution_datetime(transactions[-1])
|
||||
earliest_datetime = self._ponto_get_transaction_datetime(transactions[-1])
|
||||
if earliest_datetime < date_since:
|
||||
break
|
||||
transactions = interface_model._get_transactions(
|
||||
@@ -90,7 +103,7 @@ class OnlineBankStatementProviderPonto(models.Model):
|
||||
if attributes.get(x)
|
||||
]
|
||||
ref = " ".join(ref_list)
|
||||
date = self._ponto_get_execution_datetime(transaction)
|
||||
date = self._ponto_get_transaction_datetime(transaction)
|
||||
vals_line = {
|
||||
"sequence": sequence,
|
||||
"date": date,
|
||||
@@ -106,7 +119,7 @@ class OnlineBankStatementProviderPonto(models.Model):
|
||||
vals_line["partner_name"] = attributes["counterpartName"]
|
||||
return vals_line
|
||||
|
||||
def _ponto_get_execution_datetime(self, transaction):
|
||||
def _ponto_get_transaction_datetime(self, transaction):
|
||||
"""Get execution datetime for a transaction.
|
||||
|
||||
Odoo often names variables containing date and time just xxx_date or
|
||||
@@ -114,12 +127,16 @@ class OnlineBankStatementProviderPonto(models.Model):
|
||||
much for variables and fields of type datetime.
|
||||
"""
|
||||
attributes = transaction.get("attributes", {})
|
||||
return self._ponto_datetime_from_string(attributes.get("executionDate"))
|
||||
if self.ponto_date_field == "value_date":
|
||||
datetime_str = attributes.get("valueDate")
|
||||
else:
|
||||
datetime_str = attributes.get("executionDate")
|
||||
return self._ponto_datetime_from_string(datetime_str)
|
||||
|
||||
def _ponto_datetime_from_string(self, date_str):
|
||||
def _ponto_datetime_from_string(self, datetime_str):
|
||||
"""Dates in Ponto are expressed in UTC, so we need to convert them
|
||||
to supplied tz for proper classification.
|
||||
"""
|
||||
dt = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
dt = datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
dt = dt.replace(tzinfo=pytz.utc).astimezone(pytz.timezone(self.tz or "utc"))
|
||||
return dt.replace(tzinfo=None)
|
||||
|
||||
@@ -35,6 +35,7 @@ class PontoInterface(models.AbstractModel):
|
||||
"Accept": "application/json",
|
||||
"Authorization": "Basic %s" % login,
|
||||
}
|
||||
_logger.debug(_("POST request on %s"), url)
|
||||
response = requests.post(
|
||||
url,
|
||||
params={"grant_type": "client_credentials"},
|
||||
@@ -67,6 +68,7 @@ class PontoInterface(models.AbstractModel):
|
||||
def _set_access_account(self, access_data, account_number):
|
||||
"""Set ponto account for bank account in access_data."""
|
||||
url = PONTO_ENDPOINT + "/accounts"
|
||||
_logger.debug(_("GET request on %s"), url)
|
||||
response = requests.get(
|
||||
url, params={"limit": 100}, headers=self._get_request_headers(access_data)
|
||||
)
|
||||
@@ -124,13 +126,14 @@ class PontoInterface(models.AbstractModel):
|
||||
"""Interact with Ponto to get next page of data."""
|
||||
headers = self._get_request_headers(access_data)
|
||||
_logger.debug(
|
||||
_("Get request to %s, with headers %s and params %s"), url, params, headers
|
||||
_("GET request to %s with headers %s and params %s"), url, params, headers
|
||||
)
|
||||
response = requests.get(url, params=params, headers=headers)
|
||||
return self._get_response_data(response)
|
||||
|
||||
def _get_response_data(self, response):
|
||||
"""Get response data for GET or POST request."""
|
||||
_logger.debug(_("HTTP answer code %s from Ponto"), response.status_code)
|
||||
if response.status_code not in (200, 201):
|
||||
raise UserError(
|
||||
_("Server returned status code %s: %s")
|
||||
|
||||
@@ -9,9 +9,14 @@
|
||||
/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//group[@name='main']" position="inside">
|
||||
<group name="ponto" attrs="{'invisible':[('service','!=','ponto')]}">
|
||||
<group
|
||||
name="ponto"
|
||||
string="Ponto Config"
|
||||
attrs="{'invisible':[('service','!=','ponto')]}"
|
||||
>
|
||||
<field name="username" string="Login" />
|
||||
<field name="password" string="Secret Key" />
|
||||
<field name="ponto_date_field" />
|
||||
</group>
|
||||
</xpath>
|
||||
</field>
|
||||
|
||||
Reference in New Issue
Block a user