Subversion Repositories SmartDukaan

Rev

Rev 22919 | Rev 23139 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22919 Rev 23132
Line -... Line 1...
-
 
1
from datetime import *
-
 
2
from shop2020 import utils
1
from shop2020.clients import TransactionClient
3
from shop2020.clients import InventoryClient
2
from shop2020.clients.LogisticsClient import LogisticsClient
4
from shop2020.clients.LogisticsClient import LogisticsClient
-
 
5
from shop2020.clients.TransactionClient import TransactionClient
3
from shop2020.config.client.ConfigClient import ConfigClient
6
from shop2020.config.client.ConfigClient import ConfigClient
4
from shop2020.thriftpy.logistics.ttypes import DeliveryType
7
from shop2020.thriftpy.logistics.ttypes import DeliveryType
-
 
8
from shop2020.thriftpy.model.v1.order.TransactionService import Client
5
from shop2020.thriftpy.model.v1.order.ttypes import Order
9
from shop2020.thriftpy.model.v1.order.ttypes import Order, LineItem, SellerInfo
-
 
10
from shop2020.utils.Utils import to_py_date
-
 
11
from shop2020.utils.caching.SimpleCaching import memoized
6
import json
12
import json
-
 
13
import logging
7
import urllib
14
import urllib
8
import urllib2
15
import urllib2
9
 
16
 
10
cc = ConfigClient()
-
 
11
live = cc.get_property("live")
17
logging.basicConfig(level=logging.DEBUG)
12
 
18
 
-
 
19
cc = ConfigClient()
-
 
20
#live = cc.get_property("live")
-
 
21
live = "false"
13
warehouseMap = {}
22
warehouseMap = {}
-
 
23
shipperMap = {}
14
 
24
 
15
if live=="true":
25
if live=="true":
16
    username = 'ecomexpress'
26
    username = 'ecomexpress'
17
    password = 'Ke$3c@4oT5m6h#$'
27
    password = 'Ke$3c@4oT5m6h#$'
18
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
28
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
19
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
29
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
20
    consignee = ''
30
    serviceablePincodeApi = 'http://staging.ecomexpress.in/apiv2/pincodes/'
21
else:
31
else:
22
    username = 'ecomexpress'
32
    username = 'ecomexpress'
23
    password = 'Ke$3c@4oT5m6h#$'
33
    password = 'Ke$3c@4oT5m6h#$'
24
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
34
    wayBillApi = 'http://staging.ecomexpress.in/apiv2/fetch_awb/'
25
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
35
    forwardApi = 'http://staging.ecomexpress.in/apiv2/manifest_awb/'
26
    consignee = 'TEST'
36
    serviceablePincodeApi = 'http://staging.ecomexpress.in/apiv2/pincodes/'
27
    
37
    
28
F_AWB_NUMBER = "AWB_NUMBER" 
38
F_AWB_NUMBER = "AWB_NUMBER" 
29
F_ORDER_NUMBER = "ORDER_NUMBER" 
39
F_ORDER_NUMBER = "ORDER_NUMBER" 
30
F_PRODUCT = "PRODUCT" 
40
F_PRODUCT = "PRODUCT" 
31
F_CONSIGNEE = "CONSIGNEE" 
41
F_CONSIGNEE = "CONSIGNEE" 
32
F_CONSIGNEE_ADDRESS1 = "CONSIGNEE_ADDRESS1" 
42
F_CONSIGNEE_ADDRESS1 = "CONSIGNEE_ADDRESS1" 
33
F_CONSIGNEE_ADDRESS2 = "CONSIGNEE_ADDRESS2" 
43
F_CONSIGNEE_ADDRESS2 = "CONSIGNEE_ADDRESS2" 
-
 
44
F_CONSIGNEE_ADDRESS3 = "CONSIGNEE_ADDRESS3" 
34
F_DESTINATION_CITY = "DESTINATION_CITY" 
45
F_DESTINATION_CITY = "DESTINATION_CITY" 
35
F_PINCODE = "PINCODE" 
46
F_PINCODE = "PINCODE" 
36
F_STATE = "STATE" 
47
F_STATE = "STATE" 
37
F_MOBILE = "MOBILE" 
48
F_MOBILE = "MOBILE" 
38
F_ITEM_DESCRIPTION = "ITEM_DESCRIPTION" 
49
F_ITEM_DESCRIPTION = "ITEM_DESCRIPTION" 
Line 55... Line 66...
55
F_RETURN_ADDRESS_LINE2 = "RETURN_ADDRESS_LINE2" 
66
F_RETURN_ADDRESS_LINE2 = "RETURN_ADDRESS_LINE2" 
56
F_RETURN_PINCODE = "RETURN_PINCODE" 
67
F_RETURN_PINCODE = "RETURN_PINCODE" 
57
F_RETURN_PHONE = "RETURN_PHONE" 
68
F_RETURN_PHONE = "RETURN_PHONE" 
58
F_RETURN_MOBILE = "RETURN_MOBILE" 
69
F_RETURN_MOBILE = "RETURN_MOBILE" 
59
F_DG_SHIPMENT = "DG_SHIPMENT" 
70
F_DG_SHIPMENT = "DG_SHIPMENT" 
-
 
71
F_ADDITIONAL_INFORMATION = "ADDITIONAL_INFORMATION" 
60
F_INVOICE_NUMBER = "INVOICE_NUMBER" 
72
F_INVOICE_NUMBER = "INVOICE_NUMBER" 
61
F_INVOICE_DATE = "INVOICE_DATE" 
73
F_INVOICE_DATE = "INVOICE_DATE" 
62
F_ITEM_CATEGORY = "ITEM_CATEGORY" 
74
F_ITEM_CATEGORY = "ITEM_CATEGORY" 
63
F_PACKING_TYPE = "PACKING_TYPE" 
75
F_PACKING_TYPE = "PACKING_TYPE" 
64
F_PICKUP_TYPE = "PICKUP_TYPE" 
76
F_PICKUP_TYPE = "PICKUP_TYPE" 
Line 71... Line 83...
71
F_GST_TAX_BASE = "GST_TAX_BASE"
83
F_GST_TAX_BASE = "GST_TAX_BASE"
72
F_GST_TAX_RATE_CGSTN = "GST_TAX_RATE_CGSTN"
84
F_GST_TAX_RATE_CGSTN = "GST_TAX_RATE_CGSTN"
73
F_GST_TAX_RATE_SGSTN = "GST_TAX_RATE_SGSTN"
85
F_GST_TAX_RATE_SGSTN = "GST_TAX_RATE_SGSTN"
74
F_GST_TAX_RATE_IGSTN = "GST_TAX_RATE_IGSTN"
86
F_GST_TAX_RATE_IGSTN = "GST_TAX_RATE_IGSTN"
75
F_GST_TAX_TOTAL = "GST_TAX_TOTAL"
87
F_GST_TAX_TOTAL = "GST_TAX_TOTAL"
-
 
88
F_TELEPHONE = "TELEPHONE"
76
 
89
 
77
#returns list of awb of specified type
90
#returns list of awb of specified type
78
#will generate 500 awbs at once
91
#will generate 500 awbs at once
-
 
92
 
-
 
93
@memoized(3600)
-
 
94
def fetchStateMaster():
-
 
95
    inventory_client = InventoryClient().get_client()
-
 
96
    return inventory_client.getStateMaster()
-
 
97
 
-
 
98
 
79
def generate_awb(deliveryType):
99
def generate_awb(deliveryType):
80
    if deliveryType == DeliveryType.COD:
100
    if deliveryType == DeliveryType.COD:
81
        ecomDeliveryType = "COD"
101
        ecomDeliveryType = "COD"
82
    if deliveryType == DeliveryType.PREPAID:
102
    if deliveryType == DeliveryType.PREPAID:
83
        ecomDeliveryType = "PPD"
103
        ecomDeliveryType = "PPD"
Line 88... Line 108...
88
 
108
 
89
    data = urllib.urlencode(values)
109
    data = urllib.urlencode(values)
90
    req = urllib2.Request(wayBillApi, data)
110
    req = urllib2.Request(wayBillApi, data)
91
    response = urllib2.urlopen(req)
111
    response = urllib2.urlopen(req)
92
    the_page = response.read()
112
    the_page = response.read()
93
    return json.loads(the_page)['awb']
113
    awbs  = [str(awb) for awb in json.loads(the_page)['awb']]
-
 
114
    return awbs
94
 
115
 
95
#All orders awbwise
116
#All orders awbwise
96
#{'awb':[o1, o2, o3]}
117
#{'awb':[o1, o2, o3]}
97
def forward_request(ordersMap): 
118
def forward_request(airwaybill_no, logisticsTxnId):
-
 
119
    client = TransactionClient().get_client()
-
 
120
    orders = client.getGroupOrdersByLogisticsTxnId(logisticsTxnId)
98
    values = {'username' : username,
121
    values = {'username' : username,
99
              'password' : password,
122
              'password' : password,
100
              'json_input' : _forwardMap(ordersMap),
123
              'json_input' : _forwardMap(airwaybill_no, orders),
101
              }
124
              }
-
 
125
    print "values - ", values 
102
    data = urllib.urlencode(values)
126
    data = urllib.urlencode(values)
-
 
127
    print "data - ", data 
103
    req = urllib2.Request(wayBillApi, data)
128
    req = urllib2.Request(forwardApi, data)
104
    response = urllib2.urlopen(req)
129
    response = urllib2.urlopen(req)
105
    the_page = response.read()
130
    the_page = response.read()
106
    print the_page
131
    jsonResponse = json.loads(the_page)
-
 
132
    return jsonResponse['success']
107
    
133
    
108
def _forwardMap(ordersMap):
134
def _forwardMap(airwaybill_no, orders):
109
    shipments = []
135
    shipments = []
110
    for orders in ordersMap.itervalues():
-
 
111
        shipments.append(_createShipment(orders))
136
    shipments.append(_createShipment(airwaybill_no, orders))
112
    return shipments    
137
    return json.dumps(shipments)
113
    
138
    
114
        
139
        
115
def _createShipment(orders):
140
def _createShipment(airwaybill_no, orders):
116
    shipment = {}
141
    shipment = {}
117
    total_payable = 0
142
    total_payable = 0
118
    total_pieces = 0
143
    total_pieces = 0
119
    declared_value = 0
144
    declared_value = 0
120
    total_weight = 0
145
    total_weight = 0
121
    order1 = Order()
146
    order = orders[0]
-
 
147
    sellerInfo = getBuyerWarehouse(order.warehouse_id)
-
 
148
    itemDescription = []
122
    for order1 in orders:
149
    for order1 in orders:
123
        total_payable += order1.net_payable_amount
150
        total_payable += order1.net_payable_amount
124
        total_pieces += order1.lineitems[0].quantity
151
        total_pieces += order1.lineitems[0].quantity
125
        declared_value += order1.total_amount + order1.shippingCost
152
        declared_value += order1.total_amount
126
        total_weight += order1.total_weight
153
        total_weight += order1.total_weight
127
    order = Order()
154
        itemDescription.append(" ".join(filter(None, [order.lineitems[0].brand, order.lineitems[0].model_name,  order.lineitems[0].model_number])))
128
    warehouseAddressObj = get_shipper_object(order.warehouse_address_id)
155
    warehouseAddressObj = sellerInfo.buyerAddress
129
    shipment[F_AWB_NUMBER] = order.airwaybill_no
156
    shipment[F_AWB_NUMBER] = airwaybill_no
130
    shipment[F_ORDER_NUMBER] = order.logisticsTransactionId 
157
    shipment[F_ORDER_NUMBER] = order.logisticsTransactionId 
131
    shipment[F_PRODUCT] = 'COD' if order.cod and total_payable > 0 else 'PPD' 
158
    shipment[F_PRODUCT] = 'COD' if order.cod and total_payable > 0 else 'PPD' 
132
    shipment[F_CONSIGNEE] = order.customer_name 
159
    shipment[F_CONSIGNEE] = order.customer_name 
133
    shipment[F_CONSIGNEE_ADDRESS1] = order.customer_address1 
160
    shipment[F_CONSIGNEE_ADDRESS1] = order.customer_address1 
134
    shipment[F_CONSIGNEE_ADDRESS2] = order.customer_address2
161
    shipment[F_CONSIGNEE_ADDRESS2] = order.customer_address2
-
 
162
    shipment[F_CONSIGNEE_ADDRESS3] = ''
135
    shipment[F_DESTINATION_CITY] = order.customer_city 
163
    shipment[F_DESTINATION_CITY] = order.customer_city 
136
    shipment[F_PICKUP_PINCODE] = order.customer_pincode 
-
 
137
    shipment[F_STATE] = order.customer_state
164
    shipment[F_STATE] = order.customer_state
-
 
165
    shipment[F_TELEPHONE] = "0123456789"
138
    shipment[F_MOBILE] = order.customer_mobilenumber 
166
    shipment[F_MOBILE] = order.customer_mobilenumber 
139
    shipment[F_ITEM_DESCRIPTION] = '' 
167
    shipment[F_ITEM_DESCRIPTION] = "/".join(itemDescription)
-
 
168
    shipment[F_PINCODE] = '110037'
140
    shipment[F_PIECES] = total_pieces
169
    shipment[F_PIECES] = int(total_pieces)
141
    shipment[F_COLLECTABLE_VALUE] = total_payable if order.cod else 0
