Subversion Repositories SmartDukaan

Rev

Rev 4014 | Rev 7808 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1504 ankur.sing 1
#!/usr/bin/python
2
 
4090 chandransh 3
'''
4
Loads the static logic of assigning provider to a particular pincode
5
into the DestinationProviderAllocation table.
6
 
7
It reads the logic from a XLS workbook containing two sheets. The first sheet is
8
for shipments worth less than Rs 5000. The second sheet is for shipments costing
9
more than Rs 5000.
10
 
11
@attention: This script may need enhancement if we want to differentiate between
12
COD and Prepaid orders.
13
 
14
@attention: This table is not being used currently. If and when this comes into 
15
use, please remember to update the logic of get_logistics_estimation 
16
in DataAccessor.py. 
17
 
18
@author: Ankur Singhal
19
'''
1504 ankur.sing 20
import optparse
21
import xlrd
22
from elixir import *
23
 
24
if __name__ == '__main__' and __package__ is None:
25
    import sys
26
    import os
27
    sys.path.insert(0, os.getcwd())
28
 
29
from shop2020.logistics.service.impl import DataService
30
from shop2020.logistics.service.impl.DataService import Provider,\
31
    DestinationProviderAllocation
32
from shop2020.thriftpy.logistics.ttypes import WarehouseLocation,\
33
    LogisticsServiceException
34
 
4014 chandransh 35
def load_providers(filename, sheet_no, db_hostname):
36
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)
1504 ankur.sing 37
 
38
    workbook = xlrd.open_workbook(filename)
39
    sheet = workbook.sheet_by_index(sheet_no)
40
    num_rows = sheet.nrows
41
    providers = Provider.query.all()
42
    provider_map = dict([(p.name, p) for p in providers])
43
    provider_for_warehouse_location = {}
44
 
45
    for rownum in range(1, num_rows):
46
        dest_pincode, provider_for_warehouse_location[WarehouseLocation.Delhi], provider_for_warehouse_location[WarehouseLocation.Mumbai],\
47
         provider_for_warehouse_location[WarehouseLocation.Bangalore], provider_for_warehouse_location[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:5]
48
        dest_pincode = str(int(dest_pincode))
49
 
50
        for warehouse_loc, provider_name in provider_for_warehouse_location.iteritems():
51
            if provider_name == '' or provider_name == None:
52
                continue
53
            try:
54
                query = DestinationProviderAllocation.query.filter_by(destination_pin = dest_pincode)
55
                query = query.filter_by(warehouse_location = warehouse_loc)
56
                dpa = query.one()
57
            except:
58
                dpa = DestinationProviderAllocation()
59
                dpa.destination_pin = dest_pincode
60
                dpa.warehouse_location = warehouse_loc
61
            provider = provider_map[provider_name]
62
            if provider is None:
63
                raise LogisticsServiceException(103, "Invalid provider --> " + provider_name)
64
            if sheet_no == 0:
65
                dpa.provider_less_amount = provider
66
            else:
67
                dpa.provider_more_amount = provider
68
    session.commit()
69
 
70
def main():
71
    parser = optparse.OptionParser()
72
    parser.add_option("-f", "--file", dest="filename",
73
                   default="DestinationProviderAllocation.xls", type="string",
74
                   help="Read the providers list for different destinations and warehouse locations from FILE",
75
                   metavar="FILE")
4014 chandransh 76
    parser.add_option("-H", "--host", dest="hostname",
77
                  default="localhost",
78
                  type="string", help="The HOST where the DB server is running",
79
                  metavar="HOST")
1504 ankur.sing 80
    (options, args) = parser.parse_args()
81
    if len(args) != 0:
82
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
83
    filename = options.filename
84
    if filename is None:
85
        parser.error("A filename must be provided. Use -h for more details.")
4014 chandransh 86
    load_providers(filename, 0, options.hostname)
87
    load_providers(filename, 1, options.hostname)
1504 ankur.sing 88
 
89
if __name__ == '__main__':
90
    main()