Created
September 21, 2024 19:27
-
-
Save ray-odoo/3d43eee209d8501a5153931524f40bec to your computer and use it in GitHub Desktop.
Contractors within 50 miles of Sales Order Delivery Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# custom_ray_odoo | |
distance = 50 | |
delivery_address = record.partner_shipping_id or record.partner_id | |
lat = delivery_address.partner_latitude | |
lng = delivery_address.partner_longitude | |
if lat == 0.0 or lng == 0.0: | |
raise UserError("Customer Address has not been geocoded!") | |
query = """ | |
SELECT * | |
FROM (SELECT id, (3959 * acos(cos(radians(%s)) * cos(radians(partner_latitude)) * | |
cos(radians(partner_longitude) - radians(%s)) + sin(radians(%s)) * | |
sin(radians(partner_latitude)))) AS distance | |
FROM res_partner where date_localization is not null) al | |
WHERE distance < %s | |
ORDER BY distance | |
""" | |
env.cr.execute(query, (lat, lng, lat, distance)) | |
result = env.cr.dictfetchall() | |
# hijack / repurpose an existing transient model | |
matching_partners = env['base.partner.merge.automatic.wizard'].create({ | |
'state': 'option', | |
'partner_ids': [(4, dict(rec)['id']) for rec in result] | |
}) | |
action = { | |
'name': 'Contractors - %s miles' % distance, | |
'type': 'ir.actions.act_window', | |
'res_model': 'res.partner', | |
'view_mode': 'map,tree', | |
'domain': [('id', 'in', matching_partners.partner_ids.ids)], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment