Skip to Content
Menu
This question has been flagged
1 Reply
1020 Views

Hello everyone!
I want to set a custom domain on product_template_id field in sale.order.line model. When the quotation is created from CRM then I want to display specific products in the order lines. I have created a field called is_from_crm which will be true if the quotation is created through CRM.

This code is not working even if the boolean field is true when I added logs, even the order_id is false.

def _get_allowed_products_domain(self):
        domain = [('sale_ok', '=', True)]
        if self.order_id.is_from_crm:
            allowed_products = self.env['crm.quotation.product'].search([]).product_ids
            domain.append(('id', 'in', allowed_products.ids))
        return domain

    product_template_id = fields.Many2one(
        string="Product Template",
        comodel_name='product.template',
        compute='_compute_product_template_id',
        readonly=False,
        search='_search_product_template_id',
        domain = _get_allowed_products_domain)
Avatar
Discard
Best Answer

Hi Frah

the domain in Sale order lines is managed by many apps and logic

the best approach is tto create a field 

		​class SaleOrder(models.Model):
    _inherit = "sale.order"

​allowed_product_ids = fields.Many2many(
        comodel_name="product.product",
        string="Allowed Products",
        compute="_compute_allowed_product_ids",
    )


​def _get_allowed_products_domain(self):
​​rec.allowed_products = False
​for rec in records:
​​if rec.order_id.is_from_crm:
        ​rec.allowed_products = self.env['crm.quotation.product'].search([]).product_ids


#in the view
<field name="arch" type="xml">
         <field name="partner_id" position="after">
                <field name="allowed_product_ids" invisible="1" />
                <field name="is_from_crm" invisible="1" />
            </field>
            <!-- Form -->
            <xpath
                expr="//field[@name='order_line']/form//field[@name='product_id']"
                position="attributes"
            >
                <attribute
                    name="domain"
                    operation="domain_add"
                    condition="parent.has_allowed_products"
                >
                    [('id', 'in', parent.allowed_product_ids)]
                </attribute>
            </xpath>
            <!-- Tree -->
            <xpath
                expr="//field[@name='order_line']/tree/field[@name='product_id']"
                position="attributes"
            >
                <attribute
                    name="domain"
                    operation="domain_add"
                    condition="parent.has_allowed_products"
                >
                    [('id', 'in', parent.allowed_product_ids)]
                </attribute>
            </xpath>
        </field>


An inspiration for that is https://github.com/OCA/sale-workflow/tree/16.0/sale_order_product_assortment

hope this helps you

Daniel


Avatar
Discard
Author

Thankyou! It's working

Related Posts Replies Views Activity
0
May 15
5154
2
Oct 16
5863
2
Jan 16
3373
0
Mar 15
2440
0
Mar 15
4257