| 5944 |
mandeep.dh |
1 |
#!/usr/bin/python
|
|
|
2 |
'''
|
|
|
3 |
Load the whole inventory for a warehouse.
|
|
|
4 |
|
|
|
5 |
With the amount of data that we have now and the amount
|
|
|
6 |
of traffic we get, it's now impracitcal to do this for
|
|
|
7 |
already existing warehouses. It should only be used for
|
|
|
8 |
a newly provisioned warehouse.
|
|
|
9 |
|
|
|
10 |
@author: Chandranshu
|
|
|
11 |
'''
|
|
|
12 |
import optparse
|
|
|
13 |
import xlrd
|
|
|
14 |
import datetime
|
|
|
15 |
|
|
|
16 |
if __name__ == '__main__' and __package__ is None:
|
|
|
17 |
import sys
|
|
|
18 |
import os
|
|
|
19 |
sys.path.insert(0, os.getcwd())
|
|
|
20 |
|
|
|
21 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
22 |
from shop2020.model.v1.catalog.impl.DataAcessors import update_inventory
|
|
|
23 |
from elixir import *
|
|
|
24 |
|
|
|
25 |
def read_inventory_data(filename, product_group):
|
|
|
26 |
workbook = xlrd.open_workbook(filename)
|
|
|
27 |
sheet = workbook.sheet_by_index(0)
|
|
|
28 |
num_rows = sheet.nrows
|
|
|
29 |
availability = {}
|
|
|
30 |
for rownum in range(1, num_rows):
|
|
|
31 |
if product_group:
|
|
|
32 |
brand, model_number, color, quantity = sheet.row_values(rownum)[0:4]
|
|
|
33 |
else:
|
|
|
34 |
product_group, brand, model_number, color, quantity = sheet.row_values(rownum)[0:5]
|
|
|
35 |
if isinstance(model_number, float):
|
|
|
36 |
model_number = str(int(model_number))
|
|
|
37 |
print sheet.row_values(rownum)
|
|
|
38 |
itemid = product_group + ";" + brand + ";" + model_number + ";" + color
|
|
|
39 |
|
|
|
40 |
if availability.has_key(itemid):
|
|
|
41 |
quantity = quantity + availability.get(itemid)
|
|
|
42 |
|
|
|
43 |
availability[itemid] = quantity
|
|
|
44 |
return availability
|
|
|
45 |
|
|
|
46 |
def load_inventory_data(filename, warehouse_id, product_group):
|
|
|
47 |
DataService.initialize('catalog')
|
|
|
48 |
availability = read_inventory_data(filename, product_group)
|
|
|
49 |
update_inventory(warehouse_id, datetime.datetime.now(), availability)
|
|
|
50 |
session.commit()
|
|
|
51 |
print "Successfully updated the item inventory information."
|
|
|
52 |
|
|
|
53 |
def main():
|
|
|
54 |
parser = optparse.OptionParser()
|
|
|
55 |
parser.add_option("-f", "--file", dest="filename",
|
|
|
56 |
default="ItemInventory.xls", type="string",
|
|
|
57 |
help="Read the item inventory from FILE",
|
|
|
58 |
metavar="FILE")
|
|
|
59 |
parser.add_option("-w", "--warehouse", dest="warehouse_id",
|
|
|
60 |
default="1", type="string",
|
|
|
61 |
help="Load the item inventory for the warehouse with WarehouseId as id",
|
|
|
62 |
metavar="WarehouseId")
|
|
|
63 |
parser.add_option("-p", "--product-group", dest="product_group",
|
|
|
64 |
default=None, type="string",
|
|
|
65 |
help="Assume PRODUCT_GROUP to be the product group of all items",
|
|
|
66 |
metavar="PRODUCT_GROUP")
|
|
|
67 |
(options, args) = parser.parse_args()
|
|
|
68 |
if len(args) != 0:
|
|
|
69 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
70 |
filename = options.filename
|
|
|
71 |
warehouse_id = options.warehouse_id
|
|
|
72 |
load_inventory_data(filename, warehouse_id, options.product_group)
|
|
|
73 |
|
|
|
74 |
if __name__ == '__main__':
|
|
|
75 |
main()
|