Subversion Repositories SmartDukaan

Rev

Rev 35718 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5944 mandeep.dh 1
'''
2
Created on 23-Mar-2010
3
 
4
@author: ashish
5
'''
6
from elixir import *
7
from functools import partial
6531 vikram.rag 8
from shop2020.clients.CatalogClient import CatalogClient
19413 amit.gupta 9
from shop2020.clients.LogisticsClient import LogisticsClient
5944 mandeep.dh 10
from shop2020.clients.TransactionClient import TransactionClient
11
from shop2020.model.v1.inventory.impl import DataService
6531 vikram.rag 12
from shop2020.model.v1.inventory.impl.Convertors import to_t_warehouse, \
19413 amit.gupta 13
    to_t_itemidwarehouseid, to_t_state, to_t_item_location_availability
5944 mandeep.dh 14
from shop2020.model.v1.inventory.impl.DataService import Warehouse, \
15
    ItemInventoryHistory, CurrentInventorySnapshot, VendorItemPricing, \
16
    VendorItemMapping, Vendor, MissedInventoryUpdate, BadInventorySnapshot, \
19413 amit.gupta 17
    VendorHolidays, ItemAvailabilityCache, CurrentReservationSnapshot, \
18
    IgnoredInventoryUpdateItems, ItemStockPurchaseParams, OOSStatus, \
19
    AmazonInventorySnapshot, StateMaster, HoldInventoryDetail, \
20
    AmazonFbaInventorySnapshot, SnapdealInventorySnapshot, FlipkartInventorySnapshot, \
21
    SnapdealStockAtEOD, FlipkartStockAtEOD, StockWeightedNlcInfo, \
22
    ItemLocationAvailabilityCache
23446 amit.gupta 23
from shop2020.thriftpy.logistics.ttypes import LocationInfo, PickUpType
6531 vikram.rag 24
from shop2020.thriftpy.model.v1.inventory.ttypes import \
23446 amit.gupta 25
    InventoryServiceException, HolidayType, InventoryType, WarehouseType, \
19413 amit.gupta 26
    ItemLocationAvailability, ItemPincodeAvailability
23316 amit.gupta 27
from shop2020.thriftpy.model.v1.order.ttypes import AlertType, TransactionStatus
6531 vikram.rag 28
from shop2020.thriftpy.purchase.ttypes import PurchaseServiceException
5944 mandeep.dh 29
from shop2020.utils import EmailAttachmentSender
30
from shop2020.utils.EmailAttachmentSender import mail
6821 amar.kumar 31
from shop2020.utils.Utils import to_py_date, to_java_date
23446 amit.gupta 32
from shop2020.utils.caching.SimpleCaching import memoized
5944 mandeep.dh 33
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
7410 amar.kumar 34
from sqlalchemy.sql import or_
12363 kshitij.so 35
from sqlalchemy.sql.expression import and_, func, distinct, desc
6531 vikram.rag 36
from sqlalchemy.sql.functions import count
5944 mandeep.dh 37
import calendar
35732 amit 38
from collections import namedtuple
5944 mandeep.dh 39
import datetime
23446 amit.gupta 40
import json
19413 amit.gupta 41
import math
5944 mandeep.dh 42
import sys
43
import threading
28621 amit.gupta 44
from os.path import stat
5944 mandeep.dh 45
 
28621 amit.gupta 46
to_addresses = ["khushal.bhatia@shop2020.in", "chaitnaya.vats@shop2020.in", "chandan.kumar@shop2020.in", 'manoj.kumar@shop2020.in']
6029 rajveer 47
mail_user = "cnc.center@shop2020.in"
48
mail_password = "5h0p2o2o"
28621 amit.gupta 49
skippedItems = { 175: [27, 2160, 2175, 2163, 2158, 7128, 26, 2154],
50
                 193: [5839] }
5944 mandeep.dh 51
 
19413 amit.gupta 52
pincodePricingServiceabilityMap = {}
53
warehouseMap = {}
54
 
35732 amit 55
WarehouseInfo = namedtuple('WarehouseInfo', [
56
    'id', 'vendor_id', 'inventoryType', 'warehouseType',
57
    'billingWarehouseId', 'transferDelayInHours', 'state_id',
58
    'logisticsLocation', 'shippingWarehouseId', 'isAvailabilityMonitored'])
59
_warehouse_cache = {}
60
 
6821 amar.kumar 61
OOS_CALCULATION_TIME = 23
22991 amit.gupta 62
 
28621 amit.gupta 63
# right now these warehouse ids are ignored.
64
# physicalWarehouseIdsForFofo = [7678, 7681]
23029 amit.gupta 65
physicalWarehouseIdsForFofo = []
25468 amit.gupta 66
vendorWarehouseIdsForFofo = []
6498 vikram.rag 67
 
19413 amit.gupta 68
last_date = datetime.date.today()
28621 amit.gupta 69
adjusted_dates = {}
19413 amit.gupta 70
 
23446 amit.gupta 71
partnerMap = {}
23442 amit.gupta 72
 
23937 amit.gupta 73
default_virtual_vendor = 290 
23446 amit.gupta 74
virtualVendorWarehouses = {}
23475 amit.gupta 75
billingVendorWarehouses = {}
28621 amit.gupta 76
stateIdWarehouseIdMapWareMap = {
28628 amit.gupta 77
    #Delhi
28621 amit.gupta 78
    0: 7573,
28628 amit.gupta 79
    #HR
28630 amit.gupta 80
    3: 7720,
28629 amit.gupta 81
    #UP - UP(E)
28621 amit.gupta 82
    12: 8468,
28628 amit.gupta 83
    #PB
28621 amit.gupta 84
    9: 8889,
28628 amit.gupta 85
    #RJ
31015 amit.gupta 86
    4: 8947,
87
    #UK
31115 amit.gupta 88
    11: 9213
28621 amit.gupta 89
}
23446 amit.gupta 90
 
28621 amit.gupta 91
 
35732 amit 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
 
5944 mandeep.dh 107
def initialize(dbname='inventory', db_hostname="localhost"):
108
    DataService.initialize(dbname, db_hostname)
35732 amit 109
    _load_warehouse_cache()
19413 amit.gupta 110
    __populateWarehouseMap()
23446 amit.gupta 111
 
28621 amit.gupta 112
 
23446 amit.gupta 113
@memoized(3600)   
19413 amit.gupta 114
def __populateWarehouseMap():
28621 amit.gupta 115
    for stateId, warehouseId in stateIdWarehouseIdMapWareMap.iteritems():
116
        billingVendorWarehouses[stateId] = warehouseId
117
        warehouse = Warehouse.query.filter(Warehouse.warehouseType.in_(['THIRD_PARTY'])).filter(Warehouse.inventoryType == 'GOOD').filter(Warehouse.state_id == stateId).first()
118
        if warehouse is None:
119
            warehouse = Warehouse.query.filter(Warehouse.warehouseType.in_(['THIRD_PARTY'])).filter(Warehouse.inventoryType == 'GOOD').filter(Warehouse.state_id == 0).first()
120
        virtualVendorWarehouses[stateId] = warehouse
28647 amit.gupta 121
        warehouse.expunge()
28621 amit.gupta 122
 
19413 amit.gupta 123
 
5944 mandeep.dh 124
def get_Warehouse(warehouse_id):
125
    return Warehouse.get_by(id=warehouse_id)
126
 
28621 amit.gupta 127
 
5944 mandeep.dh 128
def get_vendor(vendorId):
129
    return Vendor.get_by(id=vendorId)
130
 
28621 amit.gupta 131
 
7410 amar.kumar 132
def get_state(stateId):
133
    return StateMaster.get_by(id=stateId)
134
 
28621 amit.gupta 135
 
5944 mandeep.dh 136
def get_all_warehouses_by_status(status):
137
    return Warehouse.query.all()
138
 
28621 amit.gupta 139
 
5944 mandeep.dh 140
def get_all_items_for_warehouse(warehouse_id):
141
    warehouse = get_Warehouse(warehouse_id)
142
    if not warehouse:
143
        raise InventoryServiceException(108, "bad warehouse")
144
    return warehouse.all_items
145
 
28621 amit.gupta 146
 
5944 mandeep.dh 147
def add_warehouse(warehouse):
148
    if not warehouse:
149
        raise InventoryServiceException(108, "Bad warehouse")
150
    if get_Warehouse(warehouse.id):
28621 amit.gupta 151
        # warehouse is already present.
5944 mandeep.dh 152
        raise InventoryServiceException(101, "Warehouse already present")
153
 
154
    ds_warehouse = Warehouse()
155
    ds_warehouse.location = warehouse.location
156
    ds_warehouse.status = 3
157
    ds_warehouse.addedOn = datetime.datetime.now()
158
    ds_warehouse.lastCheckedOn = datetime.datetime.now()
159
    ds_warehouse.tinNumber = warehouse.tinNumber
160
    ds_warehouse.pincode = warehouse.pincode
161
    ds_warehouse.billingType = warehouse.billingType
162
    ds_warehouse.billingWarehouseId = warehouse.billingWarehouseId
163
    ds_warehouse.displayName = warehouse.displayName
164
    ds_warehouse.inventoryType = InventoryType._VALUES_TO_NAMES[warehouse.inventoryType]
165
    ds_warehouse.isAvailabilityMonitored = warehouse.isAvailabilityMonitored
166
    ds_warehouse.logisticsLocation = warehouse.logisticsLocation
167
    ds_warehouse.shippingWarehouseId = warehouse.shippingWarehouseId
168
    ds_warehouse.transferDelayInHours = warehouse.transferDelayInHours
169
    ds_warehouse.vendor = get_vendor(warehouse.vendor.id)
7410 amar.kumar 170
    ds_warehouse.state = get_state(warehouse.stateId)
5944 mandeep.dh 171
    ds_warehouse.warehouseType = WarehouseType._VALUES_TO_NAMES[warehouse.warehouseType]    
172
    if warehouse.vendorString:
173
        ds_warehouse.vendorString = warehouse.vendorString
174
    session.commit()
35732 amit 175
    _load_warehouse_cache()
5944 mandeep.dh 176
    return ds_warehouse.id
177
 
28621 amit.gupta 178
 
6498 vikram.rag 179
def get_ignored_items(warehouse_id): 
6531 vikram.rag 180
    Ignored_inventory_items = IgnoredInventoryUpdateItems.query.filter_by(warehouse_id=warehouse_id).all()
6498 vikram.rag 181
    negativeItems = []
182
    for Ignored_inventory_item in Ignored_inventory_items:
183
        try:
184
            item_id = Ignored_inventory_item.item_id
185
            negativeItems.append(item_id)
186
        except:
187
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
188
    return negativeItems
189
 
28621 amit.gupta 190
 
22630 amit.gupta 191
def get_ignored_warehouses(item_id):
192
    warehouses = []
28621 amit.gupta 193
    # No item availability cache for Hotspot Specific Store
194
    # No virtual is allowed
6539 amit.gupta 195
    Ignored_inventory_items = IgnoredInventoryUpdateItems.query.filter_by(item_id=item_id).all()
6510 rajveer 196
    for Ignored_inventory_item in Ignored_inventory_items:
197
        warehouses.append(Ignored_inventory_item.warehouse_id)
25459 amit.gupta 198
    return warehouses
6510 rajveer 199
 
28621 amit.gupta 200
 
5944 mandeep.dh 201
def update_inventory_history(warehouse_id, timestamp, availability):
202
    warehouse = get_Warehouse(warehouse_id)
203
    if not warehouse:
204
        raise InventoryServiceException(107, "Warehouse? Where?")
205
    vendor = warehouse.vendor
206
    time = datetime.datetime.now()
207
    for item_key, quantity in availability.iteritems():
208
        try:
209
            vendor_item_mapping = VendorItemMapping.query.filter_by(vendor=vendor, item_key=item_key).one();
5960 mandeep.dh 210
            item_id = vendor_item_mapping.item_id
5944 mandeep.dh 211
        except:
6531 vikram.rag 212
            continue  
5944 mandeep.dh 213
        try:
6510 rajveer 214
            item_inventory_history = ItemInventoryHistory()
215
            item_inventory_history.warehouse = warehouse
216
            item_inventory_history.item_id = item_id
217
            item_inventory_history.timestamp = time
218
            item_inventory_history.availability = quantity
5944 mandeep.dh 219
        except:
220
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
221
    session.commit()
28621 amit.gupta 222
 
5944 mandeep.dh 223
 
224
def update_inventory(warehouse_id, timestamp, availability):
225
    warehouse = get_Warehouse(warehouse_id)
226
    if not warehouse:
227
        raise InventoryServiceException(107, "Warehouse? Where?")
6510 rajveer 228
 
5944 mandeep.dh 229
    time = datetime.datetime.now()
230
    warehouse.lastCheckedOn = time
231
    warehouse.vendorString = timestamp
232
    vendor = warehouse.vendor
233
    item_ids = []
234
    for item_key, quantity in availability.iteritems():
235
        try:
236
            vendor_item_mapping = VendorItemMapping.query.filter_by(vendor=vendor, item_key=item_key).one();
237
            item_id = vendor_item_mapping.item_id
6510 rajveer 238
            item_ids.append(item_id)
5944 mandeep.dh 239
        except:
240
            print 'Skipping update for ' + item_key + ' quantity ' + str(quantity) + ' warehouse id: ' + str(warehouse_id)
241
            __send_mail_for_missing_key(item_key, quantity, warehouse_id)
242
            continue
243
        try:
244
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=item_id, warehouse=warehouse)
245
            if not current_inventory_snapshot:
246
                current_inventory_snapshot = CurrentInventorySnapshot()
247
                current_inventory_snapshot.item_id = item_id
248
                current_inventory_snapshot.warehouse = warehouse
249
                current_inventory_snapshot.availability = 0
250
                current_inventory_snapshot.reserved = 0
8204 amar.kumar 251
                current_inventory_snapshot.held = 0
5944 mandeep.dh 252
            # added the difference in the current inventory    
253
            current_inventory_snapshot.availability = current_inventory_snapshot.availability + quantity
254
            item = __get_item_from_master(item_id)
255
            try:
256
                if quantity > 0 and __get_item_reserved(item_id) > 0:
257
                    cl = TransactionClient().get_client()
28621 amit.gupta 258
                    # FIXME hardcoding for warehouse id 
259
                    cl.addAlert(AlertType.NEW_INVENTORY_ALERT, 5, "Inventory received for item " + item.brand + " " + item.modelName + " " + item.modelNumber + " " + item.color)
5944 mandeep.dh 260
            except:
261
                print "Not able to raise alert for incoming inventory" 
