OpenERP 相同產品相同貨位write方法重載優化

         聲明:本篇文章只是本人的一個思考過程的記錄,對於個人來說很有意義。讀者覺得凌亂,請勿噴,

         方法一:上架相同產品,相同貨物不能合併

 def write(self,cr,uid,ids,vals,context=None):
        vals['operator_time']=time.strftime('%Y-%m-%d %H:%M:%S')
        vals['operator']=uid
        #raise except_osv(_('Warning'),_(vals))
        obj = super(stock_picking_apply,self).write(cr,uid,ids,vals,context=context)

        if context.has_key('flag'):
            if context['flag'] == 'cancel' or context['flag'] == 'close':
                return obj

        apply_obj = self.browse(cr,uid, ids)[0]
        current_apply_id = apply_obj.id
        current_supplier_id = apply_obj.supplier_id

        #product detail
        move_lines_applys = {}

        i = 1
        temp = []
        for move_lines_apply in apply_obj.move_lines_apply: key = move_lines_apply['product_id']
            #前面是+的爲做的判斷提示
+            if i == 1:
+                temp.append(key.id)
+                i = i + 1
+            else:
+                if key.id in temp:
+                    raise except_osv(_('提示'),_('不能添加已有的商品!'))
+                else:
+                    temp.append(key.id)
+                    i = i + 1

            value = move_lines_apply['product_qty'] move_lines_applys[key] = move_lines_applys.get(key,0) + value #in hand product detail move_lines_apply_hands = {} for move_lines_apply_hand in apply_obj.move_lines_apply_hand: key = move_lines_apply_hand['product_id'] value = move_lines_apply_hand['product_qty_fact'] move_lines_apply_hands[key] = move_lines_apply_hands.get(key,0) + value move_hand_obj = self.pool.get('stock.picking.apply.move.hand') hand_id = move_lines_apply_hand['id'] hand_state = move_lines_apply_hand['state'] if hand_state != 'finshed': move_hand_obj.write(cr,uid,hand_id,{'state':'finshed'},context=context) for key in move_lines_applys: val = move_lines_applys[key] hval = move_lines_apply_hands.get(key,0) #if hval == 0: # raise except_osv(_('提示'),_('入庫信息不能爲空!')) if val < hval: raise except_osv(_('提示'),_('不能超過預期數量!')) elif val > hval: #update fact quantity self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context) #raise except_osv(_('提示'),_('還沒有達到預期數量!')) return super(stock_picking_apply,self).write(cr,uid,ids,{'state':'underd_way'},context=context) else: self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context) #raise except_osv(_('提示'),_('提交成功!')) #update product quantity product_obj = self.pool.get('product.product') product_res = product_obj.write(cr,uid,key.id,{'total_quantity':hval}) if not product_res: raise except_osv(_('提示'),_('系統異常,請重新操作3!')) self.move_hand_finshed(cr,uid,ids,context=context) return obj

         方法二:上架相同產品,相同貨位,刪除已有的產品,刪除手持明細有問題

def write(self,cr,uid,ids,vals,context=None):
        obj = super(stock_picking_apply,self).write(cr,uid,ids,vals,context=context)

        if context.has_key('flag'):
            if context['flag'] == 'cancel' or context['flag'] == 'close':
                return obj

        apply_obj = self.browse(cr,uid, ids)[0]
        current_apply_id = apply_obj.id
        current_supplier_id = apply_obj.supplier_id

        #product detail
        move_lines_applys = {}
        for move_lines_apply in apply_obj.move_lines_apply:
            key = move_lines_apply['product_id']
            value = move_lines_apply['product_qty']
            move_lines_applys[key] = move_lines_applys.get(key,0) + value

        #in hand product detail
        move_lines_apply_hands = {}
        for move_lines_apply_hand in apply_obj.move_lines_apply_hand:
            key = move_lines_apply_hand['product_id']
            value = move_lines_apply_hand['product_qty_fact']
            move_lines_apply_hands[key] = move_lines_apply_hands.get(key,0) + value

        for key in  move_lines_applys:
            val = move_lines_applys[key]
            #merge data
            before_merge_obj = self.pool.get('stock.picking.apply.move')
            b_id = before_merge_obj.search(cr, uid, [('jp_apply_picking_id','=',current_apply_id),('product_id','=',key.id)])
            leave = before_merge_obj.browse(cr,uid, b_id[0])
            #raise except_osv(_('ss'),_(leave))
            value = {
                'product_number': leave.product_number,
                'product_id': key.id,
                'ref_number':leave.ref_number,
                'specifications': leave.specifications,
                'product_uom': leave.product_uom.id,
                'product_qty': val,
                'create_date': leave.create_date,
                'jp_apply_picking_id':current_apply_id
            }
            #raise except_osv(_('ss'),_(value))
            #delete_res = before_merge_obj.unlink(cr, uid, [b_id], context=context)
            for id in b_id:
                #raise except_osv(_('ss'),_(id))
                delete_res = cr.execute('delete from stock_picking_apply_move where id = {}'.format(id))
                #raise except_osv(_('提示'),_(delete_res))
            #if not delete_res:
            #    raise except_osv(_('提示'),_('系統異常,請重新操作1!'))
            c_res = before_merge_obj.create(cr, uid, value, context=context)
            if not c_res:
                raise except_osv(_('提示'),_('系統異常,請重新操作2!'))
            hval = move_lines_apply_hands.get(key,0)
            #if hval == 0:
            #    raise except_osv(_('提示'),_('入庫信息不能爲空!'))
            if val < hval:
                raise except_osv(_('提示'),_('不能超過預期數量!'))
            elif val > hval:
                #update fact quantity
                self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context)
                #raise except_osv(_('提示'),_('還沒有達到預期數量!'))
                return super(stock_picking_apply,self).write(cr,uid,ids,{'state':'underd_way'},context=context)
            else:
                self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context)
                #raise except_osv(_('提示'),_('提交成功!'))
                #update product quantity
                product_obj = self.pool.get('product.product')
                product_res = product_obj.write(cr,uid,key.id,{'total_quantity':hval})
                if not product_res:
                    raise except_osv(_('提示'),_('系統異常,請重新操作3!'))
                self.move_hand_finshed(cr,uid,ids,context=context)

        return obj

 中間的一個思考過程,有問題:

vals['operator_time']=time.strftime('%Y-%m-%d %H:%M:%S')
        vals['operator']=uid
        #raise except_osv(_('Warning'),_(vals))
        _logger.error('++++++++++++++++++++++++++++++++++++++++Start')
        obj = super(stock_picking_apply,self).write(cr,uid,ids,vals,context=context)

        if context.has_key('flag'):
            if context['flag'] == 'cancel' or context['flag'] == 'close':
                return obj

        apply_obj = self.browse(cr,uid, ids)[0]
        current_apply_id = apply_obj.id
        current_supplier_id = apply_obj.supplier_id

        #New Add Begin
        old_ids = []
        new_move_lines_hand = {}
        move_lines_hand_total_all_location = {}
        move_lines_hand_total_per_location = {}
        for move_line_hand in apply_obj['move_lines_apply_hand']:
            old_ids.append(move_line_hand.id)

            product_id = move_line_hand['product_id']
            location_id = move_line_hand['relation_location']
            value = move_line_hand['product_qty_fact']

            new_value_all_location = move_lines_hand_total_all_location.get(str(product_id),0) + value
            move_lines_hand_total_all_location[str(product_id)] = new_value_all_location

            new_value_per_location = move_lines_hand_total_per_location.get(str(product_id)+'_'+str(location_id),0) + value
            move_lines_hand_total_per_location[str(product_id)+'_'+str(location_id)] = new_value_per_location

            move_line_hand['product_qty_fact'] = new_value_per_location
            move_line_hand['id'] = None

            new_move_lines_hand[str(product_id)+'_'+str(location_id)] = [0,False,move_line_hand]

        apply_obj['move_lines_apply_hand'] = new_move_lines_hand.values()
        _logger.error('++++++++++++++++++++++++++++++++++++++++BEFORE')
        self.pool.get('stock.picking.apply.move.hand').unlink(cr, uid, old_ids, context=context)
        _logger.error('++++++++++++++++++++++++++++++++++++++++After')
        super(stock_picking_apply,self).write(cr,uid,ids,apply_obj,context=context)
        _logger.error('++++++++++++++++++++++++++++++++++++++++END')
        #new add end

        ##product detail
        #move_lines_applys = {}
        #i = 1
        #temp = []
        #for move_lines_apply in apply_obj.move_lines_apply:
        #    key = move_lines_apply['product_id']
        #    value = move_lines_apply['product_qty']
        #    move_lines_applys[key] = move_lines_applys.get(key,0) + value
        #
        ##in hand product detail
        #move_lines_apply_hands = {}
        #for move_lines_apply_hand in apply_obj.move_lines_apply_hand:
        #    key = move_lines_apply_hand['product_id']
        #    value = move_lines_apply_hand['product_qty_fact']
        #    move_lines_apply_hands[key] = move_lines_apply_hands.get(key,0) + value
        #    move_hand_obj = self.pool.get('stock.picking.apply.move.hand')
        #    hand_id = move_lines_apply_hand['id']
        #    hand_state = move_lines_apply_hand['state']
        #    if hand_state != 'finshed':
        #        move_hand_obj.write(cr,uid,hand_id,{'state':'finshed'},context=context)
        #
        #
        #
        #
        #for key in  move_lines_applys:
        #    val = move_lines_applys[key]
        #    hval = move_lines_apply_hands.get(key,0)
        #    #if hval == 0:
        #    #    raise except_osv(_('提示'),_('入庫信息不能爲空!'))
        #    if val < hval:
        #        raise except_osv(_('提示'),_('不能超過預期數量!'))
        #    elif val > hval:
        #        #update fact quantity
        #        self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context)
        #        #raise except_osv(_('提示'),_('還沒有達到預期數量!'))
        #        return super(stock_picking_apply,self).write(cr,uid,ids,{'state':'underd_way'},context=context)
        #    else:
        #        self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context)
        #        #raise except_osv(_('提示'),_('提交成功!'))
        #        #update product quantity
        #        product_obj = self.pool.get('product.product')
        #        product_res = product_obj.write(cr,uid,key.id,{'total_quantity':hval})
        #        if not product_res:
        #            raise except_osv(_('提示'),_('系統異常,請重新操作!'))
        #        self.move_hand_finshed(cr,uid,ids,context=context)

  最終完美的解決方案:

