66
66
table_in_args = True
67
67
if not table_in_args:
68
68
args.insert(0, ('currency_table_id', '=', False))
69
return super(res_currency, self).search(cr, uid, args, offset, limit,
70
order, context, count=count)
69
return super(res_currency, self).search(cr, uid, args, offset, limit, order, context, count=count)
72
71
def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, context=None):
73
72
if context is None:
75
74
if context.get('currency_table_id', False):
76
75
# A currency table is set, retrieve the correct currency ids
77
# UTP-894: use currency table rate or default rate if currency not in currency table
78
76
new_from_currency_id = self._get_table_currency(cr, uid, from_currency_id, context['currency_table_id'], context=context)
79
if new_from_currency_id:
80
from_currency_id = new_from_currency_id
81
77
new_to_currency_id = self._get_table_currency(cr, uid, to_currency_id, context['currency_table_id'], context=context)
82
if new_to_currency_id:
83
to_currency_id = new_to_currency_id
78
# only use new currencies if both are defined in the table
79
if new_from_currency_id and new_to_currency_id:
80
return super(res_currency, self).compute(cr, uid, new_from_currency_id, new_to_currency_id, from_amount, round, context=context)
81
# Fallback case if no currency table or one currency not defined in the table
84
82
return super(res_currency, self).compute(cr, uid, from_currency_id, to_currency_id, from_amount, round, context=context)
86
84
def create_associated_pricelist(self, cr, uid, currency_id, context=None):
88
86
Create purchase and sale pricelists according to the currency
90
88
pricelist_obj = self.pool.get('product.pricelist')
91
89
version_obj = self.pool.get('product.pricelist.version')
92
90
item_obj = self.pool.get('product.pricelist.item')
94
92
company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
95
93
currency = self.browse(cr, uid, currency_id, context=context)
97
95
# Create the sale pricelist
98
sale_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id,
99
'name': currency.name,
96
sale_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id,
97
'name': currency.name,
100
98
'active': currency.active,
102
100
'company_id': company_id}, context=context)
104
102
# Create the sale pricelist version
105
103
sale_version_id = version_obj.create(cr, uid, {'pricelist_id': sale_price_id,
106
104
'name': 'Default Sale %s Version' % currency.name,
107
105
'active': currency.active}, context=context)
109
107
# Create the sale pricelist item
110
108
item_obj.create(cr, uid, {'price_version_id': sale_version_id,
111
109
'name': 'Default Sale %s Line' % currency.name,
113
111
'min_quantity': 0.00}, context=context)
115
113
# Create the purchase pricelist
116
purchase_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id,
117
'name': currency.name,
114
purchase_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id,
115
'name': currency.name,
118
116
'active': currency.active,
119
117
'type': 'purchase',
120
118
'company_id': company_id}, context=context)
122
120
# Create the sale pricelist version
123
121
purchase_version_id = version_obj.create(cr, uid, {'pricelist_id': purchase_price_id,
124
122
'name': 'Default Purchase %s Version' % currency.name,
125
123
'active': currency.active}, context=context)
127
125
# Create the sale pricelist item
128
126
item_obj.create(cr, uid, {'price_version_id': purchase_version_id,
129
127
'name': 'Default Purchase %s Line' % currency.name,
131
129
'min_quantity': 0.00}, context=context)
133
131
return [sale_price_id, purchase_price_id]
135
133
def create(self, cr, uid, values, context=None):
137
135
Create automatically a purchase and a sales pricelist on
138
136
currency creation
140
138
res = super(res_currency, self).create(cr, uid, values, context=context)
142
# Create the corresponding pricelists (only for non currency that have a currency_table)
143
if not values.get('currency_table_id', False):
144
self.create_associated_pricelist(cr, uid, res, context=context)
146
# Check if currencies has no associated pricelists
147
cr.execute('SELECT id FROM res_currency WHERE id NOT IN (SELECT currency_id FROM product_pricelist) AND currency_table_id IS NULL')
148
curr_ids = cr.fetchall()
149
for cur_id in curr_ids:
150
self.create_associated_pricelist(cr, uid, cur_id[0], context=context)
140
# Create the corresponding pricelists
141
self.create_associated_pricelist(cr, uid, res, context=context)
143
# Check if currencies has no associated pricelists
144
cr.execute('SELECT id FROM res_currency WHERE id NOT IN (SELECT currency_id FROM product_pricelist)')
145
curr_ids = cr.fetchall()
146
for cur_id in curr_ids:
147
self.create_associated_pricelist(cr, uid, cur_id[0], context=context)
154
151
def write(self, cr, uid, ids, values, context=None):
156
153
Active/De-active pricelists according to activation/de-activation of the currency
158
155
pricelist_obj = self.pool.get('product.pricelist')
159
156
version_obj = self.pool.get('product.pricelist.version')
161
158
if isinstance(ids, (int, long)):
164
if 'active' in values and not values.get('currency_table_id', False):
161
if 'active' in values:
165
162
if values['active'] == False:
166
163
self.check_in_use(cr, uid, ids, 'de-activate', context=context)
167
164
# Get all pricelists and versions for the given currency
168
165
pricelist_ids = pricelist_obj.search(cr, uid, [('currency_id', 'in', ids), ('active', 'in', ['t', 'f'])], context=context)
169
166
if not pricelist_ids:
170
to_create = self.search(cr, uid, [('currency_table_id', '=', False), ('id', 'in', ids)], context=context)
171
for cur_id in to_create:
172
168
pricelist_ids = self.create_associated_pricelist(cr, uid, cur_id, context=context)
173
169
version_ids = version_obj.search(cr, uid, [('pricelist_id', 'in', pricelist_ids), ('active', 'in', ['t', 'f'])], context=context)
174
170
# Update the pricelists and versions
175
171
pricelist_obj.write(cr, uid, pricelist_ids, {'active': values['active']}, context=context)
176
172
version_obj.write(cr, uid, version_ids, {'active': values['active']}, context=context)
178
174
return super(res_currency, self).write(cr, uid, ids, values, context=context)
180
176
def check_in_use(self, cr, uid, ids, keyword='delete', context=None):
182
178
Check if the currency is currently in used in the system
200
196
partner_ids = property_obj.search(cr, uid, ['|', ('name', '=', 'property_product_pricelist'),
201
197
('name', '=', 'property_product_pricelist_purcahse'),
202
198
('value_reference', 'in', value_reference)])
204
# Raise an error if the currency is used on partner form
200
# Raise an error if the currency is used on partner form
206
202
raise osv.except_osv(_('Currency currently used !'), _('The currency you want to %s is currently used on at least one partner form.') % keyword)
208
204
# Raise an error if the currency is used on sale or purchase order
209
205
if purchase_ids or sale_ids:
210
206
raise osv.except_osv(_('Currency currently used !'), _('The currency you want to %s is currently used on at least one sale order or purchase order.') % keyword)
212
208
return pricelist_ids
214
210
def unlink(self, cr, uid, ids, context=None):
216
Unlink the pricelist associated to the currency
212
Unlink the pricelist associated to the currency
218
214
if isinstance(ids, (int, long)):
221
# Search for move lines with this currency. If those exists,
223
move_line_ids = self.pool.get('account.move.line').search(cr, uid, [('|'),
224
('currency_id','in',ids),
225
('functional_currency_id', 'in', ids)], context=context)
226
if len(move_line_ids) > 0:
227
raise osv.except_osv(_('Currency currently used !'), _('The currency cannot be deleted as one or more journal items are currently using it!'))
229
217
pricelist_obj = self.pool.get('product.pricelist')
231
219
# If no error, unlink pricelists
232
220
for p_list in self.check_in_use(cr, uid, ids, 'delete', context=context):
233
221
pricelist_obj.unlink(cr, uid, p_list, context=context)
234
return super(res_currency, self).unlink(cr, uid, ids, context=context)
223
res = super(res_currency, self).unlink(cr, uid, cur_id, context=context)
236
227
# remove this block because it has been moved to the sync module
237
228
# def get_unique_xml_name(self, cr, uid, uuid, table_name, res_id):
238
229
# currency = self.browse(cr, uid, res_id)
239
230
# table_name = currency.currency_table_id and currency.currency_table_id.name or ''
240
231
# return currency.name + table_name
243
234
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: