This question has been flagged
1 Reply
3803 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)"/>
Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

the function without the ifs worked thanks