Subversion Repositories SmartDukaan

Rev

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

from shop2020.clients import TransactionClient
from shop2020.clients.LogisticsClient import LogisticsClient
from shop2020.config.client.ConfigClient import ConfigClient
from shop2020.thriftpy.logistics.ttypes import DeliveryType
from shop2020.thriftpy.model.v1.order.ttypes import Order
import json
import urllib
import urllib2

cc = ConfigClient()
live = cc.get_property("live")

warehouseMap = {}

if live=="true":
    username = 'ecomexpress'
    password = 'Ke$3c@4oT5m6h#$'
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
    consignee = ''
else:
    username = 'ecomexpress'
    password = 'Ke$3c@4oT5m6h#$'
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
    consignee = 'TEST'
    
F_AWB_NUMBER = "AWB_NUMBER" 
F_ORDER_NUMBER = "ORDER_NUMBER" 
F_PRODUCT = "PRODUCT" 
F_CONSIGNEE = "CONSIGNEE" 
F_CONSIGNEE_ADDRESS1 = "CONSIGNEE_ADDRESS1" 
F_CONSIGNEE_ADDRESS2 = "CONSIGNEE_ADDRESS2" 
F_DESTINATION_CITY = "DESTINATION_CITY" 
F_PINCODE = "PINCODE" 
F_STATE = "STATE" 
F_MOBILE = "MOBILE" 
F_ITEM_DESCRIPTION = "ITEM_DESCRIPTION" 
F_PIECES = "PIECES" 
F_COLLECTABLE_VALUE = "COLLECTABLE_VALUE" 
F_DECLARED_VALUE = "DECLARED_VALUE" 
F_ACTUAL_WEIGHT = "ACTUAL_WEIGHT" 
F_VOLUMETRIC_WEIGHT = "VOLUMETRIC_WEIGHT" 
F_LENGTH = "LENGTH" 
F_BREADTH = "BREADTH" 
F_HEIGHT = "HEIGHT" 
F_PICKUP_NAME = "PICKUP_NAME" 
F_PICKUP_ADDRESS_LINE1 = "PICKUP_ADDRESS_LINE1" 
F_PICKUP_ADDRESS_LINE2 = "PICKUP_ADDRESS_LINE2" 
F_PICKUP_PINCODE = "PICKUP_PINCODE" 
F_PICKUP_PHONE = "PICKUP_PHONE" 
F_PICKUP_MOBILE = "PICKUP_MOBILE" 
F_RETURN_NAME = "RETURN_NAME" 
F_RETURN_ADDRESS_LINE1 = "RETURN_ADDRESS_LINE1" 
F_RETURN_ADDRESS_LINE2 = "RETURN_ADDRESS_LINE2" 
F_RETURN_PINCODE = "RETURN_PINCODE" 
F_RETURN_PHONE = "RETURN_PHONE" 
F_RETURN_MOBILE = "RETURN_MOBILE" 
F_DG_SHIPMENT = "DG_SHIPMENT" 
F_INVOICE_NUMBER = "INVOICE_NUMBER" 
F_INVOICE_DATE = "INVOICE_DATE" 
F_ITEM_CATEGORY = "ITEM_CATEGORY" 
F_PACKING_TYPE = "PACKING_TYPE" 
F_PICKUP_TYPE = "PICKUP_TYPE" 
F_RETURN_TYPE = "RETURN_TYPE" 
F_PICKUP_LOCATION_CODE = "PICKUP_LOCATION_CODE" 
F_SELLER_GSTIN = "SELLER_GSTIN" 
F_GST_HSN = "GST_HSN" 
F_GST_ERN = "GST_ERN" 
F_GST_TAX_NAME = "GST_TAX_NAME"
F_GST_TAX_BASE = "GST_TAX_BASE"
F_GST_TAX_RATE_CGSTN = "GST_TAX_RATE_CGSTN"
F_GST_TAX_RATE_SGSTN = "GST_TAX_RATE_SGSTN"
F_GST_TAX_RATE_IGSTN = "GST_TAX_RATE_IGSTN"
F_GST_TAX_TOTAL = "GST_TAX_TOTAL"

