@api.multi
def onchange_partner_id(self, part):
res = super(SaleOrder, self).onchange_partner_id(part)
domain = [('active', '=', True), ('sale_ok', '=', True)]
if part:
partner = self.env['res.partner'].browse(part)
if partner and partner.sales_channel_id:
domain.append(('sales_channel_ids', '=',
partner.sales_channel_id.id))
product_ids = self.env['product.product'].search(domain)
res.update(domain={
'order_line.product_id': ['id', 'in', [rec.id for rec in product_ids]]
})
return res
I tried another solution, so I overrode the search_name and search methods in the product.product model, and it works.
Here is my code,
1) Inherit sale_view.xml
('sales_channel_ids', '=', parent.partner_channel_id),
('sale_ok', '=', True),
('active', '=', True)
]
'partner_channel_id': parent.partner_channel_id,
'partner_id': parent.partner_id,
'quantity': product_uom_qty,
'pricelist': parent.pricelist_id,
'uom': product_uom,
'company_id': parent.company_id
}
and coverage methods
2) Search name method: @api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
if 'partner_channel_id' in self._context:
target_domain = findDomain(args, 'sales_channel_ids')
if len(target_domain) == 1:
idx = target_domain.keys()[0]
domain = target_domain.values()[0]
if False in domain:
args.pop(idx)
return super(ProductProduct, self).name_search(name=name,
args=args,
operator=operator,
limit=limit)
3) Search method: @api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
if 'partner_channel_id' in self._context:
target_domain = findDomain(args, 'sales_channel_ids')
if len(target_domain) == 1:
idx = target_domain.keys()[0]
domain = target_domain.values()[0]
if False in domain:
args.pop(idx)
return super(ProductProduct, self).search(args=args,
offset=offset,
limit=limit,
order=order,
count=count)
What I need to do here is, when they detect "partner_channel_id" in the context and domain, for example ["sales_channel_id", "=", False], I remove that domain and keep the rest of the domain (the customer has no channel to see all products and does not filter products from it).
Like and dislike comments - March 31, 2020 14:47
