Subversion Repositories SmartDukaan

Rev

Rev 5278 | Rev 5482 | 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, \
5278 rajveer 10
    PublicHolidays, ServiceableLocationDetails
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
 
3218 rajveer 34
'''
35
This class is for only data transfer. Never used outside this module.
36
'''
37
class _DeliveryEstimateObject:
4866 rajveer 38
    def __init__(self, delivery_time, reliability, provider_id, warehouse_location, codAllowed):
3218 rajveer 39
        self.delivery_time = delivery_time
40
        self.reliability = reliability
41
        self.provider_id = provider_id
4866 rajveer 42
        self.warehouse_location = warehouse_location
43
        self.codAllowed = codAllowed
3218 rajveer 44
 
3187 rajveer 45
def initialize(dbname="logistics", db_hostname="localhost"):
442 rajveer 46
    log_entry("initialize@DataAccessor", "Initializing data service")
3187 rajveer 47
    DataService.initialize(dbname, db_hostname)
3218 rajveer 48
    print "Starting cache population at: " + str(datetime.datetime.now())
3217 rajveer 49
    __cache_warehouse_allocation_table()
50
    __cache_serviceable_location_details_table()
51
    __cache_delivery_estimate_table()
52
    close_session()
3218 rajveer 53
    print "Done cache population at: " + str(datetime.datetime.now())
442 rajveer 54
 
3217 rajveer 55
def __cache_delivery_estimate_table():
56
    delivery_estimates = DeliveryEstimate.query.all()
57
    for delivery_estimate in delivery_estimates:
58
        delivery_estimate_cache[delivery_estimate.destination_pin, delivery_estimate.provider_id, delivery_estimate.warehouse_location]\
59
        =delivery_estimate.delivery_time, delivery_estimate.reliability
60
 
61
def __cache_serviceable_location_details_table():
62
    serviceable_locations = ServiceableLocationDetails.query.all()
63
    for serviceable_location in serviceable_locations:
64
        try:
65
            provider_pincodes = serviceable_location_cache[serviceable_location.provider_id]
66
        except:
67
            provider_pincodes = {}
68
            serviceable_location_cache[serviceable_location.provider_id] = provider_pincodes 
69
        provider_pincodes[serviceable_location.dest_pincode] = serviceable_location.dest_code, serviceable_location.exp, serviceable_location.cod, serviceable_location.station_type 
70
 
71
def __cache_warehouse_allocation_table():
72
    warehouse_allocations = WarehouseAllocation.query.all()
73
    for warehouse_allocation in warehouse_allocations:
74
        warehouse_allocation_cache[warehouse_allocation.pincode]=warehouse_allocation.primary_warehouse_location
75
 
644 chandransh 76
def get_provider(provider_id):
766 rajveer 77
    provider =  Provider.get_by(id=provider_id)
78
    return provider
644 chandransh 79
 
80
def get_providers():
5387 rajveer 81
    providers = Provider.query.filter_by(isActive = 1).all()
766 rajveer 82
    return providers 
644 chandransh 83
 
3064 chandransh 84
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
3355 chandransh 85
    logging.info("Getting logistics estimation for pincode:" + destination_pin + " and warehouse location: " + str(warehouse_location))
2341 chandransh 86
    if warehouse_location is None:
87
        try:
3217 rajveer 88
            warehouse_location = warehouse_allocation_cache[destination_pin]
2341 chandransh 89
        except:
90
            print "Unexpected error:", sys.exc_info()[0]
3064 chandransh 91
            raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
1504 ankur.sing 92
 
4866 rajveer 93
    provider_id, codAllowed = __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price)
3064 chandransh 94
 
3217 rajveer 95
    if not provider_id:
1504 ankur.sing 96
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
97
                                        ", and warehouse location: " + str(warehouse_location))
644 chandransh 98
    try:
3218 rajveer 99
        delivery_time = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][0]
100
        reliability = delivery_estimate_cache[destination_pin, provider_id, warehouse_location][1]
4866 rajveer 101
        delivery_estimate = _DeliveryEstimateObject(delivery_time, reliability, provider_id, warehouse_location, codAllowed)
3218 rajveer 102
        return delivery_estimate
1504 ankur.sing 103
    except Exception as ex:
104
        print ex
644 chandransh 105
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
3150 rajveer 106
 
4439 rajveer 107
def __get_logistics_provider_for_destination_pincode(type, destination_pin, item_selling_price):
4866 rajveer 108
    if ncr_pincodes.__contains__(destination_pin):
109
        if item_selling_price <= 20000:
5243 rajveer 110
            return 3, True
4866 rajveer 111
    if serviceable_location_cache.get(1).has_key(destination_pin):
112
        dest_code, exp, iscod, station_type = serviceable_location_cache.get(1).get(destination_pin)
113
        iscod = iscod and item_selling_price <= 10000
114
        return 1, iscod
115
    else:
116
        return None, False    
5278 rajveer 117
 
4866 rajveer 118
 
644 chandransh 119
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 120
    for number in numbers:
644 chandransh 121
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 122
        try:
123
            query.one()
124
        except:    
644 chandransh 125
            awb = Awb()
444 rajveer 126
            awb.awb_number = number
644 chandransh 127
            awb.provider_id = provider_id
444 rajveer 128
            awb.is_available = True
644 chandransh 129
            awb.type = type
130
    session.commit()
442 rajveer 131
 
444 rajveer 132
def set_AWB_as_used(awb_number):
644 chandransh 133
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 134
    awb = query.one() 
135
    awb.is_available=False
136
    session.commit()
137
 
3044 chandransh 138
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
2342 chandransh 139
    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
3044 chandransh 140
    if type == DeliveryType.PREPAID:
141
        query = query.filter_by(type="Prepaid")
142
    if type == DeliveryType.COD:
143
        query = query.filter_by(type="COD")
144
 
442 rajveer 145
    try:
644 chandransh 146
        awb = query.first()
147
        awb.is_available = False
148
        session.commit()
444 rajveer 149
        return awb.awb_number
442 rajveer 150
    except:
644 chandransh 151
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
1137 chandransh 152
 
3103 chandransh 153
def get_free_awb_count(provider_id, type):
154
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True, type=type).count()
1137 chandransh 155
    if count == None:
156
        count = 0
157
    return count
442 rajveer 158
 
644 chandransh 159
def get_shipment_info(awb_number, provider_id):
160
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
161
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 162
    info = query.all()
163
    return info
164
 
1730 ankur.sing 165
def get_holidays(start_date, end_date):
166
    query = PublicHolidays.query
167
    if start_date != -1:
168
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
169
    if end_date != -1:
170
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
171
    holidays = query.all()
172
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
173
    return holiday_dates
174
 
766 rajveer 175
def close_session():
176
    if session.is_active:
177
        print "session is active. closing it."
2823 chandransh 178
        session.close()
3376 rajveer 179
 
180
def is_alive():
181
    try:
182
        session.query(Awb.id).limit(1).one()
183
        return True
184
    except:
185
        return False