#returns list of awb of specified type
#will generate 500 awbs at once
def generate_awb(deliveryType):
    if deliveryType == DeliveryType.COD:
        ecomDeliveryType = "COD"
    if deliveryType == DeliveryType.PREPAID:
        ecomDeliveryType = "PPD"
    values = {'username' : username,
              'password' : password,
              'count' : '500',
              'type' : ecomDeliveryType }

    data = urllib.urlencode(values)
    req = urllib2.Request(wayBillApi, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    return json.loads(the_page)['awb']

#All orders awbwise
#{'awb':[o1, o2, o3]}
def forward_request(ordersMap): 
    values = {'username' : username,
              'password' : password,
              'json_input' : _forwardMap(ordersMap),
              }
    data = urllib.urlencode(values)
    req = urllib2.Request(wayBillApi, data)
    response = urllib2.urlopen(req)
    the_page = response.read()
    print the_page
    
def _forwardMap(ordersMap):
    shipments = []
    for orders in ordersMap.itervalues():
        shipments.append(_createShipment(orders))
    return shipments    
    
        
def _createShipment(orders):
    shipment = {}
    total_payable = 0
    total_pieces = 0
    declared_value = 0
    total_weight = 0
    order1 = Order()
    for order1 in orders:
        total_payable += order1.net_payable_amount
        total_pieces += order1.lineitems[0].quantity
        declared_value += order1.total_amount + order1.shippingCost
        total_weight += order1.total_weight
    order = Order()
    warehouseAddressObj = get_shipper_object(order.warehouse_address_id)
    shipment[F_AWB_NUMBER] = order.airwaybill_no
    shipment[F_ORDER_NUMBER] = order.logisticsTransactionId 
    shipment[F_PRODUCT] = 'COD' if order.cod and total_payable > 0 else 'PPD' 
    shipment[F_CONSIGNEE] = order.customer_name 
    shipment[F_CONSIGNEE_ADDRESS1] = order.customer_address1 
    shipment[F_CONSIGNEE_ADDRESS2] = order.customer_address2
    shipment[F_DESTINATION_CITY] = order.customer_city 
    shipment[F_PICKUP_PINCODE] = order.customer_pincode 
    shipment[F_STATE] = order.customer_state
    shipment[F_MOBILE] = order.customer_mobilenumber 
    shipment[F_ITEM_DESCRIPTION] = '' 
    shipment[F_PIECES] = total_pieces
    shipment[F_COLLECTABLE_VALUE] = total_payable if order.cod else 0
    shipment[F_DECLARED_VALUE] = declared_value 
    shipment[F_ACTUAL_WEIGHT] = total_weight 
    #shipment[F_VOLUMETRIC_WEIGHT]
    shipment[F_LENGTH] = 10
    shipment[F_BREADTH] = 10
    shipment[F_HEIGHT] = 10
    shipment[F_PICKUP_NAME] = warehouseAddressObj.contact_person
    shipment[F_PICKUP_ADDRESS_LINE1] = warehouseAddressObj.address
    #shipment[F_PICKUP_ADDRESS_LINE2] = ''
    shipment[F_PICKUP_PINCODE] = warehouseAddressObj.pin
    shipment[F_PICKUP_MOBILE] = warehouseAddressObj.contact_number
    shipment[F_PICKUP_PHONE] = warehouseAddressObj.contact_number
    
    shipment[F_RETURN_NAME] = warehouseAddressObj.contact_person
    shipment[F_RETURN_ADDRESS_LINE1] = warehouseAddressObj.address
    #shipment[F_RETURN_ADDRESS_LINE2] = ''
    shipment[F_RETURN_PINCODE] = warehouseAddressObj.pin
    shipment[F_RETURN_MOBILE] = warehouseAddressObj.contact_number
    shipment[F_RETURN_PHONE] = warehouseAddressObj.contact_number
    shipment[F_DG_SHIPMENT] = 'false'
    shipment["ADDITIONAL_INFORMATION"] = {"MULTI_SELLER_INFORMATION": _getMultiSellerInfo(orders)} 
    
def _getMultiSellerInfo(orders):
    pass

def main():
    lc = LogisticsClient().get_client()
    print lc.getEmptyAWB(48, '731467')
    
    

if __name__ == '__main__':
    ordersMap = {}
    forward_request(ordersMap)
    
    
    
    
    
    
    
def get_shipper_object(address_id):
    global shipperMap
    if shipperMap.has_key(address_id):
        return shipperMap.get(address_id)
    else:
        tc = TransactionClient().get_client()
        info = tc.getBuyerByWarehouse(address_id)
        if info is None:
            raise RuntimeError("Buyer address mapping is None")
        shipperMap[address_id] = info
    return shipperMap.get(address_id)