You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Sébastien BEAU <sebastien.beau@akretion.com>
  5. # Copyright 2014 Akretion
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm
  22. def split_char(char, output_number, size):
  23. words = char.split(' ')
  24. result = []
  25. word = words.pop(0)
  26. for index in range(0, output_number):
  27. result.append(word)
  28. word = ''
  29. while len(words) > 0:
  30. word = words.pop(0)
  31. if len(result[index] + ' %s' % word) > size:
  32. break
  33. else:
  34. result[index] += ' %s' % word
  35. word = ''
  36. return result
  37. class ResPartner(orm.Model):
  38. _inherit = "res.partner"
  39. def _get_split_address(
  40. self, cr, uid, partner, output_number, max_size, context=None):
  41. """ This method allows to get a number of street fields according to
  42. your choice. Default is 2 large fields in Odoo (128 chars).
  43. In some countries you may use 3 or 4 shorter street fields.
  44. example:
  45. res = self.pool['res.partner']._get_split_address(
  46. cr, uid, picking.partner_id, 3, 35, context=context)
  47. address['street'], address['street2'], address['street3'] = res
  48. """
  49. street = partner.street or ''
  50. street2 = partner.street2 or ''
  51. if len(street) <= max_size and len(street2) <= max_size:
  52. result = ['' for i in range(0, output_number)]
  53. result[0] = street
  54. result[1] = street2
  55. return result
  56. elif street <= max_size:
  57. return [street] + split_char(street2, output_number - 1, max_size)
  58. else:
  59. return split_char(
  60. '%s %s' % (street, street2), output_number, max_size)