Skip to content

Commit 7c0d190

Browse files
[ADD] account_avatax_oca_log
1 parent 4b3934f commit 7c0d190

21 files changed

+822
-0
lines changed

Diff for: account_avatax_oca/models/avatax_rest_api.py

+5
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ def _enrich_result_lines_with_tax_rate(self, avatax_result):
203203
)
204204
return avatax_result
205205

206+
def _get_tax_post_process(self, data, result, doc_type):
207+
"""Inherit if needed"""
208+
return True
209+
206210
def get_tax(
207211
self,
208212
company_code,
@@ -321,6 +325,7 @@ def get_tax(
321325
if log_to_record:
322326
log_to_record.avatax_request_log = pprint.pformat(data, indent=1)
323327
log_to_record.avatax_response_log = pprint.pformat(result, indent=1)
328+
self._get_tax_post_process(data, result, doc_type)
324329
return self._enrich_result_lines_with_tax_rate(result)
325330

326331
def call(self, endpoint, company_code, doc_code, model=None, params=None):

Diff for: account_avatax_oca_log/README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<to update>

Diff for: account_avatax_oca_log/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models

Diff for: account_avatax_oca_log/__manifest__.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Account Avatax OCA Log",
3+
"version": "16.0.1.0.0",
4+
"author": "Open Source Integrators, ForgeFlow, Odoo Community Association (OCA)",
5+
"summary": "Add Logs to Avatax calls",
6+
"license": "AGPL-3",
7+
"category": "Accounting",
8+
"website": "https://github.com/OCA/account-fiscal-rule",
9+
"depends": ["account_avatax_oca"],
10+
"data": [
11+
"security/ir.model.access.csv",
12+
"data/cron.xml",
13+
"data/emails.xml",
14+
"views/res_config_settings_views.xml",
15+
"views/avatax_log_views.xml",
16+
],
17+
"auto_install": False,
18+
"development_status": "Alpha",
19+
}

Diff for: account_avatax_oca_log/data/cron.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<odoo noupdate="1">
2+
<record id="ir_cron_avatax_api_call_counter" model="ir.cron">
3+
<field name="name">Avatax API Call Counter</field>
4+
<field name="model_id" ref="account_avatax_oca.model_avatax_log" />
5+
<field name="state">code</field>
6+
<field name="code">model.avatax_api_call_counter()</field>
7+
<field name="interval_number">1</field>
8+
<field name="interval_type">days</field>
9+
<field name="numbercall">-1</field>
10+
<field name="active" eval="False" />
11+
</record>
12+
</odoo>

Diff for: account_avatax_oca_log/data/emails.xml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<odoo noupdate="1">
2+
<record id="reaching_limit_avatax_api_call_email" model="mail.template">
3+
<field name="name">Avatax API Call Reaching Limit</field>
4+
<field name="model_id" ref="base.model_res_company" />
5+
<field name="email_to">${ctx.get('email')}</field>
6+
<field name="subject">Avatax API Call Reaching Limit</field>
7+
<field name="auto_delete" eval="True" />
8+
<field name="lang">${object.partner_id.lang}</field>
9+
<field
10+
name="body_html"
11+
><![CDATA[
12+
<p>Hello.</p>
13+
<br/>
14+
<p>
15+
The number of Avatax API calls was over the limit yesterday.
16+
</p>
17+
<p style="font-family: arial, sans-serif; font-size: 12.8px;">Sale Transactions : ${ctx['sales_call_count']}</p>
18+
<p style="font-family: arial, sans-serif; font-size: 12.8px;">Invoice Transactions : ${ctx['invoices_call_count']}</p>
19+
<br/>
20+
<p style="font-family: arial, sans-serif; font-size: 12.8px;">Thank you for your cooperation.</p>
21+
<p style="font-family: arial, sans-serif; font-size: 12.8px;">Kind regards,</p>
22+
<p style="font-family: arial, sans-serif; font-size: 12.8px;">Sodexis Support Team.</p>
23+
]]>
24+
</field>
25+
</record>
26+
</odoo>

Diff for: account_avatax_oca_log/models/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import avalara_salestax
2+
from . import avatax_log
3+
from . import avatax_rest_api
4+
from . import res_config_settings

Diff for: account_avatax_oca_log/models/avalara_salestax.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) 2020 Open Source Integrators
2+
# Copyright (C) 2023 ForgeFlow, S.L.
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4+
from odoo import fields, models
5+
6+
7+
class AvalaraSalestax(models.Model):
8+
_inherit = "avalara.salestax"
9+
10+
def void_transaction(self, doc_code, doc_type):
11+
result = super().void_transaction(doc_code, doc_type)
12+
if not self.disable_tax_reporting:
13+
self.env["avatax.log"].sudo().create(
14+
{
15+
"avatax_request": result.get("id"),
16+
"avatax_response": result,
17+
"create_date_time": fields.Datetime.now(),
18+
"avatax_type": "cancel",
19+
}
20+
)
21+
return result

