Subversion Repositories SmartDukaan

Rev

Rev 23206 | Rev 23211 | 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()
23132 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]}
23132 amit.gupta 119
def forward_request(airwaybill_no, logisticsTxnId):
23145 amit.gupta 120
    try:
121
        client = TransactionClient().get_client()
122
        orders = client.getGroupOrdersByLogisticsTxnId(logisticsTxnId)
123
        values = {'username' : username,
124
                  'password' : password,
125
                  'json_input' : _forwardMap(airwaybill_no, orders),
126
                  }
127
        print "values - ", values 
128
        data = urllib.urlencode(values)
129
        print "data - ", data 
130
        req = urllib2.Request(forwardApi, data)
131
        response = urllib2.urlopen(req)
132
        the_page = response.read()
133
        jsonResponse = json.loads(the_page)
134
        return jsonResponse[0]['success']
135
    except:
136
        traceback.print_exc()
137
        return False
22749 amit.gupta 138
 
23132 amit.gupta 139
def _forwardMap(airwaybill_no, orders):
22749 amit.gupta 140
    shipments = []
23132 amit.gupta 141
    shipments.append(_createShipment(airwaybill_no, orders))
142
    return json.dumps(shipments)
22749 amit.gupta 143
 
144
 
23132 amit.gupta 145
def _createShipment(airwaybill_no, orders):
22749 amit.gupta 146
    shipment = {}
147
    total_payable = 0
148
    total_pieces = 0
149
    declared_value = 0
150
    total_weight = 0
23132 amit.gupta 151
    order = orders[0]
152
    sellerInfo = getBuyerWarehouse(order.warehouse_id)
153
    itemDescription = []
22749 amit.gupta 154
    for order1 in orders:
155
        total_payable += order1.net_payable_amount
156
        total_pieces += order1.lineitems[0].quantity
23132 amit.gupta 157
        declared_value += order1.total_amount
22749 amit.gupta 158
        total_weight += order1.total_weight
23132 amit.gupta 159
        itemDescription.append(" ".join(filter(None, [order.lineitems[0].brand, order.lineitems[0].model_name,  order.lineitems[0].model_number])))
160
    warehouseAddressObj = sellerInfo.buyerAddress
161
    shipment[F_AWB_NUMBER] = airwaybill_no
22749 amit.gupta 162
    shipment[F_ORDER_NUMBER] = order.logisticsTransactionId 
163
    shipment[F_PRODUCT] = 'COD' if order.cod and total_payable > 0 else 'PPD' 
164
    shipment[F_CONSIGNEE] = order.customer_name 
165
    shipment[F_CONSIGNEE_ADDRESS1] = order.customer_address1 
166
    shipment[F_CONSIGNEE_ADDRESS2] = order.customer_address2
23132 amit.gupta 167
    shipment[F_CONSIGNEE_ADDRESS3] = ''
22749 amit.gupta 168
    shipment[F_DESTINATION_CITY] = order.customer_city 
169
    shipment[F_STATE] = order.customer_state
23132 amit.gupta 170
    shipment[F_TELEPHONE] = "0123456789"
22749 amit.gupta 171
    shipment[F_MOBILE] = order.customer_mobilenumber 
23132 amit.gupta 172
    shipment[F_ITEM_DESCRIPTION] = "/".join(itemDescription)
173
    shipment[F_PINCODE] = '110037'
174
    shipment[F_PIECES] = int(total_pieces)
22749 amit.gupta 175
    shipment[F_COLLECTABLE_VALUE] = total_payable if order.cod else 0
176
    shipment[F_DECLARED_VALUE] = declared_value 
177
    shipment[F_ACTUAL_WEIGHT] = total_weight 
23132 amit.gupta 178
    shipment[F_VOLUMETRIC_WEIGHT] = '0'
22749 amit.gupta 179
    shipment[F_LENGTH] = 10
180
    shipment[F_BREADTH] = 10
181
    shipment[F_HEIGHT] = 10
182
    shipment[F_PICKUP_NAME] = warehouseAddressObj.contact_person
183
    shipment[F_PICKUP_ADDRESS_LINE1] = warehouseAddressObj.address
23132 amit.gupta 184
    shipment[F_PICKUP_ADDRESS_LINE2] = ''
22749 amit.gupta 185
    shipment[F_PICKUP_PINCODE] = warehouseAddressObj.pin
186
    shipment[F_PICKUP_MOBILE] = warehouseAddressObj.contact_number
187
    shipment[F_PICKUP_PHONE] = warehouseAddressObj.contact_number
188
 
189
    shipment[F_RETURN_NAME] = warehouseAddressObj.contact_person
190
    shipment[F_RETURN_ADDRESS_LINE1] = warehouseAddressObj.address
23132 amit.gupta 191
    shipment[F_RETURN_ADDRESS_LINE2] = ''
22749 amit.gupta 192
    shipment[F_RETURN_PINCODE] = warehouseAddressObj.pin
193
    shipment[F_RETURN_MOBILE] = warehouseAddressObj.contact_number
194
    shipment[F_RETURN_PHONE] = warehouseAddressObj.contact_number
195
    shipment[F_DG_SHIPMENT] = 'false'
23132 amit.gupta 196
    shipment[F_ADDITIONAL_INFORMATION] = {"MULTI_SELLER_INFORMATION": _getMultiSellerInfo(orders, sellerInfo, warehouseAddressObj)}
197
    return shipment 
22749 amit.gupta 198
 
23132 amit.gupta 199
def _getMultiSellerInfo(orders, sellerInfo, warehouseAddressObj):
23205 amit.gupta 200
    itemIds = []
23206 amit.gupta 201
 
202
 
23205 amit.gupta 203
    inventory_client = InventoryClient().get_client()
204
    warehouse = inventory_client.getWarehouse(orders[0].warehouse_id)
205
 
23206 amit.gupta 206
    cc = CatalogClient().get_client()
23205 amit.gupta 207
    if sellerInfo.stateId == warehouse.stateId:
208
        taxRate = cc.getStateTaxRate(itemIds, sellerInfo.stateId)
209
    else:
210
        taxRate = cc.getStateTaxRate(itemIds, -1)
211
 
23132 amit.gupta 212
    for order in orders:
213
        lineitem = order.lineitems[0]
23205 amit.gupta 214
    for order in orders:
215
        lineitem = order.lineitems[0]
23210 amit.gupta 216
        totalTaxRate = taxRate.sgstRate + taxRate.cgstRate + taxRate.igstRate
23132 amit.gupta 217
        stateMaster = fetchStateMaster()
218
        return {
219
            "ITEM_DESCRIPTION": " ".join(filter(None, [lineitem.brand, lineitem.model_name,  lineitem.model_number])),
220
            "ITEM_VALUE": lineitem.unit_price,
221
            "ITEM_CATEGORY": "Mobile/Mobile Accessory",
222
            "SELLER_NAME": sellerInfo.organisationName,
223
            "SELLER_ADDRESS": sellerInfo.registeredAddress,
224
            "SELLER_STATE": stateMaster[sellerInfo.stateId].shortName,
225
            "SELLER_PINCODE": warehouseAddressObj.pin,
226
            "SELLER_TIN": "",
227
            "INVOICE_NUMBER": order.invoice_number,
228
            "INVOICE_DATE": datetime.strftime(to_py_date(order.billing_timestamp), '%d-%b-%Y'),
229
            "ESUGAM_NUMBER": "",
230
            "SELLER_GSTIN": sellerInfo.gstin,
231
            "GST_HSN": lineitem.hsnCode,
232
            "GST_TAX_BASE": lineitem.unit_price,
233
            "DISCOUNT": 0.0,
23205 amit.gupta 234
            "GST_TAX_RATE_CGSTN": taxRate.cgstRate,
235
            "GST_TAX_RATE_SGSTN": taxRate.sgstRate,
236
            "GST_TAX_RATE_IGSTN": taxRate.igstRate,
23132 amit.gupta 237
            "GST_TAX_TOTAL": lineitem.unit_price - lineitem.unit_price / (1 + (totalTaxRate/100)),
238
            "GST_TAX_CGSTN": lineitem.unit_price - lineitem.unit_price / (1 + (lineitem.cgstRate/100)),
239
            "GST_TAX_SGSTN": lineitem.unit_price - lineitem.unit_price / (1 + (lineitem.sgstRate/100)),
240
            "GST_TAX_IGSTN": lineitem.unit_price - lineitem.unit_price / (1 + (lineitem.igstRate/100)),
241
        }
242
 
243
#we will always fetch for previous date
244
def getServiceablePinCodes(date=None):
245
    if date:
246
        dateString = datetime.strftime(date, '%Y-%m-%d')
247
    else:
248
        dateString = ''
249
    print dateString
250
    values = {'username' : username,
251
              'password' : password,
252
              'date' :  dateString,
253
              'state' : ''
254
              }
255
    data = urllib.urlencode(values)
256
    req = urllib2.Request(serviceablePincodeApi, data)
257
    response = urllib2.urlopen(req)
258
    the_page = response.read()    
259
    return json.loads(the_page)
22636 amit.gupta 260
 
261
def main():
23132 amit.gupta 262
    print generate_awb(DeliveryType.COD)
22636 amit.gupta 263
 
23132 amit.gupta 264
def getBuyerWarehouse(warehouse_id):
22749 amit.gupta 265
    global shipperMap
23132 amit.gupta 266
    if shipperMap.has_key(warehouse_id):
267
        return shipperMap.get(warehouse_id)
22749 amit.gupta 268
    else:
269
        tc = TransactionClient().get_client()
23132 amit.gupta 270
        info = tc.getBuyerByWarehouse(warehouse_id)
22749 amit.gupta 271
        if info is None:
272
            raise RuntimeError("Buyer address mapping is None")
23132 amit.gupta 273
        shipperMap[warehouse_id] = info
274
    return shipperMap.get(warehouse_id)    
275
 
276
 
277
if __name__ == '__main__':
278
    #oc = TransactionClient().get_client()
279
    #awbs = generate_awb(DeliveryType.COD)
280
    #'706831493', '706831494'
281
    #logisticsTxnId = '1036348-1'
282
    #orders = oc.getGroupOrdersByLogisticsTxnId(logisticsTxnId)
283
    #ordersMap = {}
284
    #forward_request('706831494', logisticsTxnId)
285
    print getServiceablePinCodes(datetime.now()-timedelta(days=30))
286
    #getServiceablePinCodes()