Skip to content

Commit

Permalink
ponto: date field to use is now configurable
Browse files Browse the repository at this point in the history
Add debug messages
Set raw_data in bank statement lines
  • Loading branch information
alexis-via authored and NL66278 committed Jan 24, 2023
1 parent c32a65f commit d34fac9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@

import pytz

from odoo import _, api, models
from odoo import _, api, fields, models

_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."""
Expand Down Expand Up @@ -46,7 +59,7 @@ def _ponto_obtain_statement_data(self, date_since, date_until):
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
Expand All @@ -69,7 +82,7 @@ def _ponto_retrieve_data(self, date_since):
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(
Expand All @@ -90,7 +103,7 @@ def _ponto_get_transaction_vals(self, transaction, sequence):
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,
Expand All @@ -106,20 +119,24 @@ def _ponto_get_transaction_vals(self, transaction, sequence):
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
date_xxx. We try to avoid this misleading naming by using datetime as
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)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _login(self, username, password):
"Accept": "application/json",
"Authorization": "Basic %s" % login,
}
_logger.debug(_("POST request on %s"), url)
response = requests.post(
url,
params={"grant_type": "client_credentials"},
Expand Down Expand Up @@ -67,6 +68,7 @@ def _get_request_headers(self, access_data):
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)
)
Expand Down Expand Up @@ -124,13 +126,14 @@ def _get_request(self, access_data, url, params):
"""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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit d34fac9

Please sign in to comment.