It is currently Wed May 23, 2012 2:50 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: currency field for product, but read only-please help [SOLV]
PostPosted: Wed Jul 27, 2011 9:03 am 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
Hi,
I am trying to add in the product view a new field for currency (only to store the currency in which the product is added to the database), later on I plan to use this field in price computation.
The reason for this is fairly complex and I will not explain this here.

But I have done that:
a file called "product_currency.py"
Code:
from osv import fields
from osv import osv
import ir
import pooler


class product_product(osv.osv):
    _name = 'product.product'
    _inherit = 'product.product'

    def _get_currency(self, cr, uid, context=None):
        user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, [uid], context=context)[0]
        if user.company_id:
            return user.company_id.currency_id.id
        return pooler.get_pool(cr.dbname).get('res.currency').search(cr, uid, [('rate','=', 1.0)])[0]
      
    _columns = {
        'currency_id': fields.many2one('res.currency', 'Currency', readonly=False),
    }

    _defaults = {
        'currency_id': _get_currency,
    }


product_product()


and another "product_currency_view.xml"

Code:
<?xml version="1.0"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="product_product_tree_view_extended">
            <field name="name">product.product.tree</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="product.product_product_tree_view" />
            <field name="type">tree</field>
            <field name="arch" type="xml">
                    <field name="name" position="after">
                    <field name="currency_id" width="50"/>
                </field>
            </field>
        </record>

        <record model="ir.ui.view" id="view_product_form_expiry">
            <field name="name">product.normal.form</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="product.product_normal_form_view" />
            <field name="type">form</field>
            <field name="arch" type="xml">
                    <field name="cost_method" position="before">
                            <group colspan="2" col="2" groups="base.group_extended">
                    <field name="currency_id" />
                            </group>
               </field>
            </field>
        </record>
    </data>
</openerp>


My problem is that no matter what I do, the field is read only, in the form view of the product and I would like to make it writable so I can select a new currency.

Thanks in advance,

_________________
OpenERP debugging with Eclipse on headless server


Last edited by mihai on Sat Jul 30, 2011 6:50 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: currency field for product added, but read only-please h
PostPosted: Wed Jul 27, 2011 11:14 am 
Offline

Joined: Wed Jan 27, 2010 6:21 pm
Posts: 797
Location: Auckland, NZ
You really need to try and follow coding guidelines, it makes it easier to follow. And it is hard to geuss what the issue is when things are written unusually. Below is a slightly more sane edit, although no gaurentees it works. Just to give you an idea.

Code:
from osv import osv, fields

class product_template(osv.osv):
    _inherit = 'product.template'

    def _get_currency(self, cr, uid, context=None):
        user = self.pool.get('res.users').browse(cr, uid, uid, context=context) #why pass a list to get a list then take the first element, just pass the id.
        if user.company_id: #Isn't company_id mandatory? therefore it should always be true? anyway in multicompany this will fail if user is assigned to one company by default and using another.
            return user.company_id.currency_id.id
        return self.pool.get('res.currency').search(cr, uid, [('rate','=', 1.0)])[0]#this will fail in multicompany.
You could write the previous 3 lines just as clearly as
    return user.company_id and user.company_id.currency_id.id or self.pool.get('res.currency').search(cr, uid, [('rate','=', 1.0)])[0]
although probably unnecessary, esp. if company_id is mandatory on user.

     
    _columns = {
        'currency_id': fields.many2one('res.currency', string='Currency'),
    }

    _defaults = {
        'currency_id': _get_currency,
    }


product_template()


Anyway on to the issue of readonly. If after cleaning your code up a bit, if it is still not fixed, it might pay to just walk back through it. If you remove the defaults does it work? etc etc. I think also this module will always be buggy as res.currency is defined for each and every company, yet a product is defined for none or 1 companies and security rules allow sharing by default. But then you want it to be the same in each company. This is not a problem of your making btw, it is because of the way OpenERP handles currency. You might need to change security rules on res.currency to have a chance in multicompany, but in single company it should work fine.

Also it makes more sense to define at the template level, rather than product level, as costs and prices are currently defined there.

_________________
Graeme


Top
 Profile  
 
 Post subject: Re: currency field for product added, but read only-please h
PostPosted: Wed Jul 27, 2011 3:32 pm 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
Thank you for your support,

it is really a better idea to put it in the template.

The _get_currency code is totally copy paste from official invoice module, I just wanted a quik way to have currency there in the product.

But still read only, even if I remove the defaults.
I started fresh on a database, still read only.

I'm stuck!
Thanks.

_________________
OpenERP debugging with Eclipse on headless server


Top
 Profile  
 
 Post subject: Re: currency field for product added, but read only-please h
PostPosted: Thu Jul 28, 2011 12:30 am 
Offline

Joined: Wed Jan 27, 2010 6:21 pm
Posts: 797
Location: Auckland, NZ
Just another OpenERP default function that only works properly in single company.

Are you inheriting from base and product modules in your dependencies in __openerp__.py ? Seems unlikely to be the issue but could be. The only other thing I can think of is that the inheritances on that product form are quite tricky because of stock and mrp modules also modifying around that area, and certain settings there turn the whole group readonly. Try putting your field somewhere else unrelated and see if it persists.

It is confusing, what you want to do might be complex, but this part really is quite simple.

_________________
Graeme


Top
 Profile  
 
 Post subject: Re: currency field for product added, but read only-please h
PostPosted: Thu Jul 28, 2011 12:46 am 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
Hi again,

Well, to make it simple I just want to add the currency field in the product tree and form view.
In the form view to be changeable and in the tree view only as an info.

Perhaps there is another way.

Thanks a lot.
Mihai

_________________
OpenERP debugging with Eclipse on headless server


Top
 Profile  
 
 Post subject: Re: currency field for product added, but read only-please h
PostPosted: Thu Jul 28, 2011 2:49 am 
Offline

Joined: Wed Jan 27, 2010 6:21 pm
Posts: 797
Location: Auckland, NZ
As I say try putting the field somewhere else on the form, away from cost price, to eliminate that as an issue.

_________________
Graeme


Top
 Profile  
 
 Post subject: Re: currency field for product added, but read only-please h
PostPosted: Thu Jul 28, 2011 10:55 pm 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
After updating to the latest revision, the currency field is writable now, no changes in the code.

Thanks.

_________________
OpenERP debugging with Eclipse on headless server


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP