Subversion Repositories SmartDukaan

Rev

Rev 5572 | Rev 5624 | 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
14
from elixir import session
1730 ankur.sing 15
import datetime, time
1504 ankur.sing 16
import sys
3355 chandransh 17
import logging
18
logging.basicConfig(level=logging.DEBUG)
442 rajveer 19
 
3217 rajveer 20
warehouse_allocation_cache = {}
21
serviceable_location_cache = {}
22
delivery_estimate_cache = {}
5243 rajveer 23
ncr_pincodes = ['110001','110002','110003','110004','110005','110006','110007','110008','110009','110010','110011','110012','110013','110014','110015',\
24
                '110016','110017','110018','110019','110020','110021','110022','110023','110024','110025','110026','110027','110028','110029','110030',\
25
                '110031','110032','110033','110034','110035','110038','110041','110044','110045','110046','110047','110048','110049','110051','110052',\
26
                '110053','110054','110055','110056','110057','110058','110059','110060','110061','110062','110063','110064','110065','110066','110067',\
27
                '110068','110070','110071','110074','110075','110076','110078','110081','110082','110083','110084','110085','110086','110087','110088',\
28
                '110089','110091','110092','110093','110094','110095','110096','110101','110103','110104','110105','110106','110107','110108','110109',\
29
                '110110','110112','110113','110114','110115','110116','110117','110118','110119','110120','110122','110124','110125','110301','110302',\
30
                '110501','110502','110503','110504','110505','110510','110511','110512','110601','110602','110603','110604','110605','110606','110607',\
31
                '110608','110609','122001','122002','122003','122005','122006','122008','122009','122010','122015','122017','201010','201011','201012',\
32
                '201014','201301','201302','201303','201304','201305','201306','201307','201309','201310','201311']
4866 rajveer 33
 
5482 rajveer 34
chennai_pincodes = ['600001','600002','600003','600004','600005','600006','600007','600008','600009','600010','600011','600012','600013','600014',\
35
                    '600015','600016','600017','600018','600019','600020','600021','600022','600023','600024','600025','600026','600027','600028',\
36
                    '600029','600030','600031','600032','600033','600034','600035','600036','600037','600038','600039','600040','600041','600042',\
37
                    '600043','600044','600045','600046','600047','600049','600050','600051','600053','600054','600055','600056','600058','600059',\
38
                    '600060','600061','600062','600063','600064','600069','600070','600071','600073','600074','600075','600076','600077','600078',\
39
                    '600079','600080','600081','600082','600083','600084','600085','600086','600087','600088','600089','600090','600091','600092',\
40
                    '600093','600094','600095','600096','600097','600098','600099','600100','600101','600102','600104','600105','600106','600107',\
41
                    '600108','600110','600111','600112','600113','600118','600122','600126']
5605 rajveer 42
 
43
hyd_pincodes = ['500001','500002','500003','500004','500005','500006','500007','500008','500009','500010','500011','500012','500013','500014','500015',\
44
                '500016','500017','500018','500019','500020','500021','500022','500023','500024','500025','500026','500027','500028','500029','500030',\
45
                '500032','500033','500034','500035','500036','500037','500038','500039','500040','500041','500042','500044','500045','500047','500048',\
46
                '500049','500050','500051','500052','500053','500054','500055','500056','500057','500058','500059','500060','500061','500062','500063',\
47
                '500064','500065','500066','500067','500068','500069','500070','500071','500072','500073','500074','500076','500077','500079','500080',\
48
                '500081','500082','500085','500095']
3218 rajveer 49
'''
50
This class is for only data transfer. Never used outside this module.
51
'''
52
class _DeliveryEstimateObject:
4866 rajveer 53
    def __init__(self, delivery_time, reliability, provider_id, warehouse_location, codAllowed):
3218 rajveer 54
        self.delivery_time = delivery_time
55
        self.reliability = reliability
56
        self.provider_id = provider_id
4866 rajveer 57
        self.warehouse_location = warehouse_location
58
        self.codAllowed = codAllowed
3218 rajveer 59
 
3187 rajveer 60
def initialize(dbname="logistics", db_hostname="localhost"):
442 rajveer 61
    log_entry("initialize@DataAccessor", "Initializing data service")
3187 rajveer 62
    DataService.initialize(dbname, db_hostname)
3218 rajveer 63
    print "Starting cache population at: " + str(datetime.datetime.now())
3217 rajveer 64
    __cache_warehouse_allocation_table()
65
    __cache_serviceable_location_details_table()
66
    __cache_delivery_estimate_table()
67
    close_session()
3218 rajveer 68
    print "Done cache population at: " + str(datetime.datetime.now())
442 rajveer 69
 
3217 rajveer 70
def __cache_delivery_estimate_table():
71
    delivery_estimates = DeliveryEstimate.query.all()
72
    for delivery_estimate in delivery_estimates:
73
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
74
        =delivery_estimate.delivery_time, delivery_estimate.reliability
75
 
76
def __cache_serviceable_location_details_table():
77
    serviceable_locations = ServiceableLocationDetails.query.all()
78
    for serviceable_location in serviceable_locations:
79
        try:
80
            provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
81
        except:
82
            provider_pincodes = {}
83
            serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes 
84
        provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.station_type 
85
 
86
def __cache_warehouse_allocation_table():
87
    warehouse_allocations = WarehouseAllocation.query.all()
