This question has been flagged
2 Replies
4508 Views

In fact, I defined a function on "stock.picking", computes result for a functionnal field, on "stock.picking" object. this method browses account.move.line object, with ids of stock picking, is it correct ? 

Here after the code : 

class StockPicking(orm.Model):
    _inherit = 'stock.picking'
    _name = 'stock.picking'

    def _payed(self, cr, uid, ids, payedd, arg, context):

        rs= {}

        for picking in self.browse(cr, uid, ids, context):

            rs[picking.id] = False

            numbl = picking.num_bl
            move_line_obj = self.pool.get('account.move.line')

            for move_line in move_line_obj.browse(cr, uid, ids, context):

                if move_line.move_id.ref == numbl and move_line.reconcile_id:
                    rs[picking.id] = True
        return rs

    _columns = {
        'payedd' : fields.function(_payed, method=True, string='Encaisse', type='boolean'),
    }

 

 

I've got this error :

AttributeError: 'Field move_id not found in browse_record(account.move.line, 20058)'

It seems that when browsing withs ids of stock_picking, it does'nt recognize account_move_line model. If true, is there any workaround to get the field : reference of account_move_line object ?

Avatar
Discard
Best Answer

Your suspicion is correct that it is trying to browse account.move.line (through move_line_obj pool) with stock.picking's ids.

The question you need to ask first is which account.move.line are you going to pull?  The relationship need to be established first.  For example if you want to access related stock.move from stock.picking you can do this:

obj = self.pool.get('stock.picking').browse(cr, uid, ids[0], context=context)

stock_moves = obj.move_lines

Unfortunately specifically for account.move (and account.move.line) the official OpenERP v7 does not provide a definite relationship.  The only "clue" that you might be able to use is that the 'ref' field of account.move.line is filled with the related stock.picking's 'name' field (_create_account_move_line method in odoo/addons/stock/stock.py).  Note that AFAIK the account.move created is in 'draft' state and hence the 'ref' field can still be changed before posted and hence break the "relationship".

Assuming that you want to bet on the 'ref' field, here is the code:

    def _payed(self, cr, uid, ids, payedd, arg, context):

        rs= {}

        move_line_obj = self.pool.get('account.move.line')  # Put this outside of loop so it is not called repeatedly
        for picking in self.browse(cr, uid, ids, context):

            rs[picking.id] = False

            numbl = picking.num_bl

            move_line_ids = move_line_obj.search(cr, uid, [('ref', '=', picking.name)], context=context)
            for move_line in move_line_obj.browse(cr, uid, move_line_ids, context=context):

                if move_line.move_id.ref == numbl and move_line.reconcile_id:
                    rs[picking.id] = True
        return rs

Avatar
Discard
Author

Yes Thanks for answering, moves are posted automatically, on journal configuration , I choosed the option to skype the validation of moves, so I bet on the ref, I used a sql query to get the data from account_move_line, and it works, the way you did it is very intrestring for me, It's very usefull to know how to entertain the "relationship" using the orm, instead of sql queries, wich is a possible solution.

My pleasure.

Best Answer

You browse 'account.move.line' with ids which is the ids of 'stock.picking'?!

Avatar
Discard
Author

Yes, the key solution was to pull the ids of account_move_lines, and do a loop on them. (Ivan answer). Thanks.