~openerp-commiter/openobject-addons/trunk-extra-addons

3758.1.4 by nicolas.bessi at camptocamp
adding ports or rewriting of some c2c modules
1
# -*- coding: utf-8 -*- 
2
##############################################################################
3
#
3907.1.4 by PSO(OpenERP)
Changed licence terms
4
# Copyright (c) Camptocamp SA - Joel Grand-Guillaume porté par Nicolas Bessi 
3758.1.4 by nicolas.bessi at camptocamp
adding ports or rewriting of some c2c modules
5
#
6
# WARNING: This program as such is intended to be used by professional
7
# programmers who take the whole responsability of assessing all potential
8
# consequences resulting from its eventual inadequacies and bugs
9
# End users who are looking for a ready-to-use solution with commercial
10
# garantees and support are strongly adviced to contract a Free Software
11
# Service Company
12
#
13
# This program is Free Software; you can redistribute it and/or
14
# modify it under the terms of the GNU General Public License
15
# as published by the Free Software Foundation; either version 2
16
# of the License, or (at your option) any later version.
17
#
18
# This program is distributed in the hope that it will be useful,
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
# GNU General Public License for more details.
22
#
23
# You should have received a copy of the GNU General Public License
24
# along with this program; if not, write to the Free Software
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
import time
27
from mx import DateTime
28
import netsvc
29
from osv import fields
30
from osv import osv
31
32
33
####################################################################################
34
#  employee activity
35
####################################################################################
36
class project_activity_al(osv.osv):
37
    """Class tha inhertis osv.osv and add activities to account analytic lines"""
38
    _name = "project.activity_al"
39
    _description = "Project activity"
40
    
41
    
42
    ## @param self The object pointer.
43
    ## @param cr a psycopg cursor.
44
    ## @param uid res.user.id that is currently loged
45
    ## @param account a browse record of an account
46
    ## @return a browse reccod list of the first parent that have an activites
47
    def _get_first_AA_wich_have_activity(self,cr,uid,account):
48
        """Return browse record list of activities
49
           of the account which have an activity set 
50
           (goes bottom up, child, then parent)
51
        """
52
        if account.activity_ids:
53
            return account
54
        else:
55
            if account.parent_id:
56
                return self._get_first_AA_wich_have_activity(cr,uid,account.parent_id)
57
            else:
58
                return False
59
60
        
61
    ## @param self The object pointer.
62
    ## @param cr a psycopg cursor.
63
    ## @param uid res.user.id that is currently loged
64
    ## @param name osv._obj name of the serached object
65
    ## @param args an arbitrary list that contains search criterium
66
    ## @param operator search operator 
67
    ## @param context an arbitrary context
68
    ## @param limit int of the search limit
69
    ## @return the result of name get
70
    def name_search(self, cr, uid, name, args=None, 
71
        operator='ilike', context=None, limit=80):
72
        """ Ovveride of osv.osv name serach function that do the search
73
            on the name of the activites """
74
        if not args:
75
            args=[]
76
        acc_ids=[]
77
        if not context:
78
            context={}
79
        if context.get('account_id',False):
80
            aa_obj = self.pool.get('account.analytic.account')
81
            account_id = aa_obj.browse(cr,uid,context.get('account_id',False))
82
            #take the account wich have activity_ids
83
            acc_who_matters=self._get_first_AA_wich_have_activity(
84
                                                                cr,
85
                                                                uid,
86
                                                                account_id
87
                                                            )
88
            if acc_who_matters:
89
                for i in acc_who_matters.activity_ids:
90
                    acc_ids.append(i.id)
91
        
92
        account = self.search(
93
                                cr, 
94
                                uid, 
95
                                    [
96
                                        ('code', '=', name),
97
                                        ('id','in',acc_ids)
98
                                    ]+args, 
99
                                limit=limit, 
100
                                context=context
101
                            )
102
        if not account:
103
            account = self.search(
104
                                    cr, 
105
                                    uid, 
106
                                    [
107
                                        ('name', 'ilike', '%%%s%%' % name),
108
                                        ('id','in',acc_ids)
109
                                    ]+args, 
110
                                    limit=limit, 
111
                                    context=context
112
                                )
113
        if not account:
114
            account = self.search(
115
                                    cr, 
116
                                    uid, 
117
                                    [
118
                                        ('id','in',acc_ids)
119
                                    ]+args, 
120
                                    limit=limit, 
121
                                    context=context
122
                                 )
123
        # For searching in parent also
124
        if not account:
125
            account = self.search(
126
                                    cr, 
127
                                    uid, 
128
                                    [
129
                                        ('name', 'ilike', '%%%s%%' % name)
130
                                    ]+args, 
131
                                    limit=limit, 
132
                                    context=context
133
                                 )
134
            newacc = account
135
            while newacc:
136
                newacc = self.search(
137
                                        cr, 
138
                                        uid, 
139
                                        [
140
                                            ('parent_id', 'in', newacc)
141
                                        ]+args, 
142
                                        limit=limit, 
143
                                        context=context
144
                                    )
145
                account+=newacc
146
            
147
        return self.name_get(cr, uid, account, context=context)
148
        
149
    _columns = {
150
        ## activity code
151
        'code': fields.char('Code', required=True, size=64),
152
        ## name of the code
153
        'name': fields.char('Activity', required=True, size=64,translate=True),
154
        ## parent activity
155
        'parent_id' : fields.many2one('project.activity_al', 'Parent activity'),
156
        ## link to account.analytic account
157
        'project_ids': fields.many2many(
158
                                            'account.analytic.account',
159
                                            'proj_activity_analytic_rel', 
160
                                            'activity_id', 'analytic_id', 
161
                                            'Concerned project'
162
                                         ),
163
        ## link to the children activites                                 
164
        'child_ids': fields.one2many(
165
                                        'project.activity_al', 
166
                                        'parent_id', 
167
                                        'Childs Activity'
168
                                    ),
169
    }
170
    
171
project_activity_al()
172
173
####################################################################################
174
#  Analytic account
175
####################################################################################
176
177
class account_analytic_account(osv.osv):
178
    """ Classe that override account.analytic.account 
179
    in order to add new attributes"""
180
    _name = 'account.analytic.account'
181
    _inherit = "account.analytic.account"
182
    
183
    _columns = {
184
        ## Link activity and project 
185
        'activity_ids': fields.many2many(
186
                                            'project.activity_al',
187
                                            'proj_activity_analytic_rel',
188
                                            'analytic_id',
189
                                            'activity_id',
190
                                            'Related activities'
191
                                        ),
192
193
    }
194
195
account_analytic_account()
196
197
####################################################################################
198
#  Account analytic line
199
####################################################################################
200
class account_analytic_line(osv.osv):
201
    """ Classe that override account.analytic.line 
202
    in order to add new attributes """
203
    _name = "account.analytic.line"
204
    _inherit = "account.analytic.line"
205
206
    _columns = {
207
        'activity' : fields.many2one('project.activity_al', 'Activity'),
208
    }
209
account_analytic_line()