0
votes

Really struggling with this one:

I have inherited from stock.picking.in and have added a few columns. I then added a function field.

In the function that the function field refers to, it works if I do not use any attribute from the stock.picking.in object. The moment I use any value from the object, it starts giving 'AttributeError: ' and some attribute at random. It doesn't specify any other reasons or causes.

Code:

class stock_picking_custom(osv.osv):
_name = 'stock.picking.in'
_inherit = 'stock.picking.in'
_table = "stock_picking"

def calc_royalty(self, cr, uid, ids, field_name, arg, context=None):
    if not ids: return {}
    res = {}
    for line in self.browse(cr, uid, ids, context=context):
        res[line.id] = 0 #line.royalty_rate * line.loading_netweight

    return res   

_columns = {
            'loading_netweight': fields.float('Net weight at loading', digits=(16,2), help="Net weight at loading (interior weight)"),
            'royalty_date': fields.date('Royalty Issue date'),
            'royalty_number' : fields.char('Royalty Number', size=64),
            'royalty_rate' : fields.float('Royalty Rate (p. ton)', digits=(16,2)),
            'royalty_amount' : fields.function(calc_royalty, type='float', digits=(16,2), string='Royalty Amount', store=True, select=True)
            }
stock_picking_custom()

I have commented out the line that I want to use. The moment I put this line back in the code, it would give attribute error on royalty_date (for example) which is not even mentioned in the function.

Please guide.

EDIT: I tried the exact same code with purchase.order and it works perfectly. What is different about stock.picking.in?

Thanks

2

2 Answers

0
votes

Ok, found the answer in stock module in delivery addon. So this is a framework limitation issue related to inheritance order etc.

Sharing here in case someone ends up in a similar situation.

To solve, I repeated the same fields in stock.picking and stock.picking.in. Then I called the calc function of the picking class from the picking.in class.

Code:

class stock_picking_custom(osv.osv):
_name = 'stock.picking'
_inherit = 'stock.picking'

def calc_royalty(self, cr, uid, ids, field_name, arg, context=None):
    if not ids: return {}
    res = {}

    for line in self.browse(cr, uid, ids, context=context):
        res[line.id] = line.royalty_rate * line.loading_netweight

    return res   

_columns = {
            'loading_netweight': fields.float('Net weight at loading', digits=(16,2), help="Net weight at loading (interior weight)"),
            'royalty_date': fields.date('Royalty Issue date'),
            'royalty_number' : fields.char('Royalty Number', size=64),
            'royalty_rate' : fields.float('Royalty Rate (p. ton)', digits=(16,2)),
            'royalty_amount' : fields.function(calc_royalty, type='float', digits=(16,2), string='Royalty Amount', store=True, select=True)
            }

stock_picking_custom()

class stock_picking_in_custom(osv.osv):
_name = 'stock.picking.in'
_inherit = 'stock.picking.in'
_table = "stock_picking"

def calc_royalty(self, cr, uid, ids, field_name, arg, context=None):
    return self.pool.get('stock.picking').calc_royalty(cr,uid,ids,field_name,arg,context=context)

_columns = {
            'loading_netweight': fields.float('Net weight at loading', digits=(16,2), help="Net weight at loading (interior weight)"),
            'royalty_date': fields.date('Royalty Issue date'),
            'royalty_number' : fields.char('Royalty Number', size=64),
            'royalty_rate' : fields.float('Royalty Rate (p. ton)', digits=(16,2)),
            'royalty_amount' : fields.function(calc_royalty, type='float', digits=(16,2), string='Royalty Amount', store=True, select=True)
             }
 stock_picking_in_custom()
0
votes

I did not get much time to spend on it but I came to know that line is coming with stock.picking.in object and fields, you defined, are stored in stock_picking table that's why it may going to search that field with stock.picking.in, not getting and error is coming. There may be issue with fields defined in object and table, but not sure.