Subversion Repositories SmartDukaan

Rev

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

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