262
            if current_inventory_snapshot.availability < 0:
263
                __send_alert_for_negative_availability(item, current_inventory_snapshot.availability, warehouse)
264
        except:
265
            print "Some unforeseen error while updating inventory:", sys.exc_info()[0]
266
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
267
    session.commit()
268
 
28621 amit.gupta 269
    # **Update item availability cache**#
5944 mandeep.dh 270
    for item_id in item_ids:
5978 rajveer 271
        clear_item_availability_cache(item_id)
5944 mandeep.dh 272
 
28621 amit.gupta 273
 
5944 mandeep.dh 274
def __send_alert_for_negative_reserved(item, reserved, warehouse):
275
    itemName = " ".join([str(item.id), str(item.brand), str(item.modelName), str(item.modelNumber), str(item.color)])
10253 manish.sha 276
    EmailAttachmentSender.mail(mail_user, mail_password, 'manish.sharma@shop2020.in', 'Negative reserved: ' + str(reserved) + ' for Item Id: ' + itemName + ' warehouse id: ' + str(warehouse.id), None)
5944 mandeep.dh 277
 
28621 amit.gupta 278
 
5944 mandeep.dh 279
def __send_alert_for_negative_availability(item, availability, warehouse):
280
    itemName = " ".join([str(item.id), str(item.brand), str(item.modelName), str(item.modelNumber), str(item.color)])
5964 amar.kumar 281
    # EmailAttachmentSender.mail('cnc.center@shop2020.in', '5h0p2o2o', 'amar.kumar@shop2020.in', 'Negative availability ' + str(availability) + ' for Item id: ' + itemName + ' warehouse id: ' + str(warehouse.id), None)
5944 mandeep.dh 282
 
28621 amit.gupta 283
 
5944 mandeep.dh 284
def __send_mail_for_missing_key(item_key, quantity, warehouse_id):
28621 amit.gupta 285
    missedInventoryUpdate = MissedInventoryUpdate.get_by(itemKey=item_key, warehouseId=warehouse_id)
5944 mandeep.dh 286
    # One email per product key mismatch
287
    if not missedInventoryUpdate:
288
        missedInventoryUpdate = MissedInventoryUpdate()
289
        missedInventoryUpdate.itemKey = item_key
290
        missedInventoryUpdate.quantity = quantity
291
        missedInventoryUpdate.isIgnored = 1
292
        missedInventoryUpdate.timestamp = datetime.datetime.now()
293
        missedInventoryUpdate.warehouseId = warehouse_id
294
        session.commit()
6232 rajveer 295
        try:
8214 amar.kumar 296
            EmailAttachmentSender.mail(mail_user, mail_password, ['chaitnaya.vats@shop2020.in', 'chandan.kumar@shop2020.in', 'khushal.bhatia@shop2020.in', 'manoj.kumar@shop2020.in'], 'Skipped inventory update for ' + item_key + ' quantity ' + str(quantity) + ' warehouse id: ' + str(warehouse_id), None)
6232 rajveer 297
        except:
298
            print "Not able to send email. No issues, we can continue with updates."
5944 mandeep.dh 299
    else:
300
        missedInventoryUpdate.quantity += quantity
301
        session.commit()
302
 
28621 amit.gupta 303
 
19989 kshitij.so 304
def add_inventory(itemId, warehouseId, quantity, skipAddition=False):
5944 mandeep.dh 305
    current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
306
    if not current_inventory_snapshot:
307
        current_inventory_snapshot = CurrentInventorySnapshot()
308
        current_inventory_snapshot.item_id = itemId
309
        current_inventory_snapshot.warehouse_id = warehouseId
310
        current_inventory_snapshot.availability = 0
311
        current_inventory_snapshot.reserved = 0
8204 amar.kumar 312
        current_inventory_snapshot.held = 0
19989 kshitij.so 313
    # added the difference in the current inventory
28621 amit.gupta 314
    if skipAddition is None or not skipAddition: 
19989 kshitij.so 315
        current_inventory_snapshot.availability = current_inventory_snapshot.availability + quantity
316
    else:
317
        current_inventory_snapshot.availability = quantity
5944 mandeep.dh 318
    session.commit()
28621 amit.gupta 319
    # **Update item availability cache**#
20849 amit.gupta 320
    clear_item_availability_cache(itemId)
5944 mandeep.dh 321
    if current_inventory_snapshot.availability < 0:
322
        item = __get_item_from_master(itemId)
5978 rajveer 323
        __send_alert_for_negative_availability(item, current_inventory_snapshot.availability, get_Warehouse(warehouseId)) 
5944 mandeep.dh 324
 
28621 amit.gupta 325
 
5944 mandeep.dh 326
def add_bad_inventory(itemId, warehouseId, quantity):
327
    bad_inventory_snapshot = BadInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
328
    if not bad_inventory_snapshot:
329
        bad_inventory_snapshot = BadInventorySnapshot()
330
        bad_inventory_snapshot.item_id = itemId
331
        bad_inventory_snapshot.warehouse_id = warehouseId
332
        bad_inventory_snapshot.availability = 0
333
    # added the difference in the current inventory    
334
    bad_inventory_snapshot.availability += quantity
335
    session.commit()
336
    if bad_inventory_snapshot.availability < 0:
337
        item = __get_item_from_master(itemId)
338
        __send_alert_for_negative_availability(item, bad_inventory_snapshot.availability, get_Warehouse(warehouseId))
339
 
28621 amit.gupta 340
 
5944 mandeep.dh 341
def get_item_inventory_by_item_id(item_id):
342
    return CurrentInventorySnapshot.query.filter_by(item_id=item_id).all()
343
 
28621 amit.gupta 344
 
5944 mandeep.dh 345
def retire_warehouse(warehouse_id):
346
    if not warehouse_id:
347
        raise InventoryServiceException(101, "Bad warehouse id")
348
    warehouse = get_Warehouse(warehouse_id)
349
    if not warehouse:
350
        raise InventoryServiceException(108, "warehouse id not present")
351
    warehouse.status = 0;
352
    session.commit()
28621 amit.gupta 353
 
5944 mandeep.dh 354
 
355
def get_item_availability_for_warehouse(warehouse_id, item_id):
28621 amit.gupta 356
    ignore = IgnoredInventoryUpdateItems.query.filter_by(item_id=item_id).filter_by(warehouse_id=warehouse_id).all()
6544 rajveer 357
    if ignore:
358
        return 0
5944 mandeep.dh 359
 
360
    try:
28621 amit.gupta 361
        current_inventory_snapshot = CurrentInventorySnapshot.query.filter_by(warehouse_id=warehouse_id).filter_by(item_id=item_id).one()
15958 amit.gupta 362
        return current_inventory_snapshot.availability - current_inventory_snapshot.reserved - current_inventory_snapshot.held
5944 mandeep.dh 363
    except:
364
        return 0
365
 
28621 amit.gupta 366
 
6484 amar.kumar 367
def get_item_availability_for_our_warehouses(item_ids):
35732 amit 368
    warehouse_ids = [wh.id for wh in _warehouse_cache.values() if wh.warehouseType == 'OURS' and wh.inventoryType == 'GOOD']
6484 amar.kumar 369
    availability_map = dict()
370
 
28621 amit.gupta 371
    try:
372
        for item_id in item_ids:
6484 amar.kumar 373
            total_availability = 0
28621 amit.gupta 374
            for current_inventory_snapshot in CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).filter_by(item_id=item_id).all():
6484 amar.kumar 375
                total_availability += current_inventory_snapshot.availability
28621 amit.gupta 376
            if total_availability > 0:
6484 amar.kumar 377
                availability_map[item_id] = total_availability
378
    except Exception as e:
379
        print e
380
        raise PurchaseServiceException(101, 'Exception while fetching availability of items in our warehouses')
381
 
382
    return availability_map
383
 
28621 amit.gupta 384
 
385
# As of now as warehouse can be managed independently we just need to return itemavailability cache for the item ids provided
22719 amit.gupta 386
def get_fofo_availability(item_ids):
387
    returnMap = {}
388
    if not item_ids:
389
        return returnMap
22991 amit.gupta 390
    for item_id in item_ids:
391
        returnMap[item_id] = get_item_availability_for_location(item_id, 1)[4]
22719 amit.gupta 392
    return returnMap
393
 
28621 amit.gupta 394
 
22721 amit.gupta 395
def get_fofo_fulfilment_warehouse_map(itemIds):
396
    returnMap = {}
22722 amit.gupta 397
    snapshots = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id.in_(vendorWarehouseIdsForFofo)).filter(CurrentInventorySnapshot.item_id.in_(itemIds)).all()
22721 amit.gupta 398
    for snapshot in snapshots:
399
        availability = snapshot.availability - snapshot.reserved - snapshot.held
400
        if availability > 0:
401
            if not returnMap.has_key(snapshot.item_id):
402
                returnMap[snapshot.item_id] = to_t_warehouse(snapshot.warehouse)
403
    return returnMap 
404
 
5944 mandeep.dh 405
'''
406
This method returns quantity of a particular item across all warehouses whose ids is provided
407
if warehouse_ids is null it checks for inventory in all warehouses.
408
'''
28621 amit.gupta 409
 
410
 
5944 mandeep.dh 411
def __get_item_availability(item, warehouse_ids):
412
    if warehouse_ids is None:
28621 amit.gupta 413
        all_inventory = CurrentInventorySnapshot.query.filter_by(item=item).all()
5944 mandeep.dh 414
        availability = 0
415
        reserved = 0
416
        for currInv in all_inventory:
417
            availability = availability + currInv.availability
418
            reserved = reserved + currInv.reserved
419
        return availability - reserved
420
    else:
421
        total_availability = 0
28621 amit.gupta 422
        for current_inventory_snapshot in CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).filter_by(item_id=item.id).all():
5944 mandeep.dh 423
            total_availability += current_inventory_snapshot.availability - current_inventory_snapshot.reserved
424
        return total_availability 
425
 
28621 amit.gupta 426
 
5944 mandeep.dh 427
def __get_item_reserved(item_id):
28621 amit.gupta 428
    all_inventory = CurrentInventorySnapshot.query.filter_by(item_id=item_id).all()
5944 mandeep.dh 429
    reserved = 0
430
    for currInv in all_inventory:
431
        reserved = reserved + currInv.reserved
432
    return reserved
5966 rajveer 433
 
28621 amit.gupta 434
 
5966 rajveer 435
def __get_item_availability_at_warehouse(warehouse_id, item_id):
28621 amit.gupta 436
    inventory = CurrentInventorySnapshot.query.filter_by(warehouse_id=warehouse_id, item_id=item_id).one()
5966 rajveer 437
    return inventory.availability
438
 
28621 amit.gupta 439
 
5966 rajveer 440
def is_order_billable(item_id, warehouse_id, source_id, order_id):
28621 amit.gupta 441
    reservations = CurrentReservationSnapshot.query.filter_by(warehouse_id=warehouse_id, item_id=item_id).order_by(CurrentReservationSnapshot.promised_shipping_timestamp).order_by(CurrentReservationSnapshot.created_timestamp).all()
5966 rajveer 442
    availability = __get_item_availability_at_warehouse(warehouse_id, item_id)
443
    for reservation in reservations:
444
        availability = availability - reservation.reserved
445
        if reservation.order_id == order_id and reservation.source_id == source_id:
446
            break
447
    if availability < 0:
448
        return False
449
    return True
28621 amit.gupta 450
 
5944 mandeep.dh 451
 
19989 kshitij.so 452
def reserve_item_in_warehouse(item_id, warehouse_id, source_id, order_id, created_timestamp, promised_shipping_timestamp, quantity):
453
 
5944 mandeep.dh 454
    if not warehouse_id:
455
        raise InventoryServiceException(101, "bad warehouse_id")
456
 
28621 amit.gupta 457
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id=warehouse_id, item_id=item_id)
5944 mandeep.dh 458
    try:
459
        current_inventory_snapshot = query.one()
460
    except:
461
        current_inventory_snapshot = CurrentInventorySnapshot()
462
        current_inventory_snapshot.warehouse_id = warehouse_id
463
        current_inventory_snapshot.item_id = item_id
464
        current_inventory_snapshot.availability = 0
465
        current_inventory_snapshot.reserved = 0
8204 amar.kumar 466
        current_inventory_snapshot.held = 0
5944 mandeep.dh 467
 
468
    current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
5966 rajveer 469
 
470
    reservation = CurrentReservationSnapshot()
471
    reservation.item_id = item_id
472
    reservation.warehouse_id = warehouse_id
473
    reservation.source_id = source_id
474
    reservation.order_id = order_id
5990 rajveer 475
    reservation.created_timestamp = to_py_date(created_timestamp)
476
    reservation.promised_shipping_timestamp = to_py_date(promised_shipping_timestamp)
5966 rajveer 477
    reservation.reserved = quantity
478
 
8720 amar.kumar 479
    session.commit()
8182 amar.kumar 480
 
481
    try:
482
        order_client = TransactionClient().get_client()
483
        order = order_client.getOrder(order_id)
9574 amar.kumar 484
        if order.source:
28621 amit.gupta 485
            holdInventoryDetail = HoldInventoryDetail.query.filter_by(item_id=item_id, warehouse_id=warehouse_id, source=order.source).first()
486
            if holdInventoryDetail is None or holdInventoryDetail.held <= 0:
487
                holdInventoryDetails = HoldInventoryDetail.query.filter_by(item_id=item_id, source=order.source).all()
9694 amar.kumar 488
                if holdInventoryDetails:
489
                    for hID in holdInventoryDetails:
28621 amit.gupta 490
                        if hID.held > 0:
9694 amar.kumar 491
                            holdInventoryDetail = hID
28621 amit.gupta 492
            if holdInventoryDetail is not None and holdInventoryDetail.held > 0:
9574 amar.kumar 493
                previousHeld = holdInventoryDetail.held
28621 amit.gupta 494
                holdInventoryDetail.held = max(0, holdInventoryDetail.held - quantity)
495
                diff = previousHeld - holdInventoryDetail.held 
9770 amar.kumar 496
                current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=item_id, warehouse_id=holdInventoryDetail.warehouse_id)
9574 amar.kumar 497
                if current_inventory_snapshot is not None:
498
                    current_inventory_snapshot.held = max(0, current_inventory_snapshot.held - diff)
499
                session.commit()
8182 amar.kumar 500
    except:
501
        print "Unable to release hold Inventory for item_id " + str(item_id) + " warehouse_id " + str(warehouse_id) + " source " + str(source_id)
28621 amit.gupta 502
    # session.commit()
503
    # **Update item availability cache**#
