22
22
from openerp.osv import orm, fields
23
23
import decimal_precision as dp
25
24
from openerp.addons.product_price_history.product_price_history import (
26
25
PRODUCT_FIELD_HISTORIZE
28
from openerp import SUPERUSER_ID
30
28
from datetime import datetime, timedelta
62
60
if self._columns[field_name]._multi:
63
raise ValueError('multi is not supported on the cost_price field')
61
raise ValueError('multi is not supported on the %s field' % field_name)
64
62
# use admin user for accessing objects having rules defined on
66
64
result = self._columns[field_name].get(cr, self, ids,
71
69
if r in field_dict.keys():
72
70
if field_name in field_dict[r]:
74
for id, value in result.items():
75
tpl_id = self.read(cr, uid, id,
77
context=context)['product_tmpl_id']
78
_logger.debug("set price history: %s, product_tpl_id: %s, "
83
prod_tpl_obj._log_price_change(cr, uid, id,
72
prods = self.read(cr, uid, result.keys(), ['product_tmpl_id'],
73
context=context, load='_classic_write')
74
tmpls = dict((row['id'], row['product_tmpl_id']) for row in prods)
75
for prod_id, value in result.iteritems():
76
tmpl_id = tmpls[prod_id]
77
prod_tpl_obj._log_price_change(cr, uid, tmpl_id,
101
95
fields = list(set(fields))
102
96
fields.remove('cost_price')
103
97
self._set_field_name_values(cr, uid, ids, 'cost_price', context)
104
_logger.debug("call _store_set_values, ids %s, fields: %s",
107
98
res = super(product_product, self)._store_set_values(cr,
138
129
context=None, load='_classic_read'):
139
130
if context is None:
135
fields = fields[:] # avoid to modify the callee's list
136
if fields and not 'id' in fields:
142
137
fields.append('id')
143
138
pt_obj = self.pool.get('product.template')
140
historized_fields = [f for f in fields if f in PRODUCT_FIELD_HISTORIZE]
141
remove_tmpl_field = False
142
if fields and not 'product_tmpl_id' in fields and historized_fields:
143
remove_tmpl_field = True
144
fields.append('product_tmpl_id')
144
146
results = super(product_product, self)._read_flat(cr, uid, ids,
148
150
# Note if fields is empty => read all, so look at history table
149
if not fields or any([f in PRODUCT_FIELD_HISTORIZE for f in fields]):
151
if not fields or historized_fields:
150
152
date_crit = False
151
153
price_history = self.pool.get('product.price.history')
152
154
company_id = pt_obj._get_transaction_company_id(cr, uid,
154
156
if context.get('to_date'):
155
157
date_crit = context['to_date']
156
# if fields is empty we read all price fields
158
price_fields = PRODUCT_FIELD_HISTORIZE
159
# Otherwise we filter on price fields asked in read
158
if load == '_classic_write':
160
tmpl_ids = [row['product_tmpl_id'] for row in results]
161
price_fields = [f for f in PRODUCT_FIELD_HISTORIZE
163
prod_prices = price_history._get_historic_price(cr, uid,
167
field_names=price_fields,
163
tmpl_ids = [row['product_tmpl_id'][0] for row in results]
164
prod_prices = price_history._get_historic_price(
169
field_names=historized_fields or PRODUCT_FIELD_HISTORIZE,
169
171
for result in results:
170
dict_value = prod_prices[result['id']]
172
if load == '_classic_write':
173
tmpl_id = result['product_tmpl_id']
175
tmpl_id = result['product_tmpl_id'][0]
176
dict_value = prod_prices[tmpl_id]
171
177
result.update(dict_value)
179
if remove_tmpl_field:
181
# this field was not asked by the callee
182
del row['product_tmpl_id']
174
185
def _product_value(self, cr, uid, ids, field_names=None,
184
195
products = self.read(cr, uid, ids,
185
196
['id', 'qty_available', 'cost_price'],
187
_logger.debug("product value get, result :%s, context: %s",
189
198
for product in products:
190
199
res[product['id']] = product['qty_available'] * product['cost_price']