Subversion Repositories SmartDukaan

Rev

Rev 20718 | Rev 20726 | 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
 
20
def get_client():
21
    global raclient
22
    if raclient is None:
23
        raclient = Client(acc_url, timeout=70)
24
        return raclient
25
    else:
26
        return raclient
27
 
28
def get_profile():
29
    global user_profile
30
    if user_profile is None:
31
        profile = get_client().factory.create('ns0:UserProfile')
32
        profile.Api_type = API_TYPE
33
        profile.Area = AREA 
34
        profile.LicenceKey = LICENCE_KEY
35
        profile.LoginID = LOGIN_ID
36
        profile.Version = API_VERSION
37
        user_profile = profile
38
    return user_profile
39
 
40
def get_shipper_object():
41
    global shipper
42
    if shipper is None:
43
        ship = get_client().factory.create('ns2:Shipper')
44
        ship.CustomerName="Adonis Mobitrade Pvt Ltd"
45
        ship.CustomerAddress1 ="Plot No. 485"
46
        ship.CustomerAddress2 = "Udyog Vihar Phase V"
47
        ship.CustomerAddress3 ="Gurgoan 122016"
48
        ship.CustomerCode = "316326"
49
        ship.CustomerMobile = "9311608716"
50
        ship.VendorCode="AMPFAR"
51
        ship.OriginArea = "FAR"
52
        ship.CustomerPincode = "122016"
53
        ship.Sender = "ADONIS MOBITRADE"
54
        ship.CustomerTelephone="9311608716"
55
        shipper = ship
56
    return shipper
57
 
58
def generate_awb(orders_list):
59
    try:
60
        if not isinstance(orders_list, list) or not orders_list:
20724 kshitij.so 61
            raise ValueError("Expecting list of orders")
20718 kshitij.so 62
        consignee = get_client().factory.create('ns2:Consignee')
63
        consignee.ConsigneeAddress1 = orders_list[0].customer_address1
64
        if orders_list[0].customer_address2:
65
            consignee.ConsigneeAddress2 = orders_list[0].customer_address2 
66
            consignee.ConsigneeAddress3 = orders_list[0].customer_city+" "+orders_list[0].customer_state
67
        else:
68
            consignee.ConsigneeAddress2 = orders_list[0].customer_city
69
        consignee.ConsigneeAttention = orders_list[0].customer_name
70
        consignee.ConsigneeMobile = orders_list[0].customer_mobilenumber
71
        consignee.ConsigneeName = orders_list[0].customer_name
72
        consignee.ConsigneePincode = orders_list[0].customer_pincode
73
        consignee.ConsigneeTelephone = orders_list[0].customer_mobilenumber
74
        ser = get_client().factory.create('ns2:Services')
75
        productType = get_client().factory.create('ns1:ProductType')
76
        actual_weight = 0.0
77
        collectable_amount = 0.0
78
        declared_value = 0.0
79
        quantity = 0
80
        isCod = orders_list[0].cod
81
        for order in orders_list:
82
            line_item = order.lineitems[0]
83
            actual_weight += line_item.total_weight
84
            collectable_amount += order.total_amount + order.shippingCost
85
            declared_value += order.total_amount
86
            quantity += line_item.quantity
87
        ser.ActualWeight = actual_weight
88
        ser.CreditReferenceNo = orders_list[0].logisticsTransactionId
89
        ser.DeclaredValue = declared_value 
90
        ser.ProductCode = "A"
91
        ser.ProductType = productType.Dutiables
92
        ser.PieceCount = int(quantity)
93
        now_time = datetime.now().time()
94
        if now_time <= time(19,00):
95
            ser.PickupDate = datetime.today().strftime('%Y-%m-%d')
96
        else:
97
            ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')
98
        ser.PickupTime="1900"
99
        ser.IsReversePickup = False
100
        ser.PDFOutputNotRequired = True
101
        ser.RegisterPickup = False
102
        if isCod:
103
            ser.CollectableAmount = collectable_amount 
104
            ser.SubProductCode = "C"
105
        wbg = get_client().factory.create('ns2:WayBillGenerationRequest')
106
        wbg.Consignee = consignee
107
        wbg.Services = ser
108
        wbg.Shipper = get_shipper_object()
109
 
110
        d= get_client().factory.create('ns2:Dimension')
111
        d.Count=0
112
        ser.Dimensions=[d]
20724 kshitij.so 113
        response = get_client().service.GenerateWayBill(wbg, get_profile())
114
        if response.AWBNo is None:
115
            #Lets check whether awb has been generated before for the same reference
116
            #No error code is given, will have to try dirty method
117
            status_info = (response.Status.WayBillGenerationStatus[0].StatusInformation)
118
            matched = re.findall(r"\D(\d{11})\D", " "+status_info+" ")
119
            if len(matched)==1:
120
                return str(matched[0])
121
        else:
122
            return str(response.AWBNo)
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()