5978 rajveer 504
    clear_item_availability_cache(item_id)
5944 mandeep.dh 505
    return True
506
 
28621 amit.gupta 507
 
508
def update_reservation_for_order(item_id, warehouse_id, source_id, order_id, created_timestamp, promised_shipping_timestamp, quantity): 
7968 amar.kumar 509
    if not warehouse_id:
510
        raise InventoryServiceException(101, "bad warehouse_id")
23516 amit.gupta 511
    warehouse = get_Warehouse(warehouse_id)
512
    '''item_pricing = get_item_pricing(item_id, warehouse.vendor.id)
513
    if not item_pricing:
514
        raise InventoryServiceException(101, "No Pricing Info found for vendor and Item")'''
28621 amit.gupta 515
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id=warehouse_id, item_id=item_id)
23516 amit.gupta 516
    try:
517
        new_current_inventory_snapshot = query.one()
518
    except:
519
        new_current_inventory_snapshot = CurrentInventorySnapshot()
520
        new_current_inventory_snapshot.warehouse_id = warehouse_id
521
        new_current_inventory_snapshot.item_id = item_id
522
        new_current_inventory_snapshot.availability = 0
523
        new_current_inventory_snapshot.reserved = 0
7968 amar.kumar 524
 
23516 amit.gupta 525
    new_current_inventory_snapshot.reserved = new_current_inventory_snapshot.reserved + quantity
7968 amar.kumar 526
 
527
    new_reservation = CurrentReservationSnapshot()
528
    new_reservation.item_id = item_id
529
    new_reservation.warehouse_id = warehouse_id
530
    new_reservation.source_id = source_id
531
    new_reservation.order_id = order_id
532
    new_reservation.created_timestamp = to_py_date(created_timestamp)
533
    new_reservation.promised_shipping_timestamp = to_py_date(promised_shipping_timestamp)
534
    new_reservation.reserved = quantity
535
 
8182 amar.kumar 536
    try:
537
        order_client = TransactionClient().get_client()
538
        order = order_client.getOrder(order_id)
9574 amar.kumar 539
        if order.source:
28621 amit.gupta 540
            holdInventoryDetail = HoldInventoryDetail.query.filter_by(item_id=item_id, warehouse_id=warehouse_id, source=order.source).first()
541
            if holdInventoryDetail is None or holdInventoryDetail.held <= 0:
542
                holdInventoryDetails = HoldInventoryDetail.query.filter_by(item_id=item_id, source=order.source).all()
9694 amar.kumar 543
                if holdInventoryDetails:
544
                    for hID in holdInventoryDetails:
28621 amit.gupta 545
                        if hID.held > 0:
9694 amar.kumar 546
                            holdInventoryDetail = hID
28621 amit.gupta 547
            if holdInventoryDetail is not None and holdInventoryDetail.held > 0:
9574 amar.kumar 548
                previousHeld = holdInventoryDetail.held
28621 amit.gupta 549
                holdInventoryDetail.held = max(0, holdInventoryDetail.held - quantity)
550
                diff = previousHeld - holdInventoryDetail.held 
9770 amar.kumar 551
                current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=item_id, warehouse_id=holdInventoryDetail.warehouse_id)
9574 amar.kumar 552
                if current_inventory_snapshot is not None:
553
                    current_inventory_snapshot.held = max(0, current_inventory_snapshot.held - diff)
554
                session.commit()
8182 amar.kumar 555
    except:
556
        print "Unable to release hold Inventory for item_id " + str(item_id) + " warehouse_id " + str(warehouse_id) + " source " + str(source_id)
557
 
7968 amar.kumar 558
    order_client = TransactionClient().get_client()
559
    order = order_client.getOrder(order_id)
560
    for lineitem in order.lineitems:
28621 amit.gupta 561
        query = CurrentInventorySnapshot.query.filter_by(warehouse_id=order.fulfilmentWarehouseId, item_id=lineitem.item_id)
7968 amar.kumar 562
        try:
563
            current_inventory_snapshot = query.one()
564
            current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
565
 
28621 amit.gupta 566
            reservation = CurrentReservationSnapshot.query.filter_by(warehouse_id=order.fulfilmentWarehouseId, item_id=lineitem.item_id, source_id=source_id, order_id=order_id).one()
7968 amar.kumar 567
            if reservation.reserved == quantity:
568
                reservation.delete()
569
            else:
570
                reservation.reserved -= quantity
571
 
572
            clear_item_availability_cache(lineitem.item_id)
573
            session.commit()
574
            try:
575
                if current_inventory_snapshot.reserved < 0:
576
                    item = __get_item_from_master(lineitem.item_id)
577
                    __send_alert_for_negative_reserved(item, current_inventory_snapshot.reserved, get_Warehouse(order.fulfilmentWarehouseId))
578
            except:
8182 amar.kumar 579
                print "Error in sending negative reserved alert:", sys.exc_info()[0]
7968 amar.kumar 580
                return False
581
        except:
582
            print "Error in reducing reservation for item:", sys.exc_info()[0]
583
            return False
584
    session.commit()
28621 amit.gupta 585
    # **Update item availability cache**#
7968 amar.kumar 586
    clear_item_availability_cache(item_id)
587
    return True
588
 
28621 amit.gupta 589
 
19419 amit.gupta 590
def get_item_pincode_availability(itemPricingMap, pin_code):
19413 amit.gupta 591
    returnMap = {}
592
    missingItemPricingList = []
593
    for item_id, pricing in itemPricingMap.iteritems():
594
        if not pricing:
595
            missingItemPricingList.append(item_id)
20641 amit.gupta 596
    if missingItemPricingList:
597
        cc = CatalogClient().get_client()
598
        items = cc.getItems(missingItemPricingList)
599
        lc = LogisticsClient().get_client()
28621 amit.gupta 600
        # Consider selling price if its missing in itempricing
20641 amit.gupta 601
        for item in items:
602
            itemPricingMap[item.id] = item.sellingPrice
19413 amit.gupta 603
    pricingLocationInfoMap = __getLocationInfoMap(pin_code, itemPricingMap)    
604
    if pricingLocationInfoMap == -1:
605
        returnMap = {"pincode_serviceable":False}    
606
    else:
28621 amit.gupta 607
        # for item_id, sellingPrice in itemPricingMap.iteritems():
19413 amit.gupta 608
        allLocations = pricingLocationInfoMap[1].keys()    
609
        itemLocationAvailabilityList = __get_item_location_availability_bulk(itemPricingMap.keys(), allLocations)
610
        pricingLocationsMap = {}
611
        for itemLocationAvailability in itemLocationAvailabilityList:
612
            sellingPrice = itemPricingMap[itemLocationAvailability.item_id]
613
            if pricingLocationInfoMap[sellingPrice] == -1:
614
                continue
615
            if not pricingLocationsMap.has_key(sellingPrice):
616
                pricingLocationsMap[sellingPrice] = pricingLocationInfoMap[sellingPrice].keys() 
617
 
618
            if itemLocationAvailability.location_id not in pricingLocationsMap[sellingPrice]:
619
                continue
620
            if not returnMap.has_key(itemLocationAvailability.item_id):
28621 amit.gupta 621
                returnMap[itemLocationAvailability.item_id] = ItemPincodeAvailability(vatQty=0, totalQty=0, minDeliveryDate=-1)
19413 amit.gupta 622
            itemPincodeAvailability = ItemPincodeAvailability()
623
            itemPincodeAvailability = returnMap[itemLocationAvailability.item_id]
624
            locationInfo = LocationInfo()
625
            locationInfo = pricingLocationInfoMap[sellingPrice][itemLocationAvailability.location_id]
626
            locationQty = itemLocationAvailability.virtual_availability + itemLocationAvailability.physical_availability
627
            if locationInfo.sameState:
628
                itemPincodeAvailability.vatQty += locationQty
629
            itemPincodeAvailability.totalQty += locationQty
630
            itemPincodeAvailability.isCod = itemPincodeAvailability.isCod or locationInfo.isCod 
631
            itemPincodeAvailability.isOtg = itemPincodeAvailability.isOtg or locationInfo.isOtg 
632
            if itemPincodeAvailability.minDeliveryDate == -1:
633
                itemPincodeAvailability.minDeliveryDate = itemLocationAvailability.min_transfer_delay + locationInfo.minDelay 
634
                itemPincodeAvailability.maxDeliveryDate = itemLocationAvailability.max_transfer_delay + locationInfo.maxDelay
635
            else:
636
                itemPincodeAvailability.minDeliveryDate = min(itemPincodeAvailability.minDeliveryDate, itemLocationAvailability.min_transfer_delay + locationInfo.minDelay) 
637
                itemPincodeAvailability.maxDeliveryDate = max(itemPincodeAvailability.maxDeliveryDate, itemLocationAvailability.max_transfer_delay + locationInfo.maxDelay)
638
        for itemId, itemPincodeAvailability in returnMap.iteritems():
639
            sellingPrice = itemPricingMap[itemId]
640
            locationsMap = pricingLocationsMap[sellingPrice]
641
            minDay = math.ceil(itemPincodeAvailability.minDeliveryDate)
642
            maxDay = math.ceil(itemPincodeAvailability.maxDeliveryDate)
643
            itemPincodeAvailability.minDeliveryDate, itemPincodeAvailability.maxDeliveryDate = __getDeliveryDate(minDay, itemPincodeAvailability.isCod, maxDay)
644
 
645
    return json.dumps(returnMap)
646
 
647
 
648
def __getLocationInfoMap(pin_code, itemPricingMap):
20641 amit.gupta 649
    priceSet = set(itemPricingMap.values() + [1])
19413 amit.gupta 650
    if not pincodePricingServiceabilityMap.has_key(pin_code):
651
        pincodePricingServiceabilityMap[pin_code] = {}
652
    pricingMap = pincodePricingServiceabilityMap[pin_code]
653
    if pricingMap != -1: 
20641 amit.gupta 654
        missingInMap = list(priceSet - set(pricingMap.keys())) 
655
        if missingInMap:
656
            lc = LogisticsClient().get_client()
657
            priceLocationInfoMap = lc.getLocationInfoMap(pin_code, missingInMap)
658
            if not priceLocationInfoMap:
659
                pricingMap = pincodePricingServiceabilityMap[pin_code] = -1
660
            else:
661
                for sellingPrice in missingInMap:
662
                    if priceLocationInfoMap[sellingPrice] == {}:
663
                        pricingMap[sellingPrice] = -1
664
                    else:
665
                        pricingMap[sellingPrice] = priceLocationInfoMap[sellingPrice]
19413 amit.gupta 666
 
667
    return pricingMap
28621 amit.gupta 668
 
19413 amit.gupta 669
 
670
def __getDeliveryDate(minDays, isCod, maxDays=None):
671
    curTime = datetime.datetime.now()
672
    if maxDays is None:
673
        maxDays = minDays 
674
    if isCod and curTime.hour < 15:
675
        maxDays = maxDays + 1
676
    return __getAdjustedDate(minDays), __getAdjustedDate(maxDays) 
7968 amar.kumar 677
 
28621 amit.gupta 678
 
19413 amit.gupta 679
def __getAdjustedDate(days):
680
    curDate = datetime.date.today()
681
    if curDate == last_date:
682
        if adjusted_dates.has_key(days):
683
            return adjusted_dates[days]
684
    else:
685
        adjusted_dates = {}
686
    lc = LogisticsClient().get_client()
687
    adjusted_day = lc.adjustDeliveryDays(datetime.datetime.now(), to_java_date(days))
688
 
689
    adjusted_date = to_java_date(datetime.datetime.combine(curDate, datetime.datetime.min.time()) + datetime.timedelta(days=adjusted_day))
690
    adjusted_dates[days] = adjusted_date
691
    return adjusted_date
692
 
28621 amit.gupta 693
 
5966 rajveer 694
def reduce_reservation_count(item_id, warehouse_id, source_id, order_id, quantity):
5944 mandeep.dh 695
    if not warehouse_id:
696
        raise InventoryServiceException(101, "bad warehouse_id")
697
 
28621 amit.gupta 698
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id=warehouse_id, item_id=item_id)
5944 mandeep.dh 699
    try:
700
        current_inventory_snapshot = query.one()
701
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
5966 rajveer 702
 
28621 amit.gupta 703
        reservation = CurrentReservationSnapshot.query.filter_by(warehouse_id=warehouse_id, item_id=item_id, source_id=source_id, order_id=order_id).one()
5966 rajveer 704
        if reservation.reserved == quantity:
705
            reservation.delete()
706
        else:
707
            reservation.reserved -= quantity
5944 mandeep.dh 708
        session.commit()
28621 amit.gupta 709
        # **Update item availability cache**#
5978 rajveer 710
        clear_item_availability_cache(item_id)
5944 mandeep.dh 711
        if current_inventory_snapshot.reserved < 0:
712
            item = __get_item_from_master(item_id)
713
            __send_alert_for_negative_reserved(item, current_inventory_snapshot.reserved, get_Warehouse(warehouse_id))
714
        return True
715
    except:
716
        print "Unexpected error:", sys.exc_info()[0]
717
        return False
718
 
28621 amit.gupta 719
# This is not the source as in snapdeal or website, this source is to incorporate different pricing depending upon source id.
720
# i.e. if user visiting to our site has specific source he would see pricing on that source basis. Default source is 1.
22719 amit.gupta 721
# Have to add another source id 2 for fofo warehouses
23442 amit.gupta 722
 
28621 amit.gupta 723
 
23446 amit.gupta 724
def get_item_availability_for_state(itemId, sourceId, stateId):
725
    __populateWarehouseMap()
23442 amit.gupta 726
    currentInventorySnapshots = get_item_inventory_by_item_id(itemId)
23446 amit.gupta 727
    totalAvailability = 0
728
    expectedDelay = 0
23442 amit.gupta 729
    if len(currentInventorySnapshots) > 0:
730
        for currentInventorySnapshot in currentInventorySnapshots:
23630 amit.gupta 731
            if currentInventorySnapshot.warehouse.state_id == stateId:
23446 amit.gupta 732
                availability = currentInventorySnapshot.availability - currentInventorySnapshot.reserved
733
                if availability > 0:
23482 amit.gupta 734
                    warehouse = currentInventorySnapshot.warehouse
23446 amit.gupta 735
                totalAvailability += availability 
736
    if totalAvailability <= 0:
23611 amit.gupta 737
        warehouse = virtualVendorWarehouses.get(stateId)
23446 amit.gupta 738
        expectedDelay = 2
25469 amit.gupta 739
    warehouseId = warehouse.id
23446 amit.gupta 740
    ia = get_item_availability_for_location(itemId, sourceId)
