Subversion Repositories SmartDukaan

Rev

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