Subversion Repositories SmartDukaan

Rev

Rev 3044 | Rev 3098 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 13-Sep-2010

@author: rajveer
'''

from shop2020.logistics.service.impl import DataService
from shop2020.logistics.service.impl.DataService import Awb, AwbUpdate, Provider, \
     DeliveryEstimate, WarehouseAllocation, DestinationProviderAllocation,\
    PublicHolidays, ServiceableLocationDetails
from shop2020.thriftpy.logistics.ttypes import LogisticsServiceException,\
    DeliveryType
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
from elixir import session
import datetime, time
import sys

def initialize(dbname="logistics"):
    log_entry("initialize@DataAccessor", "Initializing data service")
    DataService.initialize(dbname)

def get_provider(provider_id):
    provider =  Provider.get_by(id=provider_id)
    return provider

def get_providers():
    providers = Provider.query.all()
    return providers 

def is_cod_allowed(destination_pin):
    try:
        sld = ServiceableLocationDetails.get_by(dest_pincode = destination_pin, cod = True)
    except Exception as ex:
        print "Unexpected error:", sys.exc_info()[0]
    
    if sld:
        return True
    else:
        return False
    

def get_logistics_estimation(destination_pin, item_selling_price, warehouse_location=None, type=DeliveryType.PREPAID):
    if warehouse_location is None:
        try:
            warehouse_location = WarehouseAllocation.get_by(pincode = destination_pin).primary_warehouse_location
        except:
            print "Unexpected error:", sys.exc_info()[0]
            raise LogisticsServiceException(101, "No Warehouse locations assigned to this pincode: " + destination_pin)
    
    try:
        sld = ServiceableLocationDetails.get_by(dest_pincode = destination_pin)
        if type == DeliveryType.PREPAID and sld.exp:
            provider = sld.provider
        if type == DeliveryType.COD and sld.cod:
            provider = sld.provider
    except Exception as ex:
        print "Unexpected error:", sys.exc_info()[0]
    
    if not provider:
        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
                                        ", and warehouse location: " + str(warehouse_location))
        
#    query_provider = DestinationProviderAllocation.query.filter_by(destination_pin = destination_pin)
#    query_provider = query_provider.filter_by(warehouse_location = warehouse_location)
#    try:
#        if (item_selling_price <= 5000):
#            provider = query_provider.one().provider_less_amount
#        else:
#            provider = query_provider.one().provider_more_amount
#    except Exception as ex:
#        print ex
#        print "Unexpected error:", sys.exc_info()[0]
#        raise LogisticsServiceException(101, "No provider assigned for pincode: " + str(destination_pin) + \
#                                        ", and warehouse location: " + str(warehouse_location))
    
    query = DeliveryEstimate.query.filter_by(warehouse_location = warehouse_location)
    query = query.filter_by(destination_pin = destination_pin)
    query = query.filter_by(provider = provider)
    try:
        return query.first()
    except Exception as ex:
        print ex
        raise LogisticsServiceException(103, "No Logistics partner listed for this destination pincode and the primary warehouse")
        
def add_empty_AWBs(numbers, provider_id, type):
    for number in numbers:
        query = Awb.query.filter_by(awb_number = number, provider_id = provider_id)
        try:
            query.one()
        except:    
            awb = Awb()
            awb.awb_number = number
            awb.provider_id = provider_id
            awb.is_available = True
            awb.type = type
    session.commit()

def set_AWB_as_used(awb_number):
    query = Awb.query.filter_by(awb_number = awb_number)
    awb = query.one() 
    awb.is_available=False
    session.commit()
    
def get_empty_AWB(provider_id, type = DeliveryType.PREPAID):
    query = Awb.query.with_lockmode("update").filter_by(provider_id = provider_id, is_available = True)  #check the provider thing
    if type == DeliveryType.PREPAID:
        query = query.filter_by(type="Prepaid")
    if type == DeliveryType.COD:
        query = query.filter_by(type="COD")
    
    try:
        awb = query.first()
        awb.is_available = False
        session.commit()
        return awb.awb_number
    except:
        raise LogisticsServiceException(103, "Unable to get an AWB for the given Provider: " + str(provider_id))

def get_free_awb_count(provider_id):
    count = Awb.query.filter_by(provider_id = provider_id, is_available = True).count()
    if count == None:
        count = 0
    return count
   
def get_shipment_info(awb_number, provider_id):
    awb = Awb.get_by(awb_number = awb_number, provider_id = provider_id)
    query = AwbUpdate.query.filter_by(awb = awb)
    info = query.all()
    return info

def get_holidays(start_date, end_date):
    query = PublicHolidays.query
    if start_date != -1:
        query = query.filter(PublicHolidays.date >= to_py_date(start_date))
    if end_date != -1:
        query = query.filter(PublicHolidays.date <= to_py_date(end_date))
    holidays = query.all()
    holiday_dates = [to_java_date(datetime.datetime(*time.strptime(str(holiday.date), '%Y-%m-%d')[:3])) for holiday in holidays] 
    return holiday_dates

def close_session():
    if session.is_active:
        print "session is active. closing it."
        session.close()