| Line 33... |
Line 33... |
| 33 |
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
|
33 |
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
|
| 34 |
from sqlalchemy.sql import or_
|
34 |
from sqlalchemy.sql import or_
|
| 35 |
from sqlalchemy.sql.expression import and_, func, distinct, desc
|
35 |
from sqlalchemy.sql.expression import and_, func, distinct, desc
|
| 36 |
from sqlalchemy.sql.functions import count
|
36 |
from sqlalchemy.sql.functions import count
|
| 37 |
import calendar
|
37 |
import calendar
|
| - |
|
38 |
from collections import namedtuple
|
| 38 |
import datetime
|
39 |
import datetime
|
| 39 |
import json
|
40 |
import json
|
| 40 |
import math
|
41 |
import math
|
| 41 |
import sys
|
42 |
import sys
|
| 42 |
import threading
|
43 |
import threading
|
| Line 49... |
Line 50... |
| 49 |
193: [5839] }
|
50 |
193: [5839] }
|
| 50 |
|
51 |
|
| 51 |
pincodePricingServiceabilityMap = {}
|
52 |
pincodePricingServiceabilityMap = {}
|
| 52 |
warehouseMap = {}
|
53 |
warehouseMap = {}
|
| 53 |
|
54 |
|
| - |
|
55 |
WarehouseInfo = namedtuple('WarehouseInfo', [
|
| - |
|
56 |
'id', 'vendor_id', 'inventoryType', 'warehouseType',
|
| - |
|
57 |
'billingWarehouseId', 'transferDelayInHours', 'state_id',
|
| - |
|
58 |
'logisticsLocation', 'shippingWarehouseId', 'isAvailabilityMonitored'])
|
| - |
|
59 |
_warehouse_cache = {}
|
| - |
|
60 |
|
| 54 |
OOS_CALCULATION_TIME = 23
|
61 |
OOS_CALCULATION_TIME = 23
|
| 55 |
|
62 |
|
| 56 |
# right now these warehouse ids are ignored.
|
63 |
# right now these warehouse ids are ignored.
|
| 57 |
# physicalWarehouseIdsForFofo = [7678, 7681]
|
64 |
# physicalWarehouseIdsForFofo = [7678, 7681]
|
| 58 |
physicalWarehouseIdsForFofo = []
|
65 |
physicalWarehouseIdsForFofo = []
|
| Line 80... |
Line 87... |
| 80 |
#UK
|
87 |
#UK
|
| 81 |
11: 9213
|
88 |
11: 9213
|
| 82 |
}
|
89 |
}
|
| 83 |
|
90 |
|
| 84 |
|
91 |
|
| - |
|
92 |
def _load_warehouse_cache():
|
| - |
|
93 |
global _warehouse_cache
|
| - |
|
94 |
rows = metadata.bind.execute(
|
| - |
|
95 |
"SELECT id, vendor_id, inventoryType, warehouseType, "
|
| - |
|
96 |
"billingWarehouseId, transferDelayInHours, state_id, "
|
| - |
|
97 |
"logisticsLocation, shippingWarehouseId, isAvailabilityMonitored "
|
| - |
|
98 |
"FROM warehouse"
|
| - |
|
99 |
)
|
| - |
|
100 |
_warehouse_cache = {}
|
| - |
|
101 |
for row in rows:
|
| - |
|
102 |
wh = WarehouseInfo(*row)
|
| - |
|
103 |
_warehouse_cache[wh.id] = wh
|
| - |
|
104 |
print "Loaded %d warehouses into cache" % len(_warehouse_cache)
|
| - |
|
105 |
|
| - |
|
106 |
|
| 85 |
def initialize(dbname='inventory', db_hostname="localhost"):
|
107 |
def initialize(dbname='inventory', db_hostname="localhost"):
|
| 86 |
DataService.initialize(dbname, db_hostname)
|
108 |
DataService.initialize(dbname, db_hostname)
|
| - |
|
109 |
_load_warehouse_cache()
|
| 87 |
__populateWarehouseMap()
|
110 |
__populateWarehouseMap()
|
| 88 |
|
111 |
|
| 89 |
|
112 |
|
| 90 |
@memoized(3600)
|
113 |
@memoized(3600)
|
| 91 |
def __populateWarehouseMap():
|
114 |
def __populateWarehouseMap():
|
| Line 147... |
Line 170... |
| 147 |
ds_warehouse.state = get_state(warehouse.stateId)
|
170 |
ds_warehouse.state = get_state(warehouse.stateId)
|
| 148 |
ds_warehouse.warehouseType = WarehouseType._VALUES_TO_NAMES[warehouse.warehouseType]
|
171 |
ds_warehouse.warehouseType = WarehouseType._VALUES_TO_NAMES[warehouse.warehouseType]
|
| 149 |
if warehouse.vendorString:
|
172 |
if warehouse.vendorString:
|
| 150 |
ds_warehouse.vendorString = warehouse.vendorString
|
173 |
ds_warehouse.vendorString = warehouse.vendorString
|
| 151 |
session.commit()
|
174 |
session.commit()
|
| - |
|
175 |
_load_warehouse_cache()
|
| 152 |
return ds_warehouse.id
|
176 |
return ds_warehouse.id
|
| 153 |
|
177 |
|
| 154 |
|
178 |
|
| 155 |
def get_ignored_items(warehouse_id):
|
179 |
def get_ignored_items(warehouse_id):
|
| 156 |
Ignored_inventory_items = IgnoredInventoryUpdateItems.query.filter_by(warehouse_id=warehouse_id).all()
|
180 |
Ignored_inventory_items = IgnoredInventoryUpdateItems.query.filter_by(warehouse_id=warehouse_id).all()
|
| Line 339... |
Line 363... |
| 339 |
except:
|
363 |
except:
|
| 340 |
return 0
|
364 |
return 0
|
| 341 |
|
365 |
|
| 342 |
|
366 |
|
| 343 |
def get_item_availability_for_our_warehouses(item_ids):
|
367 |
def get_item_availability_for_our_warehouses(item_ids):
|
| 344 |
our_warehouses = Warehouse.query.filter_by(warehouseType='OURS', inventoryType='GOOD').all()
|
368 |
warehouse_ids = [wh.id for wh in _warehouse_cache.values() if wh.warehouseType == 'OURS' and wh.inventoryType == 'GOOD']
|
| 345 |
our_thirdparty_warehouses = Warehouse.query.filter_by(warehouseType='OURS_THIRDPARTY').all()
|
- |
|
| 346 |
warehouse_ids = []
|
- |
|
| 347 |
for warehouse in our_warehouses:
|
- |
|
| 348 |
warehouse_ids.append(warehouse.id)
|
- |
|
| 349 |
# for warehouse in our_thirdparty_warehouses :
|
- |
|
| 350 |
# warehouse_ids.append(warehouse.id)
|
- |
|
| 351 |
|
- |
|
| 352 |
availability_map = dict()
|
369 |
availability_map = dict()
|
| 353 |
|
370 |
|
| 354 |
try:
|
371 |
try:
|
| 355 |
for item_id in item_ids:
|
372 |
for item_id in item_ids:
|
| 356 |
total_availability = 0
|
373 |
total_availability = 0
|
| Line 899... |
Line 916... |
| 899 |
|
916 |
|
| 900 |
warehouses = {}
|
917 |
warehouses = {}
|
| 901 |
ourGoodWarehouses = {}
|
918 |
ourGoodWarehouses = {}
|
| 902 |
thirdpartyWarehouses = {}
|
919 |
thirdpartyWarehouses = {}
|
| 903 |
preferredThirdpartyWarehouses = {}
|
920 |
preferredThirdpartyWarehouses = {}
|
| 904 |
for warehouse in Warehouse.query.all():
|
921 |
for warehouse in _warehouse_cache.values():
|
| 905 |
if (warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.BAD] or warehouse.warehouseType == WarehouseType._VALUES_TO_NAMES[WarehouseType.OURS_THIRDPARTY]):
|
922 |
if (warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.BAD] or warehouse.warehouseType == WarehouseType._VALUES_TO_NAMES[WarehouseType.OURS_THIRDPARTY]):
|
| 906 |
continue
|
923 |
continue
|
| 907 |
warehouses[warehouse.id] = warehouse
|
924 |
warehouses[warehouse.id] = warehouse
|
| 908 |
if warehouse.warehouseType == WarehouseType._VALUES_TO_NAMES[WarehouseType.OURS]:
|
925 |
if warehouse.warehouseType == WarehouseType._VALUES_TO_NAMES[WarehouseType.OURS]:
|
| 909 |
if warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.GOOD]:
|
926 |
if warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.GOOD]:
|
| Line 933... |
Line 950... |
| 933 |
warehouse = warehouses[warehouse_retid]
|
950 |
warehouse = warehouses[warehouse_retid]
|
| 934 |
billingWarehouseId = warehouse.billingWarehouseId
|
951 |
billingWarehouseId = warehouse.billingWarehouseId
|
| 935 |
|
952 |
|
| 936 |
# Fetching billing warehouse of a Good billable warehouse corresponding to the virtual one
|
953 |
# Fetching billing warehouse of a Good billable warehouse corresponding to the virtual one
|
| 937 |
if not warehouse.billingWarehouseId:
|
954 |
if not warehouse.billingWarehouseId:
|
| - |
|
955 |
for w in _warehouse_cache.values():
|
| 938 |
for w in Warehouse.query.filter_by(vendor_id=warehouse.vendor_id, inventoryType=InventoryType._VALUES_TO_NAMES[InventoryType.GOOD], state_id=warehouse.state_id).all():
|
956 |
if w.vendor_id == warehouse.vendor_id and w.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.GOOD] and w.state_id == warehouse.state_id and w.billingWarehouseId:
|
| 939 |
if w.billingWarehouseId:
|
- |
|
| 940 |
billingWarehouseId = w.billingWarehouseId
|
957 |
billingWarehouseId = w.billingWarehouseId
|
| 941 |
break
|
958 |
break
|
| 942 |
|
959 |
|
| 943 |
expectedDelay = item.expectedDelay
|
960 |
expectedDelay = item.expectedDelay
|
| 944 |
if expectedDelay is None:
|
961 |
if expectedDelay is None:
|
| Line 1267... |
Line 1284... |
| 1267 |
def get_pending_orders_inventory(vendor_id=1):
|
1284 |
def get_pending_orders_inventory(vendor_id=1):
|
| 1268 |
"""
|
1285 |
"""
|
| 1269 |
Returns a list of inventory stock for items for which there are pending orders.
|
1286 |
Returns a list of inventory stock for items for which there are pending orders.
|
| 1270 |
"""
|
1287 |
"""
|
| 1271 |
|
1288 |
|
| 1272 |
warehouse_ids = [warehouse.id for warehouse in Warehouse.query.filter_by(vendor_id=vendor_id)]
|
1289 |
warehouse_ids = [wh.id for wh in _warehouse_cache.values() if wh.vendor_id == vendor_id]
|
| 1273 |
pending_items_inventory = []
|
1290 |
pending_items_inventory = []
|
| 1274 |
if warehouse_ids:
|
1291 |
if warehouse_ids:
|
| 1275 |
pending_items_inventory = session.query(CurrentInventorySnapshot.item_id, func.sum(CurrentInventorySnapshot.availability), func.sum(CurrentInventorySnapshot.reserved)).filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).group_by(CurrentInventorySnapshot.item_id).having(func.sum(CurrentInventorySnapshot.reserved) > 0).all()
|
1292 |
pending_items_inventory = session.query(CurrentInventorySnapshot.item_id, func.sum(CurrentInventorySnapshot.availability), func.sum(CurrentInventorySnapshot.reserved)).filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).group_by(CurrentInventorySnapshot.item_id).having(func.sum(CurrentInventorySnapshot.reserved) > 0).all()
|
| 1276 |
return pending_items_inventory
|
1293 |
return pending_items_inventory
|
| 1277 |
|
1294 |
|
| Line 1280... |
Line 1297... |
| 1280 |
"""
|
1297 |
"""
|
| 1281 |
Returns a list of inventory Availability and Reserved Count for items which either have real inventory
|
1298 |
Returns a list of inventory Availability and Reserved Count for items which either have real inventory
|
| 1282 |
or have pending orders.
|
1299 |
or have pending orders.
|
| 1283 |
"""
|
1300 |
"""
|
| 1284 |
|
1301 |
|
| 1285 |
warehouse_ids = [warehouse.id for warehouse in Warehouse.query.filter(Warehouse.isAvailabilityMonitored == 1).filter(or_(Warehouse.inventoryType == 'GOOD', Warehouse.warehouseType == 'OURS'))]
|
1302 |
warehouse_ids = [wh.id for wh in _warehouse_cache.values() if wh.isAvailabilityMonitored and (wh.inventoryType == 'GOOD' or wh.warehouseType == 'OURS')]
|
| 1286 |
items_inventory = []
|
1303 |
items_inventory = []
|
| 1287 |
reserved_items_inventory = []
|
1304 |
reserved_items_inventory = []
|
| 1288 |
available_items_inventory = []
|
1305 |
available_items_inventory = []
|
| 1289 |
if warehouse_ids:
|
1306 |
if warehouse_ids:
|
| 1290 |
reserved_items_inventory = session.query(CurrentInventorySnapshot.item_id, func.sum(CurrentInventorySnapshot.availability), func.sum(CurrentInventorySnapshot.reserved)).filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).group_by(CurrentInventorySnapshot.item_id).having(func.sum(CurrentInventorySnapshot.reserved) > 0).all()
|
1307 |
reserved_items_inventory = session.query(CurrentInventorySnapshot.item_id, func.sum(CurrentInventorySnapshot.availability), func.sum(CurrentInventorySnapshot.reserved)).filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).group_by(CurrentInventorySnapshot.item_id).having(func.sum(CurrentInventorySnapshot.reserved) > 0).all()
|
| Line 1363... |
Line 1380... |
| 1363 |
session.commit()
|
1380 |
session.commit()
|
| 1364 |
|
1381 |
|
| 1365 |
|
1382 |
|
| 1366 |
def get_our_warehouse_id_for_vendor(vendor_id, billing_warehouse_id):
|
1383 |
def get_our_warehouse_id_for_vendor(vendor_id, billing_warehouse_id):
|
| 1367 |
try:
|
1384 |
try:
|
| - |
|
1385 |
for wh in _warehouse_cache.values():
|
| 1368 |
warehouse = Warehouse.query.filter_by(vendor_id=vendor_id, warehouseType='OURS', inventoryType='GOOD', billingWarehouseId=billing_warehouse_id).first()
|
1386 |
if wh.vendor_id == vendor_id and wh.warehouseType == 'OURS' and wh.inventoryType == 'GOOD' and wh.billingWarehouseId == billing_warehouse_id:
|
| 1369 |
return warehouse.id
|
1387 |
return wh.id
|
| - |
|
1388 |
raise InventoryServiceException(101, 'No our warehouse found for vendorId: ' + str(vendor_id))
|
| 1370 |
except Exception as e:
|
1389 |
except Exception as e:
|
| 1371 |
print e;
|
1390 |
print e;
|
| 1372 |
raise InventoryServiceException(101, 'No our warehouse found for vendorId: ' + str(vendor_id))
|
1391 |
raise InventoryServiceException(101, 'No our warehouse found for vendorId: ' + str(vendor_id))
|
| 1373 |
|
1392 |
|
| 1374 |
|
1393 |
|
| Line 1380... |
Line 1399... |
| 1380 |
print ex
|
1399 |
print ex
|
| 1381 |
|
1400 |
|
| 1382 |
|
1401 |
|
| 1383 |
def get_shipping_locations():
|
1402 |
def get_shipping_locations():
|
| 1384 |
shippingLocationIds = {}
|
1403 |
shippingLocationIds = {}
|
| 1385 |
warehouses = Warehouse.query.all()
|
- |
|
| 1386 |
for warehouse in warehouses:
|
1404 |
for warehouse in _warehouse_cache.values():
|
| 1387 |
if warehouse.shippingWarehouseId:
|
1405 |
if warehouse.shippingWarehouseId:
|
| 1388 |
shippingLocationIds[warehouse.shippingWarehouseId] = 1
|
1406 |
shippingLocationIds[warehouse.shippingWarehouseId] = 1
|
| 1389 |
|
1407 |
|
| 1390 |
shippingLocations = []
|
1408 |
shippingLocations = []
|
| 1391 |
for shippingLocationId in shippingLocationIds:
|
1409 |
for shippingLocationId in shippingLocationIds:
|
| Line 1440... |
Line 1458... |
| 1440 |
return client.getItem(item_id)
|
1458 |
return client.getItem(item_id)
|
| 1441 |
|
1459 |
|
| 1442 |
|
1460 |
|
| 1443 |
def get_monitored_warehouses_for_vendors(vendorIds):
|
1461 |
def get_monitored_warehouses_for_vendors(vendorIds):
|
| 1444 |
w = []
|
1462 |
w = []
|
| 1445 |
for wh in Warehouse.query.filter_by(isAvailabilityMonitored=1).all():
|
1463 |
for wh in _warehouse_cache.values():
|
| 1446 |
if wh.vendor.id in (vendorIds):
|
1464 |
if wh.isAvailabilityMonitored and wh.vendor_id in (vendorIds):
|
| 1447 |
w.append(to_t_warehouse(wh).id)
|
1465 |
w.append(wh.id)
|
| 1448 |
return w
|
1466 |
return w
|
| 1449 |
|
1467 |
|
| 1450 |
|
1468 |
|
| 1451 |
def get_ignored_warehouseids_and_itemids():
|
1469 |
def get_ignored_warehouseids_and_itemids():
|
| 1452 |
iw = []
|
1470 |
iw = []
|