Subversion Repositories SmartDukaan

Rev

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

Rev 5421 Rev 5424
Line 86... Line 86...
86
        t_item_shipping_info.isRisky = item.risky
86
        t_item_shipping_info.isRisky = item.risky
87
        warehouse_id = get_item_availability_for_location(item.id)[0]
87
        warehouse_id = get_item_availability_for_location(item.id)[0]
88
        if item.risky and item.status == status.ACTIVE:
88
        if item.risky and item.status == status.ACTIVE:
89
            availability = 0
89
            availability = 0
90
            currentInventorySnapshot = CurrentInventorySnapshot.query.filter_by(item_id = item.id, warehouse_id = warehouse_id).all()
90
            currentInventorySnapshot = CurrentInventorySnapshot.query.filter_by(item_id = item.id, warehouse_id = warehouse_id).all()
91
            if not currentInventorySnapshot or currentInventorySnapshot[0].availibility <= currentInventorySnapshot[0].reserved:
91
            if not currentInventorySnapshot or currentInventorySnapshot[0].availability <= currentInventorySnapshot[0].reserved:
92
                add_status_change_log(item, status.PAUSED_BY_RISK)
92
                add_status_change_log(item, status.PAUSED_BY_RISK)
93
                item.status = status.PAUSED_BY_RISK
93
                item.status = status.PAUSED_BY_RISK
94
                item.status_description = "This item is currently out of stock"
94
                item.status_description = "This item is currently out of stock"
95
                session.commit()
95
                session.commit()
96
                __send_mail_for_oos_item(item)
96
                __send_mail_for_oos_item(item)
97
                #This will clear cache from tomcat
97
                #This will clear cache from tomcat
98
                __clear_homepage_cache()
98
                __clear_homepage_cache()
99
            else:
99
            else:
100
                availability = currentInventorySnapshot[0].availibility
100
                availability = currentInventorySnapshot[0].availability
101
        else:
101
        else:
102
            availability = item.get_total_inventory
102
            availability = item.get_total_inventory
103
        t_item_shipping_info.isActive = (item.status == status.ACTIVE)
103
        t_item_shipping_info.isActive = (item.status == status.ACTIVE)
104
        t_item_shipping_info.quantity = availability
104
        t_item_shipping_info.quantity = availability
105
    except InventoryServiceException:
105
    except InventoryServiceException:
Line 376... Line 376...
376
        try:
376
        try:
377
            item_inventory_history = ItemInventoryHistory()
377
            item_inventory_history = ItemInventoryHistory()
378
            item_inventory_history.warehouse = warehouse
378
            item_inventory_history.warehouse = warehouse
379
            item_inventory_history.item = item
379
            item_inventory_history.item = item
380
            item_inventory_history.timestamp = time
380
            item_inventory_history.timestamp = time
381
            item_inventory_history.availibility = quantity
381
            item_inventory_history.availability = quantity
382
        except:
382
        except:
383
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
383
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
384
    session.commit()
384
    session.commit()
385
    
385
    
386
def update_inventory(warehouse_id, timestamp, availability):
386
def update_inventory(warehouse_id, timestamp, availability):
Line 405... Line 405...
405
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
405
            current_inventory_snapshot = CurrentInventorySnapshot.get_by(item=item, warehouse=warehouse)
406
            if not current_inventory_snapshot:
406
            if not current_inventory_snapshot:
407
                current_inventory_snapshot = CurrentInventorySnapshot()
407
                current_inventory_snapshot = CurrentInventorySnapshot()
408
                current_inventory_snapshot.item = item
408
                current_inventory_snapshot.item = item
409
                current_inventory_snapshot.warehouse = warehouse
409
                current_inventory_snapshot.warehouse = warehouse
410
                current_inventory_snapshot.availibility = 0
410
                current_inventory_snapshot.availability = 0
411
                current_inventory_snapshot.reserved = 0
411
                current_inventory_snapshot.reserved = 0
412
            # added the difference in the current inventory    
412
            # added the difference in the current inventory    
413
            current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
413
            current_inventory_snapshot.availability = current_inventory_snapshot.availability + quantity
414
            try:
414
            try:
415
                if quantity > 0 and __get_item_reserved(item) > 0:
415
                if quantity > 0 and __get_item_reserved(item) > 0:
416
                    cl = TransactionClient().get_client()
416
                    cl = TransactionClient().get_client()
417
                    #FIXME hardcoding for warehouse id 
417
                    #FIXME hardcoding for warehouse id 
418
                    cl.addAlert(AlertType.NEW_INVENTORY_ALERT, 5, "Inventory received for item " + item.brand + " " + item.model_name + " " + item.model_number + " " +  item.color)
418
                    cl.addAlert(AlertType.NEW_INVENTORY_ALERT, 5, "Inventory received for item " + item.brand + " " + item.model_name + " " + item.model_number + " " +  item.color)
419
            except:
419
            except:
420
                print "Not able to raise alert for incoming inventory" 
420
                print "Not able to raise alert for incoming inventory" 
421
            if current_inventory_snapshot.availibility < 0:
421
            if current_inventory_snapshot.availability < 0:
422
                __send_alert_for_negative_availability(item, current_inventory_snapshot.availibility, warehouse)
422
                __send_alert_for_negative_availability(item, current_inventory_snapshot.availability, warehouse)
423
        except:
423
        except:
424
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
424
            raise InventoryServiceException(108, "Some unforeseen error while updating inventory")
425
        session.commit() 
425
        session.commit() 
426
        #**Update item availability cache**#
426
        #**Update item availability cache**#
427
        __update_item_availability_cache(item.id)
427
        __update_item_availability_cache(item.id)
Line 455... Line 455...
455
    current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
455
    current_inventory_snapshot = CurrentInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
456
    if not current_inventory_snapshot:
456
    if not current_inventory_snapshot:
457
        current_inventory_snapshot = CurrentInventorySnapshot()
457
        current_inventory_snapshot = CurrentInventorySnapshot()
458
        current_inventory_snapshot.item_id = itemId
458
        current_inventory_snapshot.item_id = itemId
459
        current_inventory_snapshot.warehouse_id = warehouseId
459
        current_inventory_snapshot.warehouse_id = warehouseId
460
        current_inventory_snapshot.availibility = 0
460
        current_inventory_snapshot.availability = 0
461
        current_inventory_snapshot.reserved = 0
461
        current_inventory_snapshot.reserved = 0
462
    # added the difference in the current inventory    
462
    # added the difference in the current inventory    
463
    current_inventory_snapshot.availibility = current_inventory_snapshot.availibility + quantity
463
    current_inventory_snapshot.availability = current_inventory_snapshot.availability + quantity
464
    session.commit()
464
    session.commit()
465
    #**Update item availability cache**#
465
    #**Update item availability cache**#
466
    __update_item_availability_cache(itemId)
466
    __update_item_availability_cache(itemId)
467
    if current_inventory_snapshot.availibility < 0:
467
    if current_inventory_snapshot.availability < 0:
468
        __send_alert_for_negative_availability(get_item(itemId), current_inventory_snapshot.availibility, get_Warehouse(warehouseId))
468
        __send_alert_for_negative_availability(get_item(itemId), current_inventory_snapshot.availability, get_Warehouse(warehouseId))
469
    check_risky_item(get_item(itemId)) 
469
    check_risky_item(get_item(itemId)) 
470
 
470
 
471
def add_bad_inventory(itemId, warehouseId, quantity):
471
def add_bad_inventory(itemId, warehouseId, quantity):
472
    bad_inventory_snapshot = BadInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
472
    bad_inventory_snapshot = BadInventorySnapshot.get_by(item_id=itemId, warehouse_id=warehouseId)
473
    if not bad_inventory_snapshot:
473
    if not bad_inventory_snapshot:
Line 581... Line 581...
581
        
581
        
582
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
582
    query = CurrentInventorySnapshot.query.filter_by(warehouse_id = warehouse_id)
583
    query = query.filter_by(item_id = item.id)
583
    query = query.filter_by(item_id = item.id)
584
    try:
584
    try:
585
        current_inventory_snapshot = query.one()
585
        current_inventory_snapshot = query.one()
586
        return current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
586
        return current_inventory_snapshot.availability - current_inventory_snapshot.reserved
587
    except:
587
    except:
588
        return 0
588
        return 0
589
    """
589
    """
590
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
590
    current_inventory_snapshot = CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id == warehouse_id, CurrentInventorySnapshot.item_id == item_id).one()
591
    if not current_inventory_snapshot:
591
    if not current_inventory_snapshot:
592
        return 0
592
        return 0
593
    else:
593
    else:
594
        return current_inventory_snapshot.availibility
594
        return current_inventory_snapshot.availability
595
    """
595
    """
596
    
596
    
597
def check_risky_item(item):
597
def check_risky_item(item):
598
    if not item.risky:
598
    if not item.risky:
599
        return
599
        return
600
    warehouse_id = get_item_availability_for_location(item.id)[0]
600
    warehouse_id = get_item_availability_for_location(item.id)[0]
601
    currentInventorySnapshot = CurrentInventorySnapshot.query.filter_by(item_id = item.id, warehouse_id = warehouse_id).all()
