Subversion Repositories SmartDukaan

Rev

Rev 4848 | Rev 5243 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4848 Rev 4866
Line 19... Line 19...
19
 
19
 
20
warehouse_allocation_cache = {}
20
warehouse_allocation_cache = {}
21
serviceable_location_cache = {}
21
serviceable_location_cache = {}
22
delivery_estimate_cache = {}
22
delivery_estimate_cache = {}
23
destination_provider_allocation_cache = {}
23
destination_provider_allocation_cache = {}
-
 
24
ncr_pincodes = ['110001','110002','110003','110004','110005','110006','110007','110008','110009','110010','110011','110012','110013','110014','110015', \
-
 
25
                '110016','110017','110018','110019','110020','110021','110022','110023','110024','110025','110026','110027','110028','110029','110030', \
-
 
26
                '110031','110032','110033','110034','110035','110036','110037','110038','110039','110040','110041','110042','110043','110044','110045', \
-
 
27
                '110046','110047','110048','110049','110050','110051','110052','110053','110054','110055','110056','110057','110058','110059','110060', \
-
 
28
                '110061','110062','110063','110064','110065','110066','110067','110068','110069','110070','110071','110072','110073','110074','110075', \
-
 
29
                '110076','110077','110078','110081','110082','110083','110084','110085','110086','110087','110088','110089','110091','110092','110093', \
-
 
30
                '110094','110095','110096','121001','121002','121003','121004','121005','121006','121007','121008','121009','122001','122002','122003', \
-
 
31
                '122004','122005','122006','122007','122008','122009','122010','122011','122012','122015','122016','122017','122018','122050','122051', \
-
 
32
                '201001','201002','201003','201004','201005','201006','201007','201008','201009','201010','201011','201012','201301','201303','201304', \
-
 
33
                '201305','201306','201307']
-
 
34
ludhiyana_pincodes  = ['141001','141002','141003','141004','141005','141006','141007','141008','141009','141010','141011','141012','141013','141014']
-
 
35
 
24
'''
36
'''
25
This class is for only data transfer. Never used outside this module.
37
This class is for only data transfer. Never used outside this module.
26
'''
38
'''
27
class _DeliveryEstimateObject:
39
class _DeliveryEstimateObject:
28
    def __init__(self, delivery_time, reliability, provider_id, warehouse_location):
40
    def __init__(self, delivery_time, reliability, provider_id, warehouse_location, codAllowed):
29
        self.delivery_time = delivery_time
41
        self.delivery_time = delivery_time
30
        self.reliability = reliability
42
        self.reliability = reliability
31
        self.provider_id = provider_id
43
        self.provider_id = provider_id
32
        self.warehouse_location = warehouse_location 
44
        self.warehouse_location = warehouse_location
-
 
45
        self.codAllowed = codAllowed
33
    
46
    
34
def initialize(dbname="logistics", db_hostname="localhost"):
47
def initialize(dbname="logistics", db_hostname="localhost"):
35
    log_entry("initialize@DataAccessor", "Initializing data service")
48
    log_entry("initialize@DataAccessor", "Initializing data service")
36
    DataService.initialize(dbname, db_hostname)
49
    DataService.initialize(dbname, db_hostname)
37
    print "Starting cache population at: " + str(datetime.datetime.now())
50
    print "Starting cache population at: " + str(datetime.datetime.now())
Line 74... Line 87...
74
 
87
 
75
def get_providers():
88
def get_providers():
76
    providers = Provider.query.all()
89
    providers = Provider.query.all()
77
    return providers 
90
    return providers 
78
 
91
 
79
def is_cod_allowed(destination_pin):
-
 
80
    cod = False
-
 
81
    try:
-
 
82
        #FIXME As per ticket location from where only Aramex is servicing, should not be given to Aramex.
-
 
83
        for provider_id, value in serviceable_location_cache.iteritems():
-
 
84
            dest_code, exp, iscod, station_type = value[destination_pin]
-
 
85
            if provider_id == 1:
-
 
86
                cod = cod or iscod
-
 
87
    except Exception as ex:
-
 
88
        print "Unexpected error:", sys.exc_info()[0]
-
 
89
    return cod
-
 
90
 
-
 
91
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
92
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
92
    logging.info("Getting logistics estimation for pincode:" + destination_pin + " and warehouse location: " + str(warehouse_location))
93
    logging.info("Getting logistics estimation for pincode:" + destination_pin + " and warehouse location: " + str(warehouse_location))
93
    if warehouse_location is None:
94
    if warehouse_location is None:
94
        try:
95
        try:
95
            warehouse_location = warehouse_allocation_cache[destination_pin]
96
            warehouse_location = warehouse_allocation_cache[destination_pin]
96
        except:
97
        except:
97
            print "Unexpected error:", sys.exc_info()[0]
98
            print "Unexpected error:", sys.exc_info()[0]
98
            raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
99
            raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
99
    
100
    
100
    provider_id = __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price)
101
    provider_id, codAllowed = __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price)
101
    
102
    
102
    if not provider_id:
103
    if not provider_id:
103
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
104
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
104
                                        ", and warehouse location: " + str(warehouse_location))
105
                                        ", and warehouse location: " + str(warehouse_location))
105
    try:
106
    try:
106
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
107
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
107
        reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
108
        reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
108
        delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location)
109
        delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location, codAllowed)
109
        return delivery_estimate
110
        return delivery_estimate
110
    except Exception as ex:
111
    except Exception as ex:
111
        print ex
112
        print ex
112
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
113
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
113
 
114
 
114
def __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price):
115
def __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price):
-
 
116
    if ncr_pincodes.__contains__(destination_pin):
-
 
117
        if item_selling_price <= 20000:
-
 
118
            return 2, True
-
 
119
    if ludhiyana_pincodes.__contains__(destination_pin):
-
 
120
        if item_selling_price <= 10000:
-
 
121
            return 2, True
-
 
122
    if serviceable_location_cache.get(1).has_key(destination_pin):
-
 
123
        dest_code, exp, iscod, station_type = serviceable_location_cache.get(1).get(destination_pin)
-
 
124
        iscod = iscod and item_selling_price <= 10000
-
 
125
        return 1, iscod
-
 
126
    else:
115
    ret_provider_list = []
127
        return None, False    
-
 
128
    '''
116
    for provider_id, value in serviceable_location_cache.iteritems():
129
    for provider_id, value in serviceable_location_cache.iteritems():
117
        try:
130
        try:
118
            dest_code, exp, cod, station_type = value[destination_pin]
131
            dest_code, exp, cod, station_type = value[destination_pin]
119
        except:
132
        except:
120
            print "Unexpected error:", sys.exc_info()[0]
133
            print "Unexpected error:", sys.exc_info()[0]
Line 136... Line 149...
136
            return destination_provider_allocation_cache.get(destination_pin)[0]
149
            return destination_provider_allocation_cache.get(destination_pin)[0]
137
        else:
150
        else:
138
            return destination_provider_allocation_cache.get(destination_pin)[1]
151
            return destination_provider_allocation_cache.get(destination_pin)[1]
139
    else:
152
    else:
140
        return ret_provider_list[0]
153
        return ret_provider_list[0]
141
 
154
    '''
-
 
155
        
142
def add_empty_AWBs(numbers, provider_id, type):
156
def add_empty_AWBs(numbers, provider_id, type):
143
    for number in numbers:
157
    for number in numbers:
144
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
158
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
145
        try:
159
        try:
146
            query.one()
160
            query.one()