~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to analytic_distribution/analytic_account.py

  • Committer: jf
  • Date: 2012-11-29 13:00:27 UTC
  • mfrom: (1287.9.8 unifield-wm)
  • Revision ID: jfb@tempo-consulting.fr-20121129130027-564r4yns8t5d2v7d
UTP-368 [FIX] Shouldn't be possible to source a non-stockable product to stock in the order sourcing tool (fix unit test)
lp:~unifield-team/unifield-wm/utp-368

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    _name = "account.analytic.account"
32
32
    _inherit = "account.analytic.account"
33
33
 
34
 
    def _get_active(self, cr, uid, ids, field_name, args, context=None):
35
 
        '''
36
 
        If date out of date_start/date of given analytic account, then account is inactive.
37
 
        The comparison could be done via a date given in context.
38
 
        '''
39
 
        res = {}
40
 
        cmp_date = datetime.date.today().strftime('%Y-%m-%d')
41
 
        if context.get('date', False):
42
 
            cmp_date = context.get('date')
43
 
        for a in self.browse(cr, uid, ids):
44
 
            res[a.id] = True
45
 
            if a.date_start > cmp_date:
46
 
                res[a.id] = False
47
 
            if a.date and a.date <= cmp_date:
48
 
                res[a.id] = False
49
 
        return res
50
 
 
51
 
    def _search_filter_active(self, cr, uid, ids, name, args, context=None):
52
 
        """
53
 
        UTP-410: Add the search on active/inactive CC
54
 
        """
55
 
        arg = []
56
 
        cmp_date = datetime.date.today().strftime('%Y-%m-%d')
57
 
        if context.get('date', False):
58
 
            cmp_date = context.get('date')
59
 
        for x in args:
60
 
            if x[0] == 'filter_active' and x[2] == True:
61
 
                arg.append(('date_start', '<=', cmp_date))
62
 
                arg.append('|')
63
 
                arg.append(('date', '>', cmp_date))
64
 
                arg.append(('date', '=', False))
65
 
            elif x[0] == 'filter_active' and x[2] == False:
66
 
                arg.append('|')
67
 
                arg.append(('date_start', '>', cmp_date))
68
 
                arg.append(('date', '<=', cmp_date))
69
 
        return arg
70
 
 
71
 
    def _search_closed_by_a_fp(self, cr, uid, ids, name, args, context=None):
72
 
        """
73
 
        UTP-423: Do not display analytic accounts linked to a soft/hard closed contract.
74
 
        """
75
 
        res = [('id', 'not in', [])]
76
 
        if args and args[0] and len(args[0]) == 3:
77
 
            if args[0][1] != '=':
78
 
                raise osv.except_osv(_('Error'), _('Operator not supported yet!'))
79
 
            # Search all fp_ids from soft_closed contract
80
 
            sql="""SELECT a.id
81
 
                FROM account_analytic_account a, financing_contract_contract fcc, financing_contract_funding_pool_line fcfl
82
 
                WHERE fcfl.contract_id = fcc.id
83
 
                AND fcfl.funding_pool_id = a.id
84
 
                AND fcc.state in ('soft_closed', 'hard_closed');"""
85
 
            cr.execute(sql)
86
 
            sql_res = cr.fetchall()
87
 
            if sql_res:
88
 
                aa_ids = self.is_blocked_by_a_contract(cr, uid, [x and x[0] for x in sql_res])
89
 
                if aa_ids:
90
 
                    if isinstance(aa_ids, (int, long)):
91
 
                        aa_ids = [aa_ids]
92
 
                    res = [('id', 'not in', aa_ids)]
93
 
        return res
94
 
 
95
 
    def _get_fake(self, cr, uid, ids, *a, **b):
96
 
        return {}.fromkeys(ids, False)
97
 
 
98
 
    def _search_intermission_restricted(self, cr, uid, ids, name, args, context=None):
99
 
        if not args:
100
 
            return []
101
 
        newargs = []
102
 
        for arg in args:
103
 
            if arg[1] != '=':
104
 
                raise osv.except_osv(_('Error'), _('Operator not supported on field intermission_restricted!'))
105
 
            if not isinstance(arg[2], (list, tuple)):
106
 
                raise osv.except_osv(_('Error'), _('Operand not supported on field intermission_restricted!'))
107
 
            if arg[2] and (arg[2][0] or arg[2][1]):
108
 
                try:
109
 
                    intermission = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution',
