| 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 |
|
| 20809 |
kshitij.so |
66 |
def create_commodity_obj(orders_list, cd, ser):
|
|
|
67 |
SpecialInstruction = ""
|
|
|
68 |
for order in orders_list:
|
|
|
69 |
temp = xstr(order.lineitems[0].brand)+" "+xstr(order.lineitems[0].model_name)+" "+xstr(order.lineitems[0].model_number)+" "+xstr(order.lineitems[0].color)+"-"+str(order.lineitems[0].quantity)
|
| 20810 |
kshitij.so |
70 |
SpecialInstruction+=temp+","
|
| 20809 |
kshitij.so |
71 |
SpecialInstruction = re.sub(' +',' ',SpecialInstruction)
|
|
|
72 |
ser.SpecialInstruction = SpecialInstruction
|
|
|
73 |
cd.CommodityDetail1 = SpecialInstruction[:30]
|
|
|
74 |
cd.CommodityDetail2 = SpecialInstruction[30:60]
|
|
|
75 |
cd.CommodityDetail3 = SpecialInstruction[60:]
|
|
|
76 |
ser.CommodityDetail = cd
|
|
|
77 |
return ser
|
|
|
78 |
|
| 20768 |
kshitij.so |
79 |
def clean_address(order, consignee):
|
|
|
80 |
address_string = xstr(order.customer_address1)+" "+xstr(order.customer_address2)
|
|
|
81 |
address_string = re.sub(",",' ',address_string)
|
|
|
82 |
add_string = re.sub(' +',' ',address_string)
|
|
|
83 |
c_1, idx = sub_address(address_string, 0, 30)
|
|
|
84 |
c_2, idx = sub_address(address_string, idx, idx+30)
|
|
|
85 |
c_3, idx = sub_address(address_string, idx, len(add_string))
|
| 20772 |
kshitij.so |
86 |
consignee.ConsigneeAddress1 = c_1.strip()
|
|
|
87 |
consignee.ConsigneeAddress2 = c_2.strip()
|
|
|
88 |
consignee.ConsigneeAddress3 = (c_3 +" "+order.customer_city).strip()
|
| 20768 |
kshitij.so |
89 |
return consignee
|
|
|
90 |
|
|
|
91 |
def sub_address(add_str, previous_loc, final_loc):
|
|
|
92 |
sub = add_str[previous_loc:final_loc]
|
|
|
93 |
max_idx = sub.rfind(" ")
|
|
|
94 |
if max_idx==-1:
|
|
|
95 |
return sub, len(sub)
|
|
|
96 |
if max_idx == (final_loc-1):
|
|
|
97 |
return sub
|
|
|
98 |
return sub[: max_idx], max_idx+previous_loc
|
| 20808 |
kshitij.so |
99 |
|
|
|
100 |
def get_shipment_details(logisticsTxnId):
|
|
|
101 |
tc = TransactionClient().get_client()
|
|
|
102 |
shipment_cost_detail = tc.getCostDetailForLogisticsTxnId(logisticsTxnId)
|
|
|
103 |
return shipment_cost_detail
|
| 20768 |
kshitij.so |
104 |
|
|
|
105 |
|
| 20718 |
kshitij.so |
106 |
def generate_awb(orders_list):
|
|
|
107 |
try:
|
|
|
108 |
if not isinstance(orders_list, list) or not orders_list:
|
| 20724 |
kshitij.so |
109 |
raise ValueError("Expecting list of orders")
|
| 20718 |
kshitij.so |
110 |
consignee = get_client().factory.create('ns2:Consignee')
|
| 20768 |
kshitij.so |
111 |
consignee = clean_address(orders_list[0], consignee)
|
| 20718 |
kshitij.so |
112 |
consignee.ConsigneeAttention = orders_list[0].customer_name
|
|
|
113 |
consignee.ConsigneeMobile = orders_list[0].customer_mobilenumber
|
|
|
114 |
consignee.ConsigneeName = orders_list[0].customer_name
|
|
|
115 |
consignee.ConsigneePincode = orders_list[0].customer_pincode
|
|
|
116 |
consignee.ConsigneeTelephone = orders_list[0].customer_mobilenumber
|
|
|
117 |
ser = get_client().factory.create('ns2:Services')
|
|
|
118 |
productType = get_client().factory.create('ns1:ProductType')
|
|
|
119 |
actual_weight = 0.0
|
|
|
120 |
collectable_amount = 0.0
|
|
|
121 |
declared_value = 0.0
|
|
|
122 |
isCod = orders_list[0].cod
|
|
|
123 |
for order in orders_list:
|
|
|
124 |
line_item = order.lineitems[0]
|
|
|
125 |
actual_weight += line_item.total_weight
|
|
|
126 |
collectable_amount += order.total_amount + order.shippingCost
|
|
|
127 |
declared_value += order.total_amount
|
|
|
128 |
ser.ActualWeight = actual_weight
|
|
|
129 |
ser.CreditReferenceNo = orders_list[0].logisticsTransactionId
|
|
|
130 |
ser.DeclaredValue = declared_value
|
|
|
131 |
ser.ProductCode = "A"
|
|
|
132 |
ser.ProductType = productType.Dutiables
|
| 20726 |
kshitij.so |
133 |
ser.PieceCount = 1
|
| 20718 |
kshitij.so |
134 |
now_time = datetime.now().time()
|
|
|
135 |
if now_time <= time(19,00):
|
|
|
136 |
ser.PickupDate = datetime.today().strftime('%Y-%m-%d')
|
|
|
137 |
else:
|
|
|
138 |
ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')
|
|
|
139 |
ser.PickupTime="1900"
|
|
|
140 |
ser.IsReversePickup = False
|
|
|
141 |
ser.PDFOutputNotRequired = True
|
|
|
142 |
ser.RegisterPickup = False
|
|
|
143 |
if isCod:
|
|
|
144 |
ser.CollectableAmount = collectable_amount
|
|
|
145 |
ser.SubProductCode = "C"
|
|
|
146 |
wbg = get_client().factory.create('ns2:WayBillGenerationRequest')
|
|
|
147 |
|
|
|
148 |
d= get_client().factory.create('ns2:Dimension')
|
| 20808 |
kshitij.so |
149 |
shipment_cost = get_shipment_details(orders_list[0].logisticsTransactionId)
|
|
|
150 |
if shipment_cost is None or shipment_cost.packageDimensions is None:
|
|
|
151 |
raise RuntimeError("Package dim. is none")
|
|
|
152 |
t_dimensions = shipment_cost.packageDimensions.split("X")
|
| 20815 |
kshitij.so |
153 |
if len(t_dimensions)!=3:
|
| 20808 |
kshitij.so |
154 |
raise RuntimeError("Package dim. is not valid")
|
|
|
155 |
d.Count=1
|
| 20809 |
kshitij.so |
156 |
d.Length = float(t_dimensions[0].strip())
|
|
|
157 |
d.Breadth = float(t_dimensions[1].strip())
|
|
|
158 |
d.Height = float(t_dimensions[2].strip())
|
| 20718 |
kshitij.so |
159 |
ser.Dimensions=[d]
|
| 20809 |
kshitij.so |
160 |
|
|
|
161 |
cd = get_client().factory.create('ns2:CommodityDetail')
|
|
|
162 |
|
|
|
163 |
ser = create_commodity_obj(orders_list, cd, ser)
|
|
|
164 |
|
|
|
165 |
wbg.Consignee = consignee
|
|
|
166 |
wbg.Services = ser
|
|
|
167 |
wbg.Shipper = get_shipper_object()
|
|
|
168 |
|
| 20724 |
kshitij.so |
169 |
response = get_client().service.GenerateWayBill(wbg, get_profile())
|
|
|
170 |
if response.AWBNo is None:
|
| 20745 |
kshitij.so |
171 |
raise RuntimeError("AWB generated is empty")
|
| 20724 |
kshitij.so |
172 |
else:
|
| 20745 |
kshitij.so |
173 |
bluedartResponse = __BluedartAWBResponse(str(response.AWBNo),str(response.DestinationArea),str(response.DestinationLocation))
|
|
|
174 |
return bluedartResponse
|
| 20718 |
kshitij.so |
175 |
finally:
|
|
|
176 |
if DEBUG:
|
|
|
177 |
print get_client().last_sent()
|
|
|
178 |
print get_client().last_received()
|
|
|
179 |
|
|
|
180 |
def main():
|
| 20724 |
kshitij.so |
181 |
print generate_awb([])
|
| 20718 |
kshitij.so |
182 |
|
|
|
183 |
|
|
|
184 |
|
|
|
185 |
if __name__ == '__main__':
|
|
|
186 |
main()
|