1
votes

I have been trying to set the default domain on the product_uom field in sale.order.line. When you edit an existing quotation, it does not show units of measure in the same category as the default unit of sale. So, if you sell in Units, you'd want to see Units and dozens. Instead you see cm, g, in, etc. When you create an order, you don't have this problem because these values are set through onchange.

I thought this was going to be really simple, but I can't for the life of me understand why my attempts are failing. I've tried:

     <record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">

        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="attributes">
                <attribute name="domain">[('category_id', '=', product_id.uom_id.category_id.id)]</attribute>
            </xpath>
            </field>
     </record>

I've also tried

      <record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">

        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="replace">
                <field name="product_uom" domain="[('category_id', '=', product_id.uom_id.category_id.id)]" attrs="{'readonly': [('state', 'in', ('sale','done', 'cancel'))]}" context="{'company_id': parent.company_id}" groups="product.group_uom" options='{"no_open": True}'/>
            </xpath>
            </field>
      </record>

I get the error "Uncaught Error: AttributeError: object has no attribute 'uom_id' ". I don't really understand how this could be true. The field product_id has been declared in the view, the database maps it to product.product, and the field is definitely not empty when I go to edit the quote.

Any advice would be greatly appreciated.

2

2 Answers

0
votes

Check here the onchange product_id_change that sets a domain for product_uom.

The best thing to do is override it in your module.

Such domain in a view is really error prone, because when you access nested objects as you do with product_id.uom_id.category_id.id you don't really know if all the values to satisfy this chain are there. Worse, odoo's view rendering is not smart enough to be "soft" in these conditions.

0
votes

I still don't understand why my original scenario failed... but I found a workaround.

class NewSaleOrderLine(models.Model):
_inherit = 'sale.order.line'
#These computed fields are for calculating the domain on a form edit
relcatid = fields.Many2one(related='product_uom.category_id',store=True)

now in the xml...

<record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">
   <field name="name">sale.order.form.inherit</field>
   <field name="model">sale.order</field>
   <field name="inherit_id" ref="sale.view_order_form"/>
   <field name="arch" type="xml">
       <xpath expr="//field[@name='order_line']/tree/field[@name='qty_to_invoice']" position="after">
           <field name="relcatid" invisible="1"/>
       </xpath>

       <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="replace">
           <field name="product_uom" domain="[('category_id', '=', relcatid)]" attrs="{'readonly': [('state', 'in', ('sale','done', 'cancel'))]}" context="{'company_id': parent.company_id}" groups="product.group_uom" options='{"no_open": True}'/>
       </xpath>
   </field>
</record>

I'm still uncertain as to why this was necessary.