Subversion Repositories SmartDukaan

Rev

Rev 1248 | Rev 1687 | 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,\
1137 chandransh 10
    get_providers, close_session, get_free_awb_count
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
 
1248 chandransh 21
    def __init__(self, dbname='logistics'):
22
        initialize(dbname)
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'))
776 rajveer 28
            self.default_pincode = int(config_client.get_property('default_pincode'))
746 rajveer 29
        except:    
30
            self.cutoff_time = 15
31
            self.stock_threshold = 2
776 rajveer 32
            self.time_to_producre = 48
33
            self.default_pincode = "110001" 
746 rajveer 34
 
669 chandransh 35
    def getProvider(self, providerId):
675 chandransh 36
        """
37
        Returns a provider for a given provider ID. Throws an exception if none found.
38
 
39
        Parameters:
40
         - providerId
41
        """
796 rajveer 42
        try:
1137 chandransh 43
            provider = get_provider(providerId)
44
            if provider:
45
                return to_t_provider(provider)
46
            else:
47
                raise LogisticsServiceException(101, "No Provider found for the given id")
796 rajveer 48
        finally:
49
            close_session()
50
 
675 chandransh 51
    def getAllProviders(self, ):
52
        """
53
        Returns a list containing all the providers.
54
        """
796 rajveer 55
        try:
56
            return [to_t_provider(provider) for provider in get_providers()]
57
        finally:
58
            close_session()
59
 
644 chandransh 60
    def getLogisticsInfo(self, destination_pincode, itemId):
483 rajveer 61
        """
62
        Parameters:
63
         - destination_pincode
716 rajveer 64
         - item_id
483 rajveer 65
        """
796 rajveer 66
        try:
67
            logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
68
            logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
69
            return logistics_info
70
        finally:
71
            close_session()
72
 
412 ashish 73
    def getEmptyAWB(self, provider_id):
74
        """
75
        Parameters:
76
         - provider_id
77
        """
796 rajveer 78
        try:
79
            return get_empty_AWB(provider_id)
80
        finally:
81
            close_session()
82
 
644 chandransh 83
    def getShipmentInfo(self, awb, providerId):
412 ashish 84
        """
85
        Parameters:
86
         - awb
766 rajveer 87
         - providerId
412 ashish 88
        """
796 rajveer 89
        try:
90
            awb_updates = get_shipment_info(awb, providerId)
91
            t_updates = []
92
            for update in awb_updates:
93
                t_updates.append(to_t_awbupdate(update))
94
            return t_updates
95
        finally:
96
            close_session()
97
 
644 chandransh 98
    def getLogisticsEstimation(self, itemId, destination_pin):
472 rajveer 99
        """
100
        Parameters:
101
         - itemId
102
         - destination_pin
103
        """
644 chandransh 104
        try:
796 rajveer 105
            if not destination_pin:
106
                destination_pin = self.default_pincode
1504 ankur.sing 107
            try:
108
                client = InventoryClient().get_client()
109
                item = client.getItem(itemId)
110
            except Exception as ex:
111
                raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.")
112
            delivery_estimate = get_logistics_estimation(destination_pin, item.sellingPrice)
796 rajveer 113
            if delivery_estimate is None:
114
                raise LogisticsServiceException(103, "Unable to fetch delivery estimate for this pincode.")
115
            delivery_time = 24*delivery_estimate.delivery_time
116
            if self.cutoff_time <= datetime.datetime.now().hour:
117
                delivery_time = delivery_time + 24
118
            warehouse_loc = delivery_estimate.warehouse_location
119
            try:
120
                warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
1504 ankur.sing 121
            except Exception as ex:
796 rajveer 122
                raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
123
            if items_in_inventory < self.stock_threshold :
124
                delivery_time = delivery_time + self.time_to_producre
125
            delivery_time = int(math.ceil(delivery_time/24.0))
126
 
127
            logistics_info = LogisticsInfo()
128
            logistics_info.deliveryTime = delivery_time
129
            logistics_info.providerId = delivery_estimate.provider_id
130
            logistics_info.warehouseId = warehouse_id
131
 
132
            return logistics_info
133
        finally:
134
            close_session()
135
 
731 chandransh 136
    def getDestinationCode(self, providerId, pinCode):
137
        """
138
        Returns the short three letter code of a pincode for the given provider.
139
        Raises an exception if the pin code is not serviced by the given provider.
140
 
141
        Parameters:
142
         - providerId
143
         - pinCode
144
        """
796 rajveer 145
        try:
146
            sld = ServiceableLocationDetails.get_by(provider_id = providerId, dest_pincode = pinCode)
147
            if sld != None:
148
                return sld.dest_code
149
            else:
150
                raise LogisticsServiceException(101, "The pincode " + pinCode + " is not serviced by this provider: " + str(providerId))
151
        finally:
152
            close_session()
1137 chandransh 153
 
154
    def getFreeAwbCount(self, providerId):
155
        """
156
        Returns the number of unused AWB numbers for the given provider
796 rajveer 157
 
1137 chandransh 158
        Parameters:
159
         - providerId
160
        """
161
        try:
162
            return get_free_awb_count(providerId)
163
        finally:
164
            close_session()
165
 
766 rajveer 166
    def closeSession(self, ):
167
        close_session()