25469 amit.gupta 741
    return [warehouseId, expectedDelay, billingVendorWarehouses[stateId], ia[3], totalAvailability, ia[5]]
23442 amit.gupta 742
 
743
 
5978 rajveer 744
def get_item_availability_for_location(item_id, source_id):
28621 amit.gupta 745
    item_availability = ItemAvailabilityCache.get_by(itemId=item_id, sourceId=source_id)
5944 mandeep.dh 746
    if item_availability:
28621 amit.gupta 747
        print "Selling Price", item_availability.sellingPrice
24741 amit.gupta 748
        if item_availability.sellingPrice is None:
749
            item_availability.sellingPrice = 100000
750
        print "[item_availability.warehouseId, item_availability.expectedDelay, item_availability.billingWarehouseId, item_availability.sellingPrice, item_availability.totalAvailability, item_availability.weight]"
751
        print item_availability.warehouseId, item_availability.expectedDelay, item_availability.billingWarehouseId, item_availability.sellingPrice, item_availability.totalAvailability, item_availability.weight
7589 rajveer 752
        return [item_availability.warehouseId, item_availability.expectedDelay, item_availability.billingWarehouseId, item_availability.sellingPrice, item_availability.totalAvailability, item_availability.weight]
28621 amit.gupta 753
        # return [item_availability.warehouseId, item_availability.expectedDelay, item_availability.billingWarehouseId, item_availability.sellingPrice, 0, item_availability.weight]
5944 mandeep.dh 754
    else:
5978 rajveer 755
        __update_item_availability_cache(item_id, source_id)
28621 amit.gupta 756
            # #Check risky status for the source
5978 rajveer 757
        __check_risky_item(item_id, source_id)
758
        return get_item_availability_for_location(item_id, source_id)
28621 amit.gupta 759
 
5944 mandeep.dh 760
 
19413 amit.gupta 761
def get_item_location_availability(item_id, locations=[]):
762
    if locations:
763
        newLocations = locations + [-1]
764
        itemisedItemLocationAvailability = ItemLocationAvailabilityCache.filter_by(itemId=item_id).filter(ItemLocationAvailability.locationId.in_(newLocations)).all()
765
    else:
766
        itemisedItemLocationAvailability = ItemLocationAvailabilityCache.filter_by(itemId=item_id).all()
767
    if not itemisedItemLocationAvailability:
768
        __update_item_location(item_id)
769
        return get_item_location_availability(item_id, locations)
770
    else:
771
        thriftList = []
772
        for itemLocationAvailability in itemisedItemLocationAvailability:
773
            if itemLocationAvailability.location_id == -1:
774
                itemisedItemLocationAvailability
775
            else:
776
                thriftList.append(to_t_item_location_availability(itemLocationAvailability))
777
        return thriftList
778
 
28621 amit.gupta 779
 
19413 amit.gupta 780
def __get_item_location_availability_bulk(item_ids, locations=[]):
781
 
782
    query = ItemLocationAvailabilityCache.query.filter(ItemLocationAvailabilityCache.item_id.in_(item_ids))
28621 amit.gupta 783
    allPopulated = query.filter(ItemLocationAvailabilityCache.location_id == -1).all()
19413 amit.gupta 784
    if len(item_ids) > len(allPopulated):
785
        for item_id in allPopulated:
786
            if item_id not in item_ids:
787
                __update_item_location(item_id)
788
    if locations:
789
        query.filter(ItemLocationAvailabilityCache.location_id.in_(locations)).all()
790
    return query.all()
791
 
792
 
793
def clear_item_location_availability_cache(item_id, locations=[]):
28621 amit.gupta 794
    if type(item_id) == list:
19413 amit.gupta 795
        ItemLocationAvailabilityCache.query.filter(ItemLocationAvailabilityCache.itemId.in_(item_id)).delete(synchronize_session='fetch')
796
        session.commit()
797
        t = threading.Thread(target=_task_update_item_availability_cache, args=(item_id,))
798
        t.start()
799
    elif item_id:
28621 amit.gupta 800
        ItemLocationAvailabilityCache.query.filter_by(itemId=item_id).delete()
19413 amit.gupta 801
    session.commit()
802
 
28621 amit.gupta 803
 
804
def clear_item_availability_cache(item_id=None):
13150 manish.sha 805
    print item_id
28621 amit.gupta 806
    if type(item_id) == list:
13150 manish.sha 807
        ItemAvailabilityCache.query.filter(ItemAvailabilityCache.itemId.in_(item_id)).delete(synchronize_session='fetch')
13149 manish.sha 808
        session.commit()
13493 amit.gupta 809
        t = threading.Thread(target=_task_update_item_availability_cache, args=(item_id,))
810
        t.start()
811
 
13520 amit.gupta 812
    elif item_id:
28621 amit.gupta 813
        ItemAvailabilityCache.query.filter_by(itemId=item_id).delete()
12963 amit.gupta 814
        session.commit()
28621 amit.gupta 815
 
13493 amit.gupta 816
 
817
def _task_update_item_availability_cache(item_ids):
35718 amit 818
    try:
819
        client = CatalogClient("catalog_service_server_host_master", "catalog_service_server_port").get_client()
820
        items = client.getItems(item_ids)
821
        for item_id in item_ids:
822
            item_availability = ItemAvailabilityCache.get_by(itemId=item_id, sourceId=1)
823
            if not item_availability:
824
                try:
825
                    __update_item_availability_cache(item_id, 1, items[item_id])
826
                    __check_risky_item(item_id, 1)
827
                except:
828
                    print "Could not update cache for "
829
                    continue
830
        return True
831
    finally:
832
        session.remove()
5944 mandeep.dh 833
 
28621 amit.gupta 834
 
19413 amit.gupta 835
def __update_item_location(item_id):
836
    ignoredWhs = get_ignored_warehouses(item_id)
837
 
28621 amit.gupta 838
    itemsnapshot = CurrentInventorySnapshot.query.filter_by(item_id=item_id).all()
19413 amit.gupta 839
    locationsMap = {}
840
    for row in itemsnapshot:
841
        warehouse = warehouseMap[row.warehouse_id]
842
        if row.warehouse_id in ignoredWhs:
843
            continue 
844
 
845
        location = warehouse.logisticsLocation
846
        if not locationsMap.has_key(location):
28621 amit.gupta 847
            locationsMap[location] = {"physicalQty":0, "virtualQty":0, "minTransferDelay":100, "maxTransferDelay":0}
19413 amit.gupta 848
        locationMap = locationsMap[location]
849
        if warehouse.type == 'THIRD_PARTY':
850
            locationMap["virtualQty"] += max(0, row.availability - row.reserverd - row.held)
28621 amit.gupta 851
            locationMap["minTransferDelay"] = min(locationMap["minTransferDelay"], row.transferDelayInHours / 24)
852
            locationMap["maxTransferDelay"] = max(locationMap["maxTransferDelay"], row.transferDelayInHours / 24)
19413 amit.gupta 853
        else:
854
            locationMap["physicalQty"] += max(0, row.availability - row.reserverd - row.held)
855
            locationMap["minTransferDelay"] = 0
856
    for location, locationMap in locationsMap.iteritems():
857
        if locationMap["virtualQty"] > 0 or locationMap["physicalQty"] > 0: 
858
            itemLocationAvailability = ItemLocationAvailabilityCache()
859
            itemLocationAvailability.item_id = item_id
860
            itemLocationAvailability.location_id = location
861
            itemLocationAvailability.max_transfer_delay = locationMap["maxTransferDelay"]
862
            itemLocationAvailability.min_transfer_delay = locationMap["minTransferDelay"]
863
            itemLocationAvailability.virtual_availability = locationMap["virtualQty"]
864
            itemLocationAvailability.physical_availability = locationMap["physicalQty"]
28621 amit.gupta 865
    # Add location -1 for each item
19413 amit.gupta 866
    itemLocationAvailability = ItemLocationAvailabilityCache()
867
    itemLocationAvailability.item_id = item_id
868
    itemLocationAvailability.location_id = -1
869
    session.commit()
870
 
871
 
12963 amit.gupta 872
def __update_item_availability_cache(item_id, source_id, item=None):
5944 mandeep.dh 873
    """
22719 amit.gupta 874
    Then how shoud we handle virtual because for both source virtual gonna mark
875
    """
876
    """
8954 vikram.rag 877
    Determines the warehouse that should be used to fulfil an order for the given item.
5944 mandeep.dh 878
    Algorithm explained at https://sites.google.com/a/shop2020.in/virtual-w-h-and-inventory/technical-details
879
 
880
    It will be ensured that every item has either a preferred vendor specified or at least for one vendor its transfer price should be defined.
881
    This is needed to associate an item with at least one vendor so that in default case when its available no where, we know from where to procure it.
882
 
883
    if item available at any OUR-GOOD warehouse
884
        // OUR-GOOD warehouses have inventory risk; So, we empty them first! 
885
        // We can start with minimum transfer price criterion but down the line we can also bring in Inventory age 
886
        assign OUR-GOOD warehouse with minimum transfer price
887
    else
888
        if Preferred vendor is specified and marked Sticky
889
            // Always purchase from Preferred if its marked sticky
890
            assign preferred vendor's THIRDPARTY GOOD/VIRTUAL warehouse
891
        else 
892
            if item available in a THIRDPARTY GOOD/VIRTUAL warehouse
893
                assign THIRDPARTY GOOD/VIRTUAL warehouse where item is available with minimal transfer delay followed by minimum transfer price
894
            else 
895
                // Item not available at any warehouse, OURS or THIRDPARTY
896
                If Preferred vendor is specified
897
                    assign preferred vendor's THIRDPARTY GOOD/VIRTUAL warehouse
898
                else
899
                    assign THIRDPARTY GOOD/VIRTUAL warehouse with minimum transfer price
900
 
901
    Returns an ordered list of size 4 with following elements in the given order:
902
    1. Logistics location of the warehouse which was finally picked up to ship the order.
903
    2. Expected delay added by the category manager.
904
    3. Id of the warehouse which was finally picked up.
905
 
906
    Parameters:
907
     - itemId
908
    """
12963 amit.gupta 909
    if item is None:
910
        item = __get_item_from_source(item_id, source_id)
5944 mandeep.dh 911
    item_pricing = {}
912
    for vendorItemPricing in VendorItemPricing.query.filter_by(item_id=item_id).all():
913
        item_pricing[vendorItemPricing.vendor_id] = vendorItemPricing
914
 
6510 rajveer 915
    ignoredWhs = get_ignored_warehouses(item_id)
916
 
5944 mandeep.dh 917
    warehouses = {}
918
    ourGoodWarehouses = {}
919
    thirdpartyWarehouses = {}
920
    preferredThirdpartyWarehouses = {}
35732 amit 921
    for warehouse in _warehouse_cache.values():
7410 amar.kumar 922
        if (warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.BAD] or warehouse.warehouseType == WarehouseType._VALUES_TO_NAMES[WarehouseType.OURS_THIRDPARTY]):
5944 mandeep.dh 923
            continue
924
        warehouses[warehouse.id] = warehouse
925
        if warehouse.warehouseType == WarehouseType._VALUES_TO_NAMES[WarehouseType.OURS]:
926
            if warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.GOOD]:
927
                ourGoodWarehouses[warehouse.id] = warehouse
928
        else:
929
            thirdpartyWarehouses[warehouse.id] = warehouse
930
            if item.preferredVendor == warehouse.vendor_id and warehouse.inventoryType == InventoryType._VALUES_TO_NAMES[InventoryType.GOOD]:
931
                preferredThirdpartyWarehouses[warehouse.id] = warehouse
932
 
933
    warehouse_retid = -1
934
    total_availability = 0
935
 
6540 rajveer 936
    [warehouse_retid, total_availability] = __get_warehouse_with_min_transfer_price(ourGoodWarehouses, ignoredWhs, item_id, item_pricing, False)
5944 mandeep.dh 937
    if warehouse_retid == -1:
938
        if item.preferredVendor and item.isWarehousePreferenceSticky:
6540 rajveer 939
            [warehouse_retid, total_availability] = __get_warehouse_with_min_transfer_delay(preferredThirdpartyWarehouses, ignoredWhs, item_id, item_pricing)
5944 mandeep.dh 940
            if warehouse_retid == -1:
941
                warehouse_retid = preferredThirdpartyWarehouses.keys()[0]
942
        else:
6540 rajveer 943
            [warehouse_retid, total_availability] = __get_warehouse_with_min_transfer_delay(thirdpartyWarehouses, ignoredWhs, item_id, item_pricing)
5944 mandeep.dh 944
            if warehouse_retid == -1:
945
                if item.preferredVendor:
946
                    warehouse_retid = preferredThirdpartyWarehouses.keys()[0]
947
                else:
6540 rajveer 948
                    [warehouse_retid, total_availability] = __get_warehouse_with_min_transfer_price(thirdpartyWarehouses, ignoredWhs, item_id, item_pricing, True)
24741 amit.gupta 949
    print "I am able to reach here wih warehouse_retid", warehouse_retid
5944 mandeep.dh 950
    warehouse = warehouses[warehouse_retid]
951
    billingWarehouseId = warehouse.billingWarehouseId
952
 
953
    # Fetching billing warehouse of a Good billable warehouse corresponding to the virtual one
954
    if not warehouse.billingWarehouseId:
35732 amit 955
        for w in _warehouse_cache.values():
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:
5944 mandeep.dh 957
                billingWarehouseId = w.billingWarehouseId
958
                break
959
 
960
    expectedDelay = item.expectedDelay 
961
    if expectedDelay is None:
962
        print 'expectedDelay field for this item was Null. Resetting it to 0'
963
        expectedDelay = 0
964
    else:
965
        expectedDelay = int(item.expectedDelay)
966
 
967
    if total_availability <= 0:
8026 amar.kumar 968
        if item.preferredVendor in [1, 5]:
6562 rajveer 969
            expectedDelay = expectedDelay + 3
970
        else:
971
            expectedDelay = expectedDelay + 2
6643 rajveer 972
    else:
973
        if warehouse.transferDelayInHours:
974
            expectedDelay = expectedDelay + warehouse.transferDelayInHours / 24
5944 mandeep.dh 975
 
18022 manish.sha 976
    if WarehouseType._NAMES_TO_VALUES[warehouse.warehouseType] == WarehouseType.THIRD_PARTY:
8491 rajveer 977
        expectedDelay = expectedDelay + __get_vendor_holiday_delay(warehouse.vendor_id, expectedDelay) 
978
 
5963 mandeep.dh 979
    total_availability = 0
28621 amit.gupta 980
    for entry in CurrentInventorySnapshot.query.filter_by(item_id=item_id).all():
