库存明细报表的实现
- 
OE当前的功能里,查询库存要么是所有产品所有库位一张表,要么是一个产品在不同库位的数量分布,无法在一张表上实现多个产品多个库位的明细。 
 于是就有了如下的代码:# -*- coding: utf-8 -*-<br />from openerp.osv import osv,fields<br />from lxml import etree<br />from openerp import tools<br /><br /><br />class product_product(osv.osv):<br />    _inherit = "product.product"<br /><br />    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):<br />        res = super(product_product, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)<br />        if context.get('inventory_list') == True and view_type == 'tree':<br />            loc_obj = self.pool.get('stock.location')<br />            locations = loc_obj.browse(cr, uid, loc_obj.search(cr, uid, [('child_ids', '=', False), ('usage', '=', 'internal')]))<br />            view = etree.fromstring(res['arch'])<br />            fields_list = res['fields']<br />            for location in locations:<br />                fields_list[tools.ustr(location.id)+'-real'] = {'type': 'float', 'string': location.name+'-实际库存'}<br />                fields_list[tools.ustr(location.id)+'-virtual'] = {'type': 'float', 'string': location.name+'-账面库存'}<br />                etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-real'})<br />                etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-virtual'})<br />            res['arch'] = etree.tostring(view)<br />        return res<br /><br />    def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):<br />        result = super(product_product, self).read(cr, uid, ids, fields=fields, context=context)<br />        if context.get('inventory_list') == True:<br />            locations = []<br />            for field in fields:<br />                s = field.split('-')<br />                if len(s) == 2:<br />                    if s[1] == 'real':<br />                        locations.append(int(s[0]))<br />            i = 0<br />            for id in ids:<br />                for location in locations:<br />                    context['location'] = location<br />                    val = super(product_product, self).read(cr, uid, id, ['qty_available', 'virtual_available'], context, )<br />                    result[i][tools.ustr(location)+'-real'] = val['qty_available']<br />                    result[i][tools.ustr(location)+'-virtual'] = val['virtual_available']<br />                i += 1<br />        return result
 实现的效果附件
- 
能否讲解下怎么用呢 
- 
后台代码在附件中。说明下,前台显示的二级表头不是我做的,不能上传,见谅。 
 目前这个版本的效率不高,希望高手能优化下。
 模块设计思路:
 1、库存明细表是通过 fields_view_get 动态生成的。
 2、不同库位的库存根据系统stock里product.product 中的字段 qty_available,virtual_available 读出。这两个字段的值是依据context里不同库位读出。我是通过这个功能循环产品id和库位id读出的。(这个方法挺笨的)
 3、功能入口通过向导进入。向导生成的菜单如下图:
 [attachimg=2]
 如果安装出错,可能需要把 product.py里 fields_view_get()中下几句代码:
 etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-real', 'th_string':location.name, 'colspan':'2'})
 etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-virtual','colspan':'1'})
 etree.SubElement(view, 'field', {'name': 'qty_available', 'string': u'实际库存', 'th_string': u'汇总', 'colspan':'2'})
 etree.SubElement(view, 'field', {'name': 'virtual_available', 'string': u'账面库存', 'colspan':'1'})
 中的'th_string' 和'colspan' 标签去掉。