~taktik/openobject-addons/hui-extra-6.1

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
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    
#    Copyright (c) 2012 Noviat nv/sa (www.noviat.be). All rights reserved.
# 
#    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 re
from osv import fields, osv
from tools.translate import _
import logging
_logger = logging.getLogger(__name__)

class be_legal_financial_reportscheme(osv.osv):
    _name = 'be.legal.financial.reportscheme'
    _description = 'Belgian Legal Financial Report Scheme (Full)'
    _rec_name = 'account_group'
    _order = 'account_group'
    _columns = {
        'account_group': fields.char('Group', size=4, help='General Account Starting Digits'),
        'report_id': fields.many2one('account.financial.report', 'Report Entry', ondelete='cascade'),
        'account_ids': fields.related('report_id','account_ids', type='one2many', relation='account.account', string='Accounts', readonly=True),
    }
    _sql_constraints = [
        ('group_uniq', 'unique (account_group)', 'The General Account Group must be unique !')
    ]    
be_legal_financial_reportscheme()

class account_financial_report(osv.osv):
    _inherit = 'account.financial.report'
    _columns = {    
        'code': fields.char('Code', size=16),
        'invisible': fields.boolean('Invisible', help="Hide this entry from the printed report."),
    }
       
    def _get_children_by_order(self, cr, uid, ids, context=None):
        res = []
        if context.get('get_children_by_sequence'):
            res = self.search(cr, uid, [('id', 'child_of', ids[0]), ('invisible', '=', 0)], order='sequence ASC', context=context)
        else:
            for id in ids:
                res.append(id)
                ids2 = self.search(cr, uid, [('parent_id', '=', id), ('invisible', '=', 0)], order='sequence ASC', context=context)
                res += self._get_children_by_order(cr, uid, ids2, context=context)
        return res
       
    def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
        #_logger.warn('read, context = %s', context)
        res = super(account_financial_report, self).read(cr, user, ids, fields, context, load)
        context = context or {}
        if 'name' in fields:
            for entry in res:
                if entry.get('code') and context.get('code_print'):
                    entry['name'] = entry['name'] + ' - (' + entry['code'] + ')' 
        return res
    
account_financial_report()

class account_account(osv.osv):
    _inherit = 'account.account'

    def create(self, cr, uid, vals, context=None):
        acc_id = super(account_account, self).create(cr, uid, vals, context=context) 
        #_logger.warn('create, vals = %s, context = %s', vals, context)
        scheme_obj = self.pool.get('be.legal.financial.reportscheme')
        scheme_table = scheme_obj.read(cr, uid, scheme_obj.search(cr, uid, []), context=context)
        be_report_ids = [x['report_id'][0] for x in scheme_table]
        acc_code = vals['code']
        account = self.browse(cr, uid, acc_id)
        if account.type not in ['view', 'consolidation'] and account.company_id.country_id.code == 'BE':
            be_report_entries =  filter(lambda x: acc_code[0:len(x['account_group'])] == x['account_group'], scheme_table)
            if be_report_entries:
                if len(be_report_entries) > 1:
                    raise osv.except_osv(_('Configuration Error !'), _('Configuration Error in the Belgian Legal Financial Report Scheme.'))
                be_report_id = be_report_entries[0]['report_id'][0]
                self.write(cr, uid, account.id, {'financial_report_ids': [(4, be_report_id)]})
        return acc_id

    def write(self, cr, uid, ids, vals, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        if 'code' in vals.keys() or 'type' in vals.keys():
            scheme_obj = self.pool.get('be.legal.financial.reportscheme')
            scheme_table = scheme_obj.read(cr, uid, scheme_obj.search(cr, uid, []), context=context)
            be_report_ids = [x['report_id'][0] for x in scheme_table]
            acc_code = vals.get('code')
            acc_type = vals.get('type')
            for account in self.browse(cr, uid, ids):
                updated = False
                if account.company_id.country_id.code == 'BE':
                    acc_code = acc_code or account.code
                    acc_type = acc_type or account.type
                    be_report_entries =  filter(lambda x: acc_code[0:len(x['account_group'])] == x['account_group'], scheme_table)
                    if len(be_report_entries) > 1:
                        raise osv.except_osv(_('Configuration Error !'), _('Configuration Error in the Belgian Legal Financial Report Scheme.'))                       
                    be_report_id = be_report_entries and be_report_entries[0]['report_id'][0]
                    for fin_report in account.financial_report_ids:
                        if fin_report.id in be_report_ids:
                            if acc_type not in ['view', 'consolidation'] and fin_report.id == be_report_id:
                                updated = True
                            else:
                                vals.update({'financial_report_ids': [(3, fin_report.id)]})
                                updated = True
                    if be_report_id and acc_type not in ['view', 'consolidation'] and not updated:
                        vals.update({'financial_report_ids': [(4, be_report_id)]})
        return super(account_account, self).write(cr, uid, ids, vals, context=context)