1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 TeMPO Consulting, MSF
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
23
from osv import fields
24
from tools.translate import _
29
class currency_setup(osv.osv_memory):
30
_name = 'currency.setup'
31
_inherit = 'res.config'
34
'functional_id': fields.selection([('eur', 'EUR'), ('chf', 'CHF')], string='Functional currency',
36
'esc_id': fields.many2one('res.currency', string="ESC Currency", readonly=True),
37
'section_id': fields.selection([('eur', 'EUR'), ('chf', 'CHF')], string='Section currency',
39
'second_time': fields.boolean('Config. Wizard launched for the second time'),
43
'second_time': lambda *a: False,
46
def functional_on_change(self, cr, uid, ids, currency_id, context=None):
47
return {'value': {'section_id': currency_id}}
49
def default_get(self, cr, uid, fields, context=None):
53
Display the default value for delivery process
55
res = super(currency_setup, self).default_get(cr, uid, fields, context=context)
57
company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
58
esc_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'EUR')[1]
60
if company_id.currency_id.id == esc_id:
61
res['functional_id'] = 'eur'
63
res['functional_id'] = 'chf'
64
res['second_time'] = company_id and company_id.second_time or False
65
res['esc_id'] = esc_id
66
res['section_id'] = res['functional_id']
69
def execute(self, cr, uid, ids, context=None):
72
context['from_setup'] = True
74
Fill the delivery process field in company
76
assert len(ids) == 1, "We should only get one object from the form"
77
payload = self.browse(cr, uid, ids[0], context=context)
79
if payload.functional_id == 'eur':
80
cur_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'EUR')[1]
82
cur_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'CHF')[1]
84
if not self.pool.get('res.currency').read(cr, uid, cur_id, ['active'], context=context)['active']:
85
self.pool.get('res.currency').write(cr, uid, cur_id, {'active': True}, context=context)
87
company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
89
if not payload.second_time:
90
self.pool.get('res.company').write(cr, uid, [company_id], {'currency_id': cur_id, 'second_time': True}, context=context)
92
self.pool.get('res.company').write(cr, uid, [company_id], {'currency_id': cur_id,}, context=context)
94
# Search the sale and purchase pricelists for this currency
95
sale_price_id = self.pool.get('product.pricelist').search(cr, uid, [('type', '=', 'sale'), ('currency_id', '=', cur_id)])
97
raise osv.except_osv(_('Error'), _('No pricelist found for this currency !'))
99
purchase_price_id = self.pool.get('product.pricelist').search(cr, uid, [('type', '=', 'purchase'), ('currency_id', '=', cur_id)])
100
if not purchase_price_id:
101
raise osv.except_osv(_('Error'), _('No pricelist found for this currency !'))
103
# Change the currencies on all internal partners
104
partner_ids = self.pool.get('res.partner').search(cr, uid, [('partner_type', '=', 'internal')])
105
self.pool.get('res.partner').write(cr, uid, partner_ids, {'property_product_pricelist': sale_price_id[0],
106
'property_product_pricelist_purchase': purchase_price_id[0]})
108
# Change the default value of the ir.property pricelist fields
109
sale_price_property_ids = self.pool.get('ir.property').search(cr, uid, [('res_id', '=', False), ('name', '=', 'property_product_pricelist')])
110
self.pool.get('ir.property').write(cr, uid, sale_price_property_ids, {'value': sale_price_id[0]})
111
purchase_price_property_ids = self.pool.get('ir.property').search(cr, uid, [('res_id', '=', False), ('name', '=', 'property_product_pricelist_purchase')])
112
self.pool.get('ir.property').write(cr, uid, purchase_price_property_ids, {'value': purchase_price_id[0]})
115
# Modify the currency on some already created objects
117
price_type_ids = self.pool.get('product.price.type').search(cr, uid, [('currency_id', '=', 1)])
118
self.pool.get('product.price.type').write(cr, uid, price_type_ids, {'currency_id': cur_id})
120
# account.analytic.account
121
analytic_ids = self.pool.get('account.analytic.account').search(cr, uid, [('currency_id', '=', 1)])
122
# use a for to avoid a recursive account error
123
for analytic_id in analytic_ids:
124
self.pool.get('account.analytic.account').write(cr, uid, [analytic_id], {'currency_id': cur_id})
127
# UF-1766 : Pass out the OpenObject framework to gain time on currency change with a big amount of products
128
cr.execute('UPDATE product_product SET currency_id = %s, field_currency_id = %s', (cur_id, cur_id))
129
# product_ids = self.pool.get('product.product').search(cr, uid, [('currency_id', '=', 1)])
130
# product2_ids = self.pool.get('product.product').search(cr, uid, [('field_currency_id', '=', 1)])
131
# self.pool.get('product.product').write(cr, uid, product_ids, {'currency_id': cur_id})
132
# self.pool.get('product.product').write(cr, uid, product2_ids, {'field_currency_id': cur_id})
135
model_ids = self.pool.get('account.model').search(cr, uid, [('currency_id', '=', 1)])
136
self.pool.get('account.model').write(cr, uid, model_ids, {'currency_id': cur_id})
139
mcdb_ids = self.pool.get('account.mcdb').search(cr, uid, [('currency_id', '=', 1)])
140
self.pool.get('account.mcdb').write(cr, uid, mcdb_ids, {'currency_id': cur_id})