6545 rajveer 981
        if entry.warehouse_id not in ignoredWhs:
16029 amit.gupta 982
            if entry.warehouse_id not in ourGoodWarehouses and entry.warehouse_id not in thirdpartyWarehouses:
16015 amit.gupta 983
                continue
984
            if entry.warehouse_id in ourGoodWarehouses and ourGoodWarehouses[entry.warehouse_id].billingWarehouseId == billingWarehouseId:
985
                total_availability += entry.availability - entry.reserved - entry.held
986
            elif entry.warehouse_id in thirdpartyWarehouses:
987
                vendorId = thirdpartyWarehouses[entry.warehouse_id].vendor_id
988
                for goodWarehouse in ourGoodWarehouses.values():
28621 amit.gupta 989
                    if goodWarehouse.vendor_id == vendorId and goodWarehouse.billingWarehouseId == billingWarehouseId and warehouse.state_id == goodWarehouse.state_id:
16015 amit.gupta 990
                        total_availability += entry.availability - entry.reserved - entry.held
991
                        break
5963 mandeep.dh 992
 
5978 rajveer 993
    item_availability_cache = ItemAvailabilityCache.get_by(itemId=item_id, sourceId=source_id)
5944 mandeep.dh 994
    if item_availability_cache is None:
995
        item_availability_cache = ItemAvailabilityCache()
996
        item_availability_cache.itemId = item_id
5978 rajveer 997
        item_availability_cache.sourceId = source_id
5944 mandeep.dh 998
    item_availability_cache.warehouseId = int(warehouse_retid)
999
    item_availability_cache.expectedDelay = expectedDelay
1000
    item_availability_cache.billingWarehouseId = billingWarehouseId
1001
    item_availability_cache.sellingPrice = item.sellingPrice
1002
    item_availability_cache.totalAvailability = total_availability
28621 amit.gupta 1003
    # item_availability_cache.location = warehouse.logisticsLocation 
1004
    item_availability_cache.weight = 1000 * item.weight if item.weight else 300
5944 mandeep.dh 1005
    session.commit()
1006
 
28621 amit.gupta 1007
 
6540 rajveer 1008
def __get_warehouse_with_min_transfer_price(warehouses, ignoredWhs, item_id, item_pricing, ignoreAvailability):
5944 mandeep.dh 1009
    warehouse_retid = -1
1010
    minTransferPrice = None
1011
    total_availability = 0
6013 amar.kumar 1012
    availabilityForBillingWarehouses = {}
1013
    warehousesAvailability = {}
1014
    availability = 0
1015
    billing_warehouse_retid = None
5944 mandeep.dh 1016
 
1017
    if not ignoreAvailability:
28621 amit.gupta 1018
        for entry in CurrentInventorySnapshot.query.filter_by(item_id=item_id).all():
7242 amar.kumar 1019
            entry.reserved = max(entry.reserved, 0)
8524 amar.kumar 1020
            entry.held = max(entry.held, 0)
28621 amit.gupta 1021
            # if entry.availability > entry.reserved:
8182 amar.kumar 1022
            warehousesAvailability[entry.warehouse_id] = [entry.availability, entry.reserved, entry.held] 
5944 mandeep.dh 1023
 
6540 rajveer 1024
    if len(ignoredWhs) > 0:
1025
        for whid in ignoredWhs:
1026
            if warehousesAvailability.has_key(whid):
6542 rajveer 1027
                warehousesAvailability[whid][0] = 0
6683 rajveer 1028
                warehousesAvailability[whid][1] = 0
8182 amar.kumar 1029
                warehousesAvailability[whid][2] = 0
6540 rajveer 1030
 
5944 mandeep.dh 1031
    for warehouse in warehouses.values():
1032
        if not ignoreAvailability:
28621 amit.gupta 1033
            # TODO Mistake no entry for this warehouse.id in warehouseswithAvailab
6013 amar.kumar 1034
            if warehouse.id not in warehousesAvailability:
1035
                continue
1036
            entry = warehousesAvailability[warehouse.id]
1037
            if warehouse.billingWarehouseId in availabilityForBillingWarehouses:
1038
                if warehouse.billingWarehouseId is not None or warehouse.billingWarehouseId != 0: 
8182 amar.kumar 1039
                    availabilityForBillingWarehouses[warehouse.billingWarehouseId] = availabilityForBillingWarehouses[warehouse.billingWarehouseId] + entry[0] - entry[1] - entry[2]  
5944 mandeep.dh 1040
            else:
6013 amar.kumar 1041
                if warehouse.billingWarehouseId is not None or warehouse.billingWarehouseId != 0: 
8182 amar.kumar 1042
                    availabilityForBillingWarehouses[warehouse.billingWarehouseId] = entry[0] - entry[1] - entry[2]
1043
            if entry[0] <= (entry[1] + entry[2]):
5944 mandeep.dh 1044
                continue
8182 amar.kumar 1045
            total_availability += entry[0] - entry[1] - entry[2]
5944 mandeep.dh 1046
 
1047
        # Missing transfer price cases should not impact warehouse assignment
1048
        transferPrice = None
1049
        if item_pricing.has_key(warehouse.vendor_id):
6778 rajveer 1050
            transferPrice = item_pricing[warehouse.vendor_id].nlc
5944 mandeep.dh 1051
        if minTransferPrice is None or (transferPrice and minTransferPrice > transferPrice):
1052
            warehouse_retid = warehouse.id
6013 amar.kumar 1053
            billing_warehouse_retid = warehouse.billingWarehouseId
5944 mandeep.dh 1054
            minTransferPrice = transferPrice
6013 amar.kumar 1055
 
1056
    if billing_warehouse_retid in availabilityForBillingWarehouses: 
1057
        availability = availabilityForBillingWarehouses[billing_warehouse_retid]
1058
    else:
1059
        availability = total_availability
1060
 
24741 amit.gupta 1061
    print "[warehouse_retid, availability]", warehouse_retid, availability
6013 amar.kumar 1062
    return [warehouse_retid, availability]
5944 mandeep.dh 1063
 
28621 amit.gupta 1064
 
6540 rajveer 1065
def __get_warehouse_with_min_transfer_delay(warehouses, ignoredWhs, item_id, item_pricing):
5944 mandeep.dh 1066
    minTransferDelay = None
1067
    minTransferDelayWarehouses = {}
1068
    total_availability = 0
1069
 
28621 amit.gupta 1070
    for entry in CurrentInventorySnapshot.query.filter_by(item_id=item_id).all():
7242 amar.kumar 1071
        entry.reserved = max(entry.reserved, 0)
8524 amar.kumar 1072
        entry.held = max(entry.held, 0)
5944 mandeep.dh 1073
        if warehouses.has_key(entry.warehouse_id):
1074
            warehouse = warehouses[entry.warehouse_id]
28621 amit.gupta 1075
            # if entry.availability > entry.reserved:
6683 rajveer 1076
            if entry.warehouse_id not in ignoredWhs:
8182 amar.kumar 1077
                total_availability += entry.availability - entry.reserved - entry.held
1078
            if entry.availability - entry.reserved - entry.held <= 0:
6780 amar.kumar 1079
                continue
6013 amar.kumar 1080
            transferDelay = warehouse.transferDelayInHours
1081
            if minTransferDelay is None or minTransferDelay >= transferDelay:
1082
                if minTransferDelay != transferDelay:
1083
                    minTransferDelayWarehouses = {}
1084
                minTransferDelayWarehouses[warehouse.id] = warehouse
1085
                minTransferDelay = transferDelay
5944 mandeep.dh 1086
 
6540 rajveer 1087
    return [__get_warehouse_with_min_transfer_price(minTransferDelayWarehouses, ignoredWhs, item_id, item_pricing, False)[0], total_availability]
5944 mandeep.dh 1088
 
28621 amit.gupta 1089
 
5944 mandeep.dh 1090
def __get_warehouse_with_max_availability(warehouse_ids, item_id):
1091
    warehouse_retid = -1
1092
    max_availability = 0
1093
    total_availability = 0
1094
 
28621 amit.gupta 1095
    for entry in CurrentInventorySnapshot.query.filter_by(item_id=item_id).all():
7242 amar.kumar 1096
        entry.reserved = max(entry.reserved, 0)
8524 amar.kumar 1097
        entry.held = max(entry.held, 0)
5944 mandeep.dh 1098
        if entry.warehouse_id in warehouse_ids:
1099
            availability = entry.availability - entry.reserved
1100
            if availability > max_availability:
1101
                warehouse_retid = entry.warehouse_id
1102
                max_availability = availability
1103
            total_availability += availability
1104
 
1105
    return [warehouse_retid, total_availability]
1106
 
28621 amit.gupta 1107
 
8491 rajveer 1108
def __get_vendor_holiday_delay(vendor_id, expectedDelay):
28621 amit.gupta 1109
    # # If vendor is closed two days continuously
5944 mandeep.dh 1110
    holidayDelay = 0
8491 rajveer 1111
    currentDate = datetime.date.today()
28621 amit.gupta 1112
    expectedDate = currentDate + datetime.timedelta(days=expectedDelay)
8491 rajveer 1113
    holidays = VendorHolidays.query.filter(VendorHolidays.vendor_id == vendor_id).filter(VendorHolidays.date.between(currentDate, expectedDate)).all()
1114
    if holidays:
1115
        holidayDelay = holidayDelay + len(holidays)
5944 mandeep.dh 1116
    return holidayDelay 
1117
 
28621 amit.gupta 1118
 
5944 mandeep.dh 1119
def get_item_pricing(item_id, vendorId):
1120
    '''
1121
    if vendor id is -1 then we calculate an average transfer price to be populated
1122
    at the time of order creation. This will be later updated with actual transfer price
1123
    at the time of billing.
1124
    '''
1125
    if(vendorId == -1):
6778 rajveer 1126
        tp_total = 0
1127
        nlc_total = 0
5944 mandeep.dh 1128
        try:
1129
            item_pricings = []
1130
            item = __get_item_from_master(item_id)
1131
            if item.preferredVendor is not None:
1132
                item_pricing = VendorItemPricing.query.filter_by(item_id=item_id, vendor_id=item.preferredVendor).first()
1133
                if item_pricing:
1134
                    item_pricings.append(item_pricing)                    
28621 amit.gupta 1135
            else:
5944 mandeep.dh 1136
                item_pricings = VendorItemPricing.query.filter_by(item_id=item_id).all()
1137
            if item_pricings:
1138
                for item_pricing in item_pricings:
6778 rajveer 1139
                    tp_total += item_pricing.transfer_price
1140
                    nlc_total += item_pricing.nlc
1141
                tp_avg = tp_total / len(item_pricings)
1142
                nlc_avg = nlc_total / len(item_pricings)
1143
                item_pricing.transfer_price = tp_avg
1144
                item_pricing.nlc = nlc_avg
5944 mandeep.dh 1145
            else:
1146
                item_pricing = VendorItemPricing()
1147
                item_pricing.transfer_price = item.sellingPrice
6778 rajveer 1148
                item_pricing.nlc = item.sellingPrice
5944 mandeep.dh 1149
                vendor = Vendor()
1150
                vendor.id = vendorId
1151
                item_pricing.vendor = vendor
1152
                item_pricing.item_id = item_id
1153
 
1154
            return item_pricing
1155
        except:
1156
            raise InventoryServiceException(101, "Item pricing not found ")
1157
    vendor = Vendor.get_by(id=vendorId)    
1158
    try:
1159
        item_pricing = VendorItemPricing.query.filter_by(vendor=vendor, item_id=item_id).one()
1160
        return item_pricing
1161
    except MultipleResultsFound:
1162
        raise InventoryServiceException(110, "Multiple pricing information present for Vendor: " + vendor.name + " and Item: " + str(item_id))
1163
    except NoResultFound:
1164
        raise InventoryServiceException(111, "Missing pricing information for Vendor: " + vendor.name + " and Item: " + str(item_id))
1165
 
28621 amit.gupta 1166
 
5944 mandeep.dh 1167
def get_all_item_pricing(item_id):
1168
    item_pricing = VendorItemPricing.query.filter_by(item_id=item_id).all()
1169
    return item_pricing
28621 amit.gupta 1170
 
1171
 
10126 amar.kumar 1172
def get_all_vendor_item_pricing(item_id, vendor_id):
1173
    query = VendorItemPricing.query
1174
    if item_id:
28621 amit.gupta 1175
        query = query.filter_by(item_id=item_id)
10126 amar.kumar 1176
    if item_id:
28621 amit.gupta 1177
        query = query.filter_by(vendor_id=vendor_id)
10126 amar.kumar 1178
    item_pricing = query.all()
1179
    return item_pricing
28621 amit.gupta 1180
 
10126 amar.kumar 1181
 
5944 mandeep.dh 1182
def get_item_mappings(item_id):
1183
    item_mappings = VendorItemMapping.query.filter_by(item_id=item_id).all()
1184
    return item_mappings
1185
 
28621 amit.gupta 1186
 
1187
def add_vendor_pricing(vendorItemPricing, sendMail=True):
5944 mandeep.dh 1188
    if not vendorItemPricing:
1189
        raise InventoryServiceException(108, "Bad vendorItemPricing in request")
1190
    vendorId = vendorItemPricing.vendorId
1191
    itemId = vendorItemPricing.itemId
1192
 
1193
    try:
1194
        vendor = Vendor.query.filter_by(id=vendorId).one()
1195
    except:
1196
        raise InventoryServiceException(101, "Vendor not found for vendorId " + str(vendorId))
1197
 
1198
    try:
1199
        item = __get_item_from_master(itemId)
1200
    except:
1201
        raise InventoryServiceException(101, "Item not found for itemId " + str(itemId))
1202
 
1203
    validate_vendor_prices(item, vendorItemPricing)
1204
 
1205
    try:
28621 amit.gupta 1206
        ds_vendorItemPricing = VendorItemPricing.query.filter(and_(VendorItemPricing.vendor == vendor, VendorItemPricing.item_id == itemId)).one()
5944 mandeep.dh 1207
    except:
1208
        ds_vendorItemPricing = VendorItemPricing()
1209
        ds_vendorItemPricing.vendor = vendor
1210
        ds_vendorItemPricing.item_id = itemId
1211
 
1212
    subject = ""
1213
    message = ""
1214
    if vendorItemPricing.mop:
1215
        ds_vendorItemPricing.mop = vendorItemPricing.mop
1216
    if vendorItemPricing.dealerPrice:
1217
        ds_vendorItemPricing.dealerPrice = vendorItemPricing.dealerPrice
1218
    if vendorItemPricing.transferPrice:
