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

« back to all changes in this revision

Viewing changes to msf_instance/res_company.py

  • Committer: jf
  • Date: 2011-03-23 13:23:55 UTC
  • Revision ID: jf@tempo4-20110323132355-agyf1soy7m5ewatr
Initial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
3
 
##############################################################################
4
 
#
5
 
#    OpenERP, Open Source Management Solution
6
 
#    Copyright (C) 2012 TeMPO Consulting, MSF. All Rights Reserved
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU Affero General Public License as
10
 
#    published by the Free Software Foundation, either version 3 of the
11
 
#    License, or (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU Affero General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU Affero General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
##############################################################################
22
 
 
23
 
from osv import osv, fields
24
 
from tools.translate import _
25
 
import logging
26
 
import tools
27
 
from os import path
28
 
 
29
 
class res_company(osv.osv):
30
 
    _name = 'res.company'
31
 
    _inherit = 'res.company'
32
 
    
33
 
    def init(self, cr):
34
 
        """
35
 
            Create a instance for yml test
36
 
        """
37
 
        if hasattr(super(res_company, self), 'init'):
38
 
            super(res_company, self).init(cr)
39
 
 
40
 
        mod_obj = self.pool.get('ir.module.module')
41
 
        demo = False
42
 
        mod_id = mod_obj.search(cr, 1, [('name', '=', 'msf_instance')])
43
 
        if mod_id:
44
 
            demo = mod_obj.read(cr, 1, mod_id, ['demo'])[0]['demo']
45
 
        if demo:
46
 
            current_module = 'msf_instance'
47
 
            file_to_load = '%s/data/instance_data.xml' % (current_module, )
48
 
 
49
 
            logging.getLogger('init').info('HOOK: module msf_instance: loading %s' % file_to_load)
50
 
            file = tools.file_open(file_to_load)
51
 
            tools.convert_xml_import(cr, current_module, file, {}, mode='init', noupdate=False)
52
 
 
53
 
    _columns = {
54
 
        'instance_id': fields.many2one('msf.instance', string="Proprietary Instance", 
55
 
            help="Representation of the current instance"),
56
 
        'second_time': fields.boolean('Config. Wizard launched for the second time'),
57
 
        'company_second_time': fields.boolean('Company Config. Wizard launched for the second time'),
58
 
    }
59
 
    
60
 
    _defaults = {
61
 
        'second_time': lambda *a: False,
62
 
        'company_second_time': lambda *a: False,
63
 
    }
64
 
 
65
 
    def _refresh_objects(self, cr, uid, object_name, old_instance_id, new_instance_id, context=None):
66
 
        object_ids = self.pool.get(object_name).search(cr,
67
 
                                                       uid,
68
 
                                                       [('instance_id', '=', old_instance_id)],
69
 
                                                       context=context)
70
 
        self.pool.get(object_name).write(cr,
71
 
                                         uid,
72
 
                                         object_ids,
73
 
                                         {'instance_id': new_instance_id},
74
 
                                         context=context)
75
 
        return
76
 
    
77
 
    def copy_data(self, cr, uid, id, default=None, context=None):
78
 
        '''
79
 
        Erase some unused data copied from the original object, which sometime could become dangerous, as in UF-1631/1632, 
80
 
        when duplicating a new partner (by button duplicate), or company, it creates duplicated currencies
81
 
        '''
82
 
        if default is None:
83
 
            default = {}
84
 
        if context is None:
85
 
            context = {}
86
 
        fields_to_reset = ['currency_ids'] # reset this value, otherwise the content of the field triggers the creation of a new company
87
 
        to_del = []
88
 
        for ftr in fields_to_reset:
89
 
            if ftr not in default:
90
 
                to_del.append(ftr)
91
 
        res = super(res_company, self).copy_data(cr, uid, id, default=default, context=context)
92
 
        for ftd in to_del:
93
 
            if ftd in res:
94
 
                del(res[ftd])
95
 
        return res
96
 
    
97
 
    def write(self, cr, uid, ids, vals, context=None):
98
 
        if isinstance(ids, (int, long)):
99
 
            ids = [ids]
100
 
 
101
 
        if 'currency_id' in vals:
102
 
            for company in self.browse(cr, uid, ids, context=context):
103
 
                sale = self.pool.get('product.pricelist').search(cr,uid,[('currency_id','=',vals['currency_id']), ('type','=','sale')])
104
 
                purchase = self.pool.get('product.pricelist').search(cr,uid,[('currency_id','=',vals['currency_id']), ('type','=','purchase')])
105
 
                tmp_vals = {}
106
 
                if sale:
107
 
                    tmp_vals['property_product_pricelist'] = sale[0]
108
 
                if purchase:
109
 
                    tmp_vals['property_product_pricelist_purchase'] = purchase[0]
110
 
                if tmp_vals:
111
 
                    self.pool.get('res.partner').write(cr, uid, [company.partner_id.id], tmp_vals, context=context)
112
 
 
113
 
        instance_obj = self.pool.get('msf.instance')
114
 
        if 'instance_id' in vals:
115
 
            # only one company (unicity)
116
 
            if len(ids) != 1:
117
 
                raise osv.except_osv(_('Error'), _("Only one company per instance!") or '')
118
 
            company = self.browse(cr, uid, ids[0], context=context)
119
 
            if not company.instance_id:
120
 
                # An instance was not set; add DB name and activate it
121
 
                instance_obj.write(cr, uid, [vals['instance_id']], {'instance': cr.dbname,
122
 
                                                                    'state': 'active'}, context=context)
123
 
            elif company.instance_id.id != vals.get('instance_id'):
124
 
                # An instance was already set
125
 
                old_instance_id = company.instance_id.id
126
 
                # Deactivate the instance
127
 
                instance_obj.write(cr, uid, [old_instance_id], {'state': 'inactive'}, context=context)
128
 
                # add DB name and activate it
129
 
                instance_obj.write(cr, uid, [vals['instance_id']], {'instance': cr.dbname,
130
 
                                                                    'state': 'active'}, context=context)
131
 
                # refresh all objects
132
 
                for object in ['account.analytic.journal', 'account.journal', 'account.analytic.line', 'account.move', 'account.move.line', 'account.bank.statement']:
133
 
                    self._refresh_objects(cr, uid, object, old_instance_id, vals['instance_id'], context=context)
134
 
        return super(res_company, self).write(cr, uid, ids, vals, context=context)
135
 
                
136
 
res_company()
137
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: