Subversion Repositories SmartDukaan

Rev

Rev 22636 | Rev 22752 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22749 amit.gupta 1
from docutils.utils.math.math2html import URL
2
from shop2020.clients import TransactionClient
3
from shop2020.clients.LogisticsClient import LogisticsClient
4
from shop2020.config.client.ConfigClient import ConfigClient
22636 amit.gupta 5
from shop2020.thriftpy.logistics.ttypes import DeliveryType
22749 amit.gupta 6
from shop2020.thriftpy.model.v1.order.ttypes import Order
7
import json
22636 amit.gupta 8
import urllib
9
import urllib2
10
 
22749 amit.gupta 11
cc = ConfigClient()
12
live = cc.get_property("live")
22636 amit.gupta 13
 
22749 amit.gupta 14
warehouseMap = {}
22636 amit.gupta 15
 
22749 amit.gupta 16
if live=="true":
17
    username = 'ecomexpress'
18
    password = 'Ke$3c@4oT5m6h#$'
19
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
20
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
21
    consignee = ''
22
else:
23
    username = 'ecomexpress'
24
    password = 'Ke$3c@4oT5m6h#$'
25
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
26
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
27
    consignee = 'TEST'
28
 
29
F_AWB_NUMBER = "AWB_NUMBER" 
30
F_ORDER_NUMBER = "ORDER_NUMBER" 
31
F_PRODUCT = "PRODUCT" 
32
F_CONSIGNEE = "CONSIGNEE" 
33
F_CONSIGNEE_ADDRESS1 = "CONSIGNEE_ADDRESS1" 
34
F_CONSIGNEE_ADDRESS2 = "CONSIGNEE_ADDRESS2" 
35
F_DESTINATION_CITY = "DESTINATION_CITY" 
36
F_PINCODE = "PINCODE" 
37
F_STATE = "STATE" 
38
F_MOBILE = "MOBILE" 
39
F_ITEM_DESCRIPTION = "ITEM_DESCRIPTION" 
40
F_PIECES = "PIECES" 
41
F_COLLECTABLE_VALUE = "COLLECTABLE_VALUE" 
42
F_DECLARED_VALUE = "DECLARED_VALUE" 
43
F_ACTUAL_WEIGHT = "ACTUAL_WEIGHT" 
44
F_VOLUMETRIC_WEIGHT = "VOLUMETRIC_WEIGHT" 
45
F_LENGTH = "LENGTH" 
46
F_BREADTH = "BREADTH" 
47
F_HEIGHT = "HEIGHT" 
48
F_PICKUP_NAME = "PICKUP_NAME" 
49
F_PICKUP_ADDRESS_LINE1 = "PICKUP_ADDRESS_LINE1" 
50
F_PICKUP_ADDRESS_LINE2 = "PICKUP_ADDRESS_LINE2" 
51
F_PICKUP_PINCODE = "PICKUP_PINCODE" 
52
F_PICKUP_PHONE = "PICKUP_PHONE" 
53
F_PICKUP_MOBILE = "PICKUP_MOBILE" 
54
F_RETURN_NAME = "RETURN_NAME" 
55
F_RETURN_ADDRESS_LINE1 = "RETURN_ADDRESS_LINE1" 
56
F_RETURN_ADDRESS_LINE2 = "RETURN_ADDRESS_LINE2" 
57
F_RETURN_PINCODE = "RETURN_PINCODE" 
58
F_RETURN_PHONE = "RETURN_PHONE" 
59
F_RETURN_MOBILE = "RETURN_MOBILE" 
60
F_DG_SHIPMENT = "DG_SHIPMENT" 
61
F_INVOICE_NUMBER = "INVOICE_NUMBER" 
62
F_INVOICE_DATE = "INVOICE_DATE" 
63
F_ITEM_CATEGORY = "ITEM_CATEGORY" 
64
F_PACKING_TYPE = "PACKING_TYPE" 
65
F_PICKUP_TYPE = "PICKUP_TYPE" 
66
F_RETURN_TYPE = "RETURN_TYPE" 
67
F_PICKUP_LOCATION_CODE = "PICKUP_LOCATION_CODE" 
68
F_SELLER_GSTIN = "SELLER_GSTIN" 
69
F_GST_HSN = "GST_HSN" 
70
F_GST_ERN = "GST_ERN" 
71
F_GST_TAX_NAME = "GST_TAX_NAME"
72
F_GST_TAX_BASE = "GST_TAX_BASE"
73
F_GST_TAX_RATE_CGSTN = "GST_TAX_RATE_CGSTN"
74
F_GST_TAX_RATE_SGSTN = "GST_TAX_RATE_SGSTN"
75
F_GST_TAX_RATE_IGSTN = "GST_TAX_RATE_IGSTN"
76
F_GST_TAX_TOTAL = "GST_TAX_TOTAL"
77
 
22636 amit.gupta 78
#returns list of awb of specified type
22749 amit.gupta 79
#will generate 500 awbs at once
22636 amit.gupta 80
def generate_awb(deliveryType):
81
    if deliveryType == DeliveryType.COD:
82
        ecomDeliveryType = "COD"
83
    if deliveryType == DeliveryType.PREPAID:
84
        ecomDeliveryType = "PPD"
85
    values = {'username' : username,
86
              'password' : password,
22749 amit.gupta 87
              'count' : '500',
22636 amit.gupta 88
              'type' : ecomDeliveryType }
89
 
90
    data = urllib.urlencode(values)
91
    req = urllib2.Request(wayBillApi, data)
92
    response = urllib2.urlopen(req)
93
    the_page = response.read()
94
    return json.loads(the_page)['awb']
95
 
22749 amit.gupta 96
#All orders awbwise
97
#{'awb':[o1, o2, o3]}
98
def forward_request(ordersMap): 
99
    values = {'username' : username,
100
              'password' : password,
101
              'json_input' : _forwardMap(ordersMap),
102
              }
103
    data = urllib.urlencode(values)
104
    req = urllib2.Request(wayBillApi, data)
105
    response = urllib2.urlopen(req)
106
    the_page = response.read()
107
    print the_page
108
 
109
def _forwardMap(ordersMap):
110
    shipments = []
111
    for orders in ordersMap.itervalues():
112
        shipments.append(_createShipment(orders))
113
    return shipments    
114
 
115
 
116
def _createShipment(orders):
117
    shipment = {}
118
    total_payable = 0
119
    total_pieces = 0
120
    declared_value = 0
121
    total_weight = 0
122
    order1 = Order()
123
    for order1 in orders:
124
        total_payable += order1.net_payable_amount
125
        total_pieces += order1.lineitems[0].quantity
126
        declared_value += order1.total_amount + order1.shippingCost
127
        total_weight += order1.total_weight
128
    order = Order()
129
    warehouseAddressObj = get_shipper_object(order.warehouse_address_id)
130
    shipment[F_AWB_NUMBER] = order.airwaybill_no
131
    shipment[F_ORDER_NUMBER] = order.logisticsTransactionId 
132
    shipment[F_PRODUCT] = 'COD' if order.cod and total_payable > 0 else 'PPD' 
133
    shipment[F_CONSIGNEE] = order.customer_name 
134
    shipment[F_CONSIGNEE_ADDRESS1] = order.customer_address1 
135
    shipment[F_CONSIGNEE_ADDRESS2] = order.customer_address2
136
    shipment[F_DESTINATION_CITY] = order.customer_city 
137
    shipment[F_PICKUP_PINCODE] = order.customer_pincode 
138
    shipment[F_STATE] = order.customer_state
139
    shipment[F_MOBILE] = order.customer_mobilenumber 
140
    shipment[F_ITEM_DESCRIPTION] = '' 
141
    shipment[F_PIECES] = total_pieces
142
    shipment[F_COLLECTABLE_VALUE] = total_payable if order.cod else 0
143
    shipment[F_DECLARED_VALUE] = declared_value 
144
    shipment[F_ACTUAL_WEIGHT] = total_weight 
145
    #shipment[F_VOLUMETRIC_WEIGHT]
146
    shipment[F_LENGTH] = 10
147
    shipment[F_BREADTH] = 10
148
    shipment[F_HEIGHT] = 10
149
    shipment[F_PICKUP_NAME] = warehouseAddressObj.contact_person
150
    shipment[F_PICKUP_ADDRESS_LINE1] = warehouseAddressObj.address
151
    #shipment[F_PICKUP_ADDRESS_LINE2] = ''
152
    shipment[F_PICKUP_PINCODE] = warehouseAddressObj.pin
153
    shipment[F_PICKUP_MOBILE] = warehouseAddressObj.contact_number
154
    shipment[F_PICKUP_PHONE] = warehouseAddressObj.contact_number
155
 
156
    shipment[F_RETURN_NAME] = warehouseAddressObj.contact_person
157
    shipment[F_RETURN_ADDRESS_LINE1] = warehouseAddressObj.address
158
    #shipment[F_RETURN_ADDRESS_LINE2] = ''
159
    shipment[F_RETURN_PINCODE] = warehouseAddressObj.pin
160
    shipment[F_RETURN_MOBILE] = warehouseAddressObj.contact_number
161
    shipment[F_RETURN_PHONE] = warehouseAddressObj.contact_number
162
    shipment[F_DG_SHIPMENT] = 'false'
163
    shipment["ADDITIONAL_INFORMATION"] = {"MULTI_SELLER_INFORMATION": _getMultiSellerInfo(orders)} 
164
 
165
def _getMultiSellerInfo(orders):
166
 
22636 amit.gupta 167
 
168
def main():
22749 amit.gupta 169
    lc = LogisticsClient().get_client()
170
    print lc.getEmptyAWB(48, '731467')
22636 amit.gupta 171
 
172
 
173
 
174
if __name__ == '__main__':
22749 amit.gupta 175
    ordersMap = {}
176
    forward_request(ordersMap)
177
 
178
 
179
 
180
 
181
 
182
 
183
 
184
def get_shipper_object(address_id):
185
    global shipperMap
186
    if shipperMap.has_key(address_id):
187
        return shipperMap.get(address_id)
188
    else:
189
        tc = TransactionClient().get_client()
190
        info = tc.getBuyerByWarehouse(address_id)
191
        if info is None:
192
            raise RuntimeError("Buyer address mapping is None")
193
        shipperMap[address_id] = info
194
    return shipperMap.get(address_id)