~zaber/openobject-addons/stable_5.0-extra-addons

« back to all changes in this revision

Viewing changes to chricar_account_analytic/account_analytic.py

  • Committer: office at chricar
  • Date: 2010-04-16 07:19:18 UTC
  • Revision ID: office@chricar.at-20100416071918-oib34uibw05k14ez
added module chricar_account_analytic

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
#!/usr/bin/python
 
4
# -*- encoding: utf-8 -*-
 
5
##############################################
 
6
#
 
7
# ChriCar Beteiligungs- und Beratungs- GmbH
 
8
# Copyright (C) ChriCar Beteiligungs- und Beratungs- GmbH
 
9
# all rights reserved
 
10
# created 2009-08-18 23:44:30+02
 
11
#
 
12
# WARNING: This program as such is intended to be used by professional
 
13
# programmers who take the whole responsability of assessing all potential
 
14
# consequences resulting from its eventual inadequacies and bugs.
 
15
# End users who are looking for a ready-to-use solution with commercial
 
16
# garantees and support are strongly adviced to contract a Free Software
 
17
# Service Company.
 
18
#
 
19
# This program is Free Software; you can redistribute it and/or
 
20
# modify it under the terms of the GNU General Public License
 
21
# as published by the Free Software Foundation; either version 3
 
22
# of the License, or (at your option) any later version.
 
23
#
 
24
# This program is distributed in the hope that it will be useful,
 
25
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
26
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
27
# GNU General Public License for more details.
 
28
#
 
29
# You should have received a copy of the GNU General Public License
 
30
# along with this program; if not, see <http://www.gnu.org/licenses/> or
 
31
# write to the Free Software Foundation, Inc.,
 
32
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
33
#
 
34
###############################################
 
35
import time
 
36
from osv import fields,osv
 
37
import pooler
 
38
 
 
39
 
 
40
 
 
41
class account_account(osv.osv):
 
42
    _inherit = "account.account"
 
43
 
 
44
 
 
45
 
 
46
    _columns = {
 
47
     'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic Account', select=True,
 
48
          help="provide Analytic Account only for default and fixed"),
 
49
     'account_analytic_usage': fields.selection([
 
50
            ('fixed','Fixed'),
 
51
            ('default','Default'),
 
52
            ('mandatory','Mandatory'),
 
53
            ('optional','Optional'),
 
54
            ('none','Not allowed'),
 
55
            ], 'Analytic Account Usage', help="""This type is used to differenciate types with
 
56
special effects in Open ERP:
 
57
for P&L accounts
 
58
* fixed: will be used and user can not change it
 
59
* default: will be proposed and user can change it
 
60
* mandatory: user must supply analytic account
 
61
for balance accounts
 
62
* mandatory: user must supply analytic account
 
63
* optional: user may supply analytic account
 
64
* none: no analytic account allowed""",
 
65
            ) ,
 
66
 
 
67
    }
 
68
 
 
69
    def _analytic_account_type(self, cr, uid, context={}):
 
70
        account = self.pool.get('account.account').browse(cr, uid, uid, context=context)
 
71
        if account.account_analytic_usage:
 
72
            return
 
73
        if account.user_type.close_method == 'none':
 
74
            # P&L
 
75
            return 'mandatory'
 
76
        else:
 
77
            return 'none'
 
78
            
 
79
    _defaults = {
 
80
      'account_analytic_usage' : _analytic_account_type,
 
81
    }
 
82
 
 
83
 
 
84
    def _check_analytic_account_usage(self, cr, uid, ids):
 
85
         account = self.browse(cr, uid, ids[0])
 
86
         if account.user_type.close_method == 'none' and account.account_analytic_usage not in ('mandatory','default','fixed'):
 
87
                return False
 
88
         # FIXME - do we need the following check? -
 
89
         #if account.user_type.close_method != 'none' and account.account_analytic_usage not in ('mandatory','optional','none'):
 
90
         #       return False
 
91
         return True
 
92
 
 
93
    def _check_analytic_account_id(self, cr, uid, ids):
 
94
         account = self.browse(cr, uid, ids[0])
 
95
         if not account.account_analytic_id and account.account_analytic_usage in ('default','fixed'):
 
96
                return False
 
97
         # FIXME - do we need the following check? -
 
