Subversion Repositories SmartDukaan

Rev

Rev 20768 | Rev 20808 | 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
 
20768 kshitij.so 20
xstr = lambda s: s or ""
21
 
20745 kshitij.so 22
class __BluedartAWBResponse:
23
    def __init__(self, awbNo, destArea, destLocation):
24
        self.awbNo = awbNo
25
        self.destArea = destArea
26
        self.destLocation = destLocation
27
 
20718 kshitij.so 28
def get_client():
29
    global raclient
30
    if raclient is None:
31
        raclient = Client(acc_url, timeout=70)
32
        return raclient
33
    else:
34
        return raclient
35
 
36
def get_profile():
37
    global user_profile
38
    if user_profile is None:
39
        profile = get_client().factory.create('ns0:UserProfile')
40
        profile.Api_type = API_TYPE
41
        profile.Area = AREA 
42
        profile.LicenceKey = LICENCE_KEY
43
        profile.LoginID = LOGIN_ID
44
        profile.Version = API_VERSION
45
        user_profile = profile
46
    return user_profile
47
 
48
def get_shipper_object():
49
    global shipper
50
    if shipper is None:
51
        ship = get_client().factory.create('ns2:Shipper')
52
        ship.CustomerName="Adonis Mobitrade Pvt Ltd"
53
        ship.CustomerAddress1 ="Plot No. 485"
54
        ship.CustomerAddress2 = "Udyog Vihar Phase V"
55
        ship.CustomerAddress3 ="Gurgoan 122016"
56
        ship.CustomerCode = "316326"
57
        ship.CustomerMobile = "9311608716"
58
        ship.VendorCode="AMPFAR"
59
        ship.OriginArea = "FAR"
60
        ship.CustomerPincode = "122016"
61
        ship.Sender = "ADONIS MOBITRADE"
62
        ship.CustomerTelephone="9311608716"
63
        shipper = ship
64
    return shipper
65
 
20768 kshitij.so 66
def clean_address(order, consignee):
67
    address_string = xstr(order.customer_address1)+" "+xstr(order.customer_address2)
68
    address_string = re.sub(",",' ',address_string)
69
    add_string = re.sub(' +',' ',address_string)
70
    c_1, idx = sub_address(address_string, 0, 30)
71
    c_2, idx = sub_address(address_string, idx, idx+30)
72
    c_3, idx = sub_address(address_string, idx, len(add_string))
20772 kshitij.so 73
    consignee.ConsigneeAddress1 = c_1.strip()
74
    consignee.ConsigneeAddress2 = c_2.strip()
75
    consignee.ConsigneeAddress3 = (c_3 +" "+order.customer_city).strip()
20768 kshitij.so 76
    return consignee
77
 
78
def sub_address(add_str, previous_loc, final_loc):
79
    sub = add_str[previous_loc:final_loc]
80
    max_idx = sub.rfind(" ")
81
    if max_idx==-1:
82
        return sub, len(sub)
83
    if max_idx == (final_loc-1):
84
        return sub
85
    return sub[: max_idx], max_idx+previous_loc
86
 
87
 
20718 kshitij.so 88
def generate_awb(orders_list):
89
    try:
90
        if not isinstance(orders_list, list) or not orders_list:
20724 kshitij.so 91
            raise ValueError("Expecting list of orders")
20718 kshitij.so 92
        consignee = get_client().factory.create('ns2:Consignee')
20768 kshitij.so 93
        consignee = clean_address(orders_list[0], consignee)
20718 kshitij.so 94
        consignee.ConsigneeAttention = orders_list[0].customer_name
95
        consignee.ConsigneeMobile = orders_list[0].customer_mobilenumber
96
        consignee.ConsigneeName = orders_list[0].customer_name
97
        consignee.ConsigneePincode = orders_list[0].customer_pincode
98
        consignee.ConsigneeTelephone = orders_list[0].customer_mobilenumber
99
        ser = get_client().factory.create('ns2:Services')
100
        productType = get_client().factory.create('ns1:ProductType')
101
        actual_weight = 0.0
102
        collectable_amount = 0.0
103
        declared_value = 0.0
104
        isCod = orders_list[0].cod
105
        for order in orders_list:
106
            line_item = order.lineitems[0]
107
            actual_weight += line_item.total_weight
108
            collectable_amount += order.total_amount + order.shippingCost
109
            declared_value += order.total_amount
110
        ser.ActualWeight = actual_weight
111
        ser.CreditReferenceNo = orders_list[0].logisticsTransactionId
112
        ser.DeclaredValue = declared_value 
113
        ser.ProductCode = "A"
114
        ser.ProductType = productType.Dutiables
20726 kshitij.so 115
        ser.PieceCount = 1
20718 kshitij.so 116
        now_time = datetime.now().time()
117
        if now_time <= time(19,00):
118
            ser.PickupDate = datetime.today().strftime('%Y-%m-%d')
119
        else:
120
            ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')
121
        ser.PickupTime="1900"
122
        ser.IsReversePickup = False
123
        ser.PDFOutputNotRequired = True
124
        ser.RegisterPickup = False
125
        if isCod:
126
            ser.CollectableAmount = collectable_amount 
127
            ser.SubProductCode = "C"
128
        wbg = get_client().factory.create('ns2:WayBillGenerationRequest')
129
        wbg.Consignee = consignee
130
        wbg.Services = ser
131
        wbg.Shipper = get_shipper_object()
132
 
133
        d= get_client().factory.create('ns2:Dimension')
134
        d.Count=0
135
        ser.Dimensions=[d]
20724 kshitij.so 136
        response = get_client().service.GenerateWayBill(wbg, get_profile())
137
        if response.AWBNo is None:
20745 kshitij.so 138
            raise RuntimeError("AWB generated is empty")
20724 kshitij.so 139
        else:
20745 kshitij.so 140
            bluedartResponse = __BluedartAWBResponse(str(response.AWBNo),str(response.DestinationArea),str(response.DestinationLocation))
141
            return bluedartResponse
20718 kshitij.so 142
    finally:
143
        if DEBUG:
144
            print get_client().last_sent()
145
            print get_client().last_received()
146
 
147
def main():
20724 kshitij.so 148
    print generate_awb([])
20718 kshitij.so 149
 
150
 
151
 
152
if __name__ == '__main__':
153
    main()