diff --git a/account_payment_mode_auto_reconcile/README.rst b/account_payment_mode_auto_reconcile/README.rst new file mode 100644 index 0000000000..f605b5da50 --- /dev/null +++ b/account_payment_mode_auto_reconcile/README.rst @@ -0,0 +1,86 @@ +=================================== +Account Payment Mode Auto Reconcile +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:902d568e59e5a39571d93111bf87a096fdc41ff0866a796d2a15b339d3b0fc85 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Faccount--reconcile-lightgray.png?logo=github + :target: https://github.com/OCA/account-reconcile/tree/16.0/account_payment_mode_auto_reconcile + :alt: OCA/account-reconcile +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/account-reconcile-16-0/account-reconcile-16-0-account_payment_mode_auto_reconcile + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/account-reconcile&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a checkbox `auto_reconcile_outstanding_credits` on account +payment modes to allow automatic reconciliation on account invoices if it is +checked. + +Automatic reconciliation of outstanding credits will only happen on customer +invoices at validation if the payment mode is set or when the payment mode is +changed on an open invoice. If a payment mode using auto-reconcile is removed +from an open invoice, the existing auto reconciled payments will be removed. + +Another option `auto_reconcile_allow_partial` on account payment mode defines +if outstanding credits can be partially used for the auto reconciliation. + +**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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp + +Contributors +~~~~~~~~~~~~ + +* Akim Juillerat + +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. + +This module is part of the `OCA/account-reconcile `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_payment_mode_auto_reconcile/__init__.py b/account_payment_mode_auto_reconcile/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/account_payment_mode_auto_reconcile/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_payment_mode_auto_reconcile/__manifest__.py b/account_payment_mode_auto_reconcile/__manifest__.py new file mode 100644 index 0000000000..49065fa93b --- /dev/null +++ b/account_payment_mode_auto_reconcile/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +{ + "name": "Account Payment Mode Auto Reconcile", + "summary": "Reconcile outstanding credits according to payment mode", + "version": "16.0.1.0.0", + "category": "Banking addons", + "website": "https://github.com/OCA/account-reconcile", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "depends": [ + "account_payment_partner", + ], + "data": [ + "views/account_invoice.xml", + "views/account_payment_mode.xml", + ], + "demo": [ + "demo/account_payment_mode.xml", + ], +} diff --git a/account_payment_mode_auto_reconcile/demo/account_payment_mode.xml b/account_payment_mode_auto_reconcile/demo/account_payment_mode.xml new file mode 100644 index 0000000000..0f04218a32 --- /dev/null +++ b/account_payment_mode_auto_reconcile/demo/account_payment_mode.xml @@ -0,0 +1,9 @@ + + + + + + diff --git a/account_payment_mode_auto_reconcile/i18n/account_payment_mode_auto_reconcile.pot b/account_payment_mode_auto_reconcile/i18n/account_payment_mode_auto_reconcile.pot new file mode 100644 index 0000000000..090610b9e7 --- /dev/null +++ b/account_payment_mode_auto_reconcile/i18n/account_payment_mode_auto_reconcile.pot @@ -0,0 +1,98 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_payment_mode_auto_reconcile +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,field_description:account_payment_mode_auto_reconcile.field_account_payment_mode_auto_reconcile_allow_partial +msgid "Allow partial" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,help:account_payment_mode_auto_reconcile.field_account_payment_mode_auto_reconcile_allow_partial +msgid "Allows automatic partial reconciliation of outstanding credits" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,field_description:account_payment_mode_auto_reconcile.field_account_payment_mode_auto_reconcile_outstanding_credits +msgid "Auto reconcile" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.ui.view,arch_db:account_payment_mode_auto_reconcile.account_payment_mode_form_inherit +msgid "Auto reconcile outstanding credits" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: code:addons/account_payment_mode_auto_reconcile/models/account_invoice.py:160 +#, python-format +msgid "Changing payment mode will reconcile outstanding credits." +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: code:addons/account_payment_mode_auto_reconcile/models/account_invoice.py:150 +#, python-format +msgid "Changing payment mode will unreconcile existing auto reconciled payments." +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,field_description:account_payment_mode_auto_reconcile.field_account_invoice_display_payment_mode_warning +msgid "Display payment mode warning" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model,name:account_payment_mode_auto_reconcile.model_account_invoice +msgid "Invoice" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,help:account_payment_mode_auto_reconcile.field_account_payment_mode_auto_reconcile_same_journal +msgid "Only reconcile payment in the same journal than the invoice" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,field_description:account_payment_mode_auto_reconcile.field_account_payment_mode_auto_reconcile_same_journal +msgid "Only same journal" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model,name:account_payment_mode_auto_reconcile.model_account_partial_reconcile +msgid "Partial Reconcile" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model,name:account_payment_mode_auto_reconcile.model_account_payment_mode +msgid "Payment Modes" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,field_description:account_payment_mode_auto_reconcile.field_account_partial_reconcile_payment_mode_auto_reconcile +msgid "Payment mode auto reconcile" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,field_description:account_payment_mode_auto_reconcile.field_account_invoice_payment_mode_warning +msgid "Payment mode warning" +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: model:ir.model.fields,help:account_payment_mode_auto_reconcile.field_account_payment_mode_auto_reconcile_outstanding_credits +msgid "Reconcile automatically outstanding credits when an invoice using this payment mode is validated, or when this payment mode is defined on an open invoice." +msgstr "" + +#. module: account_payment_mode_auto_reconcile +#: code:addons/account_payment_mode_auto_reconcile/models/account_invoice.py:140 +#, python-format +msgid "Validating invoices with this payment mode will reconcile any outstanding credits." +msgstr "" + diff --git a/account_payment_mode_auto_reconcile/models/__init__.py b/account_payment_mode_auto_reconcile/models/__init__.py new file mode 100644 index 0000000000..ef4706f2f4 --- /dev/null +++ b/account_payment_mode_auto_reconcile/models/__init__.py @@ -0,0 +1,3 @@ +from . import account_move +from . import account_partial_reconcile +from . import account_payment_mode diff --git a/account_payment_mode_auto_reconcile/models/account_move.py b/account_payment_mode_auto_reconcile/models/account_move.py new file mode 100644 index 0000000000..5f987910e6 --- /dev/null +++ b/account_payment_mode_auto_reconcile/models/account_move.py @@ -0,0 +1,170 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from operator import itemgetter + +from odoo import _, api, fields, models + + +class AccountMove(models.Model): + + _inherit = "account.move" + + # Allow changing payment mode in open state + payment_mode_warning = fields.Char( + compute="_compute_payment_mode_warning", + ) + display_payment_mode_warning = fields.Boolean( + compute="_compute_payment_mode_warning", + ) + + def action_post(self): + res = super(AccountMove, self).action_post() + for invoice in self: + if invoice.move_type != "out_invoice": + continue + if not invoice.payment_mode_id.auto_reconcile_outstanding_credits: + continue + partial = invoice.payment_mode_id.auto_reconcile_allow_partial + invoice.with_context( + _payment_mode_auto_reconcile=True + ).auto_reconcile_credits(partial_allowed=partial) + return res + + def write(self, vals): + res = super(AccountMove, self).write(vals) + if "payment_mode_id" in vals or "state" in vals: + for invoice in self: + # Do not auto reconcile anything else than open customer inv + if invoice.state != "posted" or invoice.move_type != "out_invoice": + continue + invoice_lines = invoice.line_ids.filtered( + lambda l: l.display_type == "payment_term" + ) + # Auto reconcile if payment mode sets it + payment_mode = invoice.payment_mode_id + if payment_mode and payment_mode.auto_reconcile_outstanding_credits: + partial = payment_mode.auto_reconcile_allow_partial + invoice.with_context( + _payment_mode_auto_reconcile=True + ).auto_reconcile_credits(partial_allowed=partial) + # If the payment mode is not using auto reconcile we remove + # the existing reconciliations + elif any( + [ + invoice_lines.mapped("matched_credit_ids"), + invoice_lines.mapped("matched_debit_ids"), + ] + ): + invoice.auto_unreconcile_credits() + return res + + def auto_reconcile_credits(self, partial_allowed=True): + for invoice in self: + invoice._compute_payments_widget_to_reconcile_info() + + if not invoice.invoice_has_outstanding: + continue + credits_info = invoice.invoice_outstanding_credits_debits_widget + # Get outstanding credits in chronological order + # (using reverse because aml is sorted by date desc as default) + credits_dict = credits_info.get("content", False) + if invoice.payment_mode_id.auto_reconcile_same_journal: + credits_dict = invoice._filter_payment_same_journal(credits_dict) + sorted_credits = self._sort_credits_dict(credits_dict) + for credit in sorted_credits: + if ( + not partial_allowed + and credit.get("amount") > invoice.amount_residual + ): + continue + invoice.js_assign_outstanding_line(credit.get("id")) + + @api.model + def _sort_credits_dict(self, credits_dict): + """Sort credits dict according to their id (oldest recs first)""" + return sorted(credits_dict, key=itemgetter("id")) + + def _filter_payment_same_journal(self, credits_dict): + """Keep only credits on the same journal than the invoice.""" + self.ensure_one() + line_ids = [credit["id"] for credit in credits_dict] + lines = self.env["account.move.line"].search( + [("id", "in", line_ids), ("journal_id", "=", self.journal_id.id)] + ) + return [credit for credit in credits_dict if credit["id"] in lines.ids] + + def auto_unreconcile_credits(self): + for invoice in self: + payments_info = invoice.invoice_payments_widget + for payment in payments_info.get("content", []): + payment_aml = ( + self.env["account.payment"] + .browse(payment.get("account_payment_id")) + .line_ids + ) + + aml = payment_aml.filtered(lambda l: l.matched_debit_ids) + for apr in aml.matched_debit_ids: + if apr.amount != payment.get("amount"): + continue + if ( + apr.payment_mode_auto_reconcile + and apr.debit_move_id.move_id == invoice + ): + aml.remove_move_reconcile() + + @api.depends( + "move_type", "payment_mode_id", "payment_id", "state", "invoice_has_outstanding" + ) + def _compute_payment_mode_warning(self): + # TODO Improve me but watch out + for invoice in self: + existed_reconciliations = any( + [ + invoice.line_ids.mapped("matched_credit_ids"), + invoice.line_ids.mapped("matched_debit_ids"), + ] + ) + if invoice.move_type != "out_invoice" or ( + invoice.state == "posted" and invoice.payment_state != "paid" + ): + invoice.payment_mode_warning = "" + invoice.display_payment_mode_warning = False + continue + invoice.display_payment_mode_warning = True + if ( + invoice.state != "posted" + and invoice.payment_mode_id + and invoice.payment_mode_id.auto_reconcile_outstanding_credits + ): + invoice.payment_mode_warning = _( + "Validating invoices with this payment mode will reconcile" + " any outstanding credits." + ) + elif ( + invoice.state == "posted" + and invoice.payment_state != "paid" + and existed_reconciliations + and ( + not invoice.payment_mode_id + or not invoice.payment_mode_id.auto_reconcile_outstanding_credits + ) + ): + invoice.payment_mode_warning = _( + "Changing payment mode will unreconcile existing auto " + "reconciled payments." + ) + elif ( + invoice.state == "posted" + and invoice.payment_state != "paid" + and not existed_reconciliations + and invoice.payment_mode_id + and invoice.payment_mode_id.auto_reconcile_outstanding_credits + and invoice.invoice_has_outstanding + ): + invoice.payment_mode_warning = _( + "Changing payment mode will reconcile outstanding credits." + ) + else: + invoice.payment_mode_warning = "" + invoice.display_payment_mode_warning = False diff --git a/account_payment_mode_auto_reconcile/models/account_partial_reconcile.py b/account_payment_mode_auto_reconcile/models/account_partial_reconcile.py new file mode 100644 index 0000000000..06fb9b3e65 --- /dev/null +++ b/account_payment_mode_auto_reconcile/models/account_partial_reconcile.py @@ -0,0 +1,16 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import fields, models + + +class AccountPartialReconcile(models.Model): + + _inherit = "account.partial.reconcile" + + payment_mode_auto_reconcile = fields.Boolean() + + def create(self, vals): + if self.env.context.get("_payment_mode_auto_reconcile"): + for val in vals: + val["payment_mode_auto_reconcile"] = True + return super(AccountPartialReconcile, self).create(vals) diff --git a/account_payment_mode_auto_reconcile/models/account_payment_mode.py b/account_payment_mode_auto_reconcile/models/account_payment_mode.py new file mode 100644 index 0000000000..d4807c4755 --- /dev/null +++ b/account_payment_mode_auto_reconcile/models/account_payment_mode.py @@ -0,0 +1,25 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import fields, models + + +class AccountPaymentMode(models.Model): + + _inherit = "account.payment.mode" + + auto_reconcile_outstanding_credits = fields.Boolean( + string="Auto reconcile", + help="Reconcile automatically outstanding credits when an invoice " + "using this payment mode is validated, or when this payment mode " + "is defined on an open invoice.", + ) + auto_reconcile_allow_partial = fields.Boolean( + default=True, + string="Allow partial", + help="Allows automatic partial reconciliation of outstanding credits", + ) + auto_reconcile_same_journal = fields.Boolean( + default=False, + string="Only same journal", + help="Only reconcile payment in the same journal than the invoice", + ) diff --git a/account_payment_mode_auto_reconcile/readme/CONTRIBUTORS.rst b/account_payment_mode_auto_reconcile/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..e31e2f0c4f --- /dev/null +++ b/account_payment_mode_auto_reconcile/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Akim Juillerat diff --git a/account_payment_mode_auto_reconcile/readme/DESCRIPTION.rst b/account_payment_mode_auto_reconcile/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..b692478934 --- /dev/null +++ b/account_payment_mode_auto_reconcile/readme/DESCRIPTION.rst @@ -0,0 +1,11 @@ +This module adds a checkbox `auto_reconcile_outstanding_credits` on account +payment modes to allow automatic reconciliation on account invoices if it is +checked. + +Automatic reconciliation of outstanding credits will only happen on customer +invoices at validation if the payment mode is set or when the payment mode is +changed on an open invoice. If a payment mode using auto-reconcile is removed +from an open invoice, the existing auto reconciled payments will be removed. + +Another option `auto_reconcile_allow_partial` on account payment mode defines +if outstanding credits can be partially used for the auto reconciliation. diff --git a/account_payment_mode_auto_reconcile/static/description/icon.png b/account_payment_mode_auto_reconcile/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/account_payment_mode_auto_reconcile/static/description/icon.png differ diff --git a/account_payment_mode_auto_reconcile/static/description/index.html b/account_payment_mode_auto_reconcile/static/description/index.html new file mode 100644 index 0000000000..9824a330e0 --- /dev/null +++ b/account_payment_mode_auto_reconcile/static/description/index.html @@ -0,0 +1,428 @@ + + + + + +Account Payment Mode Auto Reconcile + + + +
+

Account Payment Mode Auto Reconcile

+ + +

Beta License: AGPL-3 OCA/account-reconcile Translate me on Weblate Try me on Runboat

+

This module adds a checkbox auto_reconcile_outstanding_credits on account +payment modes to allow automatic reconciliation on account invoices if it is +checked.

+

Automatic reconciliation of outstanding credits will only happen on customer +invoices at validation if the payment mode is set or when the payment mode is +changed on an open invoice. If a payment mode using auto-reconcile is removed +from an open invoice, the existing auto reconciled payments will be removed.

+

Another option auto_reconcile_allow_partial on account payment mode defines +if outstanding credits can be partially used for the auto reconciliation.

+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+ +
+

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.

+

This module is part of the OCA/account-reconcile project on GitHub.

+

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

+
+
+
+ + diff --git a/account_payment_mode_auto_reconcile/tests/__init__.py b/account_payment_mode_auto_reconcile/tests/__init__.py new file mode 100644 index 0000000000..729e1f4dae --- /dev/null +++ b/account_payment_mode_auto_reconcile/tests/__init__.py @@ -0,0 +1 @@ +from . import test_partner_auto_reconcile diff --git a/account_payment_mode_auto_reconcile/tests/test_partner_auto_reconcile.py b/account_payment_mode_auto_reconcile/tests/test_partner_auto_reconcile.py new file mode 100644 index 0000000000..74c647d0fe --- /dev/null +++ b/account_payment_mode_auto_reconcile/tests/test_partner_auto_reconcile.py @@ -0,0 +1,299 @@ +# Copyright 2019 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from datetime import date, timedelta + +from odoo.tests import TransactionCase +from odoo.tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT + + +class TestPartnerAutoReconcile(TransactionCase): + @classmethod + def setUpClass(cls): + super(TestPartnerAutoReconcile, cls).setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.acc_rec = cls.env["account.account"].create( + { + "name": "Receivable", + "code": "RECEIVE", + "account_type": "asset_receivable", + "company_id": cls.env.ref("base.main_company").id, + } + ) + cls.acc_pay = cls.env["account.account"].create( + { + "name": "Payable", + "code": "PAYABLE", + "account_type": "liability_payable", + "company_id": cls.env.ref("base.main_company").id, + } + ) + cls.acc_rev = cls.env["account.account"].create( + { + "name": "Income", + "code": "INCOME", + "account_type": "income", + "company_id": cls.env.ref("base.main_company").id, + } + ) + cls.partner = cls.env["res.partner"].create( + { + "name": "Test partner", + "customer_rank": 1, + "property_account_receivable_id": cls.acc_rec.id, + "property_account_payable_id": cls.acc_pay.id, + } + ) + cls.payment_mode = cls.env.ref("account_payment_mode.payment_mode_inbound_dd1") + # TODO check why it's not set from demo data + cls.payment_mode.auto_reconcile_outstanding_credits = True + cls.product = cls.env.ref("product.consu_delivery_02") + cls.journal = cls.env["account.journal"].create( + { + "name": "BANK", + "code": "BANK-TEST", + "company_id": cls.env.ref("base.main_company").id, + "type": "bank", + } + ) + + cls.sale_journal = cls.env["account.journal"].create( + { + "name": "SALE-TEST", + "code": "SALE", + "company_id": cls.env.ref("base.main_company").id, + "type": "sale", + } + ) + cls.invoice = cls.env["account.move"].create( + { + "partner_id": cls.partner.id, + "move_type": "out_invoice", + "invoice_payment_term_id": cls.env.ref( + "account.account_payment_term_immediate" + ).id, + "invoice_line_ids": [ + ( + 0, + 0, + { + "product_id": cls.product.id, + "name": cls.product.name, + "price_unit": 1000.0, + "quantity": 1, + "account_id": cls.acc_rev.id, + }, + ) + ], + } + ) + + cls.invoice.action_post() + cls.refund_wiz = ( + cls.env["account.move.reversal"] + .with_context(active_ids=cls.invoice.ids) + .create( + { + "refund_method": "refund", + "move_ids": [cls.invoice.id], + "journal_id": cls.sale_journal.id, + } + ) + ) + refund_id = cls.refund_wiz.reverse_moves().get("res_id") + cls.refund = cls.env["account.move"].browse(refund_id) + cls.payment = cls.env["account.payment"].create( + { + "amount": 500.0, + "partner_id": cls.partner.id, + } + ) + cls.payment.action_post() + cls.invoice_copy = cls.invoice.copy() + cls.invoice_copy.write( + { + "invoice_line_ids": [ + ( + 0, + 0, + { + "product_id": cls.product.id, + "name": cls.product.name, + "price_unit": 500.0, + "quantity": 1, + "account_id": cls.acc_rev.id, + }, + ) + ] + } + ) + + def test_invoice_validate_auto_reconcile(self): + auto_rec_invoice = self.invoice.copy( + { + "payment_mode_id": self.payment_mode.id, + } + ) + auto_rec_invoice.action_post() + self.assertTrue(self.payment_mode.auto_reconcile_outstanding_credits) + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + self.assertEqual(auto_rec_invoice.amount_residual, 650.0) + + def test_invoice_change_auto_reconcile(self): + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + self.invoice_copy.write({"payment_mode_id": self.payment_mode.id}) + self.invoice_copy.action_post() + # Reconcile 500 from payment + self.assertEqual(self.invoice_copy.amount_residual, 1225.0) + self.invoice_copy.button_draft() + self.invoice_copy.write({"payment_mode_id": False}) + self.invoice_copy.action_post() + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + # Copy the refund so there's more outstanding credit than invoice total + new_refund = self.refund.copy() + new_refund.date = (date.today() + timedelta(days=1)).strftime(DATE_FORMAT) + new_refund.invoice_line_ids.write({"price_unit": 1200}) + new_refund.action_post() + # Set reconcile partial to False + self.payment_mode.auto_reconcile_allow_partial = False + self.assertFalse(self.payment_mode.auto_reconcile_allow_partial) + self.invoice_copy.write({"payment_mode_id": self.payment_mode.id}) + # Only the older move is used as payment + self.assertEqual(self.invoice_copy.amount_residual, 1225.0) + self.invoice_copy.write({"payment_mode_id": False}) + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + # Set allow partial will reconcile both moves + self.payment_mode.auto_reconcile_allow_partial = True + self.invoice_copy.write({"payment_mode_id": self.payment_mode.id}) + self.assertEqual(self.invoice_copy.state, "posted") + self.assertEqual(self.invoice_copy.amount_residual, 0) + + def test_invoice_auto_unreconcile(self): + # Copy the refund so there's more outstanding credit than invoice total + new_refund = self.refund.copy() + new_refund.date = (date.today() + timedelta(days=1)).strftime(DATE_FORMAT) + new_refund.invoice_line_ids.write({"price_unit": 1200}) + new_refund.action_post() + auto_rec_invoice = self.invoice.copy( + { + "payment_mode_id": self.payment_mode.id, + } + ) + auto_rec_invoice.invoice_line_ids.write({"price_unit": 800}) + auto_rec_invoice.action_post() + self.assertEqual(auto_rec_invoice.payment_state, "paid") + self.assertEqual(auto_rec_invoice.amount_residual, 0) + # As we had 1880 (500 for payment and 1200 + 15% of new_fund) of + # outstanding credits and 920 was assigned, there's 960 left + + self.assertTrue(self.payment_mode.auto_reconcile_allow_partial) + self.invoice_copy.write({"payment_mode_id": self.payment_mode.id}) + self.invoice_copy.action_post() + self.assertEqual(self.invoice_copy.amount_residual, 765.0) + # Unreconcile of an invoice doesn't change the reconciliation of the + # other invoice + self.invoice_copy.button_draft() + self.invoice_copy.write({"payment_mode_id": False}) + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + self.assertEqual(auto_rec_invoice.state, "posted") + self.assertEqual(auto_rec_invoice.amount_residual, 0) + + def test_invoice_auto_unreconcile_only_auto_reconcile(self): + refund = self.refund.copy() + refund.invoice_line_ids.write({"price_unit": 100}) + refund.action_post() + new_invoice = self.invoice_copy.copy() + new_invoice.action_post() + # Only reconcile 1000 refund manually + new_invoice_credits = new_invoice.invoice_outstanding_credits_debits_widget.get( + "content" + ) + for cred in new_invoice_credits: + if cred.get("amount") == 115.0: + new_invoice.js_assign_outstanding_line(cred.get("id")) + self.assertEqual(new_invoice.amount_residual, 1610.0) + # Assign payment mode adds the outstanding credit of 500 + new_invoice.write({"payment_mode_id": self.payment_mode.id}) + self.assertEqual(round(new_invoice.amount_residual, 2), 1110.0) + # Remove payment mode only removes automatically added credit + new_invoice.write({"payment_mode_id": False}) + self.assertEqual(new_invoice.amount_residual, 1610.0) + + # use the same payment partially on different invoices. + other_invoice = self.invoice.copy() + other_invoice.invoice_line_ids.write( + { + "price_unit": 200, + } + ) + other_invoice.write( + { + "payment_mode_id": self.payment_mode.id, + } + ) + other_invoice.action_post() + self.assertEqual(other_invoice.state, "posted") + # since 230 (200 + 15% VAT) were assigned on other invoice adding + # auto-rec payment mode on new_invoice will reconcile 270 + # and residual will be 1340.0 + new_invoice.write({"payment_mode_id": self.payment_mode.id}) + self.assertEqual(new_invoice.amount_residual, 1340.0) + # Removing the payment mode should not remove the partial payment on + # the other invoice + new_invoice.write({"payment_mode_id": False}) + self.assertEqual(new_invoice.amount_residual, 1610.0) + self.assertEqual(other_invoice.state, "posted") + + def test_invoice_auto_reconcile_same_journal(self): + """Check reconciling credits on same journal.""" + self.payment_mode.auto_reconcile_same_journal = True + auto_rec_invoice = self.invoice.copy( + { + "payment_mode_id": self.payment_mode.id, + } + ) + + payment_method_line = self.env["account.payment.method.line"].search( + [ + ("code", "=", "manual"), + ("payment_type", "=", "inbound"), + ("journal_id", "=", self.journal.id), + ] + ) + self.invoice.journal_id.inbound_payment_method_line_ids = [ + payment_method_line.id + ] + + payment_same_journal = self.env["account.payment"].create( + { + "amount": 500.0, + "partner_id": self.partner.id, + "journal_id": auto_rec_invoice.journal_id.id, + } + ) + payment_same_journal.action_post() + + self.assertTrue(self.payment_mode.auto_reconcile_outstanding_credits) + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + auto_rec_invoice.action_post() + self.assertEqual(auto_rec_invoice.amount_residual, 650) + + def test_invoice_auto_reconcile_different_journal(self): + """Check not reconciling credits on different journal.""" + self.payment_mode.auto_reconcile_same_journal = True + auto_rec_invoice = self.invoice.copy( + { + "payment_mode_id": self.payment_mode.id, + "journal_id": self.sale_journal.id, + } + ) + payment_different_journal = self.env["account.payment"].create( + { + "amount": 500.0, + "partner_id": self.partner.id, + } + ) + payment_different_journal.action_post() + auto_rec_invoice.action_post() + self.assertTrue(self.payment_mode.auto_reconcile_outstanding_credits) + self.assertEqual(self.invoice_copy.amount_residual, 1725.0) + self.assertEqual(auto_rec_invoice.amount_residual, 1150.0) diff --git a/account_payment_mode_auto_reconcile/views/account_invoice.xml b/account_payment_mode_auto_reconcile/views/account_invoice.xml new file mode 100644 index 0000000000..81f63bb2b5 --- /dev/null +++ b/account_payment_mode_auto_reconcile/views/account_invoice.xml @@ -0,0 +1,25 @@ + + + + account_payment_partner_view_move_form_inherit + account.move + + + + + + + + + + diff --git a/account_payment_mode_auto_reconcile/views/account_payment_mode.xml b/account_payment_mode_auto_reconcile/views/account_payment_mode.xml new file mode 100644 index 0000000000..e75ac499c3 --- /dev/null +++ b/account_payment_mode_auto_reconcile/views/account_payment_mode.xml @@ -0,0 +1,26 @@ + + + + account.payment.mode.form.inherit + account.payment.mode + + + + + + + + + + + + diff --git a/setup/account_payment_mode_auto_reconcile/odoo/addons/account_payment_mode_auto_reconcile b/setup/account_payment_mode_auto_reconcile/odoo/addons/account_payment_mode_auto_reconcile new file mode 120000 index 0000000000..076fc25e6b --- /dev/null +++ b/setup/account_payment_mode_auto_reconcile/odoo/addons/account_payment_mode_auto_reconcile @@ -0,0 +1 @@ +../../../../account_payment_mode_auto_reconcile \ No newline at end of file diff --git a/setup/account_payment_mode_auto_reconcile/setup.py b/setup/account_payment_mode_auto_reconcile/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/account_payment_mode_auto_reconcile/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)