Subversion Repositories SmartDukaan

Rev

Rev 766 | Rev 1137 | 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, \
9
     DeliveryEstimate, WarehouseAllocation
483 rajveer 10
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException
442 rajveer 11
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
12
from elixir import session
13
import datetime
14
 
15
def initialize():
16
    log_entry("initialize@DataAccessor", "Initializing data service")
17
    DataService.initialize()
18
 
644 chandransh 19
def get_provider(provider_id):
766 rajveer 20
    provider =  Provider.get_by(id=provider_id)
21
    return provider
644 chandransh 22
 
23
def get_providers():
766 rajveer 24
    providers = Provider.query.all()
25
    return providers 
644 chandransh 26
 
27
def get_logistics_estimation(destination_pin):
483 rajveer 28
    try:
644 chandransh 29
        warehouse_location = WarehouseAllocation.get_by(pincode = destination_pin).primary_warehouse_location
483 rajveer 30
    except:
1123 chandransh 31
        raise LogisticsServiceException(101, "No Warehouse assigned to this pincode: " + destination_pin)
766 rajveer 32
 
644 chandransh 33
    query = DeliveryEstimate.query.filter_by(warehouse_location = warehouse_location)
34
    query = query.filter_by(destination_pin = destination_pin)
35
    query = query.order_by(DeliveryEstimate.delivery_time)
36
    try:
37
        return query.first()
483 rajveer 38
    except:
644 chandransh 39
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
766 rajveer 40
 
644 chandransh 41
def add_empty_AWBs(numbers, provider_id, type):
442 rajveer 42
    for number in numbers:
644 chandransh 43
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
444 rajveer 44
        try:
45
            query.one()
46
        except:    
644 chandransh 47
            awb = Awb()
444 rajveer 48
            awb.awb_number = number
644 chandransh 49
            awb.provider_id = provider_id
444 rajveer 50
            awb.is_available = True
644 chandransh 51
            awb.type = type
52
    session.commit()
442 rajveer 53
 
444 rajveer 54
def set_AWB_as_used(awb_number):
644 chandransh 55
    query = Awb.query.filter_by(awb_number = awb_number)
444 rajveer 56
    awb = query.one() 
57
    awb.is_available=False
58
    session.commit()
59
 
442 rajveer 60
def get_empty_AWB(provider_id):
644 chandransh 61
    query = Awb.query.filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
442 rajveer 62
    try:
644 chandransh 63
        awb = query.first()
64
        awb.is_available = False
65
        session.commit()
444 rajveer 66
        return awb.awb_number
442 rajveer 67
    except:
644 chandransh 68
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))
442 rajveer 69
 
644 chandransh 70
def get_shipment_info(awb_number, provider_id):
71
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
72
    query = AwbUpdate.query.filter_by(awb = awb)
766 rajveer 73
    info = query.all()
74
    return info
75
 
76
def close_session():
77
    if session.is_active:
78
        print "session is active. closing it."
1123 chandransh 79
        session.close()