<commit_before>##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################

from odoo import models, fields, api, _
from odoo.exceptions import UserError


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

    partner_state_enable = fields.Boolean(
        compute='_compute_partner_state_enable',
    )

    partner_state = fields.Selection(
        '_get_partner_states',
        string='Partner State',
        readonly=True,
        required=True,
        default='potential'
    )

    @api.multi
    def _compute_partner_state_enable(self):
        if self.env.user.company_id.partner_state_enable:
            partners = self.filtered(lambda r:
                                     r.commercial_partner_id == r)
            partners.update({'partner_state_enable': True})

    @api.model
    def _get_partner_states(self):
        return [
            ('potential', _('Potential')),
            ('pending', _('Pending Approval')),
            ('approved', _('Approved'))]

    @api.multi
    def write(self, vals):
        ResPartnerStateField = self.env['res.partner.state_field']
        for partner in self.filtered(lambda r:
                                     r.partner_state in
                                     ['approved', 'pending']):
            partner_block_fields = ResPartnerStateField.search(
                [('block_edition', '=', True)]).mapped('field_id.name')
            for key in vals.keys():
                if key in partner_block_fields:
                    raise UserError(
                        _('You can not modify this field "%s"' % (key)))
            fields = partner.check_fields('track')
            if fields:
                fields_set = set(fields)
                vals_set = set(vals)
                if fields_set & vals_set:
                    partner.partner_state_potential()

        return super(ResPartner, self).write(vals)

    @api.multi
    def partner_state_potential(self):
        self.write({'partner_state': 'potential'})

    @api.multi
    def partner_state_pending(self):
        for rec in self:
            fields = rec.check_fields('approval')
            if not fields:
                rec.partner_state = 'pending'
                continue
            partner_data = rec.read(fields)[0]
            if all(partner_data.values()):
                rec.partner_state = 'pending'
                continue
            for partner_field, value in partner_data.items():
                if not value:
                    raise UserError(_(
                        'Partner "%s" can not request approval, '
                        'required field %s' % (<commit_msg>[PORT] partner_state: from 11.0 (#56)<commit_after>##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################

from odoo import models, fields, api, _
from odoo.exceptions import UserError


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

    partner_state_enable = fields.Boolean(
        compute='_compute_partner_state_enable',
    )

    partner_state = fields.Selection(
        '_get_partner_states',
        string='Partner State',
        readonly=True,
        required=True,
        default='potential'
    )

    @api.multi
    def _compute_partner_state_enable(self):
        if self.env.user.company_id.partner_state_enable:
            partners = self.filtered(lambda r:
                                     r.commercial_partner_id == r)
            partners.update({'partner_state_enable': True})

    @api.model
    def _get_partner_states(self):
        return [
            ('potential', _('Potential')),
            ('pending', _('Pending Approval')),
            ('approved', _('Approved'))]

    @api.multi
    def write(self, vals):
        ResPartnerStateField = self.env['res.partner.state_field']
        for partner in self.filtered(lambda r:
                                     r.partner_state in
                                     ['approved', 'pending']):
            partner_block_fields = ResPartnerStateField.search(
                [('block_edition', '=', True)]).mapped('field_id.name')
            modified_fields = vals.keys()
            # if it's a contact we only check the none commercial fields to
            # allow them to be synchronized from parent
            if partner.commercial_partner_id != partner:
                modified_fields = list(
                    set(modified_fields) - set(self._commercial_fields()))

            for key in modified_fields:
                if key in partner_block_fields:
                    raise UserError(
                        _('You can not modify this field "%s"' % (key)))
            fields = partner.check_fields('track')
            if fields:
                fields_set = set(fields)
                vals_set = set(modified_fields)
                if fields_set & vals_set:
                    partner.partner_state_potential()

        return super(ResPartner, self).write(vals)

    @api.multi
    def partner_state_potential(self):
        self.write({'partner_state': 'potential'})

    @api.multi
    def partner_state_pending(self):
        for rec in self:
            fields = rec.check_fields('approval')
            if not fields:
                rec.partner_state = 'pending'
                continue
            partner_data = rec.read(fields)[0]
            if all(partner_data.values()):
                rec.partner_state = 'pending'
                continue
            for partner_field, value in partner_data.items():
                if not value:
                    raise UserError(_(
                        'Partner "%s" can not request approval, '
                        'required field %s' % (
{'fraction_non_alphanumeric': 0.1427627230667548, 'fraction_numerical': 0.0011566424322538005, 'mean_word_length': 1.93265503875969, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1669295', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1531, 'n_words': 350}