Subversion Repositories SmartDukaan

Rev

Rev 20724 | Rev 20745 | 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
        isCod = orders_list[0].cod
80
        for order in orders_list:
81
            line_item = order.lineitems[0]
82
            actual_weight += line_item.total_weight
83
            collectable_amount += order.total_amount + order.shippingCost
84
            declared_value += order.total_amount
85
        ser.ActualWeight = actual_weight
86
        ser.CreditReferenceNo = orders_list[0].logisticsTransactionId
87
        ser.DeclaredValue = declared_value 
88
        ser.ProductCode = "A"
89
        ser.ProductType = productType.Dutiables
20726 kshitij.so 90
        ser.PieceCount = 1
20718 kshitij.so 91
        now_time = datetime.now().time()
92
        if now_time <= time(19,00):
93
            ser.PickupDate = datetime.today().strftime('%Y-%m-%d')
94
        else:
95
            ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')
96
        ser.PickupTime="1900"
97
        ser.IsReversePickup = False
98
        ser.PDFOutputNotRequired = True
99
        ser.RegisterPickup = False
100
        if isCod:
101
            ser.CollectableAmount = collectable_amount 
102
            ser.SubProductCode = "C"
103
        wbg = get_client().factory.create('ns2:WayBillGenerationRequest')
104
        wbg.Consignee = consignee
105
        wbg.Services = ser
106
        wbg.Shipper = get_shipper_object()
107
 
108
        d= get_client().factory.create('ns2:Dimension')
109
        d.Count=0
110
        ser.Dimensions=[d]
20724 kshitij.so 111
        response = get_client().service.GenerateWayBill(wbg, get_profile())
112
        if response.AWBNo is None:
113
            #Lets check whether awb has been generated before for the same reference
114
            #No error code is given, will have to try dirty method
115
            status_info = (response.Status.WayBillGenerationStatus[0].StatusInformation)
116
            matched = re.findall(r"\D(\d{11})\D", " "+status_info+" ")
117
            if len(matched)==1:
118
                return str(matched[0])
119
        else:
120
            return str(response.AWBNo)
20718 kshitij.so 121
    finally:
122
        if DEBUG:
123
            print get_client().last_sent()
124
            print get_client().last_received()
125
 
126
def main():
20724 kshitij.so 127
    print generate_awb([])
20718 kshitij.so 128
 
129
 
130
 
131
if __name__ == '__main__':
132
    main()