Diff for: account_avatax_oca_log/models/avatax_log.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright (C) 2020 Open Source Integrators
2+
# Copyright (C) 2023 ForgeFlow, S.L.
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4+
from ast import literal_eval
5+
6+
from dateutil.relativedelta import relativedelta
7+
8+
from odoo import fields, models
9+
10+
11+
class AvataxLog(models.Model):
12+
_name = "avatax.log"
13+
_description = "Avatax API call counter"
14+
15+
avatax_request = fields.Text()
16+
avatax_response = fields.Text()
17+
create_date_time = fields.Datetime("Create Date & Time")
18+
avatax_type = fields.Selection(
19+
[
20+
("SalesOrder", "Sale Transaction"),
21+
("SalesInvoice", "Invoice Transaction"),
22+
("cancel", "Cancel / Void"),
23+
("others", "Others"),
24+
]
25+
)
26+
27+
def name_get(self):
28+
result = []
29+
for log in self:
30+
name = "Avatax Log" + " " + str(log.id)
31+
result.append((log.id, name))
32+
return result
33+
34+
def avatax_api_call_counter(self):
35+
call_counter_config_values = (
36+
self.env["ir.config_parameter"]
37+
.sudo()
38+
.get_param("account_avatax_oca.call_counter_limit")
39+
)
40+
logs = self.env["avatax.log"]
41+
sales_call_count = 0
42+
invoices_call_count = 0
43+
if not self.avatax_request:
44+
logs = self.search([]).filtered(
45+
lambda p: p.create_date_time.date()
46+
== fields.date.today() - relativedelta(days=1)
47+
)
48+
sales_call_count = len(
49+
logs.filtered(lambda p: p.avatax_type == "SalesOrder")
50+
)
51+
invoices_call_count = len(
52+
logs.filtered(lambda p: p.avatax_type == "SalesInvoice")
53+
)
54+
if len(logs) > int(call_counter_config_values):
55+
avatax_api_call_notification = (
56+
self.env["ir.config_parameter"]
57+
.sudo()
58+
.get_param(
59+
"account_avatax_oca.avatax_api_call_notification_ids", default="[]"
60+
)
61+
)
62+
user_ids = literal_eval(avatax_api_call_notification)
63+
user_email = self.env["res.users"].browse(user_ids).mapped("login")
64+
email = ",".join(user_email)
65+
self.env.ref(
66+
"account_avatax_oca.reaching_limit_avatax_api_call_email"
67+
).with_context(
68+
sales_call_count=sales_call_count,
69+
invoices_call_count=invoices_call_count,
70+
email=email,
71+
).send_mail(
72+
self.env.company.id, force_send=True
73+
)

Diff for: account_avatax_oca_log/models/avatax_rest_api.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (C) 2020 Open Source Integrators
2+
# Copyright (C) 2023 ForgeFlow, S.L.
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4+
from odoo import fields
5+
6+
from odoo.addons.account_avatax_oca.models.account_avatax_rest_api import (
7+
AvaTaxRESTService,
8+
)
9+
10+
11+
class AvaTaxRESTServiceExtended(AvaTaxRESTService):
12+
def _get_tax_post_process(self, data, result, doc_type):
13+
self.config.env["avatax.log"].sudo().create(
14+
{
15+
"avatax_request": data,
16+
"avatax_response": result,
17+
"create_date_time": fields.Datetime.now(),
18+
"avatax_type": "SalesOrder"
19+
if doc_type in ["SalesOrder", "ReturnOrder"]
20+
else "SalesInvoice",
21+
}
22+
)
23+
return True

Diff for: account_avatax_oca_log/models/res_config_settings.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (C) 2020 Open Source Integrators
2+
# Copyright (C) 2023 ForgeFlow, S.L.
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
4+
from ast import literal_eval
5+
6+
from odoo import api, fields, models
7+
8+
9+
class Settings(models.TransientModel):
10+
_inherit = "res.config.settings"
11+
12+
avatax_api_call_notification_ids = fields.Many2many(
13+
"res.users",
14+
readonly=False,
15+
)
16+
call_counter_limit = fields.Integer(
17+
config_parameter="account_avatax_oca.call_counter_limit",
18+
default=100,
19+
)
20+
21+
@api.model
22+
def get_values(self):
23+
res = super(Settings, self).get_values()
24+
ICPSudo = self.env["ir.config_parameter"].sudo()
25+
get_param = "account_avatax_oca.avatax_api_call_notification_ids"
26+
calls = ICPSudo.get_param(get_param)
27+
res.update(
28+
avatax_api_call_notification_ids=[(6, 0, literal_eval(calls))]
29+
if calls
30+
else False,
31+
)
32+
return res
33+
34+
def set_values(self):
35+
res = super(Settings, self).set_values()
36+
ICPSudo = self.env["ir.config_parameter"].sudo()
37+
set_param = "account_avatax_oca.avatax_api_call_notification_ids"
38+
ICPSudo.set_param(set_param, self.avatax_api_call_notification_ids.ids)
39+
return res

Diff for: account_avatax_oca_log/readme/CONTRIBUTORS.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* Open Source Integrators (https://opensourceintegrators.com)
2+
* ForgeFlow (https://www.forgeflow.com)
3+
4+
* Jordi Ballester Alomar <[email protected]>

Diff for: account_avatax_oca_log/readme/DESCRIPTION.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module allows to log the API calls to Avatax.

Diff for: account_avatax_oca_log/readme/USAGE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
All the calls to Avatax are logged in Odoo. You can access to the logs in the menu Avatax log.

Diff for: account_avatax_oca_log/security/ir.model.access.csv

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
2+
access_avatax_log,avatax_log,model_avatax_log,base.group_user,1,1,1,1

Diff for: account_avatax_oca_log/static/description/icon.png

9.23 KB
Loading

0 commit comments

Comments
 (0)