110
 
                            'analytic_account_project_intermission')[1]
111
 
                except ValueError:
112
 
                    pass
113
 
                if arg[2][2] == 'intermission':
114
 
                    newargs.append(('id', '=', intermission))
115
 
                else:
116
 
                    newargs.append(('id', '!=', intermission))
117
 
        return newargs
118
 
    
119
34
    _columns = {
120
 
        'name': fields.char('Name', size=128, required=True, translate=1),
 
35
        'name': fields.char('Name', size=128, required=True),
121
36
        'code': fields.char('Code', size=24),
122
37
        'type': fields.selection([('view','View'), ('normal','Normal')], 'Type', help='If you select the View Type, it means you won\'t allow to create journal entries using that account.'),
123
38
        'date_start': fields.date('Active from', required=True),
132
47
        'destination_ids': many2many_notlazy('account.account', 'account_destination_link', 'destination_id', 'account_id', 'Accounts'),
133
48
        'tuple_destination_account_ids': many2many_sorted('account.destination.link', 'funding_pool_associated_destinations', 'funding_pool_id', 'tuple_id', "Account/Destination"),
134
49
        'tuple_destination_summary': fields.one2many('account.destination.summary', 'funding_pool_id', 'Destination by accounts'),
135
 
        'filter_active': fields.function(_get_active, fnct_search=_search_filter_active, type="boolean", method=True, store=False, string="Show only active analytic accounts",),
136
 
        'hide_closed_fp': fields.function(_get_active, fnct_search=_search_closed_by_a_fp, type="boolean", method=True, store=False, string="Linked to a soft/hard closed contract?"),
137
 
        'intermission_restricted': fields.function(_get_fake, fnct_search=_search_intermission_restricted, type="boolean", method=True, store=False, string="Domain to restrict intermission cc"),
138
50
    }
139
51
 
140
52
    _defaults ={
141
53
        'date_start': lambda *a: (datetime.datetime.today() + relativedelta(months=-3)).strftime('%Y-%m-%d'),
142
54
        'for_fx_gain_loss': lambda *a: False,
143
55
    }
144
 
 
145
56
    def _check_unicity(self, cr, uid, ids, context=None):
146
57
        if not context:
147
58
            context = {}
223
134
    def _check_date(self, vals):
224
135
        if 'date' in vals and vals['date'] is not False:
225
136
            if vals['date'] <= datetime.date.today().strftime('%Y-%m-%d'):
226
 
                # validate the date (must be > today)
227
 
                raise osv.except_osv(_('Warning !'), _('You cannot set an inactivity date lower than tomorrow!'))
 
137
                 # validate the date (must be > today)
 
138
                 raise osv.except_osv(_('Warning !'), _('You cannot set an inactivity date lower than tomorrow!'))
228
139
            elif 'date_start' in vals and not vals['date_start'] < vals['date']:
229
140
                # validate that activation date 
230
141
                raise osv.except_osv(_('Warning !'), _('Activation date must be lower than inactivation date!'))
247
158
 
248
159
    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
249
160
        """
250
 
        FIXME: this method do others things that not have been documented. Please complete here what method do.
 
161
        No description found
251
162
        """
252
 
        if not context:
253
 
            context = {}
 
163
        if context and 'filter_inactive_accounts' in context and context['filter_inactive_accounts']:
 
164
            args.append(('date_start', '<=', datetime.date.today().strftime('%Y-%m-%d')))
 
165
            args.append('|')
 
166
            args.append(('date', '>', datetime.date.today().strftime('%Y-%m-%d')))
 
167
            args.append(('date', '=', False))
254
168
        if context and 'search_by_ids' in context and context['search_by_ids']:
255
169
            args2 = args[-1][2]
256
170
            del args[-1]
268
182
                    fp_ids.append(adl.get('funding_pool_ids'))
269
183
                fp_ids = flatten(fp_ids)
270
184
                args[i] = ('id', 'in', fp_ids)
271
 
        res = super(analytic_account, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
272
 
        return res
 
185
        return super(analytic_account, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
273
186
 
274
187
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
275
188
        if not context:
301
214
            args=[]
302
215
        if context is None:
303
216
            context={}
304
 
        if context.get('hide_inactive', False):
305
 
            args.append(('filter_active', '=', True))
306
217
        if context.get('current_model') == 'project.project':
307
218
            cr.execute("select analytic_account_id from project_project")
308
219
            project_ids = [x[0] for x in cr.fetchall()]