Subversion Repositories SmartDukaan

Rev

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