Skip to content

Commit

Permalink
[16.0][MIG]account_payment_mode_auto_reconcile: Migrate to version 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhd91 committed Jun 10, 2023
1 parent ef40ad7 commit f440e10
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 197 deletions.
14 changes: 7 additions & 7 deletions account_payment_mode_auto_reconcile/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Account Payment Mode Auto Reconcile
: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/10.0/account_payment_mode_auto_reconcile
: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-10-0/account-reconcile-10-0-account_payment_mode_auto_reconcile
: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/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/98/10.0
:alt: Try me on Runbot
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/account-reconcile&target_branch=16.0
:alt: Try me on Runboat

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

Expand All @@ -48,7 +48,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-reconcile/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/account-reconcile/issues/new?body=module:%20account_payment_mode_auto_reconcile%0Aversion:%2010.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/account-reconcile/issues/new?body=module:%20account_payment_mode_auto_reconcile%0Aversion:%2016.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.

Expand Down Expand Up @@ -78,6 +78,6 @@ 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 <https://github.com/OCA/account-reconcile/tree/10.0/account_payment_mode_auto_reconcile>`_ project on GitHub.
This module is part of the `OCA/account-reconcile <https://github.com/OCA/account-reconcile/tree/16.0/account_payment_mode_auto_reconcile>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion account_payment_mode_auto_reconcile/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Account Payment Mode Auto Reconcile",
"summary": "Reconcile outstanding credits according to payment mode",
"version": "10.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Banking addons",
"website": "https://github.com/OCA/account-reconcile",
"author": "Camptocamp, Odoo Community Association (OCA)",
Expand Down
2 changes: 1 addition & 1 deletion account_payment_mode_auto_reconcile/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import account_invoice
from . import account_move
from . import account_partial_reconcile
from . import account_payment_mode
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
import json
from operator import itemgetter

from odoo import _, api, fields, models


class AccountInvoice(models.Model):
class AccountMove(models.Model):

_inherit = "account.invoice"
_inherit = "account.move"

# Allow changing payment mode in open state
# TODO: Check if must be done in account_payment_partner instead
payment_mode_id = fields.Many2one(
states={"draft": [("readonly", False)], "open": [("readonly", False)]}
states={"draft": [("readonly", False)], "posted": [("readonly", False)]}
)
payment_mode_warning = fields.Char(
compute="_compute_payment_mode_warning",
Expand All @@ -22,11 +21,10 @@ class AccountInvoice(models.Model):
compute="_compute_payment_mode_warning",
)

@api.multi
def invoice_validate(self):
res = super(AccountInvoice, self).invoice_validate()
def action_post(self):
res = super(AccountMove, self).action_post()
for invoice in self:
if invoice.type != "out_invoice":
if invoice.move_type != "out_invoice":
continue
if not invoice.payment_mode_id.auto_reconcile_outstanding_credits:
continue
Expand All @@ -36,50 +34,60 @@ def invoice_validate(self):
).auto_reconcile_credits(partial_allowed=partial)
return res

@api.multi
def write(self, vals):
res = super(AccountInvoice, self).write(vals)
if "payment_mode_id" in 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 != "open" or invoice.type != "out_invoice":
if invoice.state != "posted" or invoice.move_type != "out_invoice":
continue
payment_mode = invoice.payment_mode_id
invoice_lines = invoice.line_ids.filtered(
lambda line: line.account_type == "asset_receivable"
)
# 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 invoice.payment_move_line_ids:
elif any(
[
invoice_lines.mapped("matched_credit_ids"),
invoice_lines.mapped("matched_debit_ids"),
]
):
invoice.auto_unreconcile_credits()
return res

@api.multi
def auto_reconcile_credits(self, partial_allowed=True):
for invoice in self:
if not invoice.has_outstanding:
invoice._compute_payments_widget_to_reconcile_info()

if not invoice.invoice_has_outstanding:
continue
credits_info = json.loads(invoice.outstanding_credits_debits_widget)
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")
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.residual:
if (
not partial_allowed
and credit.get("amount") > invoice.amount_residual
):
continue
invoice.assign_outstanding_credit(credit.get("id"))
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"))

@api.multi
def _filter_payment_same_journal(self, credits_dict):
"""Keep only credits on the same journal than the invoice."""
self.ensure_one()
Expand All @@ -89,34 +97,47 @@ def _filter_payment_same_journal(self, credits_dict):
)
return [credit for credit in credits_dict if credit["id"] in lines.ids]

@api.multi
def auto_unreconcile_credits(self):
for invoice in self:
payments_info = json.loads(invoice.payments_widget or "{}")
payments_info = invoice.invoice_payments_widget
for payment in payments_info.get("content", []):
aml = self.env["account.move.line"].browse(payment.get("payment_id"))
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.invoice_id == invoice
and apr.debit_move_id.move_id == invoice
):
aml.with_context(invoice_id=invoice.id).remove_move_reconcile()
aml.remove_move_reconcile()

@api.depends(
"type", "payment_mode_id", "payment_move_line_ids", "state", "has_outstanding"
"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:
if invoice.type != "out_invoice" or invoice.state == "paid":
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 != "open"
invoice.state != "posted"
and invoice.payment_mode_id
and invoice.payment_mode_id.auto_reconcile_outstanding_credits
):
Expand All @@ -125,8 +146,9 @@ def _compute_payment_mode_warning(self):
" any outstanding credits."
)
elif (
invoice.state == "open"
and invoice.payment_move_line_ids
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
Expand All @@ -137,11 +159,12 @@ def _compute_payment_mode_warning(self):
"reconciled payments."
)
elif (
invoice.state == "open"
and not invoice.payment_move_line_ids
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.has_outstanding
and invoice.invoice_has_outstanding
):
invoice.payment_mode_warning = _(
"Changing payment mode will reconcile outstanding credits."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import api, fields, models
from odoo import fields, models


class AccountPartialReconcile(models.Model):
Expand All @@ -9,8 +9,8 @@ class AccountPartialReconcile(models.Model):

payment_mode_auto_reconcile = fields.Boolean()

@api.model
def create(self, vals):
if self.env.context.get("_payment_mode_auto_reconcile"):
vals["payment_mode_auto_reconcile"] = True
for val in vals:
val["payment_mode_auto_reconcile"] = True
return super(AccountPartialReconcile, self).create(vals)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Account Payment Mode Auto Reconcile</title>
<style type="text/css">

Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Account Payment Mode Auto Reconcile</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/account-reconcile/tree/10.0/account_payment_mode_auto_reconcile"><img alt="OCA/account-reconcile" src="https://img.shields.io/badge/github-OCA%2Faccount--reconcile-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/account-reconcile-10-0/account-reconcile-10-0-account_payment_mode_auto_reconcile"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/98/10.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/account-reconcile/tree/16.0/account_payment_mode_auto_reconcile"><img alt="OCA/account-reconcile" src="https://img.shields.io/badge/github-OCA%2Faccount--reconcile-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/account-reconcile-16-0/account-reconcile-16-0-account_payment_mode_auto_reconcile"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/webui/builds.html?repo=OCA/account-reconcile&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds a checkbox <cite>auto_reconcile_outstanding_credits</cite> on account
payment modes to allow automatic reconciliation on account invoices if it is
checked.</p>
Expand All @@ -394,7 +394,7 @@ <h1><a class="toc-backref" href="#id1">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/account-reconcile/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/account-reconcile/issues/new?body=module:%20account_payment_mode_auto_reconcile%0Aversion:%2010.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/account-reconcile/issues/new?body=module:%20account_payment_mode_auto_reconcile%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -418,7 +418,7 @@ <h2><a class="toc-backref" href="#id5">Maintainers</a></h2>
<p>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.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/account-reconcile/tree/10.0/account_payment_mode_auto_reconcile">OCA/account-reconcile</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/account-reconcile/tree/16.0/account_payment_mode_auto_reconcile">OCA/account-reconcile</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
Loading

0 comments on commit f440e10

Please sign in to comment.