Subversion Repositories SmartDukaan

Rev

Rev 576 | Rev 669 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
412 ashish 1
'''
2
Created on 05-Aug-2010
3
 
4
@author: ashish
5
'''
644 chandransh 6
from shop2020.thriftpy.logistics.ttypes import LogisticsInfo,\
7
    LogisticsServiceException
8
from shop2020.logistics.service.impl.DataAccessor import get_empty_AWB,\
9
    get_shipment_info, initialize, get_logistics_estimation
10
from shop2020.logistics.service.impl.Converters import to_t_awbupdate
494 rajveer 11
from shop2020.clients.InventoryClient import InventoryClient
12
from shop2020.config.client.ConfigClient import ConfigClient
13
import datetime
644 chandransh 14
import math
472 rajveer 15
 
412 ashish 16
class LogisticsServiceHandler:
17
 
442 rajveer 18
    def __init__(self):
19
        initialize()
644 chandransh 20
        config_client = ConfigClient()
21
        self.cutoff_time = int(config_client.get_property('delivery_cutoff_time'))
22
        self.stock_threshold = int(config_client.get_property('inventory_stock_threshold'))
23
        self.time_to_producre = int(config_client.get_property('inventory_time_to_procure'))
483 rajveer 24
 
644 chandransh 25
    def getLogisticsInfo(self, destination_pincode, itemId):
483 rajveer 26
        """
27
        Parameters:
28
         - destination_pincode
29
         - sku_id
30
        """
644 chandransh 31
        logistics_info = self.getLogisticsEstimation(itemId, destination_pincode)
32
        logistics_info.airway_billno = get_empty_AWB(logistics_info.providerId)
483 rajveer 33
        return logistics_info
412 ashish 34
 
35
    def getEmptyAWB(self, provider_id):
36
        """
37
        Parameters:
38
         - provider_id
39
        """
442 rajveer 40
        return get_empty_AWB(provider_id)
41
 
644 chandransh 42
    def getShipmentInfo(self, awb, providerId):
412 ashish 43
        """
44
        Parameters:
45
         - awb
46
        """
644 chandransh 47
        awb_updates = get_shipment_info(awb, providerId)
48
        t_updates = []
49
        for update in awb_updates:
50
            t_updates.append(to_t_awbupdate(update))
51
        return t_updates
412 ashish 52
 
644 chandransh 53
    def getLogisticsEstimation(self, itemId, destination_pin):
472 rajveer 54
        """
55
        Parameters:
56
         - itemId
57
         - destination_pin
58
        """
644 chandransh 59
        delivery_estimate = get_logistics_estimation(destination_pin)
60
        delivery_time = 24*delivery_estimate.delivery_time
61
        if self.cutoff_time <= datetime.datetime.now().hour:
494 rajveer 62
            delivery_time = delivery_time + 24
644 chandransh 63
        warehouse_loc = delivery_estimate.warehouse_location
64
        try:
65
            client = InventoryClient().get_client()
66
            warehouse_id, items_in_inventory = client.getItemAvailabilityAtLocation(warehouse_loc, itemId)
67
        except:
68
            raise LogisticsServiceException(103, "Unable to fetch inventory information about this item.") 
69
        if items_in_inventory < self.stock_threshold :
70
            delivery_time = delivery_time + self.time_to_producre
71
        delivery_time = int(math.ceil(delivery_time/24.0))
472 rajveer 72
 
644 chandransh 73
        logistics_info = LogisticsInfo()
74
        logistics_info.deliveryTime = delivery_time
75
        logistics_info.providerId = delivery_estimate.provider_id
76
        logistics_info.warehouseId = warehouse_id
477 rajveer 77
 
644 chandransh 78
        return logistics_info