Subversion Repositories SmartDukaan

Rev

Rev 5944 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
'''
Load the whole inventory for a warehouse.

With the amount of data that we have now and the amount
of traffic we get, it's now impracitcal to do this for 
already existing warehouses. It should only be used for
a newly provisioned warehouse.

@author: Chandranshu
'''
import optparse
import xlrd
import datetime

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

from shop2020.model.v1.inventory.impl import DataService
from shop2020.model.v1.inventory.impl.DataAcessors import update_inventory
from elixir import *

def read_inventory_data(filename, product_group):
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    num_rows = sheet.nrows
    availability = {}
    for rownum in range(1, num_rows):
        if product_group:
            brand, model_number, color, quantity = sheet.row_values(rownum)[0:4]
        else:
            product_group, brand, model_number, color, quantity = sheet.row_values(rownum)[0:5]
        if isinstance(model_number, float):
            model_number = str(int(model_number))
        print sheet.row_values(rownum)
        itemid = product_group + ";" + brand + ";" + model_number + ";" + color
        
        if availability.has_key(itemid):
            quantity = quantity + availability.get(itemid)
       
        availability[itemid] = quantity
    return availability

def load_inventory_data(filename, warehouse_id, product_group):
    DataService.initialize('catalog')    
    availability = read_inventory_data(filename, product_group)
    update_inventory(warehouse_id, datetime.datetime.now(), availability)
    session.commit()
    print "Successfully updated the item inventory information."

def main():
    parser = optparse.OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                   default="ItemInventory.xls", type="string",
                   help="Read the item inventory from FILE",
                   metavar="FILE")
    parser.add_option("-w", "--warehouse", dest="warehouse_id",
                   default="1", type="string",
                   help="Load the item inventory for the warehouse with WarehouseId as id",
                   metavar="WarehouseId")
    parser.add_option("-p", "--product-group", dest="product_group",
                      default=None, type="string",
                      help="Assume PRODUCT_GROUP to be the product group of all items",
                      metavar="PRODUCT_GROUP")
    (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
    warehouse_id = options.warehouse_id
    load_inventory_data(filename, warehouse_id, options.product_group)

if __name__ == '__main__':
    main()