Subversion Repositories SmartDukaan

Rev

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