Wednesday, June 26, 2013

OpenERP 7: How can I add images in sale.oder.line or in treeview generally?

ok, finally i have found a way. i created a module as following:
__init__.py:
import productimport stock
__openerp__.py:
{
    "name" : "Product Image",
    "version" : "0.1",
    "author" : "Stefan Reisich, Rove.design GmbH",
    "website" : "http://www.rove.de/",
    "description": """
This Module overwrites openerp.web.list.Binary field to show the product image in the listview. A new column with product image is added.
    """,
    "depends" : [
                 "sale",
                 "sale_stock",
                 "stock"
    ],
    'js': [
           'static/src/js/view_list.js'
    ],
    "data": [
        'product_view.xml',
        'sale_view.xml',
        'stock_view.xml'
    ],
    'installable': True,
    "active": False,
}
product_view.xml:
xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>

        <record id="product_product_tree_view_image" model="ir.ui.view">
            <field name="name">product.product.tree</field>
            <field name="model">product.product</field>
            <field name="inherit_id" ref="product.product_product_tree_view"/>
            <field name="arch" type="xml">
                <field name="default_code" position="before">
                    <field name="image_small"/>
                </field>
            </field>
        </record>

    </data>
</openerp>
sale_view.xml:
xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>

        <record id="view_order_image_form_change" model="ir.ui.view">
            <field name="name">sale.order.form.sale.image</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <xpath expr="//page[@string='Order Lines']/field[@name='order_line']/form[@string='Sales Order Lines']/group/group/field[@name='product_id']" position="replace">
                    <field name="product_id"
                           context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}"
                           groups="base.group_user"
                           on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False,image_small, context)"/>
                </xpath> 

            </field>
        </record>

        <record id="view_order_image_form_inherit" model="ir.ui.view">
            <field name="name">sale.order.form.sale.image</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <xpath expr="//page[@string='Order Lines']/field[@name='order_line']/form[@string='Sales Order Lines']/group/group/field[@name='product_id']" position="after">
                    <field name="image_small" widget="image"/>
                </xpath> 

                <xpath expr="//page[@string='Order Lines']/field[@name='order_line']/tree[@string='Sales Order Lines']/field[@name='price_subtotal']" position="after">
                    <field name="image_small" widget="image"/>
                </xpath>


            </field>
         </record>

    </data>
</openerp>
stock_view.xml:
xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>

        <record id="view_move_form2" model="ir.ui.view">
            <field name="name">stock.move.form2</field>
            <field name="model">stock.move</field>
            <field name="inherit_id" ref="stock.view_move_form"/>
            <field name="arch" type="xml">
                <xpath expr="/form/sheet/group/group/field[@name='product_id']" position="after">
                        <field name="image_small" widget="image"/>
                </xpath>
            </field>
        </record>

        <record id="view_move_tree_inherit" model="ir.ui.view">
            <field name="name">stock.move.tree2</field>
            <field name="model">stock.move</field>
            <field name="inherit_id" ref="stock.view_move_picking_tree"/>
            <field name="arch" type="xml">
                <field name="product_id" position="before">
                        <field name="image_small" widget="image" string="Image"/>
                </field>
            </field>
        </record>

    </data>
</openerp>
stock.py:
from openerp.osv import fields, osv
class stock_move(osv.Model):
    _name = 'stock.move'
    _inherit = 'stock.move'

    def onchange_product_id(self, cr, uid, ids, prod_id=False, loc_id=False,
                            loc_dest_id=False, partner_id=False):
        res_prod = super(stock_move, self).onchange_product_id(cr, uid, ids, prod_id, loc_id,loc_dest_id, partner_id)
        prod_obj = self.pool.get('product.product')
        obj = prod_obj.browse(cr, uid, prod_id)
        res_prod['value'].update({'image_small': obj.image_small})
        return res_prod


    _columns = {
        'image_small' : fields.binary('Product Image'),
    }

stock_move()
class sale_order_line(osv.Model):
    _name = 'sale.order.line'
    _inherit = 'sale.order.line'
    _columns = {
        'image_small' : fields.binary('Product Image'),
    }

    def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
                          uom=False, qty_uos=0, uos=False, name='', partner_id=False,
                          lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False,image_small=False, context=None):
        context = context or {}

        res = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty=qty,
                                                             uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
                                                             lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)

        product_obj = self.pool.get('product.product')
        product_obj = product_obj.browse(cr, uid, product, context=context)

        res['value'].update({'image_small': product_obj.image_small or False})
        return res


sale_order_line()
class sale_order(osv.Model):
    _name = 'sale.order'
    _inherit = 'sale.order'

    def _prepare_order_line_move(self, cr, uid, order, line, picking_id, date_planned, context=None):
        res = super(sale_order, self)._prepare_order_line_move(cr, uid, order=order, line=line, picking_id=picking_id, date_planned=date_planned, context=context)
        res['image_small'] = line.image_small
        return res

sale_order()
view_list.js:
openerp.nfx_product_image = function(openerp) {
    var _t = openerp.web._t;

    openerp.web.list.Binary.include({
        placeholder: "/web/static/src/img/placeholder.png",
        _format: function (row_data, options) {
            var value = row_data[this.id].value;
            var download_url;
            if (value && value.substr(0, 10).indexOf(' ') == -1) {
                download_url = "data:application/octet-stream;base64," + value;
            } else {
                download_url = this.placeholder;
            }

            return _.template("", {
                href: download_url,
            });
        }

    });
}
http://help.openerp.com/question/483/openerp-7-how-can-i-add-images-in-saleoderline-or-in-treeview-generally/
linkdeleteflag offensiveedit

No comments: