Rev 21141 | Blame | Compare with Previous | Last modification | View Log | RSS feed
from suds.client import Clientfrom shop2020.clients.TransactionClient import TransactionClientfrom datetime import datetime, time, timedeltaimport tracebackimport reraclient = Noneuser_profile = NoneshipperMap = {}DEBUG=Trueacc_url="http://netconnect.bluedart.com/Ver1.7/ShippingAPI/WayBill/WayBillGeneration.svc?wsdl"LOGIN_ID ="FA316326"LICENCE_KEY="131f6cfe91591bcc9de7032bb1ed6d13"API_VERSION="1.3"API_TYPE="S"AREA="ALL"xstr = lambda s: s or ""class __BluedartAWBResponse:def __init__(self, awbNo, destArea, destLocation):self.awbNo = awbNoself.destArea = destAreaself.destLocation = destLocationdef get_client():global raclientif raclient is None:raclient = Client(acc_url, timeout=70)return raclientelse:return raclientdef get_profile():global user_profileif user_profile is None:profile = get_client().factory.create('ns0:UserProfile')profile.Api_type = API_TYPEprofile.Area = AREAprofile.LicenceKey = LICENCE_KEYprofile.LoginID = LOGIN_IDprofile.Version = API_VERSIONuser_profile = profilereturn user_profiledef get_shipper_object(warehouse_id):global shipperMapif shipperMap.has_key(warehouse_id):return shipperMap.get(warehouse_id)else:tc = TransactionClient().get_client()info = tc.getBuyerByWarehouse(warehouse_id)if info is None:raise RuntimeError("Buyer address mapping is None")ship = get_client().factory.create('ns2:Shipper')ship.CustomerAddress1 = info.buyerAddress.address[:30]ship.CustomerAddress2 = info.buyerAddress.address[30:60]ship.CustomerAddress3 = info.buyerAddress.address[60:90]ship.CustomerCode = "316326"ship.CustomerMobile = info.buyerAddress.contact_numbership.VendorCode="AMPFAR"ship.OriginArea = "FAR"ship.CustomerPincode = info.buyerAddress.pininfo.organisationName = info.organisationName+" "temp_org = info.organisationName.split(" ")ship.Sender = (xstr(temp_org[0])+" "+xstr(temp_org[1])).strip()ship.CustomerName = ship.Sendership.CustomerTelephone= info.buyerAddress.contact_numbershipperMap[warehouse_id] = shipreturn shipperMap.get(warehouse_id)def create_commodity_obj(orders_list, cd, ser):SpecialInstruction = ""it=0for order in orders_list:it+=1temp = 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)SpecialInstruction+=tempif len(orders_list) == it:passelse:SpecialInstruction+=","SpecialInstruction = re.sub(' +',' ',SpecialInstruction)ser.SpecialInstruction = SpecialInstruction[:50]cd.CommodityDetail1 = SpecialInstruction[:30]cd.CommodityDetail2 = SpecialInstruction[30:60]cd.CommodityDetail3 = SpecialInstruction[60:90]ser.Commodity = cdreturn serdef clean_address(order, consignee):address_string = xstr(order.customer_address1)+" "+xstr(order.customer_address2)address_string = re.sub(",",' ',address_string)add_string = re.sub(' +',' ',address_string)c_1, idx = sub_address(address_string, 0, 30)c_2, idx = sub_address(address_string, idx, idx+30)c_3, idx = sub_address(address_string, idx, len(add_string))consignee.ConsigneeAddress1 = c_1.strip()consignee.ConsigneeAddress2 = c_2.strip()consignee.ConsigneeAddress3 = (c_3 +" "+order.customer_city).strip()return consigneedef sub_address(add_str, previous_loc, final_loc):sub = add_str[previous_loc:final_loc]max_idx = sub.rfind(" ")if max_idx==-1:return sub, len(sub)if max_idx == (final_loc-1):return sub, final_loc-1return sub[: max_idx], max_idx+previous_locdef get_shipment_details(logisticsTxnId):tc = TransactionClient().get_client()shipment_cost_detail = tc.getCostDetailForLogisticsTxnId(logisticsTxnId)return shipment_cost_detaildef generate_awb(orders_list):try:if not isinstance(orders_list, list) or not orders_list:raise ValueError("Expecting list of orders")consignee = get_client().factory.create('ns2:Consignee')consignee = clean_address(orders_list[0], consignee)consignee.ConsigneeAttention = orders_list[0].customer_nameconsignee.ConsigneeMobile = orders_list[0].customer_mobilenumberconsignee.ConsigneeName = orders_list[0].customer_nameconsignee.ConsigneePincode = orders_list[0].customer_pincodeconsignee.ConsigneeTelephone = orders_list[0].customer_mobilenumberser = get_client().factory.create('ns2:Services')productType = get_client().factory.create('ns1:ProductType')actual_weight = 0.0collectable_amount = 0.0declared_value = 0.0isCod = orders_list[0].codfor order in orders_list:line_item = order.lineitems[0]actual_weight += line_item.total_weightcollectable_amount += order.net_payable_amountdeclared_value += order.total_amountser.ActualWeight = actual_weightser.CreditReferenceNo = orders_list[0].logisticsTransactionIdser.DeclaredValue = declared_valueser.ProductCode = "A"ser.ProductType = productType.Dutiablesser.PieceCount = 1now_time = datetime.now().time()if now_time <= time(19,00):ser.PickupDate = datetime.today().strftime('%Y-%m-%d')else:ser.PickupDate = (datetime.now()+timedelta(days=1)).strftime('%Y-%m-%d')ser.PickupTime="1900"ser.IsReversePickup = Falseser.PDFOutputNotRequired = Trueser.RegisterPickup = Falseif isCod and collectable_amount > 0:ser.CollectableAmount = collectable_amountser.SubProductCode = "C"else:ser.SubProductCode = "P"wbg = get_client().factory.create('ns2:WayBillGenerationRequest')d= get_client().factory.create('ns2:Dimension')shipment_cost = get_shipment_details(orders_list[0].logisticsTransactionId)if shipment_cost is None or shipment_cost.packageDimensions is None:raise RuntimeError("Package dim. is none")t_dimensions = shipment_cost.packageDimensions.split("X")if len(t_dimensions)!=3:raise RuntimeError("Package dim. is not valid")d.Count=1d.Length = float(t_dimensions[0].strip())d.Breadth = float(t_dimensions[1].strip())d.Height = float(t_dimensions[2].strip())ser.Dimensions.Dimension=[d]cd = get_client().factory.create('ns2:Commodity')ser = create_commodity_obj(orders_list, cd, ser)wbg.Consignee = consigneewbg.Services = serwbg.Shipper = get_shipper_object(orders_list[0].warehouse_id)response = get_client().service.GenerateWayBill(wbg, get_profile())if response.AWBNo is None:raise RuntimeError("AWB generated is empty")else:bluedartResponse = __BluedartAWBResponse(str(response.AWBNo),str(response.DestinationArea),str(response.DestinationLocation))return bluedartResponsefinally:if DEBUG:print get_client().last_sent()print get_client().last_received()def main():print generate_awb([])if __name__ == '__main__':main()