Ir al contenido
Menú
Se marcó esta pregunta

Hello,


In a sales order I want to filter products based on the selected customer previous sale orders. How can I get the partner_id information in sale order lines without saving the sales order ?


Any ideas ?


I'm working on Odoo 13 CE.


Thank you,


Alexandru Gagea

Avatar
Descartar

Hiiii,Alexandru Gagea
You can access the sales order record as active_ids from self._context and then access the partner.

@api.model
def _get_default_partner(self):
ctx = self._context
if ctx.get('active_model') == 'sale.order':
return self.env['sale.order'].browse(ctx.get('active_ids')[0]).partner_id.id

Autor

Thank you Vishnu, however, it's still not working.. bellow is my code.

I still don't get the partner_id value.

@api.model
def _get_default_partner(self):
ctx = self._context
if ctx.get('active_model') == 'sale.order':
return self.env['sale.order'].browse(ctx.get('active_ids')[0]).partner_id.id

def _partner_ordered_products_history(self):
partner_id = self._get_default_partner()
orders = self.env['sale.order'].search([('partner_id', '=', partner_id)])
order_lines = []
for order in orders.order_line:
order_lines.append(order)
products = []
for line in order_lines:
if line.return_date >= datetime.date(datetime.today()):
products.append(line.product_id.id)
domain = [
"('type', '!=', 'service')",
"('id', 'in', %s)" % products,
]
return '[' + ', '.join(domain) + ']'

Autor

@Sudhir, I'm actually not using sale order, but a similar model ( return orders ).

<field widget="section_and_note_one2many"
attrs="{'readonly': [('state', 'not in', ['new'])]}"
mode="tree"
name="return_order_line"
context="{'partner_id': partner_id}">

partner_id = self._context.get('partner_id') => this returns nothing..

Mejor respuesta

You should add partner_id in the context of the o2m field (order_line) in XML:

XML:
xpath expr="//field[@name='order_line']" position="attributes"
attribute name="context">{'partner_id': partner_id}

Now, write an onchange on display_type field to set the domain in product_id field:


    @api.onchange('display_type')
def onchange_display_type(self):
partner_id = self._context.get('partner_id')
if not partner_id:
return {}
orders = self.env['sale.order'].search([('partner_id', '=', partner_id)])
products = []
for order in orders:
for line in order.order_line:
if line.return_date >= datetime.date(datetime.today()):
products.append(line.product_id.id)
domain = [
"('type', '!=', 'service')",
"('id', 'in', %s)" % products,
]
return {'domain': {'product_id': domain}}
Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
sept 21
2028
1
ene 25
11454
2
ene 24
5890
0
jun 23
2385
1
dic 22
3783