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 Dec 22, 2021
1 parent ee3895d commit 97ecc07
Show file tree
Hide file tree
Showing 13 changed files with 866 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
121 changes: 121 additions & 0 deletions hr_expense_tax_adjust/models/hr_expense.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

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,
copy=False,
)
amount_by_group = fields.Binary(
string="Tax amount by group",
compute="_compute_expense_taxes_by_group",
help="Edit Tax amounts if you encounter rounding issues.",
)

@api.depends("quantity", "unit_amount", "tax_ids", "currency_id", "employee_id")
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()`.
"""
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]
amount_by_group.append((
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
))
expense.amount_by_group = amount_by_group

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_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
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]
tax_amount += amount_line[1]

# 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(tax, company_currency, expense.company_id, account_date)
# balance = expense.currency_id._convert(expense.tax_amount, company_currency, expense.company_id, account_date)
new_tax_amount += balance
line.update(
{
"debit": balance if balance > 0 else 0,
"credit": -balance if balance < 0 else 0,
"amount_currency": balance,
}
)
# destination move line
if "date_maturity" in line.keys():
amount_currency = line["amount_currency"]
amount = expense.currency_id._convert(amount_currency, company_currency, expense.company_id, account_date)
old_tax_amount = expense.currency_id._convert(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 97ecc07

Please sign in to comment.