Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
4031 Tampilan

I want to put the employee name to the 'account.analytic.line' model.

For that I need to go to 'hr.payslip' and get the "number" field value. With this value I need to compare this value to the 'account.analytic.line' "ref" field value. So that I know to wich record what employee is set.

step:


compare hr.payslip(number) with account.analytic.line(number)

Then get the employee name and put that in the account.analytic.line(testing) - new field.




This is my code:

from openerp import models, fields, api, osv
from openerp.http import requestfrom openerp import SUPERUSER_ID

class account_analytic_line(models.Model):
    _inherit = ['account.analytic.line']
     testing = fields.Integer(compute="employee")

     @api.one
     def employee(self):
         cr, uid, context, pool = request.cr, request.uid, request.context, request.cr
         model_obj = self.pool.get('hr.payslip')
         test = self.env['account.analytic.line']
         #record = model_obj.browse(cr, uid, your id, context = context)
         rec_ids = model_obj.search(cr, uid, [(test.ref, '=', 'number')], context=context)
         for record in model_obj.browse(cr, uid, rec_ids, context=context):
             record.ref
             print record.ref


The final result should be, that the employee should be on the account.analytic.line.


Avatar
Buang
Jawaban Terbai

Try this:


class account_analytic_line(models.Model):
    _inherit = ['account.analytic.line']   
testing = fields.Char(compute='_get_employee')
   
@api.depends('ref')
def _get_employee(self):
        for record in self:
            payslip_obj = self.env['hr.payslip']
payslip = payslip_obj.search([('number', '=', record.ref)], limit=1)
if payslip:
                record.testing = payslip.employee_id.name
else:
                record.testing = False


Avatar
Buang
Penulis

amazing thank you !

Penulis

how can I put this on the tree view ?

Penulis

ValueError

Expected singleton: account.analytic.line(3713, 3714, 3715, 3755, 3873, 3746, 3747, 3748, 3749, 3750, 3879, 3752, 3881, 3882, 3883, 3837, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3875, 3876, 3877, 3835, 3878, 3751, 3831, 3880, 3795, 3829, 3830, 3753, 3832, 3833, 3834, 3874, 3836, 3754, 3838, 3839)

I changed the code:

record.ref (before was self.ref that was wrong)

record.testing (before was self.testing that was wrong)

Penulis

thank you Martin! what was the problem before?

self is the entire recordset, in tree view are all the lines. In form view worked because it´s only one record. The correct way is to define a loop for each record (for record in self:) and calculate it´s value. The previous answer was wrong because in your tree view self was account.analytic.line(3713, 3714, 3715, 3755, 3873,...) and you can´t ask for a value of multiple records at one time (self.ref).

Post Terkait Replies Tampilan Aktivitas
1
Sep 16
4316
0
Des 16
3005
1
Jun 24
1777
0
Mar 23
2402
1
Des 20
2445