Subversion Repositories SmartDukaan

Rev

Rev 1504 | Rev 2341 | 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
483 rajveer 11
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException
442 rajveer 12
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
13
from elixir import session
1730 ankur.sing 14
import datetime, time
1504 ankur.sing 15
import sys
442 rajveer 16
 
1248 chandransh 17
def initialize(dbname="logistics"):
442 rajveer 18
    log_entry("initialize@DataAccessor", "Initializing data service")
1248 chandransh 19
    DataService.initialize(dbname)
442 rajveer 20
 
644 chandransh 21
def get_provider(provider_id):
766 rajveer 22
    provider =  Provider.get_by(id=provider_id)
23
    return provider
644 chandransh 24
 
25
def get_providers():
766 rajveer 26
    providers = Provider.query.all()
27
    return providers 
644 chandransh 28
 
1504 ankur.sing 29
def get_logistics_estimation(destination_pin, item_selling_price):
483 rajveer 30
    try:
644 chandransh 31
        warehouse_location = WarehouseAllocation.get_by(pincode = destination_pin).primary_warehouse_location
483 rajveer 32
    except:
1504 ankur.sing 33
        print "Unexpected error:", sys.exc_info()[0]
1123 chandransh 34
        raise LogisticsServiceException(101, "No Warehouse assigned to this pincode: " + destination_pin)
1504 ankur.sing 35
 
36
    query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
37
    query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
38
    try:
39
        if (item_selling_price <= 5000):
40
            provider = query_provider.one().provider_less_amount
41
        else:
42
            provider = query_provider.one().provider_more_amount
43
    except Exception as ex:
44
        print ex
45
        print "Unexpected error:", sys.exc_info()[0]
46
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
47
                                        ", and warehouse location: " + str(warehouse_location))
48
 
644 chandransh 49
    query = DeliveryEstimate.query.filter_by(warehouse_location = warehouse_location)
50
    query = query.filter_by(destination_pin = destination_pin)
1504 ankur.sing 51
    query = query.filter_by(provider = provider)
644 chandransh 52
    try:
53
        return query.first()
1504 ankur.sing 54
    except Exception as ex:
55
        print ex
644 chandransh 56
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
766 rajveer 57
 
644 chandransh 58
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 59
    for number in numbers:
644 chandransh 60
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 61
        try:
62
            query.one()
63
        except:    
644 chandransh 64
            awb = Awb()
444 rajveer 65
            awb.awb_number = number
644 chandransh 66
            awb.provider_id = provider_id
444 rajveer 67
            awb.is_available = True
644 chandransh 68
            awb.type = type
69
    session.commit()
442 rajveer 70
 
444 rajveer 71
def set_AWB_as_used(awb_number):
644 chandransh 72
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 73
    awb = query.one() 
74
    awb.is_available=False
75
    session.commit()
76
 
442 rajveer 77
def get_empty_AWB(provider_id):
644 chandransh 78
    query = Awb.query.filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
442 rajveer 79
    try:
644 chandransh 80
        awb = query.first()
81
        awb.is_available = False
82
        session.commit()
444 rajveer 83
        return awb.awb_number
442 rajveer 84
    except:
644 chandransh 85
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
1137 chandransh 86
 
87
def get_free_awb_count(provider_id):
88
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True).count()
89
    if count == None:
90
        count = 0
91
    return count
442 rajveer 92
 
644 chandransh 93
def get_shipment_info(awb_number, provider_id):
94
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
95
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 96
    info = query.all()
97
    return info
98
 
1730 ankur.sing 99
def get_holidays(start_date, end_date):
100
    query = PublicHolidays.query
101
    if start_date != -1:
102
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
103
    if end_date != -1:
104
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
105
    holidays = query.all()
106
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
107
    return holiday_dates
108
 
766 rajveer 109
def close_session():
110
    if session.is_active:
111
        print "session is active. closing it."
1123 chandransh 112
        session.close()