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.
 
 
 
 
 

64 lines
2.7 KiB

# -*- coding: utf-8 -*-
###############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2025-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
# Author: Cybrosys Techno Solutions(<https://www.cybrosys.com>)
#
# You can modify it under the terms of the GNU AFFERO
# GENERAL PUBLIC LICENSE (AGPL v3), Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details.
#
# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
# (AGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
import logging
import pytz
from odoo import api, fields, models
from datetime import datetime
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
_logger = logging.getLogger(__name__)
class ResPartner(models.Model):
_inherit = 'res.partner'
contact_local_time_tz = fields.Char(
string="Local Time", readonly=True, help="Local Time based on address")
contact_local_tz = fields.Selection(
lambda self: [(tz, tz) for tz in sorted(pytz.all_timezones, key=lambda tz: tz if not tz.startswith('Etc/') else '_')],
string='Timezone', readonly=True, help="Timezone based on address"
)
def action_compute_contact_local_time_tz(self):
"""Compute the timezone and local time based on address."""
geolocator = Nominatim(user_agent="odoo_timezone_app")
tz_finder = TimezoneFinder()
for record in self:
record.contact_local_tz = False
record.contact_local_time_tz = False
try:
# Build address for geocoding
address = ", ".join(filter(None, [
record.state_id.name, record.country_id.name
]))
location = geolocator.geocode(address)
if location:
tz_str = tz_finder.timezone_at(lat=location.latitude, lng=location.longitude)
if tz_str:
record.contact_local_tz = tz_str
tz = pytz.timezone(tz_str)
local_time = datetime.now(tz)
record.contact_local_time_tz = local_time.strftime("%Y-%m-%d %H:%M:%S")
except Exception as e:
_logger.exception(f"Error retrieving timezone or local time for {record.name}: {e}")