Subversion Repositories SmartDukaan

Rev

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