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 committed Oct 2, 2023
1 parent 4b3934f commit c81b304
Show file tree
Hide file tree
Showing 21 changed files with 820 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

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)
return self._enrich_result_lines_with_tax_rate(result)

def call(self, endpoint, company_code, doc_code, model=None, params=None):
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca_log/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<to update>
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.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>
26 changes: 26 additions & 0 deletions account_avatax_oca_log/data/emails.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<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')}</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"
><![CDATA[
<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 : ${ctx['sales_call_count']}</p>
<p style="font-family: arial, sans-serif; font-size: 12.8px;">Invoice Transactions : ${ctx['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;">Sodexis 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
21 changes: 21 additions & 0 deletions account_avatax_oca_log/models/avalara_salestax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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 fields, models


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

def void_transaction(self, doc_code, doc_type):
result = super().void_transaction(doc_code, doc_type)
if not self.disable_tax_reporting:
self.env["avatax.log"].sudo().create(
{
"avatax_request": result.get("id"),
"avatax_response": result,
"create_date_time": fields.Datetime.now(),
"avatax_type": "cancel",
}
)
return result
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()
create_date_time = fields.Datetime("Create Date & Time")
avatax_type = fields.Selection(
[
("SalesOrder", "Sale Transaction"),
("SalesInvoice", "Invoice Transaction"),
("cancel", "Cancel / Void"),
("others", "Others"),
]
)

def name_get(self):
result = []
for log in self:
name = "Avatax Log" + " " + str(log.id)
result.append((log.id, name))
return result

def avatax_api_call_counter(self):
call_counter_config_values = (
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
if not self.avatax_request:
logs = self.search([]).filtered(
lambda p: p.create_date_time.date()
== fields.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 = (
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(
"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
)
21 changes: 21 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,21 @@
# 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 fields

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(
{
"avatax_request": data,
"avatax_response": result,
"create_date_time": fields.Datetime.now(),
"avatax_type": "SalesOrder"
if doc_type in ["SalesOrder", "ReturnOrder"]
else "SalesInvoice",
}
)
return True
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(
avatax_api_call_notification_ids=[(6, 0, literal_eval(calls))]
if calls
else False,
)
return res

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
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 c81b304

Please sign in to comment.