Skip to content

Commit

Permalink
[ADD] account_avatax_oca_log
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiBForgeFlow authored and BernatPForgeFlow committed Jun 13, 2024
1 parent c816411 commit d401cf2
Show file tree
Hide file tree
Showing 21 changed files with 854 additions and 0 deletions.
5 changes: 5 additions & 0 deletions account_avatax_oca/models/avatax_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ def _enrich_result_lines_with_tax_rate(self, avatax_result):
)
return avatax_result

def _get_tax_post_process(self, data, result, doc_type):
"""Inherit if needed"""
return True

Check warning on line 208 in account_avatax_oca/models/avatax_rest_api.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/avatax_rest_api.py#L208

Added line #L208 was not covered by tests

def get_tax(
self,
company_code,
Expand Down Expand Up @@ -321,6 +325,7 @@ def get_tax(
if log_to_record:
log_to_record.avatax_request_log = pprint.pformat(data, indent=1)
log_to_record.avatax_response_log = pprint.pformat(result, indent=1)
self._get_tax_post_process(data, result, doc_type)

Check warning on line 328 in account_avatax_oca/models/avatax_rest_api.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/avatax_rest_api.py#L328

Added line #L328 was not covered by tests
return self._enrich_result_lines_with_tax_rate(result)

def call(self, endpoint, company_code, doc_code, model=None, params=None):
Expand Down
90 changes: 90 additions & 0 deletions account_avatax_oca_log/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
======================
Account Avatax OCA Log
======================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f2ef9f0fced1ab2776c5c31c6e4595b45327f1f7b09c60843ff0ca2b58542d23
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |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-OCA%2Faccount--fiscal--rule-lightgray.png?logo=github
:target: https://github.com/OCA/account-fiscal-rule/tree/16.0/account_avatax_oca_log
:alt: OCA/account-fiscal-rule
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/account-fiscal-rule-16-0/account-fiscal-rule-16-0-account_avatax_oca_log
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/account-fiscal-rule&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to log the API calls to Avatax.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Usage
=====

All the calls to Avatax are logged in Odoo. You can access to the logs in the menu Avatax log.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-fiscal-rule/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/OCA/account-fiscal-rule/issues/new?body=module:%20account_avatax_oca_log%0Aversion:%2016.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
~~~~~~~

* Open Source Integrators
* ForgeFlow

Contributors
~~~~~~~~~~~~

* Open Source Integrators (https://opensourceintegrators.com)
* ForgeFlow (https://www.forgeflow.com)

* Jordi Ballester Alomar <[email protected]>

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

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/account-fiscal-rule <https://github.com/OCA/account-fiscal-rule/tree/16.0/account_avatax_oca_log>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions account_avatax_oca_log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions account_avatax_oca_log/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Account Avatax OCA Log",
"version": "16.0.1.0.0",
"author": "Open Source Integrators, ForgeFlow, Odoo Community Association (OCA)",
"summary": "Add Logs to Avatax calls",
"license": "AGPL-3",
"category": "Accounting",
"website": "https://github.com/OCA/account-fiscal-rule",
"depends": ["account_avatax_oca"],
"data": [
"security/ir.model.access.csv",
"data/cron.xml",
"data/emails.xml",
"views/res_config_settings_views.xml",
"views/avatax_log_views.xml",
],
"auto_install": False,
"development_status": "Alpha",
}
12 changes: 12 additions & 0 deletions account_avatax_oca_log/data/cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo noupdate="1">
<record id="ir_cron_avatax_api_call_counter" model="ir.cron">
<field name="name">Avatax API Call Counter</field>
<field name="model_id" ref="account_avatax_oca_log.model_avatax_log" />
<field name="state">code</field>
<field name="code">model.avatax_api_call_counter()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="active" eval="False" />
</record>
</odoo>
33 changes: 33 additions & 0 deletions account_avatax_oca_log/data/emails.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<odoo noupdate="1">
<record id="reaching_limit_avatax_api_call_email" model="mail.template">
<field name="name">Avatax API Call Reaching Limit</field>
<field name="model_id" ref="base.model_res_company" />
<field name="email_to">{{ ctx.get('email_to', False) }}</field>
<field name="subject">Avatax API Call Reaching Limit</field>
<field name="auto_delete" eval="True" />
<field name="lang">{{ object.partner_id.lang }}</field>
<field name="body_html" type="html">
<p>Hello.</p>
<br />
<p>
The number of Avatax API calls was over the limit yesterday.
</p>
<p
style="font-family: arial, sans-serif; font-size: 12.8px;"
>Sale Transactions : <t t-out="ctx.get('sales_call_count')" /></p>
<p
style="font-family: arial, sans-serif; font-size: 12.8px;"
>Invoice Transactions : <t t-out="ctx.get('invoices_call_count')" /></p>
<br />
<p
style="font-family: arial, sans-serif; font-size: 12.8px;"
>Thank you for your cooperation.</p>
<p
style="font-family: arial, sans-serif; font-size: 12.8px;"
>Kind regards,</p>
<p
style="font-family: arial, sans-serif; font-size: 12.8px;"
>Support Team.</p>
</field>
</record>
</odoo>
4 changes: 4 additions & 0 deletions account_avatax_oca_log/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import avalara_salestax
from . import avatax_log
from . import avatax_rest_api
from . import res_config_settings
20 changes: 20 additions & 0 deletions account_avatax_oca_log/models/avalara_salestax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) 2020 Open Source Integrators
# Copyright (C) 2023 ForgeFlow, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import models


class AvalaraSalestax(models.Model):
_inherit = "avalara.salestax"

def void_transaction(self, doc_code, doc_type):
result = super().void_transaction(doc_code, doc_type)

Check warning on line 11 in account_avatax_oca_log/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avalara_salestax.py#L11

Added line #L11 was not covered by tests
if not self.disable_tax_reporting:
self.env["avatax.log"].sudo().create(

Check warning on line 13 in account_avatax_oca_log/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avalara_salestax.py#L13

Added line #L13 was not covered by tests
{
"avatax_request": result.get("id"),
"avatax_response": result,
"avatax_type": "cancel",
}
)
return result

Check warning on line 20 in account_avatax_oca_log/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avalara_salestax.py#L20

Added line #L20 was not covered by tests
73 changes: 73 additions & 0 deletions account_avatax_oca_log/models/avatax_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2020 Open Source Integrators
# Copyright (C) 2023 ForgeFlow, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from ast import literal_eval

from dateutil.relativedelta import relativedelta

from odoo import fields, models


class AvataxLog(models.Model):
_name = "avatax.log"
_description = "Avatax API call counter"

avatax_request = fields.Text()
avatax_response = fields.Text()
avatax_type = fields.Selection(
[
("SalesOrder", "Sale Transaction"),
("SalesInvoice", "Invoice Transaction"),
("cancel", "Cancel / Void"),
("others", "Others"),
]
)

def name_get(self):
result = []

Check warning on line 27 in account_avatax_oca_log/models/avatax_log.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_log.py#L27

Added line #L27 was not covered by tests
for log in self:
name = "Avatax Log" + " " + str(log.id)
result.append((log.id, name))
return result

Check warning on line 31 in account_avatax_oca_log/models/avatax_log.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_log.py#L29-L31

Added lines #L29 - L31 were not covered by tests

def avatax_api_call_counter(self):
call_counter_config_values = (

Check warning on line 34 in account_avatax_oca_log/models/avatax_log.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_log.py#L34

Added line #L34 was not covered by tests
self.env["ir.config_parameter"]
.sudo()
.get_param("account_avatax_oca.call_counter_limit")
)
logs = self.env["avatax.log"]
sales_call_count = 0
invoices_call_count = 0
today = fields.Date.today()
yesterday = today - relativedelta(days=1)
logs = self.search(

Check warning on line 44 in account_avatax_oca_log/models/avatax_log.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_log.py#L39-L44

Added lines #L39 - L44 were not covered by tests
[
("create_date", ">=", yesterday),
("create_date", "<", today + relativedelta(days=1)),
]
)
sales_call_count = len(logs.filtered(lambda p: p.avatax_type == "SalesOrder"))
invoices_call_count = len(
logs.filtered(lambda p: p.avatax_type == "SalesInvoice")
)
if len(logs) > int(call_counter_config_values):
avatax_api_call_notification = (

Check warning on line 55 in account_avatax_oca_log/models/avatax_log.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_log.py#L55

Added line #L55 was not covered by tests
self.env["ir.config_parameter"]
.sudo()
.get_param(
"account_avatax_oca.avatax_api_call_notification_ids", default="[]"
)
)
user_ids = literal_eval(avatax_api_call_notification)
user_email = self.env["res.users"].browse(user_ids).mapped("login")
email = ",".join(user_email)
self.env.ref(

Check warning on line 65 in account_avatax_oca_log/models/avatax_log.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_log.py#L62-L65

Added lines #L62 - L65 were not covered by tests
"account_avatax_oca.reaching_limit_avatax_api_call_email"
).with_context(
sales_call_count=sales_call_count,
invoices_call_count=invoices_call_count,
email=email,
).send_mail(
self.env.company.id, force_send=True
)
19 changes: 19 additions & 0 deletions account_avatax_oca_log/models/avatax_rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2020 Open Source Integrators
# Copyright (C) 2023 ForgeFlow, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo.addons.account_avatax_oca.models.avatax_rest_api import AvaTaxRESTService


class AvaTaxRESTServiceExtended(AvaTaxRESTService):
def _get_tax_post_process(self, data, result, doc_type):
self.config.env["avatax.log"].sudo().create(

Check warning on line 10 in account_avatax_oca_log/models/avatax_rest_api.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_rest_api.py#L10

Added line #L10 was not covered by tests
{
"avatax_request": data,
"avatax_response": result,
"avatax_type": "SalesOrder"
if doc_type in ["SalesOrder", "ReturnOrder"]
else "SalesInvoice",
}
)
return True

Check warning on line 19 in account_avatax_oca_log/models/avatax_rest_api.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/avatax_rest_api.py#L19

Added line #L19 was not covered by tests
39 changes: 39 additions & 0 deletions account_avatax_oca_log/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (C) 2020 Open Source Integrators
# Copyright (C) 2023 ForgeFlow, S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from ast import literal_eval

from odoo import api, fields, models


class Settings(models.TransientModel):
_inherit = "res.config.settings"

avatax_api_call_notification_ids = fields.Many2many(
"res.users",
readonly=False,
)
call_counter_limit = fields.Integer(
config_parameter="account_avatax_oca.call_counter_limit",
default=100,
)

@api.model
def get_values(self):
res = super(Settings, self).get_values()
ICPSudo = self.env["ir.config_parameter"].sudo()
get_param = "account_avatax_oca.avatax_api_call_notification_ids"
calls = ICPSudo.get_param(get_param)
res.update(

Check warning on line 27 in account_avatax_oca_log/models/res_config_settings.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/res_config_settings.py#L23-L27

Added lines #L23 - L27 were not covered by tests
avatax_api_call_notification_ids=[(6, 0, literal_eval(calls))]
if calls
else False,
)
return res

Check warning on line 32 in account_avatax_oca_log/models/res_config_settings.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/res_config_settings.py#L32

Added line #L32 was not covered by tests

def set_values(self):
res = super(Settings, self).set_values()
ICPSudo = self.env["ir.config_parameter"].sudo()
set_param = "account_avatax_oca.avatax_api_call_notification_ids"
ICPSudo.set_param(set_param, self.avatax_api_call_notification_ids.ids)
return res

Check warning on line 39 in account_avatax_oca_log/models/res_config_settings.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca_log/models/res_config_settings.py#L35-L39

Added lines #L35 - L39 were not covered by tests
4 changes: 4 additions & 0 deletions account_avatax_oca_log/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Open Source Integrators (https://opensourceintegrators.com)
* ForgeFlow (https://www.forgeflow.com)

* Jordi Ballester Alomar <[email protected]>
1 change: 1 addition & 0 deletions account_avatax_oca_log/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to log the API calls to Avatax.
1 change: 1 addition & 0 deletions account_avatax_oca_log/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All the calls to Avatax are logged in Odoo. You can access to the logs in the menu Avatax log.
2 changes: 2 additions & 0 deletions account_avatax_oca_log/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_avatax_log,avatax_log,model_avatax_log,base.group_user,1,1,1,1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d401cf2

Please sign in to comment.