Subversion Repositories SmartDukaan

Rev

Rev 2823 | Rev 3064 | 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,\
10
    PublicHolidays
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
 
2341 chandransh 30
def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None):
31
    if warehouse_location is None:
32
        try:
33
            warehouse_location = WarehouseAllocation.get_by(pincode = destination_pin).primary_warehouse_location
34
        except:
35
            print "Unexpected error:", sys.exc_info()[0]
36
            raise LogisticsServiceException(101, "No Warehouse assigned to this pincode: " + destination_pin)
1504 ankur.sing 37
 
38
    query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
39
    query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
40
    try:
41
        if (item_selling_price <= 5000):
42
            provider = query_provider.one().provider_less_amount
43
        else:
44
            provider = query_provider.one().provider_more_amount
45
    except Exception as ex:
46
        print ex
47
        print "Unexpected error:", sys.exc_info()[0]
48
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
49
                                        ", and warehouse location: " + str(warehouse_location))
50
 
644 chandransh 51
    query = DeliveryEstimate.query.filter_by(warehouse_location = warehouse_location)
52
    query = query.filter_by(destination_pin = destination_pin)
1504 ankur.sing 53
    query = query.filter_by(provider = provider)
644 chandransh 54
    try:
55
        return query.first()
1504 ankur.sing 56
    except Exception as ex:
57
        print ex
644 chandransh 58
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
766 rajveer 59
 
644 chandransh 60
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 61
    for number in numbers:
644 chandransh 62
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 63
        try:
64
            query.one()
65
        except:    
644 chandransh 66
            awb = Awb()
444 rajveer 67
            awb.awb_number = number
644 chandransh 68
            awb.provider_id = provider_id
444 rajveer 69
            awb.is_available = True
644 chandransh 70
            awb.type = type
71
    session.commit()
442 rajveer 72
 
444 rajveer 73
def set_AWB_as_used(awb_number):
644 chandransh 74
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 75
    awb = query.one() 
76
    awb.is_available=False
77
    session.commit()
78
 
3044 chandransh 79
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
2342 chandransh 80
    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
3044 chandransh 81
    if type == DeliveryType.PREPAID:
82
        query = query.filter_by(type="Prepaid")
83
    if type == DeliveryType.COD:
84
        query = query.filter_by(type="COD")
85
 
442 rajveer 86
    try:
644 chandransh 87
        awb = query.first()
88
        awb.is_available = False
89
        session.commit()
444 rajveer 90
        return awb.awb_number
442 rajveer 91
    except:
644 chandransh 92
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
1137 chandransh 93
 
94
def get_free_awb_count(provider_id):
95
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True).count()
96
    if count == None:
97
        count = 0
98
    return count
442 rajveer 99
 
644 chandransh 100
def get_shipment_info(awb_number, provider_id):
101
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
102
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 103
    info = query.all()
104
    return info
105
 
1730 ankur.sing 106
def get_holidays(start_date, end_date):
107
    query = PublicHolidays.query
108
    if start_date != -1:
109
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
110
    if end_date != -1:
111
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
112
    holidays = query.all()
113
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
114
    return holiday_dates
115
 
766 rajveer 116
def close_session():
117
    if session.is_active:
118
        print "session is active. closing it."
2823 chandransh 119
        session.close()