Subversion Repositories SmartDukaan

Rev

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