98
         if account.account_analytic_id and account.account_analytic_usage not in ('default','fixed'):
 
99
                return False
 
100
         return True
 
101
 
 
102
 
 
103
    _constraints = [
 
104
        (_check_analytic_account_usage,
 
105
            'You must assign a correct analytic account usage', ['account_analytic_usage']),
 
106
        (_check_analytic_account_id,
 
107
            'You must define an analytic account for fixed and default, else nothing', ['account_analytic_id']),
 
108
 
 
109
        ]
 
110
 
 
111
 
 
112
 
 
113
    def init(self, cr):
 
114
      # We set some resonable values
 
115
      # P & L accounts - mandatory
 
116
      # other no analytic account
 
117
      cr.execute("""
 
118
         update account_account
 
119
           set account_analytic_usage = (
 
120
                select distinct
 
121
                   case when close_method = 'none' and code != 'view' then 'mandatory' when code = 'view'  then null else 'none' end
 
122
                  from account_account_type aat
 
123
                 where aat.id = user_type)
 
124
          where account_analytic_usage is null;
 
125
      """)
 
126
 
 
127
 
 
128
account_account()
 
129
 
 
130
 
 
131
class account_move_line(osv.osv):
 
132
    _inherit = "account.move.line"
 
133
 
 
134
    def _check_analytic_account(self, cr, uid, ids):
 
135
        for move in self.browse(cr, uid, ids):
 
136
            # FIXME - does not write the found value
 
137
            #if not move.analytic_account_id and move.account_id.account_analytic_usage in ['default','fixed']:
 
138
            #    move.account_id.analytic_account_id = move.account_id.account_analytic_id
 
139
            #    return True
 
140
            if not move.analytic_account_id and move.account_id.account_analytic_usage in ['mandatory','default','fixed']:
 
141
                return False
 
142
            if move.analytic_account_id and move.account_id.account_analytic_usage in ['none']:
 
143
                return False
 
144
        return True
 
145
 
 
146
    _constraints = [
 
147
        (_check_analytic_account,
 
148
            'You must assign an analytic account using this account', ['analytic_account_id'] ),
 
149
        ]
 
150
 
 
151
account_move_line()
 
152
 
 
153
 
 
154
class account_bank_statement_line(osv.osv):
 
155
    _inherit = "account.bank.statement.line"
 
156
 
 
157
    def _check_analytic_account(self, cr, uid, ids):
 
158
        for move in self.browse(cr, uid, ids):
 
159
            if not move.account_analytic_id and move.account_id.account_analytic_usage in ['default','fixed']:
 
160
                move.account_analytic_id = move.account_id.account_analytic_id
 
161
                return True
 
162
            if not move.account_analytic_id and move.account_id.account_analytic_usage in ['mandatory','default','fixed']:
 
163
                return False
 
164
            if move.account_analytic_id and move.account_id.account_analytic_usage in ['none']:
 
165
                return False
 
166
        return True
 
167
 
 
168
    _constraints = [
 
169
        (_check_analytic_account,
 
170
            'You must assign an analytic account using this account', ['account_analytic_id'] ),
 
171
        ]
 
172
 
 
173
account_bank_statement_line()
 
174
 
 
175
class account_invoice_line(osv.osv):
 
176
    _inherit = "account.invoice.line"
 
177
 
 
178
 
 
179
    def _check_analytic_account(self, cr, uid, ids):
 
180
        for move in self.browse(cr, uid, ids):
 
181
            # FIXME - does not write the found value
 
182
            #if not move.account_analytic_id and move.account_id.account_analytic_usage in ['default','fixed']:
 
183
            #    move.account_analytic_id = move.account_id.account_analytic_id
 
184
            #    res = {'value': {'account_analytic_id' : move.account_id.account_analytic_id.id }}
 
185
            #    return res
 
186
            if not move.account_analytic_id and move.account_id.account_analytic_usage in ['mandatory','default','fixed']:
 
187
                return False
 
188
            if move.account_analytic_id and move.account_id.account_analytic_usage in ['none']:
 
189
                return False
 
190
        return True
 
191
 
 
192
    _constraints = [
 
193
        (_check_analytic_account,
 
194
            'You must assign an analytic account using this account', ['account_analytic_id'] ),
 
195
        ]
 
196
 
 
197
account_invoice_line()