更新完成odoo的库存移动记录

时间:2016-09-09 19:34:38

标签: openerp odoo-8

我在odoo 8工作,我必须更新已完成的库存移动。即假设移动完成并且状态变为“完成”但输入的数量不正确,因此要纠正它,我需要减少移动的数量。 我正在尝试更新数量,但它给出了错误:

_('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).'))

我正在尝试如下:

move.write({'product_uos_qty':correct_qty})

我也试过这个:

move.sudo().write({'product_uos_qty':correct_qty})

总是我得到同样的错误。

请帮助

谢谢,

1 个答案:

答案 0 :(得分:0)

最简单的方法是不使用安全性或删除Odoo实施的约束或限制是使用sql。只需编写完成任务的原始sql,然后在上面尝试写入的地方执行它。不确定这是不是最好的方法。但是在您的插件中,您可以继承stock.move并完全覆盖该函数。或者只需手动编辑文件并重新启动服务器。不确定它可能导致的其他问题,但它应该让你写到现场。

sql = ".....YOUR SQL HERE"
self.env.cr.execute(sql)
self.env.cr.commit()

覆盖addons / stock / stock.py

中的写入功能
def write(self, cr, uid, ids, vals, context=None):
    if context is None:
        context = {}
    if isinstance(ids, (int, long)):
        ids = [ids]
    # Check that we do not modify a stock.move which is done
    frozen_fields = set(['product_qty', 'product_uom', 'product_uos_qty', 'product_uos', 'location_id', 'location_dest_id', 'product_id'])
    for move in self.browse(cr, uid, ids, context=context):
        if move.state == 'done':


            #if frozen_fields.intersection(vals):
            #    raise osv.except_osv(_('Operation Forbidden!'),
            #        _('Quantities, Units of Measure, Products and Locations cannot be modified on stock moves that have already been processed (except by the Administrator).'))


    propagated_changes_dict = {}
    #propagation of quantity change
    if vals.get('product_uom_qty'):
        propagated_changes_dict['product_uom_qty'] = vals['product_uom_qty']
    if vals.get('product_uom_id'):
        propagated_changes_dict['product_uom_id'] = vals['product_uom_id']
    #propagation of expected date:
    propagated_date_field = False
    if vals.get('date_expected'):
        #propagate any manual change of the expected date
        propagated_date_field = 'date_expected'
    elif (vals.get('state', '') == 'done' and vals.get('date')):
        #propagate also any delta observed when setting the move as done
        propagated_date_field = 'date'

    if not context.get('do_not_propagate', False) and (propagated_date_field or propagated_changes_dict):
        #any propagation is (maybe) needed
        for move in self.browse(cr, uid, ids, context=context):
            if move.move_dest_id and move.propagate:
                if 'date_expected' in propagated_changes_dict:
                    propagated_changes_dict.pop('date_expected')
                if propagated_date_field:
                    current_date = datetime.strptime(move.date_expected, DEFAULT_SERVER_DATETIME_FORMAT)
                    new_date = datetime.strptime(vals.get(propagated_date_field), DEFAULT_SERVER_DATETIME_FORMAT)
                    delta = new_date - current_date
                    if abs(delta.days) >= move.company_id.propagation_minimum_delta:
                        old_move_date = datetime.strptime(move.move_dest_id.date_expected, DEFAULT_SERVER_DATETIME_FORMAT)
                        new_move_date = (old_move_date + relativedelta.relativedelta(days=delta.days or 0)).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
                        propagated_changes_dict['date_expected'] = new_move_date
                #For pushed moves as well as for pulled moves, propagate by recursive call of write().
                #Note that, for pulled moves we intentionally don't propagate on the procurement.
                if propagated_changes_dict:
                    self.write(cr, uid, [move.move_dest_id.id], propagated_changes_dict, context=context)
    return super(stock_move, self).write(cr, uid, ids, vals, context=context)