Subversion Repositories SmartDukaan

Rev

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