2
votes

I have added a custom field to my sale_order form, and now I want it to show up on the corresponding delivery order which is generated after the sale order is confirmed. There is probably an easier way to do this in v8 but I must do this in v7, so please only respond with answers that will work in OpenERP v7.

After having tried many other approaches to accomplish this, I found a suggestion on odoo.com from someone saying that you should try overriding the '_prepare_order_picking' function as it is what actually creates the delivery order from the values in the sale order, by returning a dictionary of values that are to be displayed on the delivery order. I have taken the code provided from that link, but it is not working for me. Here is the link. Any help is greatly appreciated, I have tried to make everything clear but let me know if I need to clarify anything and I will edit my question.

I have modified my code to be easier to read, and have limited the number of custom fields I need to take from the sale order to just one field, I need multiple when this is actually working, but it should be easy to scale once it is correctly implemented.

My modified code is as follows:

__openerp__.py:

{
"name"        : "Nick's Sale order/delivery order form fields",
"version"     : "1.0",
"author"      : "Nick",
"category"    : "Custom",
"depends"     : ['sale', 'stock'],
"description" : """ Sale and delivery order customizations by Nick """,
'data'        : ['nicks_sale_fields.xml', 'nicks_delivery_fields.xml'],
'installable' : True,
}

nicks_sale_fields.py:

from openerp.osv import fields,osv
class nicks_sale_fields(osv.Model):
    _inherit='sale.order'
    def _prepare_order_picking(self, cr, uid, order, context=None):
        vals=super(sale_order, self)._prepare_order_picking(cr, uid, order, context=context)
        vals.update({'my_custom_field': 'my_custom_field_value'})
        return vals

    _columns={
        'my_custom_field': fields.char('field description', required=True, store=True, size=3, help='field help text',),
    }
    _defaults={
        'my_custom_field': 'yes'
    }

nicks_sale_fields.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
    <record model="ir.ui.view" id="nicks_sale_order_fields">
        <field name="name">sale.order.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="/form/sheet/notebook/page[1]/group" position="after">
                <group string="Sales order: custom fields">
                    <field name="my_custom_field" string="Sale form descriptor" class="oe_inline"/>
                </group>
            </xpath>
        </field>
</record>
</data>
</openerp>

nicks_delivery_fields.xml:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
    <record model="ir.ui.view" id="nicks_deliv_order_fields">
        <field name="name">stock.picking.out.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="stock.view_picking_out_form"/>
        <field name="arch" type="xml">
            <xpath expr="/form/sheet/group/group[2]/field[@name='origin']" position="after">
                <group string="Delivery order: custom fields">
                    <field name="my_custom_field" string="Delivery form descriptor" class="oe_inline"/>
                </group>
            </xpath>
        </field>
    </record>
</data>
</openerp>
3
Note: you don't need the store=True since it's not a function field.Daniel Reis

3 Answers

1
votes

You need to add dependency of sale_stock(which is dependent on sale and stock) module in __openerp__.py file. like

"depends"     : ['sale_stock'],

And in nicks_delivery_fields.xml you have given a wrong model name to modify the delivery order view. it should be stock.picking

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>
      <record model="ir.ui.view" id="nicks_deliv_order_fields">
          <field name="name">stock.picking.out.form</field>
          <field name="model">stock.picking</field>
          <field name="inherit_id" ref="stock.view_picking_out_form"/>
          <field name="arch" type="xml">
              <xpath expr="/form/sheet/group/group[2]/field[@name='origin']" position="after">
                  <group string="Delivery order: custom fields">
                      <field name="my_custom_field" string="Delivery form descriptor" class="oe_inline"/>
                  </group>
              </xpath>
          </field>
      </record>
  </data>
</openerp>
1
votes

Assuming that you just want to make the custom Sale Order field available to read on the Delivery, you could use a related calculated field:

class StockPicking(osv.Model):
    _inherit='stock.picking'
    _columns={
        'my_custom_field': fields.related(
            'sale_id', 'my_custom_field', type='char',
            readonly=True, string='field description'),
    }
1
votes

Thanks to the help from everyone here I was able to solve my problem, @daniel-reis correctly stated how to use the related field, and that works great. Making my module depend on 'sale_stock' in the __openerp__.py file also helped, so thanks to @atul-arvind for that. After this didn't completely work I had to dig around a bit more and found that due to the way the stock.picking.out model inherits from stock.picking (in v7, I think this is fixed in v8) it is necessary to create two custom classes, one which inherits from stock.picking and one that inherits from stock.picking.out. Then in each of these classes, define your custom fields and then they will show up and the inheritence will work correctly, like so:

class stock_picking_out(osv.Model):
    _name = 'stock.picking.out'
    _inherit = 'stock.picking.out'
    _columns = { 'my_custom_field': fields.related(
        'sale_id', 'my_custom_field', type='char',
        readonly=True, string='field description'),
    }
class stock_picking(osv.Model):
    _name = 'stock.picking'
    _inherit = 'stock.picking'
    _columns = { 'my_custom_field': fields.related(
        'sale_id', 'my_custom_field', type='char',
        readonly=True, string='field description'),
    }

Then, in my xml file, I changed the model line to:

<field name="model">stock.picking.out</field>

Everything worked from here, my custom fields from my sale order were stored in the database table sale_order, and were properly displayed in the delivery order field.

Feel free to contact me with any questions about this post, and I will try to answer them.