diff --git a/hr_expense_payment/README.rst b/hr_expense_payment/README.rst new file mode 100644 index 000000000..b82c96946 --- /dev/null +++ b/hr_expense_payment/README.rst @@ -0,0 +1,82 @@ +================== +HR Expense Payment +================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |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_payment + :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_payment + :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 allow payment link to expense. +After you register payment on expense sheet, it will link between expense sheet and payment. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Ecosoft + +Contributors +~~~~~~~~~~~~ + +* Saran Lim. + +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-Saran440| image:: https://github.com/Saran440.png?size=40px + :target: https://github.com/Saran440 + :alt: Saran440 + +Current `maintainer `__: + +|maintainer-Saran440| + +This module is part of the `OCA/hr-expense `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/hr_expense_payment/__init__.py b/hr_expense_payment/__init__.py new file mode 100644 index 000000000..c127efb6a --- /dev/null +++ b/hr_expense_payment/__init__.py @@ -0,0 +1,5 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models +from . import wizard +from .hooks import post_init_hook diff --git a/hr_expense_payment/__manifest__.py b/hr_expense_payment/__manifest__.py new file mode 100644 index 000000000..a0d156b95 --- /dev/null +++ b/hr_expense_payment/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "HR Expense Payment", + "version": "14.0.1.0.0", + "category": "Human Resources", + "author": "Tecnativa, Ecosoft, Odoo Community Association (OCA)", + "license": "AGPL-3", + "website": "https://github.com/OCA/hr-expense", + "depends": ["hr_expense"], + "data": [], + "installable": True, + "post_init_hook": "post_init_hook", +} diff --git a/hr_expense_payment/hooks.py b/hr_expense_payment/hooks.py new file mode 100644 index 000000000..997bf6842 --- /dev/null +++ b/hr_expense_payment/hooks.py @@ -0,0 +1,19 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import SUPERUSER_ID, api + + +def post_init_hook(cr, registry): + """ Trying to fill the source expense sheet in payments """ + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + sheets = env["hr.expense.sheet"].search([("payment_mode", "=", "own_account")]) + for sheet in sheets: + amls = sheet.account_move_id.mapped("line_ids") + reconcile = amls.mapped("full_reconcile_id") + aml_payment = reconcile.mapped("reconciled_line_ids").filtered( + lambda r: r not in amls + ) + payment = aml_payment.mapped("payment_id") + payment.write({"expense_sheet_ids": sheet.ids}) diff --git a/hr_expense_payment/models/__init__.py b/hr_expense_payment/models/__init__.py new file mode 100644 index 000000000..e9fa1d92d --- /dev/null +++ b/hr_expense_payment/models/__init__.py @@ -0,0 +1,5 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import account_payment +from . import hr_expense +from . import hr_expense_sheet diff --git a/hr_expense_payment/models/account_payment.py b/hr_expense_payment/models/account_payment.py new file mode 100644 index 000000000..0edcaea31 --- /dev/null +++ b/hr_expense_payment/models/account_payment.py @@ -0,0 +1,19 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class AccountPayment(models.Model): + _inherit = "account.payment" + + expense_sheet_ids = fields.Many2many( + comodel_name="hr.expense.sheet", + relation="payment_expense_sheet_rel", + column1="payment_id", + column2="sheet_id", + string="Expense sheet", + readonly=True, + copy=False, + ) diff --git a/hr_expense_payment/models/hr_expense.py b/hr_expense_payment/models/hr_expense.py new file mode 100644 index 000000000..ad0830489 --- /dev/null +++ b/hr_expense_payment/models/hr_expense.py @@ -0,0 +1,63 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class HrExpense(models.Model): + _inherit = "hr.expense" + + def _prepare_payment_expense_company( + self, + payment_methods, + journal, + different_currency, + journal_currency, + total_amount_currency, + ): + self.ensure_one() + commercial_partner_id = ( + self.employee_id.sudo().address_home_id.commercial_partner_id + ) + payment_dict = { + "date": self.date, + "amount": total_amount_currency, + "payment_type": "outbound", + "partner_type": "supplier", + "ref": self.name, + "journal_id": journal.id, + "currency_id": self.currency_id.id + if different_currency + else journal_currency.id, + "partner_id": commercial_partner_id.id, + "payment_method_id": payment_methods and payment_methods[0].id or False, + "expense_sheet_ids": self.sheet_id.ids, + } + return payment_dict + + def action_move_create(self): + move_group_by_sheet = super().action_move_create() + payment_list = [] + for expense in self: + if expense.payment_mode == "company_account": + total_amount_currency = expense.total_amount + different_currency = ( + expense.currency_id != expense.company_id.currency_id + ) + journal = expense.sheet_id.bank_journal_id + journal_currency = journal.currency_id or journal.company_id.currency_id + payment_methods = journal.outbound_payment_method_ids + # prepare payment dict value for case paid by company + payment_dict = expense._prepare_payment_expense_company( + payment_methods, + journal, + different_currency, + journal_currency, + total_amount_currency, + ) + payment_list.append(payment_dict) + # create payment and auto post from expense paid by company + if payment_list: + payment = self.env["account.payment"].create(payment_list) + payment.action_post() + return move_group_by_sheet diff --git a/hr_expense_payment/models/hr_expense_sheet.py b/hr_expense_payment/models/hr_expense_sheet.py new file mode 100644 index 000000000..4acab3067 --- /dev/null +++ b/hr_expense_payment/models/hr_expense_sheet.py @@ -0,0 +1,26 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class HrExpenseSheet(models.Model): + _inherit = "hr.expense.sheet" + + payment_ids = fields.Many2many( + comodel_name="account.payment", + relation="payment_expense_sheet_rel", + column1="sheet_id", + column2="payment_id", + string="Payment", + readonly=True, + copy=False, + ) + + def action_register_payment(self): + """ Send context when you register payment from expense sheet """ + action = super().action_register_payment() + if self._name == "hr.expense.sheet": + action["context"].update({"expense_sheet_ids": self.ids}) + return action diff --git a/hr_expense_payment/readme/CONTRIBUTORS.rst b/hr_expense_payment/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..cc6b23102 --- /dev/null +++ b/hr_expense_payment/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Saran Lim. diff --git a/hr_expense_payment/readme/DESCRIPTION.rst b/hr_expense_payment/readme/DESCRIPTION.rst new file mode 100644 index 000000000..2bfb486f3 --- /dev/null +++ b/hr_expense_payment/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module allow payment link to expense. +After you register payment on expense sheet, it will link between expense sheet and payment. diff --git a/hr_expense_payment/static/description/icon.png b/hr_expense_payment/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/hr_expense_payment/static/description/icon.png differ diff --git a/hr_expense_payment/static/description/index.html b/hr_expense_payment/static/description/index.html new file mode 100644 index 000000000..e6349f8e0 --- /dev/null +++ b/hr_expense_payment/static/description/index.html @@ -0,0 +1,422 @@ + + + + + + +HR Expense Payment + + + +
+

