Subversion Repositories SmartDukaan

Rev

Rev 746 | Rev 776 | 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,\
675 chandransh 9
    get_shipment_info, initialize, get_logistics_estimation, get_provider,\
766 rajveer 10
    get_providers, close_session
669 chandransh 11
from shop2020.logistics.service.impl.Converters import to_t_awbupdate,\
12
    to_t_provider
494 rajveer 13
from shop2020.clients.InventoryClient import InventoryClient
14
from shop2020.config.client.ConfigClient import ConfigClient
15
import datetime
644 chandransh 16
import math
731 chandransh 17
from shop2020.logistics.service.impl.DataService import ServiceableLocationDetails
472 rajveer 18
 
412 ashish 19
class LogisticsServiceHandler:
20
 
442 rajveer 21
    def __init__(self):
22
        initialize()
746 rajveer 23
        try:
24
            config_client = ConfigClient()
25
            self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
26
            self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
27
            self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
28
        except:    
29
            self.cutoff_time = 15
30
            self.stock_threshold = 2
31
            self.time_to_producre = 48 
32
 
669 chandransh 33
    def getProvider(self, providerId):
675 chandransh 34
        """
35
        Returns a provider for a given provider ID. Throws an exception if none found.
36
 
37
        Parameters:
38
         - providerId
39
        """
40
        return to_t_provider(get_provider(providerId))
669 chandransh 41
 
675 chandransh 42
    def getAllProviders(self, ):
43
        """
44
        Returns a list containing all the providers.
45
        """
46
        return [to_t_provider(provider) for provider in get_providers()]
47
 
644 chandransh 48
    def getLogisticsInfo(self, destination_pincode, itemId):
483 rajveer 49
        """
50
        Parameters:
51
         - destination_pincode
716 rajveer 52
         - item_id
483 rajveer 53
        """
644 chandransh 54
        logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
55
        logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
483 rajveer 56
        return logistics_info
412 ashish 57
 
58
    def getEmptyAWB(self, provider_id):
59
        """
60
        Parameters:
61
         - provider_id
62
        """
442 rajveer 63
        return get_empty_AWB(provider_id)
64
 
644 chandransh 65
    def getShipmentInfo(self, awb, providerId):
412 ashish 66
        """
67
        Parameters:
68
         - awb
766 rajveer 69
         - providerId
412 ashish 70
        """
644 chandransh 71
        awb_updates = get_shipment_info(awb, providerId)
72
        t_updates = []
73
        for update in awb_updates:
74
            t_updates.append(to_t_awbupdate(update))
75
        return t_updates
412 ashish 76
 
644 chandransh 77
    def getLogisticsEstimation(self, itemId, destination_pin):
472 rajveer 78
        """
79
        Parameters:
80
         - itemId
81
         - destination_pin
82
        """
644 chandransh 83
        delivery_estimate = get_logistics_estimation(destination_pin)
84
        delivery_time = 24*delivery_estimate.delivery_time
85
        if self.cutoff_time <= datetime.datetime.now().hour:
494 rajveer 86
            delivery_time = delivery_time + 24
644 chandransh 87
        warehouse_loc = delivery_estimate.warehouse_location
88
        try:
89
            client = InventoryClient().get_client()
90
            warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
91
        except:
92
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
93
        if items_in_inventory < self.stock_threshold :
94
            delivery_time = delivery_time + self.time_to_producre
95
        delivery_time = int(math.ceil(delivery_time/24.0))
472 rajveer 96
 
644 chandransh 97
        logistics_info = LogisticsInfo()
98
        logistics_info.deliveryTime = delivery_time
99
        logistics_info.providerId = delivery_estimate.provider_id
100
        logistics_info.warehouseId = warehouse_id
477 rajveer 101
 
731 chandransh 102
        return logistics_info
103
 
104
    def getDestinationCode(self, providerId, pinCode):
105
        """
106
        Returns the short three letter code of a pincode for the given provider.
107
        Raises an exception if the pin code is not serviced by the given provider.
108
 
109
        Parameters:
110
         - providerId
111
         - pinCode
112
        """
113
        sld = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pinCode)
114
        if sld != None:
115
            return sld.dest_code
116
        else:
766 rajveer 117
            raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))
118
 
119
    def closeSession(self, ):
120
        close_session()