~camptocamp/margin-analysis/7.0-product_cost_incl_bom_price_history-mismatch-product-template

« back to all changes in this revision

Viewing changes to product_cost_incl_bom_price_history/product.py

  • Committer: Guewen Baconnier
  • Date: 2014-01-13 16:21:16 UTC
  • mfrom: (54.3.4 margin-analysis)
  • Revision ID: guewen.baconnier@camptocamp.com-20140113162116-wrhgb0r117i1zjqb
[MRG] from lp:~camptocamp/margin-analysis/7.0-product_cost_incl_bom_price_history-mismatch-product-template

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
from openerp.osv import orm, fields
23
23
import decimal_precision as dp
24
 
import openerp
25
24
from openerp.addons.product_price_history.product_price_history import (
26
25
    PRODUCT_FIELD_HISTORIZE
27
26
)
28
 
from openerp import SUPERUSER_ID
29
27
import time
30
28
from datetime import datetime, timedelta
31
29
import logging
60
58
                                    if not field_flag:
61
59
                                        field_flag = True
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
65
63
        # store fields
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]:
73
71
                        result.pop(r)
74
 
        for id, value in result.items():
75
 
            tpl_id = self.read(cr, uid, id,
76
 
                               ['product_tmpl_id'],
77
 
                               context=context)['product_tmpl_id']
78
 
            _logger.debug("set price history: %s, product_tpl_id: %s, "
79
 
                          "context: %s",
80
 
                          value,
81
 
                          tpl_id,
82
 
                          context)
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,
84
78
                                           field_name,
85
79
                                           value,
86
80
                                           context=context)
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",
105
 
                      ids,
106
 
                      fields)
107
98
        res = super(product_product, self)._store_set_values(cr,
108
99
                                                             uid,
109
100
                                                             ids,
138
129
                   context=None, load='_classic_read'):
139
130
        if context is None:
140
131
            context = {}
141
 
        if fields:
 
132
        if not fields:
 
133
            fields = []
 
134
        else:
 
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')
 
139
 
 
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')
 
145
 
144
146
        results = super(product_product, self)._read_flat(cr, uid, ids,
145
147
                                                          fields,
146
148
                                                          context=context,
147
149
                                                          load=load)
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,
153
155
                                                            context=context)
154
156
            if context.get('to_date'):
155
157
                date_crit = context['to_date']
156
 
            # if fields is empty we read all price fields
157
 
            if not fields:
158
 
                price_fields = PRODUCT_FIELD_HISTORIZE
159
 
            # Otherwise we filter on price fields asked in read
 
158
            if load == '_classic_write':
 
159
                # list of ids
 
160
                tmpl_ids = [row['product_tmpl_id'] for row in results]
160
161
            else:
161
 
                price_fields = [f for f in PRODUCT_FIELD_HISTORIZE
162
 
                                if f in fields]
163
 
            prod_prices = price_history._get_historic_price(cr, uid,
164
 
                                                            ids,
165
 
                                                            company_id,
166
 
                                                            datetime=date_crit,
167
 
                                                            field_names=price_fields,
168
 
                                                            context=context)
 
162
                # list of (id, name)
 
163
                tmpl_ids = [row['product_tmpl_id'][0] for row in results]
 
164
            prod_prices = price_history._get_historic_price(
 
165
                cr, uid,
 
166
                tmpl_ids,
 
167
                company_id,
 
168
                datetime=date_crit,
 
169
                field_names=historized_fields or PRODUCT_FIELD_HISTORIZE,
 
170
                context=context)
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']
 
174
                else:
 
175
                    tmpl_id = result['product_tmpl_id'][0]
 
176
                dict_value = prod_prices[tmpl_id]
171
177
                result.update(dict_value)
 
178
 
 
179
        if remove_tmpl_field:
 
180
            for row in results:
 
181
                # this field was not asked by the callee
 
182
                del row['product_tmpl_id']
172
183
        return results
173
184
 
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'],
186
197
                             context=context)
187
 
        _logger.debug("product value get, result :%s, context: %s",
188
 
                      products, context)
189
198
        for product in products:
190
199
            res[product['id']] = product['qty_available'] * product['cost_price']
191
200
        return res