| 1 |
import math |
| 2 |
|
| 3 |
|
| 4 |
def format_product_stock_values(product, wh_id=None, free_qty=None): |
| 5 |
"""Format product stock values for the location selector. |
| 6 |
|
| 7 |
:param product.product|product.template product: The product whose stock values to format. |
| 8 |
:param int wh_id: The warehouse whose stock to check for the given product. |
| 9 |
:param int free_qty: The free quantity of the product. If not given, calculated from the |
| 10 |
warehouse. |
| 11 |
:return: The formatted product stock values. |
| 12 |
:rtype: dict |
| 13 |
""" |
| 14 |
if product.is_product_variant: |
| 15 |
if free_qty is None: |
| 16 |
free_qty = product.with_context(warehouse_id=wh_id).free_qty |
| 17 |
|
| 18 |
in_stock = free_qty > 0 |
| 19 |
show_quantity = ( |
| 20 |
product.show_availability and in_stock and product.available_threshold >= free_qty |
| 21 |
) |
| 22 |
return { |
| 23 |
'in_stock': in_stock or product.allow_out_of_stock_order, |
| 24 |
'show_quantity': show_quantity, |
| 25 |
'quantity': free_qty, |
| 26 |
} |
| 27 |
return {} |
| 28 |
|
| 29 |
|
| 30 |
def calculate_partner_distance(partner1, partner2): |
| 31 |
"""Calculate the Haversine distance between two partners. |
| 32 |
|
| 33 |
See https://en.wikipedia.org/wiki/Haversine_formula. |
| 34 |
|
| 35 |
:param res.partner partner1: The partner to calculate distance from. |
| 36 |
:param res.partner partner2: The partner to calculate distance to. |
| 37 |
:return: The distance between the two partners (in kilometers). |
| 38 |
:rtype: float |
| 39 |
""" |
| 40 |
R = 6371 |
| 41 |
lat1, long1 = partner1.partner_latitude, partner1.partner_longitude |
| 42 |
lat2, long2 = partner2.partner_latitude, partner2.partner_longitude |
| 43 |
dlat = math.radians(lat2 - lat1) |
| 44 |
dlong = math.radians(long2 - long1) |
| 45 |
arcsin = ( |
| 46 |
math.sin(dlat / 2) * math.sin(dlat / 2) |
| 47 |
+ math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) |
| 48 |
* (math.sin(dlong / 2) * math.sin(dlong / 2)) |
| 49 |
) |
| 50 |
d = 2 * R * math.atan2(math.sqrt(arcsin), math.sqrt(1 - arcsin)) |
| 51 |
|
| 52 |
return d |