24
24
from tools.translate import _
26
26
from dateutil.relativedelta import relativedelta
27
from account_period_closing_level import ACCOUNT_FY_STATE_SELECTION
28
30
class account_fiscalyear(osv.osv):
29
31
_name = "account.fiscalyear"
30
32
_inherit = "account.fiscalyear"
32
34
def _get_is_closable(self, cr, uid, ids, field_names, args, context=None):
33
# US-131: closable if all periods HQ closed (special ones too)
40
41
level = self.pool.get('res.users').browse(cr, uid, [uid],
41
42
context=context)[0].company_id.instance_id.level
42
if level != 'section':
43
# not at HQ level: FY not closable
49
# - FY is not already closed
50
# - all FY's periods are HQ closed (state 'done') (special ones too)
51
for br in self.browse(cr, uid, ids, context=context):
54
if br.state != 'done':
55
closable = all([ True if p.state == 'done' else False \
56
for p in br.period_ids ])
43
ayec_obj = self.pool.get('account.year.end.closing')
48
# - periods, with special ones too except those for year end closing
49
for fy in self.browse(cr, uid, ids, context=context):
50
# check previous FY closed
51
# check next FY exists (we need FY+1 Period 0 for initial balances)
55
prev_fy_id = ayec_obj._get_next_fy_id(cr, uid, fy,
56
get_previous=True, context=context)
58
prev_fy = self.browse(cr, uid, prev_fy_id, context=context)
61
prev_fy_ok = prev_fy.state in ('mission-closed', 'done', )
62
elif level == 'section':
63
prev_fy_ok = prev_fy.state in ('done', )
68
mission = level == 'coordo' and fy.state == 'draft' \
69
and all([ p.state in ('mission-closed', 'done') \
70
for p in fy.period_ids if 0 < p.number < 16 ]) \
72
hq = level == 'section' and fy.state in ('draft', 'mission-closed') \
73
and all([ p.state == 'done' \
74
for p in fy.period_ids if 0 < p.number < 16 ]) \
78
'is_mission_closable': mission,
80
'can_reopen_mission': level == 'coordo' \
81
and fy.state == 'mission-closed' or False,
64
'is_closable': fields.function(_get_is_closable, type='boolean',
65
method=True, string='Closable ? (all periods HQ closed)'),
86
'state': fields.selection(ACCOUNT_FY_STATE_SELECTION, 'State',
88
'is_mission_closable': fields.function(_get_is_closable, type='boolean',
89
method=True, multi="closable",
90
string='Mission Closable ? (all periods Mission closed)'),
91
'is_hq_closable': fields.function(_get_is_closable, type='boolean',
92
method=True, multi="closable",
93
string='HQ Closable ? (all periods HQ closed)'),
94
'can_reopen_mission': fields.function(_get_is_closable, type='boolean',
95
method=True, multi="closable",
96
string='Mission reopen available ?'),
100
'is_mission_closable': False,
101
'is_hq_closable': False,
102
'can_reopen_mission': False,
72
105
def create_period(self,cr, uid, ids, context=None, interval=1):
114
147
# First default behaviour
115
148
res = super(account_fiscalyear, self).create(cr, uid, vals, context=context)
150
# update fiscalyear state
151
self.pool.get('account.fiscalyear.state').update_state(cr, uid, [res],
116
154
# Prepare some values
117
155
current_instance_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.instance_id.id
118
156
name = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.name
122
160
self.pool.get('account.journal').create_fiscalyear_sequence(cr, uid, res, name, "journal_%s"%(journal.id), vals['date_start'], journal.sequence_id and journal.sequence_id.id or False, context=context)
125
def uf_close_fy(self, cr, uid, ids, context=None):
127
US-131: Close FY(s) at HQ level: just set state to 'done'
128
(to prevent use of FY(s) in many2one fields)
163
def write(self, cr, uid, ids, vals, context=None):
166
if isinstance(ids, (int, long, )):
171
res = super(account_fiscalyear, self).write(cr, uid, ids, vals,
174
# update fiscalyear state
175
self.pool.get('account.fiscalyear.state').update_state(cr, uid, ids,
180
def _close_fy(self, cr, uid, ids, context=None):
133
if isinstance(ids, (int, long)):
186
fy_id = ids[0] # active form's FY
188
# open year end wizard
189
context['fy_id'] = fy_id
190
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid,
191
'account_period_closing_level',
192
'wizard_view_account_year_end_closing')[1]
194
'name': 'Close the fiscal year',
195
'type': 'ir.actions.act_window',
196
'res_model': 'wizard.account.year.end.closing',
199
'view_id': [view_id],
204
def btn_mission_close(self, cr, uid, ids, context=None):
205
return self._close_fy(cr, uid, ids, context=context)
207
def btn_hq_close(self, cr, uid, ids, context=None):
208
return self._close_fy(cr, uid, ids, context=context)
210
def btn_mission_reopen(self, cr, uid, ids, context=None):
213
if isinstance(ids, (int, long, )):
136
for r in self.read(cr, uid, ids, ['is_closable', ], context=context):
137
if not r['is_closable']:
138
raise osv.except_osv(_("Warning"),
139
_("Fiscal Year can not be closed. Aborted." \
140
" (All periods must be HQ closed)"))
142
self.write(cr, uid, ids, { 'state': 'done' }, context=context)
216
ayec_obj = self.pool.get('account.year.end.closing')
218
ayec_obj.delete_year_end_entries(cr, uid, fy_id, context=context)
219
ayec_obj.update_fy_state(cr, uid, fy_id, reopen=True, context=context)
145
222
account_fiscalyear()
146
223
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: