Subversion Repositories SmartDukaan

Rev

Rev 6370 | Rev 6527 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
442 rajveer 1
'''
2
Created on 13-Sep-2010
3
 
4
@author: rajveer
5
'''
6
 
7
from shop2020.logistics.service.impl import DataService
644 chandransh 8
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
3218 rajveer 9
     DeliveryEstimate, WarehouseAllocation, \
5555 rajveer 10
    PublicHolidays, ServiceableLocationDetails, PickupStore
3044 chandransh 11
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
12
    DeliveryType
442 rajveer 13
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
5964 amar.kumar 14
from sqlalchemy.sql import or_
442 rajveer 15
from elixir import session
1730 ankur.sing 16
import datetime, time
1504 ankur.sing 17
import sys
3355 chandransh 18
import logging
19
logging.basicConfig(level=logging.DEBUG)
442 rajveer 20
 
3217 rajveer 21
warehouse_allocation_cache = {}
22
serviceable_location_cache = {}
23
delivery_estimate_cache = {}
6370 rajveer 24
 
5243 rajveer 25
ncr_pincodes = ['110001','110002','110003','110004','110005','110006','110007','110008','110009','110010','110011','110012','110013','110014','110015',\
26
                '110016','110017','110018','110019','110020','110021','110022','110023','110024','110025','110026','110027','110028','110029','110030',\
5692 rajveer 27
                '110031','110032','110033','110034','110035','110037','110038','110041','110042','110044','110045','110046','110047','110048','110049',\
28
                '110051','110052','110053','110054','110055','110056','110057','110058','110059','110060','110061','110062','110063','110064','110065',\
29
                '110066','110067','110068','110070','110071','110074','110075','110076','110078','110081','110082','110083','110084','110085','110086',\
30
                '110087','110088','110089','110091','110092','110093','110094','110095','110096','110101','110103','110104','110105','110106','110107',\
31
                '110108','110109','110110','110112','110113','110114','110115','110116','110117','110118','110119','110120','110122','110124','110125',\
32
                '110301','110302','110501','110502','110503','110504','110505','110510','110511','110512','110601','110602','110603','110604','110605',\
33
                '110606','110607','110608','110609','121001','121002','121003','121004','121005','121006','121007','121008','121009','122001','122002',\
6370 rajveer 34
                '122003','122005','122006','122008','122009','122010','122015','122017','201001','201002','201003','201004','201005','201006','201007',\
35
                '201008','201009','201010','201011','201012','201014','201301','201302','201303','201304','201305','201306','201307','201309','201310',\
36
                '201311']
37
 
3218 rajveer 38
'''
39
This class is for only data transfer. Never used outside this module.
40
'''
41
class _DeliveryEstimateObject:
6524 rajveer 42
    def __init__(self, delivery_time, reliability, provider_id, codAllowed, otgAvailable):
3218 rajveer 43
        self.delivery_time = delivery_time
44
        self.reliability = reliability
45
        self.provider_id = provider_id
4866 rajveer 46
        self.codAllowed = codAllowed
6524 rajveer 47
        self.otgAvailable = otgAvailable
3218 rajveer 48
 
3187 rajveer 49
def initialize(dbname="logistics", db_hostname="localhost"):
442 rajveer 50
    log_entry("initialize@DataAccessor", "Initializing data service")
3187 rajveer 51
    DataService.initialize(dbname, db_hostname)
3218 rajveer 52
    print "Starting cache population at: " + str(datetime.datetime.now())
3217 rajveer 53
    __cache_warehouse_allocation_table()
54
    __cache_serviceable_location_details_table()
55
    __cache_delivery_estimate_table()
56
    close_session()
3218 rajveer 57
    print "Done cache population at: " + str(datetime.datetime.now())
442 rajveer 58
 
3217 rajveer 59
def __cache_delivery_estimate_table():
60
    delivery_estimates = DeliveryEstimate.query.all()
61
    for delivery_estimate in delivery_estimates:
5692 rajveer 62
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id]\
3217 rajveer 63
        =delivery_estimate.delivery_time, delivery_estimate.reliability
64
 
65
def __cache_serviceable_location_details_table():
5964 amar.kumar 66
    serviceable_locations = ServiceableLocationDetails.query.filter(or_("exp!=0","cod!=0"))
3217 rajveer 67
    for serviceable_location in serviceable_locations:
68
        try:
69
            provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
70
        except:
71
            provider_pincodes = {}
72
            serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes 
6524 rajveer 73
        provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.otgAvailable
3217 rajveer 74
 
75
def __cache_warehouse_allocation_table():
76
    warehouse_allocations = WarehouseAllocation.query.all()
77
    for warehouse_allocation in warehouse_allocations:
78
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
79
 
644 chandransh 80
def get_provider(provider_id):
766 rajveer 81
    provider =  Provider.get_by(id=provider_id)
82
    return provider
644 chandransh 83
 
84
def get_providers():
5387 rajveer 85
    providers = Provider.query.filter_by(isActive = 1).all()
766 rajveer 86
    return providers 
644 chandransh 87
 
6524 rajveer 88
def get_logistics_estimation(destination_pin, item_selling_price):
5692 rajveer 89
    logging.info("Getting logistics estimation for pincode:" + destination_pin )
1504 ankur.sing 90
 
6524 rajveer 91
    provider_id, codAllowed, otgAvailable = __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price)
3064 chandransh 92
 
3217 rajveer 93
    if not provider_id:
5692 rajveer 94
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin))
644 chandransh 95
    try:
5692 rajveer 96
        delivery_time = delivery_estimate_cache[destination_pin, provider_id][0]
97
        reliability = delivery_estimate_cache[destination_pin, provider_id][1]
6524 rajveer 98
        delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, codAllowed, otgAvailable)
3218 rajveer 99
        return delivery_estimate
1504 ankur.sing 100
    except Exception as ex:
101
        print ex
644 chandransh 102
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
3150 rajveer 103
 
6524 rajveer 104
def __get_logistics_provider_for_destination_pincode(destination_pin, item_selling_price):
5624 rajveer 105
    if ncr_pincodes.__contains__(destination_pin) and item_selling_price <= 20000:
6524 rajveer 106
        dest_code, exp, iscod, otgAvailable = serviceable_location_cache.get(3).get(destination_pin)
107
        otgAvailable = otgAvailable and item_selling_price >= 2000
108
        return 3, True, otgAvailable
6308 amit.gupta 109
 
5692 rajveer 110
    if serviceable_location_cache.get(3).has_key(destination_pin):
6524 rajveer 111
        dest_code, exp, iscod, otgAvailable = serviceable_location_cache.get(3).get(destination_pin)
6237 rajveer 112
        iscod = iscod and item_selling_price <= 9600
6524 rajveer 113
        otgAvailable = otgAvailable and item_selling_price >= 2000
114
        return 3, iscod, otgAvailable
6370 rajveer 115
 
4866 rajveer 116
    if serviceable_location_cache.get(1).has_key(destination_pin):
6524 rajveer 117
        dest_code, exp, iscod, otgAvailable = serviceable_location_cache.get(1).get(destination_pin)
6237 rajveer 118
        iscod = iscod and item_selling_price <= 9600
6524 rajveer 119
        otgAvailable = otgAvailable and item_selling_price >= 2000
120
        return 1, iscod, otgAvailable
4866 rajveer 121
    else:
122
        return None, False    
5278 rajveer 123
 
6017 amar.kumar 124
def get_destination_code(providerId, pinCode):
125
    serviceableLocationDetail = ServiceableLocationDetails.query.filter_by(provider_id = providerId, dest_pincode = pinCode).one()
126
    return serviceableLocationDetail.dest_code
127
 
644 chandransh 128
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 129
    for number in numbers:
644 chandransh 130
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 131
        try:
