Rev 12895 | Rev 19421 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 05-Aug-2010@author: ashish'''from shop2020.clients.CatalogClient import CatalogClientfrom shop2020.clients.InventoryClient import InventoryClientfrom shop2020.config.client.ConfigClient import ConfigClientfrom shop2020.logistics.service.impl import DataAccessorfrom shop2020.logistics.service.impl.Converters import to_t_awbupdate, \to_t_provider, to_t_pickup_storefrom shop2020.logistics.service.impl.DataAccessor import get_empty_AWB, \get_shipment_info, initialize, get_logistics_estimation, get_provider, \get_providers, close_session, get_free_awb_count, get_holidays, is_alive, \get_provider_for_pickup_type, get_pickup_store, get_all_pickup_stores, \get_pickup_store_by_hotspot_id, get_destination_code, update_pincode, \add_pincode, store_shipment_info, adjust_delivery_time, get_min_advance_amount, \add_new_awbs, run_Logistics_Location_Info_Update, get_first_delivery_estimate_for_wh_location, \get_provider_limit_details_for_pincode, get_new_empty_awbfrom shop2020.logistics.service.impl.DataService import \ServiceableLocationDetailsfrom shop2020.thriftpy.logistics.ttypes import LogisticsInfo, \LogisticsServiceException, DeliveryType, PickUpTypefrom shop2020.thriftpy.model.v1.catalog.ttypes import statusfrom shop2020.thriftpy.logistics.ttypes import ItemTextfrom shop2020.utils.Utils import to_java_date, to_py_dateimport collectionsimport datetimeimport mathimport sys#Start:- Added by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013#End:- Added by Manish Sharma for Multiple Pincode Updation on 05-Jul-2013class LogisticsServiceHandler:def __init__(self, dbname='logistics', db_hostname='localhost'):initialize(dbname, db_hostname)try:config_client = ConfigClient()self.sourceId = int(config_client.get_property("sourceid"))self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))self.cod_cutoff_time = 24 #int(config_client.get_property('delivery_cutoff_time'))self.default_pincode = int(config_client.get_property('default_pincode'))except Exception as ex:print "[ERROR] Unexpected config error:", sys.exc_info()[0]self.sourceId = 1self.cutoff_time = 15self.cod_cutoff_time = 24self.default_pincode = "110001"def getProvider(self, providerId):"""Returns a provider for a given provider ID. Throws an exception if none found.Parameters:- providerId"""try:provider = get_provider(providerId)if provider:return to_t_provider(provider)else:raise LogisticsServiceException(101, "No Provider found for the given id")finally:close_session()def getAllProviders(self, ):"""Returns a list containing all the providers."""try:return [to_t_provider(provider) for provider in get_providers()]finally:close_session()def getLogisticsInfo(self, destination_pincode, itemId, type, pickUp):"""Parameters:- destination_pincode- item_id- type"""try:logistics_info = self.get_logistics_estimation_with_type(itemId, destination_pincode, type)if pickUp == PickUpType.RUNNER or pickUp == PickUpType.SELF:logistics_info.providerId = get_provider_for_pickup_type(pickUp)#logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId, type)return logistics_infofinally:close_session()def getEmptyAWB(self, providerId, type):"""Parameters:- provider_id- type"""try:return get_empty_AWB(providerId, type)finally:close_session()def getShipmentInfo(self, awbNumber, providerId):"""Parameters:- awbNumber- providerId"""try:awb_updates = get_shipment_info(awbNumber, providerId)t_updates = []for update in awb_updates:t_updates.append(to_t_awbupdate(update))return t_updatesfinally:close_session()def storeShipmentInfo(self, update):"""Parameters:- update"""try:store_shipment_info(update)finally:close_session()def getLogisticsEstimation(self, itemId, destination_pin, type):"""Parameters:- itemId- destination_pin- type"""try:return self.get_logistics_estimation_with_type(itemId, destination_pin, type)finally:close_session()def getLogisticsEstimationForStore(self, itemId, destination_pin, type):"""Parameters:- itemId- destination_pin- type"""try:todate = datetime.datetime.now()logistics_info = self.get_logistics_estimation_with_type(itemId, destination_pin, type)logistics_info.deliveryTime = to_java_date(todate + datetime.timedelta(days = adjust_delivery_time(todate, logistics_info.deliveryTime)))logistics_info.shippingTime = to_java_date(todate + datetime.timedelta(days = adjust_delivery_time(todate, logistics_info.shippingTime)))minAdvanceAmount, logistics_info.codAllowed = get_min_advance_amount(itemId, destination_pin, logistics_info.providerId, logistics_info.codAllowed)## Send minadvanceamount in providerId fieldlogistics_info.providerId = int(minAdvanceAmount)return logistics_infofinally:close_session()def get_logistics_estimation_with_type(self, itemId, destination_pin, type):try:#Get the id and location of actual warehouse that'll be used to fulfil this order.client = InventoryClient().get_client()fulfilmentWarehouseId, expected_delay, billingWarehouseId, sellingPrice, totalAvailability, weight = client.getItemAvailabilityAtLocation(itemId, self.sourceId)except Exception as ex:raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")delivery_estimate = get_logistics_estimation(destination_pin, sellingPrice, weight, type, billingWarehouseId)if delivery_estimate is None:raise LogisticsServiceException(104, "Unable to fetch delivery estimate for this pincode.")## Commented below part as we have only Delhi as warehouse city. If we will add some more warehouses in different cities, this could be useful.# We are revising the estimates based on the actual warehouse that this order will be assigned to.# This warehouse may be located in a zone which is different from the one we allocated for this pincode.#delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice, warehouse_loc, type)#if delivery_estimate is None:# raise LogisticsServiceException(105, "Unable to fetch delivery estimate for pincode: " + destination_pin + " and revised location: " + str(warehouse_loc))delivery_time = 24 * (delivery_estimate.delivery_time + delivery_estimate.delivery_delay)'''We're now calculating the expected shipping delay which is independent ofthe courier agency and is completely within our control (well, almost).'''#Always add the expected delayshipping_delay = 24 * expected_delay# Sometimes we set negative shipping delay just in case we know time to procure will be less than the default.# If we have received inventory and forgot to remove expected delay from item, it could lead to display negative shipping days.if shipping_delay < 0:shipping_delay = 0#Further increase the estimate if it's late in the daycurrent_hour = datetime.datetime.now().hourif type == DeliveryType.PREPAID and self.cutoff_time <= current_hour:shipping_delay = shipping_delay + 24#In case of COD,increase delay by one more dayif type == DeliveryType.COD:shipping_delay = shipping_delay + 24delivery_estimate.otgAvailable = Falsedelivery_time = delivery_time + shipping_delayshipping_delay = int(math.ceil(shipping_delay/24.0))delivery_time = int(math.ceil(delivery_time/24.0))logistics_info = LogisticsInfo()logistics_info.deliveryTime = delivery_timelogistics_info.providerId = delivery_estimate.provider_idlogistics_info.warehouseId = billingWarehouseIdlogistics_info.fulfilmentWarehouseId = fulfilmentWarehouseIdlogistics_info.shippingTime = shipping_delaylogistics_info.codAllowed = delivery_estimate.codAllowedlogistics_info.otgAvailable = delivery_estimate.otgAvailablelogistics_info.deliveryDelay = delivery_estimate.delivery_delaytry:return logistics_infofinally:close_session()def getDestinationCode(self, providerId, pinCode):"""Returns the short three letter code of a pincode for the given provider.Raises an exception if the pin code is not serviced by the given provider.Parameters:- providerId- pinCode"""try:try:dest_code = DataAccessor.serviceable_location_cache[providerId][pinCode][0]return dest_codeexcept:try:dest_code = get_destination_code(providerId, pinCode)return dest_codeexcept:if providerId >7:return ""raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))finally:close_session()def getFreeAwbCount(self, providerId, type):"""Returns the number of unused AWB numbers for the given provider of the given typeParameters:- providerId- type"""try:return get_free_awb_count(providerId, type)finally:close_session()def getHolidays(self, fromDate, toDate):"""Returns list of Holiday dates between fromDate and toDate (both inclusive)fromDate should be passed as milliseconds corresponding to the start of the day.If fromDate is passed as -1, fromDate is not considered for filteringIf toDate is passed as -1, toDate is not considered for filteringParameters:- fromDate- toDate"""try:return get_holidays(fromDate, toDate)finally:close_session()def getProviderForPickupType(self, pickUp):try:return get_provider_for_pickup_type(pickUp)finally:close_session()def closeSession(self, ):close_session()def isAlive(self, ):"""For checking weather service is active alive or not. It also checks connectivity with database"""try:return is_alive()finally:close_session()def getEntityLogisticsEstimation(self, catalogItemId, destination_pin, type):"""Returns a LogisticsInfo structure w/o an airway bill number. Use this method during the estimation phase.Raises an exception if this pincode is not allocated to any warehouse zone or provider. Also, if the pincodeis allocated to a warehouse zone but there are no actual warehouses in that zone, an exception is raised.Parameters:- catalogItemId- destination_pin- type"""try:return self.get_entity_logistics_estimation_with_type(catalogItemId, destination_pin, type)finally:close_session()def get_entity_logistics_estimation_with_type(self, catalog_item_id, destination_pin, type):try:client = CatalogClient().get_client()items = client.getValidItemsByCatalogId(catalog_item_id)except Exception as ex:raise LogisticsServiceException(103, "Unable to fetch inventory information about this entity.")estimateList = []for item in items:try:estimationInfo = self.get_logistics_estimation_with_type(item.id, destination_pin, type)except Exception as ex:estimationInfo = LogisticsInfo()estimationInfo.deliveryTime = 0if item.itemStatus == status.ACTIVE:estimateList.append((0, estimationInfo.deliveryTime, "BUY NOW", item.id))elif item.itemStatus == status.PAUSED:estimateList.append((1, estimationInfo.deliveryTime, "NOTIFY ME", item.id))elif item.itemStatus == status.PAUSED_BY_RISK:estimateList.append((2, estimationInfo.deliveryTime, "NOTIFY ME", item.id))elif item.itemStatus == status.COMING_SOON:estimateList.append((3, estimationInfo.deliveryTime, "NOTIFY ME", item.id))estimateList.sort()try:return [ ItemText(estimate[-1], estimate[-2]) for estimate in estimateList]finally:close_session()def getAllPickupStores(self):try:return [to_t_pickup_store(pickup_store) for pickup_store in get_all_pickup_stores()]finally:close_session()def getPickupStore(self, storeId):"""Parameters:- storeId"""try:storeToReturn = to_t_pickup_store(get_pickup_store(storeId))return storeToReturnfinally:close_session()def getPickupStoreByHotspotId(self, hotspotId):"""Parameters:- hotspotId"""try:storeToReturn = to_t_pickup_store(get_pickup_store_by_hotspot_id(hotspotId))return storeToReturnfinally:close_session()def addPincode(self, providerId, pincode, destCode, exp, cod, stationType, otgAvailable):try:add_pincode(providerId, pincode, destCode, exp, cod, stationType, otgAvailable)finally:close_session()def updatePincode(self, providerId, pincode, exp, cod, otgAvailable):try:update_pincode(providerId, pincode, exp, cod, otgAvailable)finally:close_session()def addNewAwbs(self, providerId, isCod, awbs, awbUsedFor):try:return add_new_awbs(providerId, isCod, awbs, awbUsedFor)finally:close_session()def runLogisticsLocationInfoUpdate(self, logisticsLocationInfoList, runCompleteUpdate):try:run_Logistics_Location_Info_Update(logisticsLocationInfoList, runCompleteUpdate)finally:close_session()def adjustDeliveryDays(self, startDate, days):try:return adjust_delivery_time(to_py_date(startDate), days)finally:close_session()def getFirstDeliveryEstimateForWhLocation(self, pincode, whLocation):try:return get_first_delivery_estimate_for_wh_location(pincode, whLocation)finally:close_session()def getProviderLimitDetailsForPincode(self, providerId, pincode):try:return get_provider_limit_details_for_pincode(providerId, pincode)finally:close_session()def getNewEmptyAwb(self, providerId, type, orderQuantity):try:return get_new_empty_awb(providerId, type, orderQuantity)finally:close_session()