Subversion Repositories SmartDukaan

Rev

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

Rev 19230 Rev 19413
Line 8... Line 8...
8
from shop2020.clients.CatalogClient import CatalogClient
8
from shop2020.clients.CatalogClient import CatalogClient
9
from shop2020.clients.InventoryClient import InventoryClient
9
from shop2020.clients.InventoryClient import InventoryClient
10
from shop2020.logistics.service.impl import DataService
10
from shop2020.logistics.service.impl import DataService
11
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
11
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
12
    DeliveryEstimate, WarehouseAllocation, PublicHolidays, \
12
    DeliveryEstimate, WarehouseAllocation, PublicHolidays, \
13
    ServiceableLocationDetails, PickupStore
13
    ServiceableLocationDetails, PickupStore, Locations
14
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
14
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException, \
15
    DeliveryType, LogisticsLocationInfo
15
    DeliveryType, LogisticsLocationInfo, LocationInfo, ProviderInfo
16
from shop2020.thriftpy.model.v1.inventory.ttypes import Warehouse, BillingType,\
16
from shop2020.thriftpy.model.v1.inventory.ttypes import Warehouse, BillingType, \
17
    WarehouseType
17
    WarehouseType
18
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
18
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
19
from sqlalchemy.sql import or_
19
from sqlalchemy.sql import or_
20
import datetime
20
import datetime
21
import logging
21
import logging
22
import os
22
import os
23
import sys
23
import sys
24
import time
24
import time
-
 
25
from collections import namedtuple
-
 
26
 
25
logging.basicConfig(level=logging.DEBUG)
27
logging.basicConfig(level=logging.DEBUG)
26
 
28
 
-
 
29
PincodeProvider = namedtuple("PincodeProvider", ["dest_pin", "provider"])
-
 
30
 
27
warehouse_allocation_cache = {}
31
warehouse_allocation_cache = {}
28
serviceable_location_cache = {}
32
serviceable_location_cache = {}
29
delivery_estimate_cache = {}
33
delivery_estimate_cache = {}
30
warehouse_location_cache = {}
34
warehouse_location_cache = {}
-
 
35
location_state={}
-
 
36
state_locations = {}
-
 
37
 
-
 
38
#pincode location map is ab
-
 
39
#{'110001':{1:{"sameState":False, "providerInfo":{1:(1,1), 2:(1,1)}}},}
-
 
40
pincode_locations = {}
-
 
41
#{(dest_pin, provider):(otgAvailable,providerCodLimit, websiteCodLimit, storeCodLimit, providerPrepaidLimit)} 
-
 
42
serviceable_map = {}
-
 
43
 
-
 
44
#Need to identify a better way
-
 
45
statepinmap={'11':0, 
-
 
46
             '40':1,'41':1,'42':1,'43':1,'44':1,
-
 
47
             '56':2, '57':2, '58':2, '59':2,
-
 
48
             '12':3, '13':3, 
-
 
49
             '30':4, '31':4, '32':4, '33':4, '34':4,
-
 
50
             '36':6,'37':6,'38':6,'39':6
-
 
51
             }
31
 
52
 
32
'''
53
'''
33
This class is for only data transfer. Never used outside this module.
54
This class is for only data transfer. Never used outside this module.
34
'''
55
'''
35
class _DeliveryEstimateObject:
56
class _DeliveryEstimateObject:
Line 46... Line 67...
46
    print "Starting cache population at: " + str(datetime.datetime.now())
67
    print "Starting cache population at: " + str(datetime.datetime.now())
47
    #__cache_warehouse_allocation_table()
68
    #__cache_warehouse_allocation_table()
48
    __cache_warehouse_locations()
69
    __cache_warehouse_locations()
49
    __cache_serviceable_location_details_table()
70
    __cache_serviceable_location_details_table()
50
    __cache_delivery_estimate_table()
71
    __cache_delivery_estimate_table()
-
 
72
    __cache_locations()
-
 
73
    __cache_pincode_estimates()    
51
    close_session()
74
    close_session()
52
    print "Done cache population at: " + str(datetime.datetime.now())
75
    print "Done cache population at: " + str(datetime.datetime.now())
53
 
76
 
-
 
77
def __cache_locations():
-
 
