Subversion Repositories SmartDukaan

Rev

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