1
votes

I've been trying to copy over certain fields from the hr_expenses form into my custom module, purchase_orders.

The below method was written for me to do this in the extended hr_expenses.py file:

def po_generator(self, cr, uid, ids, arg, context=None):
            res = {}

            for expense in self.browse(cr, uid, ids, context=context):
                expense_line = self.pool.get('hr.expense.line')
                purchase_orders = self.pool.get('purchase.orders')

                for line in expense.line_ids:
                    expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context)
                    # pdb.set_trace()
                    purchase_orders.create(cr, uid,
                        {
                            'submitted_employee_name':expense.employee_id,
                            'dateofexpense':expense_line.date_value,
                            'project_id_expense':expense_line.project_id,
                            'Description':expense_line.name,
                            'netamount':expense_line.product_exc_VAT,
                            'raised_employee_name':expense.employee_id,
                            'project_id_account_type':expense_line.project_id_account_type,
                        },
                        context
                    )

            return res  

However, when I tru to execute this piece of code, OpenERP gives me an 'AttributeError' with the traceback:

File "/opt/openerp/custom_modules/legacy_expense_po/legacy_expense.py", line 121, in po_generator 'dateofexpense':expense_line.date_value, AttributeError: 'hr.expense.line' object has no attribute 'date_value'

I'm presuming this has something to do with the line_ids field in the Expenses form. I'm trying to get each row in the line_ids and enter these as new records in the purchase_orders table.

Thanks.

1
expense line is an object, you cant get value from object by giving dot with fieldNameBaiju KS

1 Answers

1
votes

@Sagar Mohan

You are doing vary basic mistake here.

You are already looping on browse record by saying statement

 for line in expense.line_ids:

When you say expense.line_ids this will already pull all related o2m record you don't need to search again by writing statement expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context) and you must know that search in v7 return then only integer id of the matching ids not recordset or browse record. Correct code is :

 def po_generator(self, cr, uid, ids, arg, context=None):
        res = {}
        purchase_orders = self.pool.get('purchase.orders')
        for expense in self.browse(cr, uid, ids, context=context):
            expense_line = self.pool.get('hr.expense.line')
            purchase_orders = self.pool.get('purchase.orders')
            for line in expense.line_ids:
                purchase_orders.create(cr, uid,
                    {
                        'submitted_employee_name':line.employee_id.name,
                        'dateofexpense': line.date_value,
                        'project_id_expense': line.project_id.id,
                        'Description': line.name,
                        'netamount': line.product_exc_VAT,
                        'raised_employee_name': expense.employee_id.name,
                        'project_id_account_type': line.project_id_account_type,
                    },
                    context
                )
        return res

This code might need correction based on your need but you see that line is direct browse variable so you just need to . and access the field.

and one more note here that you never use self.pool.get inside loop that make your code heavy.

Thanks