-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatement.py
227 lines (195 loc) · 8.42 KB
/
statement.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# This file is part of the account_check_ar module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.model import fields, Workflow
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, If, Bool
class AccountIssuedCheck(metaclass=PoolMeta):
__name__ = 'account.issued.check'
related_statement_line = fields.Many2One('account.statement.line',
'Statement Line', readonly=True)
class AccountThirdCheck(metaclass=PoolMeta):
__name__ = 'account.third.check'
related_statement_line = fields.Many2One('account.statement.line',
'Statement Line', readonly=True)
class Statement(metaclass=PoolMeta):
__name__ = 'account.statement'
@classmethod
@Workflow.transition('validated')
def validate_statement(cls, statements):
pool = Pool()
ThirdCheck = pool.get('account.third.check')
StatementLine = Pool().get('account.statement.line')
super(Statement, cls).validate_statement(statements)
# Remove created draft moves when line is related to third checks
lines = [l for s in statements for l in s.lines
if isinstance(l.related_to, ThirdCheck)]
StatementLine.delete_move(lines)
@classmethod
@Workflow.transition('posted')
def post(cls, statements):
pool = Pool()
IssuedCheck = pool.get('account.issued.check')
super(Statement, cls).post(statements)
# Change issued checks state
lines = [l for s in statements for l in s.lines
if isinstance(l.related_to, IssuedCheck)]
checks = []
for l in lines:
check = IssuedCheck(l.related_to.id)
check.state = 'debited'
check.debit_date = l.date
checks.append(check)
IssuedCheck.save(checks)
class StatementLine(metaclass=PoolMeta):
__name__ = 'account.statement.line'
statement_journal_bank_account = fields.Function(
fields.Many2One('bank.account', 'Bank Account',),
'on_change_with_statement_journal_bank_account')
@classmethod
def __setup__(cls):
super().__setup__()
cls.related_to.domain['account.issued.check'] = ['OR',
[('related_statement_line', '=', Eval('id', -1))],
[('related_statement_line', '=', None),
('voucher', '!=', None),
('voucher.company', '=', Eval('company', -1)),
If(Bool(Eval('voucher.party')),
('voucher.party', '=', Eval('party')),
()),
('checkbook.bank_account', '=',
Eval('statement_journal_bank_account', -1)),
('voucher.state', 'in', ['posted']),
('state', 'in', ['issued']),
('voucher.currency', '=', Eval('currency', -1)),
('voucher.voucher_type', '=',
If(Eval('amount', 0) > 0, 'receipt',
If(Eval('amount', 0) < 0, 'payment', ''))),
('amount', '=', Eval('abs_amount', 0)),
]]
cls.related_to.domain['account.third.check'] = ['OR',
[('related_statement_line', '=', Eval('id', -1))],
[('related_statement_line', '=', None),
('state', 'in', ['deposited']),
('amount', '=', Eval('abs_amount', 0)),
]]
cls.related_to.search_order['account.issued.check'] = [
('amount', 'ASC'),
]
cls.related_to.search_order['account.third.check'] = [
('amount', 'ASC'),
]
@fields.depends('statement')
def on_change_with_statement_journal_bank_account(self, name=None):
if self.statement and self.statement.journal \
and self.statement.journal.bank_account:
return self.statement.journal.bank_account.id
return None
@classmethod
def _get_relations(cls):
return super()._get_relations() + [
'account.issued.check', 'account.third.check']
@property
@fields.depends('related_to')
def issued_check(self):
pool = Pool()
IssuedCheck = pool.get('account.issued.check')
related_to = getattr(self, 'related_to', None)
if isinstance(related_to, IssuedCheck) and related_to.id >= 0:
return related_to
@issued_check.setter
def issued_check(self, value):
self.related_to = value
@property
@fields.depends('related_to')
def third_check(self):
pool = Pool()
ThirdCheck = pool.get('account.third.check')
related_to = getattr(self, 'related_to', None)
if isinstance(related_to, ThirdCheck) and related_to.id >= 0:
return related_to
@third_check.setter
def third_check(self, value):
self.related_to = value
@fields.depends('party', 'statement',
methods=['issued_check', 'third_check'])
def on_change_related_to(self):
super().on_change_related_to()
if self.issued_check:
if not self.party:
self.party = self.issued_check.voucher.party
if self.issued_check.voucher.journal.issued_check_account:
self.account = \
self.issued_check.voucher.journal.issued_check_account
if self.third_check:
if not self.party and self.third_check.source_party:
self.party = self.third_check.source_party
if self.third_check.account_bank_out:
self.account = self.third_check.account_bank_out.credit_account
@classmethod
def create(cls, vlist):
pool = Pool()
IssuedCheck = pool.get('account.issued.check')
ThirdCheck = pool.get('account.third.check')
lines = super(StatementLine, cls).create(vlist)
update_issued = {}
update_third = {}
for l in lines:
if l.related_to:
if str(l.related_to).split(',')[0] == IssuedCheck.__name__:
check_id = int(str(l.related_to).split(',')[1])
update_issued[check_id] = l.id
elif str(l.related_to).split(',')[0] == ThirdCheck.__name__:
check_id = int(str(l.related_to).split(',')[1])
update_third[check_id] = l.id
if update_issued:
cls.update_issued_checks(update_issued)
if update_third:
cls.update_third_checks(update_third)
return lines
@classmethod
def write(cls, *args):
pool = Pool()
IssuedCheck = pool.get('account.issued.check')
ThirdCheck = pool.get('account.third.check')
actions = iter(args)
update_issued = {}
update_third = {}
for lines, values in zip(actions, actions):
if 'related_to' in values:
if values['related_to'] is None:
if isinstance(lines[0].related_to, IssuedCheck):
update_issued[lines[0].related_to.id] = None
elif isinstance(lines[0].related_to, ThirdCheck):
update_third[lines[0].related_to.id] = None
elif values['related_to'].split(',')[0] \
== IssuedCheck.__name__:
check_id = int(values['related_to'].split(',')[1])
update_issued[check_id] = lines[0].id
elif values['related_to'].split(',')[0] \
== ThirdCheck.__name__:
check_id = int(values['related_to'].split(',')[1])
update_third[check_id] = lines[0].id
super(StatementLine, cls).write(*args)
if update_issued:
cls.update_issued_checks(update_issued)
if update_third:
cls.update_third_checks(update_third)
def update_issued_checks(update_issued):
pool = Pool()
IssuedCheck = pool.get('account.issued.check')
issued_checks = []
for key in update_issued:
check = IssuedCheck(key)
check.related_statement_line = update_issued[key]
issued_checks.append(check)
IssuedCheck.save(issued_checks)
def update_third_checks(update_third):
pool = Pool()
ThirdCheck = pool.get('account.third.check')
third_checks = []
for key in update_third:
check = ThirdCheck(key)
check.related_statement_line = update_third[key]
third_checks.append(check)
ThirdCheck.save(third_checks)