Odoo Help

0

How do you get shop_id from company_id?

By
Mathieu Stumpf
on 4/29/14, 11:05 AM 2,126 views

The goal is, in a multi-company instance, to enable duplication of sale orders emitted by other companies. So far other the instance configuration enable companies to see each other sale orders without modify them, which is good. Now when it comes to duplication, a write permission error rises. It looks like the problem is the shop_id field, which can't be set to an other company shop, which is fine as you would indeed expect the shop of the company attached to the current user to be used instead. So overriding the copy method of the sale_order class would probably be enough.

The only remaining problem is, how do you get the needed shop_id?

Here is what I have tried so far :

class sale_order(osv.Model):

    _inherit = 'sale.order'

    def _get_shop_id(self, cr, uid, id, default=None, context=None):
    print 'uid', uid
    user = self.pool.get('res.users').browse(cr, uid, uid,
                               context=context)
    company_id = user.company_id.id
    shop = self.pool.get('sale.shop').browse(cr, uid, company_id,
                               context=context)

    print 'company_id', company_id
    print 'shop_id', shop.id # return the same result as company_id :(
    return shop.id

    def copy(self, cr, uid, id, default=None, context=None):
    print '========== overloaded copy sale_order'
    shop_id = self._get_shop_id(cr, uid, id, default, context)

    if not default:
        default = {}
    default.update({
        'date_order': fields.date.context_today(self, cr, uid, context=context),
        'state': 'draft',
        'invoice_ids': [],
        'date_confirm': False,
        'client_order_ref': '',
        'name': self.pool.get('ir.sequence').get(cr, uid, 'sale.order'),
        'shop_id': shop_id,
    })
    return super(sale_order, self).copy(cr, uid, id, default, context=context)
0
Mathieu Stumpf
On 4/29/14, 12:26 PM

Here is a solution :

def _get_shop_id(self, cr, uid, id, default=None, context=None):
    user = self.pool.get('res.users').browse(cr, uid, uid,
                        context=context)
    company_id = user.company_id.id
    company_name = user.company_id.name
    shop_ids = self.pool.get('sale.shop').search(
    cr, uid, [ ('company_id','=', company_id) ], context=context)

    if shop_ids:
    return shop_ids[0]

    raise osv.except_osv(
    _("Error"),
    _('No shop defined for the company "%s"'% company_name)
    )

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)

Stats

Asked: 4/29/14, 11:05 AM
Seen: 2126 times
Last updated: 3/16/15, 8:10 AM