Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][WIP][ADD] dominio_fiscal: add new module #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions dominio_base/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
============
Dominio Base
============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ac0188be54a8cd1b173ceff91eb5462d2b77915eb715a0afe1bf4e82af5bb329
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-Escodoo%2Fclog--addons-lightgray.png?logo=github
:target: https://github.com/Escodoo/clog-addons/tree/14.0/dominio_base
:alt: Escodoo/clog-addons

|badge1| |badge2| |badge3|


**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/Escodoo/clog-addons/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/Escodoo/clog-addons/issues/new?body=module:%20dominio_base%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Escodoo

Maintainers
~~~~~~~~~~~

This module is part of the `Escodoo/clog-addons <https://github.com/Escodoo/clog-addons/tree/14.0/dominio_base>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions dominio_base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
16 changes: 16 additions & 0 deletions dominio_base/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 - TODAY, Escodoo
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Dominio Base",
"summary": """
Dominio API Base""",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "Escodoo",
"website": "https://github.com/Escodoo/clog-addons",
"depends": ["base"],
"data": [
"views/res_company.xml",
],
}
7 changes: 7 additions & 0 deletions dominio_base/constants/dominio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2024 - TODAY, Kaynnan Lemes <kaynnan.lemes@escodoo.com.br>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

DOMINIO_ENVIRONMENTS = [("1", "Produção"), ("2", "Homologação")]
DOMINIO_ENVIRONMENT_DEFAULT = "2"
COOKIE = "did=s%3Av0%3A145b8a90-ea57-11eb-ae8a-877f15a4a518.QhUcTCGsMP28yWAB%2BYsUUZ5Gw4Srxf%2F0IDRkKPUQQHs; did_compat=s%3Av0%3A145b8a90-ea57-11eb-ae8a-877f15a4a518.QhUcTCGsMP28yWAB%2BYsUUZ5Gw4Srxf%2F0IDRkKPUQQHs" # noqa: B950
AUDIENCE = "409f91f6-dc17-44c8-a5d8-e0a1bafd8b67"
1 change: 1 addition & 0 deletions dominio_base/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import res_company
164 changes: 164 additions & 0 deletions dominio_base/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Copyright 2024 - TODAY, Kaynnan Lemes <kaynnan.lemes@escodoo.com.br>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import requests

from odoo import _, fields, models
from odoo.exceptions import UserError

from ..constants.dominio import (
AUDIENCE,
COOKIE,
DOMINIO_ENVIRONMENT_DEFAULT,
DOMINIO_ENVIRONMENTS,
)


class ResCompany(models.Model):

_inherit = "res.company"

dominio_environment = fields.Selection(
selection=DOMINIO_ENVIRONMENTS,
string="Environment",
default=DOMINIO_ENVIRONMENT_DEFAULT,
)
dominio_client_token = fields.Char(
string="Client ID Token",
)
dominio_secret_token = fields.Char(
string="Client Secret Token",
)
dominio_production_integration_key = fields.Char(
string="Integration Production Token"
)
dominio_homologation_integration_key = fields.Char(
string="Integration Homologation Token"
)

def get_dominio_environment(self):
"""
Retrieve the appropriate Dominio token based on the current environment setting.
Decide between the production and homologation (test) environment tokens by
examining the 'dominio_environment' field of the record.

Precondition:
- Call this method on a single record only. The method uses ensure_one to
enforce this rule.

Returns:
- str: The Dominio token. Return the production token if 'dominio_environment'
is set to "1"; otherwise, return the homologation token.

Raises:
- ValueError: If the method is called on a recordset containing more than one
record.
"""
self.ensure_one()
return (
self.dominio_production_integration_key
if self.dominio_environment == "1"
else self.dominio_homologation_integration_key
)

def _generate_dominio_token(self):
"""
Generate OAuth2 token for Dominio service.

Returns:
requests.Response: Response object containing the token.

Raises:
UserError: If an error occurs during token generation.
"""
try:
url = "https://auth.thomsonreuters.com/oauth/token"

payload = {
"grant_type": "client_credentials",
"client_id": self.dominio_client_token,
"client_secret": self.dominio_secret_token,
"audience": AUDIENCE,
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": COOKIE,
}

response = requests.post(url, data=payload, headers=headers)
response.raise_for_status()
return response

except requests.HTTPError as e:
raise UserError(
_("Error communicating with Dominio service: %s") % e
) from e

def _generate_key_integration(self):
"""
Generate integration key for Dominio activation.

Returns:
str: Integration key retrieved from the response.

Raises:
UserError: If an error occurs during integration key generation.
"""
try:
url = "https://api.onvio.com.br/dominio/integration/v1/activation/enable"

x_integration_key = self.get_dominio_environment()
token_response = self._generate_dominio_token()
token_data = token_response.json()
access_token = token_data.get("access_token")

headers = {
"Authorization": f"Bearer {access_token}",
"x-integration-key": x_integration_key,
}

response = requests.post(url, headers=headers)
response.raise_for_status()
response_data = response.json()

return {
"integrationKey": response_data.get("integrationKey"),
"access_token": access_token,
}

except requests.HTTPError as e:
raise UserError(
_("Error while generating integration key in Dominio: %s") % e
) from e

def _check_dominio_customer(self):
"""
Check customer authorization status in Dominio service.

Returns:
requests.Response: Response object containing the authorization status.

Raises:
UserError: If an error occurs while verifying customer authorization.
"""
try:
url = "https://api.onvio.com.br/dominio/integration/v1/activation/info"

x_integration_key = self.get_dominio_environment()
token_response = self._generate_dominio_token()
token_data = token_response.json()
access_token = token_data.get("access_token")

headers = {
"Authorization": "Bearer " + access_token,
"x-integration-key": x_integration_key,
}

response = requests.get(url, headers=headers)
response.raise_for_status()
return response

except requests.HTTPError as e:
raise UserError(
_("Error while verifying customer authorization: %s") % e
) from e
Empty file.
Empty file.
Empty file added dominio_base/readme/USAGE.rst
Empty file.
Binary file added dominio_base/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading