Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
20718 kshitij.so 1
from suds.client import Client
2
from shop2020.clients.TransactionClient import TransactionClient
3
from datetime import datetime, time, timedelta
4
import traceback
20724 kshitij.so 5
import re
20718 kshitij.so 6
 
7
raclient = None
8
user_profile = None
9
shipper = None
10
 
11
DEBUG=True
12
 
13
acc_url="http://netconnect.bluedart.com/Ver1.7/Demo/ShippingAPI/WayBill/WayBillGeneration.svc?wsdl"
14
LOGIN_ID ="FA316326"
15
LICENCE_KEY="5f1740fdfb8196e7980faea7fe52dd0d"
16
API_VERSION="1.7"
17
API_TYPE="S"
18
AREA="ALL"
19
 
20745 kshitij.so 20
class __BluedartAWBResponse:
21
    def __init__(self, awbNo, destArea, destLocation):
22
        self.awbNo = awbNo
23
        self.destArea = destArea
24
        self.destLocation = destLocation
25
 
20718 kshitij.so 26
def get_client():
27
    global raclient
28
    if raclient is None:
29
        raclient = Client(acc_url, timeout=70)
30
        return raclient
31
    else:
32
        return raclient
33
 
34
def get_profile():
35
    global user_profile
36
    if user_profile is None:
37
        profile = get_client().factory.create('ns0:UserProfile')
38
        profile.Api_type = API_TYPE
39
        profile.Area = AREA 
40
        profile.LicenceKey = LICENCE_KEY
41
        profile.LoginID = LOGIN_ID
42
        profile.Version = API_VERSION
43
        user_profile = profile
44
    return user_profile
45
 
46
def get_shipper_object():
47
    global shipper
48
    if shipper is None:
49
        ship = get_client().factory.create('ns2:Shipper')
50
        ship.CustomerName="Adonis Mobitrade Pvt Ltd"
51
        ship.CustomerAddress1 ="Plot No. 485"
52
        ship.CustomerAddress2 = "Udyog Vihar Phase V"
53
        ship.CustomerAddress3 ="Gurgoan 122016"
54
        ship.CustomerCode = "316326"
55
        ship.CustomerMobile = "9311608716"
56
        ship.VendorCode="AMPFAR"
57
        ship.OriginArea = "FAR"
58
        ship.CustomerPincode = "122016"
59
        ship.Sender = "ADONIS MOBITRADE"
60
        ship.CustomerTelephone="9311608716"
61
        shipper = ship
62
    return shipper
63
 
64
def generate_awb(orders_list):
65
    try:
66
        if not isinstance(orders_list, list) or not orders_list:
20724 kshitij.so 67
            raise ValueError("Expecting list of orders")
20718 kshitij.so 68
        consignee = get_client().factory.create('ns2:Consignee')
69
        consignee.ConsigneeAddress1 = orders_list[0].customer_address1
70
        if orders_list[0].customer_address2:
71
            consignee.ConsigneeAddress2 = orders_list[0].customer_address2 
72
            consignee.ConsigneeAddress3 = orders_list[0].customer_city+" "+orders_list[0].customer_state
73
        else:
74
            consignee.ConsigneeAddress2 = orders_list[0].customer_city
75
        consignee.ConsigneeAttention = orders_list[0].customer_name
76
        consignee.ConsigneeMobile = orders_list[0].customer_mobilenumber
77
        consignee.ConsigneeName = orders_list[0].customer_name
78
        consignee.ConsigneePincode = orders_list[0].customer_pincode
79
        consignee.ConsigneeTelephone = orders_list[0].customer_mobilenumber
80
        ser = get_client().factory.create('ns2:Services')
81
        productType = get_client().factory.create('ns1:ProductType')
82
        actual_weight = 0.0
83
        collectable_amount = 0.0
84
        declared_value = 0.0
85
        isCod = orders_list[0].cod
86
        for order in orders_list:
87
            line_item = order.lineitems[0]
88
            actual_weight += line_item.total_weight
89
            collectable_amount += order.total_amount + order.shippingCost
90
            declared_value += order.total_amount
91
        ser.ActualWeight = actual_weight
92
        ser.CreditReferenceNo = orders_list[0].logisticsTransactionId
93
        ser.DeclaredValue = declared_value 
94
        ser.ProductCode = "A"
95
        ser.ProductType = productType.Dutiables
20726 kshitij.so 96
        ser.PieceCount = 1
20718 kshitij.so 97
        now_time = datetime.now().time()
98
        if now_time <= time(19,00):
99
            ser.PickupDate = datetime.today().strftime('%Y-%m-%d')
100
        else:
101
            ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')
102
        ser.PickupTime="1900"
103
        ser.IsReversePickup = False
104
        ser.PDFOutputNotRequired = True
105
        ser.RegisterPickup = False
106
        if isCod:
107
            ser.CollectableAmount = collectable_amount 
108
            ser.SubProductCode = "C"
109
        wbg = get_client().factory.create('ns2:WayBillGenerationRequest')
110
        wbg.Consignee = consignee
111
        wbg.Services = ser
112
        wbg.Shipper = get_shipper_object()
113
 
114
        d= get_client().factory.create('ns2:Dimension')
115
        d.Count=0
116
        ser.Dimensions=[d]
20724 kshitij.so 117
        response = get_client().service.GenerateWayBill(wbg, get_profile())
118
        if response.AWBNo is None:
20745 kshitij.so 119
            raise RuntimeError("AWB generated is empty")
20724 kshitij.so 120
        else:
20745 kshitij.so 121
            bluedartResponse = __BluedartAWBResponse(str(response.AWBNo),str(response.DestinationArea),str(response.DestinationLocation))
122
            return bluedartResponse
20718 kshitij.so 123
    finally:
124
        if DEBUG:
125
            print get_client().last_sent()
126
            print get_client().last_received()
127
 
128
def main():
20724 kshitij.so 129
    print generate_awb([])
20718 kshitij.so 130
 
131
 
132
 
133
if __name__ == '__main__':
134
    main()