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.

165 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Partner Address on Map module for OpenERP
  5. # Copyright (C) 2015 Akretion (http://www.akretion.com/)
  6. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp import models, fields, api, _
  23. from openerp.exceptions import Warning
  24. class MapUrl(models.Model):
  25. _name = 'map.url'
  26. _description = 'Map System'
  27. name = fields.Char(string='Map Provider', required=True)
  28. address_url = fields.Char(
  29. string='URL that uses the address',
  30. help="In this URL, {ADDRESS} will be replaced by the address.")
  31. lat_lon_url = fields.Char(
  32. string='URL that uses latitude and longitude',
  33. help="In this URL, {LATITUDE} and {LONGITUDE} will be replaced by "
  34. "the latitude and longitude (requires the module 'base_geolocalize')")
  35. route_address_url = fields.Char(
  36. string='Route URL that uses the addresses',
  37. help="In this URL, {START_ADDRESS} and {DEST_ADDRESS} will be "
  38. "replaced by the start and destination addresses.")
  39. route_lat_lon_url = fields.Char(
  40. string='Route URL that uses latitude and longitude',
  41. help="In this URL, {START_LATITUDE}, {START_LONGITUDE}, "
  42. "{DEST_LATITUDE} and {DEST_LONGITUDE} will be replaced by the "
  43. "latitude and longitude of the start and destination adresses "
  44. "(requires the module 'base_geolocalize').")
  45. class ResUsers(models.Model):
  46. _inherit = 'res.users'
  47. # begin with context_ to allow user to change it by himself
  48. context_map_url_id = fields.Many2one('map.url', string='Map Provider')
  49. # IDEA : should we add the ability to have 1 map provider for map
  50. # and another one for routing ?
  51. context_route_start_partner_id = fields.Many2one(
  52. 'res.partner', string='Start Address for Route Map')
  53. # called from post-install script
  54. # I can't use a default method on the field, because it would be executed
  55. # before loading map_url_data.xml, so it would not be able to set a value
  56. @api.model
  57. def _default_map_url(self):
  58. users = self.env['res.users'].search([])
  59. map_url = self.env['map.url'].search([], limit=1)
  60. users.write({'context_map_url_id': map_url.id})
  61. class ResPartner(models.Model):
  62. _inherit = 'res.partner'
  63. @api.model
  64. def address_as_string(self):
  65. addr = u''
  66. if self.street:
  67. addr += self.street
  68. if self.street2:
  69. addr += u' ' + self.street2
  70. if self.city:
  71. addr += u' ' + self.city
  72. if self.state_id:
  73. addr += u' ' + self.state_id.name
  74. if self.country_id:
  75. addr += u' ' + self.country_id.name
  76. return addr
  77. @api.multi
  78. def open_map(self):
  79. if not self.env.user.context_map_url_id:
  80. raise Warning(
  81. _('Missing map provider: '
  82. 'you should set it in your preferences.'))
  83. map_url = self.env.user.context_map_url_id
  84. if (
  85. map_url.lat_lon_url and
  86. hasattr(self, 'partner_latitude') and
  87. self.partner_latitude and self.partner_longitude):
  88. url = map_url.lat_lon_url.replace(
  89. '{LATITUDE}', unicode(self.partner_latitude))
  90. url = url.replace('{LONGITUDE}', unicode(self.partner_longitude))
  91. else:
  92. if not map_url.address_url:
  93. raise Warning(
  94. _("Missing parameter 'URL that uses the address' "
  95. "for map provider '%s'.") % map_url.name)
  96. url = map_url.address_url.replace(
  97. '{ADDRESS}', self.address_as_string())
  98. return {
  99. 'type': 'ir.actions.act_url',
  100. 'url': url,
  101. 'target': 'new',
  102. }
  103. @api.multi
  104. def open_route_map(self):
  105. if not self.env.user.context_map_url_id:
  106. raise Warning(
  107. _('Missing map provider: '
  108. 'you should set it in your preferences.'))
  109. map_url = self.env.user.context_map_url_id
  110. if not self.env.user.context_route_start_partner_id:
  111. raise Warning(
  112. _('Missing start address for route map: '
  113. 'you should set it in your preferences.'))
  114. start_partner = self.env.user.context_route_start_partner_id
  115. if map_url.route_address_url:
  116. start_address = start_partner.address_as_string()
  117. dest_address = self.address_as_string()
  118. url = map_url.route_address_url
  119. url = url.replace('{START_ADDRESS}', start_address)
  120. url = url.replace('{DEST_ADDRESS}', dest_address)
  121. else:
  122. if not hasattr(self, 'partner_latitude'):
  123. raise Warning(
  124. _("Missing module 'base_geolocalize'"))
  125. if not map_url.route_lat_lon_url:
  126. raise Warning(
  127. _("No route URL that uses latitude and longitude "
  128. "on map provider '%s'.") % map_url.name)
  129. url = map_url.route_lat_lon_url
  130. if (
  131. not start_partner.partner_latitude or
  132. not start_partner.partner_longitude):
  133. raise Warning(
  134. _("Missing geo-localization information on "
  135. "start partner '%s'."))
  136. if not self.partner_latitude or not self.partner_longitude:
  137. raise Warning(
  138. _("Missing geo-localization information on destination "
  139. "partner '%s'.") % self.name)
  140. url = url.replace(
  141. '{START_LATITUDE}', unicode(start_partner.partner_latitude))
  142. url = url.replace(
  143. '{START_LONGITUDE}', unicode(start_partner.partner_longitude))
  144. url = url.replace(
  145. '{DEST_LATITUDE}', unicode(self.partner_latitude))
  146. url = url.replace(
  147. '{DEST_LONGITUDE}', unicode(self.partner_longitude))
  148. return {
  149. 'type': 'ir.actions.act_url',
  150. 'url': url,
  151. 'target': 'new',
  152. }