res_partner.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, models
  4. from odoo.addons.snailmail.country_utils import SNAILMAIL_COUNTRIES
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. def write(self, vals):
  8. letter_address_vals = {}
  9. address_fields = ['street', 'street2', 'city', 'zip', 'state_id', 'country_id']
  10. for field in address_fields:
  11. if field in vals:
  12. letter_address_vals[field] = vals[field]
  13. if letter_address_vals:
  14. letters = self.env['snailmail.letter'].search([
  15. ('state', 'not in', ['sent', 'canceled']),
  16. ('partner_id', 'in', self.ids),
  17. ])
  18. letters.write(letter_address_vals)
  19. return super(ResPartner, self).write(vals)
  20. def _get_country_name(self):
  21. # when sending a letter, thus rendering the report with the snailmail_layout,
  22. # we need to override the country name to its english version following the
  23. # dictionary imported in country_utils.py
  24. country_code = self.country_id.code
  25. if self.env.context.get('snailmail_layout') and country_code in SNAILMAIL_COUNTRIES:
  26. return SNAILMAIL_COUNTRIES.get(country_code)
  27. return super(ResPartner, self)._get_country_name()
  28. @api.model
  29. def _get_address_format(self):
  30. # When sending a letter, the fields 'street' and 'street2' should be on a single line to fit in the address area
  31. if self.env.context.get('snailmail_layout') and self.street2:
  32. return "%(street)s, %(street2)s\n%(city)s %(state_code)s %(zip)s\n%(country_name)s"
  33. return super(ResPartner, self)._get_address_format()