170
    shipment[F_COLLECTABLE_VALUE] = total_payable if order.cod else 0
142
    shipment[F_DECLARED_VALUE] = declared_value 
171
    shipment[F_DECLARED_VALUE] = declared_value 
143
    shipment[F_ACTUAL_WEIGHT] = total_weight 
172
    shipment[F_ACTUAL_WEIGHT] = total_weight 
144
    #shipment[F_VOLUMETRIC_WEIGHT]
173
    shipment[F_VOLUMETRIC_WEIGHT] = '0'
145
    shipment[F_LENGTH] = 10
174
    shipment[F_LENGTH] = 10
146
    shipment[F_BREADTH] = 10
175
    shipment[F_BREADTH] = 10
147
    shipment[F_HEIGHT] = 10
176
    shipment[F_HEIGHT] = 10
148
    shipment[F_PICKUP_NAME] = warehouseAddressObj.contact_person
177
    shipment[F_PICKUP_NAME] = warehouseAddressObj.contact_person
149
    shipment[F_PICKUP_ADDRESS_LINE1] = warehouseAddressObj.address
178
    shipment[F_PICKUP_ADDRESS_LINE1] = warehouseAddressObj.address
150
    #shipment[F_PICKUP_ADDRESS_LINE2] = ''
179
    shipment[F_PICKUP_ADDRESS_LINE2] = ''
151
    shipment[F_PICKUP_PINCODE] = warehouseAddressObj.pin
180
    shipment[F_PICKUP_PINCODE] = warehouseAddressObj.pin
152
    shipment[F_PICKUP_MOBILE] = warehouseAddressObj.contact_number
181
    shipment[F_PICKUP_MOBILE] = warehouseAddressObj.contact_number
153
    shipment[F_PICKUP_PHONE] = warehouseAddressObj.contact_number
182
    shipment[F_PICKUP_PHONE] = warehouseAddressObj.contact_number
154
    
183
    
155
    shipment[F_RETURN_NAME] = warehouseAddressObj.contact_person
184
    shipment[F_RETURN_NAME] = warehouseAddressObj.contact_person
156
    shipment[F_RETURN_ADDRESS_LINE1] = warehouseAddressObj.address
185
    shipment[F_RETURN_ADDRESS_LINE1] = warehouseAddressObj.address
157
    #shipment[F_RETURN_ADDRESS_LINE2] = ''
186
    shipment[F_RETURN_ADDRESS_LINE2] = ''
158
    shipment[F_RETURN_PINCODE] = warehouseAddressObj.pin
187
    shipment[F_RETURN_PINCODE] = warehouseAddressObj.pin
159
    shipment[F_RETURN_MOBILE] = warehouseAddressObj.contact_number
188
    shipment[F_RETURN_MOBILE] = warehouseAddressObj.contact_number
160
    shipment[F_RETURN_PHONE] = warehouseAddressObj.contact_number
189
    shipment[F_RETURN_PHONE] = warehouseAddressObj.contact_number
161
    shipment[F_DG_SHIPMENT] = 'false'
190
    shipment[F_DG_SHIPMENT] = 'false'
162
    shipment["ADDITIONAL_INFORMATION"] = {"MULTI_SELLER_INFORMATION": _getMultiSellerInfo(orders)} 
191
    shipment[F_ADDITIONAL_INFORMATION] = {"MULTI_SELLER_INFORMATION": _getMultiSellerInfo(orders, sellerInfo, warehouseAddressObj)}
-
 
192
    return shipment 
163
    
193
    
-
 
194
def _getMultiSellerInfo(orders, sellerInfo, warehouseAddressObj):
-
 
195
    for order in orders:
-
 
196
        lineitem = order.lineitems[0]
-
 
197
        totalTaxRate = lineitem.sgstRate + lineitem.cgstRate + lineitem.igstRate
-
 
198
        stateMaster = fetchStateMaster()
-
 
199
        return {
-
 
200
            "ITEM_DESCRIPTION": " ".join(filter(None, [lineitem.brand, lineitem.model_name,  lineitem.model_number])),
-
 
201
            "ITEM_VALUE": lineitem.unit_price,
-
 
202
            "ITEM_CATEGORY": "Mobile/Mobile Accessory",
-
 
203
            "SELLER_NAME": sellerInfo.organisationName,
-
 
204
            "SELLER_ADDRESS": sellerInfo.registeredAddress,
-
 
205
            "SELLER_STATE": stateMaster[sellerInfo.stateId].shortName,
-
 
206
            "SELLER_PINCODE": warehouseAddressObj.pin,
-
 
207
            "SELLER_TIN": "",
-
 
208
            "INVOICE_NUMBER": order.invoice_number,
-
 
209
            "INVOICE_DATE": datetime.strftime(to_py_date(order.billing_timestamp), '%d-%b-%Y'),
-
 
210
            "ESUGAM_NUMBER": "",
-
 
211
            "SELLER_GSTIN": sellerInfo.gstin,
-
 
212
            "GST_HSN": lineitem.hsnCode,
-
 
213
            "GST_TAX_BASE": lineitem.unit_price,
-
 
214
            "DISCOUNT": 0.0,
-
 
215
            "GST_TAX_RATE_CGSTN": lineitem.cgstRate,
-
 
216
            "GST_TAX_RATE_SGSTN": lineitem.sgstRate,
-
 
217
            "GST_TAX_RATE_IGSTN": lineitem.igstRate,
-
 
218
            "GST_TAX_TOTAL": lineitem.unit_price - lineitem.unit_price / (1 + (totalTaxRate/100)),
-
 
219
            "GST_TAX_CGSTN": lineitem.unit_price - lineitem.unit_price / (1 + (lineitem.cgstRate/100)),
-
 
220
            "GST_TAX_SGSTN": lineitem.unit_price - lineitem.unit_price / (1 + (lineitem.sgstRate/100)),
-
 
221
            "GST_TAX_IGSTN": lineitem.unit_price - lineitem.unit_price / (1 + (lineitem.igstRate/100)),
-
 
222
        }
-
 
223
        
-
 
224
#we will always fetch for previous date
164
def _getMultiSellerInfo(orders):
225
def getServiceablePinCodes(date=None):
-
 
226
    if date:
-
 
227
        dateString = datetime.strftime(date, '%Y-%m-%d')
165
    pass
228
    else:
-
 
229
        dateString = ''
-
 
230
    print dateString
-
 
231
    values = {'username' : username,
-
 
232
              'password' : password,
-
 
233
              'date' :  dateString,
-
 
234
              'state' : ''
-
 
235
              }
-
 
236
    data = urllib.urlencode(values)
-
 
237
    req = urllib2.Request(serviceablePincodeApi, data)
-
 
238
    response = urllib2.urlopen(req)
-
 
239
    the_page = response.read()    
-
 
240
    return json.loads(the_page)
166
 
241
 
167
def main():
242
def main():
168
    lc = LogisticsClient().get_client()
-
 
169
    print lc.getEmptyAWB(48, '731467')
243
    print generate_awb(DeliveryType.COD)
170
    
-
 
171
    
-
 
172
 
244
 
173
if __name__ == '__main__':
-
 
174
    ordersMap = {}
-
 
175
    forward_request(ordersMap)
-
 
176
    
-
 
177
    
-
 
178
    
-
 
179
    
-
 
180
    
-
 
181
    
-
 
182
    
-
 
183
def get_shipper_object(address_id):
245
def getBuyerWarehouse(warehouse_id):
184
    global shipperMap
246
    global shipperMap
185
    if shipperMap.has_key(address_id):
247
    if shipperMap.has_key(warehouse_id):
186
        return shipperMap.get(address_id)
248
        return shipperMap.get(warehouse_id)
187
    else:
249
    else:
188
        tc = TransactionClient().get_client()
250
        tc = TransactionClient().get_client()
189
        info = tc.getBuyerByWarehouse(address_id)
251
        info = tc.getBuyerByWarehouse(warehouse_id)
190
        if info is None:
252
        if info is None:
191
            raise RuntimeError("Buyer address mapping is None")
253
            raise RuntimeError("Buyer address mapping is None")
192
        shipperMap[address_id] = info
-
 
193
    return shipperMap.get(address_id)
-
 
194
254
        shipperMap[warehouse_id] = info
-
 
255
    return shipperMap.get(warehouse_id)    
-
 
256
    
-
 
257
 
-
 
258
if __name__ == '__main__':
-
 
259
    #oc = TransactionClient().get_client()
-
 
260
    #awbs = generate_awb(DeliveryType.COD)
-
 
261
    #'706831493', '706831494'
-
 
262
    #logisticsTxnId = '1036348-1'
-
 
263
    #orders = oc.getGroupOrdersByLogisticsTxnId(logisticsTxnId)
-
 
264
    #ordersMap = {}
-
 
265
    #forward_request('706831494', logisticsTxnId)
-
 
266
    print getServiceablePinCodes(datetime.now()-timedelta(days=30))
-
 
267
    #getServiceablePinCodes()
-
 
268