0
votes

I was trying to update a employee profile(fill up a one2many tree list in employee profile) like the one in the following image

One2many list tree view

But this is based on the following form,

Assets many2one orm class

Now the above form is of class 'asset.asset' and has one many2one**(Assigned To)** field to 'hr.employee' class. When I click save, particular asset name and few details had to be updated in the one2many tree view of first image.

Following is the modification of code:

asset_asset.py

class asset_asset(osv.osv):
    """
    Assets
    """
    _name = 'asset.asset'
    _description = 'Asset'
    _inherit = ['mail.thread']

    CRITICALITY_SELECTION = [
        ('0', 'General'),
        ('1', 'Important'),
        ('2', 'Very important'),
        ('3', 'Critical')
    ]

    _columns = {
        'name': fields.char('Asset Name', size=64, required=True, translate=True),
        'finance_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','0')]),
        'warehouse_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','1')]),
        'manufacture_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','2')]),
        'maintenance_state_id': fields.many2one('asset.state', 'State', domain=[('team','=','3')]),
        'maintenance_state_color': fields.related('maintenance_state_id', 'state_color', type="selection", selection=STATE_COLOR_SELECTION, string="Color", readonly=True),
        'criticality': fields.selection(CRITICALITY_SELECTION, 'Criticality'),
        'property_stock_asset': fields.property(
          type='many2one',
          relation='stock.location',
          string="Asset Location",
          store=True,
          help="This location will be used as the destination location for installed parts during asset life."),
        'user_id': fields.many2one('hr.employee', 'Assigned to', track_visibility='onchange'),
        'employee_id': fields.many2one('hr.employee', 'Ref'),
        'active': fields.boolean('Active'),
        'profit_id':fields.char('Profit Center', size=64, readonly=True),
        'issue_date':fields.date('Issued On'),
        'return_date':fields.date('Returned On'),
        'test':fields.char('Employee'),
        'asset_number': fields.char('Asset Number', size=64),
        'model': fields.char('Model', size=64),
        'serial': fields.char('Serial no.', size=64),
        'vendor_id':fields.many2one('res.partner', 'Vendor'),
        'manufacturer_id': fields.many2one('res.partner', 'Manufacturer'),
        'start_date': fields.date('Start Date'),
        'purchase_date': fields.date('Purchase Date'),
        'warranty_start_date': fields.date('Warranty Start'),
        'warranty_end_date': fields.date('Warranty End'),
        'maintain_asset': fields.selection([
        ('month', 'Monthly'),
        ('qtr', 'Quarterly'),
        ('half', 'Half Yearly'),
        ('year', 'Yearly'),

        ], 'Maintenance Duration', readonly= False, select=True),
        # image: all image fields are base64 encoded and PIL-supported
        'image': fields.binary("Image",
            help="This field holds the image used as image for the asset, limited to 1024x1024px."),
        'image_medium': fields.function(_get_image, fnct_inv=_set_image,
            string="Medium-sized image", type="binary", multi="_get_image",
            store={
                'asset.asset': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
            },
            help="Medium-sized image of the asset. It is automatically "\
                 "resized as a 128x128px image, with aspect ratio preserved, "\
                 "only when the image exceeds one of those sizes. Use this field in form views or some kanban views."),
        'image_small': fields.function(_get_image, fnct_inv=_set_image,
            string="Small-sized image", type="binary", multi="_get_image",
            store={
                'asset.asset': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
            },
            help="Small-sized image of the asset. It is automatically "\
                 "resized as a 64x64px image, with aspect ratio preserved. "\
                 "Use this field anywhere a small image is required."),
    }

    def _employee_get_asset(self, cr, uid,ids, context=None): 

            for rec in self.browse(cr,uid,ids,context=context):
                if user_id:
                    employee = self.pool.get('hr.employee').browse(cr, uid, user_id, context=context)           
                    proj_obj = self.pool.get('hr.employee.asset')
                    values = {'name': rec.name, 'issue_date': rec.issue_date, 'return_date': rec.return_date}
                    proj_obj.write(cr, user, proj_ids, values)
                    proj_obj.create(cr,uid,{'emp_id':rec.user_id, 'name': rec.name, 'issue_date': rec.issue_date, 'return_date':                         rec.return_date},context=context)

            return True

    _defaults = {
        'active': True,
        'employee_id': _employee_get_asset,
    }

Class for one2many tree view in Employee profile

class hr_employee_asset(osv.osv):
    _name = "hr.employee.asset"
    _columns = {
            'name': fields.char('Asset Name', size=64),
            'issue_date':fields.date('Issued On'),
            'return_date':fields.date('Returned On'),
            'emp_id':fields.many2one('hr.employee','Employee ID'),

    }

Added a one2many field in hr.employee

class hr_employee(osv.osv):
    _inherit="hr.employee"

    _columns={

        'asset_hr':fields.one2many('asset.asset','employee_id','Employee Assets'),
        'asset_ed':fields.one2many('hr.employee.asset','emp_id','Employee ID'),


    }
hr_employee()

Someone please suggest me the right way of doing this as I am totally messed up on this scenario. Any guidance would be really valuable.

1

1 Answers

0
votes

Thanks for providing such detail regarding your situation. I think the problem is related to your default values declaration for employee_id in asset.asset. You reference a variable user_id which I dont see any value assignment for. This should always resolve False which means none of the code in your

if user_id:
    employee = self.pool.get('hr.employee').browse(cr, uid, user_id, context=context)           
    proj_obj = self.pool.get('hr.employee.asset')
    values = {'name': rec.name, 'issue_date': rec.issue_date, 'return_date': rec.return_date}
    proj_obj.write(cr, user, proj_ids, values)
    proj_obj.create(cr,uid,{'emp_id':rec.user_id, 'name': rec.name, 'issue_date': rec.issue_date, 'return_date':                         rec.return_date},context=context)

section should ever be run. I think that you may want to leave this default function out of your code entirely. In your hr_employee class you define a one2many relationship with your asset.asset class. From the employee form, if you add a new asset the employee_id should be assigned automatically and no default value computation should be necessary.

You may want to take a look at some of the logging options available to you. If you import logging you should get a better idea of what is actually happening in your code. These logs will be printed wherever your server logs are being stored. You could place them in your default function to see the value of your variables at any given point in time or simply mark sections of code by calling _logger.info("SECTION 1 STARTED") to make it clear that each portion of your function is running as expected.

import logging
_logger = logging.getLogger(__name__)

Also I think you have an extra class which is confusing the matter. I believe you only need hr_employee and asset.asset. hr_employee will have a one2many relationship with asset.asset and asset.asset will have a many2one relationship with hr_employee. If you do not want all the fields to appear in the hr_employee list view then just select them in your xml like this. Im using asset_ids as I think that is what you should define your one2many as in hr_employee.

<field name="asset_ids">
    <tree>
        <field name="name"/>
        <field name="issue_date"/>
        <field name="return_date"/>
    </tree>
</field>