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

« back to all changes in this revision

Viewing changes to personal_base/account.py

  • Committer: pso (tiny)
  • Date: 2009-02-10 13:58:54 UTC
  • mto: This revision was merged to the branch mainline in revision 3554.
  • Revision ID: pso@tinyerp.com-20090210135854-egbyysq88p1v41w8
personal_base:made compatible to 5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2008 Sandas. (http://www.sandas.eu) All Rights Reserved.
 
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
#
 
27
##############################################################################
 
28
 
 
29
from osv import fields, osv
 
30
from base import base_delete_unit_test_records, base_get_default_currency
 
31
import datetime
 
32
 
 
33
class personal_account_type(osv.osv):
 
34
    _name = "personal.base.account.type"
 
35
    _description = "Account Type"
 
36
    _order = "name"
 
37
    _columns = {
 
38
        'name': fields.char('Acc. Type Name', size=64, required=True, translate=True),
 
39
        'sign': fields.selection([(-1, 'Negative'), (1, 'Positive')], 'Sign', required=True),
 
40
    }
 
41
    _sql_constraints = [
 
42
        ('name_uniq', 'unique (name)', 'Name must be unique !')
 
43
    ]
 
44
    
 
45
    #demo data
 
46
    #EQU_ID = 1
 
47
    #ASS_ID = 2
 
48
    #LIA_ID = 3
 
49
    #INC_ID = 4
 
50
    #EXP_ID = 5
 
51
    
 
52
    def try_create_demo(self, cr, uid):
 
53
        pass
 
54
        #if self.search(cr, uid, []) == []:
 
55
        #    self.create(cr, uid, {'name': 'Begin balance', 'sign': 1})
 
56
        #    self.create(cr, uid, {'name': 'Asset', 'sign': 1})
 
57
        #    self.create(cr, uid, {'name': 'Liability', 'sign': -1})
 
58
        #    self.create(cr, uid, {'name': 'Income', 'sign': -1})
 
59
        #    self.create(cr, uid, {'name': 'Expense', 'sign': 1})
 
60
            #self.pool.get('ir.model.data')._update(cr, uid, self._name, 'personal_base', {'name': 'Begin balance'}, 'TYPE_EQU_ID')
 
61
        
 
62
personal_account_type()
 
63
 
 
64
class personal_account(osv.osv):
 
65
    _name = "personal.base.account"
 
66
    _description = "Account"
 
67
    _order = "name"
 
68
    
 
69
#    def _account_type_code_get(self, cr, uid, context={}):
 
70
#        acc_type_obj = self.pool.get('personal.base.account.type')
 
71
#        ids = acc_type_obj.search(cr, uid, [])
 
72
#        res = acc_type_obj.read(cr, uid, ids, ['id', 'name'], context)
 
73
#        return [(r['id'], r['name']) for r in res]
 
74
 
 
75
    def _get_balance_fnc(self, cr, uid, ids, prop, unknow_none, context):
 
76
        currency_pool = self.pool.get('res.currency')
 
77
        res = {}
 
78
        cr.execute(("SELECT a.id, " \
 
79
                    "SUM(COALESCE(l.amount_base, 0)) " \
 
80
                "FROM personal_base_account a " \
 
81
                    "LEFT JOIN personal_base_account_entry_line l " \
 
82
                    "ON (a.id=l.account_id) " \
 
83
                "WHERE l.state = 'confirmed' " \
 
84
                    "AND a.id IN (%s) " \
 
85
                "GROUP BY a.id") % (','.join(map(str,ids)),))
 
86
        for id in ids:
 
87
            res[id] = 0
 
88
        for account_id, sum in cr.fetchall():
 
89
            account = self.browse(cr, uid, account_id)
 
90
            res[account_id] = currency_pool.round(cr, uid, account.currency_id, sum * account.type_id.sign)
 
91
        
 
92
        #line_pool = self.pool.get('personal.base.account.entry.line')
 
93
        #for id in ids:
 
94
        #    account = self.browse(cr, uid, id)
 
95
        #    res[id] = 0.0
 
96
        #    for search_line_id in line_pool.search(cr, uid, 
 
97
        #            [('account_id', '=', id),
 
98
        #             ('state', '=', 'confirmed'),]
 
99
        #    ):
 
100
        #        line = line_pool.browse(cr, uid, search_line_id)
 
101
        #        res[id] = res[id] + (line.amount_base * account.type_id.sign)
 
102
        return res
 
103
 
 
104
    _columns = {
 
105
        'user_id': fields.many2one('res.users', 'User', required=True),
 
106
        'name': fields.char('Name', size=128, required=True, select=True),
 
107
        'currency_id': fields.many2one('res.currency', 'Currency', required=True),
 
108
        'type_id': fields.many2one('personal.base.account.type', 'Account Type', required=True),
 
109
        'parent_id': fields.many2one('personal.base.account', 'Parent Code', select=True),
 
110
        'child_ids': fields.one2many('personal.base.account', 'parent_id', 'Childs Codes'),
 
111
        'note': fields.text('Note'),
 
112
        'balance': fields.function(_get_balance_fnc, method=True, type="float", digits=(10,2), string='Balance'),
 
113
        
 
114
        #fields for unit_test
 
115
        'unit_test': fields.boolean(string='unit_test')
 
116
    }
 
117
 
 
118
    _defaults = {
 
119
        'user_id': lambda self, cr, uid, context: uid,
 
120
        'currency_id': lambda self, cr, uid, context: base_get_default_currency(cr, uid, context),
 
121
        'unit_test': lambda *a: False,
 
122
    }
 
123
    
 
124
    def search(self, cr, uid, args, offset=0, limit=2000, order=None, context=None, count=False):
 
125
        args.append(('user_id','=',uid))
 
126
        return osv.orm.orm.search(self, cr, uid, args, offset, limit, order,
 
127
                context=context)
 
128
    
 
129
    #for unit tests
 
130
    def try_create_litas(self, cr, uid):
 
131
        cur_pool = self.pool.get('res.currency')
 
132
        cur_rate_pool = self.pool.get('res.currency.rate')
 
133
        if cur_pool.search(cr, uid, [('code','=','LTL')]) == []:
 
134
            cur_id = cur_pool.create(cr, uid, {'code': 'LTL', 'name': 'Litas', 
 
135
                'rounding': 0.01, 'accuracy': 4})
 
136
            cur_rate_pool.create(cr, uid, {'currency_id': cur_id, 'rate': 3.4528, 
 
137
                'name': datetime.date(2002,2,2)})
 
138
    
 
139
    def delete_unit_test_records(self, cr, uid):
 
140
        base_delete_unit_test_records(self, cr, uid)
 
141
            
 
142
personal_account()