Subversion Repositories SmartDukaan

Rev

Rev 644 | Rev 675 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
412 ashish 1
'''
2
Created on 05-Aug-2010
3
 
4
@author: ashish
5
'''
644 chandransh 6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
7
    LogisticsServiceException
8
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
669 chandransh 9
    get_shipment_info, initialize, get_logistics_estimation, get_provider
10
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
11
    to_t_provider
494 rajveer 12
from shop2020.clients.InventoryClient import InventoryClient
13
from shop2020.config.client.ConfigClient import ConfigClient
14
import datetime
644 chandransh 15
import math
472 rajveer 16
 
412 ashish 17
class LogisticsServiceHandler:
18
 
442 rajveer 19
    def __init__(self):
20
        initialize()
644 chandransh 21
        config_client = ConfigClient()
22
        self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
23
        self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
24
        self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
483 rajveer 25
 
669 chandransh 26
    def getProvider(self, providerId):
27
      """
28
      Returns a provider for a given provider ID. Throws an exception if none found.
29
 
30
      Parameters:
31
       - providerId
32
      """
33
      return to_t_provider(get_provider(providerId))
34
 
644 chandransh 35
    def getLogisticsInfo(self, destination_pincode, itemId):
483 rajveer 36
        """
37
        Parameters:
38
         - destination_pincode
39
         - sku_id
40
        """
644 chandransh 41
        logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
42
        logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
483 rajveer 43
        return logistics_info
412 ashish 44
 
45
    def getEmptyAWB(self, provider_id):
46
        """
47
        Parameters:
48
         - provider_id
49
        """
442 rajveer 50
        return get_empty_AWB(provider_id)
51
 
644 chandransh 52
    def getShipmentInfo(self, awb, providerId):
412 ashish 53
        """
54
        Parameters:
55
         - awb
56
        """
644 chandransh 57
        awb_updates = get_shipment_info(awb, providerId)
58
        t_updates = []
59
        for update in awb_updates:
60
            t_updates.append(to_t_awbupdate(update))
61
        return t_updates
412 ashish 62
 
644 chandransh 63
    def getLogisticsEstimation(self, itemId, destination_pin):
472 rajveer 64
        """
65
        Parameters:
66
         - itemId
67
         - destination_pin
68
        """
644 chandransh 69
        delivery_estimate = get_logistics_estimation(destination_pin)
70
        delivery_time = 24*delivery_estimate.delivery_time
71
        if self.cutoff_time <= datetime.datetime.now().hour:
494 rajveer 72
            delivery_time = delivery_time + 24
644 chandransh 73
        warehouse_loc = delivery_estimate.warehouse_location
74
        try:
75
            client = InventoryClient().get_client()
76
            warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
77
        except:
78
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
79
        if items_in_inventory < self.stock_threshold :
80
            delivery_time = delivery_time + self.time_to_producre
81
        delivery_time = int(math.ceil(delivery_time/24.0))
472 rajveer 82
 
644 chandransh 83
        logistics_info = LogisticsInfo()
84
        logistics_info.deliveryTime = delivery_time
85
        logistics_info.providerId = delivery_estimate.provider_id
86
        logistics_info.warehouseId = warehouse_id
477 rajveer 87
 
644 chandransh 88
        return logistics_info