Skip to content

Commit

Permalink
[14.0][ADD] hr_expense_tax_adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-tubtim committed Jun 1, 2022
1 parent 3028302 commit 6f463f3
Show file tree
Hide file tree
Showing 15 changed files with 1,037 additions and 0 deletions.
88 changes: 88 additions & 0 deletions hr_expense_tax_adjust/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
======================
Expense Tax Adjustment
======================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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%2Fhr--expense-lightgray.png?logo=github
:target: https://github.com/OCA/hr-expense/tree/14.0/hr_expense_tax_adjust
:alt: OCA/hr-expense
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-expense-14-0/hr-expense-14-0-hr_expense_tax_adjust
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/289/14.0
:alt: Try me on Runbot

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

This module allows to edit tax amount before Post Journal Entries on Expense Report.

.. 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:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr-expense/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/hr-expense/issues/new?body=module:%20hr_expense_tax_adjust%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
~~~~~~~

* Ecosoft

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

* `Ecosoft <http://ecosoft.co.th>`__:

* Pimolnat Suntian <pimolnats@ecosoft.co.th>

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.

.. |maintainer-ps-tubtim| image:: https://github.com/ps-tubtim.png?size=40px
:target: https://github.com/ps-tubtim
:alt: ps-tubtim

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-ps-tubtim|

This module is part of the `OCA/hr-expense <https://github.com/OCA/hr-expense/tree/14.0/hr_expense_tax_adjust>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions hr_expense_tax_adjust/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
23 changes: 23 additions & 0 deletions hr_expense_tax_adjust/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Expense Tax Adjustment",
"version": "14.0.1.0.0",
"author": "Ecosoft, Odoo Community Association (OCA)",
"summary": "Allow to edit tax amount on expenses",
"website": "https://github.com/OCA/hr-expense",
"license": "AGPL-3",
"depends": ["hr_expense"],
"category": "Human Resources/Expenses",
"data": [
"views/asset_backend.xml",
"views/hr_expense_views.xml",
],
"qweb": [
"static/src/xml/tax_group.xml",
],
"installable": True,
"maintainers": ["ps-tubtim"],
"development_status": "Alpha",
}
3 changes: 3 additions & 0 deletions hr_expense_tax_adjust/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import hr_expense
215 changes: 215 additions & 0 deletions hr_expense_tax_adjust/models/hr_expense.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import ast
from collections import defaultdict

from odoo import api, fields, models
from odoo.tools.misc import formatLang


class HrExpense(models.Model):
_inherit = "hr.expense"

tax_amount = fields.Float(
digits="Account",
string="Tax",
compute="_compute_tax_amount",
store=True,
readonly=False,
)
amount_by_group = fields.Binary(
string="Tax amount by group",
compute="_compute_amount_by_group",
help="Edit Tax amounts if you encounter rounding issues.",
)
amount_by_group_txt = fields.Text(
string="Tax Amount Text",
default="[]",
compute="_compute_expense_taxes_by_group",
store=True,
readonly=False,
)
editable_tax_adjustment = fields.Boolean(
compute="_compute_editable_tax_adjustment",
string="Edit Tax Adjustment",
)

@api.depends("state", "sheet_id.state")
def _compute_editable_tax_adjustment(self):
"""Editable tax adjustment on expense state
'Draft', 'Submited' and 'Approved'"""
for rec in self:
rec.editable_tax_adjustment = (
(
rec.sheet_id.state in ["draft", "submit", "approve"]
or rec.state == "draft"
)
and True
or False
)

@api.depends("amount_by_group_txt")
def _compute_amount_by_group(self):
for expense in self:
expense.amount_by_group = ast.literal_eval(
expense.amount_by_group_txt or "[]"
)

@api.depends("amount_by_group")
def _compute_tax_amount(self):
for expense in self:
tax_amount = 0.0
for amount_line in expense.amount_by_group:
tax_amount += amount_line[1]
expense.tax_amount = tax_amount

@api.depends("quantity", "unit_amount", "tax_ids", "currency_id", "tax_amount")
def _compute_amount(self):
"""Override the `_compute_amount()`."""
self._compute_tax_amount() # Make sure that tax_amount updated
for expense in self:
taxes = expense.tax_ids.with_context(round=True).compute_all(
expense.unit_amount,
expense.currency_id,
expense.quantity,
expense.product_id,
expense.employee_id.user_id.partner_id,
)
expense.untaxed_amount = taxes.get("total_excluded", 0.0)
expense.total_amount = expense.untaxed_amount + expense.tax_amount

@api.depends("quantity", "unit_amount", "tax_ids", "currency_id", "employee_id")
def _compute_expense_taxes_by_group(self):
for expense in self:
partner_id = expense.employee_id.user_id.partner_id
lang_env = expense.with_context(lang=partner_id.lang).env
taxes = expense.tax_ids.with_context(round=True).compute_all(
expense.unit_amount,
expense.currency_id,
expense.quantity,
expense.product_id,
expense.employee_id.user_id.partner_id,
)
base_amount = taxes.get("total_excluded", 0.0)

tax_group_mapping = defaultdict(
lambda: {
"base_amount": base_amount,
"tax_amount": 0.0,
"tax_name": "",
}
)

for tax in expense.tax_ids.flatten_taxes_hierarchy():
tax_group_vals = tax_group_mapping[tax.id]
tax_group_vals["tax_name"] = tax.name
for tax_line in taxes["taxes"]:
tax_id = tax_line["id"]
tax_group_vals = tax_group_mapping[tax_id]
tax_group_vals["tax_amount"] += tax_line["amount"]
amount_by_group = []
for tax_group in tax_group_mapping.keys():
tax_group_vals = tax_group_mapping[tax_group]
if isinstance(tax_group, models.NewId):
tax_group = tax_group.origin
amount_by_group_str = "['{}', {}, {}, '{}', '{}', {}, {}]".format(
tax_group_vals["tax_name"],
tax_group_vals["tax_amount"],
tax_group_vals["base_amount"],
formatLang(
lang_env,
tax_group_vals["tax_amount"],
currency_obj=expense.currency_id,
),
formatLang(
lang_env,
tax_group_vals["base_amount"],
currency_obj=expense.currency_id,
),
len(tax_group_mapping),
tax_group,
)
amount_by_group.append(amount_by_group_str)
expense.amount_by_group_txt = "[{}]".format(", ".join(amount_by_group))

# def _get_expense_account_destination(self):
# self.ensure_one()
# # support module `hr_expense_advance_clearing` for case clearing
# if (
# self.sheet_id._fields.get("advance_sheet_id", False)
# and self.sheet_id.advance_sheet_id
# ):
# account_dest = self.env["account.account"]
# if not self.employee_id.sudo().address_home_id:
# raise UserError(
# _(
# "No Home Address found for the employee %s, please configure one."
# )
# % (self.employee_id.name)
# )
# partner = self.employee_id.sudo().address_home_id.with_company(
# self.company_id
# )
# # clearing is reverse payable to receivable
# account_dest = (
# partner.property_account_receivable_id.id
# or partner.parent_id.property_account_receivable_id.id
# )
# return account_dest
# return super()._get_expense_account_destination()

def _get_account_move_line_values(self):
move_line_values_by_expense = super()._get_account_move_line_values()
for expense in self:
# Prepare data
account_dst = expense._get_expense_account_destination()
account_date = (
expense.sheet_id.accounting_date
or expense.date
or fields.Date.context_today(expense)
)
company_currency = expense.company_id.currency_id
new_tax_amount = 0.0
old_tax_amount = 0.0
amount_by_group = dict()
for amount_line in expense.amount_by_group:
amount_by_group[amount_line[0]] = amount_line[1]

Check warning on line 177 in hr_expense_tax_adjust/models/hr_expense.py

View check run for this annotation

Codecov / codecov/patch

hr_expense_tax_adjust/models/hr_expense.py#L177

Added line #L177 was not covered by tests

# Update move_line_values_by_expense
for line in move_line_values_by_expense[expense.id]:
# taxes move lines
if line["name"] in amount_by_group.keys():
tax = amount_by_group[line["name"]]
balance = expense.currency_id._convert(

Check warning on line 184 in hr_expense_tax_adjust/models/hr_expense.py

View check run for this annotation

Codecov / codecov/patch

hr_expense_tax_adjust/models/hr_expense.py#L183-L184

Added lines #L183 - L184 were not covered by tests
tax, company_currency, expense.company_id, account_date
)
old_tax_amount += line["amount_currency"]
new_tax_amount += balance
line.update(

Check warning on line 189 in hr_expense_tax_adjust/models/hr_expense.py

View check run for this annotation

Codecov / codecov/patch

hr_expense_tax_adjust/models/hr_expense.py#L187-L189

Added lines #L187 - L189 were not covered by tests
{
"debit": balance if balance > 0 else 0,
"credit": -balance if balance < 0 else 0,
"amount_currency": balance,
}
)
# destination move line
if line["account_id"] == account_dst:
debit = line["debit"] or 0
credit = line["credit"] or 0
amount = debit - credit
old_tax_amount = expense.currency_id._convert(
old_tax_amount,
company_currency,
expense.company_id,
account_date,
)
balance = amount + (old_tax_amount - new_tax_amount)
line.update(
{
"debit": balance if balance > 0 else 0,
"credit": -balance if balance < 0 else 0,
"amount_currency": balance,
}
)
return move_line_values_by_expense
3 changes: 3 additions & 0 deletions hr_expense_tax_adjust/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Ecosoft <http://ecosoft.co.th>`__:

* Pimolnat Suntian <pimolnats@ecosoft.co.th>
1 change: 1 addition & 0 deletions hr_expense_tax_adjust/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to edit tax amount before Post Journal Entries on Expense Report.
Binary file added hr_expense_tax_adjust/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

0 comments on commit 6f463f3

Please sign in to comment.