Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

from elixir import *
from functools import partial
from shop2020.clients.CatalogClient import CatalogClient
from shop2020.clients.TransactionClient import TransactionClient
from shop2020.model.v1.inventory.impl import DataService
from shop2020.model.v1.inventory.impl.Convertors import to_t_warehouse, \
    to_t_itemidwarehouseid, to_t_state
from shop2020.model.v1.inventory.impl.DataService import Warehouse, \
    ItemInventoryHistory, CurrentInventorySnapshot, VendorItemPricing, \
    VendorItemMapping, Vendor, MissedInventoryUpdate, BadInventorySnapshot, \
    VendorHolidays, ItemAvailabilityCache, \
    CurrentReservationSnapshot, IgnoredInventoryUpdateItems, ItemStockPurchaseParams, \
    OOSStatus, AmazonInventorySnapshot, StateMaster, HoldInventoryDetail, AmazonFbaInventorySnapshot, \
    SnapdealInventorySnapshot, FlipkartInventorySnapshot, SnapdealStockAtEOD, FlipkartStockAtEOD, StockWeightedNlcInfo
    
from shop2020.model.v1.inventory.impl.DataAcessors import clear_item_availability_cache
from shop2020.thriftpy.model.v1.order.ttypes import AlertType
from shop2020.thriftpy.purchase.ttypes import PurchaseServiceException
from shop2020.utils import EmailAttachmentSender
from shop2020.utils.EmailAttachmentSender import mail
from shop2020.utils.Utils import to_py_date, to_java_date
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
from sqlalchemy.sql import or_
from sqlalchemy.sql.expression import and_, func, distinct, desc
from sqlalchemy.sql.functions import count
import calendar
import datetime
import sys
import threading
import math
import xlrd


def add_inventory(itemId, warehouseId, quantity):
    current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
    if not current_inventory_snapshot:
        current_inventory_snapshot = CurrentInventorySnapshot()
        current_inventory_snapshot.item_id = itemId
        current_inventory_snapshot.warehouse_id = warehouseId
        current_inventory_snapshot.availability = 0
        current_inventory_snapshot.reserved = 0
        current_inventory_snapshot.held = 0
    # added the difference in the current inventory    
    current_inventory_snapshot.availability = current_inventory_snapshot.availability + quantity
    session.commit()  

        
def read_data(filename):
    DataService.initialize(db_hostname='192.168.190.114')
    workbook = xlrd.open_workbook(filename)
    sheet = workbook.sheet_by_index(0)
    num_rows = sheet.nrows
    count =0
    itemIds = []
    for rownum in range(1, num_rows):
        print sheet.row_values(rownum)
        itemId = long(sheet.row_values(rownum)[0])
        warehouse_id = long(sheet.row_values(rownum)[1])
        quantity = long(sheet.row_values(rownum)[2])
        add_inventory(itemId, warehouse_id, quantity)
        itemIds.append(itemId)
        count = count+1
    
    clear_item_availability_cache(itemIds)
    print "Count", count
    session.close()
    
def main():
    read_data("/home/manish/virtualInv.xls")

if __name__ == '__main__':
    main()