Exemplo n.º 1
0
def partial_inventory_check(inventory):
    """ Given the inventory items, fix the current system stocks to match the physical quantities """

    inventory_items = db(
        db.inventory_item.id_inventory == inventory.id).iterselect()

    for inventory_item in inventory_items:
        diff = inventory_item.system_qty - inventory_item.physical_qty
        # more system stock than actual physical stock (missing items)
        if diff > 0:
            _remove_stocks(inventory_item.id_item,
                           diff,
                           request.now,
                           inventory_item=inventory_item)

        # more physical stock than system stock
        elif diff < 0:
            # avg item purchase_price
            avg = db.stock_item.price.avg()
            avg_item_price = 0
            last_inventory = db(
                db.inventory.id != inventory.id).select().last()
            if last_inventory:
                # the average price is based on the items obtained after the last inventory
                last_inventory_date = last_inventory.modified_on
                avg_item_price = db(
                    (db.stock_item.id_store == session.store)
                    & (db.stock_item.id_item == inventory_item.id_item.id)
                    & (db.stock_item.created_on > last_inventory_date)).select(
                        avg).first()[avg]
            else:
                # the average price is based on all the obtained items
                avg_item_price = db(
                    (db.stock_item.id_store == session.store)
                    & (db.stock_item.id_item == inventory_item.id_item.id)
                ).select(avg).first()[avg] or 0
            #  add items to an inventory stock
            db.stock_item.insert(
                id_item=inventory_item.id_item.id,
                purchase_qty=DQ(abs(diff)),
                stock_qty=DQ(abs(diff)),
                price=avg_item_price,
                taxes=0  # we do not consider taxes
                ,
                id_inventory=inventory_item.id_inventory.id,
                id_store=session.store)
    return inventory_items
Exemplo n.º 2
0
def partial_inventory_check(inventory):
    """ Given the inventory items, fix the current system stocks to match the physical quantities """

    inventory_items = db(db.inventory_item.id_inventory == inventory.id).iterselect()

    for inventory_item in inventory_items:
        diff = inventory_item.system_qty - inventory_item.physical_qty
        # more system stock than actual physical stock (missing items)
        if diff > 0:
            item_utils._remove_stocks(
                inventory_item.id_item, diff, request.now,
                inventory_item=inventory_item
            )

        # more physical stock than system stock
        elif diff < 0:
            # avg item purchase_price
            avg = db.stock_item.price.avg()
            avg_item_price = 0
            last_inventory = db(db.inventory.id != inventory.id).select().last()
            if last_inventory:
                # the average price is based on the items obtained after the last inventory
                last_inventory_date = last_inventory.modified_on
                avg_item_price = db(
                    (db.stock_item.id_store == session.store)
                    & (db.stock_item.id_item == inventory_item.id_item.id)
                    & (db.stock_item.created_on > last_inventory_date)
                ).select(avg).first()[avg]
            else:
                # the average price is based on all the obtained items
                avg_item_price = db(
                    (db.stock_item.id_store == session.store)
                    & (db.stock_item.id_item == inventory_item.id_item.id)
                ).select(avg).first()[avg] or 0
            #  add items to an inventory stock
            db.stock_item.insert(
                id_item=inventory_item.id_item.id
                , purchase_qty=DQ(abs(diff))
                , stock_qty=DQ(abs(diff))
                , price=avg_item_price
                , taxes=0 # we do not consider taxes
                , id_inventory=inventory_item.id_inventory.id
                , id_store=session.store
            )
    return inventory_items
Exemplo n.º 3
0
def full_inventory_check(inventory):
    """ Checks all the inventory items and report items that were missed after the inventory
    """

    partial_inventory_check(inventory)

    inventory_items = db(
        db.inventory_item.id_inventory == inventory.id
    )._select(db.inventory_item.id_item)

    # get the items with stock in the current store and were not reported in the inventory
    missing_items = db(
        (db.stock_item.id_store == session.store) &
        (db.stock_item.stock_qty > 0) &
        (~db.stock_item.id_item.belongs(inventory_items))
    ).iterselect(db.stock_item.id_item, groupby=db.stock_item.id_item)

    has_missing_items = False

    for stock_item in missing_items:
        has_missing_items = True

        missing_item = db.item(stock_item.id_item)

        quantity = item_stock_qty(missing_item, session.store)

        new_inventory_item_id = db.inventory_item(db.inventory_item.insert(
            id_inventory=inventory.id
            , id_item=missing_item.id
            , system_qty=quantity
            , physical_qty=0
            , is_missing=True
        ))

        item_utils._remove_stocks(
            missing_item, quantity, request.now,
            inventory_item=new_inventory_item_id
        )

    return has_missing_items