-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfiscalyear.py
112 lines (96 loc) · 4.13 KB
/
fiscalyear.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
# This file is part of the account_voucher_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
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, Id
from trytond.exceptions import UserError
from trytond.i18n import gettext
class FiscalYear(metaclass=PoolMeta):
__name__ = 'account.fiscalyear'
payment_sequence = fields.Many2One('ir.sequence',
'Payment Sequence', required=True,
domain=[
('sequence_type', '=',
Id('account_voucher_ar', 'seq_type_account_voucher_payment')),
('company', '=', Eval('company')),
],
context={
'company': Eval('company'),
},
depends={'company'})
receipt_sequence = fields.Many2One('ir.sequence',
'Receipt Sequence', required=True,
domain=[
('sequence_type', '=',
Id('account_voucher_ar', 'seq_type_account_voucher_receipt')),
('company', '=', Eval('company')),
],
context={
'company': Eval('company'),
},
depends={'company'})
@classmethod
def validate(cls, years):
super().validate(years)
for year in years:
year.check_voucher_sequences()
def check_voucher_sequences(self):
for sequence in ('payment_sequence', 'receipt_sequence'):
fiscalyears = self.search([
(sequence, '=', getattr(self, sequence).id),
('id', '!=', self.id),
])
if fiscalyears:
raise UserError(gettext(
'account_voucher_ar.msg_different_voucher_sequence',
first=self.rec_name, second=fiscalyears[0].rec_name))
@classmethod
def write(cls, *args):
Voucher = Pool().get('account.voucher')
actions = iter(args)
for fiscalyears, values in zip(actions, actions):
for sequence in ('payment_sequence', 'receipt_sequence'):
if not values.get(sequence):
continue
for fiscalyear in fiscalyears:
if (getattr(fiscalyear, sequence) and
(getattr(fiscalyear, sequence).id !=
values[sequence])):
if Voucher.search([
('date', '>=', fiscalyear.start_date),
('date', '<=', fiscalyear.end_date),
('number', '!=', None),
('voucher_type', '=', sequence[:-9]),
]):
raise UserError(gettext('account_voucher_ar.'
'msg_change_voucher_sequence',
fiscal_year=fiscalyear.rec_name))
super().write(*args)
def get_voucher_sequence(self, voucher_type):
return getattr(self, voucher_type + '_sequence')
class RenewFiscalYear(metaclass=PoolMeta):
__name__ = 'account.fiscalyear.renew'
def fiscalyear_defaults(self):
pool = Pool()
Sequence = pool.get('ir.sequence')
defaults = super().fiscalyear_defaults()
prev_payment_sequence = Sequence(
self.start.previous_fiscalyear.payment_sequence.id)
payment_sequence, = Sequence.copy([prev_payment_sequence])
if self.start.reset_sequences:
payment_sequence.number_next = 1
else:
payment_sequence.number_next = prev_payment_sequence.number_next
payment_sequence.save()
defaults['payment_sequence'] = payment_sequence.id
prev_receipt_sequence = Sequence(
self.start.previous_fiscalyear.receipt_sequence.id)
receipt_sequence, = Sequence.copy([prev_receipt_sequence])
if self.start.reset_sequences:
receipt_sequence.number_next = 1
else:
receipt_sequence.number_next = prev_receipt_sequence.number_next
receipt_sequence.save()
defaults['receipt_sequence'] = receipt_sequence.id
return defaults