This question has been flagged
1 Reply
3963 Views

I'm new in Odoo and Phyton. I've just develop a module to extend CRM, adding products in opportunities.

I used function fields to calculate price vs. quantity on opportunities lines (products). Now I want to sumarize all lines and put on opportunity's planned_revenue field. But I don't know what to put in this FOR statment. This line rises an error:  for line in x_crm_lead.crm_lead_line: "crm_lead.py", line 13, in _amount_all AttributeError: type object 'x_crm_lead' has no attribute 'crm_lead_line'

There is my two classes:

class x_crm_lead(osv.Model):
    _name = 'crm.lead'
    _description = 'CRM Lead Extended'
    _inherit = 'crm.lead'

    def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for lead in self.browse(cr, uid, ids, context=context):
            res[lead.id] = {}
            val = 0.0
            for line in x_crm_lead.crm_lead_line:
                val += line.price_subtotal
            res[line.id]= val
        return res

    _columns = {
        'planned_revenue': fields.function(_amount_all,string='Receita Esperada',type='float',store=True),
        'crm_lead_line': fields.one2many('crm.lead.line','crm_lead_id','Produto da Oportunidade')
    }

class crm_lead_line(osv.Model):
    _name = 'crm.lead.line'
    _description = 'CRM Lead Line'

    def _amount_line(self, cr, uid, ids, name, arg, context=None):
        res = {}
        for record in self.browse(cr, uid, ids, context):
            res[record.id] = float(record.price_unit) * float(record.quantity)
        return res


    _columns = {
        'crm_lead_id': fields.many2one('crm.lead','Oportunidade',ondelete='cascade'),
        'product_id': fields.many2one('product.product','Produto'),
        'description': fields.text('Descricao'),
        'quantity': fields.float('Quantidade',digits=(12,2)),
        'price_unit': fields.float('Preco Unitario',digits=(12,2)),
        'price_subtotal': fields.function(_amount_line,string='Subtotal',type='float',store=True),
    }

Thank you in advance!

Avatar
Discard
Best Answer

Try this method:

def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for lead in self.browse(cr, uid, ids, context=context):
            val = 0.0
            for line in lead.crm_lead_line:
                val += line.price_subtotal
            res[lead.id]= val
        return res

The outer loop iterates over all selected lead records, while the inner loop iterates over all lines for each lead.

Also think about renaming your filed in crm_lead_line_ids. This is more intuitive.

EDIT:
If you change the field name, then you have to change the code accordingly:

for line in lead.crm_lead_line:

to:

for line in lead.crm_lead_line_ids:

 

Hope it helps.

Regards.

Avatar
Discard
Author

Hi René, thank for help. I made the changes you suggested: class x_crm_lead(osv.Model): _name = 'crm.lead' _description = 'CRM Lead Extended' _inherit = 'crm.lead' def _amount_all(self, cr, uid, ids, field_name, arg, context=None): res = {} for lead in self.browse(cr, uid, ids, context=context): val = 0.0 for line in lead.crm_lead_line: val += line.price_subtotal res[lead.id]= val return res _columns = { 'planned_revenue': fields.function(_amount_all,string='Receita Esperada',type='float',store=True), 'crm_lead_line_ids': fields.one2many('crm.lead.line','crm_lead_id','Produto da Oportunidade') } But still rises an error: "crm_lead.py", line 13, in _amount_all AttributeError: type object 'crm_lead_line' has no attribute 'crm_lead_id' What is wrong here?

Edited my answer.

Author

Thank you René! The error was occurring because of a missing coma, at second column of x_crm_lead class.