Skip to Content
Menu
This question has been flagged
3 Replies
6645 Views

Hi all,

I am trying to replace the position of smart button "Purchase" when viewing a product.

This smart button is injected by app "purchase" in the form view named "product.template.purchase.button.inherit"

Code is located in "purchase_views.xml" as below

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

            <field name="name">product.template.purchase.button.inherit</field>
            <field name="model">product.template</field>
            <field name="inherit_id" ref="product.product_template_only_form_view"/>
            <field name="groups_id" eval="[(4, ref('purchase.group_purchase_user'))]"/>
            <field name="arch" type="xml">
                <button name="toggle_active" position="before">
                   <button class="oe_stat_button" name="%(purchase.action_purchase_line_product_tree)d"
                       type="action" icon="fa-shopping-cart">
                       <field string="Purchases" name="purchase_count" widget="statinfo"/>
                   </button>
                </button>
            </field>
</record>

Most smart buttons are placed by view inheritance with """ <button name="toggle_active" position="before"> """ in another module/app

I'd like to override everything in this view record by creating and loading a new xml file in my custom module. My understanding is that I need to refer to the right record id to override it.

My xml code:

 <?xml version="1.0" encoding="utf-8" ?>

<odoo>
 <data>
      <record id="purchase.view_product_template_purchase_buttons_from" model="ir.ui.view">
         <field name="name">product.template.purchase.button.inherit</field>
         <field name="model">product.template</field>
         <field name="inherit_id" ref="product.product_template_only_form_view"/>
         <field name="groups_id" eval="[(4, ref('purchase.group_purchase_user'))]"/>
         <field name="arch" type="xml">
            <button name="action_view_sales" position="after">
             <button class="oe_stat_button" name="%(purchase.action_purchase_line_product_tree)d" type="action" icon="fa-shopping-cart">
               <field string="Purchases" name="purchase_count" widget="statinfo"/>
             </button>
            </button>
         </field>
      </record>
 </data>
</odoo>

With my code, the error shows "purchase_count" is not found in model.

What is the correct way of replacing everything in existing view?

Avatar
Discard

Did You Taken Field "purchase_count" on .py File?.

Best Answer


 

In __manifest__.py (in your module), try this

'depends': ['purchase'],

Avatar
Discard
Author Best Answer

Hey guys, thank you for your help.

I did some testing today, though I am able to solve the problem by using Zbik's answer by including purchase module as dependency.
However, not every field needed to be addressed or CAN be addressed by adding module to dependencies.

I also tried to override record id "product_template_form_view_procurement_button" in the stock module(product.views.xml).
The button name to "Update Qty on Hand" cannot be found due to error shown as "Not being able to locate action_view_change_product_quantity", the original code:

<header position="inside">
    <button string="Update Qty On Hand"
            type="action" name="%(action_view_change_product_quantity)d"
            attrs="{'invisible': [('type', '!=', 'product')]}"/>
</header> 

In my override xml file, I solved the problem by changing this sector of code to:

<header position="inside">
    <button string="Update Qty On Hand"
            name="%(stock.action_view_change_product_quantity)d"             type="action"             attrs="{'invisible': [('type', '!=', 'product')]}"/>
</header>

With this, I don't need to include stock in 'depends' = ['stock'] in __manifest__.py

I am very curious why, but I suppose it has to do with the sequence of Odoo loading each module?


Here's all the override xml code

<?xml version="1.0" encoding="utf-8" ?>