132
            query.one()
133
        except:    
644 chandransh 134
            awb = Awb()
444 rajveer 135
            awb.awb_number = number
644 chandransh 136
            awb.provider_id = provider_id
444 rajveer 137
            awb.is_available = True
644 chandransh 138
            awb.type = type
139
    session.commit()
442 rajveer 140
 
444 rajveer 141
def set_AWB_as_used(awb_number):
644 chandransh 142
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 143
    awb = query.one() 
144
    awb.is_available=False
145
    session.commit()
146
 
3044 chandransh 147
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
2342 chandransh 148
    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
3044 chandransh 149
    if type == DeliveryType.PREPAID:
150
        query = query.filter_by(type="Prepaid")
151
    if type == DeliveryType.COD:
152
        query = query.filter_by(type="COD")
153
 
442 rajveer 154
    try:
644 chandransh 155
        awb = query.first()
156
        awb.is_available = False
157
        session.commit()
444 rajveer 158
        return awb.awb_number
442 rajveer 159
    except:
644 chandransh 160
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
1137 chandransh 161
 
3103 chandransh 162
def get_free_awb_count(provider_id, type):
163
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
1137 chandransh 164
    if count == None:
165
        count = 0
166
    return count
442 rajveer 167
 
644 chandransh 168
def get_shipment_info(awb_number, provider_id):
169
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
170
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 171
    info = query.all()
172
    return info
173
 
1730 ankur.sing 174
def get_holidays(start_date, end_date):
175
    query = PublicHolidays.query
176
    if start_date != -1:
177
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
178
    if end_date != -1:
179
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
180
    holidays = query.all()
181
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
182
    return holiday_dates
183
 
5527 anupam.sin 184
def get_provider_for_pickup_type(pickUp):
185
    return Provider.query.filter(Provider.pickup == pickUp).first().id
186
 
766 rajveer 187
def close_session():
188
    if session.is_active:
189
        print "session is active. closing it."
2823 chandransh 190
        session.close()
3376 rajveer 191
 
192
def is_alive():
193
    try:
194
        session.query(Awb.id).limit(1).one()
195
        return True
196
    except:
5555 rajveer 197
        return False
198
 
199
def get_all_pickup_stores():
5572 anupam.sin 200
    pickupStores = PickupStore.query.all()
201
    return pickupStores
5555 rajveer 202
 
203
def get_pickup_store(storeId):
5719 rajveer 204
    return PickupStore.query.filter_by(id = storeId).one()
205
 
206
def get_pickup_store_by_hotspot_id(hotspotId):
207
    return PickupStore.query.filter_by(hotspotId = hotspotId).one()
6322 amar.kumar 208
 
6524 rajveer 209
def add_pincode(provider, pincode, destCode, exp, cod, stationType, otgAvailable):
6322 amar.kumar 210
    provider = Provider().query.filter_by(id = provider).one()
211
    serviceableLocationDetails = ServiceableLocationDetails()
212
    serviceableLocationDetails.provider = provider
213
    serviceableLocationDetails.dest_pincode = pincode
214
    serviceableLocationDetails.dest_code = destCode
215
    serviceableLocationDetails.exp = exp
216
    serviceableLocationDetails.cod = cod
217
    serviceableLocationDetails.station_type = stationType
6524 rajveer 218
    serviceableLocationDetails.otgAvailable = otgAvailable
6322 amar.kumar 219
    session.commit()
220
 
6524 rajveer 221
def update_pincode(providerId, pincode, exp, cod, otgAvailable):
6322 amar.kumar 222
    serviceableLocationDetails = ServiceableLocationDetails().get_by(provider_id = providerId, dest_pincode = pincode).one()
223
    serviceableLocationDetails.exp = exp
224
    serviceableLocationDetails.cod = cod
6524 rajveer 225
    serviceableLocationDetails.otgAvailable = otgAvailable
6322 amar.kumar 226
    session.commit()