Subversion Repositories SmartDukaan

Rev

Rev 4090 | Rev 12785 | 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 *
7808 anupam.sin 23
from shop2020.thriftpy.model.v1 import inventory
1504 ankur.sing 24
 
25
if __name__ == '__main__' and __package__ is None:
26
    import sys
27
    import os
28
    sys.path.insert(0, os.getcwd())
29
 
30
from shop2020.logistics.service.impl import DataService
7808 anupam.sin 31
from shop2020.logistics.service.impl.DataService import Provider, DestinationProviderAllocation
32
from shop2020.thriftpy.model.v1.inventory.ttypes import WarehouseLocation, InventoryServiceException
1504 ankur.sing 33
 
4014 chandransh 34
def load_providers(filename, sheet_no, db_hostname):
35
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)
1504 ankur.sing 36
 
37
    workbook = xlrd.open_workbook(filename)
38
    sheet = workbook.sheet_by_index(sheet_no)
39
    num_rows = sheet.nrows
40
    providers = Provider.query.all()
41
    provider_map = dict([(p.name, p) for p in providers])
42
    provider_for_warehouse_location = {}
43
 
44
    for rownum in range(1, num_rows):
45
        dest_pincode, provider_for_warehouse_location[WarehouseLocation.Delhi], provider_for_warehouse_location[WarehouseLocation.Mumbai],\
46
         provider_for_warehouse_location[WarehouseLocation.Bangalore], provider_for_warehouse_location[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:5]
47
        dest_pincode = str(int(dest_pincode))
48
 
49
        for warehouse_loc, provider_name in provider_for_warehouse_location.iteritems():
50
            if provider_name == '' or provider_name == None:
51
                continue
52
            try:
53
                query = DestinationProviderAllocation.query.filter_by(destination_pin = dest_pincode)
54
                query = query.filter_by(warehouse_location = warehouse_loc)
55
                dpa = query.one()
56
            except:
57
                dpa = DestinationProviderAllocation()
58
                dpa.destination_pin = dest_pincode
59
                dpa.warehouse_location = warehouse_loc
60
            provider = provider_map[provider_name]
61
            if provider is None:
7808 anupam.sin 62
                raise InventoryServiceException(103, "Invalid provider --> " + provider_name)
1504 ankur.sing 63
            if sheet_no == 0:
64
                dpa.provider_less_amount = provider
65
            else:
66
                dpa.provider_more_amount = provider
67
    session.commit()
68
 
69
def main():
70
    parser = optparse.OptionParser()
71
    parser.add_option("-f", "--file", dest="filename",
72
                   default="DestinationProviderAllocation.xls", type="string",
73
                   help="Read the providers list for different destinations and warehouse locations from FILE",
74
                   metavar="FILE")
4014 chandransh 75
    parser.add_option("-H", "--host", dest="hostname",
76
                  default="localhost",
77
                  type="string", help="The HOST where the DB server is running",
78
                  metavar="HOST")
1504 ankur.sing 79
    (options, args) = parser.parse_args()
80
    if len(args) != 0:
81
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
82
    filename = options.filename
83
    if filename is None:
84
        parser.error("A filename must be provided. Use -h for more details.")
4014 chandransh 85
    load_providers(filename, 0, options.hostname)
86
    load_providers(filename, 1, options.hostname)
1504 ankur.sing 87
 
88
if __name__ == '__main__':
89
    main()