Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1378 Lượt xem

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)
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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


Ảnh đại diện
Huỷ bỏ
Tác giả

Thankyou! It's working

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 5 15
5508
2
thg 10 16
6236
2
thg 1 16
3652
0
thg 3 15
2740
0
thg 3 15
4591