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

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)
    )
Avatar
Discard