<odoo>
 <data>

  <!-- Override view_product_template_purchase_buttons_from in purchase -->
        <record id="purchase.view_product_template_purchase_buttons_from" model="ir.ui.view">
         <field name="name">product.template.purchase.button.inherit</field>
         <field name="model">product.template</field>
         <field name="inherit_id" ref="product.product_template_only_form_view"/>
         <field name="groups_id" eval="[(4, ref('purchase.group_purchase_user'))]"/>
         <field name="arch" type="xml">
    <button name="action_view_sales" position="after">
     <button class="oe_stat_button" name="%(purchase.action_purchase_line_product_tree)d" type="action" icon="fa-shopping-cart">
      <field string="Purchases" name="purchase_count" widget="statinfo"/>
     </button>
    </button>
   </field>
  </record>

  <!-- Override product_template_form_view_procurement_button in stock -->
  <record id="stock.product_template_form_view_procurement_button" model="ir.ui.view">
   <field name="name">product.template_procurement</field>
   <field name="model">product.template</field>
   <field name="inherit_id" ref="product.product_template_only_form_view"/>
   <field name="arch" type="xml">
    <data>

     <header position="inside">
      <button name="%(stock.action_view_change_product_quantity)d" string="Update Qty On Hand" type="action" attrs="{'invisible': [('type', '!=', 'product')]}"/>
     </header>

     <button name="toggle_active" position="before">

      <button type="object" name="action_open_quants" attrs="{'invisible':[('type', '!=', 'product')]}" class="oe_stat_button" icon="fa-building-o">
       <field name="qty_available" widget="statinfo" string="On Hand"/>
      </button>

      <button string="Product Moves" type="object" name= "action_view_stock_move_lines" attrs="{'invisible':[('type', 'not in', ['product', 'consu'])]}" class="oe_stat_button" icon="fa-arrows-v" groups="stock.group_stock_user"/>
      <button type="object"
                      name="action_view_orderpoints"
                      attrs="{'invisible':['|',('type', '!=', 'product'),('nbr_reordering_rules', '==', 1)]}"
                      class="oe_stat_button"
                      icon="fa-refresh">
       <field name="nbr_reordering_rules" widget="statinfo"/>
      </button>

      <button type="object" name="action_view_orderpoints" attrs="{'invisible':['|',('type', '!=', 'product'),('nbr_reordering_rules', '!=', 1)]}" class="oe_stat_button" icon="fa-refresh">
       <div class="o_field_widget o_stat_info mr4">
        <span class="o_stat_text">Min:</span>
        <span class="o_stat_text">Max:</span>
       </div>
       <div class="o_field_widget o_stat_info">
        <span class="o_stat_value"><field name="reordering_min_qty"/></span>
        <span class="o_stat_value"><field name="reordering_max_qty"/></span>
       </div>
      </button>

      <button string="Routes" type="object" name="action_view_routes" attrs="{'invisible':[('type', '!=', 'product')]}" class="oe_stat_button" icon="fa-cogs" groups="base.group_no_one"/>

      <button string="Lot/Serial Number" type="object" name="action_open_product_lot" attrs="{'invisible': [('tracking', '=', 'none')]}" class="oe_stat_button" icon="fa-bars" groups="stock.group_production_lot"/>

     </button>

     <button name="toggle_active" position="after">
      <button type="action" name="%(stock.action_stock_level_forecast_report_template)d" attrs="{'invisible':[('type', '!=', 'product')]}" class="oe_stat_button" icon="fa-building-o">
       <div class="o_field_widget o_stat_info">
        <span class="o_stat_value"><field name="virtual_available" widget="statinfo" nolabel="1"/>
         <span attrs="{'invisible':[('outgoing_qty', '=', 0),('incoming_qty','=',0)]}" groups="base.group_no_one">
                                  (-<field name="outgoing_qty" widget="statinfo" nolabel="1"/>+<field name="incoming_qty" widget="statinfo" nolabel="1"/>)
         </span>
        </span>
        <span class="o_stat_text">Forecasted</span>
       </div>
      </button>
     </button>


     <xpath expr="//group[@name='group_lots_and_weight']" position="attributes">
      <attribute name="attrs">{'invisible':['|', ('type', 'not in', ['product', 'consu']), ('product_variant_count', '&gt;', 1)]}</attribute>
     </xpath>

     <xpath expr="//group[@name='group_lots_and_weight']" position="inside">
      <field name="responsible_id"/>
     </xpath>

          </data>
      </field>
  </record>

 </data>
</odoo>
Link to final result screenshot: https://imgur.com/h19PeLI

Avatar
Discard
Related Posts Replies Views Activity
1
Apr 17
2766
2
Sep 24
2597
2
Jul 19
7537
6
Sep 17
6350
1
Mar 15
3135