Debian Codegraph @

addons/website_sale_collect/models/stock_warehouse.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import fields, models
4 from odoo.tools.misc import format_duration
5
6
7 class StockWarehouse(models.Model):
8 _inherit = 'stock.warehouse'
9
10 opening_hours = fields.Many2one(
11 string="Opening Hours", comodel_name='resource.calendar', check_company=True
12 )
13
14 def _prepare_pickup_location_data(self):
15
16 def are_coordinates_missing(loc_):
17 return (loc_.partner_latitude, loc_.partner_longitude) == (0, 0)
18
19 # Find the longitude and latitude of the warehouse.
20 wh_location = self.partner_id
21 if are_coordinates_missing(wh_location):
22 wh_location.geo_localize()
23 if are_coordinates_missing(wh_location): # Geolocation failed.
24 # Assign invalid coordinates to skip future geolocation attempts. As coordinates are
25 # only updated when *both* latitude and longitude are zero, this prevents a spam of
26 # OpenStreetMap's API when warehouses with an invalid address are loaded in the
27 # location selector of Click and Collect.
28 wh_location.write({'partner_latitude': 1000, 'partner_longitude': 1000})
29
30 # Format the pickup location values of the warehouse.
31 try:
32 pickup_location_values = {
33 'id': self.id,
34 'name': wh_location['name'].title(),
35 'street': wh_location['street'].title(),
36 'city': wh_location.city.title(),
37 'state': wh_location.state_id.code or '',
38 'zip_code': wh_location.zip or '',
39 'country_code': wh_location.country_code,
40 'latitude': wh_location.partner_latitude,
41 'longitude': wh_location.partner_longitude,
42 }
43 except AttributeError:
44 return {}
45
46 # Prepare the opening hours data.
47 if self.opening_hours:
48 opening_hours_dict = {str(i): [] for i in range(7)}
49 for att in self.opening_hours.attendance_ids:
50 if att.day_period in ('morning', 'afternoon'):
51 opening_hours_dict[att.dayofweek].append(
52 f'{format_duration(att.hour_from)} - {format_duration(att.hour_to)}'
53 )
54 pickup_location_values['opening_hours'] = opening_hours_dict
55 else:
56 pickup_location_values['opening_hours'] = {}
57 return pickup_location_values