This question has been flagged
3 Replies
8528 Views

I added two many2one mandatory fields (taxid_type and taxpayer_type) to the Partner model. Just one of these is used in Company model(taxpayer_id).

I also created the _inverse_taxpayer method to syncronize the values from res.partner to res.company and inherited the create method from res.company to do the inverse syncronization.

Now I'm having a problem. The Vat field must be constrained in res.partner according to taxid_type when creating contacts type company. But this same field in res.company just must have a constraint, its length must be igual to 13. When I install the module and configure the vat in res.company form, I'm having the next error


File "/home/odoo/odoo13/extra-addons/custommodules/mymodule/models/partner.py", line 35, in check_vat
    if len(record.vat) < 13:
TypeError: object of type 'bool' has no len()


This is my partner.py module


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

    taxid_type = fields.Many2one('lec.taxid.type', string='TaxID Type')
    taxpayer_type = fields.Many2one('lec.taxpayer.type', string='Tax Payer Type')

    @api.constrains('vat', 'taxid_type', 'taxpayer_type')
    def check_vat(self):
        for record in self:
            if len(record.vat) < 13:
                raise ValidationError('Tax id is minor than allowed partner')
            elif len(record.vat) > 13:
                raise ValidationError('Tax id is major than allowed')


And this is my company.py module

class Company(models.Model):
    _inherit = 'res.company'

    taxpayer_type = fields.Many2one('lec.taxpayer.type', related='partner_id.taxpayer_type', string='Tax Payer Type', compute='_compute_taxpayertype', inverse='_inverse_taxpayer')

    @api.constrains('vat')
    def check_vat(self):

        if len(self.vat) < 13:
            raise UserError('tax id is minor than allowed company')
        elif len(self.vat) > 13:
            raise UserError('tax id is major than allowed')
   
    #The next method returns the value inserted in res.partner to res.company and assign to the corresponding field
    def _inverse_taxpayer(self):
        for company in self:
            company.partner_id.taxpayer_type = company.taxpayer_type


    #The next method set the value inserted in res.company to res.partner and assign to the corresponding field
    # partner's contact payertype
    @api.model
    def create(self, vals):
        if not vals.get('name') or vals.get('partner_id'):
            self.clear_caches()
            return super(Company, self).create(vals)
        partner = self.env['res.partner'].create({
            'taxpayer_type': vals.get('taxpayer_type'),
        })
        # compute stored fields, for example address dependent fields
        partner.flush()
        vals['partner_id'] = partner.id
        self.clear_caches()
        company = super(Company, self).create(vals)
        return company

Does anyone know how can I solve this problem and achieve my objective

Avatar
Discard
Best Answer

Hi,

You are getting this error because as there is no value in the corresponding field. You can solve it by adding an IF condition in the code,

Error:

TypeError: object of type 'bool' has no len()

Solution:

if record.vat:
if len(record.vat) < 13:
raise ValidationError('Tax id is minor than allowed partner')
elif len(record.vat) > 13:
raise ValidationError('Tax id is major than allowed')

You can apply similar if condition in the other places too.


Thanks

Avatar
Discard
Author

But with that approach, the field vat will not have a restriction of mandatory field. How can I make these fields mandatory only for company contact creation in res.partner?

If you are saying you need to make it as required add and else condition to the first it and show warning that the value is required

Author

The the problems comes again, when the user tries to setup the company configuration res.company the message will appear again because the vat in res.partner is empty.