-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.py
99 lines (80 loc) · 3.55 KB
/
account.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from sql import Column
from sql.aggregate import Max
from trytond.pool import Pool, PoolMeta
from trytond.model import fields
from trytond.tools import grouped_slice, reduce_ids
from trytond.transaction import Transaction
class Move(metaclass=PoolMeta):
__name__ = 'account.move'
@classmethod
def _get_origin(cls):
return super()._get_origin() + ['account.statement.origin']
class MoveLine(metaclass=PoolMeta):
__name__ = 'account.move.line'
payment_group = fields.Function(fields.Many2One('account.payment.group',
'Payment Group'),
'get_payment_fields', searcher='search_payment_group')
payment_date = fields.Function(fields.Date('Payment Date'),
'get_payment_fields', searcher='search_payment_date')
@classmethod
def get_payment_fields(cls, lines, name):
pool = Pool()
Payment = pool.get('account.payment')
table = Payment.__table__()
cursor = Transaction().connection.cursor()
line_ids = [l.id for l in lines]
result = {}.fromkeys(line_ids, None)
for sub_ids in grouped_slice(line_ids):
query = table.select(table.line, Max(Column(table, name[8:])),
where=((table.state != 'failed')
& reduce_ids(table.line, sub_ids)), group_by=table.line)
cursor.execute(*query)
result.update(dict(cursor.fetchall()))
return result
@classmethod
def search_payment_group(cls, name, clause):
return [('payments.group.rec_name',) + tuple(clause[1:])]
@classmethod
def search_payment_date(cls, name, clause):
return [('payments.date',) + tuple(clause[1:])]
@classmethod
def _get_origin(cls):
return super()._get_origin() + ['account.statement.origin']
@classmethod
def check_modify(cls, *args, **kwargs):
# It's needed to modify the lines even if the move is in 'posted'
# state.
if Transaction().context.get('from_account_statement_origin',
False):
return
return super().check_modify(*args, **kwargs)
@classmethod
def reconcile(cls, *lines_list, date=None, writeoff=None, description=None,
delegate_to=None):
pool = Pool()
StatementLine = pool.get('account.statement.line')
StatementSuggest = pool.get('account.statement.origin.suggested.line')
# If are reocniling move lines that ara in some statement line related
# or some suggested lines related. Remove the statement or suggested.
lines = [line for lines in lines_list for line in lines]
domain = [
('related_to', 'in', lines),
('origin.state', '!=', 'posted'),
]
statement_lines = Transaction().context.get(
'account_statement_lines', [])
if statement_lines:
domain.append(('id', 'not in', statement_lines))
statement_lines_to_remove = StatementLine.search(domain)
if statement_lines_to_remove:
StatementLine.delete(statement_lines_to_remove)
suggest_to_remove = StatementSuggest.search([
('related_to', 'in', lines),
('origin.state', '!=', 'posted'),
])
if suggest_to_remove:
StatementSuggest.delete(suggest_to_remove)
return super().reconcile(*lines_list, date=date, writeoff=writeoff,
description=description, delegate_to=delegate_to)