HR Expense Payment

+ + +

Beta License: AGPL-3 OCA/hr-expense Translate me on Weblate Try me on Runbot

+

This module allow payment link to expense. +After you register payment on expense sheet, it will link between expense sheet and payment.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Ecosoft
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

Saran440

+

This module is part of the OCA/hr-expense project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/hr_expense_payment/tests/__init__.py b/hr_expense_payment/tests/__init__.py new file mode 100644 index 000000000..36579e552 --- /dev/null +++ b/hr_expense_payment/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import test_hr_expense_payment diff --git a/hr_expense_payment/tests/test_hr_expense_payment.py b/hr_expense_payment/tests/test_hr_expense_payment.py new file mode 100644 index 000000000..b24ae55c3 --- /dev/null +++ b/hr_expense_payment/tests/test_hr_expense_payment.py @@ -0,0 +1,91 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# Copyright 2021 Ecosoft Co., Ltd (http://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import Form, TransactionCase + +from ..hooks import post_init_hook + + +class TestHrExpensePayment(TransactionCase): + def setUp(self): + super().setUp() + self.account_payment_register = self.env["account.payment.register"] + self.payment_journal = self.env["account.journal"].search( + [("type", "in", ["cash", "bank"])], limit=1 + ) + + company = self.env.ref("base.main_company") + self.expense_journal = self.env["account.journal"].create( + { + "name": "Purchase Journal - Test", + "code": "HRTPJ", + "type": "purchase", + "company_id": company.id, + } + ) + + self.expense_sheet = self.env["hr.expense.sheet"].create( + { + "employee_id": self.ref("hr.employee_admin"), + "name": "Expense test", + "journal_id": self.expense_journal.id, + } + ) + self.expense_sheet.approve_expense_sheets() + + self.expense = self.env["hr.expense"].create( + { + "name": "Expense test", + "employee_id": self.ref("hr.employee_admin"), + "product_id": self.ref("hr_expense.air_ticket"), + "unit_amount": 1, + "quantity": 10, + "sheet_id": self.expense_sheet.id, + } + ) + + def _get_payment_wizard(self, expense_sheet): + action = expense_sheet.action_register_payment() + ctx = action.get("context") + with Form( + self.account_payment_register.with_context(ctx), + view="account.view_account_payment_register_form", + ) as f: + f.journal_id = self.payment_journal + f.amount = self.expense_sheet.total_amount + register_payment = f.save() + return register_payment + + def test_post_init_hook(self): + self.expense_sheet.action_sheet_move_create() + payment_wizard = self._get_payment_wizard(self.expense_sheet) + payment_wizard.action_create_payments() + + payment = self.expense_sheet.payment_ids + + self.assertEqual(len(payment), 1) + self.assertEqual(len(payment.expense_sheet_ids), 1) + + payment.expense_sheet_ids = False + # Recompute many2one + payment = self.expense_sheet.payment_ids + + self.assertFalse(payment) + self.assertFalse(payment.expense_sheet_ids) + post_init_hook(self.env.cr, self.registry) + + self.assertEqual(len(self.expense_sheet.payment_ids), 1) + + def test_get_payment_vals(self): + self.expense_sheet.action_sheet_move_create() + payment_wizard = self._get_payment_wizard(self.expense_sheet) + self.assertFalse(self.expense_sheet.payment_ids) + payment_wizard.action_create_payments() + self.assertEqual(len(self.expense_sheet.payment_ids), 1) + + def test_action_sheet_move_create(self): + self.expense.payment_mode = "company_account" + self.assertFalse(self.expense_sheet.payment_ids) + self.expense_sheet.action_sheet_move_create() + self.assertEqual(len(self.expense_sheet.payment_ids), 1) diff --git a/hr_expense_payment/wizard/__init__.py b/hr_expense_payment/wizard/__init__.py new file mode 100644 index 000000000..09e22cb07 --- /dev/null +++ b/hr_expense_payment/wizard/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import account_payment_register diff --git a/hr_expense_payment/wizard/account_payment_register.py b/hr_expense_payment/wizard/account_payment_register.py new file mode 100644 index 000000000..dc4f40e1f --- /dev/null +++ b/hr_expense_payment/wizard/account_payment_register.py @@ -0,0 +1,30 @@ +# Copyright 2019 Tecnativa - Ernesto Tejeda +# Copyright 2020 Ecosoft Co., Ltd (https://ecosoft.co.th/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class AccountPaymentRegister(models.TransientModel): + _inherit = "account.payment.register" + + def _create_payment_vals_from_wizard(self): + payment_vals = super()._create_payment_vals_from_wizard() + expense_sheet_ids = self._context.get("expense_sheet_ids", False) + if expense_sheet_ids: + payment_vals.update(expense_sheet_ids=expense_sheet_ids) + return payment_vals + + def _create_payment_vals_from_batch(self, batch_result): + payment_vals = super()._create_payment_vals_from_batch(batch_result) + expense_sheet_ids = self._context.get("expense_sheet_ids", False) + if expense_sheet_ids: + move_line_ids = self.env["account.move.line"].browse( + batch_result["lines"].ids + ) + sheet_ids = self.env["hr.expense.sheet"].browse(expense_sheet_ids) + sheet_id = sheet_ids.filtered( + lambda l: l.account_move_id.id == move_line_ids.move_id.id + ) + payment_vals.update(expense_sheet_ids=sheet_id.ids) + return payment_vals diff --git a/setup/hr_expense_payment/odoo/addons/hr_expense_payment b/setup/hr_expense_payment/odoo/addons/hr_expense_payment new file mode 120000 index 000000000..1ad6a85f5 --- /dev/null +++ b/setup/hr_expense_payment/odoo/addons/hr_expense_payment @@ -0,0 +1 @@ +../../../../hr_expense_payment \ No newline at end of file diff --git a/setup/hr_expense_payment/setup.py b/setup/hr_expense_payment/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/hr_expense_payment/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)