Subversion Repositories SmartDukaan

Rev

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