-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount_voucher_ar.py
210 lines (192 loc) · 8.36 KB
/
account_voucher_ar.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
# 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 decimal import Decimal
from trytond.model import ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, Not, In, Or
from trytond.exceptions import UserError
from trytond.i18n import gettext
_ZERO = Decimal('0.0')
class AccountVoucher(metaclass=PoolMeta):
__name__ = 'account.voucher'
issued_check = fields.One2Many('account.issued.check', 'voucher',
'Issued Checks',
add_remove=[('state', '=', 'draft')],
states={
'invisible': Not(In(Eval('voucher_type'), ['payment'])),
'readonly': Or(
In(Eval('state'), ['posted', 'cancelled']),
Not(In(Eval('currency_code'), ['ARS']))),
})
third_pay_checks = fields.Many2Many('account.voucher-account.third.check',
'voucher', 'third_check', 'Third Checks',
states={
'invisible': Not(In(Eval('voucher_type'), ['payment'])),
'readonly': Or(
In(Eval('state'), ['posted', 'cancelled']),
Not(In(Eval('currency_code'), ['ARS']))),
},
domain=[
('state', 'in', ['held', 'reverted']),
('not_to_order', '=', False),
])
third_check = fields.One2Many('account.third.check', 'voucher_in',
'Third Checks',
add_remove=[('state', '=', 'draft')],
states={
'invisible': Not(In(Eval('voucher_type'), ['receipt'])),
'readonly': Or(
In(Eval('state'), ['posted', 'cancelled']),
Not(In(Eval('currency_code'), ['ARS']))),
})
@classmethod
def __setup__(cls):
super().__setup__()
cls.third_pay_checks.search_order = [
('date', 'ASC'),
]
@fields.depends('third_check', 'issued_check', 'third_pay_checks')
def on_change_with_amount(self, name=None):
amount = super().on_change_with_amount(name)
if self.third_check:
for t_check in self.third_check:
amount += t_check.amount
if self.issued_check:
for i_check in self.issued_check:
amount += i_check.amount
if self.third_pay_checks:
for check in self.third_pay_checks:
amount += check.amount
return amount
def prepare_move_lines(self):
pool = Pool()
Period = pool.get('account.period')
move_lines = super().prepare_move_lines()
journal = self.journal
if self.voucher_type == 'receipt':
if self.third_check:
if not journal.third_check_account:
raise UserError(gettext(
'account_voucher_ar.msg_no_journal_check_account',
journal=journal.name))
for check in self.third_check:
if check.state != 'draft':
raise UserError(gettext(
'account_voucher_ar.msg_check_not_in_draft',
check=check.name))
move_lines.append({
'debit': check.amount,
'credit': _ZERO,
'account': journal.third_check_account.id,
'move': self.move.id,
'journal': journal.id,
'period': Period.find(self.company.id, date=self.date),
'party': (
journal.third_check_account.party_required and
self.party.id or None),
'maturity_date': check.date,
})
if self.voucher_type == 'payment':
if self.issued_check:
if not journal.issued_check_account:
raise UserError(gettext(
'account_voucher_ar.msg_no_journal_check_account',
journal=journal.name))
for check in self.issued_check:
move_lines.append({
'debit': _ZERO,
'credit': check.amount,
'account': journal.issued_check_account.id,
'move': self.move.id,
'journal': journal.id,
'period': Period.find(self.company.id, date=self.date),
'party': (
journal.issued_check_account.party_required and
self.party.id or None),
'maturity_date': check.date,
})
if self.third_pay_checks:
for check in self.third_pay_checks:
move_lines.append({
'debit': _ZERO,
'credit': check.amount,
'account': journal.third_check_account.id,
'move': self.move.id,
'journal': journal.id,
'period': Period.find(self.company.id, date=self.date),
'party': (
journal.third_check_account.party_required and
self.party.id or None),
'maturity_date': check.date,
})
return move_lines
@classmethod
@ModelView.button
def post(cls, vouchers):
pool = Pool()
ThirdCheck = pool.get('account.third.check')
IssuedCheck = pool.get('account.issued.check')
Date = pool.get('ir.date')
super().post(vouchers)
today = Date.today()
for voucher in vouchers:
if voucher.issued_check:
for check in voucher.issued_check:
IssuedCheck.write([check], {
'receiving_party': voucher.party.id,
'state': 'issued',
'name': check.checkbook and
check.checkbook.sequence.get() or check.name
})
IssuedCheck.issued(voucher.issued_check)
if voucher.third_check:
ThirdCheck.write(list(voucher.third_check), {
'source_party': voucher.party.id,
'state': 'held',
})
if voucher.third_pay_checks:
ThirdCheck.write(list(voucher.third_pay_checks), {
'destiny_party': voucher.party.id,
'date_out': today,
'state': 'delivered',
})
@classmethod
@ModelView.button
def cancel(cls, vouchers):
pool = Pool()
ThirdCheck = pool.get('account.third.check')
IssuedCheck = pool.get('account.issued.check')
for voucher in vouchers:
if voucher.issued_check:
for check in voucher.issued_check:
if check.state != 'issued':
raise UserError(gettext(
'account_voucher_ar.msg_issued_check_not_issued',
check=check.name))
IssuedCheck.write(list(voucher.issued_check), {
'receiving_party': None,
'state': 'draft',
})
if voucher.third_check:
for check in voucher.third_check:
if check.state not in ['held', 'reverted']:
raise UserError(gettext(
'account_voucher_ar.msg_third_check_not_held',
check=check.name))
ThirdCheck.write(list(voucher.third_check), {
'source_party': None,
'state': 'draft',
})
if voucher.third_pay_checks:
for check in voucher.third_pay_checks:
if check.state != 'delivered':
raise UserError(gettext('account_voucher_ar.'
'msg_third_pay_check_not_delivered',
check=check.name))
ThirdCheck.write(list(voucher.third_pay_checks), {
'destiny_party': None,
'date_out': None,
'state': 'held',
})
super().cancel(vouchers)