Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

库存明细报表的实现



  • OE当前的功能里,查询库存要么是所有产品所有库位一张表,要么是一个产品在不同库位的数量分布,无法在一张表上实现多个产品多个库位的明细。<br />于是就有了如下的代码:<br />[code]# -- 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[/code]<br /><br />实现的效果附件<br /><br /><br />



  • OE当前的功能里,查询库存要么是所有产品所有库位一张表,要么是一个产品在不同库位的数量分布,无法在一张表上实现多个产品多个库位的明细。<br />于是就有了如下的代码:<br />[code]# -- 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[/code]<br /><br />实现的效果附件<br /><br /><br />



  • 谢谢分享, <br /><br />又学了一招



  • 这个是怎么用啊。。没看懂



  • 能否讲解下怎么用呢



  • 后台代码在附件中。说明下,前台显示的二级表头不是我做的,不能上传,见谅。<br />目前这个版本的效率不高,希望高手能优化下。<br /><br />模块设计思路:<br /><br />1、库存明细表是通过 fields_view_get 动态生成的。<br />2、不同库位的库存根据系统stock里product.product 中的字段 qty_available,virtual_available 读出。这两个字段的值是依据context里不同库位读出。我是通过这个功能循环产品id和库位id读出的。(这个方法挺笨的)<br />3、功能入口通过向导进入。向导生成的菜单如下图:<br />[attachimg=2]<br /><br />如果安装出错,可能需要把 product.py里 fields_view_get()中下几句代码:<br />etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-real', 'th_string':location.name, 'colspan':'2'})<br />etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-virtual','colspan':'1'})<br />etree.SubElement(view, 'field', {'name': 'qty_available', 'string': u'实际库存', 'th_string': u'汇总', 'colspan':'2'})<br />etree.SubElement(view, 'field', {'name': 'virtual_available', 'string': u'账面库存', 'colspan':'1'})<br />中的'th_string' 和'colspan' 标签去掉。<br /><br />



  • 感谢分享!


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待