88
    for warehouse_allocation in warehouse_allocations:
89
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
90
 
644 chandransh 91
def get_provider(provider_id):
766 rajveer 92
    provider =  Provider.get_by(id=provider_id)
93
    return provider
644 chandransh 94
 
95
def get_providers():
5387 rajveer 96
    providers = Provider.query.filter_by(isActive = 1).all()
766 rajveer 97
    return providers 
644 chandransh 98
 
3064 chandransh 99
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
3355 chandransh 100
    logging.info("Getting logistics estimation for pincode:" + destination_pin + " and warehouse location: " + str(warehouse_location))
2341 chandransh 101
    if warehouse_location is None:
102
        try:
3217 rajveer 103
            warehouse_location = warehouse_allocation_cache[destination_pin]
2341 chandransh 104
        except:
105
            print "Unexpected error:", sys.exc_info()[0]
3064 chandransh 106
            raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
1504 ankur.sing 107
 
4866 rajveer 108
    provider_id, codAllowed = __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price)
3064 chandransh 109
 
3217 rajveer 110
    if not provider_id:
1504 ankur.sing 111
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
112
                                        ", and warehouse location: " + str(warehouse_location))
644 chandransh 113
    try:
3218 rajveer 114
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
115
        reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
4866 rajveer 116
        delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location, codAllowed)
3218 rajveer 117
        return delivery_estimate
1504 ankur.sing 118
    except Exception as ex:
119
        print ex
644 chandransh 120
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
3150 rajveer 121
 
4439 rajveer 122
def __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price):
4866 rajveer 123
    if ncr_pincodes.__contains__(destination_pin):
124
        if item_selling_price <= 20000:
5243 rajveer 125
            return 3, True
5482 rajveer 126
    if type == DeliveryType.COD and chennai_pincodes.__contains__(destination_pin):
127
        if item_selling_price <= 10000:
5605 rajveer 128
            return 3, True
129
    if type == DeliveryType.COD and hyd_pincodes.__contains__(destination_pin):
130
        if item_selling_price <= 10000: 
131
            return 3, True
132
    if type == DeliveryType.PREPAID and hyd_pincodes.__contains__(destination_pin):
133
        if item_selling_price <= 5000: 
134
            return 3, True
4866 rajveer 135
    if serviceable_location_cache.get(1).has_key(destination_pin):
136
        dest_code, exp, iscod, station_type = serviceable_location_cache.get(1).get(destination_pin)
137
        iscod = iscod and item_selling_price <= 10000
138
        return 1, iscod
5482 rajveer 139
    elif type == DeliveryType.PREPAID and chennai_pincodes.__contains__(destination_pin):
140
        return 3, False
4866 rajveer 141
    else:
142
        return None, False    
5278 rajveer 143
 
4866 rajveer 144
 
644 chandransh 145
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 146
    for number in numbers:
644 chandransh 147
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 148
        try:
149
            query.one()
150
        except:    
644 chandransh 151
            awb = Awb()
444 rajveer 152
            awb.awb_number = number
644 chandransh 153
            awb.provider_id = provider_id
444 rajveer 154
            awb.is_available = True
644 chandransh 155
            awb.type = type
156
    session.commit()
442 rajveer 157
 
444 rajveer 158
def set_AWB_as_used(awb_number):
644 chandransh 159
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 160
    awb = query.one() 
161
    awb.is_available=False
162
    session.commit()
163
 
3044 chandransh 164
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
2342 chandransh 165
    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
3044 chandransh 166
    if type == DeliveryType.PREPAID:
167
        query = query.filter_by(type="Prepaid")
168
    if type == DeliveryType.COD:
169
        query = query.filter_by(type="COD")
170
 
442 rajveer 171
    try:
644 chandransh 172
        awb = query.first()
173
        awb.is_available = False
174
        session.commit()
444 rajveer 175
        return awb.awb_number
442 rajveer 176
    except:
644 chandransh 177
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
1137 chandransh 178
 
3103 chandransh 179
def get_free_awb_count(provider_id, type):
180
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
1137 chandransh 181
    if count == None:
182
        count = 0
183
    return count
442 rajveer 184
 
644 chandransh 185
def get_shipment_info(awb_number, provider_id):
186
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
187
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 188
    info = query.all()
189
    return info
190
 
1730 ankur.sing 191
def get_holidays(start_date, end_date):
192
    query = PublicHolidays.query
193
    if start_date != -1:
194
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
195
    if end_date != -1:
196
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
197
    holidays = query.all()
198
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
199
    return holiday_dates
200
 
5527 anupam.sin 201
def get_provider_for_pickup_type(pickUp):
202
    return Provider.query.filter(Provider.pickup == pickUp).first().id
203
 
766 rajveer 204
def close_session():
205
    if session.is_active:
206
        print "session is active. closing it."
2823 chandransh 207
        session.close()
3376 rajveer 208
 
209
def is_alive():
210
    try:
211
        session.query(Awb.id).limit(1).one()
212
        return True
213
    except:
5555 rajveer 214
        return False
215
 
216
def get_all_pickup_stores():
5572 anupam.sin 217
    pickupStores = PickupStore.query.all()
218
    return pickupStores
5555 rajveer 219
 
220
def get_pickup_store(storeId):
5572 anupam.sin 221
    return PickupStore.query.filter_by(id = storeId).one()