Subversion Repositories SmartDukaan

Rev

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

from suds.client import Client
from shop2020.clients.TransactionClient import TransactionClient
from datetime import datetime, time, timedelta
import traceback

raclient = None
user_profile = None
shipper = None

DEBUG=True

acc_url="http://netconnect.bluedart.com/Ver1.7/Demo/ShippingAPI/WayBill/WayBillGeneration.svc?wsdl"
LOGIN_ID ="FA316326"
LICENCE_KEY="5f1740fdfb8196e7980faea7fe52dd0d"
API_VERSION="1.7"
API_TYPE="S"
AREA="ALL"

def get_client():
    global raclient
    if raclient is None:
        raclient = Client(acc_url, timeout=70)
        return raclient
    else:
        return raclient

def get_profile():
    global user_profile
    if user_profile is None:
        profile = get_client().factory.create('ns0:UserProfile')
        profile.Api_type = API_TYPE
        profile.Area = AREA 
        profile.LicenceKey = LICENCE_KEY
        profile.LoginID = LOGIN_ID
        profile.Version = API_VERSION
        user_profile = profile
    return user_profile

def get_shipper_object():
    global shipper
    if shipper is None:
        ship = get_client().factory.create('ns2:Shipper')
        ship.CustomerName="Adonis Mobitrade Pvt Ltd"
        ship.CustomerAddress1 ="Plot No. 485"
        ship.CustomerAddress2 = "Udyog Vihar Phase V"
        ship.CustomerAddress3 ="Gurgoan 122016"
        ship.CustomerCode = "316326"
        ship.CustomerMobile = "9311608716"
        ship.VendorCode="AMPFAR"
        ship.OriginArea = "FAR"
        ship.CustomerPincode = "122016"
        ship.Sender = "ADONIS MOBITRADE"
        ship.CustomerTelephone="9311608716"
        shipper = ship
    return shipper

def generate_awb(orders_list):
    try:
        if not isinstance(orders_list, list) or not orders_list:
            return ""
        consignee = get_client().factory.create('ns2:Consignee')
        consignee.ConsigneeAddress1 = orders_list[0].customer_address1
        if orders_list[0].customer_address2:
            consignee.ConsigneeAddress2 = orders_list[0].customer_address2 
            consignee.ConsigneeAddress3 = orders_list[0].customer_city+" "+orders_list[0].customer_state
        else:
            consignee.ConsigneeAddress2 = orders_list[0].customer_city
        consignee.ConsigneeAttention = orders_list[0].customer_name
        consignee.ConsigneeMobile = orders_list[0].customer_mobilenumber
        consignee.ConsigneeName = orders_list[0].customer_name
        consignee.ConsigneePincode = orders_list[0].customer_pincode
        consignee.ConsigneeTelephone = orders_list[0].customer_mobilenumber
        ser = get_client().factory.create('ns2:Services')
        productType = get_client().factory.create('ns1:ProductType')
        actual_weight = 0.0
        collectable_amount = 0.0
        declared_value = 0.0
        quantity = 0
        isCod = orders_list[0].cod
        for order in orders_list:
            line_item = order.lineitems[0]
            actual_weight += line_item.total_weight
            collectable_amount += order.total_amount + order.shippingCost
            declared_value += order.total_amount
            quantity += line_item.quantity
        ser.ActualWeight = actual_weight
        ser.CreditReferenceNo = orders_list[0].logisticsTransactionId
        ser.DeclaredValue = declared_value 
        ser.ProductCode = "A"
        ser.ProductType = productType.Dutiables
        ser.PieceCount = int(quantity)
        now_time = datetime.now().time()
        if now_time <= time(19,00):
            ser.PickupDate = datetime.today().strftime('%Y-%m-%d')
        else:
            ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')
        ser.PickupTime="1900"
        ser.IsReversePickup = False
        ser.PDFOutputNotRequired = True
        ser.RegisterPickup = False
        if isCod:
            ser.CollectableAmount = collectable_amount 
            ser.SubProductCode = "C"
        wbg = get_client().factory.create('ns2:WayBillGenerationRequest')
        wbg.Consignee = consignee
        wbg.Services = ser
        wbg.Shipper = get_shipper_object()
        
        d= get_client().factory.create('ns2:Dimension')
        d.Count=0
        ser.Dimensions=[d]
        print get_client().service.GenerateWayBill(wbg, get_profile())
    except:
        print traceback.print_exc()
    finally:
        if DEBUG:
            print get_client().last_sent()
            print get_client().last_received()

def main():
    tc = TransactionClient().get_client()
    orders = tc.getOrderList([1546485])
    generate_awb(orders)
    
    

if __name__ == '__main__':
    main()