Subversion Repositories SmartDukaan

Rev

Rev 5964 | Rev 5978 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5964 Rev 5966
Line 8... Line 8...
8
from shop2020.clients.TransactionClient import TransactionClient
8
from shop2020.clients.TransactionClient import TransactionClient
9
from shop2020.model.v1.inventory.impl import DataService
9
from shop2020.model.v1.inventory.impl import DataService
10
from shop2020.model.v1.inventory.impl.DataService import Warehouse, \
10
from shop2020.model.v1.inventory.impl.DataService import Warehouse, \
11
    ItemInventoryHistory, CurrentInventorySnapshot, VendorItemPricing, \
11
    ItemInventoryHistory, CurrentInventorySnapshot, VendorItemPricing, \
12
    VendorItemMapping, Vendor, MissedInventoryUpdate, BadInventorySnapshot, \
12
    VendorItemMapping, Vendor, MissedInventoryUpdate, BadInventorySnapshot, \
13
    VendorItemProcurementDelay, VendorHolidays, ItemAvailabilityCache
13
    VendorItemProcurementDelay, VendorHolidays, ItemAvailabilityCache,\
-
 
14
    CurrentReservationSnapshot
14
from shop2020.thriftpy.model.v1.inventory.ttypes import InventoryServiceException, \
15
from shop2020.thriftpy.model.v1.inventory.ttypes import InventoryServiceException, \
15
    HolidayType, InventoryType, WarehouseType
16
    HolidayType, InventoryType, WarehouseType
16
from shop2020.thriftpy.model.v1.order.ttypes import AlertType
17
from shop2020.thriftpy.model.v1.order.ttypes import AlertType
17
from shop2020.utils import EmailAttachmentSender
18
from shop2020.utils import EmailAttachmentSender
18
from shop2020.utils.EmailAttachmentSender import mail
19
from shop2020.utils.EmailAttachmentSender import mail
Line 257... Line 258...
257
    all_inventory = CurrentInventorySnapshot.query.filter_by(item_id = item_id).all()
258
    all_inventory = CurrentInventorySnapshot.query.filter_by(item_id = item_id).all()
258
    reserved = 0
259
    reserved = 0
259
    for currInv in all_inventory:
260
    for currInv in all_inventory:
260
        reserved = reserved + currInv.reserved
261
        reserved = reserved + currInv.reserved
261
    return reserved
262
    return reserved
-
 
263
 
-
 
264
def __get_item_availability_at_warehouse(warehouse_id, item_id):
-
 
265
    inventory = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id).one()
-
 
266
    return inventory.availability
-
 
267
 
-
 
268
def is_order_billable(item_id, warehouse_id, source_id, order_id):
-
 
269
    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()
-
 
270
    availability = __get_item_availability_at_warehouse(warehouse_id, item_id)
-
 
271
    for reservation in reservations:
-
 
272
        availability = availability - reservation.reserved
-
 
273
        if reservation.order_id == order_id and reservation.source_id == source_id:
-
 
274
            break
-
 
275
    if availability < 0:
-
 
276
        return False
-
 
277
    return True
262
    
278
    
263
def reserve_item_in_warehouse(item_id, warehouse_id, quantity):    
279
def reserve_item_in_warehouse(item_id, warehouse_id, source_id, order_id, created_timestamp, promised_shipping_timestamp, quantity):    
264
    if not warehouse_id:
280
    if not warehouse_id:
265
        raise InventoryServiceException(101, "bad warehouse_id")
281
        raise InventoryServiceException(101, "bad warehouse_id")
266
        
282
        
267
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
283
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
268
    try:
284
    try:
Line 273... Line 289...
273
        current_inventory_snapshot.item_id = item_id
289
        current_inventory_snapshot.item_id = item_id
274
        current_inventory_snapshot.availability = 0
290
        current_inventory_snapshot.availability = 0
275
        current_inventory_snapshot.reserved = 0
291
        current_inventory_snapshot.reserved = 0
276
        
292
        
277
    current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
293
    current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
-
 
294
    
-
 
295
    reservation = CurrentReservationSnapshot()
-
 
296
    reservation.item_id = item_id
-
 
297
    reservation.warehouse_id = warehouse_id
-
 
298
    reservation.source_id = source_id
-
 
299
    reservation.order_id = order_id
-
 
300
    reservation.created_timestamp = created_timestamp
-
 
301
    reservation.promised_shipping_timestamp = promised_shipping_timestamp
-
 
302
    reservation.reserved = quantity
-
 
303
    
278
    session.commit()
304
    session.commit()
279
    #**Update item availability cache**#
305
    #**Update item availability cache**#
280
    __update_item_availability_cache(item_id)
306
    __update_item_availability_cache(item_id)
281
    __check_risky_item(item_id)
307
    __check_risky_item(item_id)
282
    return True
308
    return True
283
 
309
 
284
def reduce_reservation_count(item_id, warehouse_id, quantity):
310
def reduce_reservation_count(item_id, warehouse_id, source_id, order_id, quantity):
285
    if not warehouse_id:
311
    if not warehouse_id:
286
        raise InventoryServiceException(101, "bad warehouse_id")
312
        raise InventoryServiceException(101, "bad warehouse_id")
287
        
313
        
288
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
314
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id)
289
    try:
315
    try:
290
        current_inventory_snapshot = query.one()
316
        current_inventory_snapshot = query.one()
291
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
317
        current_inventory_snapshot.reserved = current_inventory_snapshot.reserved - quantity
-
 
318
        
-
 
319
        reservation = CurrentReservationSnapshot.query.filter_by(warehouse_id = warehouse_id, item_id = item_id, source_id = source_id, order_id = order_id).one()
-
 
320
        if reservation.reserved == quantity:
-
 
321
            reservation.delete()
-
 
322
        else:
-
 
323
            reservation.reserved -= quantity
292
        session.commit()
324
        session.commit()
293
        #**Update item availability cache**#
325
        #**Update item availability cache**#
294
        __update_item_availability_cache(item_id)
326
        __update_item_availability_cache(item_id)
295
        __check_risky_item(item_id)
327
        __check_risky_item(item_id)
296
        if current_inventory_snapshot.reserved < 0:
328
        if current_inventory_snapshot.reserved < 0: