Skip to content
Snippets Groups Projects
Commit 9df4acde authored by Cédric Krier's avatar Cédric Krier :atom:
Browse files

Reconcile payment clearing line with all statement lines

issue11173
review385851002
parent b79478a7
1 merge request!114Return an empty list when the treeview has no selection
* Reconcile payment clearing line with all statement lines
* Use reference field for statement line relation
* Add support for Python 3.10
* Remove support for Python 3.6
......
......@@ -9,8 +9,9 @@
Pool.register(
payment.Journal,
payment.Payment,
payment.Group,
payment.SucceedStart,
account.Move,
ir.Cron,
module='account_payment_clearing', type_='model')
Pool.register(
......@@ -12,8 +13,10 @@
payment.SucceedStart,
account.Move,
ir.Cron,
module='account_payment_clearing', type_='model')
Pool.register(
statement.Payment,
statement.PaymentGroup,
statement.Statement,
statement.StatementLine,
module='account_payment_clearing', type_='model',
......
......@@ -192,6 +192,7 @@
if not sum(l.debit - l.credit for l in lines):
to_reconcile.append(lines)
Line.reconcile(*to_reconcile)
cls.reconcile_clearing(payments)
@property
def clearing_account(self):
......@@ -278,6 +279,34 @@
super(Payment, cls).fail(payments)
@classmethod
def reconcile_clearing(cls, payments):
pool = Pool()
MoveLine = pool.get('account.move.line')
Group = pool.get('account.payment.group')
to_reconcile = []
for payment in payments:
if not payment.clearing_move:
continue
clearing_account = payment.journal.clearing_account
if not clearing_account or not clearing_account.reconcile:
continue
lines = [l for l in payment.clearing_lines if not l.reconciliation]
if lines and not sum((l.debit - l.credit) for l in lines):
to_reconcile.append(lines)
if to_reconcile:
MoveLine.reconcile(*to_reconcile)
Group.reconcile_clearing(
Group.browse(list({p.group for p in payments if p.group})))
@property
def clearing_lines(self):
clearing_account = self.journal.clearing_account
if self.clearing_move:
for line in self.clearing_move.lines:
if line.account == clearing_account:
yield line
@classmethod
def copy(cls, payments, default=None):
if default is None:
default = {}
......@@ -287,6 +316,30 @@
return super(Payment, cls).copy(payments, default=default)
class Group(metaclass=PoolMeta):
__name__ = 'account.payment.group'
@classmethod
def reconcile_clearing(cls, groups):
pool = Pool()
MoveLine = pool.get('account.move.line')
to_reconcile = []
for group in groups:
clearing_account = group.journal.clearing_account
if not clearing_account or not clearing_account.reconcile:
continue
lines = [l for l in group.clearing_lines if not l.reconciliation]
if lines and not sum((l.debit - l.credit) for l in lines):
to_reconcile.append(lines)
if to_reconcile:
MoveLine.reconcile(*to_reconcile)
@property
def clearing_lines(self):
for payment in self.payments:
yield from payment.clearing_lines
class Succeed(Wizard):
"Succeed Payment"
__name__ = 'account.payment.succeed'
......
......@@ -8,9 +8,45 @@
from trytond.transaction import Transaction
class Payment(metaclass=PoolMeta):
__name__ = 'account.payment'
statement_lines = fields.One2Many(
'account.statement.line', 'related_to', "Statement Lines",
readonly=True)
@property
def clearing_lines(self):
clearing_account = self.journal.clearing_account
yield from super().clearing_lines
for statement_line in self.statement_lines:
if statement_line.move:
for line in statement_line.move.lines:
if line.account == clearing_account:
yield line
class PaymentGroup(metaclass=PoolMeta):
__name__ = 'account.payment.group'
statement_lines = fields.One2Many(
'account.statement.line', 'related_to', "Statement Lines",
readonly=True)
@property
def clearing_lines(self):
clearing_account = self.journal.clearing_account
yield from super().clearing_lines
for statement_line in self.statement_lines:
if statement_line.move:
for line in statement_line.move.lines:
if line.account == clearing_account:
yield line
class Statement(metaclass=PoolMeta):
__name__ = 'account.statement'
@classmethod
def create_move(cls, statements):
pool = Pool()
......@@ -11,10 +47,9 @@
class Statement(metaclass=PoolMeta):
__name__ = 'account.statement'
@classmethod
def create_move(cls, statements):
pool = Pool()
MoveLine = pool.get('account.move.line')
Payment = pool.get('account.payment')
moves = super(Statement, cls).create_move(statements)
......@@ -46,19 +81,8 @@
with Transaction().set_context(clearing_date=date):
Payment.fail(Payment.browse(payments))
for move, statement, lines in moves:
assert len({l.payment for l in lines}) == 1
line = lines[0]
if line.payment and line.payment.clearing_move:
clearing_account = line.payment.journal.clearing_account
if clearing_account.reconcile:
to_reconcile = []
for line in move.lines + line.payment.clearing_move.lines:
if (line.account == clearing_account
and not line.reconciliation):
to_reconcile.append(line)
if not sum((l.debit - l.credit) for l in to_reconcile):
MoveLine.reconcile(to_reconcile)
Payment.__queue__.reconcile_clearing(
list(set.union(*to_success.values(), *to_fail.values())))
return moves
def _group_key(self, line):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment