Subversion Repositories SmartDukaan

Rev

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