28621 amit.gupta 1219
        # if vendorItemPricing.transferPrice != ds_vendorItemPricing.transfer_price:
26911 amit.gupta 1220
        #    client = CatalogClient("catalog_service_server_host_master", "catalog_service_server_port").get_client()
1221
        #    item = client.getItem(itemId)
1222
        #    message = "Transfer price for Item {0} {1} {2} {3} \nand Vendor:{4} is changed from {5} to {6}.".format(item.brand, item.modelName, item.modelNumber, item.color, vendor.name, ds_vendorItemPricing.transfer_price, vendorItemPricing.transferPrice)
1223
        #    subject = "Alert:Change in Transfer Price {0} {1} {2} {3} {4}".format(item.brand, item.modelName, item.modelNumber, item.color, itemId)
5944 mandeep.dh 1224
        ds_vendorItemPricing.transfer_price = vendorItemPricing.transferPrice
6751 amar.kumar 1225
    if vendorItemPricing.nlc:
28621 amit.gupta 1226
        # if vendorItemPricing.nlc != ds_vendorItemPricing.nlc:
26911 amit.gupta 1227
        #    client = CatalogClient("catalog_service_server_host_master", "catalog_service_server_port").get_client()
1228
        #    item = client.getItem(itemId)
1229
        #    message = message + "\nNLC for Item {0} {1} {2} {3} \nand Vendor:{4} is changed from {5} to {6}.".format(item.brand, item.modelName, item.modelNumber, item.color, vendor.name, ds_vendorItemPricing.nlc, vendorItemPricing.nlc)
1230
        #    subject = "Alert:Change in NLC {0} {1} {2} {3} {4}".format(item.brand, item.modelName, item.modelNumber, item.color, itemId)
6751 amar.kumar 1231
        ds_vendorItemPricing.nlc = vendorItemPricing.nlc
9895 vikram.rag 1232
    session.commit()    
28621 amit.gupta 1233
    # client = CatalogClient("catalog_service_server_host_staging", "catalog_service_server_port").get_client()
1234
    # client.updateNlcAtMarketplaces(itemId,vendorId,ds_vendorItemPricing.nlc)
1235
    # if subject and sendMail:
26911 amit.gupta 1236
    #    __send_mail(subject, message)
5944 mandeep.dh 1237
    return
1238
 
28621 amit.gupta 1239
 
5944 mandeep.dh 1240
def add_vendor_item_mapping(key, vendorItemMapping):
1241
    if not vendorItemMapping:
1242
        raise InventoryServiceException(108, "Bad vendorItemMapping in request")
1243
    vendorId = vendorItemMapping.vendorId
1244
    itemId = vendorItemMapping.itemId
1245
 
1246
    try:
1247
        vendor = Vendor.query.filter_by(id=vendorId).one()
1248
    except:
1249
        raise InventoryServiceException(101, "Vendor not found for vendorId " + str(vendorId))
1250
 
1251
    try:
28621 amit.gupta 1252
        ds_vendorItemMapping = VendorItemMapping.query.filter(and_(VendorItemMapping.vendor == vendor, VendorItemMapping.item_id == itemId, VendorItemMapping.item_key == key)).one()
5944 mandeep.dh 1253
    except:
1254
        ds_vendorItemMapping = VendorItemMapping()
1255
        ds_vendorItemMapping.vendor = vendor
1256
        ds_vendorItemMapping.item_id = itemId
1257
    ds_vendorItemMapping.item_key = vendorItemMapping.itemKey
1258
 
1259
    session.commit()
1260
 
1261
    # Marking the missed inventory as not ignored as the catalog dashboard user has updated their key
28621 amit.gupta 1262
    for missedInventoryUpdate in MissedInventoryUpdate.query.filter_by(itemKey=vendorItemMapping.itemKey).all():
5944 mandeep.dh 1263
        missedInventoryUpdate.isIgnored = 0
1264
    session.commit()
1265
 
1266
    return
1267
 
28621 amit.gupta 1268
 
5944 mandeep.dh 1269
def validate_vendor_prices(item, vendorPrices):
28621 amit.gupta 1270
    if item.mrp != None and item.mrp != "" and vendorPrices.mop != "" and item.mrp < vendorPrices.mop:
5944 mandeep.dh 1271
        print "[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(item.mrp), str(vendorPrices.mop), str(vendorPrices.vendorId))
1272
        raise InventoryServiceException(101, "[BAD MRP and MOP:] for {0} {1} {2} {3}. MRP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(item.mrp), str(vendorPrices.mop), str(vendorPrices.vendorId)))
18460 kshitij.so 1273
    return
28621 amit.gupta 1274
    # if vendorPrices.mop != "" and vendorPrices.transferPrice != "" and vendorPrices.transferPrice > vendorPrices.mop:
18460 kshitij.so 1275
#         print "[BAD MOP and TP:] for {0} {1} {2} {3}. TP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(vendorPrices.transferPrice), str(vendorPrices.mop), str(vendorPrices.vendorId))
1276
#         raise InventoryServiceException(101, "[BAD MOP and TP:] for {0} {1} {2} {3}. TP={4}. MOP={5}, Vendor={6}".format(item.productGroup, item.brand, item.modelNumber, item.color, str(vendorPrices.transferPrice), str(vendorPrices.mop), str(vendorPrices.vendorId)))
28621 amit.gupta 1277
    # return 
5944 mandeep.dh 1278
 
28621 amit.gupta 1279
 
5944 mandeep.dh 1280
def get_all_vendors():
1281
    return Vendor.query.all()
1282
 
28621 amit.gupta 1283
 
5944 mandeep.dh 1284
def get_pending_orders_inventory(vendor_id=1):
1285
    """
1286
    Returns a list of inventory stock for items for which there are pending orders.
1287
    """
1288
 
35732 amit 1289
    warehouse_ids = [wh.id for wh in _warehouse_cache.values() if wh.vendor_id == vendor_id]
5944 mandeep.dh 1290
    pending_items_inventory = []
1291
    if warehouse_ids:
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()
1293
    return pending_items_inventory
1294
 
28621 amit.gupta 1295
 
7149 amar.kumar 1296
def get_billable_inventory_and_pending_orders():
1297
    """
1298
    Returns a list of inventory Availability and Reserved Count for items which either have real inventory
1299
    or have pending orders.
1300
    """
1301
 
35732 amit 1302
    warehouse_ids = [wh.id for wh in _warehouse_cache.values() if wh.isAvailabilityMonitored and (wh.inventoryType == 'GOOD' or wh.warehouseType == 'OURS')]
7149 amar.kumar 1303
    items_inventory = []
1304
    reserved_items_inventory = []
1305
    available_items_inventory = []
1306
    if warehouse_ids:
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()
1308
        available_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.availability) > 0).all()
1309
 
1310
    items_inventory.extend(reserved_items_inventory)
1311
    items_inventory.extend(available_items_inventory)
1312
    return items_inventory
1313
 
1314
 
5944 mandeep.dh 1315
def close_session():
35718 amit 1316
    session.remove()
5944 mandeep.dh 1317
 
28621 amit.gupta 1318
 
5944 mandeep.dh 1319
def is_alive():
1320
    try:
1321
        session.query(Vendor.id).limit(1).one()
1322
        return True
1323
    except:
1324
        return False
1325
 
28621 amit.gupta 1326
 
5944 mandeep.dh 1327
def add_vendor(vendor):
1328
    if not vendor:
1329
        raise InventoryServiceException(108, "Bad vendor")
1330
    if get_vendor(vendor.id):
28621 amit.gupta 1331
        # vendor is already present.
5944 mandeep.dh 1332
        raise InventoryServiceException(101, "Vendor already present")
1333
 
1334
    ds_vendor = Vendor()
1335
    ds_vendor.id = vendor.id
1336
    ds_vendor.name = vendor.name
1337
    session.commit()
1338
    return ds_vendor.id
1339
 
28621 amit.gupta 1340
 
5944 mandeep.dh 1341
def add_warehouse_vendor_mapping(warehouse_id, VendorId):
1342
    return True
1343
 
28621 amit.gupta 1344
 
5944 mandeep.dh 1345
def mark_missed_inventory_updates_as_processed(itemKey, warehouseId):
28621 amit.gupta 1346
    MissedInventoryUpdate.query.filter_by(itemKey=itemKey, warehouseId=warehouseId).delete()
5944 mandeep.dh 1347
    session.commit()
1348
 
28621 amit.gupta 1349
 
5944 mandeep.dh 1350
def get_item_keys_to_be_processed(warehouseId):
28621 amit.gupta 1351
    return [i.itemKey for i in MissedInventoryUpdate.query.filter_by(warehouseId=warehouseId, isIgnored=0)]
5944 mandeep.dh 1352
 
28621 amit.gupta 1353
 
5944 mandeep.dh 1354
def reset_availability(itemKey, vendorId, quantity, warehouseId):
28621 amit.gupta 1355
    vendorItemMapping = VendorItemMapping.get_by(vendor_id=vendorId, item_key=itemKey)
5944 mandeep.dh 1356
    if vendorItemMapping:
1357
        itemId = vendorItemMapping.item_id
1358
 
1359
        if skippedItems.has_key(warehouseId) and itemId in skippedItems[warehouseId]:
1360
            quantity = 0
1361
 
28621 amit.gupta 1362
        currentInventorySnapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
5944 mandeep.dh 1363
        if currentInventorySnapshot:
1364
            currentInventorySnapshot.availability = quantity
5978 rajveer 1365
            clear_item_availability_cache(itemId) 
5944 mandeep.dh 1366
        else:
1367
            add_inventory(itemId, warehouseId, quantity)
1368
 
1369
    else:
1370
        raise InventoryServiceException(101, 'VendorMapping not found for: ' + itemKey)
1371
    session.commit()
1372
 
28621 amit.gupta 1373
 
5944 mandeep.dh 1374
def reset_availability_for_warehouse(warehouseId):
13149 manish.sha 1375
    itemIds = []
5944 mandeep.dh 1376
    for currentInventorySnapshot in CurrentInventorySnapshot.query.filter_by(warehouse_id=warehouseId).all():
1377
        currentInventorySnapshot.availability = 0
13149 manish.sha 1378
        itemIds.append(currentInventorySnapshot.item_id)
1379
    clear_item_availability_cache(itemIds) 
5944 mandeep.dh 1380
    session.commit()
1381
 
28621 amit.gupta 1382
 
7718 amar.kumar 1383
def get_our_warehouse_id_for_vendor(vendor_id, billing_warehouse_id):
6467 amar.kumar 1384
    try:
35732 amit 1385
        for wh in _warehouse_cache.values():
1386
            if wh.vendor_id == vendor_id and wh.warehouseType == 'OURS' and wh.inventoryType == 'GOOD' and wh.billingWarehouseId == billing_warehouse_id:
1387
                return wh.id
1388
        raise InventoryServiceException(101, 'No our warehouse found for vendorId: ' + str(vendor_id))
6467 amar.kumar 1389
    except Exception as e:
1390
        print e;
7755 amar.kumar 1391
        raise InventoryServiceException(101, 'No our warehouse found for vendorId: ' + str(vendor_id))
28621 amit.gupta 1392
 
5944 mandeep.dh 1393
 
1394
def __send_mail(subject, message):
1395
    try:
6029 rajveer 1396
        thread = threading.Thread(target=partial(mail, mail_user, mail_password, to_addresses, subject, message))
5944 mandeep.dh 1397
        thread.start()
1398
    except Exception as ex:
1399
        print ex    
1400
 
28621 amit.gupta 1401
 
5944 mandeep.dh 1402
def get_shipping_locations():
1403
    shippingLocationIds = {}
35732 amit 1404
    for warehouse in _warehouse_cache.values():
5944 mandeep.dh 1405
        if warehouse.shippingWarehouseId:
1406
            shippingLocationIds[warehouse.shippingWarehouseId] = 1
1407
 
1408
    shippingLocations = []
1409
    for shippingLocationId in shippingLocationIds:
1410
        shippingLocations.append(get_Warehouse(shippingLocationId))
1411
 
1412
    return shippingLocations
1413
 
28621 amit.gupta 1414
 
5944 mandeep.dh 1415
def get_inventory_snapshot(warehouseId):
1416
    query = CurrentInventorySnapshot.query
1417
 
1418
    if warehouseId:
28621 amit.gupta 1419
        query = query.filter_by(warehouse_id=warehouseId)
5944 mandeep.dh 1420
 
1421
    itemInventoryMap = {}
1422
    for row in query.all():
1423
        if not itemInventoryMap.has_key(row.item_id):
1424
            itemInventoryMap[row.item_id] = []
1425
 
1426
        itemInventoryMap[row.item_id].append(row)
1427
 
1428
    return itemInventoryMap
1429
 
28621 amit.gupta 1430
 
5944 mandeep.dh 1431
def update_vendor_string(warehouseId, vendorString):
1432
    warehouse = get_Warehouse(warehouseId)
1433
    warehouse.vendorString = vendorString
1434
    session.commit()
1435
 
28621 amit.gupta 1436
 
5944 mandeep.dh 1437
def __get_item_from_master(item_id):
1438
    client = CatalogClient("catalog_service_server_host_master", "catalog_service_server_port").get_client()
5978 rajveer 1439
    return client.getItem(item_id)
1440
 
28621 amit.gupta 1441
 
5978 rajveer 1442
def __check_risky_item(item_id, source_id):
28621 amit.gupta 1443
    # # We should get the list of strings which will identify to the catalog servers
5978 rajveer 1444
    if source_id == 1:
1445
        client = CatalogClient("catalog_service_server_host_master", "catalog_service_server_port").get_client()
1446
        client.validateRiskyStatus(item_id)
1447
    if source_id == 2:
1448
        client = CatalogClient("catalog_service_server_host_hotspot", "catalog_service_server_port").get_client()
1449
        client.validateRiskyStatus(item_id)
1450
 
28621 amit.gupta 1451
 
5978 rajveer 1452
def __get_item_from_source(item_id, source_id):
1453
    if source_id == 1:
1454
        client = CatalogClient("catalog_service_server_host_master", "catalog_service_server_port").get_client()
1455
        return client.getItem(item_id)
1456
    if source_id == 2:
1457
        client = CatalogClient("catalog_service_server_host_hotspot", "catalog_service_server_port").get_client()
6531 vikram.rag 1458
        return client.getItem(item_id)
1459
 
28621 amit.gupta 1460
 
6531 vikram.rag 1461
def get_monitored_warehouses_for_vendors(vendorIds):
1462
    w = []
35732 amit 1463
    for wh in _warehouse_cache.values():
