Subversion Repositories SmartDukaan

Rev

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