I am working in a stock new module that extend "Locations",  adding
new field "Stock Days", and the fields works fine. But when I try in
View Tree of Locations to search records with parameter "Stock Days" I
see this message:  "Missing search function on field 'Stock Days'",
and I don't get results. So I don't know what to do..

My code is:

class Template(ModelSQL, ModelView):
    _name = 'product.template'

    stock_days = fields.Function(fields.Integer('Stock Days',
select=1),
            'get_stock_days')

    def get_stock_days(self, ids, name):
        res = {}
        if name not in ('stock_days'):
            raise Exception('Bad argument')

        for template in self.browse(ids):
            res[template.id] = 0
            for product in template.products:
                res[template.id] += product[name]
        return res

    def __init__(self):
        super(Template, self).__init__()

    def search(self, args, offset=0, limit=None, order=None,
count=False,
            query_string=False):

        return super(Template, self).search(args, offset=offset,
limit=limit,
                order=order, count=count, query_string=query_string)

Template()


class Product(ModelSQL, ModelView):
    _name = "product.product"

    stock_days = fields.Function(fields.Integer('Stock Days',
select=1),
            'get_stock_days')

    def get_stock_days(self, ids, name):
        res = {}
        now = datetime.now()
        laptime = now - timedelta(TIME_HISTORY)
        move_obj = self.pool.get('stock.move')
        for product in self.browse(ids):
            res.setdefault(product.id, 0)
            domain = ['AND', [('product','=', product.id),],
                             [('create_date','>=', laptime),],
                             [('state','=', 'done'),],
                             ['OR', ('to_location.type','=',
'production'),
                                    ('to_location.type','=',
'customer'),],]
            ids_move_history = move_obj.search(domain)
            move_history_quantity = 0
            for move in move_obj.browse(ids_move_history):
                move_history_quantity += move.quantity
            if move_history_quantity > 0:
                res[product.id] = int(TIME_HISTORY *
product.quantity) / move_history_quantity
            else:
                res[product.id] = None
        res['stock_days'] = res[product.id]
        return res

    def search(self, args, offset=0, limit=None, order=None,
count=False,
            query_string=False):

        return super(Product, self).search(args, offset=offset,
limit=limit,
                order=order, count=count, query_string=query_string)

Product()


Thanks for advance.

Oscar Alvarez
Prexik Technologies
Colombia

-- 
[email protected] mailing list

Reply via email to