Last active
July 8, 2022 08:35
-
-
Save fazlurr/898e60c79d5261e7345baa15328ae91f to your computer and use it in GitHub Desktop.
This file contains 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
// Degrees to Radians | |
export const degsToRads = deg => (deg * Math.PI) / 180.0; | |
// Radians to Degrees | |
export const radsToDegs = rad => rad * 180 / Math.PI; | |
// Round like PHP Function | |
export const round = (num, dec) => { | |
var num_sign = num >= 0 ? 1 : -1; | |
return parseFloat((Math.round((num * Math.pow(10, dec)) + (num_sign * 0.0001)) / Math.pow(10, dec)).toFixed(dec)); | |
} | |
// Get Distance Between Two Points | |
export const getDistance = (latitude1, longitude1, latitude2, longitude2, unit = 'Km') => { | |
const theta = longitude1 - longitude2; | |
let distance = ( Math.sin(degsToRads(latitude1)) * Math.sin(degsToRads(latitude2)) ) | |
+ ( Math.cos(degsToRads(latitude1)) * Math.cos(degsToRads(latitude2)) * Math.cos(degsToRads(theta)) ); | |
// Format for acos, min -1, max 1 | |
if (distance > 1) { | |
distance = 1; | |
} else if (distance < -1) { | |
distance = -1; | |
} | |
distance = Math.acos(distance); | |
distance = radsToDegs(distance); | |
distance = distance * 60 * 1.1515; | |
switch(unit) | |
{ | |
case 'Mi': break; | |
case 'Km' : distance = distance * 1.609344; | |
} | |
return round(distance, 2); | |
} | |
// Get Location Distances | |
export const getLocationsDistances = (locations, lat, lng) => { | |
// Get distance for each locations | |
const mapper = (location) => { | |
const distance = getDistance(lat, lng, location.warehouse_latitude, location.warehouse_longitude); | |
location.distance = distance; | |
return location; | |
}; | |
const distances = locations.map(mapper); | |
// Sort distances | |
const compare = (a, b) => { | |
if (a.distance < b.distance) { | |
return -1; | |
} | |
if (b.distance < a.distance) { | |
return 1; | |
} | |
// a must be equal to b | |
return 0; | |
}; | |
const sortedDistances = distances.sort(compare); | |
return sortedDistances; | |
} | |
// Get Nearest Location | |
export const getNearestLocation = (locations, lat, lng) => { | |
const distances = getLocationsDistances(locations, lat, lng); | |
const nearest = distances[0]; | |
return nearest; | |
} | |
// Get Nearest Origin | |
export const getNearestOrigin = (availableWarehouses, subdistrictData) => { | |
let nearestOrigin = null; | |
const nearestLocation = getNearestLocation(availableWarehouses, subdistrictData.latitude, subdistrictData.longitude); | |
if (nearestLocation) { | |
const nearestProvinceId = nearestLocation.warehouse_province_id; | |
const nearestProvinceName = nearestLocation.warehouse_province_name; | |
const nearestCityId = nearestLocation.warehouse_city_id; | |
const nearestCityName = nearestLocation.warehouse_city_name; | |
const nearestSubdistrictId = nearestLocation.warehouse_subdistrict_id; | |
const nearestSubdistrictName = nearestLocation.warehouse_subdistrict_name; | |
const nearestAddress = nearestLocation.warehouse_address; | |
const nearestZip = nearestLocation.warehouse_zip; | |
const nearestPhonePrimary = nearestLocation.warehouse_phone_primary; | |
const nearestPhoneSecondary = nearestLocation.warehouse_phone_secondary; | |
nearestOrigin = { | |
id: nearestSubdistrictId, | |
type: 'subdistrict', | |
warehouse_id: nearestLocation.warehouse_id, | |
warehouse_label: nearestLocation.warehouse_label, | |
province_id: nearestProvinceId, | |
province_name: nearestProvinceName, | |
city_id: nearestCityId, | |
city_name: nearestCityName, | |
subdistrict_id: nearestSubdistrictId, | |
subdistrict_name: nearestSubdistrictName, | |
address: nearestAddress, | |
zip: nearestZip, | |
phone_primary: nearestPhonePrimary, | |
phone_secondary: nearestPhoneSecondary, | |
}; | |
} | |
return nearestOrigin; | |
}; | |
// Get Available Warehouses | |
const getAvailableWarehouses = (inventories, productData, formId) => { | |
let availableWarehouses = inventories; | |
if (productData.type === 'simple') { | |
const simpleFilter = inventory => inventory.quantity && inventory.quantity > 0; | |
availableWarehouses = inventories.filter(simpleFilter); | |
} else { | |
const variation = getSelectedVariation(formId); | |
const variationName = variation.name; | |
const filter = (inventory) => { | |
let isAvailable = inventory.quantity && inventory.quantity > 0; | |
const variations = inventory.variations ? inventory.variations : []; | |
const variation = variations.find(variation => variation.name === variationName); | |
if (variation) { | |
isAvailable = variation.quantity && variation.quantity > 0; | |
} | |
return isAvailable; | |
}; | |
availableWarehouses = inventories.filter(filter); | |
} | |
return availableWarehouses; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment