| 1504 |
ankur.sing |
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
import optparse
|
|
|
4 |
import xlrd
|
|
|
5 |
from elixir import *
|
|
|
6 |
|
|
|
7 |
if __name__ == '__main__' and __package__ is None:
|
|
|
8 |
import sys
|
|
|
9 |
import os
|
|
|
10 |
sys.path.insert(0, os.getcwd())
|
|
|
11 |
|
|
|
12 |
from shop2020.logistics.service.impl import DataService
|
|
|
13 |
from shop2020.logistics.service.impl.DataService import Provider,\
|
|
|
14 |
DestinationProviderAllocation
|
|
|
15 |
from shop2020.thriftpy.logistics.ttypes import WarehouseLocation,\
|
|
|
16 |
LogisticsServiceException
|
|
|
17 |
|
|
|
18 |
def load_providers(filename, sheet_no):
|
|
|
19 |
DataService.initialize('logistics')
|
|
|
20 |
|
|
|
21 |
workbook = xlrd.open_workbook(filename)
|
|
|
22 |
sheet = workbook.sheet_by_index(sheet_no)
|
|
|
23 |
num_rows = sheet.nrows
|
|
|
24 |
providers = Provider.query.all()
|
|
|
25 |
provider_map = dict([(p.name, p) for p in providers])
|
|
|
26 |
provider_for_warehouse_location = {}
|
|
|
27 |
|
|
|
28 |
for rownum in range(1, num_rows):
|
|
|
29 |
dest_pincode, provider_for_warehouse_location[WarehouseLocation.Delhi], provider_for_warehouse_location[WarehouseLocation.Mumbai],\
|
|
|
30 |
provider_for_warehouse_location[WarehouseLocation.Bangalore], provider_for_warehouse_location[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:5]
|
|
|
31 |
dest_pincode = str(int(dest_pincode))
|
|
|
32 |
|
|
|
33 |
for warehouse_loc, provider_name in provider_for_warehouse_location.iteritems():
|
|
|
34 |
if provider_name == '' or provider_name == None:
|
|
|
35 |
continue
|
|
|
36 |
try:
|
|
|
37 |
query = DestinationProviderAllocation.query.filter_by(destination_pin = dest_pincode)
|
|
|
38 |
query = query.filter_by(warehouse_location = warehouse_loc)
|
|
|
39 |
dpa = query.one()
|
|
|
40 |
except:
|
|
|
41 |
dpa = DestinationProviderAllocation()
|
|
|
42 |
dpa.destination_pin = dest_pincode
|
|
|
43 |
dpa.warehouse_location = warehouse_loc
|
|
|
44 |
provider = provider_map[provider_name]
|
|
|
45 |
if provider is None:
|
|
|
46 |
raise LogisticsServiceException(103, "Invalid provider --> " + provider_name)
|
|
|
47 |
if sheet_no == 0:
|
|
|
48 |
dpa.provider_less_amount = provider
|
|
|
49 |
else:
|
|
|
50 |
dpa.provider_more_amount = provider
|
|
|
51 |
session.commit()
|
|
|
52 |
|
|
|
53 |
def main():
|
|
|
54 |
parser = optparse.OptionParser()
|
|
|
55 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
56 |
default="DestinationProviderAllocation.xls", type="string",
|
|
|
57 |
help="Read the providers list for different destinations and warehouse locations from FILE",
|
|
|
58 |
metavar="FILE")
|
|
|
59 |
(options, args) = parser.parse_args()
|
|
|
60 |
if len(args) != 0:
|
|
|
61 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
62 |
filename = options.filename
|
|
|
63 |
if filename is None:
|
|
|
64 |
parser.error("A filename must be provided. Use -h for more details.")
|
|
|
65 |
load_providers(filename, 0)
|
|
|
66 |
load_providers(filename, 1)
|
|
|
67 |
|
|
|
68 |
if __name__ == '__main__':
|
|
|
69 |
main()
|