This question has been flagged
1 Reply
8251 Views

When creating a purchase order, if you happen to change the price first then quantity or unit of measure after, the price will reset back to what it was before you changeg it (default price or 0.0). It have something to do with the onchange_product_id function but, cannot figure out what exactly creates this.

here is the code of the function (with non relevent code removed) :

def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
        partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
        name=False, price_unit=False, context=None):

    if context is None:
        context = {}

    res = {'value': {'price_unit': price_unit or 0.0, 'name': name or '', 'product_uom' : uom_id or False}}
    if not product_id:
        return res

    product_product = self.pool.get('product.product')
    product_uom = self.pool.get('product.uom')
    product_pricelist = self.pool.get('product.pricelist')

 ....

    if pricelist_id:
        price = product_pricelist.price_get(cr, uid, [pricelist_id],
                product.id, qty or 1.0, partner_id or False, {'uom': uom_id, 'date': date_order})[pricelist_id]
    else:
        price = product.standard_price

....

    res['value'].update({'price_unit': price, 'taxes_id': taxes_ids})

    return res

product_id_change = onchange_product_id
product_uom_change = onchange_product_uom

What cause that behavior and how to modify it to keep the price entered by the user at any time ? (This has been like that since at least V.6)

Avatar
Discard
Best Answer

Hello,

the price changed by this code:

# - determine price_unit and taxes_id
    if pricelist_id:
        price = product_pricelist.price_get(cr, uid, [pricelist_id],
                product.id, qty or 1.0, partner_id or False, {'uom': uom_id, 'date': date_order})[pricelist_id]
    else:
        price = product.standard_price

it'll check the price list for the product or set it to the standard one. So if you need the price entered by the user never changed you have to overwrite this function to don't use the price variable in the line "before the return res":

res['value'].update({'price_unit': price, 'taxes_id': taxes_ids})

Update

you can add

if price_unit:
         price = price_unit

after the if - else condition ...

note: this is simple solution, in case the user doesn't change the price and then he changed the uom, there will be no automatic converting to the price ! so you'll need more work here.

I hope this could helps..

Regards...

Avatar
Discard
Author

I did had tried that change, it did not worked.

It had something to do with the declaration price_unit=False in the signature. I have moved price_unit before the date_order=False, in the signature so it did not need to be initiated, did the changes in form view too. It works. Not for the the onchange_product_uom function which calls the onchange_product_id function in its return. Did the same changes in same order (in signature) but, getting a string instead of int error.... Looking at it now !

there is no something to to with price_unit=False it just a default value, you've to don't interrupt the order of the signature. I have tried my change before I post this answer and it worked! I hope if you can return the function as it is original one with that change and try it...

Author

When using:

res['value'].update({'price_unit': price_unit, 'taxes_id': taxes_ids})

If I change the quantity afterwards or the unit of measure, the price returns to 0.00$.

I have changed everything back to original, I had already tried your solution before and it was behaving that way. I will also retry the price = price_unit and see but as I remember, it was also setting it back to 0.00$

I've updated the answer, if you still get the 0.00 , please check the order of the params in the xml view you can check the price_unit value by printing it to server and then try to change the qty.

hey man, I didn't notice that you're tagging V8, this may not work with you cause I checked it with V7 I'm so sorry for that...

Author

No prob, I have a production server which runs 6.0.x, will try your solution on it. For 8, I have set everything back to normal, re-moved price_unit in the signature of only onchange_product_id fonction so it does not need to be initiated to false, and made the appropriated changes in the xml also and it works as intended for both, onchange_product_id and onchange_product_uom. Looks like the signature sets it to 0 (false) no mather what the call pass as argument.