Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

on_change event won't go into loop

By
Kris Coenen
on 3/27/14, 7:43 AM 1,038 views

I'm currently making a module for a pharmaceutical company.

When one of the representitives gives out samples to the doctors they have to register it in openerp. Every doctor only gets 8 samples of any product a year. so when they add a sample to openerp they need to supply a product, quantity and parent_id, date is automatic today.

i can get the information from the current form and add it to the raise osv.except_osv event. but it only outputs the data of the form no sum of all the quantity's. so it seems that the function doesn't go into the loop.

here is my code

samples.py

    def on_change_quantity(self, cr, uid, ids, quantity, parent ,product , context=None):
    if parent:
        currentYear = datetime.now().year
        totaal=quantity
        for item in self.browse(cr, uid, ids):
            if parent==item.parent_id:
                if currentYear==item.year:
                    if product==item.product_id:
                        totaal=sum([rec.quantity for rec in self.browse(cr, uid, ids)])

        raise osv.except_osv(('fout'), (totaal) )

samples.xml

<field name="quantity" on_change="on_change_quantity(quantity,parent_id,product_id)"/>
0

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 3/27/14, 8:03 AM

If you try to count the amount of the same product for one doctor in one year, change

totaal=sum([rec.quantity for rec in self.browse(cr, uid, ids)])

to

totaal += 1

Or instead of the nested ifs use the search method:

def on_change_quantity(self, cr, uid, ids, quantity, parent ,product , context=None):
    if parent:
        currentYear = datetime.now().year
        totaal=quantity
        rec_ids = self.search(cr, uid, [('id','!=',ids[0]), ('parent_id','=',parent), ('year','=',currentYear), ('product_id','=',product)]);
        total += sum([rec.quantity for rec in self.browse(cr, uid, rec_ids, context=context)])

Hope that helps somehow.

the function without the ifs worked thanks

ANALYS4IT, Kris Coenen
on 3/27/14, 10:11 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 3/27/14, 7:43 AM
Seen: 1038 times
Last updated: 3/16/15, 8:10 AM