78
    locations = Locations.query.all()
-
 
79
    for location in locations:
-
 
80
        location_state[location.id] = location.state_id
-
 
81
        if not state_locations.has_key(location.state_id):
-
 
82
            state_locations[location.state_id] = []
-
 
83
        state_locations[location.state_id] = state_locations[location.state_id].append(location.state_id)
-
 
84
 
-
 
85
def __cache_pincode_estimates():
-
 
86
    delivery_estimates = DeliveryEstimate.query.all()
-
 
87
    for delivery_estimate in delivery_estimates:
-
 
88
        if not pincode_locations.has_key(delivery_estimate.destination_pin):
-
 
89
            pincode_locations[delivery_estimate.destination_pin] = {}
-
 
90
        destination_pinMap = pincode_locations[delivery_estimate.destination_pin] 
-
 
91
        if not destination_pinMap.has_key(delivery_estimate.warehouse_location):
-
 
92
            destination_pinMap[delivery_estimate.warehouse_location] = {}
-
 
93
            state_id = location_state.get(delivery_estimate.warehouse_location)
-
 
94
            sameState = __getStateFromPin(delivery_estimate.destination_pin) == state_id
-
 
95
            destination_pinMap[delivery_estimate.warehouse_location]['sameState']=sameState
-
 
96
            destination_pinMap[delivery_estimate.warehouse_location]['providerInfo'] = {} 
-
 
97
        destination_pinMap[delivery_estimate.warehouse_location]['providerInfo'][delivery_estimate.provider_id] = delivery_estimate.delivery_time + delivery_estimate.delivery_delay
-
 
98
        
-
 
99
 
-
 
100
    
-
 
101
def __cache_pincode_provider_serviceability():
-
 
102
    pincode_providers = ServiceableLocationDetails.query.filter(ServiceableLocationDetails.exp==1)
-
 
103
    for pp in pincode_providers:
-
 
104
        serviceable_map[PincodeProvider(pp.dest_pincode, pp.provider_id)] = (pp.cod, pp.otgAvailable,pp.providerCodLimit, pp.websiteCodLimit, pp.storeCodLimit, pp.providerPrepaidLimit)
-
 
105
        
-
 
106
 
54
def __cache_delivery_estimate_table():
107
def __cache_delivery_estimate_table():
55
    delivery_estimates = DeliveryEstimate.query.all()
108
    delivery_estimates = DeliveryEstimate.query.all()
56
    for delivery_estimate in delivery_estimates:
109
    for delivery_estimate in delivery_estimates:
57
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
110
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
58
        =delivery_estimate.delivery_time, delivery_estimate.delivery_delay
111
        =delivery_estimate.delivery_time, delivery_estimate.delivery_delay
Line 72... Line 125...
72
    for warehouse_allocation in warehouse_allocations:
125
    for warehouse_allocation in warehouse_allocations:
73
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
126
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
74
 
127
 
75
def __cache_warehouse_locations():
128
def __cache_warehouse_locations():
76
    client = InventoryClient().get_client()
129
    client = InventoryClient().get_client()
77
    client.getAllWarehouses(True)
130
    #client.getAllWarehouses(True)
78
    #for WarehouseAllocation.query.all()
-
 
79
    warehouse = Warehouse()
-
 
80
    for warehouse in client.getAllWarehouses(True):
131
    for warehouse in client.getAllWarehouses(True):
81
        warehouse_location_cache[warehouse.billingWarehouseId]=warehouse.logisticsLocation
132
        warehouse_location_cache[warehouse.billingWarehouseId]=warehouse.logisticsLocation
82
 
133
 
83
def get_provider(provider_id):
134
def get_provider(provider_id):
84
    provider =  Provider.get_by(id=provider_id)
135
    provider =  Provider.get_by(id=provider_id)
85
    return provider
136
    return provider
86
 
137
 
87
def get_providers():
138
def get_providers():
88
    providers = Provider.query.filter_by(isActive = 1).all()
139
    providers = Provider.query.filter_by(isActive = 1).all()
89
    return providers 
140
    return providers
90
 
141
 
-
 
142
#it would return only states where our warehouses are present else return -1
-
 
143
def __getStateFromPin(pin):
-
 
