Subversion Repositories SmartDukaan

Rev

Rev 4090 | Rev 12785 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python

'''
Loads the static logic of assigning provider to a particular pincode
into the DestinationProviderAllocation table.

It reads the logic from a XLS workbook containing two sheets. The first sheet is
for shipments worth less than Rs 5000. The second sheet is for shipments costing
more than Rs 5000.

@attention: This script may need enhancement if we want to differentiate between
COD and Prepaid orders.

@attention: This table is not being used currently. If and when this comes into 
use, please remember to update the logic of get_logistics_estimation 
in DataAccessor.py. 

@author: Ankur Singhal
'''
import optparse
import xlrd
from elixir import *
from shop2020.thriftpy.model.v1 import inventory

if __name__ == '__main__' and __package__ is None:
    import sys
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.logistics.service.impl import DataService
from shop2020.logistics.service.impl.DataService import Provider, DestinationProviderAllocation
from shop2020.thriftpy.model.v1.inventory.ttypes import WarehouseLocation, InventoryServiceException

def load_providers(filename, sheet_no, db_hostname):
    DataService.initialize(dbname='logistics', db_hostname=db_hostname)

    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(sheet_no)
    num_rows = sheet.nrows
    providers = Provider.query.all()
    provider_map = dict([(p.name, p) for p in providers])
    provider_for_warehouse_location = {}

    for rownum in range(1, num_rows):
        dest_pincode, provider_for_warehouse_location[WarehouseLocation.Delhi], provider_for_warehouse_location[WarehouseLocation.Mumbai],\
         provider_for_warehouse_location[WarehouseLocation.Bangalore], provider_for_warehouse_location[WarehouseLocation.Kolkata] = sheet.row_values(rownum)[0:5]
        dest_pincode = str(int(dest_pincode))
        
        for warehouse_loc, provider_name in provider_for_warehouse_location.iteritems():
            if provider_name == '' or provider_name == None:
                continue
            try:
                query = DestinationProviderAllocation.query.filter_by(destination_pin = dest_pincode)
                query = query.filter_by(warehouse_location = warehouse_loc)
                dpa = query.one()
            except:
                dpa = DestinationProviderAllocation()
                dpa.destination_pin = dest_pincode
                dpa.warehouse_location = warehouse_loc
            provider = provider_map[provider_name]
            if provider is None:
                raise InventoryServiceException(103, "Invalid provider --> " + provider_name)
            if sheet_no == 0:
                dpa.provider_less_amount = provider
            else:
                dpa.provider_more_amount = provider
    session.commit()

def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                   default="DestinationProviderAllocation.xls", type="string",
                   help="Read the providers list for different destinations and warehouse locations from FILE",
                   metavar="FILE")
    parser.add_option("-H", "--host", dest="hostname",
                  default="localhost",
                  type="string", help="The HOST where the DB server is running",
                  metavar="HOST")
    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
    filename = options.filename
    if filename is None:
        parser.error("A filename must be provided. Use -h for more details.")
    load_providers(filename, 0, options.hostname)
    load_providers(filename, 1, options.hostname)

if __name__ == '__main__':
    main()