1464
        if wh.isAvailabilityMonitored and wh.vendor_id in (vendorIds):
1465
            w.append(wh.id)
6531 vikram.rag 1466
    return w
28621 amit.gupta 1467
 
1468
 
6531 vikram.rag 1469
def get_ignored_warehouseids_and_itemids():
1470
    iw = []
1471
    for i in IgnoredInventoryUpdateItems.query.all():
1472
        iw.append(to_t_itemidwarehouseid(i)) 
1473
    return iw
28621 amit.gupta 1474
 
1475
 
1476
def insert_item_to_ignore_inventory_update_list(item_id, warehouse_id):
6531 vikram.rag 1477
    try:
28621 amit.gupta 1478
        ds_warehouse = IgnoredInventoryUpdateItems()
1479
        ds_warehouse.item_id = item_id
1480
        ds_warehouse.warehouse_id = warehouse_id
6532 amit.gupta 1481
        clear_item_availability_cache(item_id)
6531 vikram.rag 1482
        session.commit()
1483
        return True
1484
    except:
1485
        return False       
28621 amit.gupta 1486
 
1487
 
1488
def delete_item_from_ignore_inventory_update_list(item_id, warehouse_id):
6531 vikram.rag 1489
    try:
28621 amit.gupta 1490
        session.query(IgnoredInventoryUpdateItems).filter_by(item_id=item_id, warehouse_id=warehouse_id).delete()
6532 amit.gupta 1491
        clear_item_availability_cache(item_id)
6531 vikram.rag 1492
        session.commit()
1493
        return True
1494
    except:
1495
        return False           
28621 amit.gupta 1496
 
6531 vikram.rag 1497
 
1498
def get_all_ignored_inventoryupdate_items_count():
1499
    return  session.query(func.count(distinct(IgnoredInventoryUpdateItems.item_id))).scalar()
1500
 
28621 amit.gupta 1501
 
1502
def get_ignored_inventoryupdate_itemids(offset=0, limit=None):
6531 vikram.rag 1503
    itemIds = session.query(distinct(IgnoredInventoryUpdateItems.item_id))
1504
    '''if limit is not None:
1505
        itemIds = itemIds.limit(limit)'''
1506
    print itemIds.all()
28621 amit.gupta 1507
    return [id for (id,) in itemIds.all()]
6821 amar.kumar 1508
 
28621 amit.gupta 1509
 
6821 amar.kumar 1510
def update_item_stock_purchase_params(item_id, numOfDaysStock, minStockLevel):
1511
    if numOfDaysStock is None or minStockLevel is None:
1512
        raise InventoryServiceException(108, "Bad params : numOfDaysStock = " + str(numOfDaysStock) + "minStockLevel = " + str(minStockLevel))
28621 amit.gupta 1513
    itemStockPurchaseParams = ItemStockPurchaseParams.query.filter_by(item_id=item_id).first()
6821 amar.kumar 1514
    if itemStockPurchaseParams is None:
1515
        itemStockPurchaseParams = ItemStockPurchaseParams()
1516
    itemStockPurchaseParams.item_id = item_id
1517
    itemStockPurchaseParams.numOfDaysStock = numOfDaysStock
1518
    itemStockPurchaseParams.minStockLevel = minStockLevel
1519
    session.commit()
1520
 
28621 amit.gupta 1521
 
6821 amar.kumar 1522
def get_item_stock_purchase_params(item_id):
28621 amit.gupta 1523
    return ItemStockPurchaseParams.query.filter_by(item_id=item_id).first()
6821 amar.kumar 1524
 
28621 amit.gupta 1525
 
6821 amar.kumar 1526
def add_oos_status_for_item(oosStatusMap, date):
1527
 
1528
    oosDate = to_py_date(date)
1529
    oosDate.replace(second=0, microsecond=0)
1530
 
28621 amit.gupta 1531
    cartAdditionStartDate = oosDate - datetime.timedelta(days=1)
6821 amar.kumar 1532
 
1533
    client = TransactionClient().get_client()
1534
 
28621 amit.gupta 1535
    # Gets physical orders in the last day
6821 amar.kumar 1536
    orders = client.getPhysicalOrders(to_java_date(cartAdditionStartDate), to_java_date(oosDate))
8019 amar.kumar 1537
    rtoOrders = client.getAllOrders([20], 0, 0, 0)
9665 rajveer 1538
    orderCountByItemIdSourceId = {}
1539
    rtoOrderCountByItemIdSourceId = {}
1540
 
6821 amar.kumar 1541
    for order in orders:
9665 rajveer 1542
        if not orderCountByItemIdSourceId.has_key(order.lineitems[0].item_id):
1543
            orderCountByItemIdSourceId[order.lineitems[0].item_id] = {}
1544
 
1545
        if orderCountByItemIdSourceId[order.lineitems[0].item_id].has_key(order.source):
18430 manish.sha 1546
            orderCountByItemIdSourceId[order.lineitems[0].item_id][order.source] = orderCountByItemIdSourceId[order.lineitems[0].item_id][order.source] + order.lineitems[0].quantity
6821 amar.kumar 1547
        else:
18430 manish.sha 1548
            orderCountByItemIdSourceId[order.lineitems[0].item_id][order.source] = order.lineitems[0].quantity
8019 amar.kumar 1549
 
1550
    for order in rtoOrders:
9665 rajveer 1551
        if not rtoOrderCountByItemIdSourceId.has_key(order.lineitems[0].item_id):
1552
            rtoOrderCountByItemIdSourceId[order.lineitems[0].item_id] = {}
1553
 
1554
        if rtoOrderCountByItemIdSourceId[order.lineitems[0].item_id].has_key(order.source):
18430 manish.sha 1555
            rtoOrderCountByItemIdSourceId[order.lineitems[0].item_id][order.source] = rtoOrderCountByItemIdSourceId[order.lineitems[0].item_id][order.source] + order.lineitems[0].quantity 
8019 amar.kumar 1556
        else:
18430 manish.sha 1557
            rtoOrderCountByItemIdSourceId[order.lineitems[0].item_id][order.source] = order.lineitems[0].quantity
9665 rajveer 1558
 
6821 amar.kumar 1559
    for itemId, status in oosStatusMap.iteritems():
9665 rajveer 1560
        total_order_count = 0 
9791 rajveer 1561
        total_rto_count = 0
28621 amit.gupta 1562
        for sid in (1, 3, 4, 6, 7, 8):
6821 amar.kumar 1563
            oosStatus = OOSStatus()
1564
            oosStatus.item_id = itemId
1565
            oosStatus.date = oosDate
28621 amit.gupta 1566
            oosStatus.sourceId = sid
9791 rajveer 1567
            order_count = 0
1568
            rto_count = 0
1569
            if orderCountByItemIdSourceId.has_key(itemId) and orderCountByItemIdSourceId[itemId].has_key(sid):
1570
                order_count = orderCountByItemIdSourceId[itemId][sid]
1571
            if rtoOrderCountByItemIdSourceId.has_key(itemId) and rtoOrderCountByItemIdSourceId[itemId].has_key(sid):
18430 manish.sha 1572
                rto_count = rtoOrderCountByItemIdSourceId[itemId][sid]
9791 rajveer 1573
            oosStatus.num_orders = order_count
1574
            oosStatus.rto_orders = rto_count
9666 rajveer 1575
            oosStatus.is_oos = status
9791 rajveer 1576
            if oosStatus.is_oos and order_count > 0:
1577
                oosStatus.is_oos = False
1578
            total_order_count = total_order_count + order_count
1579
            total_rto_count = total_rto_count + rto_count
1580
        oosStatus = OOSStatus()
1581
        oosStatus.item_id = itemId
1582
        oosStatus.date = oosDate
28621 amit.gupta 1583
        oosStatus.sourceId = 0
9791 rajveer 1584
        oosStatus.num_orders = total_order_count
1585
        oosStatus.rto_orders = total_rto_count
9804 rajveer 1586
        if itemId in orderCountByItemIdSourceId and 1 in orderCountByItemIdSourceId[itemId]:
1587
            order_count = orderCountByItemIdSourceId[itemId][1]
9791 rajveer 1588
        oosStatus.is_oos = status
1589
        if oosStatus.is_oos and order_count > 0:
1590
            oosStatus.is_oos = False
6821 amar.kumar 1591
 
9791 rajveer 1592
        session.commit()
1593
 
9861 rajveer 1594
    itemCountMap = {}
28621 amit.gupta 1595
    oosDate = oosDate - datetime.timedelta(days=1) - datetime.timedelta(hours=1)
1596
    lines = session.query(OOSStatus.item_id, func.sum(OOSStatus.num_orders) / func.count(OOSStatus.num_orders)).filter(OOSStatus.date >= oosDate).filter(OOSStatus.sourceId == 1).filter(OOSStatus.is_oos == 0).group_by(OOSStatus.item_id).all()
9861 rajveer 1597
    for line in lines:
1598
        item_id = line[0]
28621 amit.gupta 1599
        quantity = int(math.ceil(max(1, 2 * line[1])))
9861 rajveer 1600
        itemCountMap[item_id] = quantity
28621 amit.gupta 1601
    cl = CatalogClient('catalog_service_server_host_prod', 'catalog_service_server_port').get_client()
9861 rajveer 1602
    cl.updateItemHoldInventory(itemCountMap)
28621 amit.gupta 1603
 
9791 rajveer 1604
 
9665 rajveer 1605
def get_oos_statuses_for_x_days_for_item(itemId, sourceId, days):
6832 amar.kumar 1606
    timestamp = datetime.datetime.now()
28621 amit.gupta 1607
    timestamp = timestamp - datetime.timedelta(days=days)
1608
    return OOSStatus.query.filter_by(item_id=itemId).filter_by(sourceId=sourceId).filter(OOSStatus.date > timestamp).all()
6857 amar.kumar 1609
 
28621 amit.gupta 1610
 
10126 amar.kumar 1611
def get_oos_statuses_for_x_days(sourceId, days):
1612
    timestamp = datetime.datetime.now()
28621 amit.gupta 1613
    timestamp = timestamp - datetime.timedelta(days=days)
10126 amar.kumar 1614
    if sourceId == -1:
1615
        return OOSStatus.query.filter(OOSStatus.date > timestamp).all()
1616
    else:
28621 amit.gupta 1617
        return OOSStatus.query.filter_by(sourceId=sourceId).filter(OOSStatus.date > timestamp).all()
10126 amar.kumar 1618
 
28621 amit.gupta 1619
 
6857 amar.kumar 1620
def get_non_zero_item_stock_purchase_params():
28621 amit.gupta 1621
    return ItemStockPurchaseParams.query.filter(or_("numOfDaysStock!=0", "minStockLevel!=0"))
7281 kshitij.so 1622
 
28621 amit.gupta 1623
 
7972 amar.kumar 1624
def get_last_n_day_sale_for_item(itemId, numberOfDays):
1625
    lastNdaySale = ""
9685 rajveer 1626
    oosStatuses = get_oos_statuses_for_x_days_for_item(itemId, 0, numberOfDays)
7972 amar.kumar 1627
    for oosStatus in oosStatuses:
1628
        if oosStatus.is_oos == True:
28621 amit.gupta 1629
            lastNdaySale += "X-"
7972 amar.kumar 1630
        else:
28621 amit.gupta 1631
            lastNdaySale += str(oosStatus.num_orders) + "-"
7972 amar.kumar 1632
    return lastNdaySale[:-1] 
1633
 
28621 amit.gupta 1634
 
7281 kshitij.so 1635
def get_warehouse_name(warehouseId):
28621 amit.gupta 1636
    row = Warehouse.get_by(id=warehouseId)
7281 kshitij.so 1637
    return row.displayName
1638
 
28621 amit.gupta 1639
 
7281 kshitij.so 1640
def get_amazon_inventory_for_item(amazonItemId):
1641
    inventory = AmazonInventorySnapshot.get_by(item_id=amazonItemId)
1642
    return inventory
1643
 
28621 amit.gupta 1644
 
7281 kshitij.so 1645
def get_all_amazon_inventory():
1646
    return session.query(AmazonInventorySnapshot).all()
1647
 
28621 amit.gupta 1648
 
1649
def add_or_update_amazon_inventory_for_item(amazoninventorysnapshot, time):
1650
    inventory = AmazonInventorySnapshot.get_by(item_id=amazoninventorysnapshot.item_id)
7281 kshitij.so 1651
    if inventory is None:
1652
        amazon_inventory = AmazonInventorySnapshot()
1653
        amazon_inventory.item_id = amazoninventorysnapshot.item_id
1654
        amazon_inventory.availability = amazoninventorysnapshot.availability
1655
        amazon_inventory.reserved = amazoninventorysnapshot.reserved
10450 vikram.rag 1656
        amazon_inventory.is_oos = amazoninventorysnapshot.is_oos
1657
        if time != 0:
1658
            amazon_inventory.lastUpdatedOnAmazon = to_py_date(time)
1659
    else: 
7281 kshitij.so 1660
        inventory.availability = amazoninventorysnapshot.availability
1661
        inventory.reserved = amazoninventorysnapshot.reserved
10450 vikram.rag 1662
        if not inventory.is_oos and (to_py_date(time) - inventory.lastUpdatedOnAmazon).days == 0:
1663
            pass
1664
        else:
1665
            inventory.is_oos = amazoninventorysnapshot.is_oos
28621 amit.gupta 1666
        if time != 0: 
10450 vikram.rag 1667
            inventory.lastUpdatedOnAmazon = to_py_date(time)      
7281 kshitij.so 1668
    session.commit()
28621 amit.gupta 1669
 
7281 kshitij.so 1670
 
8182 amar.kumar 1671
def add_update_hold_inventory(itemId, warehouseId, holdQuantity, source):
28621 amit.gupta 1672
    if holdQuantity < 0:
9762 amar.kumar 1673
        print "Negative holdQuantity : " + str(holdQuantity) + " is not allowed"
1674
        raise InventoryServiceException(108, "Negative heldQuantity is not allowed")
28621 amit.gupta 1675
    hold_inventory_detail = HoldInventoryDetail.get_by(item_id=itemId, warehouse_id=warehouseId, source=source)
8197 amar.kumar 1676
    if  hold_inventory_detail is None:
8182 amar.kumar 1677
        diffTobeAddedInCIS = holdQuantity
1678
        hold_inventory_detail = HoldInventoryDetail()
1679
        hold_inventory_detail.item_id = itemId 
1680
        hold_inventory_detail.warehouse_id = warehouseId 
1681
        hold_inventory_detail.held = holdQuantity 
1682
        hold_inventory_detail.source = source
1683
    else:
