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

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
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 MSF, TeMPO Consulting.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import datetime
from osv import fields
from osv import osv
from tools.translate import _
from destination_tools import many2many_sorted
from destination_tools import many2many_notlazy
from tools.misc import flatten

class analytic_account(osv.osv):
    _name = "account.analytic.account"
    _inherit = "account.analytic.account"

    def _get_active(self, cr, uid, ids, field_name, args, context=None):
        '''
        If date out of date_start/date of given analytic account, then account is inactive.
        The comparison could be done via a date given in context.
        '''
        res = {}
        cmp_date = datetime.date.today().strftime('%Y-%m-%d')
        if context.get('date', False):
            cmp_date = context.get('date')
        for a in self.browse(cr, uid, ids):
            res[a.id] = True
            if a.date_start > cmp_date:
                res[a.id] = False
            if a.date and a.date <= cmp_date:
                res[a.id] = False
        return res

    def is_blocked_by_a_contract(self, cr, uid, ids):
        """
        Return ids (analytic accounts) that are blocked by a contract (just FP1)
        """
        # Some verifications
        if isinstance(ids, (int, long)):
            ids = [ids]
        # Prepare some values
        res = []
        for aa in self.browse(cr, uid, ids):
            # Only check funding pool accounts
            if aa.category != 'FUNDING':
                continue
            link_ids = self.pool.get('financing.contract.funding.pool.line').search(cr, uid, [('funding_pool_id', '=', aa.id)])
            format_ids = []
            for link in self.pool.get('financing.contract.funding.pool.line').browse(cr, uid, link_ids):
                if link.contract_id:
                    format_ids.append(link.contract_id.id)
            contract_ids = self.pool.get('financing.contract.contract').search(cr, uid, [('format_id', 'in', format_ids)])
            for contract in self.pool.get('financing.contract.contract').browse(cr, uid, contract_ids):
                if contract.state in ['soft_closed', 'hard_closed']:
                    res.append(aa.id)
        return res

    def _search_closed_by_a_fp(self, cr, uid, ids, name, args, context=None):
        """
        UTP-423: Do not display analytic accounts linked to a soft/hard closed contract.
        """
        res = [('id', 'not in', [])]
        if args and args[0] and len(args[0]) == 3:
            if args[0][1] != '=':
                raise osv.except_osv(_('Error'), _('Operator not supported yet!'))
            # Search all fp_ids from soft_closed contract
            sql="""SELECT a.id
                FROM account_analytic_account a, financing_contract_contract fcc, financing_contract_funding_pool_line fcfl
                WHERE fcfl.contract_id = fcc.id
                AND fcfl.funding_pool_id = a.id
                AND fcc.state in ('soft_closed', 'hard_closed');"""
            cr.execute(sql)
            sql_res = cr.fetchall()
            if sql_res:
                aa_ids = self.is_blocked_by_a_contract(cr, uid, [x and x[0] for x in sql_res])
                if aa_ids:
                    if isinstance(aa_ids, (int, long)):
                        aa_ids = [aa_ids]
                    res = [('id', 'not in', aa_ids)]
        return res

    _columns = {
        'destination_ids': many2many_notlazy('account.account', 'account_destination_link', 'destination_id', 'account_id', 'Accounts'),
        'tuple_destination_account_ids': many2many_sorted('account.destination.link', 'funding_pool_associated_destinations', 'funding_pool_id', 'tuple_id', "Account/Destination"),
        '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?"),
    }

    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
        """
        FIXME: this method do others things that not have been documented. Please complete here what method do.
        """
        if not context:
            context = {}
        if context and 'search_by_ids' in context and context['search_by_ids']:
            args2 = args[-1][2]
            del args[-1]
            ids = []
            for arg in args2:
                ids.append(arg[1])
            args.append(('id', 'in', ids))
        # UF-1713: Active/inactive functionnality was missing.
        if context and 'filter_inactive' in context and context['filter_inactive']:
            args.append(('filter_active', '=', context['filter_inactive']))
        # Tuple Account/Destination search
        for i, arg in enumerate(args):
            if arg[0] and arg[0] == 'tuple_destination':
                fp_ids = []
                destination_ids = self.pool.get('account.destination.link').search(cr, uid, [('account_id', '=', arg[2][0]), ('destination_id', '=', arg[2][1])])
                for adl in self.pool.get('account.destination.link').read(cr, uid, destination_ids, ['funding_pool_ids']):
                    fp_ids.append(adl.get('funding_pool_ids'))
                fp_ids = flatten(fp_ids)
                args[i] = ('id', 'in', fp_ids)
        return super(analytic_account, self).search(cr, uid, args, offset, limit, order, context=context, count=count)

analytic_account()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: