~julie-w/unifield-wm/UTP-758

« back to all changes in this revision

Viewing changes to account_period_closing_level/account_fiscalyear.py

  • Committer: jf
  • Date: 2016-01-25 14:14:07 UTC
  • mfrom: (2703.20.59 unifield-wm)
  • Revision ID: jfb@tempo-consulting.fr-20160125141407-vgfz8lrdvvgq0zip
US-822 [IMP] Year End Closure functionnality
lp:~unifield-team/unifield-wm/us-822

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from tools.translate import _
25
25
import datetime
26
26
from dateutil.relativedelta import relativedelta
 
27
from account_period_closing_level import ACCOUNT_FY_STATE_SELECTION
 
28
 
27
29
 
28
30
class account_fiscalyear(osv.osv):
29
31
    _name = "account.fiscalyear"
30
32
    _inherit = "account.fiscalyear"
31
33
 
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)
34
35
        res = {}
35
36
        if not ids:
36
37
            return res
39
40
 
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
44
 
            for id in ids:
45
 
                res[id] = False
46
 
            return res
47
 
 
48
 
        # check:
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):
52
 
            closable = False
53
 
 
54
 
            if br.state != 'done':
55
 
                closable = all([ True if p.state == 'done' else False \
56
 
                    for p in br.period_ids ])
57
 
 
58
 
            res[br.id] = closable
59
 
 
 
43
        ayec_obj = self.pool.get('account.year.end.closing')
 
44
 
 
45
        # check matching of:
 
46
        # - instance level
 
47
        # - FY state
 
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)
 
52
            mission = False
 
53
            hq = False
 
54
 
 
55
            prev_fy_id = ayec_obj._get_next_fy_id(cr, uid, fy,
 
56
                get_previous=True, context=context)
 
57
            if prev_fy_id:
 
58
                prev_fy = self.browse(cr, uid, prev_fy_id, context=context)
 
59
                prev_fy_ok = False
 
60
                if level == 'coordo':
 
61
                    prev_fy_ok = prev_fy.state in ('mission-closed', 'done', )
 
62
                elif level == 'section':
 
63
                    prev_fy_ok = prev_fy.state in ('done', )
 
64
            else:
 
65
                prev_fy_ok = True
 
66
 
 
67
            if prev_fy_ok:
 
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 ]) \
 
71
                    or False
 
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 ]) \
 
75
                    or False
 
76
 
 
77
            res[fy.id] = {
 
78
                'is_mission_closable': mission,
 
79
                'is_hq_closable': hq,
 
80
                'can_reopen_mission': level == 'coordo' \
 
81
                    and fy.state == 'mission-closed' or False,
 
82
            }
60
83
        return res
61
84
 
62
85
    _columns = {
63
 
        # US-131
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',
 
87
            readonly=True),
 
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 ?'),
66
97
    }
67
98
 
68
99
    _defaults = {
69
 
        'is_closable': False,
 
100
        'is_mission_closable': False,
 
101
        'is_hq_closable': False,
 
102
        'can_reopen_mission': False,
70
103
    }
71
104
 
72
105
    def create_period(self,cr, uid, ids, context=None, interval=1):
113
146
            context = {}
114
147
        # First default behaviour
115
148
        res = super(account_fiscalyear, self).create(cr, uid, vals, context=context)
 
149
 
 
150
        # update fiscalyear state
 
151
        self.pool.get('account.fiscalyear.state').update_state(cr, uid, [res],
 
152
            context=context)
 
153
 
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)
123
161
        return res
124
162
 
125
 
    def uf_close_fy(self, cr, uid, ids, context=None):
126
 
        """
127
 
        US-131: Close FY(s) at HQ level: just set state to 'done'
128
 
        (to prevent use of FY(s) in many2one fields)
129
 
        """
 
163
    def write(self, cr, uid, ids, vals, context=None):
 
164
        if not ids:
 
165
            return False
 
166
        if isinstance(ids, (int, long, )):
 
167
            ids = [ids]
 
168
        if context is None:
 
169
            context = {}
 
170
 
 
171
        res = super(account_fiscalyear, self).write(cr, uid, ids, vals,
 
172
            context=context)
 
173
 
 
174
        # update fiscalyear state
 
175
        self.pool.get('account.fiscalyear.state').update_state(cr, uid, ids,
 
176
            context=context)
 
177
 
 
178
        return res
 
179
 
 
180
    def _close_fy(self, cr, uid, ids, context=None):
130
181
        res = {}
131
182
        if not ids:
132
183
            return res
133
 
        if isinstance(ids, (int, long)):
 
184
        if context is None:
 
185
            context = {}
 
186
        fy_id = ids[0]  # active form's FY
 
187
 
 
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]
 
193
        return {
 
194
            'name': 'Close the fiscal year',
 
195
            'type': 'ir.actions.act_window',
 
196
            'res_model': 'wizard.account.year.end.closing',
 
197
            'view_type': 'form',
 
198
            'view_mode': 'form',
 
199
            'view_id': [view_id],
 
200
            'target': 'new',
 
201
            'context': context,
 
202
        }
 
203
 
 
204
    def btn_mission_close(self, cr, uid, ids, context=None):
 
205
        return self._close_fy(cr, uid, ids, context=context)
 
206
 
 
207
    def btn_hq_close(self, cr, uid, ids, context=None):
 
208
        return self._close_fy(cr, uid, ids, context=context)
 
209
 
 
210
    def btn_mission_reopen(self, cr, uid, ids, context=None):
 
211
        if not ids:
 
212
            return
 
213
        if isinstance(ids, (int, long, )):
134
214
            ids = [ids]
135
 
 
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)"))
141
 
 
142
 
        self.write(cr, uid, ids, { 'state': 'done' }, context=context)
143
 
        return res
 
215
        fy_id = ids[0]
 
216
        ayec_obj = self.pool.get('account.year.end.closing')
 
217
 
 
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)
 
220
        return {}
144
221
 
145
222
account_fiscalyear()
146
223
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: