[13.0][MIG] account_document_reversal

This commit is contained in:
Kitti U
2022-01-11 17:53:07 +07:00
parent cf19a91ade
commit 7656e7fad6
23 changed files with 443 additions and 623 deletions

View File

@@ -1,38 +1,44 @@
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
from odoo import _, api, models
from odoo.exceptions import ValidationError
from odoo import _, models
from odoo.exceptions import UserError
class AccountPayment(models.Model):
_name = "account.payment"
_inherit = ["account.payment", "account.document.reversal"]
@api.multi
def cancel_reversal(self):
return self.reverse_document_wizard()
def action_draft(self):
""" Case cancel reversal, set to draft allowed only when no moves """
for rec in self:
if rec.is_cancel_reversal and rec.move_line_ids:
raise UserError(_("Cannot set to draft!"))
return super().action_draft()
def cancel(self):
""" If cancel method is to reverse, use document reversal wizard """
cancel_reversal = all(
self.mapped("move_line_ids.move_id.journal_id.is_cancel_reversal")
)
states = self.mapped("state")
if cancel_reversal and "draft" not in states:
return self.reverse_document_wizard()
if any(self.mapped("is_cancel_reversal")):
raise UserError(_("Please use cancel_reversal()"))
return super().cancel()
@api.multi
def action_document_reversal(self, date=None, journal_id=None):
""" Reverse all moves related to this payment + set state to cancel """
# Check document state
if "cancelled" in self.mapped("state"):
raise ValidationError(_("You are trying to cancel the cancelled document"))
move_lines = self.mapped("move_line_ids")
# Check document readiness
valid_state = (
len(self.mapped("state")) == 1
and list(set(self.mapped("state")))[0] == "posted"
)
if not valid_state:
raise UserError(_("Only validated document can be cancelled (reversal)"))
# Find moves to get reversed
move_lines = self.mapped("move_line_ids").filtered(
lambda x: x.journal_id == self.mapped("journal_id")[0]
)
moves = move_lines.mapped("move_id")
# Set all moves to unreconciled
move_lines.filtered(lambda x: x.account_id.reconcile).remove_move_reconcile()
# Important to remove relation with move.line before reverse
move_lines.write({"payment_id": False})
# Create reverse entries
moves.reverse_moves(date, journal_id)
moves._cancel_reversal(journal_id)
# Set state cancelled and unlink with account.move
self.write({"move_name": False, "state": "cancelled"})
self.write({"state": "cancelled"})
return True