2
# -*- coding: utf-8 -*-
3
##############################################################################
5
# OpenERP, Open Source Management Solution
6
# Copyright (C) 2012 TeMPO Consulting, MSF. All Rights Reserved
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.
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.
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/>.
21
##############################################################################
23
from osv import osv, fields
24
from tools.translate import _
29
class res_company(osv.osv):
31
_inherit = 'res.company'
35
Create a instance for yml test
37
if hasattr(super(res_company, self), 'init'):
38
super(res_company, self).init(cr)
40
mod_obj = self.pool.get('ir.module.module')
42
mod_id = mod_obj.search(cr, 1, [('name', '=', 'msf_instance')])
44
demo = mod_obj.read(cr, 1, mod_id, ['demo'])[0]['demo']
46
current_module = 'msf_instance'
47
file_to_load = '%s/data/instance_data.xml' % (current_module, )
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)
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'),
61
'second_time': lambda *a: False,
62
'company_second_time': lambda *a: False,
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,
68
[('instance_id', '=', old_instance_id)],
70
self.pool.get(object_name).write(cr,
73
{'instance_id': new_instance_id},
77
def copy_data(self, cr, uid, id, default=None, context=None):
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
86
fields_to_reset = ['currency_ids'] # reset this value, otherwise the content of the field triggers the creation of a new company
88
for ftr in fields_to_reset:
89
if ftr not in default:
91
res = super(res_company, self).copy_data(cr, uid, id, default=default, context=context)
97
def write(self, cr, uid, ids, vals, context=None):
98
if isinstance(ids, (int, long)):
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')])
107
tmp_vals['property_product_pricelist'] = sale[0]
109
tmp_vals['property_product_pricelist_purchase'] = purchase[0]
111
self.pool.get('res.partner').write(cr, uid, [company.partner_id.id], tmp_vals, context=context)
113
instance_obj = self.pool.get('msf.instance')
114
if 'instance_id' in vals:
115
# only one company (unicity)
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)
137
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: