Search code examples
pythonodoo

How relation country group field to invoice custom field in Odoo or OpenERP?


this is my first question here! I want to know how to relation export_message text field and dual_box boolean field in Country Group form to x_message text field and x_dual boolean field in the invoice form.

And I want to know how can i get the country group of a country. Let me explain that...

For example:

I have 3 country groups (A, B and C) and I have 5 countries (1,2,3,4,5).

Then, Country Group A has country (1 and 5), Country Group B has (2 and 3) and Country Group C has (1 and 4). In the invoice form I have a customer with an address and a Country.

How can I get the country group in the invoice if the customer's country is 1 and save it in a custom field for example?

I'm working with Odoo 12

My CountryGroup Class

class CountryGroup(models.Model):
   _name = 'country.group'

   name = fields.Char()
   country_ids = fields.Many2many("res.country")
   export_message = fields.Text(string="Export Message")
   dual_box = fields.Boolean(string="Dual Box")

My Invoice Class

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    x_confirm_message = fields.Boolean(string='Confirm Print Message')
    x_message = fields.Text(string='Message', related='')
    x_dual = fields.Boolean(string="Dual Box", related='')

Thank you


Solution

  • Implement a computed field on country_group of account_invoice model:

    class AccountInvoice(models.Model):
        _inherit = 'account.invoice'
    
       country_group = fields.Many2one('country.group', 'Country Group', 
       compute="_find_country_group")
        x_confirm_message = fields.Boolean(string='Confirm Print Message')
        x_message = fields.Text(string='Message', related='country_group.export_message')
        x_dual = fields.Boolean(string="Dual Box", related='country_group.dual_box')
    
       @api.depends('customer_id')
       def _find_country_group(self):
            # Get country of the customer
            country_id = self.env['res.partner'].search([('id', '=', 
            self.customer_id.id)]).country_id
            # Get country_group based on customer's country
            country_gp_id = self.env['rel.country.group'].search([('country_id', '=', country_id.id)], limit=1)
            self.country_group = country_gp_id
    
    class ResCountry(models.Model):
        _inherit = 'res.country'
        
        country_gp_ids = fields.Many2many("country.group", 'rel_country_group', 'country_id', 'country_group_id')
    
    
    class CountryGroup(models.Model):
       _name = 'country.group'
    
       name = fields.Char()
       country_ids = fields.Many2many("res.country", 'rel_country_group', 'country_group_id', 'country_id')
       export_message = fields.Text(string="Export Message")
       dual_box = fields.Boolean(string="Dual Box")
    
       @api.model
       def create(self, vals):
           gpA = [1, 5]
           gpB = [2, 3]
           if vals['country_ids'].ids in gpA:
               vals['name'] = 'Group A'
           elif vals['country_ids'].ids in gpB:
               vals['name'] = 'Group B'
           ...
           return super(CountryGroup, self).create(vals)
    

    In odoo v14, you can just check many2many in many2many i.e list in list
    In odoo v12, You have to loop through each many2many to check.

           if [x for x in vals['country_ids'].ids if x in gpA]:
               vals['name'] = 'Group A'
           elif [x for x in vals['country_ids'].ids if x in gpB]:
               vals['name'] = 'Group B'