-
Notifications
You must be signed in to change notification settings - Fork 10
/
fiscalyear.py
655 lines (592 loc) · 23.8 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# 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 dateutil.relativedelta import relativedelta
from trytond.i18n import gettext
from trytond.model import ModelSQL, ModelView, Workflow, fields
from trytond.model.exceptions import AccessError
from trytond.pool import Pool
from trytond.pyson import Eval, Id
from trytond.rpc import RPC
from trytond.transaction import Transaction
from trytond.wizard import (
Button, StateAction, StateTransition, StateView, Wizard)
from .exceptions import (
FiscalYearCloseError, FiscalYearDatesError, FiscalYearNotFoundError,
FiscalYearReOpenError, FiscalYearSequenceError)
STATES = {
'readonly': Eval('state') != 'open',
}
class FiscalYear(Workflow, ModelSQL, ModelView):
'Fiscal Year'
__name__ = 'account.fiscalyear'
name = fields.Char('Name', size=None, required=True)
start_date = fields.Date('Starting Date', required=True, states=STATES,
domain=[('start_date', '<=', Eval('end_date', None))])
end_date = fields.Date('Ending Date', required=True, states=STATES,
domain=[('end_date', '>=', Eval('start_date', None))])
periods = fields.One2Many('account.period', 'fiscalyear', 'Periods',
states=STATES,
domain=[
('company', '=', Eval('company')),
],
order=[('start_date', 'ASC'), ('id', 'ASC')])
state = fields.Selection([
('open', 'Open'),
('close', 'Close'),
('locked', 'Locked'),
], 'State', readonly=True, required=True, sort=False)
post_move_sequence = fields.Many2One(
'ir.sequence', "Post Move Sequence", required=True,
domain=[
('sequence_type', '=',
Id('account', 'sequence_type_account_move')),
('company', '=', Eval('company')),
])
company = fields.Many2One(
'company.company', "Company", required=True, select=True)
icon = fields.Function(fields.Char("Icon"), 'get_icon')
@classmethod
def __setup__(cls):
super(FiscalYear, cls).__setup__()
cls._order.insert(0, ('start_date', 'DESC'))
cls._transitions |= set((
('open', 'close'),
('close', 'locked'),
('close', 'open'),
))
cls._buttons.update({
'create_periods': {
'invisible': ((Eval('state') != 'open')
| Eval('periods', [0])),
'depends': ['state'],
},
'close': {
'invisible': Eval('state') != 'open',
'depends': ['state'],
},
'reopen': {
'invisible': Eval('state') != 'close',
'depends': ['state'],
},
'lock_': {
'invisible': Eval('state') != 'close',
'depends': ['state'],
},
})
cls.__rpc__.update({
'create_period': RPC(readonly=False, instantiate=0),
})
@staticmethod
def default_state():
return 'open'
@staticmethod
def default_company():
return Transaction().context.get('company')
def get_icon(self, name):
return {
'open': 'tryton-account-open',
'close': 'tryton-account-close',
'locked': 'tryton-account-block',
}.get(self.state)
@classmethod
def validate_fields(cls, fiscalyears, field_names):
super().validate_fields(fiscalyears, field_names)
cls.check_dates(fiscalyears, field_names)
cls.check_post_move_sequence(fiscalyears, field_names)
@classmethod
def check_dates(cls, fiscalyears, field_names=None):
if field_names and not (field_names & {
'start_date', 'end_date', 'state', 'company'}):
return
transaction = Transaction()
connection = transaction.connection
cls.lock()
cursor = connection.cursor()
table = cls.__table__()
for year in fiscalyears:
cursor.execute(*table.select(table.id,
where=(((table.start_date <= year.start_date)
& (table.end_date >= year.start_date))
| ((table.start_date <= year.end_date)
& (table.end_date >= year.end_date))
| ((table.start_date >= year.start_date)
& (table.end_date <= year.end_date)))
& (table.company == year.company.id)
& (table.id != year.id)))
second_id = cursor.fetchone()
if second_id:
second = cls(second_id[0])
raise FiscalYearDatesError(
gettext('account.msg_fiscalyear_overlap',
first=year.rec_name,
second=second.rec_name))
if year.state == 'open':
fiscalyears = cls.search([
('start_date', '>=', year.end_date),
('state', 'in', ['close', 'locked']),
('company', '=', year.company.id),
],
limit=1,
order=[('start_date', 'ASC')])
if fiscalyears:
fiscalyear, = fiscalyears
raise FiscalYearDatesError(
gettext('account.msg_open_fiscalyear_earlier',
open=year.rec_name,
close=fiscalyear.rec_name))
@classmethod
def check_post_move_sequence(cls, fiscalyears, field_names=None):
if field_names and 'post_move_sequence' not in field_names:
return
for fiscalyear in fiscalyears:
sequence = fiscalyear.post_move_sequence
years = cls.search([
('post_move_sequence', '=', sequence.id),
('id', '!=', fiscalyear.id),
], limit=1)
if years:
raise FiscalYearSequenceError(gettext(
'account.msg_fiscalyear_different_post_move_sequence',
first=fiscalyear.rec_name,
second=years[0].rec_name))
@classmethod
def write(cls, *args):
pool = Pool()
Move = pool.get('account.move')
actions = iter(args)
for fiscalyears, values in zip(actions, actions):
if values.get('post_move_sequence'):
for fiscalyear in fiscalyears:
if (fiscalyear.post_move_sequence
and fiscalyear.post_move_sequence.id
!= values['post_move_sequence']):
if Move.search([
('period.fiscalyear', '=', fiscalyear.id),
('state', '=', 'posted'),
]):
raise AccessError(
gettext('account.'
'msg_change_fiscalyear_post_move_sequence',
fiscalyear=fiscalyear.rec_name))
super(FiscalYear, cls).write(*args)
@classmethod
def delete(cls, fiscalyears):
Period = Pool().get('account.period')
Period.delete([p for f in fiscalyears for p in f.periods])
super(FiscalYear, cls).delete(fiscalyears)
@classmethod
def create_period(cls, fiscalyears, interval=1, end_day=31):
'''
Create periods for the fiscal years with month interval
'''
Period = Pool().get('account.period')
to_create = []
for fiscalyear in fiscalyears:
period_start_date = fiscalyear.start_date
while period_start_date < fiscalyear.end_date:
month_offset = 1 if period_start_date.day < end_day else 0
period_end_date = (period_start_date
+ relativedelta(months=interval - month_offset)
+ relativedelta(day=end_day))
if period_end_date > fiscalyear.end_date:
period_end_date = fiscalyear.end_date
name = period_start_date.strftime('%Y-%m')
if name != period_end_date.strftime('%Y-%m'):
name += ' - ' + period_end_date.strftime('%Y-%m')
to_create.append({
'name': name,
'start_date': period_start_date,
'end_date': period_end_date,
'fiscalyear': fiscalyear.id,
'type': 'standard',
})
period_start_date = period_end_date + relativedelta(days=1)
if to_create:
Period.create(to_create)
@classmethod
@ModelView.button_action('account.act_create_periods')
def create_periods(cls, fiscalyears):
pass
@classmethod
def find(cls, company_id, date=None, exception=True):
'''
Return the fiscal year for the company_id
at the date or the current date.
If exception is set the function will raise an exception
if any fiscal year is found.
'''
pool = Pool()
Lang = pool.get('ir.lang')
Date = pool.get('ir.date')
if not date:
with Transaction().set_context(company=company_id):
date = Date.today()
fiscalyears = cls.search([
('start_date', '<=', date),
('end_date', '>=', date),
('company', '=', company_id),
], order=[('start_date', 'DESC')], limit=1)
if not fiscalyears:
if exception:
lang = Lang.get()
raise FiscalYearNotFoundError(
gettext('account.msg_no_fiscalyear_date',
date=lang.strftime(date)))
else:
return None
return fiscalyears[0].id
def get_deferral(self, account):
'Computes deferrals for accounts'
pool = Pool()
Currency = pool.get('currency.currency')
Deferral = pool.get('account.account.deferral')
if not account.type:
return
if not account.deferral:
if not Currency.is_zero(self.company.currency, account.balance):
raise FiscalYearCloseError(
gettext('account'
'.msg_close_fiscalyear_account_balance_not_zero',
account=account.rec_name))
else:
deferral = Deferral()
deferral.account = account
deferral.fiscalyear = self
deferral.debit = account.debit
deferral.credit = account.credit
deferral.line_count = account.line_count
deferral.amount_second_currency = account.amount_second_currency
return deferral
@classmethod
@ModelView.button
@Workflow.transition('close')
def close(cls, fiscalyears):
'''
Close a fiscal year
'''
pool = Pool()
Period = pool.get('account.period')
Account = pool.get('account.account')
Deferral = pool.get('account.account.deferral')
# Prevent create new fiscal year or period
cls.lock()
Period.lock()
deferrals = []
for fiscalyear in fiscalyears:
if cls.search([
('end_date', '<=', fiscalyear.start_date),
('state', '=', 'open'),
('company', '=', fiscalyear.company.id),
]):
raise FiscalYearCloseError(
gettext('account.msg_close_fiscalyear_earlier',
fiscalyear=fiscalyear.rec_name))
periods = Period.search([
('fiscalyear', '=', fiscalyear.id),
])
Period.close(periods)
with Transaction().set_context(fiscalyear=fiscalyear.id,
date=None, cumulate=True, journal=None):
accounts = Account.search([
('company', '=', fiscalyear.company.id),
])
for account in accounts:
deferral = fiscalyear.get_deferral(account)
if deferral:
deferrals.append(deferral)
Deferral.save(deferrals)
@classmethod
@ModelView.button
@Workflow.transition('open')
def reopen(cls, fiscalyears):
'''
Re-open a fiscal year
'''
Deferral = Pool().get('account.account.deferral')
for fiscalyear in fiscalyears:
if cls.search([
('start_date', '>=', fiscalyear.end_date),
('state', '!=', 'open'),
('company', '=', fiscalyear.company.id),
]):
raise FiscalYearReOpenError(
gettext('account.msg_reopen_fiscalyear_later',
fiscalyear=fiscalyear.rec_name))
deferrals = Deferral.search([
('fiscalyear', '=', fiscalyear.id),
])
Deferral.delete(deferrals)
@classmethod
@ModelView.button
@Workflow.transition('locked')
def lock_(cls, fiscalyears):
pool = Pool()
Period = pool.get('account.period')
periods = Period.search([
('fiscalyear', 'in', [f.id for f in fiscalyears]),
])
Period.lock_(periods)
class BalanceNonDeferralStart(ModelView):
'Balance Non-Deferral'
__name__ = 'account.fiscalyear.balance_non_deferral.start'
fiscalyear = fields.Many2One('account.fiscalyear', 'Fiscal Year',
required=True, domain=[('state', '=', 'open')])
company = fields.Function(fields.Many2One('company.company', 'Company'),
'on_change_with_company')
journal = fields.Many2One('account.journal', 'Journal', required=True,
domain=[
('type', '=', 'situation'),
],
context={
'company': Eval('company', -1),
},
depends={'company'})
period = fields.Many2One('account.period', 'Period', required=True,
domain=[
('fiscalyear', '=', Eval('fiscalyear')),
('type', '=', 'adjustment'),
])
credit_account = fields.Many2One('account.account', 'Credit Account',
required=True,
domain=[
('type', '!=', None),
('closed', '!=', True),
('company', '=', Eval('company', -1)),
('deferral', '=', True),
])
debit_account = fields.Many2One('account.account', 'Debit Account',
required=True,
domain=[
('type', '!=', None),
('closed', '!=', True),
('company', '=', Eval('company', -1)),
('deferral', '=', True),
])
@fields.depends('fiscalyear')
def on_change_with_company(self, name=None):
if self.fiscalyear:
return self.fiscalyear.company.id
class BalanceNonDeferral(Wizard):
'Balance Non-Deferral'
__name__ = 'account.fiscalyear.balance_non_deferral'
start = StateView('account.fiscalyear.balance_non_deferral.start',
'account.fiscalyear_balance_non_deferral_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('OK', 'balance', 'tryton-ok', default=True),
])
balance = StateAction('account.act_move_form')
def get_move_line(self, account):
pool = Pool()
Line = pool.get('account.move.line')
# Don't use account.balance because we need the non-commulated balance
balance = account.debit - account.credit
if account.company.currency.is_zero(balance):
return
line = Line()
line.account = account
if balance >= 0:
line.credit = abs(balance)
line.debit = 0
else:
line.credit = 0
line.debit = abs(balance)
return line
def get_counterpart_line(self, amount):
pool = Pool()
Line = pool.get('account.move.line')
if self.start.fiscalyear.company.currency.is_zero(amount):
return
line = Line()
if amount >= 0:
line.credit = abs(amount)
line.debit = 0
line.account = self.start.credit_account
else:
line.credit = 0
line.debit = abs(amount)
line.account = self.start.debit_account
return line
def create_move(self):
pool = Pool()
Account = pool.get('account.account')
Move = pool.get('account.move')
with Transaction().set_context(fiscalyear=self.start.fiscalyear.id,
date=None, cumulate=False):
accounts = Account.search([
('company', '=', self.start.fiscalyear.company.id),
('deferral', '=', False),
('type', '!=', None),
('closed', '!=', True),
])
lines = []
for account in accounts:
line = self.get_move_line(account)
if line:
lines.append(line)
if not lines:
return
amount = sum(l.debit - l.credit for l in lines)
counter_part_line = self.get_counterpart_line(amount)
if counter_part_line:
lines.append(counter_part_line)
move = Move()
move.period = self.start.period
move.journal = self.start.journal
move.date = self.start.period.start_date
move.origin = self.start.fiscalyear
move.lines = lines
move.save()
return move
def do_balance(self, action):
move = self.create_move()
if move:
action['views'].reverse()
return action, {'res_id': move.id if move else None}
class CreatePeriodsStart(ModelView):
"Create Periods Start"
__name__ = 'account.fiscalyear.create_periods.start'
frequency = fields.Selection([
('monthly', "Monthly"),
('quarterly', "Quarterly"),
('other', "Other"),
], "Frequency", sort=False, required=True)
interval = fields.Integer("Interval", required=True,
states={
'invisible': Eval('frequency') != 'other',
},
help="The length of each period, in months.")
end_day = fields.Integer("End Day", required=True,
help="The day of the month on which periods end.\n"
"Months with fewer days will end on the last day.")
@classmethod
def default_frequency(cls):
return 'monthly'
@classmethod
def default_end_day(cls):
return 31
@classmethod
def frequency_intervals(cls):
return {
'monthly': 1,
'quarterly': 3,
'other': None,
}
@fields.depends('frequency', 'interval')
def on_change_frequency(self):
if self.frequency:
self.interval = self.frequency_intervals()[self.frequency]
class CreatePeriods(Wizard):
"Create Periods"
__name__ = 'account.fiscalyear.create_periods'
start = StateView('account.fiscalyear.create_periods.start',
'account.fiscalyear_create_periods_start_view_form', [
Button("Cancel", 'end', 'tryton-cancel'),
Button("Create", 'create_periods', 'tryton-ok', default=True),
])
create_periods = StateTransition()
def transition_create_periods(self):
self.model.create_period(
[self.record], self.start.interval, self.start.end_day)
return 'end'
def month_delta(d1, d2):
month_offset = 1 if d1.day < d2.day else 0
return (d1.year - d2.year) * 12 + d1.month - d2.month - month_offset
class RenewFiscalYearStart(ModelView):
"Renew Fiscal Year Start"
__name__ = 'account.fiscalyear.renew.start'
name = fields.Char("Name", required=True)
company = fields.Many2One('company.company', "Company", required=True)
previous_fiscalyear = fields.Many2One(
'account.fiscalyear', "Previous Fiscalyear", required=True,
domain=[
('company', '=', Eval('company')),
],
help="Used as reference for fiscalyear configuration.")
start_date = fields.Date("Start Date", required=True)
end_date = fields.Date("End Date", required=True)
reset_sequences = fields.Boolean("Reset Sequences",
help="If checked, new sequences will be created.")
@classmethod
def default_company(cls):
return Transaction().context.get('company')
@classmethod
def default_previous_fiscalyear(cls):
pool = Pool()
FiscalYear = pool.get('account.fiscalyear')
fiscalyears = FiscalYear.search([
('company', '=', cls.default_company() or -1),
],
order=[('end_date', 'DESC')], limit=1)
if fiscalyears:
fiscalyear, = fiscalyears
return fiscalyear.id
@classmethod
def default_reset_sequences(cls):
return True
@fields.depends('previous_fiscalyear')
def on_change_previous_fiscalyear(self):
if self.previous_fiscalyear:
fiscalyear = self.previous_fiscalyear
months = month_delta(
fiscalyear.end_date, fiscalyear.start_date) + 1
self.start_date = fiscalyear.start_date + relativedelta(
months=months, day=fiscalyear.start_date.day)
self.end_date = fiscalyear.end_date + relativedelta(
months=months, day=fiscalyear.end_date.day)
self.name = fiscalyear.name.replace(
str(fiscalyear.end_date.year),
str(self.end_date.year)).replace(
str(fiscalyear.start_date.year),
str(self.start_date.year))
class RenewFiscalYear(Wizard):
"Renew Fiscal Year"
__name__ = 'account.fiscalyear.renew'
start = StateView('account.fiscalyear.renew.start',
'account.fiscalyear_renew_start_view_form', [
Button("Cancel", 'end', 'tryton-cancel'),
Button("Create", 'create_', 'tryton-ok', default=True),
])
create_ = StateAction('account.act_fiscalyear_form')
def fiscalyear_defaults(self):
pool = Pool()
Sequence = pool.get('ir.sequence')
defaults = {
'name': self.start.name,
'start_date': self.start.start_date,
'end_date': self.start.end_date,
'periods': [],
}
previous_sequence = self.start.previous_fiscalyear.post_move_sequence
sequence, = Sequence.copy([previous_sequence],
default={
'name': lambda data: data['name'].replace(
self.start.previous_fiscalyear.name,
self.start.name)
})
if self.start.reset_sequences:
sequence.number_next = 1
else:
sequence.number_next = previous_sequence.number_next
sequence.save()
defaults['post_move_sequence'] = sequence.id
return defaults
def create_fiscalyear(self):
pool = Pool()
FiscalYear = pool.get('account.fiscalyear')
fiscalyear, = FiscalYear.copy(
[self.start.previous_fiscalyear],
default=self.fiscalyear_defaults())
periods = [p for p in self.start.previous_fiscalyear.periods
if p.type == 'standard']
months = month_delta(fiscalyear.end_date, fiscalyear.start_date) + 1
interval = months / len(periods)
end_day = max(p.end_date.day
for p in self.start.previous_fiscalyear.periods
if p.type == 'standard')
if interval.is_integer():
FiscalYear.create_period([fiscalyear], interval, end_day)
return fiscalyear
def do_create_(self, action):
fiscalyear = self.create_fiscalyear()
action['views'].reverse()
return action, {'res_id': fiscalyear.id}