144
    if pin[:3] in ['744', '682']:
-
 
145
        return -1
-
 
146
    elif statepinmap.has_key(pin[:2]):
-
 
147
        return statepinmap[pin[:2]]
-
 
148
    else:
-
 
149
        return -1
-
 
150
           
-
 
151
     
-
 
152
def get_logistics_locations(destination_pin, selling_price_list):
-
 
153
    #pincode_locations = {1:{"sameState":False, "providerInfo":{<provider_id>:<delay_days>}},}
-
 
154
    #pp.otgAvailable,pp.providerCodLimit, pp.websiteCodLimit, pp.storeCodLimit, pp.providerPrepaidLimit
-
 
155
    returnMap = {}
-
 
156
    if pincode_locations.has_key(destination_pin):
-
 
157
        locations = pincode_locations[destination_pin]
-
 
158
        for item_selling_price in selling_price_list:
-
 
159
            returnMap[item_selling_price] = {}
-
 
160
            for location_id, value in locations.iteritems():
-
 
161
                returnMap[item_selling_price][location_id] = LocationInfo(locationId = location_id, sameState=value['sameState'])
-
 
162
                #put weight logic here
-
 
163
                otgAvailable = False
-
 
164
                codAvailable = False
-
 
165
                minDelay = -1
-
 
166
                maxDelay = -1
-
 
167
                serviceable = False
-
 
168
                for provider_id, delay_days in value['providerInfo'].iteritems():
-
 
169
                    iscod, isotg, providerCodLimit, websiteCodLimit, storeCodLimit, providerPrepaidLimit = serviceable_map[PincodeProvider(destination_pin, provider_id)]
-
 
170
                    if item_selling_price <= providerPrepaidLimit:
-
 
171
                        if not serviceable:
-
 
172
                            serviceable = True
-
 
173
                            minDelay = delay_days
-
 
174
                            maxDelay = delay_days
-
 
175
                        else:
-
 
176
                            minDelay = min(delay_days,minDelay)
-
 
177
                            maxDelay = max(delay_days,maxDelay)
-
 
178
                        iscod = iscod and item_selling_price <= min(providerCodLimit, websiteCodLimit)
-
 
179
                        isotg = (isotg and item_selling_price >= 2000)
-
 
180
                        otgAvailable = otgAvailable or isotg
-
 
181
                        codAvailable = codAvailable or iscod
-
 
182
                if serviceable:        
-
 
183
                    returnMap[item_selling_price][location_id].otgAvailable = otgAvailable
-
 
184
                    returnMap[item_selling_price][location_id].codAvailable = codAvailable
-
 
185
                    returnMap[item_selling_price][location_id].minDelay = minDelay
-
 
186
                    returnMap[item_selling_price][location_id].maxDelay = maxDelay
-
 
187
    return returnMap
-
 
188
    
91
def get_logistics_estimation(destination_pin, item_selling_price, weight, type, billingWarehouseId):
189
def get_logistics_estimation(destination_pin, item_selling_price, weight, type, billingWarehouseId):
92
    logging.info("Getting logistics estimation for pincode:" + destination_pin )
190
    logging.info("Getting logistics estimation for pincode:" + destination_pin )
93
    
191
    
94
    provider_id, codAllowed, otgAvailable = __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price, weight, type, billingWarehouseId)
192
    provider_id, codAllowed, otgAvailable = __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price, weight, type, billingWarehouseId)
95
    
193
    
Line 175... Line 273...
175
            awb.awb_number = number
273
            awb.awb_number = number
176
            awb.provider_id = provider_id
274
            awb.provider_id = provider_id
177
            awb.is_available = True
275
            awb.is_available = True
178
            awb.type = type
276
            awb.type = type
179
    session.commit()
277
    session.commit()
-
 
278
    
-
 
279
def get_pincode_item_serviceability():
-
 
280
    pass
180
 
281
 
181
def set_AWB_as_used(awb_number):
282
def set_AWB_as_used(awb_number):
182
    query = Awb.query.filter_by(awb_number = awb_number)
283
    query = Awb.query.filter_by(awb_number = awb_number)
183
    awb = query.one() 
284
    awb = query.one() 
184
    awb.is_available=False
285
    awb.is_available=False