def write(self,cr,uid,ids,vals,context=None):
        vals['operator_time']=time.strftime('%Y-%m-%d %H:%M:%S')
        vals['operator']=uid
        obj = super(stock_picking_apply,self).write(cr,uid,ids,vals,context=context)
        if context.has_key('flag'):
            if context['flag'] == 'cancel' or context['flag'] == 'close':
                return obj

        apply_obj = self.browse(cr,uid, ids)[0]
        current_apply_id = apply_obj.id

        move_lines_applys = {}
        for move_lines_apply in apply_obj.move_lines_apply:
            key = move_lines_apply['product_id']
            value = move_lines_apply['product_qty']
            move_lines_applys[key] = move_lines_applys.get(key,0) + value

        new_move_lines_hand = {}
        move_lines_hand_total_per_porduct = {}
        move_lines_hand_total_per_location = {}
        for move_line_hand in apply_obj['move_lines_apply_hand']:
            product_id = move_line_hand['product_id']
            location_id = move_line_hand['relation_location']
            value = move_line_hand['product_qty_fact']

            new_value_all_location = move_lines_hand_total_per_porduct.get(str(product_id),0) + value
            move_lines_hand_total_per_porduct[str(product_id)] = new_value_all_location

            new_value_per_location = move_lines_hand_total_per_location.get(str(product_id)+'_'+str(location_id),0) + value
            move_lines_hand_total_per_location[str(product_id)+'_'+str(location_id)] = new_value_per_location

            key = str(product_id)+'_'+str(location_id)

            #If Same Product In Same Location,Then remove current record;Else Update record
            if new_move_lines_hand.has_key(key):
                obj = [2, move_line_hand['id'], False]
                new_move_lines_hand[str(move_line_hand['id'])] = obj
                new_move_lines_hand[key][2]['product_qty_fact'] = new_value_per_location
            else:
                obj = [1, move_line_hand['id'], {'product_qty_fact': new_value_per_location}]
                new_move_lines_hand[key] = obj

        vals['move_lines_apply_hand'] = new_move_lines_hand.values()

        super(stock_picking_apply,self).write(cr,uid,ids,vals,context=context)

        i = 0
        for key in  move_lines_applys:
            val = move_lines_applys[key]
            hval = move_lines_hand_total_per_porduct.get(str(key),0)

            if val < hval:
                raise except_osv(_('提示'),_('不能超過預期數量!'))
            elif val > hval:
                #update fact quantity
                self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context)
                super(stock_picking_apply,self).write(cr,uid,ids,{'state':'underd_way'},context=context)
            else:
                self.update_fact_quantity(cr, uid, current_apply_id, key.id, hval, context=context)
                #update product quantity
                product_obj = self.pool.get('product.product')
                product_res = product_obj.write(cr,uid,key.id,{'total_quantity':hval})
                if not product_res:
                    raise except_osv(_('提示'),_('系統異常,請重新操作!'))
                i += 1
                #All Product have finshed then change state finshed
                if i== len(move_lines_applys):
                    self.move_hand_finshed(cr,uid,ids,context=context)
        return obj


    #Move To
    def move_hand_finshed(self,cr,uid,ids,context=None):
        super(stock_picking_apply,self).write(cr,uid,ids,{'state':'finshed'},context=context)

    def update_fact_quantity(self, cr, uid, aid, pid, value, context=None):
        picking_move_obj = self.pool.get('stock.picking.apply.move')
        update_obj_ids = picking_move_obj.search(cr, uid, [('jp_apply_picking_id','=',aid),('product_id','=',pid)])
        if update_obj_ids:
            update_obj_id = update_obj_ids[0]
            update_res = picking_move_obj.write(cr,uid,update_obj_id,{'product_qty_fact':value})
            if not update_res:
                raise except_osv(_('提示'),_('系統異常,請重新操作!'))
            else:
                return True



相同商品數量累加:
def write(self,cr,uid,ids,vals,context=None):
        obj = super(stock_picking_apply,self).write(cr,uid,ids,vals,context=context)
        apply_obj = self.browse(cr,uid, ids)[0]
        move_lines_applys = {}


        for move_lines_apply in apply_obj.move_lines_apply:
            _logger.warning('---------------%s',type(apply_obj))
            _logger.warning('++++++++++%s',move_lines_apply)
            _logger.warning('++++++++++%s',type(apply_obj['move_lines_apply']))
            key = move_lines_apply['product_id']
            value = move_lines_apply['product_qty']
            move_lines_applys[key] = move_lines_applys.get(key,0) + value


        move_lines_apply_hands = {}
        for move_lines_apply_hand in apply_obj['move_lines_apply_hand']:
            key = move_lines_apply_hand['product_id']
            value = move_lines_apply_hand['product_qty']
            move_lines_apply_hands[key] = move_lines_apply_hands.get(key,0) + value


        for key in  move_lines_applys:
            val = move_lines_applys[key]
            hval = move_lines_apply_hands.get(key,0)
            if val != hval:
                return obj


        self.move_hand_finshed(cr,uid,ids,context=context)
        return obj


發佈了62 篇原創文章 · 獲贊 33 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章