| Line 1... |
Line 1... |
| 1 |
'''
|
1 |
'''
|
| 2 |
Created on 27-Apr-2010
|
2 |
Created on 27-Apr-2010
|
| 3 |
|
3 |
|
| 4 |
@author: ashish
|
4 |
@author: ashish
|
| 5 |
'''
|
5 |
'''
|
| 6 |
from shop2020.model.v1.catalog.impl import DataService
|
- |
|
| 7 |
from shop2020.utils.Utils import log_entry, to_py_date
|
- |
|
| 8 |
from shop2020.model.v1.catalog.impl.DataAcessors import add_item, add_warehouse,\
|
- |
|
| 9 |
update_inventory, retire_warehouse, retire_item, change_item_status,\
|
- |
|
| 10 |
get_item, get_all_items, get_all_items_by_status,\
|
- |
|
| 11 |
get_item_availability_for_warehouse, get_item_availability_for_location, get_all_warehouses_by_status,\
|
- |
|
| 12 |
get_Warehouse, get_all_warehouses_for_item, get_all_items_for_warehouse,\
|
- |
|
| 13 |
update_item, start_item_on, close_session, \
|
- |
|
| 14 |
add_vendor, add_warehouse_vendor_mapping, get_warehouses_for_vendor, get_vendors_for_warehouse, \
|
- |
|
| 15 |
retire_item_on, get_item_inventory_by_item_id, get_items_by_catalog_id,\
|
- |
|
| 16 |
get_best_deals, get_best_deals_catalog_ids, get_best_deals_count, get_best_sellers, get_latest_arrivals, get_latest_arrivals_catalog_ids, get_latest_arrivals_count,\
|
- |
|
| 17 |
is_active, get_best_sellers_catalog_ids, get_best_sellers_count, put_category_object,\
|
- |
|
| 18 |
get_category_object, mark_item_as_content_complete,\
|
- |
|
| 19 |
reserve_item_in_warehouse, reduce_reservation_count, generate_new_entity_id,\
|
- |
|
| 20 |
get_all_item_pricing, add_vendor_pricing, \
|
- |
|
| 21 |
get_item_pricing, get_category, get_all_categories, add_category,\
|
- |
|
| 22 |
get_item_status_description, get_all_vendors,\
|
- |
|
| 23 |
get_item_mappings, add_vendor_item_mapping, check_similar_item,\
|
- |
|
| 24 |
change_risky_flag, get_items_for_mastersheet, get_risky_items,\
|
- |
|
| 25 |
get_similar_items_catalog_ids, add_product_notification,\
|
- |
|
| 26 |
send_product_notifications, update_inventory_history,\
|
- |
|
| 27 |
get_all_brands_by_category, is_alive, get_item_pricing_by_source,\
|
- |
|
| 28 |
add_source_item_pricing, get_all_sources, get_item_for_source,\
|
- |
|
| 29 |
get_all_source_pricing, get_item_count_by_status, search_items, get_search_result_count,\
|
- |
|
| 30 |
get_pending_orders_inventory, get_product_notifications, get_product_notification_request_count,\
|
- |
|
| 31 |
add_inventory, get_all_similar_items_catalog_ids, add_similar_item_catalog_id, delete_similar_item_catalog_id, add_authorization_log_for_item,\
|
- |
|
| 32 |
get_thrift_item_list, get_all_brands, mark_missed_inventory_updates_as_processed, \
|
- |
|
| 33 |
reset_availability, get_item_keys_to_be_processed
|
- |
|
| 34 |
from shop2020.model.v1.catalog.impl.Convertors import to_t_item,\
|
- |
|
| 35 |
to_t_item_inventory, to_t_warehouse, to_t_vendor_item_pricing, to_t_category, to_t_vendor,\
|
- |
|
| 36 |
to_t_vendor_item_mapping, to_t_source_item_pricing, to_t_product_notification_request,\
|
- |
|
| 37 |
to_t_product_notification_request_count
|
- |
|
| 38 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status,\
|
- |
|
| 39 |
InventoryServiceException, AvailableAndReservedStock
|
- |
|
| 40 |
from shop2020.config.client.ConfigClient import ConfigClient
|
- |
|
| 41 |
from shop2020.clients.CatalogClient import CatalogClient
|
6 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 42 |
from shop2020.clients.PurchaseClient import PurchaseClient
|
7 |
from shop2020.clients.PurchaseClient import PurchaseClient
|
| - |
|
8 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| - |
|
9 |
from shop2020.model.v1.catalog.impl import DataService
|
| - |
|
10 |
from shop2020.model.v1.catalog.impl.Convertors import to_t_item, \
|
| - |
|
11 |
to_t_item_inventory, to_t_warehouse, to_t_vendor_item_pricing, to_t_category, \
|
| - |
|
12 |
to_t_vendor, to_t_vendor_item_mapping, to_t_source_item_pricing, \
|
| - |
|
13 |
to_t_product_notification_request, to_t_product_notification_request_count
|
| - |
|
14 |
from shop2020.model.v1.catalog.impl.DataAcessors import add_item, add_warehouse, \
|
| - |
|
15 |
update_inventory, retire_warehouse, retire_item, change_item_status, get_item, \
|
| - |
|
16 |
get_all_items, get_all_items_by_status, get_item_availability_for_warehouse, \
|
| - |
|
17 |
get_item_availability_for_location, get_all_warehouses_by_status, get_Warehouse, \
|
| - |
|
18 |
get_all_warehouses_for_item, get_all_items_for_warehouse, update_item, \
|
| - |
|
19 |
start_item_on, close_session, add_vendor, retire_item_on, get_item_inventory_by_item_id, \
|
| - |
|
20 |
get_items_by_catalog_id, get_best_deals, get_best_deals_catalog_ids, \
|
| - |
|
21 |
get_best_deals_count, get_best_sellers, get_latest_arrivals, \
|
| - |
|
22 |
get_latest_arrivals_catalog_ids, get_latest_arrivals_count, is_active, \
|
| - |
|
23 |
get_best_sellers_catalog_ids, get_best_sellers_count, put_category_object, \
|
| - |
|
24 |
get_category_object, mark_item_as_content_complete, reserve_item_in_warehouse, \
|
| - |
|
25 |
reduce_reservation_count, generate_new_entity_id, get_all_item_pricing, \
|
| - |
|
26 |
add_vendor_pricing, get_item_pricing, get_category, get_all_categories, \
|
| - |
|
27 |
add_category, get_item_status_description, get_all_vendors, get_item_mappings, \
|
| - |
|
28 |
add_vendor_item_mapping, check_similar_item, change_risky_flag, \
|
| - |
|
29 |
get_items_for_mastersheet, get_risky_items, get_similar_items_catalog_ids, \
|
| - |
|
30 |
add_product_notification, send_product_notifications, update_inventory_history, \
|
| - |
|
31 |
get_all_brands_by_category, is_alive, get_item_pricing_by_source, \
|
| - |
|
32 |
add_source_item_pricing, get_all_sources, get_item_for_source, \
|
| - |
|
33 |
get_all_source_pricing, get_item_count_by_status, search_items, \
|
| - |
|
34 |
get_search_result_count, get_pending_orders_inventory, get_product_notifications, \
|
| - |
|
35 |
get_product_notification_request_count, add_inventory, \
|
| - |
|
36 |
get_all_similar_items_catalog_ids, add_similar_item_catalog_id, \
|
| - |
|
37 |
delete_similar_item_catalog_id, add_authorization_log_for_item, \
|
| - |
|
38 |
get_thrift_item_list, get_all_brands, add_bad_inventory, \
|
| - |
|
39 |
mark_missed_inventory_updates_as_processed, \
|
| - |
|
40 |
get_item_keys_to_be_processed, reset_availability
|
| - |
|
41 |
from shop2020.model.v1.catalog.impl.DataService import Warehouse, \
|
| - |
|
42 |
MissedInventoryUpdate
|
| - |
|
43 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status, \
|
| - |
|
44 |
InventoryServiceException, AvailableAndReservedStock, WarehouseType, InventoryType
|
| 43 |
from shop2020.thriftpy.purchase.ttypes import PurchaseOrder, LineItem
|
45 |
from shop2020.thriftpy.purchase.ttypes import PurchaseOrder, LineItem
|
| 44 |
|
- |
|
| 45 |
import xlrd
|
46 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
| 46 |
import datetime
|
47 |
import datetime
|
| - |
|
48 |
import xlrd
|
| - |
|
49 |
|
| 47 |
|
50 |
|
| 48 |
|
51 |
|
| 49 |
class InventoryServiceHandler:
|
52 |
class InventoryServiceHandler:
|
| 50 |
'''
|
53 |
'''
|
| 51 |
classdocs
|
54 |
classdocs
|
| Line 128... |
Line 131... |
| 128 |
log_entry(self, "add Inventory called")
|
131 |
log_entry(self, "add Inventory called")
|
| 129 |
try:
|
132 |
try:
|
| 130 |
return add_inventory(itemId, warehouseId, quantity)
|
133 |
return add_inventory(itemId, warehouseId, quantity)
|
| 131 |
finally:
|
134 |
finally:
|
| 132 |
close_session()
|
135 |
close_session()
|
| 133 |
|
- |
|
| 134 |
|
136 |
|
| 135 |
def isActive(self, item_id):
|
137 |
def isActive(self, item_id):
|
| 136 |
"""
|
138 |
"""
|
| 137 |
Parameters:
|
139 |
Parameters:
|
| 138 |
- item_id
|
140 |
- item_id
|
| Line 461... |
Line 463... |
| 461 |
try:
|
463 |
try:
|
| 462 |
warehouse = get_Warehouse(warehouse_id)
|
464 |
warehouse = get_Warehouse(warehouse_id)
|
| 463 |
if warehouse:
|
465 |
if warehouse:
|
| 464 |
return to_t_warehouse(warehouse)
|
466 |
return to_t_warehouse(warehouse)
|
| 465 |
else:
|
467 |
else:
|
| 466 |
raise InventoryServiceException(101, "no such warehouse")
|
468 |
raise InventoryServiceException(101, "no such warehouse: " + str(warehouse_id))
|
| 467 |
finally:
|
469 |
finally:
|
| 468 |
close_session()
|
470 |
close_session()
|
| 469 |
|
471 |
|
| 470 |
def getAllWarehousesForItem(self, item_id):
|
472 |
def getAllWarehousesForItem(self, item_id):
|
| 471 |
"""
|
473 |
"""
|
| Line 1011... |
Line 1013... |
| 1011 |
"""
|
1013 |
"""
|
| 1012 |
try:
|
1014 |
try:
|
| 1013 |
return add_vendor(vendor)
|
1015 |
return add_vendor(vendor)
|
| 1014 |
finally:
|
1016 |
finally:
|
| 1015 |
close_session()
|
1017 |
close_session()
|
| 1016 |
|
- |
|
| 1017 |
def addWarehouseVendorMapping(self, warehouse_id, vendorId):
|
- |
|
| 1018 |
"""
|
- |
|
| 1019 |
map given warehouse id to given vendor id
|
- |
|
| 1020 |
"""
|
- |
|
| 1021 |
try:
|
- |
|
| 1022 |
return add_warehouse_vendor_mapping(warehouse_id, vendorId)
|
- |
|
| 1023 |
finally:
|
- |
|
| 1024 |
close_session()
|
- |
|
| 1025 |
|
- |
|
| 1026 |
def getVendorsForWarehouse(self, warehouse_id):
|
- |
|
| 1027 |
"""
|
- |
|
| 1028 |
get all vendors for the given warehouse_id
|
- |
|
| 1029 |
"""
|
- |
|
| 1030 |
try:
|
- |
|
| 1031 |
vendors = get_vendors_for_warehouse(warehouse_id)
|
- |
|
| 1032 |
ret_vendors = []
|
- |
|
| 1033 |
for vendor in vendors:
|
- |
|
| 1034 |
if vendor:
|
- |
|
| 1035 |
ret_vendors.append(to_t_vendor(vendor))
|
- |
|
| 1036 |
return ret_vendors
|
- |
|
| 1037 |
finally:
|
- |
|
| 1038 |
close_session()
|
- |
|
| 1039 |
|
- |
|
| 1040 |
def getWarehousesForVendor(self, vendorId):
|
- |
|
| 1041 |
'''
|
- |
|
| 1042 |
get all warehouses for the given vendorId
|
- |
|
| 1043 |
'''
|
- |
|
| 1044 |
try:
|
- |
|
| 1045 |
warehouses = get_warehouses_for_vendor(vendorId)
|
- |
|
| 1046 |
ret_warehouses = []
|
- |
|
| 1047 |
for warehouse in warehouses:
|
- |
|
| 1048 |
if warehouse:
|
- |
|
| 1049 |
ret_warehouses.append(to_t_warehouse(warehouse))
|
- |
|
| 1050 |
return ret_warehouses
|
- |
|
| 1051 |
finally:
|
- |
|
| 1052 |
close_session()
|
- |
|
| 1053 |
|
- |
|
| 1054 |
def processPurchaseOrder(self, filename, vendorId):
|
- |
|
| 1055 |
'''
|
- |
|
| 1056 |
create purchase order for given filename
|
- |
|
| 1057 |
'''
|
- |
|
| 1058 |
workbook = xlrd.open_workbook(filename)
|
- |
|
| 1059 |
sheet = workbook.sheet_by_index(0)
|
- |
|
| 1060 |
num_rows = sheet.nrows
|
- |
|
| 1061 |
if(sheet.ncols != 2):
|
- |
|
| 1062 |
raise InventoryServiceException(101, "Sheet format Invalid")
|
- |
|
| 1063 |
catalogClient = CatalogClient().get_client()
|
- |
|
| 1064 |
lineitems = []
|
- |
|
| 1065 |
purchaseOrder = PurchaseOrder()
|
- |
|
| 1066 |
purchaseOrder.supplierId = vendorId
|
- |
|
| 1067 |
purchaseOrder.warehouseId = 7
|
- |
|
| 1068 |
totalCost = 0
|
- |
|
| 1069 |
|
- |
|
| 1070 |
for rownum in range(1, num_rows):
|
- |
|
| 1071 |
itemId = sheet.row_values(rownum)[0]
|
- |
|
| 1072 |
quantity = sheet.row_values(rownum)[1]
|
- |
|
| 1073 |
|
- |
|
| 1074 |
lineitem = LineItem()
|
- |
|
| 1075 |
item = catalogClient.getItem(itemId)
|
- |
|
| 1076 |
lineitem.itemId = item.id
|
- |
|
| 1077 |
lineitem.productGroup = item.productGroup
|
- |
|
| 1078 |
lineitem.brand = item.brand
|
- |
|
| 1079 |
lineitem.modelNumber = item.modelNumber
|
- |
|
| 1080 |
lineitem.modelName = item.modelName
|
- |
|
| 1081 |
lineitem.color = item.color
|
- |
|
| 1082 |
lineitem.quantity = quantity
|
- |
|
| 1083 |
lineitem.unfulfilledQuantity = quantity
|
- |
|
| 1084 |
try:
|
- |
|
| 1085 |
item_pricing = catalogClient.getItemPricing(itemId, vendorId)
|
- |
|
| 1086 |
lineitem.unitPrice = item_pricing.transferPrice
|
- |
|
| 1087 |
except:
|
- |
|
| 1088 |
raise InventoryServiceException(101, "Unable to get pricing for " + item.brand + " " + item.modelName \
|
- |
|
| 1089 |
+ " " + item.modelNumber + "(Item id : " + str(int(itemId)) + \
|
- |
|
| 1090 |
")" + " for VendorId : " + str(vendorId))
|
- |
|
| 1091 |
lineitem.fulfilled = 0
|
- |
|
| 1092 |
|
- |
|
| 1093 |
totalCost += quantity*lineitem.unitPrice
|
- |
|
| 1094 |
|
- |
|
| 1095 |
lineitems.append(lineitem)
|
- |
|
| 1096 |
|
- |
|
| 1097 |
purchaseOrder.totalCost = totalCost
|
- |
|
| 1098 |
purchaseOrder.lineitems = lineitems
|
- |
|
| 1099 |
|
- |
|
| 1100 |
purchaseClient = PurchaseClient().get_client()
|
- |
|
| 1101 |
po_id = purchaseClient.createPurchaseOrder(purchaseOrder)
|
- |
|
| 1102 |
return "POId:" + str(po_id)
|
- |
|
| 1103 |
|
1018 |
|
| 1104 |
def addAuthorizationLog(self, itemId, username, reason):
|
1019 |
def addAuthorizationLog(self, itemId, username, reason):
|
| 1105 |
"""
|
1020 |
"""
|
| 1106 |
add a log to authorize table.
|
1021 |
add a log to authorize table.
|
| 1107 |
|
1022 |
|
| Line 1111... |
Line 1026... |
| 1111 |
try:
|
1026 |
try:
|
| 1112 |
return add_authorization_log_for_item(itemId, username, reason)
|
1027 |
return add_authorization_log_for_item(itemId, username, reason)
|
| 1113 |
finally:
|
1028 |
finally:
|
| 1114 |
close_session()
|
1029 |
close_session()
|
| 1115 |
|
1030 |
|
| - |
|
1031 |
def getWarehouses(self, warehouseType, inventoryType, vendorId, billingWarehouseId, shippingWarehouseId):
|
| - |
|
1032 |
"""
|
| - |
|
1033 |
This method returns all warehouses for a given warehosueType, inventoryType, vendor, billingWarehouse and shippingWarehouse.
|
| - |
|
1034 |
getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 3, 7, 7) would return ours warehouse with GOOD type inventory for vendor 1 for billing warehouse 7 and shipping warehouse 7
|
| - |
|
1035 |
getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 3, 7, 7) would return ours warehouse with GOOD type inventory for vendor 3 for billing warehouse 7 and shipping warehouse 7
|
| - |
|
1036 |
getWarehouses(null, null, 3, 7, 7) would return all type warehouses with all type inventory for vendor 3 for billing warehouse 7 and shipping warehouse 7
|
| - |
|
1037 |
getWarehouses(null, null, 0, 0, 7) would return all type warehouses with all type inventory for all vendors for all billing warehouses at shipping warehouse 7
|
| - |
|
1038 |
|
| - |
|
1039 |
Parameters:
|
| - |
|
1040 |
- warehouseType
|
| - |
|
1041 |
- inventoryType
|
| - |
|
1042 |
- vendorId
|
| - |
|
1043 |
- billingWarehouseId
|
| - |
|
1044 |
- shippingWarehouseId
|
| - |
|
1045 |
"""
|
| - |
|
1046 |
try:
|
| - |
|
1047 |
query = Warehouse.query
|
| - |
|
1048 |
if warehouseType is not None:
|
| - |
|
1049 |
query = query.filter_by(warehouseType = WarehouseType._VALUES_TO_NAMES[warehouseType])
|
| - |
|
1050 |
if inventoryType is not None:
|
| - |
|
1051 |
query = query.filter_by(inventoryType = InventoryType._VALUES_TO_NAMES[inventoryType])
|
| - |
|
1052 |
if vendorId:
|
| - |
|
1053 |
query = query.filter_by(vendor_id = vendorId)
|
| - |
|
1054 |
if billingWarehouseId:
|
| - |
|
1055 |
query = query.filter_by(billingWarehouseId = billingWarehouseId)
|
| - |
|
1056 |
if shippingWarehouseId:
|
| - |
|
1057 |
query = query.filter_by(shippingWarehouseId = shippingWarehouseId)
|
| - |
|
1058 |
|
| - |
|
1059 |
return [to_t_warehouse(w) for w in query.all()]
|
| - |
|
1060 |
finally:
|
| - |
|
1061 |
close_session()
|
| - |
|
1062 |
|
| 1116 |
def getItemKeysToBeProcessed(self, warehouseId):
|
1063 |
def getItemKeysToBeProcessed(self, warehouseId):
|
| 1117 |
"""
|
1064 |
"""
|
| 1118 |
Returns the list of item keys which need to be processed for a given warehouse.
|
1065 |
Returns the list of item keys which need to be processed for a given warehouse.
|
| 1119 |
This is currently used by Support application to send item keys whose inventory needs
|
1066 |
This is currently used by Support application to send item keys whose inventory needs
|
| 1120 |
to be updated from PLB
|
1067 |
to be updated from PLB
|
| Line 1154... |
Line 1101... |
| 1154 |
try:
|
1101 |
try:
|
| 1155 |
reset_availability(itemKey, vendorId, quantity, warehouseId)
|
1102 |
reset_availability(itemKey, vendorId, quantity, warehouseId)
|
| 1156 |
finally:
|
1103 |
finally:
|
| 1157 |
close_session()
|
1104 |
close_session()
|
| 1158 |
|
1105 |
|
| - |
|
1106 |
def getIgnoredItemKeys(self, ):
|
| - |
|
1107 |
"""
|
| - |
|
1108 |
Returns all the item key mappings that have been ignored until date. Value of map has the warehouse id
|
| - |
|
1109 |
and the timestamp from where alert was raised.
|
| - |
|
1110 |
"""
|
| - |
|
1111 |
try:
|
| - |
|
1112 |
itemKeysMap = {}
|
| - |
|
1113 |
for key in MissedInventoryUpdate.query.filter_by(isIgnored = 1).all():
|
| - |
|
1114 |
itemKeysMap[key.itemKey] = { key.warehouseId : to_java_date(key.timestamp) }
|
| - |
|
1115 |
|
| - |
|
1116 |
return itemKeysMap
|
| - |
|
1117 |
finally:
|
| - |
|
1118 |
close_session()
|
| - |
|
1119 |
|
| - |
|
1120 |
def addBadInventory(self, itemId, warehouseId, quantity):
|
| - |
|
1121 |
"""
|
| - |
|
1122 |
Add the BAD type inventory to existing stock.
|
| - |
|
1123 |
|
| - |
|
1124 |
Parameters:
|
| - |
|
1125 |
- itemId
|
| - |
|
1126 |
- warehouseId
|
| - |
|
1127 |
- quantity
|
| - |
|
1128 |
"""
|
| - |
|
1129 |
try:
|
| - |
|
1130 |
add_bad_inventory(itemId, warehouseId, quantity)
|
| - |
|
1131 |
finally:
|
| - |
|
1132 |
close_session()
|
| - |
|
1133 |
|
| - |
|
1134 |
|
| 1159 |
def is_valid(item):
|
1135 |
def is_valid(item):
|
| 1160 |
if item.status in [status.ACTIVE, status.PAUSED, status.PAUSED_BY_RISK]:
|
1136 |
if item.status in [status.ACTIVE, status.PAUSED, status.PAUSED_BY_RISK]:
|
| 1161 |
if item.startDate:
|
1137 |
if item.startDate:
|
| 1162 |
return not(datetime.datetime.now() < item.startDate or item.sellingPrice == 0)
|
1138 |
return not(datetime.datetime.now() < item.startDate or item.sellingPrice == 0)
|
| 1163 |
else:
|
1139 |
else:
|