# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2014-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################
from odoo import models, fields, api
from odoo.tools.translate import _
from odoo.osv.orm import setup_modifiers

from lxml import etree

class ResPartner(models.Model):
    _inherit = "res.partner"

    plafond = fields.Float('Plafond', track_visibility='onchange')

    @api.model
    def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False):
        context = self._context or {}
        res = super(ResPartner, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        if view_type == 'form':
            if self.env.user.has_group('sale_plafond.group_sale_allow_plafond'):
                doc = etree.XML(res['arch'])
                for node in doc.xpath("//field[@name='plafond']"):
                    node.set('readonly', "0")
                    setup_modifiers(node, res['fields']['plafond'])
                    res['arch'] = etree.tostring(doc)
        return res