8497 amar.kumar 1684
        diffTobeAddedInCIS = holdQuantity - hold_inventory_detail.held
8182 amar.kumar 1685
        hold_inventory_detail.held = holdQuantity
1686
 
1687
    current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
1688
    if not current_inventory_snapshot:
1689
        current_inventory_snapshot = CurrentInventorySnapshot()
1690
        current_inventory_snapshot.item_id = itemId
1691
        current_inventory_snapshot.warehouse_id = warehouseId
1692
        current_inventory_snapshot.availability = 0
1693
        current_inventory_snapshot.reserved = 0
1694
        current_inventory_snapshot.held = 0
1695
    current_inventory_snapshot.held = current_inventory_snapshot.held + diffTobeAddedInCIS
1696
    session.commit()
28621 amit.gupta 1697
    # **Update item availability cache**#
8182 amar.kumar 1698
    clear_item_availability_cache(itemId)
28621 amit.gupta 1699
 
8282 kshitij.so 1700
 
1701
def add_or_update_amazon_fba_inventory(amazonfbainventorysnapshot):
28621 amit.gupta 1702
    inventory = AmazonFbaInventorySnapshot.query.filter_by(item_id=amazonfbainventorysnapshot.item_id, location=amazonfbainventorysnapshot.location).first()
8282 kshitij.so 1703
    if inventory is None:
1704
        amazon_fba_inventory = AmazonFbaInventorySnapshot()
1705
        amazon_fba_inventory.item_id = amazonfbainventorysnapshot.item_id
1706
        amazon_fba_inventory.availability = amazonfbainventorysnapshot.availability
11173 vikram.rag 1707
        amazon_fba_inventory.location = amazonfbainventorysnapshot.location
1708
        amazon_fba_inventory.reserved = amazonfbainventorysnapshot.reserved
1709
        amazon_fba_inventory.inbound = amazonfbainventorysnapshot.inbound
1710
        amazon_fba_inventory.unfulfillable = amazonfbainventorysnapshot.unfulfillable
1711
 
8282 kshitij.so 1712
    else:
11173 vikram.rag 1713
        print 'updating'
8282 kshitij.so 1714
        inventory.availability = amazonfbainventorysnapshot.availability
11173 vikram.rag 1715
        inventory.location = amazonfbainventorysnapshot.location
1716
        inventory.reserved = amazonfbainventorysnapshot.reserved
1717
        inventory.inbound = amazonfbainventorysnapshot.inbound
1718
        inventory.unfulfillable = amazonfbainventorysnapshot.unfulfillable
8282 kshitij.so 1719
 
1720
 
1721
def get_amazon_fba_inventory(itemId):
28621 amit.gupta 1722
    return AmazonFbaInventorySnapshot.query.filter_by(item_id=itemId)
8282 kshitij.so 1723
 
28621 amit.gupta 1724
 
8363 vikram.rag 1725
def get_all_amazon_fba_inventory():
1726
    return AmazonFbaInventorySnapshot.query.all() 
1727
 
28621 amit.gupta 1728
 
12799 manish.sha 1729
def get_oursgood_warehouseids_for_location(stateId):
28621 amit.gupta 1730
    warehouseId = []
1731
    x = session.query(Warehouse.id).filter(Warehouse.id == Warehouse.billingWarehouseId).filter(Warehouse.warehouseType == 'OURS').filter(Warehouse.state_id == stateId).all()
8363 vikram.rag 1732
    for id in x:
1733
        warehouseId.append(id[0])
28621 amit.gupta 1734
    return session.query(Warehouse.id).filter(Warehouse.inventoryType == 'GOOD').filter(Warehouse.warehouseType == 'OURS').filter(Warehouse.billingWarehouseId.in_(warehouseId)).all()
8954 vikram.rag 1735
 
28621 amit.gupta 1736
 
1737
def get_holdinventorydetail_forItem_forWarehouseId_exceptsource(item_id, warehouse_id, source):
8954 vikram.rag 1738
    holddetails = HoldInventoryDetail.query.filter(HoldInventoryDetail.item_id == item_id).all()
1739
    print holddetails
1740
    hold = 0
1741
    for holddetail in holddetails:
28621 amit.gupta 1742
        if holddetail.source != source and holddetail.warehouse_id == warehouse_id:
8954 vikram.rag 1743
            hold = hold + holddetail.held
9404 vikram.rag 1744
    return hold
1745
 
28621 amit.gupta 1746
 
9404 vikram.rag 1747
def get_snapdeal_inventory_for_item(id):
28621 amit.gupta 1748
    print SnapdealInventorySnapshot.get_by(item_id=id)
1749
    return SnapdealInventorySnapshot.get_by(item_id=id)
9404 vikram.rag 1750
 
28621 amit.gupta 1751
 
9404 vikram.rag 1752
def add_or_update_snapdeal_inventor_for_item(snapdealinventoryitem):
28621 amit.gupta 1753
    snapdeal_inventory_item = SnapdealInventorySnapshot.get_by(item_id=snapdealinventoryitem.item_id)
9404 vikram.rag 1754
    if snapdeal_inventory_item is None:
1755
        snapdeal_inventory_item = SnapdealInventorySnapshot()
1756
        snapdeal_inventory_item.item_id = snapdealinventoryitem.item_id
1757
        snapdeal_inventory_item.availability = snapdealinventoryitem.availability
9495 vikram.rag 1758
        snapdeal_inventory_item.pendingOrders = snapdealinventoryitem.pendingOrders
9404 vikram.rag 1759
        snapdeal_inventory_item.lastUpdatedOnSnapdeal = to_py_date(snapdealinventoryitem.lastUpdatedOnSnapdeal)
10450 vikram.rag 1760
        snapdeal_inventory_item.is_oos = snapdealinventoryitem.is_oos
9404 vikram.rag 1761
    else:
1762
        snapdeal_inventory_item.availability = snapdealinventoryitem.availability
9495 vikram.rag 1763
        snapdeal_inventory_item.pendingOrders = snapdealinventoryitem.pendingOrders
10450 vikram.rag 1764
        if not snapdeal_inventory_item.is_oos and (to_py_date(snapdealinventoryitem.lastUpdatedOnSnapdeal) - snapdeal_inventory_item.lastUpdatedOnSnapdeal).days == 0:
1765
            pass
1766
        else:
1767
            snapdeal_inventory_item.is_oos = snapdealinventoryitem.is_oos
9404 vikram.rag 1768
        snapdeal_inventory_item.lastUpdatedOnSnapdeal = to_py_date(snapdealinventoryitem.lastUpdatedOnSnapdeal)
1769
    session.commit()
28621 amit.gupta 1770
 
8954 vikram.rag 1771
 
28621 amit.gupta 1772
def get_nlc_for_warehouse(warehouse_id, itemid):
9404 vikram.rag 1773
    warehouse = Warehouse.get_by(id=warehouse_id)
1774
    if warehouse is None:
1775
        return 0
1776
    vendoritempricing = VendorItemPricing.query.filter_by(item_id=itemid, vendor_id=warehouse.vendor_id).first()
1777
    '''vendoritempricing = VendorItemPricing.get_by(id=warehouse.vendor_id,item_id=itemid)'''
1778
    if vendoritempricing is None:
1779
        return 0
9456 vikram.rag 1780
    return vendoritempricing.nlc
1781
 
28621 amit.gupta 1782
 
9495 vikram.rag 1783
def get_snapdeal_inventory_snapshot():
9640 amar.kumar 1784
    return SnapdealInventorySnapshot.query.all()
10050 vikram.rag 1785
 
28621 amit.gupta 1786
 
9640 amar.kumar 1787
def get_held_inventory_map_for_item(itemId, warehouseId):
1788
    heldInventoryMap = {}
28621 amit.gupta 1789
    holdInventories = HoldInventoryDetail.query.filter_by(item_id=itemId, warehouse_id=warehouseId).all()
9640 amar.kumar 1790
    for holdInventory in holdInventories:
1791
        heldInventoryMap[holdInventory.source] = holdInventory.held
1792
    return heldInventoryMap 
10050 vikram.rag 1793
 
28621 amit.gupta 1794
 
9761 amar.kumar 1795
def get_hold_inventory_details(itemId, warehouseId, source):
1796
    heldInventoryQuery = HoldInventoryDetail.query
1797
    if itemId:
28621 amit.gupta 1798
        heldInventoryQuery = heldInventoryQuery.filter_by(item_id=itemId)
9761 amar.kumar 1799
    if warehouseId:
28621 amit.gupta 1800
        heldInventoryQuery = heldInventoryQuery.filter_by(warehouse_id=warehouseId)
9761 amar.kumar 1801
    if source:
28621 amit.gupta 1802
        heldInventoryQuery = heldInventoryQuery.filter_by(source=source)
9761 amar.kumar 1803
    holdInventoryDetails = heldInventoryQuery.all()
1804
    return holdInventoryDetails
28621 amit.gupta 1805
 
9896 rajveer 1806
 
28621 amit.gupta 1807
def add_or_update_flipkart_inventory_snapshot(flipkartInventorySnapshot, time):
10050 vikram.rag 1808
    for snapshot in flipkartInventorySnapshot:
28621 amit.gupta 1809
        flipkart_inventory = FlipkartInventorySnapshot.get_by(item_id=snapshot.item_id)
10050 vikram.rag 1810
        if flipkart_inventory is None:
1811
            flipkart_inventory = FlipkartInventorySnapshot()
1812
            flipkart_inventory.item_id = snapshot.item_id
1813
            flipkart_inventory.availability = snapshot.availability
1814
            flipkart_inventory.createdOrders = snapshot.createdOrders
1815
            flipkart_inventory.heldOrders = snapshot.heldOrders
10450 vikram.rag 1816
            flipkart_inventory.is_oos = snapshot.is_oos
1817
            flipkart_inventory.lastUpdatedOnFlipkart = to_py_date(time) 
10050 vikram.rag 1818
        else:
1819
            flipkart_inventory.availability = snapshot.availability
1820
            flipkart_inventory.createdOrders = snapshot.createdOrders
1821
            flipkart_inventory.heldOrders = snapshot.heldOrders
10450 vikram.rag 1822
            if not flipkart_inventory.is_oos and (to_py_date(time) - flipkart_inventory.lastUpdatedOnFlipkart).days == 0:
1823
                pass
1824
            else:
1825
                flipkart_inventory.is_oos = snapshot.is_oos
1826
            flipkart_inventory.lastUpdatedOnFlipkart = to_py_date(time) 
10050 vikram.rag 1827
    session.commit()
28621 amit.gupta 1828
 
10050 vikram.rag 1829
 
1830
def get_flipkart_inventory_snapshot():
1831
    return FlipkartInventorySnapshot.query.all()     
28621 amit.gupta 1832
 
10050 vikram.rag 1833
 
10097 kshitij.so 1834
def get_flipkart_inventory_for_Item(itemId):
28621 amit.gupta 1835
    return FlipkartInventorySnapshot.get_by(item_id=itemId)
10485 vikram.rag 1836
 
28621 amit.gupta 1837
 
10485 vikram.rag 1838
def get_state_master():
1839
    stateIdNameMap = {} 
1840
    statemaster = StateMaster.query.all()
1841
    for state in statemaster:
12280 amit.gupta 1842
        stateIdNameMap[state.id] = to_t_state(state)
10544 vikram.rag 1843
    return stateIdNameMap    
1844
 
28621 amit.gupta 1845
 
10544 vikram.rag 1846
def update_snapdeal_stock_at_eod(allsnapdealstock):
1847
    for stockitem in allsnapdealstock:
1848
        snapdealstockateod = SnapdealStockAtEOD()
1849
        snapdealstockateod.item_id = stockitem.item_id
1850
        snapdealstockateod.availability = stockitem.availability
28621 amit.gupta 1851
        snapdealstockateod.date = to_py_date(stockitem.date)
10544 vikram.rag 1852
    session.commit()
28621 amit.gupta 1853
 
10544 vikram.rag 1854
 
1855
def update_flipkart_stock_at_eod(allflipkartstock):
1856
    for stockitem in allflipkartstock:
1857
        snapdealstockateod = FlipkartStockAtEOD()
1858
        snapdealstockateod.item_id = stockitem.item_id
1859
        snapdealstockateod.availability = stockitem.availability
28621 amit.gupta 1860
        snapdealstockateod.date = to_py_date(stockitem.date)
10544 vikram.rag 1861
    session.commit()
28621 amit.gupta 1862
 
10687 rajveer 1863
 
28621 amit.gupta 1864
def get_wanlc_for_source(item_id, sourceId):
1865
    stockWanlc = StockWeightedNlcInfo.query.filter(StockWeightedNlcInfo.itemId == item_id).filter(StockWeightedNlcInfo.source == sourceId).order_by(desc(StockWeightedNlcInfo.updatedTimestamp)).limit(1).all()
1866
    if stockWanlc is None or len(stockWanlc) == 0:
12363 kshitij.so 1867
        return 0.0
1868
    else:
1869
        return stockWanlc[0].avgWeightedNlc
28621 amit.gupta 1870
 
12363 kshitij.so 1871
 
1872
def get_all_available_amazon_inventory():
28621 amit.gupta 1873
    return AmazonFbaInventorySnapshot.query.filter(AmazonFbaInventorySnapshot.availability > 0).all()
19247 kshitij.so 1874
 
28621 amit.gupta 1875
 
19247 kshitij.so 1876
def add_vendor_item_pricing_in_bulk(vendorItemPricingList):
1877
    exceptionItems = []
1878
    for vendorItemPricing in vendorItemPricingList:
1879
        try:
1880
            add_vendor_pricing(vendorItemPricing, False)
1881
        except:
1882
            exceptionItems.append(vendorItemPricing.itemId)
1883
    return exceptionItems
1884
 
28621 amit.gupta 1885
 
19247 kshitij.so 1886
def add_inventory_in_bulk(bulkInventoryList):
1887
    for item in bulkInventoryList:
19989 kshitij.so 1888
        add_inventory(item.item_id, item.warehouse_id, item.inventory, True)
23316 amit.gupta 1889
 
28621 amit.gupta 1890
# from shop2020.clients.TransactionClient import TransactionClient
1891
# from shop2020.thriftpy.model.v1.order.ttypes import OrderType, OrderSource, TransactionStatus
1892
# from shop2020.thriftpy.logistics.ttypes import LocationInfo, PickUpType
1893
# tc = TransactionClient().get_client()
1894
# tc.changeTransactionStatus(1037182, TransactionStatus.COD_IN_PROCESS, 'Cod Payment Awaited', PickUpType.COURIER, OrderType.B2C, OrderSource.WEBSITE)