mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
[12.0][ADD] account_document_reversal
This commit is contained in:
committed by
Jordi Ballester Alomar
parent
3a53ba8f7d
commit
0e44a9de54
7
account_document_reversal/models/__init__.py
Normal file
7
account_document_reversal/models/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
# Copyright 2019 Ecosoft Co., Ltd (http://ecosoft.co.th/)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
|
||||
from . import account
|
||||
from . import account_document_reversal
|
||||
from . import account_invoice
|
||||
from . import account_payment
|
||||
from . import account_bank_statement
|
||||
24
account_document_reversal/models/account.py
Normal file
24
account_document_reversal/models/account.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# 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 models, fields, api
|
||||
|
||||
|
||||
class AccountJournal(models.Model):
|
||||
_inherit = 'account.journal'
|
||||
|
||||
cancel_method = fields.Selection(
|
||||
[('normal', 'Normal (delete journal entries if exists)'),
|
||||
('reversal', 'Reversal (create reversed journal entries)')],
|
||||
string='Cancel Method',
|
||||
default='normal',
|
||||
required=True)
|
||||
is_cancel_reversal = fields.Boolean(
|
||||
string='Use Cancel Reversal',
|
||||
compute='_compute_is_cancel_reversal',
|
||||
help="True, when journal allow cancel entries with method is reversal")
|
||||
|
||||
@api.multi
|
||||
def _compute_is_cancel_reversal(self):
|
||||
for rec in self:
|
||||
rec.is_cancel_reversal = \
|
||||
rec.update_posted and rec.cancel_method == 'reversal'
|
||||
65
account_document_reversal/models/account_bank_statement.py
Normal file
65
account_document_reversal/models/account_bank_statement.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# 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 UserError
|
||||
|
||||
|
||||
class AccountPayment(models.Model):
|
||||
_name = 'account.bank.statement.line'
|
||||
_inherit = ['account.bank.statement.line', 'account.document.reversal']
|
||||
|
||||
@api.multi
|
||||
def button_cancel_reconciliation(self):
|
||||
""" If cancel method is to reverse, use document reversal wizard """
|
||||
cancel_reversal = all(self.mapped('journal_entry_ids.move_id.'
|
||||
'journal_id.is_cancel_reversal'))
|
||||
states = self.mapped('statement_id.state')
|
||||
if cancel_reversal:
|
||||
if not all(st == 'open' for st in states):
|
||||
raise UserError(
|
||||
_('Only new bank statement can be cancelled'))
|
||||
return self.reverse_document_wizard()
|
||||
return super().button_cancel_reconciliation()
|
||||
|
||||
@api.multi
|
||||
def action_document_reversal(self, date=None, journal_id=None):
|
||||
""" Reverse all moves related to this statement + delete payment """
|
||||
# This part is from button_cancel_reconciliation()
|
||||
aml_to_unbind = self.env['account.move.line']
|
||||
aml_to_cancel = self.env['account.move.line']
|
||||
payment_to_unreconcile = self.env['account.payment']
|
||||
payment_to_cancel = self.env['account.payment']
|
||||
for st_line in self:
|
||||
aml_to_unbind |= st_line.journal_entry_ids
|
||||
for line in st_line.journal_entry_ids:
|
||||
payment_to_unreconcile |= line.payment_id
|
||||
if st_line.move_name and \
|
||||
line.payment_id.payment_reference == st_line.move_name:
|
||||
# there can be several moves linked to a statement line but
|
||||
# maximum one created by the line itself
|
||||
aml_to_cancel |= line
|
||||
payment_to_cancel |= line.payment_id
|
||||
aml_to_unbind = aml_to_unbind - aml_to_cancel
|
||||
if aml_to_unbind:
|
||||
aml_to_unbind.write({'statement_line_id': False})
|
||||
payment_to_unreconcile = payment_to_unreconcile - payment_to_cancel
|
||||
if payment_to_unreconcile:
|
||||
payment_to_unreconcile.unreconcile()
|
||||
# --
|
||||
|
||||
# Set all moves to unreconciled
|
||||
aml_to_cancel.filtered(lambda x:
|
||||
x.account_id.reconcile).remove_move_reconcile()
|
||||
moves = aml_to_cancel.mapped('move_id')
|
||||
# Important to remove relation with move.line before reverse
|
||||
aml_to_cancel.write({'payment_id': False,
|
||||
'statement_id': False,
|
||||
'statement_line_id': False})
|
||||
# Create reverse entries
|
||||
moves.reverse_moves(date, journal_id)
|
||||
# Delete related payments
|
||||
if payment_to_cancel:
|
||||
payment_to_cancel.unlink()
|
||||
# Unlink from statement line
|
||||
self.write({'move_name': False})
|
||||
return True
|
||||
@@ -0,0 +1,26 @@
|
||||
# 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 models, api
|
||||
|
||||
|
||||
class AccountDocumentReversal(models.AbstractModel):
|
||||
_name = 'account.document.reversal'
|
||||
_description = 'Abstract Module for Document Reversal'
|
||||
|
||||
@api.model
|
||||
def reverse_document_wizard(self):
|
||||
""" Return Wizard to Cancel Document """
|
||||
action = self.env.ref('account_document_reversal.'
|
||||
'action_view_reverse_account_document')
|
||||
vals = action.read()[0]
|
||||
return vals
|
||||
|
||||
@api.multi
|
||||
def action_document_reversal(self, date=None, journal_id=None):
|
||||
""" Reverse with following guildeline,
|
||||
- Check existing document state / raise warning
|
||||
- Find all related moves and unreconcile
|
||||
- Create reversed moves
|
||||
- Set state to cancel
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
52
account_document_reversal/models/account_invoice.py
Normal file
52
account_document_reversal/models/account_invoice.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# 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, UserError
|
||||
|
||||
|
||||
class AccountInvoice(models.Model):
|
||||
_name = 'account.invoice'
|
||||
_inherit = ['account.invoice', 'account.document.reversal']
|
||||
|
||||
@api.multi
|
||||
def action_invoice_cancel(self):
|
||||
""" If cancel method is to reverse, use document reversal wizard
|
||||
* Draft invoice, fall back to standard invoice cancel
|
||||
* Non draft, must be fully open (not even partial reconciled) to cancel
|
||||
"""
|
||||
cancel_reversal = all(self.mapped('journal_id.is_cancel_reversal'))
|
||||
states = self.mapped('state')
|
||||
if cancel_reversal and 'draft' not in states:
|
||||
if not all(st == 'open' for st in states) or \
|
||||
(self.mapped('move_id.line_ids.matched_debit_ids') |
|
||||
self.mapped('move_id.line_ids.matched_credit_ids')):
|
||||
raise UserError(
|
||||
_('Only fully unpaid invoice can be cancelled.\n'
|
||||
'To cancel this invoice, make sure all payment(s) '
|
||||
'are also cancelled.'))
|
||||
return self.reverse_document_wizard()
|
||||
return super().action_invoice_cancel()
|
||||
|
||||
@api.multi
|
||||
def action_document_reversal(self, date=None, journal_id=None):
|
||||
""" Reverse all moves related to this invoice + set state to cancel """
|
||||
# Check document state
|
||||
if 'cancel' in self.mapped('state'):
|
||||
raise ValidationError(
|
||||
_('You are trying to cancel the cancelled document'))
|
||||
MoveLine = self.env['account.move.line']
|
||||
move_lines = MoveLine.search([('invoice_id', 'in', self.ids)])
|
||||
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({'invoice_id': False})
|
||||
# Create reverse entries
|
||||
moves.reverse_moves(date, journal_id)
|
||||
# Set state cancelled and unlink with account.move
|
||||
self.write({'move_id': False,
|
||||
'move_name': False,
|
||||
'reference': False,
|
||||
'state': 'cancel'})
|
||||
return True
|
||||
40
account_document_reversal/models/account_payment.py
Normal file
40
account_document_reversal/models/account_payment.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# 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
|
||||
|
||||
|
||||
class AccountPayment(models.Model):
|
||||
_name = 'account.payment'
|
||||
_inherit = ['account.payment', 'account.document.reversal']
|
||||
|
||||
@api.multi
|
||||
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()
|
||||
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')
|
||||
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)
|
||||
# Set state cancelled and unlink with account.move
|
||||
self.write({'move_name': False,
|
||||
'state': 'cancelled'})
|
||||
return True
|
||||
Reference in New Issue
Block a user