601
    currentInventorySnapshot = CurrentInventorySnapshot.query.filter_by(item_id = item.id, warehouse_id = warehouse_id).all()
602
    if not currentInventorySnapshot or currentInventorySnapshot[0].availibility <= currentInventorySnapshot[0].reserved:
602
    if not currentInventorySnapshot or currentInventorySnapshot[0].availability <= currentInventorySnapshot[0].reserved:
603
        if item.status == status.ACTIVE:
603
        if item.status == status.ACTIVE:
604
            change_item_status(item.id, status.PAUSED_BY_RISK)
604
            change_item_status(item.id, status.PAUSED_BY_RISK)
605
            __send_mail_for_oos_item(item)
605
            __send_mail_for_oos_item(item)
606
    else:
606
    else:
607
        if item.status == status.PAUSED_BY_RISK:
607
        if item.status == status.PAUSED_BY_RISK:
Line 616... Line 616...
616
    if warehouse_ids is None:
616
    if warehouse_ids is None:
617
        all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
617
        all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
618
        availability = 0
618
        availability = 0
619
        reserved = 0
619
        reserved = 0
620
        for currInv in all_inventory:
620
        for currInv in all_inventory:
621
            availability = availability + currInv.availibility
621
            availability = availability + currInv.availability
622
            reserved = reserved + currInv.reserved
622
            reserved = reserved + currInv.reserved
623
        return availability - reserved
623
        return availability - reserved
624
    else:
624
    else:
625
        total_availability = 0
625
        total_availability = 0
626
        for current_inventory_snapshot in CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).filter_by(item_id = item.id).all():
626
        for current_inventory_snapshot in CurrentInventorySnapshot.query.filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).filter_by(item_id = item.id).all():
627
            total_availability += current_inventory_snapshot.availibility - current_inventory_snapshot.reserved
627
            total_availability += current_inventory_snapshot.availability - current_inventory_snapshot.reserved
628
        return total_availability 
628
        return total_availability 
629
 
629
 
630
def __get_item_reserved(item):
630
def __get_item_reserved(item):
631
    all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
631
    all_inventory = CurrentInventorySnapshot.query.filter_by(item = item).all()
632
    reserved = 0
632
    reserved = 0
Line 646... Line 646...
646
        current_inventory_snapshot = query.one()
646
        current_inventory_snapshot = query.one()
647
    except:
647
    except:
648
        current_inventory_snapshot = CurrentInventorySnapshot()
648
        current_inventory_snapshot = CurrentInventorySnapshot()
649
        current_inventory_snapshot.warehouse_id = warehouse_id
649
        current_inventory_snapshot.warehouse_id = warehouse_id
650
        current_inventory_snapshot.item_id = item_id
650
        current_inventory_snapshot.item_id = item_id
651
        current_inventory_snapshot.availibility = 0
651
        current_inventory_snapshot.availability = 0
652
        current_inventory_snapshot.reserved = 0
652
        current_inventory_snapshot.reserved = 0
653
        
653
        
654
    current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
654
    current_inventory_snapshot.reserved = current_inventory_snapshot.reserved + quantity
655
    session.commit()
655
    session.commit()
656
    #**Update item availability cache**#
656
    #**Update item availability cache**#
Line 831... Line 831...
831
    warehousesWithAvailability = {}
831
    warehousesWithAvailability = {}
832
    
832
    
833
    if not ignoreAvailability:
833
    if not ignoreAvailability:
834
        if item.currentInventory:
834
        if item.currentInventory:
835
            for entry in item.currentInventory:
835
            for entry in item.currentInventory:
836
                if entry.availibility > entry.reserved:
836
                if entry.availability > entry.reserved:
837
                    warehousesWithAvailability[entry.warehouse_id] = entry
837
                    warehousesWithAvailability[entry.warehouse_id] = entry
838
 
838
 
839
    for warehouse in warehouses.values():
839
    for warehouse in warehouses.values():
840
        if not ignoreAvailability:
840
        if not ignoreAvailability:
841
            if warehousesWithAvailability.has_key(warehouse.id):
841
            if warehousesWithAvailability.has_key(warehouse.id):
842
                entry = warehousesWithAvailability[warehouse.id]
842
                entry = warehousesWithAvailability[warehouse.id]
843
                total_availability += entry.availibility - entry.reserved
843
                total_availability += entry.availability - entry.reserved
844
            else:
844
            else:
845
                continue
845
                continue
846
 
846
 
847
        # Missing transfer price cases should not impact warehouse assignment
847
        # Missing transfer price cases should not impact warehouse assignment
848
        transferPrice = None
848
        transferPrice = None
Line 861... Line 861...
861
 
861
 
862
    if item.currentInventory:
862
    if item.currentInventory:
863
        for entry in item.currentInventory:
863
        for entry in item.currentInventory:
864
            if warehouses.has_key(entry.warehouse_id):
864
            if warehouses.has_key(entry.warehouse_id):
865
                warehouse = warehouses[entry.warehouse_id]
865
                warehouse = warehouses[entry.warehouse_id]
866
                if entry.availibility > entry.reserved:
866
                if entry.availability > entry.reserved:
867
                    total_availability += entry.availibility - entry.reserved
867
                    total_availability += entry.availability - entry.reserved
868
                    transferDelay = warehouse.transferDelayInHours
868
                    transferDelay = warehouse.transferDelayInHours
869
                    if minTransferDelay is None or minTransferDelay >= transferDelay:
869
                    if minTransferDelay is None or minTransferDelay >= transferDelay:
870
                        if minTransferDelay != transferDelay:
870
                        if minTransferDelay != transferDelay:
871
                            minTransferDelayWarehouses = {}
871
                            minTransferDelayWarehouses = {}
872
                        minTransferDelayWarehouses[warehouse.id] = warehouse
872
                        minTransferDelayWarehouses[warehouse.id] = warehouse
Line 880... Line 880...
880
    total_availability = 0
880
    total_availability = 0
881
 
881
 
882
    if item.currentInventory:
882
    if item.currentInventory:
883
        for entry in item.currentInventory:
883
        for entry in item.currentInventory:
884
            if entry.warehouse_id in warehouse_ids:
884
            if entry.warehouse_id in warehouse_ids:
885
                availability = entry.availibility - entry.reserved
885
                availability = entry.availability - entry.reserved
886
                if availability > max_availability:
886
                if availability > max_availability:
887
                    warehouse_retid = entry.warehouse_id
887
                    warehouse_retid = entry.warehouse_id
888
                    max_availability = availability
888
                    max_availability = availability
889
                total_availability += availability
889
                total_availability += availability
890
 
890
 
Line 1740... Line 1740...
1740
    """
1740
    """
1741
    
1741
    
1742
    warehouse_ids = [warehouse.id for warehouse in Warehouse.query.filter_by(vendor_id = vendor_id)]
1742
    warehouse_ids = [warehouse.id for warehouse in Warehouse.query.filter_by(vendor_id = vendor_id)]
1743
    pending_items_inventory = []
1743
    pending_items_inventory = []
1744
    if warehouse_ids:
1744
    if warehouse_ids:
1745
        pending_items_inventory = session.query(CurrentInventorySnapshot.item_id, func.sum(CurrentInventorySnapshot.availibility), func.sum(CurrentInventorySnapshot.reserved)).filter(CurrentInventorySnapshot.warehouse_id.in_(warehouse_ids)).group_by(CurrentInventorySnapshot.item_id).having(func.sum(CurrentInventorySnapshot.reserved) > 0).all()
1745
        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()
1746
    return pending_items_inventory
1746
    return pending_items_inventory
1747
 
1747
 
1748
def get_product_notifications(start_datetime):
1748
def get_product_notifications(start_datetime):
1749
    '''
1749
    '''
1750
    Returns a list of Product Notification objects each representing user requests for notification
1750
    Returns a list of Product Notification objects each representing user requests for notification
Line 1825... Line 1825...
1825
    vendorItemMapping = VendorItemMapping.get_by(vendor_id = vendorId, item_key = itemKey)
1825
    vendorItemMapping = VendorItemMapping.get_by(vendor_id = vendorId, item_key = itemKey)
1826
    if vendorItemMapping:
1826
    if vendorItemMapping:
1827
        itemId = vendorItemMapping.item_id
1827
        itemId = vendorItemMapping.item_id
1828
        currentInventorySnapshot = CurrentInventorySnapshot.get_by(item_id = itemId, warehouse_id = warehouseId)
1828
        currentInventorySnapshot = CurrentInventorySnapshot.get_by(item_id = itemId, warehouse_id = warehouseId)
1829
        if currentInventorySnapshot:
1829
        if currentInventorySnapshot:
1830
            currentInventorySnapshot.availibility = quantity
1830
            currentInventorySnapshot.availability = quantity
1831
        else:
1831
        else:
1832
            add_inventory(itemId, warehouseId, quantity)
1832
            add_inventory(itemId, warehouseId, quantity)
1833
    else:
1833
    else:
1834
        raise InventoryServiceException(101, 'VendorMapping not found for: ' + itemKey)
1834
        raise InventoryServiceException(101, 'VendorMapping not found for: ' + itemKey)
1835
    session.commit()
1835
    session.commit()