Subversion Repositories SmartDukaan

Rev

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